Skip to content

Commit 213dbb5

Browse files
authored
Use Creds based off of zappa_settings
1 parent 756d24d commit 213dbb5

File tree

1 file changed

+62
-18
lines changed

1 file changed

+62
-18
lines changed

zappadock/zappadock.py

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
21
import os
2+
import json
33
import platform
44
import configparser
55
import traceback
@@ -29,8 +29,11 @@ def get_creds_from_env():
2929
key = os.environ.get('AWS_ACCESS_KEY_ID')
3030
secret = os.environ.get('AWS_SECRET_ACCESS_KEY')
3131
region = os.environ.get('AWS_DEFAULT_REGION')
32-
profile = 'default'
33-
return key, secret, region ,profile if not None in (key, secret, region) else False
32+
33+
if None == key or None == secret or None == region:
34+
return False
35+
else:
36+
return key, secret, region
3437

3538
def get_creds_from_credentials_file():
3639
# Get credentials from ~/.aws/credentials
@@ -54,11 +57,14 @@ def get_creds_from_credentials_file():
5457
key = config[profile].get('aws_access_key_id')
5558
secret = config[profile].get('aws_secret_access_key')
5659
region = config[profile].get('region')
57-
return key, secret, region ,profile if not None in (key, secret, region) else False
60+
return False if None in (key, secret, region) else profile
5861

5962
@click.command()
6063
def zappadock():
64+
# Set Zappadock Docker File
6165
docker_file = '.zappadock-Dockerfile'
66+
67+
# Create Dockerfile
6268
if not os.path.isfile(docker_file):
6369
click.echo(f"Creating Dockerfile.")
6470
with open(docker_file, 'w') as f:
@@ -80,16 +86,58 @@ def zappadock():
8086

8187
f.write(DOCKERFILE.format(base_image=image))
8288

83-
# Get credentials
84-
click.echo("Getting AWS credentials.")
85-
credentials = get_creds_from_env()
86-
if not credentials:
87-
credentials = get_creds_from_credentials_file()
88-
if not credentials:
89-
click.echo("Credentials not found.\nYou can set them in ~/.aws/credentials or by setting environment variables.\nSee https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html#environment-variables for more info.")
90-
click.echo("Exiting...")
91-
exit()
89+
# Check if Zappa has already been run
90+
if os.path.isfile('zappa_settings.json'):
91+
92+
# Check from settings if Zappadock should use environment variables or credentials file
93+
with open('zappa_settings.json') as f:
94+
# Try to get 'profile_name' from settings
95+
try:
96+
_ = list(json.load(f).values())[0]['profile_name']
97+
creds_type = 'file'
98+
99+
# File exists but no profile name is set
100+
except KeyError:
101+
creds_type = 'env'
102+
103+
# File exists but there are no settings or invalid JSON
104+
except (IndexError , json.decoder.JSONDecodeError):
105+
creds_type = 'any'
106+
else:
107+
creds_type = 'any'
92108

109+
# Get credentials from environment variables
110+
if creds_type == 'env':
111+
env_creds = get_creds_from_env()
112+
if env_creds:
113+
run_docker_settings = f' -e AWS_ACCESS_KEY_ID={env_creds[0]} -e AWS_SECRET_ACCESS_KEY={env_creds[1]} -e AWS_DEFAULT_REGION={env_creds[2]} '
114+
else:
115+
click.echo("Please set the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_DEFAULT_REGION environment variables.")
116+
exit()
117+
118+
# Get credentials from ~/.aws/credentials
119+
elif creds_type == 'file':
120+
profile_name = get_creds_from_credentials_file()
121+
if profile_name:
122+
run_docker_settings = f' -e AWS_PROFILE={profile_name} -v ~/.aws/:/root/.aws '
123+
else:
124+
click.echo("Your Zappa settings are configured to use credentials from ~/.aws/credentials.\nNone of the profiles in ~/.aws/credentials were found.")
125+
exit()
126+
127+
# Get credentials from any source
128+
elif creds_type == 'any':
129+
env_creds = get_creds_from_env()
130+
if env_creds:
131+
run_docker_settings = f' -e AWS_ACCESS_KEY_ID={env_creds[0]} -e AWS_SECRET_ACCESS_KEY={env_creds[1]} -e AWS_DEFAULT_REGION={env_creds[2]} '
132+
else:
133+
profile_name = get_creds_from_credentials_file()
134+
if profile_name:
135+
run_docker_settings = f' -e AWS_PROFILE={profile_name} -v ~/.aws/:/root/.aws '
136+
else:
137+
click.echo("Credentials not found.\nYou can set them in ~/.aws/credentials or by setting environment variables.\nSee https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html#environment-variables for more info.")
138+
exit()
139+
140+
93141
# Create Docker client
94142
try:
95143
click.echo("Creating Docker client.")
@@ -111,13 +159,9 @@ def zappadock():
111159

112160
# Create command to start ZappaDock
113161
cmnd1 ="docker run -ti --rm"
114-
cmnd2 = f"-e AWS_ACCESS_KEY_ID={credentials[0]} -e AWS_SECRET_ACCESS_KEY={credentials[1]} -e AWS_DEFAULT_REGION={credentials[2]} -e AWS_PROFILE={credentials[3]}"
162+
cmnd2 = run_docker_settings
115163
cmnd3 = f'-v "{os.getcwd()}:/var/task" {docker_image[0].id}'
116164

117165
# Run command
118166
click.echo("Starting ZappaDock...")
119167
os.system(f"{cmnd1} {cmnd2} {cmnd3}")
120-
121-
122-
123-

0 commit comments

Comments
 (0)