forked from Azure/batch-shipyard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshipyard.py
executable file
·1714 lines (1501 loc) · 51 KB
/
shipyard.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
# Copyright (c) Microsoft Corporation
#
# All rights reserved.
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# compat imports
from __future__ import absolute_import, division, print_function
from builtins import ( # noqa
bytes, dict, int, list, object, range, str, ascii, chr, hex, input,
next, oct, open, pow, round, super, filter, map, zip)
# stdlib imports
import json
import logging
try:
import pathlib2 as pathlib
except ImportError:
import pathlib
# non-stdlib imports
import click
# local imports
import convoy.clients
import convoy.fleet
import convoy.settings
import convoy.util
# create logger
logger = logging.getLogger('shipyard')
# global defines
_CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
class CliContext(object):
"""CliContext class: holds context for CLI commands"""
def __init__(self):
"""Ctor for CliContext"""
self.show_config = False
self.verbose = False
self.yes = False
self.config = None
self.json_fs = None
# clients
self.batch_mgmt_client = None
self.batch_client = None
self.blob_client = None
self.queue_client = None
self.table_client = None
self.keyvault_client = None
self.resource_client = None
self.compute_client = None
self.network_client = None
# aad/keyvault options
self.keyvault_uri = None
self.keyvault_credentials_secret_id = None
self.aad_directory_id = None
self.aad_application_id = None
self.aad_auth_key = None
self.aad_user = None
self.aad_password = None
self.aad_cert_private_key = None
self.aad_cert_thumbprint = None
self.aad_endpoint = None
# management options
self.subscription_id = None
def initialize_for_fs(self):
# type: (CliContext) -> None
"""Initialize context for fs commands
:param CliContext self: this
"""
self._read_credentials_config()
self._set_global_cli_options()
self.keyvault_client = convoy.clients.create_keyvault_client(self)
self._init_config(
skip_global_config=False, skip_pool_config=True, fs_storage=True)
self.resource_client, self.compute_client, self.network_client, \
_, _ = convoy.clients.create_arm_clients(self)
self.blob_client, _, _ = convoy.clients.create_storage_clients()
self._cleanup_after_initialize(
skip_global_config=False, skip_pool_config=True)
def initialize_for_keyvault(self):
# type: (CliContext) -> None
"""Initialize context for keyvault commands
:param CliContext self: this
"""
self._read_credentials_config()
self._set_global_cli_options()
self.keyvault_client = convoy.clients.create_keyvault_client(self)
self._init_config(
skip_global_config=True, skip_pool_config=True, fs_storage=False)
self._cleanup_after_initialize(
skip_global_config=True, skip_pool_config=True)
def initialize_for_batch(self):
# type: (CliContext) -> None
"""Initialize context for batch commands
:param CliContext self: this
"""
self._read_credentials_config()
self._set_global_cli_options()
self.keyvault_client = convoy.clients.create_keyvault_client(self)
self._init_config(
skip_global_config=False, skip_pool_config=False, fs_storage=False)
self.resource_client, self.compute_client, self.network_client, \
self.batch_mgmt_client, self.batch_client = \
convoy.clients.create_arm_clients(self, batch_clients=True)
self.blob_client, self.queue_client, self.table_client = \
convoy.clients.create_storage_clients()
self._cleanup_after_initialize(
skip_global_config=False, skip_pool_config=False)
def initialize_for_storage(self):
# type: (CliContext) -> None
"""Initialize context for storage commands
:param CliContext self: this
"""
self._read_credentials_config()
self._set_global_cli_options()
self.keyvault_client = convoy.clients.create_keyvault_client(self)
self._init_config(
skip_global_config=False, skip_pool_config=False, fs_storage=False)
self.blob_client, self.queue_client, self.table_client = \
convoy.clients.create_storage_clients()
self._cleanup_after_initialize(
skip_global_config=False, skip_pool_config=False)
def _set_global_cli_options(self):
# type: (CliContext) -> None
"""Set global cli options
:param CliContext self: this
"""
if self.config is None:
self.config = {}
# set internal config kv pairs
self.config['_verbose'] = self.verbose
self.config['_auto_confirm'] = self.yes
# increase detail in logger formatters
if self.verbose:
convoy.util.set_verbose_logger_handlers()
def _cleanup_after_initialize(
self, skip_global_config, skip_pool_config):
# type: (CliContext) -> None
"""Cleanup after initialize_for_* funcs
:param CliContext self: this
:param bool skip_global_config: skip global config
:param bool skip_pool_config: skip pool config
"""
# free json objects
del self.json_credentials
del self.json_fs
if not skip_global_config:
del self.json_config
if not skip_pool_config:
del self.json_pool
del self.json_jobs
# free cli options
del self.verbose
del self.yes
del self.aad_directory_id
del self.aad_application_id
del self.aad_auth_key
del self.aad_user
del self.aad_password
del self.aad_cert_private_key
del self.aad_cert_thumbprint
del self.aad_endpoint
del self.keyvault_credentials_secret_id
del self.subscription_id
def _read_json_file(self, json_file):
# type: (CliContext, pathlib.Path) -> None
"""Read a json file into self.config, while checking for invalid
JSON and returning an error that makes sense if ValueError
:param CliContext self: this
:param pathlib.Path json_file: json file to load
"""
try:
with json_file.open('r') as f:
if self.config is None:
self.config = json.load(f)
else:
self.config = convoy.util.merge_dict(
self.config, json.load(f))
except ValueError:
raise ValueError(
('Detected invalid JSON in file: {}. Please ensure the JSON '
'is valid and is encoded UTF-8 without BOM.'.format(
json_file)))
def _read_credentials_config(self):
# type: (CliContext) -> None
"""Read credentials config file only
:param CliContext self: this
"""
# use configdir if available
if self.configdir is not None and self.json_credentials is None:
self.json_credentials = pathlib.Path(
self.configdir, 'credentials.json')
if self.json_credentials is not None:
if not isinstance(self.json_credentials, pathlib.Path):
self.json_credentials = pathlib.Path(self.json_credentials)
if self.json_credentials.exists():
self._read_json_file(self.json_credentials)
def _init_config(
self, skip_global_config=False, skip_pool_config=False,
fs_storage=False):
# type: (CliContext, bool, bool, bool) -> None
"""Initializes configuration of the context
:param CliContext self: this
:param bool skip_global_config: skip global config
:param bool skip_pool_config: skip pool config
:param bool fs_storage: adjust storage settings for fs
"""
# reset config
self.config = None
self._set_global_cli_options()
# use configdir if available
if self.configdir is not None:
if self.json_credentials is None:
self.json_credentials = pathlib.Path(
self.configdir, 'credentials.json')
if not skip_global_config and self.json_config is None:
self.json_config = pathlib.Path(
self.configdir, 'config.json')
if not skip_pool_config:
if self.json_pool is None:
self.json_pool = pathlib.Path(self.configdir, 'pool.json')
if self.json_jobs is None:
self.json_jobs = pathlib.Path(self.configdir, 'jobs.json')
if self.json_fs is None:
self.json_fs = pathlib.Path(self.configdir, 'fs.json')
# check for required json files
if (self.json_credentials is not None and
not isinstance(self.json_credentials, pathlib.Path)):
self.json_credentials = pathlib.Path(self.json_credentials)
if not skip_global_config:
if self.json_config is None:
raise ValueError('config json was not specified')
elif not isinstance(self.json_config, pathlib.Path):
self.json_config = pathlib.Path(self.json_config)
if not skip_pool_config:
if self.json_pool is None:
raise ValueError('pool json was not specified')
elif not isinstance(self.json_pool, pathlib.Path):
self.json_pool = pathlib.Path(self.json_pool)
if (self.json_fs is not None and not isinstance(
self.json_fs, pathlib.Path)):
self.json_fs = pathlib.Path(self.json_fs)
# fetch credentials from keyvault, if json file is missing
kvcreds = None
if self.json_credentials is None or not self.json_credentials.exists():
kvcreds = convoy.fleet.fetch_credentials_json_from_keyvault(
self.keyvault_client, self.keyvault_uri,
self.keyvault_credentials_secret_id)
# read credentials json, perform special keyvault processing if
# required sections are missing
if kvcreds is None:
self._read_json_file(self.json_credentials)
kv = convoy.settings.credentials_keyvault(self.config)
self.keyvault_uri = self.keyvault_uri or kv.keyvault_uri
self.keyvault_credentials_secret_id = (
self.keyvault_credentials_secret_id or
kv.keyvault_credentials_secret_id
)
if self.keyvault_credentials_secret_id is not None:
try:
convoy.settings.credentials_batch(self.config)
if len(list(convoy.settings.iterate_storage_credentials(
self.config))) == 0:
raise KeyError()
except KeyError:
# fetch credentials from keyvault
self.config = \
convoy.fleet.fetch_credentials_json_from_keyvault(
self.keyvault_client, self.keyvault_uri,
self.keyvault_credentials_secret_id)
else:
self.config = kvcreds
del kvcreds
# re-populate global cli options again
self._set_global_cli_options()
# parse any keyvault secret ids from credentials
convoy.fleet.fetch_secrets_from_keyvault(
self.keyvault_client, self.config)
# read rest of config files
if not skip_global_config:
self._read_json_file(self.json_config)
# read fs config regardless of skip setting
if self.json_fs is not None and self.json_fs.exists():
self._read_json_file(self.json_fs)
if not skip_pool_config:
self._read_json_file(self.json_pool)
if self.json_jobs is not None:
if not isinstance(self.json_jobs, pathlib.Path):
self.json_jobs = pathlib.Path(self.json_jobs)
if self.json_jobs.exists():
self._read_json_file(self.json_jobs)
# adjust settings
if not skip_global_config:
convoy.fleet.check_for_invalid_config(self.config)
convoy.fleet.populate_global_settings(self.config, fs_storage)
# show config if specified
if self.show_config:
logger.debug('config:\n' + json.dumps(self.config, indent=4))
def _set_clients(
self, batch_mgmt_client, batch_client, blob_client, queue_client,
table_client):
"""Sets clients for the context"""
self.batch_mgmt_client = batch_mgmt_client
self.batch_client = batch_client
self.blob_client = blob_client
self.queue_client = queue_client
self.table_client = table_client
# create a pass decorator for shared context between commands
pass_cli_context = click.make_pass_decorator(CliContext, ensure=True)
def _confirm_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.yes = value
return value
return click.option(
'-y', '--yes',
expose_value=False,
is_flag=True,
help='Assume yes for all confirmation prompts',
callback=callback)(f)
def _log_file_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.logfile = value
return value
return click.option(
'--log-file',
expose_value=False,
envvar='SHIPYARD_LOG_FILE',
help='Log to file',
callback=callback)(f)
def _show_config_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.show_config = value
return value
return click.option(
'--show-config',
expose_value=False,
is_flag=True,
help='Show configuration',
callback=callback)(f)
def _verbose_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.verbose = value
return value
return click.option(
'-v', '--verbose',
expose_value=False,
is_flag=True,
help='Verbose output',
callback=callback)(f)
def _azure_keyvault_uri_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.keyvault_uri = value
return value
return click.option(
'--keyvault-uri',
expose_value=False,
envvar='SHIPYARD_KEYVAULT_URI',
help='Azure KeyVault URI',
callback=callback)(f)
def _azure_keyvault_credentials_secret_id_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.keyvault_credentials_secret_id = value
return value
return click.option(
'--keyvault-credentials-secret-id',
expose_value=False,
envvar='SHIPYARD_KEYVAULT_CREDENTIALS_SECRET_ID',
help='Azure KeyVault credentials secret id',
callback=callback)(f)
def _aad_directory_id_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.aad_directory_id = value
return value
return click.option(
'--aad-directory-id',
expose_value=False,
envvar='SHIPYARD_AAD_DIRECTORY_ID',
help='Azure Active Directory directory (tenant) id',
callback=callback)(f)
def _aad_application_id_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.aad_application_id = value
return value
return click.option(
'--aad-application-id',
expose_value=False,
envvar='SHIPYARD_AAD_APPLICATION_ID',
help='Azure Active Directory application (client) id',
callback=callback)(f)
def _aad_auth_key_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.aad_auth_key = value
return value
return click.option(
'--aad-auth-key',
expose_value=False,
envvar='SHIPYARD_AAD_AUTH_KEY',
help='Azure Active Directory authentication key',
callback=callback)(f)
def _aad_user_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.aad_user = value
return value
return click.option(
'--aad-user',
expose_value=False,
envvar='SHIPYARD_AAD_USER',
help='Azure Active Directory user',
callback=callback)(f)
def _aad_password_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.aad_password = value
return value
return click.option(
'--aad-password',
expose_value=False,
envvar='SHIPYARD_AAD_PASSWORD',
help='Azure Active Directory password',
callback=callback)(f)
def _aad_cert_private_key_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.aad_cert_private_key = value
return value
return click.option(
'--aad-cert-private-key',
expose_value=False,
envvar='SHIPYARD_AAD_CERT_PRIVATE_KEY',
help='Azure Active Directory private key for X.509 certificate',
callback=callback)(f)
def _aad_cert_thumbprint_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.aad_cert_thumbprint = value
return value
return click.option(
'--aad-cert-thumbprint',
expose_value=False,
envvar='SHIPYARD_AAD_CERT_THUMBPRINT',
help='Azure Active Directory certificate SHA1 thumbprint',
callback=callback)(f)
def _aad_endpoint_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.aad_endpoint = value
return value
return click.option(
'--aad-endpoint',
expose_value=False,
envvar='SHIPYARD_AAD_ENDPOINT',
help='Azure Active Directory endpoint',
callback=callback)(f)
def _azure_subscription_id_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.subscription_id = value
return value
return click.option(
'--subscription-id',
expose_value=False,
envvar='SHIPYARD_SUBSCRIPTION_ID',
help='Azure Subscription ID',
callback=callback)(f)
def _configdir_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.configdir = value
return value
return click.option(
'--configdir',
expose_value=False,
envvar='SHIPYARD_CONFIGDIR',
help='Configuration directory where all configuration files can be '
'found. Each json config file must be named exactly the same as the '
'regular switch option, e.g., pool.json for --pool. Individually '
'specified config options take precedence over this option.',
callback=callback)(f)
def _credentials_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.json_credentials = value
return value
return click.option(
'--credentials',
expose_value=False,
envvar='SHIPYARD_CREDENTIALS_JSON',
help='Credentials json config file',
callback=callback)(f)
def _config_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.json_config = value
return value
return click.option(
'--config',
expose_value=False,
envvar='SHIPYARD_CONFIG_JSON',
help='Global json config file',
callback=callback)(f)
def _pool_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.json_pool = value
return value
return click.option(
'--pool',
expose_value=False,
envvar='SHIPYARD_POOL_JSON',
help='Pool json config file',
callback=callback)(f)
def _jobs_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.json_jobs = value
return value
return click.option(
'--jobs',
expose_value=False,
envvar='SHIPYARD_JOBS_JSON',
help='Jobs json config file',
callback=callback)(f)
def fs_option(f):
def callback(ctx, param, value):
clictx = ctx.ensure_object(CliContext)
clictx.json_fs = value
return value
return click.option(
'--fs',
expose_value=False,
envvar='SHIPYARD_FS_JSON',
help='Filesystem json config file',
callback=callback)(f)
def _storage_cluster_id_argument(f):
def callback(ctx, param, value):
return value
return click.argument(
'storage-cluster-id',
callback=callback)(f)
def common_options(f):
f = _config_option(f)
f = _credentials_option(f)
f = _configdir_option(f)
f = _verbose_option(f)
f = _show_config_option(f)
# f = _log_file_option(f)
f = _confirm_option(f)
return f
def aad_options(f):
f = _aad_cert_thumbprint_option(f)
f = _aad_cert_private_key_option(f)
f = _aad_password_option(f)
f = _aad_user_option(f)
f = _aad_auth_key_option(f)
f = _aad_application_id_option(f)
f = _aad_directory_id_option(f)
f = _aad_endpoint_option(f)
return f
def batch_options(f):
f = _azure_subscription_id_option(f)
f = _jobs_option(f)
f = _pool_option(f)
return f
def keyvault_options(f):
f = _azure_keyvault_credentials_secret_id_option(f)
f = _azure_keyvault_uri_option(f)
return f
def fs_options(f):
f = _azure_subscription_id_option(f)
f = fs_option(f)
return f
def fs_cluster_options(f):
f = fs_options(f)
f = _storage_cluster_id_argument(f)
return f
@click.group(context_settings=_CONTEXT_SETTINGS)
@click.version_option(version=convoy.__version__)
@click.pass_context
def cli(ctx):
"""Batch Shipyard: Provision and Execute Docker Workloads on Azure Batch"""
pass
@cli.group()
@pass_cli_context
def fs(ctx):
"""Filesystem in Azure actions"""
pass
@fs.group()
@pass_cli_context
def cluster(ctx):
"""Filesystem storage cluster in Azure actions"""
pass
@cluster.command('add')
@common_options
@fs_cluster_options
@aad_options
@pass_cli_context
def fs_cluster_add(ctx, storage_cluster_id):
"""Create a filesystem storage cluster in Azure"""
ctx.initialize_for_fs()
convoy.fleet.action_fs_cluster_add(
ctx.resource_client, ctx.compute_client, ctx.network_client,
ctx.blob_client, ctx.config, storage_cluster_id)
@cluster.command('resize')
@common_options
@fs_cluster_options
@aad_options
@pass_cli_context
def fs_cluster_resize(ctx, storage_cluster_id):
"""Resize a filesystem storage cluster in Azure. Only increasing the
storage cluster size is supported."""
ctx.initialize_for_fs()
convoy.fleet.action_fs_cluster_resize(
ctx.compute_client, ctx.network_client, ctx.blob_client, ctx.config,
storage_cluster_id)
@cluster.command('expand')
@click.option(
'--no-rebalance', is_flag=True,
help='Do not rebalance filesystem, if applicable')
@common_options
@fs_cluster_options
@aad_options
@pass_cli_context
def fs_cluster_expand(ctx, storage_cluster_id, no_rebalance):
"""Expand a filesystem storage cluster in Azure"""
ctx.initialize_for_fs()
convoy.fleet.action_fs_cluster_expand(
ctx.compute_client, ctx.network_client, ctx.config,
storage_cluster_id, not no_rebalance)
@cluster.command('del')
@click.option(
'--delete-resource-group', is_flag=True,
help='Delete all resources in the storage cluster resource group')
@click.option(
'--delete-data-disks', is_flag=True,
help='Delete all attached managed data disks')
@click.option(
'--delete-virtual-network', is_flag=True, help='Delete virtual network')
@click.option(
'--generate-from-prefix', is_flag=True,
help='Generate resources to delete from storage cluster hostname prefix')
@click.option(
'--no-wait', is_flag=True, help='Do not wait for deletion to complete')
@common_options
@fs_cluster_options
@aad_options
@pass_cli_context
def fs_cluster_del(
ctx, storage_cluster_id, delete_resource_group, delete_data_disks,
delete_virtual_network, generate_from_prefix, no_wait):
"""Delete a filesystem storage cluster in Azure"""
ctx.initialize_for_fs()
convoy.fleet.action_fs_cluster_del(
ctx.resource_client, ctx.compute_client, ctx.network_client,
ctx.blob_client, ctx.config, storage_cluster_id,
delete_resource_group, delete_data_disks, delete_virtual_network,
generate_from_prefix, not no_wait)
@cluster.command('suspend')
@click.option(
'--no-wait', is_flag=True, help='Do not wait for suspension to complete')
@common_options
@fs_cluster_options
@aad_options
@pass_cli_context
def fs_cluster_suspend(ctx, storage_cluster_id, no_wait):
"""Suspend a filesystem storage cluster in Azure"""
ctx.initialize_for_fs()
convoy.fleet.action_fs_cluster_suspend(
ctx.compute_client, ctx.config, storage_cluster_id, not no_wait)
@cluster.command('start')
@click.option(
'--no-wait', is_flag=True, help='Do not wait for restart to complete')
@common_options
@fs_cluster_options
@aad_options
@pass_cli_context
def fs_cluster_start(ctx, storage_cluster_id, no_wait):
"""Starts a previously suspended filesystem storage cluster in Azure"""
ctx.initialize_for_fs()
convoy.fleet.action_fs_cluster_start(
ctx.compute_client, ctx.network_client, ctx.config,
storage_cluster_id, not no_wait)
@cluster.command('status')
@click.option(
'--detail', is_flag=True, help='Detailed storage cluster status')
@click.option(
'--hosts', is_flag=True,
help='Output /etc/hosts compatible name resolution for GlusterFS clusters')
@common_options
@fs_cluster_options
@aad_options
@pass_cli_context
def fs_cluster_status(ctx, storage_cluster_id, detail, hosts):
"""Query status of a filesystem storage cluster in Azure"""
ctx.initialize_for_fs()
convoy.fleet.action_fs_cluster_status(
ctx.compute_client, ctx.network_client, ctx.config,
storage_cluster_id, detail, hosts)
@cluster.command('ssh')
@click.option(
'--cardinal',
help='Zero-based cardinal number of remote fs vm to connect to',
type=int)
@click.option(
'--hostname', help='Hostname of remote fs vm to connect to')
@click.option(
'--tty', is_flag=True, help='Allocate a pseudo-tty')
@common_options
@fs_cluster_options
@click.argument('command', nargs=-1)
@aad_options
@pass_cli_context
def fs_cluster_ssh(ctx, storage_cluster_id, cardinal, hostname, tty, command):
"""Interactively login via SSH to a filesystem storage cluster virtual
machine in Azure"""
ctx.initialize_for_fs()
convoy.fleet.action_fs_cluster_ssh(
ctx.compute_client, ctx.network_client, ctx.config,
storage_cluster_id, cardinal, hostname, tty, command)
@fs.group()
@pass_cli_context
def disks(ctx):
"""Managed disk actions"""
pass
@disks.command('add')
@common_options
@fs_options
@aad_options
@pass_cli_context
def fs_disks_add(ctx):
"""Create managed disks in Azure"""
ctx.initialize_for_fs()
convoy.fleet.action_fs_disks_add(
ctx.resource_client, ctx.compute_client, ctx.config)
@disks.command('del')
@click.option(
'--all', is_flag=True, help='Delete all disks in resource group')
@click.option(
'--name', help='Delete disk with specified name only')
@click.option(
'--resource-group',
help='Delete disks matching specified resource group only')
@click.option(
'--no-wait', is_flag=True,
help='Do not wait for disk deletion to complete')
@common_options
@fs_options
@aad_options
@pass_cli_context
def fs_disks_del(ctx, all, name, resource_group, no_wait):
"""Delete managed disks in Azure"""
ctx.initialize_for_fs()
convoy.fleet.action_fs_disks_del(
ctx.compute_client, ctx.config, name, resource_group, all, not no_wait)
@disks.command('list')
@click.option(
'--resource-group',
help='List disks matching specified resource group only')
@click.option(
'--restrict-scope', is_flag=True,
help='List disks present only in configuration if they exist')
@common_options
@fs_options
@aad_options
@pass_cli_context
def fs_disks_list(ctx, resource_group, restrict_scope):
"""List managed disks in resource group"""
ctx.initialize_for_fs()
convoy.fleet.action_fs_disks_list(
ctx.compute_client, ctx.config, resource_group, restrict_scope)
@cli.group()
@pass_cli_context
def storage(ctx):
"""Storage actions"""
pass
@storage.command('del')
@click.option(
'--clear-tables', is_flag=True, help='Clear tables instead of deleting')
@click.option(
'--poolid', help='Delete storage containers for the specified pool')
@common_options
@batch_options
@keyvault_options
@pass_cli_context
def storage_del(ctx, clear_tables, poolid):
"""Delete Azure Storage containers used by Batch Shipyard"""
ctx.initialize_for_storage()
convoy.fleet.action_storage_del(
ctx.blob_client, ctx.queue_client, ctx.table_client, ctx.config,
clear_tables, poolid)
@storage.command('clear')
@click.option(
'--poolid', help='Clear storage containers for the specified pool')
@common_options
@batch_options
@keyvault_options
@pass_cli_context
def storage_clear(ctx, poolid):
"""Clear Azure Storage containers used by Batch Shipyard"""
ctx.initialize_for_storage()
convoy.fleet.action_storage_clear(
ctx.blob_client, ctx.table_client, ctx.config, poolid)
@cli.group()
@pass_cli_context
def keyvault(ctx):
"""KeyVault actions"""
pass
@keyvault.command('add')
@click.argument('name')
@common_options
@keyvault_options
@aad_options
@pass_cli_context
def keyvault_add(ctx, name):
"""Add a credentials json as a secret to Azure KeyVault"""
ctx.initialize_for_keyvault()
convoy.fleet.action_keyvault_add(
ctx.keyvault_client, ctx.config, ctx.keyvault_uri, name)
@keyvault.command('del')
@click.argument('name')
@common_options
@keyvault_options
@aad_options
@pass_cli_context
def keyvault_del(ctx, name):
"""Delete a secret from Azure KeyVault"""
ctx.initialize_for_keyvault()
convoy.fleet.action_keyvault_del(
ctx.keyvault_client, ctx.keyvault_uri, name)
@keyvault.command('list')
@common_options
@keyvault_options
@aad_options
@pass_cli_context
def keyvault_list(ctx):
"""List secret ids and metadata in an Azure KeyVault"""
ctx.initialize_for_keyvault()
convoy.fleet.action_keyvault_list(ctx.keyvault_client, ctx.keyvault_uri)
@cli.group()
@pass_cli_context
def cert(ctx):
"""Certificate actions"""
pass
@cert.command('create')
@common_options
@batch_options
@keyvault_options
@aad_options
@pass_cli_context
def cert_create(ctx):
"""Create a certificate to use with a Batch account"""
ctx.initialize_for_batch()
convoy.fleet.action_cert_create(ctx.config)