-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathshell.py
213 lines (182 loc) · 7.74 KB
/
shell.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
# -*- coding: utf-8 -*-
# Copyright 2015-2016 Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""lambda-uploader - Simple way to create and upload python lambda jobs"""
from __future__ import print_function
import sys
import logging
import traceback
import lambda_uploader
from os import getcwd, path, getenv
from lambda_uploader import package, config, uploader, subscribers
from boto3 import __version__ as boto3_version
from botocore import __version__ as botocore_version
LOG = logging.getLogger(__name__)
NAMESPACE = 'rax_jira'
CHECK = '✅'
INTERROBANG = '‽'
RED_X = '❌'
LAMBDA = 'λ'
TRACEBACK_MESSAGE = """%s Unexpected error. Please report this traceback.
Uploader: %s
Botocore: %s
Boto3: %s
"""
# Used for stdout for shell
def _print(txt):
# Windows Powershell doesn't support Unicode
if sys.platform == 'win32' or sys.platform == 'cygwin':
print(txt)
else:
# Add the lambda symbol
print("%s %s" % (LAMBDA, txt))
def _execute(args):
pth = path.abspath(args.function_dir)
cfg = config.Config(pth, args.config, role=args.role,
variables=args.variables)
if args.image_uri:
cfg.image_uri(args.image_uri)
if args.s3_bucket:
cfg.set_s3(args.s3_bucket, args.s3_key)
if args.no_virtualenv:
# specified flag to omit entirely
venv = False
elif args.virtualenv:
# specified a custom virtualenv
venv = args.virtualenv
else:
# build and include virtualenv, the default
venv = None
if not cfg.image_uri:
if args.no_build:
pkg = package.create_package(pth)
else:
_print('Building Package')
requirements = cfg.requirements
if args.requirements:
requirements = path.abspath(args.requirements)
extra_files = cfg.extra_files
if args.extra_files:
extra_files = args.extra_files
pkg = package.build_package(pth, requirements,
venv, cfg.ignore, extra_files,
pyexec=cfg.runtime)
if not args.no_clean:
pkg.clean_workspace()
else:
pkg = None
if not args.no_upload:
# Set publish if flagged to do so
if args.publish:
cfg.set_publish()
create_alias = False
# Set alias if the arg is passed
if args.alias is not None:
cfg.set_alias(args.alias, args.alias_description)
create_alias = True
_print('Uploading Package')
upldr = uploader.PackageUploader(cfg, args.profile)
upldr.upload(pkg)
# If the alias was set create it
if create_alias:
upldr.alias()
if cfg.subscription:
_print('Creating subscription')
subscribers.create_subscriptions(cfg, args.profile)
if pkg:
pkg.clean_zipfile()
_print('Fin')
def main(arv=None):
"""lambda-uploader command line interface."""
# Check for Python 2.7 or later
if sys.version_info[0] < 3 and not sys.version_info[1] == 7:
raise RuntimeError('lambda-uploader requires Python 2.7 or later')
import argparse
parser = argparse.ArgumentParser(
description='Simple way to create and upload python lambda jobs')
parser.add_argument('--version', '-v', action='version',
version=lambda_uploader.__version__)
parser.add_argument('--no-upload', dest='no_upload',
action='store_const', help='dont upload the zipfile',
const=True)
parser.add_argument('--no-clean', dest='no_clean',
action='store_const',
help='dont cleanup the temporary workspace',
const=True)
parser.add_argument('--publish', '-p', dest='publish',
action='store_const',
help='publish an upload to an immutable version',
const=True)
parser.add_argument('--virtualenv', '-e',
help='use specified virtualenv instead of making one',
default=None)
parser.add_argument('--extra-files', '-x',
action='append',
help='include file or directory path in package',
default=[])
parser.add_argument('--no-virtualenv', dest='no_virtualenv',
action='store_const',
help='do not create or include a virtualenv at all',
const=True)
parser.add_argument('--role', dest='role',
default=getenv('LAMBDA_UPLOADER_ROLE'),
help=('IAM role to assign the lambda function, '
'can be set with $LAMBDA_UPLOADER_ROLE'))
parser.add_argument('--variables', dest='variables',
help='add environment variables')
parser.add_argument('--profile', dest='profile',
help='specify AWS cli profile')
parser.add_argument('--requirements', '-r', dest='requirements',
help='specify a requirements.txt file')
alias_help = 'alias for published version (WILL SET THE PUBLISH FLAG)'
parser.add_argument('--alias', '-a', dest='alias',
default=None, help=alias_help)
parser.add_argument('--alias-description', '-m', dest='alias_description',
default=None, help='alias description')
parser.add_argument('--image-uri', '-i', dest='image_uri',
help='uri of a container image in the amazon ecr registry to deploy',
default=None)
parser.add_argument('--s3-bucket', '-s', dest='s3_bucket',
help='S3 bucket to store the lambda function in',
default=None)
parser.add_argument('--s3-key', '-k', dest='s3_key',
help='Key name of the lambda function s3 object',
default=None)
parser.add_argument('--config', '-c', help='Overrides lambda.json',
default='lambda.json')
parser.add_argument('function_dir', default=getcwd(), nargs='?',
help='lambda function directory')
parser.add_argument('--no-build', dest='no_build',
action='store_const', help='dont build the sourcecode',
const=True)
verbose = parser.add_mutually_exclusive_group()
verbose.add_argument('-V', dest='loglevel', action='store_const',
const=logging.INFO,
help="Set log-level to INFO.")
verbose.add_argument('-VV', dest='loglevel', action='store_const',
const=logging.DEBUG,
help="Set log-level to DEBUG.")
parser.set_defaults(loglevel=logging.WARNING)
args = parser.parse_args()
logging.basicConfig(level=args.loglevel)
try:
_execute(args)
except Exception:
print(TRACEBACK_MESSAGE
% (INTERROBANG, lambda_uploader.__version__,
boto3_version, botocore_version),
file=sys.stderr)
traceback.print_exc()
sys.stderr.flush()
sys.exit(1)