Bugfixes from LXC refactor

Also reclassify the new functionality as being added for the Lithium
release
This commit is contained in:
Erik Johnson 2014-12-09 11:57:30 -06:00
parent 7029bd978b
commit 62e25fc016

View File

@ -16,6 +16,7 @@ import traceback
import datetime
import pipes
import copy
import difflib
import logging
import tempfile
import os
@ -289,7 +290,7 @@ def cloud_init_interface(name, vm_=None, **kwargs):
ethx['ipv4'] += '/{0}'.format(nm)
for i in ('mac', 'hwaddr'):
if i in iopts:
ethx['hwaddr'] = iopts[i]
ethx['mac'] = iopts[i]
if 'hwaddr' not in ethx:
raise ValueError('No mac for {0}'.format(ifh))
gateway = vm_.get('gateway', 'auto')
@ -390,7 +391,7 @@ def get_container_profile(name=None, **kwargs):
def get_network_profile(name=None):
'''
.. versionadded:: 2014.7.1
.. versionadded:: Lithium
Gather a pre-configured set of network configuration parameters. If no
arguments are passed, the following default profile is returned:
@ -418,7 +419,7 @@ def get_network_profile(name=None):
A profile can be passed either as the name of the profile, or a
dictionary of variable names and values. See the :ref:`LXC Tutorial
<tutorial-lxc-profiles>` for more information on how to use container
<tutorial-lxc-profiles>` for more information on how to use network
profiles.
CLI Example::
@ -426,11 +427,15 @@ def get_network_profile(name=None):
.. code-block:: bash
salt-call lxc.get_network_profile default
salt-call lxc.get_network_profile default link=mybridge
'''
return __salt__['config.get'](
'lxc.network_profile.{0}'.format(name), DEFAULT_NIC_PROFILE
# Legacy location (lxc.nic)
net_profile = __salt__['config.get']('lxc.nic', {}).get(name)
if net_profile is not None:
return net_profile
net_profile = __salt__['config.get'](
'lxc.network_profile.{0}'.format(name)
)
return net_profile if net_profile is not None else DEFAULT_NIC_PROFILE
def _rand_cpu_str(cpu):
@ -759,6 +764,7 @@ def init(name,
memory=None,
profile=None,
network_profile=None,
nic=None,
nic_opts=None,
cpu=None,
autostart=True,
@ -826,17 +832,24 @@ def init(name,
bridge
the bridge to use
the default does nothing more than lxcutils does
network_profile
Network interfaces profile
.. versionadded:: Lithium
nic
Network interfaces profile (defined in config or pillar).
.. deprecated:: Lithium
Use ``network_profile`` instead
nic_opts
Extra options for network interfaces, will override
``{"eth0": {"mac": "aa:bb:cc:dd:ee:ff", "ipv4": "10.1.1.1", "ipv6": "2001:db8::ff00:42:8329"}}``
``{"eth0": {"hwaddr": "aa:bb:cc:dd:ee:ff", "ipv4": "10.1.1.1", "ipv6": "2001:db8::ff00:42:8329"}}``
or
``{"eth0": {"mac": "aa:bb:cc:dd:ee:ff", "ipv4": "10.1.1.1/24", "ipv6": "2001:db8::ff00:42:8329"}}``
``{"eth0": {"hwaddr": "aa:bb:cc:dd:ee:ff", "ipv4": "10.1.1.1/24", "ipv6": "2001:db8::ff00:42:8329"}}``
users
Sysadmins users to set the administrative password to
@ -878,7 +891,7 @@ def init(name,
Delay in seconds between end of container creation and bootstrapping.
Useful when waiting for container to obtain a DHCP lease.
.. versionadded:: 2014.7.1
.. versionadded:: Lithium
bootstrap_url
See lxc.bootstrap
@ -915,6 +928,14 @@ def init(name,
'result': False,
'changes': {}}
if nic:
salt.utils.warn_until(
'Boron',
'The \'nic\' argument to \'lxc.init\' has been deprecated, '
'please use \'network_profile\' instead.'
)
network_profile = nic
# Changes is a pointer to changes_dict['init']. This method is used so that
# we can have a list of changes as they are made, providing an ordered list
# of things that were changed.
@ -997,12 +1018,15 @@ def init(name,
with cfg.tempfile() as cfile:
try:
create(name, config=cfile.name, profile=profile, **kwargs)
changes.append({'create': 'Container created'})
except (SaltInvocationError, CommandExecutionError) as exc:
ret['comment'] = exc.message
if changes:
ret['changes'] = changes_dict
return ret
changes.append({'create': 'Container created'})
if 'already exists' in exc.strerror:
changes.append({'create': 'Container already exists'})
else:
ret['comment'] = exc.strerror
if changes:
ret['changes'] = changes_dict
return ret
path = '/var/lib/lxc/{0}/config'.format(name)
old_chunks = []
if os.path.exists(path):
@ -1202,7 +1226,7 @@ def cloud_init(name, vm_=None, **kwargs):
def images(dist=None):
'''
.. versionadded:: 2014.7.1
.. versionadded:: Lithium
List the available images for LXC's ``download`` template.
@ -1233,8 +1257,9 @@ def images(dist=None):
except ValueError:
continue
if not passed_header and dist == 'DIST':
passed_header = True
if not passed_header:
if distro == 'DIST':
passed_header = True
continue
dist_list = ret.setdefault(distro, [])
@ -1253,6 +1278,7 @@ def images(dist=None):
def create(name,
config=None,
profile=None,
network_profile=None,
**kwargs):
'''
Create a new container.
@ -1270,11 +1296,16 @@ def create(name,
<salt.modules.lxc.get_container_profile>`). Values in a profile will be
overridden by the **Container Creation Arguments** listed below.
network_profile
Network profile to use for container
.. versionadded:: Lithium
**Container Creation Arguments**
template
The template to use. E.g., 'ubuntu' or 'fedora'. Conflicts with the
``image`` argument.
The template to use. For example, ``ubuntu`` or ``fedora``. Conflicts
with the ``image`` argument.
.. note::
@ -1410,6 +1441,14 @@ def create(name,
output_loglevel='trace')
_clear_context()
if ret['retcode'] == 0 and exists(name):
if network_profile:
network_changes = apply_network_profile(name, network_profile)
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}}
@ -1441,7 +1480,7 @@ def clone(name,
clone_from
Name of the original container to be cloned
.. versionadded:: 2014.7.1
.. versionadded:: Lithium
profile
Profile to use in container cloning (see
@ -1567,7 +1606,7 @@ def list_(extra=False, limit=None):
Return output matching a specific state (**frozen**, **running**, or
**stopped**).
.. versionadded:: 2014.7.1
.. versionadded:: Lithium
CLI Examples:
@ -1695,7 +1734,7 @@ def _ensure_running(name, no_start=False):
def restart(name, force=False):
'''
.. versionadded:: 2014.7.1
.. versionadded:: Lithium
Restart the named container. If the container was not running, the
container will merely be started.
@ -1729,7 +1768,7 @@ def start(name, **kwargs):
Start the named container
restart : False
.. deprecated:: 2014.7.1
.. deprecated:: Lithium
Use :mod:`lxc.restart <salt.modules.lxc.restart>`
Restart the container if it is already running
@ -1764,7 +1803,7 @@ def stop(name, kill=False):
Older LXC versions will stop containers like this irrespective of this
argument.
.. versionchanged:: 2014.7.1
.. versionchanged:: Lithium
Default value changed to ``False``
CLI Example:
@ -1796,7 +1835,7 @@ def freeze(name, **kwargs):
If ``True`` and the container is stopped, the container will be started
before attempting to freeze.
.. versionadded:: 2014.7.1
.. versionadded:: Lithium
CLI Example:
@ -2111,7 +2150,7 @@ def info(name):
def set_password(name, users, password, encrypted=True):
'''
.. versionchanged:: 2014.7.1
.. versionchanged:: Lithium
Function renamed from ``set_pass`` to ``set_password``. Additionally,
this function now supports (and defaults to using) a password hash
instead of a plaintext password.
@ -2129,7 +2168,7 @@ def set_password(name, users, password, encrypted=True):
If true, ``password`` must be a password hash. Set to ``False`` to set
a plaintext password (not recommended).
.. versionadded:: 2014.7.1
.. versionadded:: Lithium
CLI Example:
@ -2277,7 +2316,7 @@ def update_lxc_conf(name, lxc_conf, lxc_conf_unset):
def set_dns(name, dnsservers=None, searchdomains=None):
'''
.. versionchanged:: 2014.7.1
.. versionchanged:: Lithium
The ``dnsservers`` and ``searchdomains`` parameters can now be passed
as a comma-separated list.
@ -2360,7 +2399,7 @@ def bootstrap(name,
Delay in seconds between end of container creation and bootstrapping.
Useful when waiting for container to obtain a DHCP lease.
.. versionadded:: 2014.7.1
.. versionadded:: Lithium
bootstrap_url
url, content or filepath to the salt bootstrap script
@ -2643,7 +2682,7 @@ def run_cmd(name,
ignore_retcode=False,
keep_env='http_proxy,https_proxy,no_proxy'):
'''
.. deprecated:: 2014.7.1
.. deprecated:: Lithium
Use :mod:`lxc.cmd_run <salt.module.lxc.cmd_run>` instead
'''
salt.utils.warn_until(
@ -2674,7 +2713,7 @@ def cmd_run(name,
ignore_retcode=False,
keep_env='http_proxy,https_proxy,no_proxy'):
'''
.. versionadded:: 2014.7.1
.. versionadded:: Lithium
Run :mod:`cmd.run <salt.modules.cmdmod.run>` within a container
@ -2749,7 +2788,7 @@ def cmd_run_stdout(name,
ignore_retcode=False,
keep_env='http_proxy,https_proxy,no_proxy'):
'''
.. versionadded:: 2014.7.1
.. versionadded:: Lithium
Run :mod:`cmd.run_stdout <salt.modules.cmdmod.run_stdout>` within a container
@ -2824,7 +2863,7 @@ def cmd_run_stderr(name,
ignore_retcode=False,
keep_env='http_proxy,https_proxy,no_proxy'):
'''
.. versionadded:: 2014.7.1
.. versionadded:: Lithium
Run :mod:`cmd.run_stderr <salt.modules.cmdmod.run_stderr>` within a container
@ -2897,7 +2936,7 @@ def cmd_retcode(name,
ignore_retcode=False,
keep_env='http_proxy,https_proxy,no_proxy'):
'''
.. versionadded:: 2014.7.1
.. versionadded:: Lithium
Run :mod:`cmd.retcode <salt.modules.cmdmod.retcode>` within a container
@ -2972,7 +3011,7 @@ def cmd_run_all(name,
ignore_retcode=False,
keep_env='http_proxy,https_proxy,no_proxy'):
'''
.. versionadded:: 2014.7.1
.. versionadded:: Lithium
Run :mod:`cmd.run_all <salt.modules.cmdmod.run_all>` within a container
@ -3061,7 +3100,7 @@ def cp(name, source, dest, makedirs=False):
dest
Destination on the container. Must be an absolute path.
.. versionchanged:: 2014.7.1
.. versionchanged:: Lithium
If the destination is a directory, the file will be copied into
that directory.
@ -3070,7 +3109,7 @@ def cp(name, source, dest, makedirs=False):
Create the parent directory on the container if it does not already
exist.
.. versionadded:: 2014.7.1
.. versionadded:: Lithium
CLI Example:
@ -3294,3 +3333,42 @@ def edit_conf(conf_file, out_format='simple', **kwargs):
write_conf(conf_file, data)
return read_conf(conf_file, out_format)
def apply_network_profile(name, network_profile):
'''
.. versionadded:: Lithium
Apply a network profile to a container
CLI Examples:
.. code-block:: bash
salt 'minion' lxc.apply_network_profile web1 centos
'''
path = '/var/lib/lxc/{0}/config'.format(name)
before = []
with salt.utils.fopen(path, 'r') as fp_:
for line in fp_:
before.append(line)
network_params = {}
for param in _network_conf(network_profile=network_profile):
network_params.update(param)
if network_params:
edit_conf(path, **network_params)
after = []
with salt.utils.fopen(path, 'r') as fp_:
for line in fp_:
after.append(line)
diff = ''
for line in difflib.unified_diff(before,
after,
fromfile='before',
tofile='after'):
diff += line
return diff