|
18 | 18 | LOG = logging.getLogger(__name__) |
19 | 19 |
|
20 | 20 |
|
21 | | -def upload_package(pkg, config, profile_name): |
22 | | - with open(pkg.zip_file, "rb") as fil: |
23 | | - zip_file = fil.read() |
24 | | - |
25 | | - session = boto3.session.Session(region_name=config.region, |
26 | | - profile_name=profile_name) |
27 | | - client = session.client('lambda') |
28 | | - # Assume the function already exists in AWS |
29 | | - existing_function = True |
30 | | - |
31 | | - try: |
32 | | - get_resp = client.get_function_configuration(FunctionName=config.name) |
33 | | - LOG.debug("AWS get_function_configuration response: %s" % get_resp) |
34 | | - except: |
35 | | - existing_function = False |
36 | | - LOG.debug("function not found creating new function") |
37 | | - |
38 | | - if existing_function: |
| 21 | +class PackageUploader(object): |
| 22 | + def __init__(self, config, profile_name): |
| 23 | + self._config = config |
| 24 | + session = boto3.session.Session(region_name=config.region, |
| 25 | + profile_name=profile_name) |
| 26 | + self._client = session.client('lambda') |
| 27 | + self.version = None |
| 28 | + |
| 29 | + ''' |
| 30 | + Calls the AWS methods to upload an existing package and update |
| 31 | + the function configuration |
| 32 | +
|
| 33 | + returns the package version |
| 34 | + ''' |
| 35 | + def upload_existing(self, pkg): |
| 36 | + with open(pkg.zip_file, "rb") as fil: |
| 37 | + zip_file = fil.read() |
| 38 | + |
39 | 39 | LOG.debug('running update_function_code') |
40 | | - response = client.update_function_code( |
41 | | - FunctionName=config.name, |
| 40 | + conf_update_resp = self._client.update_function_code( |
| 41 | + FunctionName=self._config.name, |
42 | 42 | ZipFile=zip_file, |
43 | | - Publish=config.publish, |
| 43 | + Publish=False, |
44 | 44 | ) |
45 | | - LOG.debug("AWS update_function_code response: %s" % response) |
| 45 | + LOG.debug("AWS update_function_code response: %s" |
| 46 | + % conf_update_resp) |
46 | 47 | LOG.debug('running update_function_configuration') |
47 | | - response = client.update_function_configuration( |
48 | | - FunctionName=config.name, |
49 | | - Handler=config.handler, |
50 | | - Role=config.role, |
51 | | - Description=config.description, |
52 | | - Timeout=config.timeout, |
53 | | - MemorySize=config.memory, |
| 48 | + response = self._client.update_function_configuration( |
| 49 | + FunctionName=self._config.name, |
| 50 | + Handler=self._config.handler, |
| 51 | + Role=self._config.role, |
| 52 | + Description=self._config.description, |
| 53 | + Timeout=self._config.timeout, |
| 54 | + MemorySize=self._config.memory, |
54 | 55 | ) |
55 | | - LOG.debug("AWS update_function_configuration response: %s" % response) |
56 | | - else: |
| 56 | + LOG.debug("AWS update_function_configuration response: %s" |
| 57 | + % response) |
| 58 | + |
| 59 | + version = response.get('Version') |
| 60 | + # Publish the version after upload and config update if needed |
| 61 | + if self._config.publish: |
| 62 | + resp = self._client.publish_version( |
| 63 | + FunctionName=self._config.name, |
| 64 | + ) |
| 65 | + LOG.debug("AWS publish_version response: %s" % resp) |
| 66 | + version = resp.get('Version') |
| 67 | + |
| 68 | + return version |
| 69 | + |
| 70 | + ''' |
| 71 | + Creates and uploads a new lambda function |
| 72 | +
|
| 73 | + returns the package version |
| 74 | + ''' |
| 75 | + def upload_new(self, pkg): |
| 76 | + with open(pkg.zip_file, "rb") as fil: |
| 77 | + zip_file = fil.read() |
| 78 | + |
57 | 79 | LOG.debug('running create_function_code') |
58 | | - response = client.create_function( |
59 | | - FunctionName=config.name, |
| 80 | + response = self._client.create_function( |
| 81 | + FunctionName=self._config.name, |
60 | 82 | Runtime='python2.7', |
61 | | - Handler=config.handler, |
62 | | - Role=config.role, |
| 83 | + Handler=self._config.handler, |
| 84 | + Role=self._config.role, |
63 | 85 | Code={'ZipFile': zip_file}, |
64 | | - Description=config.description, |
65 | | - Timeout=config.timeout, |
66 | | - MemorySize=config.memory, |
67 | | - Publish=config.publish, |
| 86 | + Description=self._config.description, |
| 87 | + Timeout=self._config.timeout, |
| 88 | + MemorySize=self._config.memory, |
| 89 | + Publish=self._config.publish, |
68 | 90 | ) |
69 | 91 | LOG.debug("AWS create_function response: %s" % response) |
| 92 | + |
| 93 | + return response.get('Version') |
| 94 | + |
| 95 | + ''' |
| 96 | + Auto determines whether the function exists or not and calls |
| 97 | + the appropriate method (upload_existing or upload_new). |
| 98 | + ''' |
| 99 | + def upload(self, pkg): |
| 100 | + existing_function = True |
| 101 | + try: |
| 102 | + get_resp = self._client.get_function_configuration( |
| 103 | + FunctionName=self._config.name) |
| 104 | + LOG.debug("AWS get_function_configuration response: %s" % get_resp) |
| 105 | + except: |
| 106 | + existing_function = False |
| 107 | + LOG.debug("function not found creating new function") |
| 108 | + |
| 109 | + if existing_function: |
| 110 | + self.version = self.upload_existing(pkg) |
| 111 | + else: |
| 112 | + self.version = self.upload_new(pkg) |
| 113 | + |
| 114 | + ''' |
| 115 | + Create/update an alias to point to the package. Raises an |
| 116 | + exception if the package has not been uploaded. |
| 117 | + ''' |
| 118 | + def alias(self): |
| 119 | + # if self.version is still None raise exception |
| 120 | + if self.version is None: |
| 121 | + raise Exception('Must upload package before applying alias') |
| 122 | + |
| 123 | + if self._alias_exists(): |
| 124 | + self._update_alias() |
| 125 | + else: |
| 126 | + self._create_alias() |
| 127 | + |
| 128 | + ''' |
| 129 | + Pulls down the current list of aliases and checks to see if |
| 130 | + an alias exists. |
| 131 | + ''' |
| 132 | + def _alias_exists(self): |
| 133 | + resp = self._client.list_aliases( |
| 134 | + FunctionName=self._config.name) |
| 135 | + |
| 136 | + for alias in resp.get('Aliases'): |
| 137 | + if alias.get('Name') == self._config.alias: |
| 138 | + return True |
| 139 | + return False |
| 140 | + |
| 141 | + '''Creates alias''' |
| 142 | + def _create_alias(self): |
| 143 | + LOG.debug("Creating new alias %s" % self._config.alias) |
| 144 | + resp = self._client.create_alias( |
| 145 | + FunctionName=self._config.name, |
| 146 | + Name=self._config.alias, |
| 147 | + FunctionVersion=self.version, |
| 148 | + Description=self._config.alias_description, |
| 149 | + ) |
| 150 | + LOG.debug("AWS create_alias response: %s" % resp) |
| 151 | + |
| 152 | + '''Update alias''' |
| 153 | + def _update_alias(self): |
| 154 | + LOG.debug("Updating alias %s" % self._config.alias) |
| 155 | + resp = self._client.update_alias( |
| 156 | + FunctionName=self._config.name, |
| 157 | + Name=self._config.alias, |
| 158 | + FunctionVersion=self.version, |
| 159 | + Description=self._config.alias_description, |
| 160 | + ) |
| 161 | + LOG.debug("AWS update_alias response: %s" % resp) |
0 commit comments