2
2
import base64
3
3
import calendar
4
4
import collections
5
- import configparser
6
5
import datetime
7
6
import ipaddress
8
7
import json
37
36
from senza .components .elastigroup import ELASTIGROUP_RESOURCE_TYPE
38
37
from .spotinst .components import elastigroup_api
39
38
from .arguments import (
40
- GLOBAL_OPTIONS ,
41
39
json_output_option ,
42
40
output_option ,
43
41
parameter_file_option ,
46
44
watch_option ,
47
45
watchrefresh_option ,
48
46
field_option ,
47
+ get_region
49
48
)
50
49
from .aws import (
51
50
StackReference ,
@@ -432,27 +431,6 @@ def read_parameter_file(parameter_file):
432
431
return tuple (paras )
433
432
434
433
435
- def get_region (region ):
436
- if not region :
437
- region = GLOBAL_OPTIONS .get ("region" )
438
- if not region :
439
- config = configparser .ConfigParser ()
440
- try :
441
- config .read (os .path .expanduser ("~/.aws/config" ))
442
- if "default" in config :
443
- region = config ["default" ]["region" ]
444
- except Exception :
445
- pass
446
-
447
- if not region :
448
- raise click .UsageError (
449
- "Please specify the AWS region on the "
450
- "command line (--region) or in ~/.aws/config"
451
- )
452
-
453
- return region
454
-
455
-
456
434
def check_credentials (region ):
457
435
iam = BotoClientProxy ("iam" )
458
436
return iam .list_account_aliases ()
@@ -523,7 +501,6 @@ def all_with_version(stack_refs: list):
523
501
def list_stacks (region , stack_ref , all , output , field , w , watch ):
524
502
"""List Cloud Formation stacks"""
525
503
526
- region = get_region (region )
527
504
check_credentials (region )
528
505
529
506
stack_refs = get_stack_refs (stack_ref )
@@ -652,7 +629,6 @@ def get_classic_load_balancer_metrics(cloudwatch, lb_name, start, now):
652
629
def health (region , stack_ref , all , output , field , w , watch ):
653
630
"""Show stack health (ELB req/s, ..)"""
654
631
655
- region = get_region (region )
656
632
check_credentials (region )
657
633
658
634
cloudwatch = BotoClientProxy ("cloudwatch" , region )
@@ -753,7 +729,6 @@ def create(
753
729
):
754
730
"""Create a new Cloud Formation stack from the given Senza definition file"""
755
731
756
- region = get_region (region )
757
732
data = create_cf_template (
758
733
definition , region , version , parameter , force , parameter_file
759
734
)
@@ -825,7 +800,6 @@ def update(
825
800
"""Update an existing Cloud Formation stack from the given Senza
826
801
definition file"""
827
802
828
- region = get_region (region )
829
803
data = create_cf_template (
830
804
definition , region , version , parameter , force , parameter_file
831
805
)
@@ -844,7 +818,6 @@ def update(
844
818
def print_cfjson (definition , region , version , parameter , output , force , parameter_file ):
845
819
"""Print the generated Cloud Formation template"""
846
820
847
- region = get_region (region )
848
821
data = create_cf_template (
849
822
definition , region , version , parameter , force , parameter_file , pretty = True
850
823
)
@@ -953,7 +926,6 @@ def delete(stack_ref, region, dry_run, force, interactive, ignore_non_existent):
953
926
"""Delete a single Cloud Formation stack"""
954
927
955
928
stack_refs = get_stack_refs (stack_ref )
956
- region = get_region (region )
957
929
check_credentials (region )
958
930
959
931
if not stack_refs :
@@ -1015,7 +987,6 @@ def resources(stack_ref, region, w, watch, output, field):
1015
987
"""Show all resources of a single Cloud Formation stack"""
1016
988
1017
989
stack_refs = get_stack_refs (stack_ref )
1018
- region = get_region (region )
1019
990
check_credentials (region )
1020
991
cf = BotoClientProxy ("cloudformation" , region )
1021
992
@@ -1063,7 +1034,6 @@ def events(stack_ref, region, w, watch, output, field):
1063
1034
"""Show all Cloud Formation events for a single stack"""
1064
1035
1065
1036
stack_refs = get_stack_refs (stack_ref )
1066
- region = get_region (region )
1067
1037
check_credentials (region )
1068
1038
cf = BotoClientProxy ("cloudformation" , region )
1069
1039
@@ -1120,7 +1090,6 @@ def events(stack_ref, region, w, watch, output, field):
1120
1090
def init (definition_file , region , template , user_variable ):
1121
1091
"""Initialize a new Senza definition"""
1122
1092
1123
- region = get_region (region )
1124
1093
check_credentials (region )
1125
1094
account_info = AccountArguments (region = region )
1126
1095
@@ -1263,7 +1232,6 @@ def instances(
1263
1232
"""List the stack's EC2 instances"""
1264
1233
1265
1234
stack_refs = get_stack_refs (stack_ref )
1266
- region = get_region (region )
1267
1235
check_credentials (region )
1268
1236
1269
1237
ec2 = boto3 .resource ("ec2" , region )
@@ -1358,7 +1326,6 @@ def status(stack_ref, region, output, field, w, watch):
1358
1326
"""Show stack status information"""
1359
1327
1360
1328
stack_refs = get_stack_refs (stack_ref )
1361
- region = get_region (region )
1362
1329
check_credentials (region )
1363
1330
1364
1331
ec2 = boto3 .resource ("ec2" , region )
@@ -1451,7 +1418,6 @@ def domains(stack_ref, region, output, field, w, watch):
1451
1418
"""List the stack's Route53 domains"""
1452
1419
1453
1420
stack_refs = get_stack_refs (stack_ref )
1454
- region = get_region (region )
1455
1421
check_credentials (region )
1456
1422
1457
1423
cf = boto3 .resource ("cloudformation" , region )
@@ -1560,7 +1526,6 @@ def traffic(stack_name, stack_version, percentage, region, output, timeout, inte
1560
1526
"""Route traffic to a specific stack (weighted DNS record)"""
1561
1527
1562
1528
stack_refs = get_stack_refs ([stack_name , stack_version ])
1563
- region = get_region (region )
1564
1529
check_credentials (region )
1565
1530
1566
1531
with OutputFormat (output ):
@@ -1593,7 +1558,6 @@ def images(stack_ref, region, output, field, hide_older_than, show_instances):
1593
1558
"""Show all used AMIs and available Taupage AMIs"""
1594
1559
1595
1560
stack_refs = get_stack_refs (stack_ref )
1596
- region = get_region (region )
1597
1561
check_credentials (region )
1598
1562
1599
1563
ec2 = boto3 .resource ("ec2" , region )
@@ -1730,7 +1694,6 @@ def console(instance_or_stack_ref, limit, region, w, watch):
1730
1694
# filter out instances not part of any stack
1731
1695
filters = [{"Name" : "tag-key" , "Values" : ["aws:cloudformation:stack-name" ]}]
1732
1696
1733
- region = get_region (region )
1734
1697
check_credentials (region )
1735
1698
1736
1699
ec2 = boto3 .resource ("ec2" , region )
@@ -1784,7 +1747,6 @@ def dump(stack_ref, region, output):
1784
1747
"""Dump Cloud Formation template of existing stack"""
1785
1748
1786
1749
stack_refs = get_stack_refs (stack_ref )
1787
- region = get_region (region )
1788
1750
check_credentials (region )
1789
1751
1790
1752
cf = BotoClientProxy ("cloudformation" , region )
@@ -1848,7 +1810,6 @@ def patch(stack_ref, region, image, instance_type, user_data):
1848
1810
Currently supports patching ASG launch configurations and ElastiGroup groups."""
1849
1811
1850
1812
stack_refs = get_stack_refs (stack_ref )
1851
- region = get_region (region )
1852
1813
check_credentials (region )
1853
1814
1854
1815
if image in taupage .CHANNELS :
@@ -1948,7 +1909,6 @@ def respawn_instances(stack_ref, inplace, force, batch_size_percentage, batch_pe
1948
1909
Performs a rolling update to prevent downtimes."""
1949
1910
1950
1911
stack_refs = get_stack_refs (stack_ref )
1951
- region = get_region (region )
1952
1912
check_credentials (region )
1953
1913
1954
1914
stacks = get_stacks (stack_refs , region )
@@ -1980,7 +1940,6 @@ def scale(stack_ref, region, desired_capacity, min_size, force):
1980
1940
"""Scale Auto Scaling Group to desired capacity"""
1981
1941
1982
1942
stack_refs = get_stack_refs (stack_ref )
1983
- region = get_region (region )
1984
1943
check_credentials (region )
1985
1944
1986
1945
stacks = get_stacks (stack_refs , region )
@@ -2120,7 +2079,6 @@ def wait(stack_ref, region, deletion, timeout, interval):
2120
2079
"""
2121
2080
2122
2081
stack_refs = get_stack_refs (stack_ref )
2123
- region = get_region (region )
2124
2082
cf = BotoClientProxy ("cloudformation" , region )
2125
2083
2126
2084
target_status = (
0 commit comments