31
31
write_json ,
32
32
whoami ,
33
33
get_package_name ,
34
- user_error ,
34
+ CFRExitError ,
35
35
is_package_url ,
36
36
print_progress_dot ,
37
- ChecksumError ,
37
+ CFRChecksumError ,
38
+ CFRUserError ,
38
39
)
39
40
from cf_remote .spawn import VM , VMRequest , Providers , AWSCredentials , GCPCredentials
40
41
from cf_remote .spawn import spawn_vms , destroy_vms , dump_vms_info , get_cloud_driver
@@ -71,6 +72,7 @@ def run(hosts, command, users=None, sudo=False, raw=False):
71
72
continue
72
73
cmd = command
73
74
lines = lines .replace ("\r " , "" )
75
+ fill = ""
74
76
for line in lines .split ("\n " ):
75
77
if raw :
76
78
print (line )
@@ -79,6 +81,7 @@ def run(hosts, command, users=None, sudo=False, raw=False):
79
81
fill = " " * (len (cmd ) + 7 )
80
82
cmd = None
81
83
else :
84
+ assert fill , "First iteration of loop should have set fill variable"
82
85
print ("{}{}'{}'" .format (host_colon , fill , line ))
83
86
return errors
84
87
@@ -123,7 +126,9 @@ def _download_urls(urls):
123
126
paths .append (path )
124
127
125
128
if path in downloaded_paths and url not in downloaded_urls :
126
- user_error ("2 packages with the same name '%s' from different URLs" % name )
129
+ raise CFRExitError (
130
+ "2 packages with the same name '%s' from different URLs" % name
131
+ )
127
132
128
133
download_package (url , path )
129
134
downloaded_urls .append (url )
@@ -143,7 +148,7 @@ def _verify_package_urls(urls):
143
148
if is_package_url (package_url ):
144
149
verified_urls .append (package_url )
145
150
else :
146
- user_error ("Wrong package URL: {}" .format (package_url ))
151
+ raise CFRExitError ("Wrong package URL: {}" .format (package_url ))
147
152
148
153
return verified_urls
149
154
@@ -184,7 +189,7 @@ def install(
184
189
else :
185
190
try :
186
191
package , hub_package , client_package = _download_urls (packages )
187
- except ChecksumError as ce :
192
+ except CFRChecksumError as ce :
188
193
log .error (ce )
189
194
return 1
190
195
@@ -288,14 +293,15 @@ def install(
288
293
def _iterate_over_packages (
289
294
tags = None , version = None , edition = None , download = False , output_dir = None
290
295
):
296
+ assert edition in ["enterprise" , "community" , None ]
291
297
releases = Releases (edition )
292
298
print ("Available releases: {}" .format (releases ))
293
299
294
300
release_versions = [rel .version for rel in releases .releases ]
295
301
if version and version not in release_versions :
296
- user_error ("CFEngine version '%s' doesn't exist (yet)." % version )
302
+ raise CFRExitError ("CFEngine version '%s' doesn't exist (yet)." % version )
297
303
298
- if not version :
304
+ if tags and not version :
299
305
for tag in tags :
300
306
if tag in release_versions :
301
307
version = tag
@@ -305,10 +311,11 @@ def _iterate_over_packages(
305
311
release = releases .default
306
312
if version :
307
313
release = releases .pick_version (version )
314
+ if not release :
315
+ raise CFRExitError ("Failed to find a release for version '%s'" % version )
308
316
print ("Using {}:" .format (release ))
309
317
log .debug ("Looking for a release based on host tags: {}" .format (tags ))
310
318
artifacts = release .find (tags )
311
-
312
319
if len (artifacts ) == 0 :
313
320
print ("No suitable packages found" )
314
321
else :
@@ -318,14 +325,14 @@ def _iterate_over_packages(
318
325
package_path = download_package (
319
326
artifact .url , checksum = artifact .checksum
320
327
)
321
- except ChecksumError as ce :
328
+ except CFRChecksumError as ce :
322
329
log .error (ce )
323
330
return 1
324
331
if output_dir :
325
332
output_dir = os .path .abspath (os .path .expanduser (output_dir ))
326
333
parent = os .path .dirname (output_dir )
327
334
if not os .path .exists (parent ):
328
- user_error (
335
+ raise CFRExitError (
329
336
"'{}' doesn't exist. Make sure this path is correct and exists." .format (
330
337
parent
331
338
)
@@ -373,16 +380,17 @@ def spawn(
373
380
public_ip = True ,
374
381
extend_group = False ,
375
382
):
383
+ creds_data = None
376
384
if os .path .exists (CLOUD_CONFIG_FPATH ):
377
385
creds_data = read_json (CLOUD_CONFIG_FPATH )
378
- else :
379
- print ("Cloud configuration not found at %s" % CLOUD_CONFIG_FPATH )
380
- return 1
386
+ if not creds_data :
387
+ raise CFRUserError ("Cloud configuration not found at %s" % CLOUD_CONFIG_FPATH )
381
388
389
+ vms_info = None
382
390
if os .path .exists (CLOUD_STATE_FPATH ):
383
391
vms_info = read_json (CLOUD_STATE_FPATH )
384
- else :
385
- vms_info = dict ()
392
+ if not vms_info :
393
+ vms_info = {}
386
394
387
395
group_key = "@%s" % group_name
388
396
group_exists = group_key in vms_info
@@ -523,6 +531,8 @@ def destroy(group_name=None):
523
531
return 1
524
532
525
533
vms_info = read_json (CLOUD_STATE_FPATH )
534
+ if not vms_info :
535
+ raise CFRUserError ("No saved VMs found in '{}'" .format (CLOUD_STATE_FPATH ))
526
536
527
537
to_destroy = []
528
538
if group_name :
@@ -541,16 +551,23 @@ def destroy(group_name=None):
541
551
542
552
region = vms_info [group_name ]["meta" ]["region" ]
543
553
provider = vms_info [group_name ]["meta" ]["provider" ]
554
+ if provider not in ["aws" , "gcp" ]:
555
+ raise CFRUserError (
556
+ "Unsupported provider '{}' encountered in '{}', only aws / gcp is supported" .format (
557
+ provider , CLOUD_STATE_FPATH
558
+ )
559
+ )
560
+
561
+ driver = None
544
562
if provider == "aws" :
545
563
if aws_creds is None :
546
- user_error ("Missing/incomplete AWS credentials" )
547
- return 1
564
+ raise CFRExitError ("Missing/incomplete AWS credentials" )
548
565
driver = get_cloud_driver (Providers .AWS , aws_creds , region )
549
566
if provider == "gcp" :
550
567
if gcp_creds is None :
551
- user_error ("Missing/incomplete GCP credentials" )
552
- return 1
568
+ raise CFRExitError ("Missing/incomplete GCP credentials" )
553
569
driver = get_cloud_driver (Providers .GCP , gcp_creds , region )
570
+ assert driver is not None
554
571
555
572
nodes = driver .list_nodes ()
556
573
for name , vm_info in vms_info [group_name ].items ():
@@ -572,16 +589,23 @@ def destroy(group_name=None):
572
589
573
590
region = vms_info [group_name ]["meta" ]["region" ]
574
591
provider = vms_info [group_name ]["meta" ]["provider" ]
592
+ if provider not in ["aws" , "gcp" ]:
593
+ raise CFRUserError (
594
+ "Unsupported provider '{}' encountered in '{}', only aws / gcp is supported" .format (
595
+ provider , CLOUD_STATE_FPATH
596
+ )
597
+ )
598
+
599
+ driver = None
575
600
if provider == "aws" :
576
601
if aws_creds is None :
577
- user_error ("Missing/incomplete AWS credentials" )
578
- return 1
602
+ raise CFRExitError ("Missing/incomplete AWS credentials" )
579
603
driver = get_cloud_driver (Providers .AWS , aws_creds , region )
580
604
if provider == "gcp" :
581
605
if gcp_creds is None :
582
- user_error ("Missing/incomplete GCP credentials" )
583
- return 1
606
+ raise CFRExitError ("Missing/incomplete GCP credentials" )
584
607
driver = get_cloud_driver (Providers .GCP , gcp_creds , region )
608
+ assert driver is not None
585
609
586
610
nodes = driver .list_nodes ()
587
611
for name , vm_info in vms_info [group_name ].items ():
@@ -673,11 +697,15 @@ def save(name, hosts, role):
673
697
674
698
675
699
def _ansible_inventory ():
676
- if not os .path .exists (CLOUD_STATE_FPATH ):
677
- print ("No saved cloud state info" )
678
- return 1
679
700
680
- vms_info = read_json (CLOUD_STATE_FPATH )
701
+ vms_info = None
702
+ if os .path .exists (CLOUD_STATE_FPATH ):
703
+ vms_info = read_json (CLOUD_STATE_FPATH )
704
+
705
+ if not vms_info :
706
+ raise CFRUserError (
707
+ "No saved cloud state info in '{}'" .format (CLOUD_STATE_FPATH )
708
+ )
681
709
all_lines = []
682
710
hub_lines = []
683
711
client_lines = []
@@ -851,7 +879,7 @@ def deploy(hubs, masterfiles):
851
879
print ("Found saved/spawned hubs: " + ", " .join (hubs ))
852
880
853
881
if not hubs :
854
- user_error (
882
+ raise CFRExitError (
855
883
"No hub to deploy to (Specify with --hub or use spawn/save commands to add to cf-remote)"
856
884
)
857
885
@@ -866,7 +894,7 @@ def deploy(hubs, masterfiles):
866
894
urls = [masterfiles ]
867
895
try :
868
896
paths = _download_urls (urls )
869
- except ChecksumError as ce :
897
+ except CFRChecksumError as ce :
870
898
log .error (ce )
871
899
return 1
872
900
assert len (paths ) == 1
@@ -876,7 +904,7 @@ def deploy(hubs, masterfiles):
876
904
if not masterfiles :
877
905
masterfiles = "."
878
906
if not (os .path .isfile ("promises.cf" ) or os .path .isfile ("promises.cf.in" )):
879
- user_error ("No cfbs or masterfiles policy set found" )
907
+ raise CFRExitError ("No cfbs or masterfiles policy set found" )
880
908
881
909
masterfiles = os .path .abspath (os .path .expanduser (masterfiles ))
882
910
print ("Found masterfiles policy set: '{}'" .format (masterfiles ))
@@ -937,23 +965,23 @@ def deploy(hubs, masterfiles):
937
965
938
966
939
967
def agent (hosts , bootstrap = None ):
940
-
941
- if len (bootstrap ) > 1 :
942
- user_error (
968
+ if bootstrap and len (bootstrap ) > 1 :
969
+ raise CFRExitError (
943
970
"Cannot boostrap {} to {}. Cannot bootstrap to more than one host." .format (
944
971
hosts , bootstrap
945
972
)
946
973
)
947
974
948
- hub_host = bootstrap [0 ]
949
-
950
975
for host in hosts :
951
976
data = get_info (host )
952
977
953
978
if not data ["agent_location" ]:
954
- user_error ("CFEngine not installed on {}" .format (host ))
979
+ raise CFRExitError ("CFEngine not installed on {}" .format (host ))
980
+
981
+ command = "{}" .format (data ["agent_location" ])
982
+ if bootstrap :
983
+ command += "--bootstrap {}" .format (bootstrap [0 ])
955
984
956
- command = "{} --bootstrap {}" .format (data ["agent_location" ], hub_host )
957
985
output = run_command (host , command , sudo = True )
958
986
if output :
959
987
print (output )
@@ -965,7 +993,7 @@ def connect_cmd(hosts):
965
993
assert hosts and len (hosts ) >= 1 # Ensured by argument parser
966
994
967
995
if len (hosts ) > 1 :
968
- user_error ("You can only connect to one host at a time" )
996
+ raise CFRExitError ("You can only connect to one host at a time" )
969
997
970
998
print ("Opening a SSH command shell..." )
971
999
r = subprocess .run (["ssh" , hosts [0 ]])
0 commit comments