Merge pull request #23995 from makinacorpus/lxc_path_pre

Lxc path pre
This commit is contained in:
Colton Myers 2015-05-21 11:26:03 -06:00
commit f7fae26059
3 changed files with 108 additions and 21 deletions

View file

@ -1369,10 +1369,6 @@ def init(name,
chunks = read_conf(path, out_format='commented')
if old_chunks != chunks:
to_reboot = True
if remove_seed_marker:
run(name,
'rm -f \'{0}\''.format(SEED_MARKER),
python_shell=False)
# last time to be sure any of our property is correctly applied
cfg = _LXCConfig(name=name, network_profile=network_profile,
@ -1405,6 +1401,12 @@ def init(name,
ret['changes'] = changes_dict
return ret
if remove_seed_marker:
run(name,
'rm -f \'{0}\''.format(SEED_MARKER),
chroot_fallback=False,
python_shell=False)
# set the default user/password, only the first time
if ret.get('result', True) and password:
gid = '/.lxc.initial_pass'
@ -1657,6 +1659,39 @@ def templates():
return [x[4:] for x in template_scripts if x.startswith('lxc-')]
def _after_ignition_network_profile(cmd,
ret,
name,
network_profile,
nic_opts):
_clear_context()
if ret['retcode'] == 0 and exists(name):
if network_profile:
network_changes = apply_network_profile(name,
network_profile,
nic_opts=nic_opts)
if network_changes:
log.info(
'Network changes from applying network profile \'{0}\' '
'to newly-created container \'{1}\':\n{2}'
.format(network_profile, name, network_changes)
)
c_state = state(name)
return {'result': True,
'state': {'old': None, 'new': c_state}}
else:
if exists(name):
# destroy the container if it was partially created
cmd = 'lxc-destroy'
cmd += ' -n {0}'.format(name)
__salt__['cmd.retcode'](cmd, python_shell=False)
raise CommandExecutionError(
'Container could not be created with cmd \'{0}\': {1}'
.format(cmd, ret['stderr'])
)
def create(name,
config=None,
profile=None,
@ -1854,11 +1889,19 @@ def create(name,
'Container could not be created with cmd \'{0}\': {1}'
.format(cmd, ret['stderr'])
)
ret = __salt__['cmd.run_all'](cmd, python_shell=False)
return _after_ignition_network_profile(cmd,
ret,
name,
network_profile,
nic_opts)
def clone(name,
orig,
profile=None,
network_profile=None,
nic_opts=None,
**kwargs):
'''
Create a new container as a clone of another container
@ -1887,6 +1930,16 @@ def clone(name,
The type of storage to use. Set to ``lvm`` to use an LVM group.
Defaults to filesystem within /var/lib/lxc.
network_profile
Network profile to use for container
.. versionadded:: 2015.5.2
nic_opts
give extra opts overriding network profile values
.. versionadded:: 2015.5.2
CLI Examples:
@ -1953,6 +2006,11 @@ def clone(name,
'Container could not be cloned with cmd \'{0}\': {1}'
.format(cmd, ret['stderr'])
)
return _after_ignition_network_profile(cmd,
ret,
name,
network_profile,
nic_opts)
def ls_(active=None, cache=True):

View file

@ -208,6 +208,19 @@ def init(names, host=None, saltcloud_mode=False, quiet=False, **kwargs):
ret['comment'] = 'Container names are not formed as a list'
ret['result'] = False
return ret
# check that the host is alive
client = salt.client.get_local_client(__opts__['conf_file'])
alive = False
try:
if client.cmd(host, 'test.ping', timeout=20).get(host, None):
alive = True
except (TypeError, KeyError):
pass
if not alive:
ret['comment'] = 'Host {0} is not reachable'.format(host)
ret['result'] = False
return ret
log.info('Searching for LXC Hosts')
data = __salt__['lxc.list'](host, quiet=True)
for host, containers in data.items():
@ -222,9 +235,7 @@ def init(names, host=None, saltcloud_mode=False, quiet=False, **kwargs):
ret['result'] = False
return ret
client = salt.client.get_local_client(__opts__['conf_file'])
kw = dict((k, v) for k, v in kwargs.items() if not k.startswith('__'))
kw = salt.utils.clean_kwargs(**kwargs)
pub_key = kw.get('pub_key', None)
priv_key = kw.get('priv_key', None)
explicit_auth = pub_key and priv_key
@ -257,7 +268,7 @@ def init(names, host=None, saltcloud_mode=False, quiet=False, **kwargs):
cmds = []
for name in names:
args = [name]
kw = kwargs
kw = salt.utils.clean_kwargs(**kwargs)
if saltcloud_mode:
kw = copy.deepcopy(kw)
kw['name'] = name

View file

@ -18,6 +18,7 @@ def present(name,
clone_from=None,
snapshot=False,
profile=None,
network_profile=None,
template=None,
options=None,
image=None,
@ -57,6 +58,14 @@ def present(name,
<tutorial-lxc-profiles-container>` for more information). Values in a
profile will be overridden by the parameters listed below.
network_profile
Network Profile to use in container creation
(see the :ref:`LXC Tutorial <tutorial-lxc-profiles-container>`
for more information). Values in a profile will be overridden by
the parameters listed below.
.. versionadded:: 2015.5.2
**Container Creation Arguments**
template
@ -201,21 +210,24 @@ def present(name,
result = __salt__['lxc.clone'](name,
clone_from,
profile=profile,
network_profile=network_profile,
snapshot=snapshot,
size=size,
backing=backing)
else:
result = __salt__['lxc.create'](name,
profile=profile,
template=template,
options=options,
image=image,
config=config,
fstype=fstype,
size=size,
backing=backing,
vgname=vgname,
lvname=lvname)
result = __salt__['lxc.create'](
name,
profile=profile,
network_profile=network_profile,
template=template,
options=options,
image=image,
config=config,
fstype=fstype,
size=size,
backing=backing,
vgname=vgname,
lvname=lvname)
except (CommandExecutionError, SaltInvocationError) as exc:
ret['result'] = False
ret['comment'] = exc.strerror
@ -290,13 +302,19 @@ def present(name,
return ret
def absent(name):
def absent(name, stop=False):
'''
Ensure a container is not present, destroying it if present
name
Name of the container to destroy
stop
stop before destroying
default: false
.. versionadded:: 2015.5.2
.. code-block:: yaml
web01:
@ -316,7 +334,7 @@ def absent(name):
return ret
try:
result = __salt__['lxc.destroy'](name)
result = __salt__['lxc.destroy'](name, stop=stop)
except (SaltInvocationError, CommandExecutionError) as exc:
ret['result'] = False
ret['comment'] = 'Failed to destroy container: {0}'.format(exc)