Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Allow starting an environment from a remote init file #489

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions lago/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import pkg_resources
import sys
import warnings
import urllib2
from tempfile import NamedTemporaryFile

from collections import defaultdict

Expand Down Expand Up @@ -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,
)
Expand All @@ -73,7 +76,7 @@
'$PWD/.lago'
),
metavar='WORKDIR',
type=os.path.abspath,
type=str,
nargs='?',
default=None,
)
Expand Down Expand Up @@ -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))

Expand Down