Skip to content

Commit 7263fc9

Browse files
authored
Merge pull request #2 from matt-land/feature/allowconfigs
all files matching function name get zipped
2 parents 144f4f0 + a807b2b commit 7263fc9

1 file changed

Lines changed: 19 additions & 16 deletions

File tree

deploylambda/deploylambda.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import ConfigParser
77
import sys
88

9+
910
class DeployLambda:
1011

1112
profile = ''
@@ -27,7 +28,7 @@ def get_account(self):
2728

2829
@staticmethod
2930
def create_venv_zip(function_name, path, extra_path=''):
30-
droppath = os.getcwd() #drop in cwd
31+
droppath = os.getcwd() # drop in cwd
3132
if not droppath.endswith('/'):
3233
droppath = path + "/"
3334
if not path.endswith('/'):
@@ -49,11 +50,14 @@ def create_venv_zip(function_name, path, extra_path=''):
4950
os.chdir(path)
5051
zf = zipfile.ZipFile(zippath, mode='w', compression=zipfile.ZIP_DEFLATED)
5152

52-
if not os.path.isfile(function_name + '.py'):
53-
raise Exception('no such file ' + path + function_name + '.py')
54-
zf.write(function_name + '.py')
55-
if os.path.isfile(function_name + '-config.json'): #add config if we have it
56-
zf.write(function_name + '-config.json')
53+
if not os.path.isfile('{}.py'.format(function_name)):
54+
raise Exception('no such file: {}{}.py'.format(path, function_name))
55+
if not os.path.isfile('{}-config.json'.format(function_name)):
56+
raise Exception('required config file not found: {}{}.py'.format(path, function_name))
57+
repo_files = os.listdir(path)
58+
for repo_file in repo_files:
59+
if repo_file.startwith(function) and not repo_file.endswith('.pyc'):
60+
zf.write(repo_file)
5761

5862
for subpath in extra_paths:
5963
os.chdir(path + subpath)
@@ -62,11 +66,11 @@ def create_venv_zip(function_name, path, extra_path=''):
6266
dirs.remove('.git')
6367

6468
for file in files:
65-
if root.startswith('./psycopg2') and 'aws' not in subpath: #psycopg2 from aws folder
69+
if root.startswith('./psycopg2') and 'aws' not in subpath: # psycopg2 from aws folder
6670
continue
6771
if file == '.DS_Store':
6872
continue
69-
if file.endswith('.zip'): #skip zips
73+
if file.endswith('.zip'): # skip zips
7074
continue
7175
if file.endswith(".pyc"):
7276
continue
@@ -103,7 +107,7 @@ def create_zip(function_name, path):
103107
zf.write(root + "/" + file)
104108
counter += 1
105109
zf.close()
106-
#print str(counter) + " files added to "+ zippath
110+
# print str(counter) + " files added to "+ zippath
107111
return zippath
108112

109113
def backup_old_lambda(self, path):
@@ -151,12 +155,12 @@ def deploy_new_lambda(self, zippath):
151155
print "Deploying lambda [ " + self.function_name + " ] in [ " + self.get_account() + " ]"
152156
try:
153157
code = "aws lambda update-function-code --function-name " + self.function_name + " --zip-file fileb://" + zippath + " --profile " + self.profile
154-
#print code
158+
# print code
155159
output = subprocess.check_output(code, shell=True)
156160
obj = json.loads(output)
157-
#print " Last Modified: "+obj['LastModified']
161+
# print " Last Modified: "+obj['LastModified']
158162
print " Sha: "+obj['CodeSha256']
159-
#print " Code Size: " + str(obj['CodeSize'])
163+
# print " Code Size: " + str(obj['CodeSize'])
160164
except Exception, e:
161165
raise Exception("unable to deploy lambdas from [" + self.get_account()+']')
162166

@@ -169,7 +173,6 @@ def _setup_os(self):
169173
if not os.path.isfile(os.path.expanduser('~') + '/.aws/credentials'):
170174
raise Exception('please run aws configure, missing credentials')
171175

172-
173176
userconfig = ConfigParser.ConfigParser()
174177
userconfig.readfp(open(os.path.expanduser('~') + '/.aws/credentials'))
175178
os.environ['AWS_ACCESS_KEY_ID'] = userconfig.get(self.profile, 'aws_access_key_id')
@@ -228,7 +231,7 @@ def update_metadata(self, path):
228231

229232
# build our input data from file
230233
cli_input_json = dict(skeleton.items() + file_obj.items())
231-
#pop this key if seen
234+
# pop this key if seen
232235
if 'VpcId' in cli_input_json['VpcConfig']:
233236
cli_input_json['VpcConfig'].pop('VpcId')
234237

@@ -238,7 +241,7 @@ def update_metadata(self, path):
238241
#print "No metadata changes detected"
239242
return
240243

241-
#print "updating metadata"
244+
# print "updating metadata"
242245
try:
243246
code = "aws lambda update-function-configuration --function " + self.function_name + " --profile " + self.profile + " --cli-input-json " + json.dumps(json.dumps(cli_input_json))
244247
data = subprocess.check_output(code, shell=True)
@@ -248,7 +251,7 @@ def update_metadata(self, path):
248251
def version_and_create_alias(self, name):
249252
"""publish a version from $LATEST and add an alias"""
250253
version = self._create_version()
251-
#see if we need to create or update the alias
254+
# see if we need to create or update the alias
252255

253256
aliases = self._list_aliases()
254257
hasAlias = False

0 commit comments

Comments
 (0)