|
22 | 22 | import os |
23 | 23 | import sys |
24 | 24 | import time |
25 | | - |
| 25 | +import subprocess |
| 26 | +import shutil |
26 | 27 | import click |
27 | 28 | import yaml |
| 29 | +import validators |
28 | 30 |
|
29 | 31 | import apployer |
30 | 32 | from .appstack import AppStack |
|
35 | 37 |
|
36 | 38 | DEFAULT_EXPANDED_APPSTACK_FILE = 'expanded_appstack.yml' |
37 | 39 | DEFAULT_APPSTACK_FILE = 'appstack.yml' |
| 40 | +DEFAULT_ARTIFACTS_PATH = 'artifacts' |
38 | 41 |
|
39 | 42 | _log = logging.getLogger(__name__) #pylint: disable=invalid-name |
40 | 43 |
|
@@ -143,6 +146,10 @@ def deploy( #pylint: disable=too-many-arguments |
143 | 146 | """ |
144 | 147 | start_time = time.time() |
145 | 148 |
|
| 149 | + if validators.url(artifacts_location): |
| 150 | + _download_artifacts_from_url(artifacts_location, appstack) |
| 151 | + artifacts_location = DEFAULT_ARTIFACTS_PATH |
| 152 | + |
146 | 153 | cf_info = CfInfo(api_url=cf_api_endpoint, password=cf_password, user=cf_user, |
147 | 154 | org=cf_org, space=cf_space) |
148 | 155 | filled_appstack = _get_filled_appstack(appstack, expanded_appstack, filled_appstack, |
@@ -188,6 +195,53 @@ def fetch(artifacts_location, fetcher_config, expanded_appstack, appstack): |
188 | 195 | _log.info('Content of %s file: \n %s', final_appstack_path, yaml.dump(filled_appstack_dict)) |
189 | 196 |
|
190 | 197 |
|
| 198 | +@cli.command() |
| 199 | +@click.argument('ARTIFACTS_URL') |
| 200 | +@click.option('-a', '--appstack', |
| 201 | + default=DEFAULT_APPSTACK_FILE, show_default=True, |
| 202 | + help='Path to the file containing non-expanded appstack. Only used if expanded' |
| 203 | + 'appstack has not been specified.') |
| 204 | +def download_apps(artifacts_url, appstack): |
| 205 | + """ |
| 206 | + Downloads applications artifacts from specified source. Url should include '{name}' |
| 207 | + parameter like here: http://artifacts.com/download/{name} |
| 208 | + """ |
| 209 | + if not validators.url(artifacts_url): |
| 210 | + _log.error('Value %s is not valid Url.', artifacts_url) |
| 211 | + raise ApployerArgumentError('Provided invalid url') |
| 212 | + |
| 213 | + _download_artifacts_from_url(artifacts_url, appstack) |
| 214 | + |
| 215 | + |
| 216 | +def _download_artifacts_from_url(url, appstack): |
| 217 | + """ |
| 218 | + Does the neccessary things to download applications artifacts. |
| 219 | +
|
| 220 | + Args: |
| 221 | + url: address of artifacts server, i.e. http://artifacts.com/download/{name} |
| 222 | + where 'name' param is dynamically replaced to app name |
| 223 | + appstack: path to appstack file |
| 224 | + """ |
| 225 | + if os.path.exists(DEFAULT_ARTIFACTS_PATH): |
| 226 | + shutil.rmtree(DEFAULT_ARTIFACTS_PATH, ignore_errors=True) |
| 227 | + os.makedirs(DEFAULT_ARTIFACTS_PATH) |
| 228 | + |
| 229 | + with open(appstack) as appstack_file: |
| 230 | + appstack_dict = yaml.load(appstack_file) |
| 231 | + |
| 232 | + artifacts_names = set() |
| 233 | + for app in appstack_dict['apps']: |
| 234 | + artifacts_names.add(app.get('artifact_name', app.get('name'))) |
| 235 | + |
| 236 | + for artifact_name in artifacts_names: |
| 237 | + artifact_url = url.format(name=artifact_name) |
| 238 | + _log.info('Downloading artifact for %s app from %s', artifact_name, artifact_url) |
| 239 | + proc = subprocess.Popen(['wget', '--no-check-certificate', '--content-disposition', |
| 240 | + '-nv', artifact_url], cwd=DEFAULT_ARTIFACTS_PATH) |
| 241 | + if proc.wait() != 0: |
| 242 | + _log.error('Error during download! wget output:\n%s', proc.stdout.read()) |
| 243 | + |
| 244 | + |
191 | 245 | def _get_filled_appstack( #pylint: disable=too-many-arguments |
192 | 246 | appstack_path, |
193 | 247 | expanded_appstack_path, |
|
0 commit comments