def lxc_clone(source, name): """ Clone the container source to target. Die with RuntimeError in case of a problem """ command = ['sudo', 'lxc-clone', '-o', source, '-n', name] _, err, code = call(command) if code != 0: raise RuntimeError(err)
def lxc_wait(name, state='STOPPED', timeout=10): """ Wait while LXC container reaches a defined state :raises: RuntimeError, if process was unable to wait for changing the state of container """ command = ['sudo', ] + _get_lxc_wait_command(name, state, timeout) _, err, code = call(command) if code != 0: raise RuntimeError(err)
def lxc_destroy(name): """ Destroy the LXC container Die with RuntimeError in case of a problem """ lxc_wait(name) command = ['sudo', 'lxc-destroy', '-n', name] _, err, code = call(command) if code != 0: raise RuntimeError(err)
def check_timelimit(self): """ Command timelimit """ out, _, code = call('which timelimit') reason = ('This command is used to limit the maximum time span ' 'of command execution. ') if code == 0: return ok('Command {0} found. {1}'.format(out, reason)) else: return err('Command "timelimit" is not found. {0}' 'Ubuntu and Debian users can set it up with ' '"apt-get install timeline"'.format(reason))
def check_sudo(self): """ Sudo support """ _, _, code = call('sudo whoami') reason = 'Feature is required to support LXC' if code == 0: return ok('User can execute command with sudo. {0}'.format(reason)) else: if settings.TRYTRY_LXC_ENABLED: return err('User CANNOT execute command with sudo. {0}'.format(reason)) else: return info('User cannot execute command with sudo. {0}'.format(reason))
def check_lxc_commands(self): """ LXC userspace support """ if settings.TRYTRY_LXC_ENABLED: out, _, code = call('which lxc') reason = 'This command is the indicator whether the LXC module is set up' if code == 0: return ok('Command {0} found. {1}'.format(out, reason)) else: return err('Command "lxc" is not found. {0}. Ubuntu and Debian ' 'users can set it up with "apt-get install lxc". See ' 'http://try-try.readthedocs.org/en/latest/lxc.html ' 'for more details'.format(reason))
def run_command(self, user_input): """ Run user provided command, applying timeout constraints :returns: A tuple containing (stdout, stderr, returncode) """ command, stdin = self.get_command(user_input) if self.flow and settings.TRYTRY_LXC_ENABLED: command = self.wrap_in_lxc(command) else: command = self.wrap_in_timeout(command) stdin = smart_str(stdin) command = [smart_str(c) for c in command] out, err, returncode = call(command, stdin) out = smart_unicode(out, strings_only=True) err = smart_unicode(err, strings_only=True) return (out, err, returncode)
def lxc_list(): """ Return the list of LXC containers """ out, _, _ = call(['sudo', 'lxc-ls']) return out.strip().split()
def test_call_string(self): cmd = "echo 'hello\"world'" out, _, _ = call(cmd) self.assertEqual(out, 'hello"world')
def test_call_list(self): cmd = ['echo', 'hello"world'] out, _, _ = call(cmd) self.assertEqual(out, 'hello"world')