From 0d0dda2a802659d2124cb0ac33fb6cb925ded8d4 Mon Sep 17 00:00:00 2001 From: Nadav Goldin Date: Sat, 18 Mar 2017 20:57:15 +0200 Subject: [PATCH] Allow starting an environment from a remote init file This commit adds support for fetching the init file from an http or https server, i.e.: lago init http://localhost:8000/inits/LagoInitFile Signed-off-by: Nadav Goldin --- lago/cmd.py | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/lago/cmd.py b/lago/cmd.py index 689fd390..6ded2b8e 100755 --- a/lago/cmd.py +++ b/lago/cmd.py @@ -27,6 +27,8 @@ import pkg_resources import sys import warnings +import urllib2 +from tempfile import NamedTemporaryFile from collections import defaultdict @@ -55,14 +57,15 @@ @lago.plugins.cli.cli_plugin_add_argument( 'virt_config', help=( - 'Configuration of resources to deploy, json and yaml file formats ' - 'are supported, takes option precedence over workdir. Will use ' - '$PWD/LagoInitFile by default. You can use any env vars in that file, ' - 'inculuding the extra ones LAGO_PREFIX_PATH LAGO_WORKDIR_PATH and ' - 'LAGO_INITFILE_PATH' + 'Configuration of resources to deploy, YAML and JSON file formats ' + 'are supported, takes option precedence over workdir. Also possible ' + 'to pass a valid URL which will be downloaed. Will use ' + '$PWD/LagoInitFile as default. You can use any env vars in that file ' + 'including the extra ones: LAGO_PREFIX_PATH, LAGO_WORKDIR_PATH and ' + 'LAGO_INITFILE_PATH. ' ), metavar='VIRT_CONFIG', - type=os.path.abspath, + type=str, nargs='?', default=None, ) @@ -73,7 +76,7 @@ '$PWD/.lago' ), metavar='WORKDIR', - type=os.path.abspath, + type=str, nargs='?', default=None, ) @@ -124,21 +127,38 @@ def do_init( skip_bootstrap=False, **kwargs ): - if virt_config is None and workdir is not None: virt_config = workdir workdir = None if workdir is None: workdir = os.path.abspath('.lago') + else: + workdir = os.path.abspath(workdir) if virt_config is None: virt_config = os.path.abspath('LagoInitFile') + elif virt_config.startswith(('http://', 'https://')): + try: + LOGGER.debug('Downloading init file from url %s', virt_config) + res = urllib2.urlopen(virt_config) + except urllib2.URLError as e: + raise LagoUserException( + ('Error downloading {0}: ' + ' {1}').format(virt_config, str(e.msg)) + ), None, sys.exc_info()[2] + os.environ['LAGO_INITFILE_PATH'] = os.path.abspath(os.curdir) + with NamedTemporaryFile(delete=False) as temp_fd: + temp_fd.writelines(res.readlines()) + virt_config = temp_fd.name + if not os.path.isfile(virt_config): raise LagoUserException( 'Unable to find init file: {0}'.format(virt_config) ) + virt_config = os.path.abspath(virt_config) + os.environ['LAGO_INITFILE_PATH' ] = os.path.dirname(os.path.abspath(virt_config))