-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathupgrade.py
1643 lines (1379 loc) · 67.5 KB
/
upgrade.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
This file contains the upgrade logic for Nebari.
Each release of Nebari requires an upgrade step class (which is a child class of UpgradeStep) to be created.
When a user runs `nebari upgrade -c nebari-config.yaml`, then the do_upgrade function will then run through all required upgrade steps to bring the config file up to date with the current version of Nebari.
"""
import json
import logging
import os
import re
import secrets
import string
import textwrap
from abc import ABC
from pathlib import Path
from typing import Any, ClassVar, Dict
import kubernetes.client
import kubernetes.config
import requests
import rich
from packaging.version import Version
from pydantic import ValidationError
from rich.prompt import Confirm, Prompt
from typing_extensions import override
from _nebari.config import backup_configuration
from _nebari.keycloak import get_keycloak_admin
from _nebari.stages.infrastructure import (
provider_enum_default_node_groups_map,
provider_enum_name_map,
)
from _nebari.utils import (
get_k8s_version_prefix,
get_provider_config_block_name,
load_yaml,
yaml,
)
from _nebari.version import __version__, rounded_ver_parse
from nebari.schema import ProviderEnum, is_version_accepted
logger = logging.getLogger(__name__)
NEBARI_WORKFLOW_CONTROLLER_DOCS = (
"https://www.nebari.dev/docs/how-tos/using-argo/#jupyterflow-override-beta"
)
ARGO_JUPYTER_SCHEDULER_REPO = "https://github.com/nebari-dev/argo-jupyter-scheduler"
UPGRADE_KUBERNETES_MESSAGE = "Please see the [green][link=https://www.nebari.dev/docs/how-tos/kubernetes-version-upgrade]Kubernetes upgrade docs[/link][/green] for more information."
DESTRUCTIVE_UPGRADE_WARNING = "-> This version upgrade will result in your cluster being completely torn down and redeployed. Please ensure you have backed up any data you wish to keep before proceeding!!!"
TERRAFORM_REMOVE_TERRAFORM_STAGE_FILES_CONFIRMATION = (
"Nebari needs to generate an updated set of Terraform scripts for your deployment and delete the old scripts.\n"
"Do you want Nebari to remove your [green]stages[/green] directory automatically for you? It will be recreated the next time Nebari is run.\n"
"[red]Warning:[/red] This will remove everything in the [green]stages[/green] directory.\n"
"If you do not have Nebari do it automatically here, you will need to remove the [green]stages[/green] manually with a command"
"like [green]rm -rf stages[/green]."
)
DESTROY_STAGE_FILES_WITH_TF_STATE_NOT_REMOTE = (
"⚠️ CAUTION ⚠️\n"
"Nebari would like to remove your old Terraform/Opentofu [green]stages[/green] files. Your [blue]terraform_state[/blue] configuration is not set to [blue]remote[/blue], so destroying your [green]stages[/green] files could potentially be very detructive.\n"
"If you don't have active Terraform/Opentofu deployment state files contained within your [green]stages[/green] directory, you may proceed by entering [red]y[/red] at the prompt."
"If you have an active Terraform/Opentofu deployment with active state files in your [green]stages[/green] folder, you will need to either bring Nebari down temporarily to redeploy or pursue some other means to upgrade. Enter [red]n[/red] at the prompt.\n\n"
"Do you want to proceed by deleting your [green]stages[/green] directory and everything in it? ([red]POTENTIALLY VERY DESTRUCTIVE[/red])"
)
def do_upgrade(config_filename, attempt_fixes=False):
"""
Perform an upgrade of the Nebari configuration file.
This function loads the YAML configuration file, checks for deprecated keys,
validates the current version, and if necessary, upgrades the configuration
to the latest version of Nebari.
Args:
config_filename (str): The path to the configuration file.
attempt_fixes (bool): Whether to attempt automatic fixes for validation errors.
Returns:
None
"""
config = load_yaml(config_filename)
if config.get("qhub_version"):
rich.print(
f"Your config file [purple]{config_filename}[/purple] uses the deprecated qhub_version key. Please change qhub_version to nebari_version and re-run the upgrade command."
)
return
try:
from nebari.plugins import nebari_plugin_manager
nebari_plugin_manager.read_config(config_filename)
rich.print(
f"Your config file [purple]{config_filename}[/purple] appears to be already up-to-date for Nebari version [green]{__version__}[/green]"
)
return
except (ValidationError, ValueError) as e:
if is_version_accepted(config.get("nebari_version", "")):
# There is an unrelated validation problem
rich.print(
f"Your config file [purple]{config_filename}[/purple] appears to be already up-to-date for Nebari version [green]{__version__}[/green] but there is another validation error.\n"
)
raise e
start_version = config.get("nebari_version", "")
UpgradeStep.upgrade(
config, start_version, __version__, config_filename, attempt_fixes
)
# Backup old file
backup_configuration(config_filename, f".{start_version or 'old'}")
with config_filename.open("wt") as f:
yaml.dump(config, f)
rich.print(
f"Saving new config file [purple]{config_filename}[/purple] ready for Nebari version [green]{__version__}[/green]"
)
ci_cd = config.get("ci_cd", {}).get("type", "")
if ci_cd in ("github-actions", "gitlab-ci"):
rich.print(
f"\nSince you are using ci_cd [green]{ci_cd}[/green] you also need to re-render the workflows and re-commit the files to your Git repo:\n"
f" nebari render -c [purple]{config_filename}[/purple]\n"
)
class UpgradeStep(ABC):
"""
Abstract base class representing an upgrade step.
Attributes:
_steps (ClassVar[Dict[str, Any]]): Class variable holding registered upgrade steps.
version (ClassVar[str]): The version of the upgrade step.
"""
_steps: ClassVar[Dict[str, Any]] = {}
version: ClassVar[str] = ""
def __init_subclass__(cls):
"""
Initializes a subclass of UpgradeStep.
This method validates the version string and registers the subclass
in the _steps dictionary.
"""
try:
parsed_version = Version(cls.version)
except ValueError as exc:
raise ValueError(f"Invalid version string {cls.version}") from exc
cls.parsed_version = parsed_version
assert (
rounded_ver_parse(cls.version) == parsed_version
), f"Invalid version {cls.version}: must be a full release version, not a dev/prerelease/postrelease version"
assert (
cls.version not in cls._steps
), f"Duplicate UpgradeStep version {cls.version}"
cls._steps[cls.version] = cls
@classmethod
def clear_steps_registry(cls):
"""Clears the steps registry. Useful for testing."""
cls._steps.clear()
@classmethod
def has_step(cls, version):
"""
Checks if there is an upgrade step for a given version.
Args:
version (str): The version to check.
Returns:
bool: True if the step exists, False otherwise.
"""
return version in cls._steps
@classmethod
def upgrade(
cls, config, start_version, finish_version, config_filename, attempt_fixes=False
):
"""
Runs through all required upgrade steps (i.e. relevant subclasses of UpgradeStep).
Calls UpgradeStep.upgrade_step for each.
Args:
config (dict): The current configuration dictionary.
start_version (str): The starting version of the configuration.
finish_version (str): The target version for the configuration.
config_filename (str): The path to the configuration file.
attempt_fixes (bool): Whether to attempt automatic fixes for validation errors.
Returns:
dict: The updated configuration dictionary.
"""
starting_ver = rounded_ver_parse(start_version or "0.0.0")
finish_ver = rounded_ver_parse(finish_version)
if finish_ver < starting_ver:
raise ValueError(
f"Your nebari-config.yaml already belongs to a later version ({start_version}) than the installed version of Nebari ({finish_version}).\n"
"You should upgrade the installed nebari package (e.g. pip install --upgrade nebari) to work with your deployment."
)
step_versions = sorted(
[
v
for v in cls._steps.keys()
if rounded_ver_parse(v) > starting_ver
and rounded_ver_parse(v) <= finish_ver
],
key=rounded_ver_parse,
)
current_start_version = start_version
for stepcls in [cls._steps[str(v)] for v in step_versions]:
step = stepcls()
config = step.upgrade_step(
config,
current_start_version,
config_filename,
attempt_fixes=attempt_fixes,
)
current_start_version = step.get_version()
print("\n")
return config
@classmethod
def _rm_rf_stages(cls, config_filename, dry_run: bool = False, verbose=False):
"""
Remove stage files during and upgrade step
Usually used when you need files in your `stages` directory to be
removed in order to avoid resource conflicts
Args:
config_filename (str): The path to the configuration file.
Returns:
None
"""
config_dir = Path(config_filename).resolve().parent
if Path.is_dir(config_dir):
stage_dir = config_dir / "stages"
stage_filenames = [d for d in stage_dir.rglob("*") if d.is_file()]
for stage_filename in stage_filenames:
if dry_run and verbose:
rich.print(f"Dry run: Would remove {stage_filename}")
else:
stage_filename.unlink(missing_ok=True)
if verbose:
rich.print(f"Removed {stage_filename}")
stage_filedirs = sorted(
(d for d in stage_dir.rglob("*") if d.is_dir()),
reverse=True,
)
for stage_filedir in stage_filedirs:
if dry_run and verbose:
rich.print(f"Dry run: Would remove {stage_filedir}")
else:
stage_filedir.rmdir()
if verbose:
rich.print(f"Removed {stage_filedir}")
if dry_run and verbose:
rich.print(f"Dry run: Would remove {stage_dir}")
elif stage_dir.is_dir():
stage_dir.rmdir()
if verbose:
rich.print(f"Removed {stage_dir}")
def get_version(self):
"""
Returns:
str: The version of the upgrade step.
"""
return self.version
def requires_nebari_version_field(self):
"""
Checks if the nebari_version field is required for this upgrade step.
Returns:
bool: True if the nebari_version field is required, False otherwise.
"""
return rounded_ver_parse(self.version) > rounded_ver_parse("0.3.13")
def upgrade_step(self, config, start_version, config_filename, *args, **kwargs):
"""
Perform the upgrade from start_version to self.version.
Generally, this will be in-place in config, but must also return config dict.
config_filename may be useful to understand the file path for nebari-config.yaml, for example
to output another file in the same location.
The standard body here will take care of setting nebari_version and also updating the image tags.
It should normally be left as-is for all upgrades. Use _version_specific_upgrade below
for any actions that are only required for the particular upgrade you are creating.
Args:
config (dict): The current configuration dictionary.
start_version (str): The starting version of the configuration.
config_filename (str): The path to the configuration file.
Returns:
dict: The updated configuration dictionary.
"""
finish_version = self.get_version()
__rounded_finish_version__ = str(rounded_ver_parse(finish_version))
rich.print(
f"\n---> Starting upgrade from [green]{start_version or 'old version'}[/green] to [green]{finish_version}[/green]\n"
)
# Set the new version
if start_version == "":
assert "nebari_version" not in config
assert self.version != start_version
if self.requires_nebari_version_field():
rich.print(f"Setting nebari_version to [green]{self.version}[/green]")
config["nebari_version"] = self.version
def contains_image_and_tag(s: str) -> bool:
"""
Check if the string matches the Nebari image pattern.
Args:
s (str): The string to check.
Returns:
bool: True if the string matches the pattern, False otherwise.
"""
pattern = r"^quay\.io\/nebari\/nebari-(jupyterhub|jupyterlab|dask-worker)(-gpu)?:\d{4}\.\d+\.\d+$"
return bool(re.match(pattern, s))
def replace_image_tag_legacy(
image: str, start_version: str, new_version: str
) -> str:
"""
Replace legacy image tags with the new version.
Args:
image (str): The current image string.
start_version (str): The starting version of the image.
new_version (str): The new version to replace with.
Returns:
str: The updated image string with the new version, or None if no match.
"""
start_version_regex = start_version.replace(".", "\\.")
if not start_version:
start_version_regex = "0\\.[0-3]\\.[0-9]{1,2}"
docker_image_regex = re.compile(
f"^([A-Za-z0-9_-]+/[A-Za-z0-9_-]+):v{start_version_regex}$"
)
m = docker_image_regex.match(image)
if m:
return ":".join([m.groups()[0], f"v{new_version}"])
return None
def replace_image_tag(
s: str, new_version: str, config_path: str, attempt_fixes: bool
) -> str:
"""
Replace the image tag with the new version.
Args:
s (str): The current image string.
new_version (str): The new version to replace with.
config_path (str): The path to the configuration file.
Returns:
str: The updated image string with the new version, or the original string if no changes.
"""
legacy_replacement = replace_image_tag_legacy(s, start_version, new_version)
if legacy_replacement:
return legacy_replacement
if not contains_image_and_tag(s):
return s
image_name, current_tag = s.split(":")
if current_tag == new_version:
return s
loc = f"{config_path}: {image_name}"
response = attempt_fixes or Confirm.ask(
f"\nDo you want to replace current tag [green]{current_tag}[/green] with [green]{new_version}[/green] for:\n[purple]{loc}[/purple]?",
default=True,
)
if response:
return s.replace(current_tag, new_version)
else:
return s
def set_nested_item(config: dict, config_path: list, value: str):
"""
Set a nested item in the configuration dictionary.
Args:
config (dict): The configuration dictionary.
config_path (list): The path to the item to set.
value (str): The value to set.
Returns:
None
"""
config_path = config_path.split(".")
for k in config_path[:-1]:
try:
k = int(k)
except ValueError:
pass
config = config[k]
try:
config_path[-1] = int(config_path[-1])
except ValueError:
pass
config[config_path[-1]] = value
def update_image_tag(
config: dict,
config_path: str,
current_image: str,
new_version: str,
attempt_fixes: bool,
) -> dict:
"""
Update the image tag in the configuration.
Args:
config (dict): The configuration dictionary.
config_path (str): The path to the item to update.
current_image (str): The current image string.
new_version (str): The new version to replace with.
Returns:
dict: The updated configuration dictionary.
"""
new_image = replace_image_tag(
current_image,
new_version,
config_path,
attempt_fixes,
)
if new_image != current_image:
set_nested_item(config, config_path, new_image)
return config
# update default_images
for k, v in config.get("default_images", {}).items():
config_path = f"default_images.{k}"
config = update_image_tag(
config,
config_path,
v,
__rounded_finish_version__,
kwargs.get("attempt_fixes", False),
)
# update profiles.jupyterlab images
for i, v in enumerate(config.get("profiles", {}).get("jupyterlab", [])):
current_image = v.get("kubespawner_override", {}).get("image", None)
if current_image:
config = update_image_tag(
config,
f"profiles.jupyterlab.{i}.kubespawner_override.image",
current_image,
__rounded_finish_version__,
kwargs.get("attempt_fixes", False),
)
# update profiles.dask_worker images
for k, v in config.get("profiles", {}).get("dask_worker", {}).items():
current_image = v.get("image", None)
if current_image:
config = update_image_tag(
config,
f"profiles.dask_worker.{k}.image",
current_image,
__rounded_finish_version__,
kwargs.get("attempt_fixes", False),
)
# Run any version-specific tasks
return self._version_specific_upgrade(
config,
start_version,
config_filename,
*args,
**kwargs,
)
def _version_specific_upgrade(
self, config, start_version, config_filename, *args, **kwargs
):
"""
Perform version-specific upgrade tasks.
Override this method in subclasses if you need to do anything specific to your version.
Args:
config (dict): The current configuration dictionary.
start_version (str): The starting version of the configuration.
config_filename (str): The path to the configuration file.
Returns:
dict: The updated configuration dictionary.
"""
return config
class Upgrade_0_3_12(UpgradeStep):
version = "0.3.12"
@override
def _version_specific_upgrade(
self, config, start_version, config_filename, *args, **kwargs
):
"""
This version of Nebari requires a conda_store image for the first time.
"""
if config.get("default_images", {}).get("conda_store", None) is None:
newimage = "quansight/conda-store-server:v0.3.3"
rich.print(
f"Adding default_images: conda_store image as [green]{newimage}[/green]"
)
if "default_images" not in config:
config["default_images"] = {}
config["default_images"]["conda_store"] = newimage
return config
class Upgrade_0_4_0(UpgradeStep):
version = "0.4.0"
@override
def _version_specific_upgrade(
self, config, start_version, config_filename: Path, *args, **kwargs
):
"""
This version of Nebari introduces Keycloak for authentication, removes deprecated fields,
and generates a default password for the Keycloak root user.
"""
security = config.get("security", {})
users = security.get("users", {})
groups = security.get("groups", {})
# Custom Authenticators are no longer allowed
if (
config.get("security", {}).get("authentication", {}).get("type", "")
== "custom"
):
customauth_warning = (
f"Custom Authenticators are no longer supported in {self.version} because Keycloak "
"manages all authentication.\nYou need to find a way to support your authentication "
"requirements within Keycloak."
)
if not kwargs.get("attempt_fixes", False):
raise ValueError(
f"{customauth_warning}\n\nRun `nebari upgrade --attempt-fixes` to switch to basic Keycloak authentication instead."
)
else:
rich.print(f"\nWARNING: {customauth_warning}")
rich.print(
"\nSwitching to basic Keycloak authentication instead since you specified --attempt-fixes."
)
config["security"]["authentication"] = {"type": "password"}
# Create a group/user import file for Keycloak
realm_import_filename = config_filename.parent / "nebari-users-import.json"
realm = {"id": "nebari", "realm": "nebari"}
realm["users"] = [
{
"username": k,
"enabled": True,
"groups": sorted(
list(
(
{v.get("primary_group", "")}
| set(v.get("secondary_groups", []))
)
- {""}
)
),
}
for k, v in users.items()
]
realm["groups"] = [
{"name": k, "path": f"/{k}"}
for k, v in groups.items()
if k not in {"users", "admin"}
]
backup_configuration(realm_import_filename)
with realm_import_filename.open("wt") as f:
json.dump(realm, f, indent=2)
rich.print(
f"\nSaving user/group import file [purple]{realm_import_filename}[/purple].\n\n"
"ACTION REQUIRED: You must import this file into the Keycloak admin webpage after you redeploy Nebari.\n"
"Visit the URL path /auth/ and login as 'root'. Under Manage, click Import and select this file.\n\n"
"Non-admin users will default to analyst group membership after the upgrade (no dask access), "
"so you may wish to promote some users into the developer group.\n"
)
if "users" in security:
del security["users"]
if "groups" in security:
if "users" in security["groups"]:
# Ensure the users default group is added to Keycloak
security["shared_users_group"] = True
del security["groups"]
if "terraform_modules" in config:
del config["terraform_modules"]
rich.print(
"Removing terraform_modules field from config as it is no longer used.\n"
)
if "default_images" not in config:
config["default_images"] = {}
# Remove conda_store image from default_images
if "conda_store" in config["default_images"]:
del config["default_images"]["conda_store"]
# Remove dask_gateway image from default_images
if "dask_gateway" in config["default_images"]:
del config["default_images"]["dask_gateway"]
# Create root password
default_password = "".join(
secrets.choice(string.ascii_letters + string.digits) for i in range(16)
)
security.setdefault("keycloak", {})["initial_root_password"] = default_password
rich.print(
f"Generated default random password=[green]{default_password}[/green] for Keycloak root user (Please change at /auth/ URL path).\n"
)
# project was never needed in Azure - it remained as PLACEHOLDER in earlier nebari inits!
if "azure" in config:
if "project" in config["azure"]:
del config["azure"]["project"]
# "oauth_callback_url" and "scope" not required in nebari-config.yaml
# for Auth0 and Github authentication
auth_config = config["security"]["authentication"].get("config", None)
if auth_config:
if "oauth_callback_url" in auth_config:
del auth_config["oauth_callback_url"]
if "scope" in auth_config:
del auth_config["scope"]
# It is not safe to immediately redeploy without backing up data ready to restore data
# since a new cluster will be created for the new version.
# Setting the following flag will prevent deployment and display guidance to the user
# which they can override if they are happy they understand the situation.
config["prevent_deploy"] = True
return config
class Upgrade_0_4_1(UpgradeStep):
version = "0.4.1"
@override
def _version_specific_upgrade(
self, config, start_version, config_filename: Path, *args, **kwargs
):
"""
Upgrade jupyterlab profiles.
"""
rich.print("\nUpgrading jupyterlab profiles in order to specify access type:\n")
profiles_jupyterlab = config.get("profiles", {}).get("jupyterlab", [])
for profile in profiles_jupyterlab:
name = profile.get("display_name", "")
if "groups" in profile or "users" in profile:
profile["access"] = "yaml"
else:
profile["access"] = "all"
rich.print(
f"Setting access type of JupyterLab profile [green]{name}[/green] to [green]{profile['access']}[/green]"
)
return config
class Upgrade_2023_4_2(UpgradeStep):
version = "2023.4.2"
@override
def _version_specific_upgrade(
self, config, start_version, config_filename: Path, *args, **kwargs
):
"""
Prompt users to delete Argo CRDs
"""
argo_crds = [
"clusterworkflowtemplates.argoproj.io",
"cronworkflows.argoproj.io",
"workfloweventbindings.argoproj.io",
"workflows.argoproj.io",
"workflowtasksets.argoproj.io",
"workflowtemplates.argoproj.io",
]
argo_sa = ["argo-admin", "argo-dev", "argo-view"]
namespace = config.get("namespace", "default")
if kwargs.get("attempt_fixes", False):
try:
kubernetes.config.load_kube_config()
except kubernetes.config.config_exception.ConfigException:
rich.print(
"[red bold]No default kube configuration file was found. Make sure to [link=https://www.nebari.dev/docs/how-tos/debug-nebari#generating-the-kubeconfig]have one pointing to your Nebari cluster[/link] before upgrading.[/red bold]"
)
exit()
for crd in argo_crds:
api_instance = kubernetes.client.ApiextensionsV1Api()
try:
api_instance.delete_custom_resource_definition(
name=crd,
)
except kubernetes.client.exceptions.ApiException as e:
if e.status == 404:
rich.print(f"CRD [yellow]{crd}[/yellow] not found. Ignoring.")
else:
raise e
else:
rich.print(f"Successfully removed CRD [green]{crd}[/green]")
for sa in argo_sa:
api_instance = kubernetes.client.CoreV1Api()
try:
api_instance.delete_namespaced_service_account(
sa,
namespace,
)
except kubernetes.client.exceptions.ApiException as e:
if e.status == 404:
rich.print(
f"Service account [yellow]{sa}[/yellow] not found. Ignoring."
)
else:
raise e
else:
rich.print(
f"Successfully removed service account [green]{sa}[/green]"
)
else:
kubectl_delete_argo_crds_cmd = " ".join(
(
*("kubectl delete crds",),
*argo_crds,
),
)
kubectl_delete_argo_sa_cmd = " ".join(
(
*(
"kubectl delete sa",
f"-n {namespace}",
),
*argo_sa,
),
)
rich.print(
f"\n\n[bold cyan]Note:[/] Upgrading requires a one-time manual deletion of the Argo Workflows Custom Resource Definitions (CRDs) and service accounts. \n\n[red bold]"
f"Warning: [link=https://{config['domain']}/argo/workflows]Workflows[/link] and [link=https://{config['domain']}/argo/workflows]CronWorkflows[/link] created before deleting the CRDs will be erased when the CRDs are deleted and will not be restored.[/red bold] \n\n"
f"The updated CRDs will be installed during the next [cyan bold]nebari deploy[/cyan bold] step. Argo Workflows will not function after deleting the CRDs until the updated CRDs and service accounts are installed in the next nebari deploy. "
f"You must delete the Argo Workflows CRDs and service accounts before upgrading to {self.version} (or later) or the deploy step will fail. "
f"Please delete them before proceeding by generating a kubeconfig (see [link=https://www.nebari.dev/docs/how-tos/debug-nebari/#generating-the-kubeconfig]docs[/link]), installing kubectl (see [link=https://www.nebari.dev/docs/how-tos/debug-nebari#installing-kubectl]docs[/link]), and running the following two commands:\n\n\t[cyan bold]{kubectl_delete_argo_crds_cmd} [/cyan bold]\n\n\t[cyan bold]{kubectl_delete_argo_sa_cmd} [/cyan bold]"
)
continue_ = Confirm.ask(
"Have you deleted the Argo Workflows CRDs and service accounts?",
default=False,
)
if not continue_:
rich.print(
f"You must delete the Argo Workflows CRDs and service accounts before upgrading to [green]{self.version}[/green] (or later)."
)
exit()
return config
class Upgrade_2023_7_1(UpgradeStep):
version = "2023.7.1"
@override
def _version_specific_upgrade(
self, config, start_version, config_filename: Path, *args, **kwargs
):
provider = config["provider"]
if provider == ProviderEnum.aws.value:
rich.print("\n ⚠️ DANGER ⚠️")
rich.print(
DESTRUCTIVE_UPGRADE_WARNING,
"The 'prevent_deploy' flag has been set in your config file and must be manually removed to deploy.",
)
config["prevent_deploy"] = True
return config
class Upgrade_2023_7_2(UpgradeStep):
version = "2023.7.2"
@override
def _version_specific_upgrade(
self, config, start_version, config_filename: Path, *args, **kwargs
):
argo = config.get("argo_workflows", {})
if argo.get("enabled"):
response = kwargs.get("attempt_fixes", False) or Confirm.ask(
f"\nDo you want to enable the [green][link={NEBARI_WORKFLOW_CONTROLLER_DOCS}]Nebari Workflow Controller[/link][/green], required for [green][link={ARGO_JUPYTER_SCHEDULER_REPO}]Argo-Jupyter-Scheduler[/link][green]?",
default=True,
)
if response:
argo["nebari_workflow_controller"] = {"enabled": True}
rich.print("\n ⚠️ Deprecation Warnings ⚠️")
rich.print(
f"-> [green]{self.version}[/green] is the last Nebari version that supports CDS Dashboards"
)
return config
class Upgrade_2023_10_1(UpgradeStep):
"""
Upgrade step for Nebari version 2023.10.1
Note:
Upgrading to 2023.10.1 is considered high-risk because it includes a major refactor
to introduce the extension mechanism system. This version introduces significant
changes, including the support for third-party plugins, upgrades JupyterHub to version 3.1,
and deprecates certain components such as CDS Dashboards, ClearML, Prefect, and kbatch.
"""
version = "2023.10.1"
# JupyterHub Helm chart 2.0.0 (app version 3.0.0) requires K8S Version >=1.23. (reference: https://z2jh.jupyter.org/en/stable/)
# This released has been tested against 1.26
min_k8s_version = 1.26
@override
def _version_specific_upgrade(
self, config, start_version, config_filename: Path, *args, **kwargs
):
# Upgrading to 2023.10.1 is considered high-risk because it includes a major refacto
# to introduce the extension mechanism system.
rich.print("\n ⚠️ Warning ⚠️")
rich.print(
f"-> Nebari version [green]{self.version}[/green] includes a major refactor to introduce an extension mechanism that supports the development of third-party plugins."
)
rich.print(
"-> Data should be backed up before performing this upgrade ([green][link=https://www.nebari.dev/docs/how-tos/manual-backup]see docs[/link][/green]) The 'prevent_deploy' flag has been set in your config file and must be manually removed to deploy."
)
# Setting the following flag will prevent deployment and display guidance to the user
# which they can override if they are happy they understand the situation.
config["prevent_deploy"] = True
# Nebari version 2023.10.1 upgrades JupyterHub to 3.1. CDS Dashboards are only compatible with
# JupyterHub versions 1.X and so will be removed during upgrade.
rich.print("\n ⚠️ Deprecation Warning ⚠️")
rich.print(
f"-> CDS dashboards are no longer supported in Nebari version [green]{self.version}[/green] and will be uninstalled."
)
if config.get("cdsdashboards"):
rich.print("-> Removing cdsdashboards from config file.")
del config["cdsdashboards"]
# Deprecation Warning - ClearML, Prefect, kbatch
rich.print("\n ⚠️ Deprecation Warning ⚠️")
rich.print(
"-> We will be removing and ending support for ClearML, Prefect and kbatch in the next release. The kbatch has been functionally replaced by Argo-Jupyter-Scheduler. We have seen little interest in ClearML and Prefect in recent years, and removing makes sense at this point. However if you wish to continue using them with Nebari we encourage you to [green][link=https://www.nebari.dev/docs/how-tos/nebari-extension-system/#developing-an-extension]write your own Nebari extension[/link][/green]."
)
# Kubernetes version check
# JupyterHub Helm chart 2.0.0 (app version 3.0.0) requires K8S Version >=1.23. (reference: https://z2jh.jupyter.org/en/stable/)
provider = config["provider"]
provider_config_block = get_provider_config_block_name(provider)
# Get current Kubernetes version if available in config.
current_version = config.get(provider_config_block, {}).get(
"kubernetes_version", None
)
# Convert to decimal prefix
if provider in ["aws", "azure", "gcp", "do"]:
current_version = get_k8s_version_prefix(current_version)
# Try to convert known Kubernetes versions to float.
if current_version is not None:
try:
current_version = float(current_version)
except ValueError:
current_version = None
# Handle checks for when Kubernetes version should be detectable
if provider in ["aws", "azure", "gcp", "do"]:
# Kubernetes version not found in provider block
if current_version is None:
rich.print("\n ⚠️ Warning ⚠️")
rich.print(
f"-> Unable to detect Kubernetes version for provider {provider}. Nebari version [green]{self.version}[/green] requires Kubernetes version {str(self.min_k8s_version)}. Please confirm your Kubernetes version is configured before upgrading."
)
# Kubernetes version less than required minimum
if (
isinstance(current_version, float)
and current_version < self.min_k8s_version
):
rich.print("\n ⚠️ Warning ⚠️")
rich.print(
f"-> Nebari version [green]{self.version}[/green] requires Kubernetes version {str(self.min_k8s_version)}. Your configured Kubernetes version is [red]{current_version}[/red]. {UPGRADE_KUBERNETES_MESSAGE}"
)
version_diff = round(self.min_k8s_version - current_version, 2)
if version_diff > 0.01:
rich.print(
"-> The Kubernetes version is multiple minor versions behind the minimum required version. You will need to perform the upgrade one minor version at a time. For example, if your current version is 1.24, you will need to upgrade to 1.25, and then 1.26."
)
rich.print(
f"-> Update the value of [green]{provider_config_block}.kubernetes_version[/green] in your config file to a newer version of Kubernetes and redeploy."
)
else:
rich.print("\n ⚠️ Warning ⚠️")
rich.print(
f"-> Unable to detect Kubernetes version for provider {provider}. Nebari version [green]{self.version}[/green] requires Kubernetes version {str(self.min_k8s_version)} or greater."
)
rich.print(
"-> Please ensure your Kubernetes version is up-to-date before proceeding."
)
if provider == "aws":
rich.print("\n ⚠️ DANGER ⚠️")
rich.print(DESTRUCTIVE_UPGRADE_WARNING)
if kwargs.get("attempt_fixes", False) or Confirm.ask(
TERRAFORM_REMOVE_TERRAFORM_STAGE_FILES_CONFIRMATION,
default=False,
):
if (
(_terraform_state_config := config.get("terraform_state"))
and (_terraform_state_config.get("type") != "remote")
and not Confirm.ask(
DESTROY_STAGE_FILES_WITH_TF_STATE_NOT_REMOTE,
default=False,
)
):
exit()
self._rm_rf_stages(
config_filename,
dry_run=kwargs.get("dry_run", False),
verbose=True,
)
return config
class Upgrade_2023_11_1(UpgradeStep):
"""
Upgrade step for Nebari version 2023.11.1
Note:
- ClearML, Prefect, and kbatch are no longer supported in this version.
"""
version = "2023.11.1"
@override
def _version_specific_upgrade(
self, config, start_version, config_filename: Path, *args, **kwargs
):
rich.print("\n ⚠️ Deprecation Warning ⚠️")
rich.print(
f"-> ClearML, Prefect and kbatch are no longer supported in Nebari version [green]{self.version}[/green] and will be uninstalled."