update lxc commands

This commit is contained in:
Justin Findlay 2015-01-10 17:39:42 -07:00
parent f554acc54c
commit 8dca3cd0e5

View File

@ -1198,14 +1198,14 @@ def create(name, config=None, profile=None, options=None, **kwargs):
for k, v in options.items(): for k, v in options.items():
cmd += ' --{0} {1}'.format(k, v) cmd += ' --{0} {1}'.format(k, v)
ret = __salt__['cmd.run_all'](cmd) ret = __salt__['cmd.run_all'](cmd, python_shell=False)
if ret['retcode'] == 0 and exists(name): if ret['retcode'] == 0 and exists(name):
return {'created': True} return {'created': True}
else: else:
if exists(name): if exists(name):
# destroy the container if it was partially created # destroy the container if it was partially created
cmd = 'lxc-destroy -n {0}'.format(name) cmd = 'lxc-destroy -n {0}'.format(name)
__salt__['cmd.retcode'](cmd) __salt__['cmd.retcode'](cmd, python_shell=False)
log.warn('lxc-create failed to create container') log.warn('lxc-create failed to create container')
return {'created': False, 'error': return {'created': False, 'error':
'container could not be created with cmd "{0}": {1}'.format(cmd, ret['stderr'])} 'container could not be created with cmd "{0}": {1}'.format(cmd, ret['stderr'])}
@ -1292,14 +1292,14 @@ def clone(name,
if backing: if backing:
cmd += ' -B {0}'.format(backing) cmd += ' -B {0}'.format(backing)
ret = __salt__['cmd.run_all'](cmd) ret = __salt__['cmd.run_all'](cmd, python_shell=False)
if ret['retcode'] == 0 and exists(name): if ret['retcode'] == 0 and exists(name):
return {'cloned': True} return {'cloned': True}
else: else:
if exists(name): if exists(name):
# destroy the container if it was partially created # destroy the container if it was partially created
cmd = 'lxc-destroy -n {0}'.format(name) cmd = 'lxc-destroy -n {0}'.format(name)
__salt__['cmd.retcode'](cmd) __salt__['cmd.retcode'](cmd, python_shell=False)
log.warn('lxc-clone failed to create container') log.warn('lxc-clone failed to create container')
return {'cloned': False, 'error': ( return {'cloned': False, 'error': (
'container could not be created' 'container could not be created'
@ -1317,7 +1317,7 @@ def ls():
salt '*' lxc.ls salt '*' lxc.ls
''' '''
return __salt__['cmd.run']('lxc-ls | sort -u').splitlines() return __salt__['cmd.run']('lxc-ls | sort -u', python_shell=True).splitlines()
def list_(extra=False): def list_(extra=False):
@ -1341,7 +1341,7 @@ def list_(extra=False):
salt '*' lxc.list salt '*' lxc.list
salt '*' lxc.list extra=True salt '*' lxc.list extra=True
''' '''
ctnrs = __salt__['cmd.run']('lxc-ls | sort -u').splitlines() ctnrs = __salt__['cmd.run']('lxc-ls | sort -u', python_shell=True).splitlines()
if extra: if extra:
stopped = {} stopped = {}
@ -1358,7 +1358,7 @@ def list_(extra=False):
for container in ctnrs: for container in ctnrs:
c_infos = __salt__['cmd.run']( c_infos = __salt__['cmd.run'](
'lxc-info -n {0}'.format(container)).splitlines() 'lxc-info -n {0}'.format(container), python_shell=False).splitlines()
log.debug(c_infos) log.debug(c_infos)
c_state = None c_state = None
for c_info in c_infos: for c_info in c_infos:
@ -1402,7 +1402,7 @@ def _change_state(cmd, name, expected):
return {'state': expected, 'change': False} return {'state': expected, 'change': False}
cmd = '{0} -n {1}'.format(cmd, name) cmd = '{0} -n {1}'.format(cmd, name)
err = __salt__['cmd.run_stderr'](cmd) err = __salt__['cmd.run_stderr'](cmd, python_shell=False)
if err: if err:
s2 = state(name) s2 = state(name)
r = {'state': s2, 'change': s1 != s2, 'error': err} r = {'state': s2, 'change': s1 != s2, 'error': err}
@ -1410,7 +1410,7 @@ def _change_state(cmd, name, expected):
if expected is not None: if expected is not None:
# some commands do not wait, so we will # some commands do not wait, so we will
cmd = 'lxc-wait -n {0} -s {1}'.format(name, expected.upper()) cmd = 'lxc-wait -n {0} -s {1}'.format(name, expected.upper())
__salt__['cmd.run'](cmd, timeout=30) __salt__['cmd.run'](cmd, timeout=30, python_shell=False)
s2 = state(name) s2 = state(name)
r = {'state': s2, 'change': s1 != s2} r = {'state': s2, 'change': s1 != s2}
return r return r
@ -1581,7 +1581,7 @@ def state(name):
return None return None
cmd = 'lxc-info -n {0}'.format(name) cmd = 'lxc-info -n {0}'.format(name)
ret = __salt__['cmd.run_all'](cmd) ret = __salt__['cmd.run_all'](cmd, python_shell=False)
if ret['retcode'] != 0: if ret['retcode'] != 0:
return False return False
else: else:
@ -1609,7 +1609,7 @@ def get_parameter(name, parameter):
return None return None
cmd = 'lxc-cgroup -n {0} {1}'.format(name, parameter) cmd = 'lxc-cgroup -n {0} {1}'.format(name, parameter)
ret = __salt__['cmd.run_all'](cmd) ret = __salt__['cmd.run_all'](cmd, python_shell=False)
if ret['retcode'] != 0: if ret['retcode'] != 0:
return False return False
else: else:
@ -1630,7 +1630,7 @@ def set_parameter(name, parameter, value):
return None return None
cmd = 'lxc-cgroup -n {0} {1} {2}'.format(name, parameter, value) cmd = 'lxc-cgroup -n {0} {1} {2}'.format(name, parameter, value)
ret = __salt__['cmd.run_all'](cmd) ret = __salt__['cmd.run_all'](cmd, python_shell=False)
if ret['retcode'] != 0: if ret['retcode'] != 0:
return False return False
else: else:
@ -1722,9 +1722,9 @@ def info(name):
ret['memory_free'] = free ret['memory_free'] = free
ret['size'] = __salt__['cmd.run']( ret['size'] = __salt__['cmd.run'](
('lxc-attach --clear-env -n {0} -- ' ('lxc-attach --clear-env -n {0} -- '
'df /|tail -n1|awk \'{{print $2}}\'').format(pipes.quote(name))) 'df /|tail -n1|awk \'{{print $2}}\'').format(pipes.quote(name)), python_shell=True)
ipaddr = __salt__['cmd.run']( ipaddr = __salt__['cmd.run'](
'lxc-attach --clear-env -n {0} -- ip addr show'.format(pipes.quote(name))) 'lxc-attach --clear-env -n {0} -- ip addr show'.format(pipes.quote(name)), python_shell=False)
for line in ipaddr.splitlines(): for line in ipaddr.splitlines():
if 'inet' in line: if 'inet' in line:
line = line.split() line = line.split()
@ -1784,7 +1784,7 @@ def set_pass(name, users, password):
pipes.quote(password), pipes.quote(password),
) )
cmd += " /bin/true\"" cmd += " /bin/true\""
cret = __salt__['cmd.run_all'](cmd) cret = __salt__['cmd.run_all'](cmd, python_shell=False)
if cret['retcode'] != 0: if cret['retcode'] != 0:
raise ValueError('Can\'t change passwords') raise ValueError('Can\'t change passwords')
ret['comment'] = 'Password updated for {0}'.format(users) ret['comment'] = 'Password updated for {0}'.format(users)
@ -1935,7 +1935,7 @@ def set_dns(name, dnsservers=None, searchdomains=None):
'lxc-attach --clear-env -n {0} -- rm /etc/resolv.conf &&' 'lxc-attach --clear-env -n {0} -- rm /etc/resolv.conf &&'
'echo {1}|lxc-attach --clear-env -n {0} -- ' 'echo {1}|lxc-attach --clear-env -n {0} -- '
'tee /etc/resolv.conf' 'tee /etc/resolv.conf'
).format(pipes.quote(name), pipes.quote(dns))) ).format(pipes.quote(name), pipes.quote(dns)), python_shell=True)
if not cret['retcode']: if not cret['retcode']:
ret['result'] = True ret['result'] = True
return ret return ret
@ -2093,7 +2093,7 @@ def attachable(name):
salt 'minion' lxc.attachable ubuntu salt 'minion' lxc.attachable ubuntu
''' '''
cmd = 'lxc-attach -n {0} -- /usr/bin/env'.format(pipes.quote(name)) cmd = 'lxc-attach -n {0} -- /usr/bin/env'.format(pipes.quote(name))
data = __salt__['cmd.run_all'](cmd) data = __salt__['cmd.run_all'](cmd, python_shell=False)
if not data['retcode']: if not data['retcode']:
return True return True
if data['stderr'].startswith('lxc-attach: failed to get the init pid'): if data['stderr'].startswith('lxc-attach: failed to get the init pid'):
@ -2159,7 +2159,7 @@ def run_cmd(name, cmd, no_start=False, preserve_state=True,
cmd = 'lxc-attach --clear-env {0} -n {1} -- {2}'.format(env, pipes.quote(name), cmd) cmd = 'lxc-attach --clear-env {0} -n {1} -- {2}'.format(env, pipes.quote(name), cmd)
if not use_vt: if not use_vt:
res = __salt__['cmd.run_all'](cmd) res = __salt__['cmd.run_all'](cmd, python_shell=False)
else: else:
stdout, stderr = '', '' stdout, stderr = '', ''
try: try:
@ -2212,7 +2212,7 @@ def run_cmd(name, cmd, no_start=False, preserve_state=True,
proc.terminate() proc.terminate()
else: else:
rootfs = info(name).get('rootfs') rootfs = info(name).get('rootfs')
res = __salt__['cmd.run_chroot'](rootfs, cmd) res = __salt__['cmd.run_chroot'](rootfs, cmd, python_shell=False)
if preserve_state: if preserve_state:
if prior_state == 'stopped': if prior_state == 'stopped':
@ -2257,13 +2257,13 @@ def cp(name, src, dest):
# before touching to existing file which may disturb any running # before touching to existing file which may disturb any running
# process, check that the md5sum are different # process, check that the md5sum are different
cmd = 'md5sum {0} 2> /dev/null'.format(src) cmd = 'md5sum {0}'.format(src)
csrcmd5 = __salt__['cmd.run_all'](cmd) csrcmd5 = __salt__['cmd.run_all'](cmd, python_shell=False)
srcmd5 = csrcmd5['stdout'].split()[0] srcmd5 = csrcmd5['stdout'].split()[0]
cmd = 'lxc-attach --clear-env -n {0} -- md5sum {1} 2> /dev/null'.format( cmd = 'lxc-attach --clear-env -n {0} -- md5sum {1}'.format(
pipes.quote(name), dest) name, dest)
cdestmd5 = __salt__['cmd.run_all'](cmd) cdestmd5 = __salt__['cmd.run_all'](cmd, python_shell=False)
if not cdestmd5['retcode']: if not cdestmd5['retcode']:
try: try:
destmd5 = cdestmd5['stdout'].split()[0] destmd5 = cdestmd5['stdout'].split()[0]
@ -2281,7 +2281,7 @@ def cp(name, src, dest):
cmd = 'cat {0} | lxc-attach --clear-env -n {1} -- tee {2} > /dev/null'.format( cmd = 'cat {0} | lxc-attach --clear-env -n {1} -- tee {2} > /dev/null'.format(
pipes.quote(src), pipes.quote(name), pipes.quote(dest)) pipes.quote(src), pipes.quote(name), pipes.quote(dest))
log.info(cmd) log.info(cmd)
ret = __salt__['cmd.run_all'](cmd) ret = __salt__['cmd.run_all'](cmd, python_shell=True)
return ret return ret