-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathorchestrators.py
1120 lines (952 loc) · 40.3 KB
/
orchestrators.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
#!/usr/bin/env python3
# # -*- coding: utf-8 -*-
"""
Orchestrators for launching docker containers.
________________________________________________________________________________
Created by brightSPARK Labs
www.brightsparklabs.com
"""
# standard libraries
from __future__ import annotations
import os
import subprocess
import sys
from pathlib import Path
from subprocess import CompletedProcess
from tempfile import NamedTemporaryFile
from typing import Iterable, List
# vendor libraries
import click
# local libraries
from appcli.commands.appcli_command import AppcliCommand
from appcli.crypto import crypto
from appcli.logger import logger
from appcli.models.cli_context import CliContext
from appcli.dev_mode import wrap_dev_mode
# ------------------------------------------------------------------------------
# CLASSES
# ------------------------------------------------------------------------------
class Orchestrator:
"""
Interface for Orchestrators. Use a subclass, all methods in this class raise NotImplementedError.
Raises:
NotImplementedError: If any of these methods are called directly.
"""
def start(
self, cli_context: CliContext, service_name: tuple[str, ...] = None
) -> CompletedProcess:
"""
Starts Docker containers (services). Optionally accepts a tuple of service names to start.
Args:
cli_context (CliContext): The current CLI context.
service_names (tuple[str,...], optional): Names of the services to start. If not provided, starts all services.
Returns:
CompletedProcess: Result of the orchestrator command.
"""
raise NotImplementedError
def shutdown(
self, cli_context: CliContext, service_name: tuple[str, ...] = None
) -> CompletedProcess:
"""
Stops Docker containers (services). Optionally accepts a tuple of service names to shutdown.
Args:
cli_context (CliContext): The current CLI context.
service_names (tuple[str,...], optional): Names of the services to shutdown. If not provided, shuts down all services.
Returns:
CompletedProcess: Result of the orchestrator command.
"""
raise NotImplementedError
def status(
self, cli_context: CliContext, service_name: tuple[str, ...] = None
) -> CompletedProcess:
"""
Gets the status of Docker containers (services). Optionally accepts a tuple of service names to get the status of.
Args:
cli_context (CliContext): The current CLI context.
service_names (tuple[str,...], optional): Names of the services to get the status of. If not provided, gets the status of all services.
Returns:
CompletedProcess: Result of the orchestrator command.
"""
raise NotImplementedError
def task(
self,
cli_context: CliContext,
service_name: str,
extra_args: Iterable[str],
detached: bool = False,
) -> CompletedProcess:
"""
Runs a specified Docker container which is expected to exit
upon completing a short-lived task.
Args:
cli_context (CliContext): The current CLI context.
service_name (str): Name of the container to run.
extra_args (Iterable[str]): Extra arguments for running the container.
detached (bool): Optional - defaults to False. Whether the task should run in `--detached` mode or not.
Returns:
CompletedProcess: Result of the orchestrator command.
"""
raise NotImplementedError
def exec(
self,
cli_context: CliContext,
service_name: str,
command: Iterable[str],
stdin_input: str = None,
capture_output: bool = False,
) -> CompletedProcess:
"""
Executes a command in a running container.
Args:
cli_context (CliContext): The current CLI context.
service_name (str): Name of the container to be acted upon.
command (str): The command to be executed, along with any arguments.
stdin_input (str): Optional - defaults to None. String passed through to the stdin of the exec command.
capture_output (bool): Optional - defaults to False. True to capture stdout/stderr for the run command.
Returns:
CompletedProcess: Result of the orchestrator command.
"""
raise NotImplementedError
def get_logs_command(self) -> click.Command:
"""
Returns a click command which streams logs for Docker containers.
Args:
cli_context (CliContext): The current CLI context.
Returns:
click.Command: Command for streaming logs.
"""
raise NotImplementedError
def get_additional_commands(self) -> Iterable[click.Command]:
"""
Returns any additional commands supported by this orchestrator.
Returns:
Iterable[click.Command]: Additional orchestrator specific commands.
"""
return ()
def get_name(self) -> str:
"""
Returns the name of this orchestrator.
Returns:
str: the name of this orchestrator.
"""
raise NotImplementedError
def verify_service_names(
self, cli_context: CliContext, service_names: tuple[str, ...]
) -> bool:
"""
Checks whether a list of named services exist. Returns True if all services exist, otherwise returns False.
Args:
cli_context (CliContext): The current CLI context.
service_names (tuple[str,...]): Names of the services.
Returns:
bool: if the services exists.
"""
raise NotImplementedError
def get_disabled_commands(self) -> list[str]:
"""
Returns the list of default appcli commands which this orchestrator wants to disable.
E.g. ['init', 'backup'].
This allows orchestrators to prevent commands being run which are not supported.
Returns:
list[str]: The disabled command names.
"""
return []
class DockerComposeOrchestrator(Orchestrator):
"""
Uses Docker Compose to orchestrate containers.
"""
def __init__(
self,
docker_compose_file: Path = Path("docker-compose.yml"),
docker_compose_override_directory: Path = Path("docker-compose.override.d/"),
docker_compose_task_file: Path = Path("docker-compose.tasks.yml"),
docker_compose_task_override_directory: Path = Path(
"docker-compose.tasks.override.d/"
),
):
"""
Creates a new instance of an orchestrator for docker-compose-based applications.
Args:
docker_compose_file (Path): Path to a docker compose file containing long-running services. Path is relative
to the generated configuration directory.
docker_compose_override_directory (Path, optional): Path to a directory containing any additional
docker-compose override files. Overrides are applied in alphanumeric order of filename. Path is relative
to the generated configuration directory.
docker_compose_task_file (Path): Path to a docker compose file containing services to be run as short-lived
tasks. Path is relative to the generated configuration directory.
docker_compose_task_override_directory (Path): Path to a directory containing any additional
docker-compose override files for services used as tasks. Path is relative to the generated
configuration directory.
"""
self.docker_compose_file = docker_compose_file
self.docker_compose_override_directory = docker_compose_override_directory
self.docker_compose_task_file = docker_compose_task_file
self.docker_compose_task_override_directory = (
docker_compose_task_override_directory
)
def start(
self, cli_context: CliContext, service_names: tuple[str, ...] = None
) -> CompletedProcess:
command = ("up", "-d")
if service_names is not None and len(service_names) > 0:
command += service_names
return self.__compose_service(cli_context, command)
def shutdown(
self, cli_context: CliContext, service_names: tuple[str, ...] = None
) -> CompletedProcess:
if service_names is not None and len(service_names) > 0:
# We cannot use the 'down' command as it removes more than just the specified service (by design).
# https://github.com/docker/compose/issues/5420
# `-fsv` flags mean forcibly stop the container before removing, and delete attached anonymous volumes
command = ("rm", "-fsv") + service_names
return self.__compose_service(cli_context, command)
return self.__compose_service(cli_context, ("down",))
def status(
self, cli_context: CliContext, service_names: tuple[str, ...] = None
) -> CompletedProcess:
command = ("ps", "-a")
if service_names is not None and len(service_names) > 0:
command += service_names
return self.__compose_service(cli_context, command)
def task(
self,
cli_context: CliContext,
service_name: str,
extra_args: Iterable[str],
detached: bool = False,
) -> CompletedProcess:
command = ["run"] # Command is: run [OPTIONS] --rm TASK [ARGS]
if detached:
command.append("-d")
command.append("--rm")
command.append(service_name)
command.extend(list(extra_args))
return self.__compose_task(cli_context, command)
def exec(
self,
cli_context: CliContext,
service_name: str,
command: Iterable[str],
stdin_input: str = None,
capture_output: bool = False,
) -> CompletedProcess:
cmd = ["exec"] # Command is: exec SERVICE COMMAND
# If there's stdin_input being piped to the command, we need to provide
# the -T flag to `docker-compose`: https://github.com/docker/compose/issues/7306
if stdin_input is not None:
cmd.append("-T")
cmd.append(service_name)
cmd.extend(list(command))
return self.__compose_service(cli_context, cmd, stdin_input, capture_output)
def verify_service_names(
self, cli_context: CliContext, service_names: tuple[str, ...]
) -> bool:
if service_names is None or len(service_names) == 0:
return True
command = ["config", "--services"]
result = self.__compose_service(cli_context, command, capture_output=True)
if result.returncode != 0:
error_msg = result.stderr.decode()
logger.error(
f"An unexpected error occured while verifying services. Error: {error_msg}"
)
return False
# Converts the byte type into list of names, and removes trailing empty string
valid_service_names = result.stdout.decode().split("\n")[:-1]
logger.debug("Valid Services: %s", ", ".join(valid_service_names))
return service_name_verifier(service_names, valid_service_names)
def get_logs_command(self):
@click.command(
help="Prints logs from all services (or the ones specified).",
context_settings=dict(ignore_unknown_options=True),
)
@click.pass_context
@click.option(
"--lines",
"-n",
help="Output the last NUM lines instead of all.",
type=click.STRING,
required=False,
default="all",
)
@click.argument("service", nargs=-1, type=click.UNPROCESSED)
def logs(ctx, lines, service):
cli_context: CliContext = ctx.obj
cli_context.get_configuration_dir_state().verify_command_allowed(
AppcliCommand.SERVICE_LOGS
)
subcommand = ["logs", "--follow", f"--tail={lines}"]
subcommand.extend(service)
result = self.__compose_service(cli_context, subcommand)
sys.exit(result.returncode)
return logs
def get_additional_commands(self):
@click.command(help="List the status of services.")
@click.pass_context
def ps(ctx):
result = self.__compose_service(ctx.obj, ("ps",))
sys.exit(result.returncode)
@click.command(
help="Runs a docker compose command.",
context_settings=dict(ignore_unknown_options=True),
)
@click.pass_context
@click.argument("command", nargs=-1, type=click.UNPROCESSED)
def compose(ctx, command):
result = self.__compose_service(ctx.obj, command)
sys.exit(result.returncode)
return (
ps,
compose,
)
def get_name(self):
return "compose"
def __compose_service(
self,
cli_context: CliContext,
command: Iterable[str],
stdin_input: str = None,
capture_output: bool = False,
):
return execute_compose(
cli_context,
command,
self.docker_compose_file,
self.docker_compose_override_directory,
stdin_input=stdin_input,
capture_output=capture_output,
)
def __compose_task(
self,
cli_context: CliContext,
command: Iterable[str],
stdin_input: str = None,
capture_output: bool = False,
):
return execute_compose(
cli_context,
command,
self.docker_compose_task_file,
self.docker_compose_task_override_directory,
stdin_input=stdin_input,
capture_output=capture_output,
)
class DockerSwarmOrchestrator(Orchestrator):
"""
Uses Docker Swarm to orchestrate containers.
"""
def __init__(
self,
docker_compose_file: Path = Path("docker-compose.yml"),
docker_compose_override_directory: Path = Path("docker-compose.override.d/"),
docker_compose_task_file: Path = Path("docker-compose.tasks.yml"),
docker_compose_task_override_directory: Path = Path(
"docker-compose.tasks.override.d/"
),
):
"""
Creates a new instance of an orchestrator for docker swarm applications.
Args:
docker_compose_file (Path): Path to a docker compose file containing long-running services. Path is relative
to the generated configuration directory.
docker_compose_override_directory (Path, optional): Path to a directory containing any additional
docker-compose override files. Overrides are applied in alphanumeric order of filename. Path is relative
to the generated configuration directory.
docker_compose_task_file (Path): Path to a docker compose file containing services to be run as short-lived
tasks. Path is relative to the generated configuration directory.
docker_compose_task_override_directory (Path): Path to a directory containing any additional
docker-compose override files for services used as tasks. Path is relative to the generated
configuration directory.
"""
self.docker_compose_file = docker_compose_file
self.docker_compose_override_directory = docker_compose_override_directory
self.docker_compose_task_file = docker_compose_task_file
self.docker_compose_task_override_directory = (
docker_compose_task_override_directory
)
def start(
self, cli_context: CliContext, service_names: tuple[str, ...] = None
) -> CompletedProcess:
if service_names is not None and len(service_names) > 0:
logger.error(
"Docker Swarm orchestrator cannot start individual services. Attempted to start [%s].",
service_names,
)
return CompletedProcess(args=None, returncode=1)
subcommand = ["deploy"]
compose_files = decrypt_docker_compose_files(
cli_context,
self.docker_compose_file,
self.docker_compose_override_directory,
)
if len(compose_files) == 0:
logger.error(
"No valid docker compose files were found. Expected file [%s] or files in directory [%s]",
self.docker_compose_file,
self.docker_compose_override_directory,
)
return CompletedProcess(args=None, returncode=1)
for compose_file in compose_files:
subcommand.extend(("--compose-file", str(compose_file)))
return self.__docker_stack(cli_context, subcommand)
def shutdown(
self, cli_context: CliContext, service_names: tuple[str, ...] = None
) -> CompletedProcess:
if service_names is not None and len(service_names) > 0:
logger.error(
"Docker Swarm orchestrator cannot stop individual services. Attempted to shutdown [%s].",
service_names,
)
return CompletedProcess(args=None, returncode=1)
return self.__docker_stack(cli_context, ("rm",))
def status(
self, cli_context: CliContext, service_names: tuple[str, ...] = None
) -> CompletedProcess:
if service_names is not None and len(service_names) > 0:
logger.error(
"Docker Swarm orchestrator cannot check the status of individual services. Attempted to get the status of [%s].",
service_names,
)
return CompletedProcess(args=None, returncode=1)
return self.__docker_stack(cli_context, ("ps",))
def task(
self,
cli_context: CliContext,
service_name: str,
extra_args: Iterable[str],
detached: bool = False,
) -> CompletedProcess:
command = ["run"] # Command is: run [OPTIONS] --rm TASK [ARGS]
if detached:
command.append("-d")
command.append("--rm")
command.append(service_name)
command.extend(list(extra_args))
return self.__compose_task(cli_context, command)
def exec(
self,
cli_context: CliContext,
service_name: str,
command: Iterable[str],
stdin_input: str = None,
capture_output: bool = False,
) -> CompletedProcess:
# Running 'docker exec' on containers in a docker swarm is non-trivial
# due to the distributed nature of docker swarm, and the fact there could
# be replicas of a single service.
raise NotImplementedError
def verify_service_names(
self, cli_context: CliContext, service_names: tuple[str, ...]
) -> bool:
if service_names is None or len(service_names) == 0:
return True
subcommand = ["config", "--services"]
result = self.__docker_stack(cli_context, subcommand, capture_output=True)
if result.returncode != 0:
error_msg = result.stderr.decode()
logger.error(
f"An unexpected error occured while verifying services. Error: {error_msg}"
)
return False
# Converts the byte type into list of names, and removes trailing empty string
valid_service_names = result.stdout.decode().split("\n")[:-1]
logger.debug("Valid Services: %s", ", ".join(valid_service_names))
return service_name_verifier(service_names, valid_service_names)
def get_logs_command(self):
@click.command(
help="Prints logs from the specified service.",
context_settings=dict(ignore_unknown_options=True),
)
@click.pass_context
@click.option(
"--lines",
"-n",
help="Output the last NUM lines instead of all.",
type=click.STRING,
required=False,
default="all",
)
@click.argument("service", type=click.STRING)
def logs(ctx, lines, service):
cli_context: CliContext = ctx.obj
cli_context.get_configuration_dir_state().verify_command_allowed(
AppcliCommand.SERVICE_LOGS
)
command = ["docker", "service", "logs", "--follow", f"--tail={lines}"]
command.append(f"{cli_context.get_project_name()}_{service}")
result = self.__exec_command(command)
sys.exit(result.returncode)
return logs
def get_additional_commands(self):
@click.command(help="List the status of services.")
@click.pass_context
def ps(ctx):
result = self.__docker_stack(ctx.obj, ("ps",))
sys.exit(result.returncode)
@click.command(help="List the defined services.")
@click.pass_context
def ls(ctx):
result = self.__docker_stack(ctx.obj, ("services",))
sys.exit(result.returncode)
return (ps, ls)
def get_name(self):
return "swarm"
def __docker_stack(
self, cli_context: CliContext, subcommand: Iterable[str]
) -> CompletedProcess:
command = ["docker", "stack"]
command.extend(subcommand)
command.append(cli_context.get_project_name())
return self.__exec_command(command)
def __compose_task(
self,
cli_context: CliContext,
command: Iterable[str],
stdin_input: str = None,
capture_output: bool = False,
):
return execute_compose(
cli_context,
command,
self.docker_compose_task_file,
self.docker_compose_task_override_directory,
stdin_input=stdin_input,
capture_output=capture_output,
)
def __exec_command(self, command: Iterable[str]) -> CompletedProcess:
logger.debug("Running [%s]", " ".join(command))
return subprocess.run(command, capture_output=False)
class HelmOrchestrator(Orchestrator):
"""
Uses helm to provision a kubernetes backed helm chart.
"""
DEV_CHART_VARIABLE_NAME = "DEV_MODE_HELM_CHART"
" Name suffix for the DEV_MODE chart location variable. "
def __init__(
self,
chart_location: Path = Path("cli/helm/chart"),
helm_set_values_dir: Path = Path("cli/helm/set-values"),
helm_set_files_dir: Path = Path("cli/helm/set-files"),
):
"""
Creates a new instance of an orchestrator for helm-based applications.
NOTE: All `Path` objects are relative to the configuration directory.
Args:
chart_location (Path): Path to the helm chart file/directory to deploy.
helm_set_values_dir (Path): The directory containing all main `values.yaml` files.
Defaults to: `${GENERATED_CONFIGURATION_DIR}/cli/helm/set-values`
All files in this directory are applied with:
--values <file>
See below for more details.
helm_set_files_dir (Path): The directory containing all key-specific files.
Defaults to: `${GENERATED_CONFIGURATION_DIR}/cli/helm/set-files`
Take the following directory:
```
./
├── set-files/
│ ├── baz/
│ │ ├── foo.json
│ │ └── qux.waldo.txt
│ └── thud.bang.yml
└── set-values/
├── foo.yml
└── bar.txt
```
This would result in the following arguments being passed to helm:
```
--set-file baz.foo=cli/helm/set-files/baz/foo.json
--set-file baz.qux=cli/helm/set-files/baz/qux.waldo.yml # NOTE: Key is `qux` not `qux.waldo`.
--set-file thud=cli/helm/set-files/thud.bang.yml # NOTE: Key is `thud` not `thud.bang`.
--values cli/helm/set-values/foo.yml
--values cli/helm/set-values/bar.yml
```
"""
self.chart_location = chart_location
self.helm_set_values_dir = helm_set_values_dir
self.helm_set_files_dir = helm_set_files_dir
def start(
self, cli_context: CliContext, service_name: tuple[str, ...] = None
) -> CompletedProcess:
"""
Installs (or upgrades) a helm chart inside the current kubernetes cluster.
Args:
cli_context (CliContext): The current CLI context.
Returns:
CompletedProcess: Result of the orchestrator command.
"""
# Generate the command string.
command = [
"helm",
"upgrade",
"--install",
"--namespace",
cli_context.get_project_name(make_helm_safe=True),
"--create-namespace",
]
# Set values args.
for arg in self.__generate_values_args(
cli_context.get_generated_configuration_dir()
):
command.append(arg)
# Set release name.
command.append(cli_context.get_project_name(make_helm_safe=True))
# Set chart location.
# If we're in `DEV_MODE` and `<APP-NAME>_DEV_MODE_HELM_CHART` is set, use that.
if (
cli_context.is_dev_mode
and f"{cli_context.app_name_slug.upper()}_{HelmOrchestrator.DEV_CHART_VARIABLE_NAME}"
in cli_context.dev_mode_variables.keys()
):
with wrap_dev_mode():
chart_location = cli_context.dev_mode_variables[
f"{cli_context.app_name_slug.upper()}_{HelmOrchestrator.DEV_CHART_VARIABLE_NAME}"
]
logger.debug(
f"Found DEV_MODE chart. Ignoring bundled chart from `{self.chart_location}`"
)
logger.debug(f"Deploying chart from `{chart_location}`")
# If not, then generate the absolute path to the `chart_location`.
else:
chart_location = (
cli_context.get_generated_configuration_dir() / self.chart_location
)
command.append(chart_location)
# Run the command.
return self.__run_command(command)
def shutdown(
self, cli_context: CliContext, service_name: tuple[str, ...] = None
) -> CompletedProcess:
"""
Uninstalls a helm chart from current kubernetes cluster.
Args:
cli_context (CliContext): The current CLI context.
Returns:
CompletedProcess: Result of the orchestrator command.
"""
# Generate the command string.
command = [
"helm",
"uninstall",
cli_context.get_project_name(make_helm_safe=True),
"-n",
cli_context.get_project_name(make_helm_safe=True),
]
# Run the command.
return self.__run_command(command)
def status(
self, cli_context: CliContext, service_name: tuple[str, ...] = None
) -> CompletedProcess:
"""
Get the status of the chart (through `helm status`).
Args:
cli_context (CliContext): The current CLI context.
Returns:
CompletedProcess: Result of the orchestrator command.
"""
# Generate the command string.
command = [
"helm",
"status",
cli_context.get_project_name(make_helm_safe=True),
"-n",
cli_context.get_project_name(make_helm_safe=True),
]
# Run the command.
return self.__run_command(command)
def task(
self,
cli_context: CliContext,
service_name: str,
extra_args: Iterable[str],
detached: bool = False,
) -> CompletedProcess:
logger.info("HelmOrchestrator has no services to run tasks for.")
return None
def exec(
self,
cli_context: CliContext,
service_name: str,
command: Iterable[str],
stdin_input: str = None,
capture_output: bool = False,
) -> CompletedProcess:
logger.info("HelmOrchestrator does not support executing arbitrary commands.")
return None
def get_logs_command(self) -> click.Command:
@click.command()
def log():
logger.info("HelmOrchestrator does not support getting logs.")
return None
return log
def get_additional_commands(self):
# TODO: AF-248: Add `kubectl`, `helm` and possibly `k9s` as commands that can be called.
return []
def get_name(self) -> str:
return "helm"
def verify_service_names(
self, cli_context: CliContext, service_names: tuple[str, ...]
) -> bool:
if service_names is None or len(service_names) == 0:
return True
logger.info("HelmOrchestrator has no services.")
return False
def get_disabled_commands(self) -> list[str]:
# The `init` command is disabled because it's used to initialise additional services, and this orchestrator
# The `task` command is disabled because helm is only used for install/upgrade/uninstall/downgrade operations.
# has no services. NOTE: The `init` command will be removed in the future.
return ["init", "task"]
def __run_command(self, command: list[str]) -> CompletedProcess:
"""Run the given command and return the CompletedProcess.
Args:
command (list[str]): The command to execute.
Returns:
CompletedProcess: The execution result.
"""
logger.debug(f"Executing {str(command)}")
result = subprocess.run(command, capture_output=False)
if result.returncode != 0:
message = f"Unknown error from running: {str(command)}."
logger.error(message)
raise subprocess.CalledProcessError(
result.returncode,
command,
result.stdout,
result.stderr,
)
return result
def __generate_values_args(self, generated_configuration_dir: Path) -> list[str]:
"""Recursively takes all the values files in the directories and generates an args array to
pass to helm through either `--values` or `--set-file` (depending on the location).
e.g:
["--values", "values.yaml", "--set-file", "path.to.foo=path/to/foo.yaml"...
Args:
generated_configuration_dir (Path): Generated configuration directory form the cli object, e.g:
`cli_context.get_generated_configuration_dir()`
Returns:
list[str]: The arg list.
"""
arg_list = []
# Create all `--values` args.
values_dir = generated_configuration_dir / self.helm_set_values_dir
values = [
file
for file in list(values_dir.rglob("*"))
if file.suffix in [".yml", ".yaml"]
]
for file in values:
arg_list.append("--values")
arg_list.append(str(file))
# Create all `--set-file` args.
values_files_dir = generated_configuration_dir / self.helm_set_files_dir
values_files = [
file for file in list(values_files_dir.rglob("*")) if file.is_file()
]
for file in values_files:
# NOTE: Make path relative to `helm_values_files_dir` so we know which helm key to set it as.
relative_file = file.relative_to(
generated_configuration_dir / self.helm_set_files_dir
)
# Get the helm key in `dot.notation.to.value`
key = ".".join(
[*relative_file.parent.parts, relative_file.stem.split(".")[0]]
)
arg_list.append("--set-file")
arg_list.append(f"{key}={file}")
return arg_list
class NullOrchestrator(Orchestrator):
"""
Orchestrator which has no services to orchestrate. This is useful for appcli applications which
consist only of the launcher container containing various additional CLI command groups.
"""
def start(
self, cli_context: CliContext, service_name: tuple[str, ...] = None
) -> CompletedProcess:
logger.info("NullOrchestrator has no services to start.")
return None
def shutdown(
self, cli_context: CliContext, service_name: tuple[str, ...] = None
) -> CompletedProcess:
logger.info("NullOrchestrator has no services to shutdown.")
return None
def status(
self, cli_context: CliContext, service_name: tuple[str, ...] = None
) -> CompletedProcess:
logger.info("NullOrchestrator has no services to get the status of.")
return None
def task(
self,
cli_context: CliContext,
service_name: str,
extra_args: Iterable[str],
detached: bool = False,
) -> CompletedProcess:
logger.info("NullOrchestrator has no services to run tasks for.")
return None
def exec(
self,
cli_context: CliContext,
service_name: str,
command: Iterable[str],
stdin_input: str = None,
capture_output: bool = False,
) -> CompletedProcess:
logger.info(
"NullOrchestrator has no running containers to execute commands in."
)
return None
def get_logs_command(self) -> click.Command:
@click.command()
def log():
logger.info("NullOrchestrator has no services to get logs of.")
return None
return log
def get_name(self) -> str:
return "null_orchestrator"
def verify_service_names(
self, cli_context: CliContext, service_names: tuple[str, ...]
) -> bool:
if service_names is None or len(service_names) == 0:
return True
logger.info("NullOrchestrator has no services.")
return False
def get_disabled_commands(self) -> list[str]:
# The `service` and `task` commands are disabled because they are used for managing and interacting with
# services, and this orchestrator has no services.
# The `init` command is disabled because it's used to initialise additional services, and this orchestrator
# has no services. NOTE: The `init` command will be removed in the future.
return ["init", "service", "task"]
# ------------------------------------------------------------------------------
# PUBLIC METHODS
# ------------------------------------------------------------------------------
def service_name_verifier(
service_names: tuple[str, ...], valid_service_names: List[str]
) -> bool:
"""Verify all services exist.
Args:
service_names (tuple[str, ...]): The list of service names to check.
valid_service_names [List[str]]: The list of valid service names.
"""
invalid_service_names = set(service_names) - set(valid_service_names)
for service_name in invalid_service_names:
logger.error("Service [%s] does not exist", service_name)
return len(invalid_service_names) == 0
def decrypt_docker_compose_files(
cli_context: CliContext,
docker_compose_file_relative_path: Path,
docker_compose_override_directory_relative_path: Path,
) -> List[Path]:
"""Decrypt docker-compose and docker-compose override files.
Args:
cli_context (CliContext): The current CLI context.
docker_compose_file_relative_path (Path): The relative path to the docker-compose file. Path is relative to the
generated configuration directory.
docker_compose_override_directory_relative_path (Path): The relative path to a directory containing
docker-compose override files. Path is relative to the generated configuration directory.
Returns:
List[Path]: sorted list of absolute paths to decrypted docker-compose files. The first path is the decrypted
docker-compose file, and the rest of the paths are the alphanumerically sorted docker compose override
files in the docker compose override directory.
"""
compose_files = []