コード例 #1
0
 def run_status(cls, name, cmd, config):
     """ Runs cloud-status in container
     """
     ip = cls.ip(name)
     cmd = ("sudo -H -u {2} TERM=xterm256-color ssh -t -q "
            "-l ubuntu -o \"StrictHostKeyChecking=no\" "
            "-o \"UserKnownHostsFile=/dev/null\" "
            "-o \"ControlMaster=auto\" "
            "-o \"ControlPersist=600\" "
            "-i {1} "
            "{0} {3}".format(ip, utils.ssh_privkey(),
                             utils.install_user(), cmd))
     log.debug("Running command without waiting "
               "for response.: {}".format(cmd))
     args = deque(shlex.split(cmd))
     os.execlp(args.popleft(), *args)
コード例 #2
0
 def run_status(cls, name, cmd, config):
     """ Runs cloud-status in container
     """
     ip = cls.ip(name)
     cmd = ("sudo -H -u {2} TERM=xterm256-color ssh -t -q "
            "-l ubuntu -o \"StrictHostKeyChecking=no\" "
            "-o \"UserKnownHostsFile=/dev/null\" "
            "-o \"ControlMaster=auto\" "
            "-o \"ControlPersist=600\" "
            "-i {1} "
            "{0} {3}".format(ip, utils.ssh_privkey(), utils.install_user(),
                             cmd))
     log.debug("Running command without waiting "
               "for response.: {}".format(cmd))
     args = deque(shlex.split(cmd))
     os.execlp(args.popleft(), *args)
コード例 #3
0
    def cp(cls, name, filepath, dst):
        """ copy file to container

        :param str name: name of container
        :param str filepath: file to copy to container
        :param str dst: destination of remote path
        """
        ip = cls.ip(name)
        cmd = ("scp -r -q "
               "-o \"StrictHostKeyChecking=no\" "
               "-o \"UserKnownHostsFile=/dev/null\" "
               "-i {identity} "
               "{filepath} "
               "ubuntu@{ip}:{dst} ".format(ip=ip, dst=dst,
                                           identity=utils.ssh_privkey(),
                                           filepath=filepath))
        ret = utils.get_command_output(cmd)
        if ret['status'] > 0:
            raise Exception("There was a problem copying ({0}) to the "
                            "container ({1}:{2}): {3}".format(
                                filepath, name, ip, ret['output']))
コード例 #4
0
    def cp(cls, name, filepath, dst):
        """ copy file to container

        :param str name: name of container
        :param str filepath: file to copy to container
        :param str dst: destination of remote path
        """
        ip = cls.ip(name)
        cmd = ("scp -r -q "
               "-o \"StrictHostKeyChecking=no\" "
               "-o \"UserKnownHostsFile=/dev/null\" "
               "-i {identity} "
               "{filepath} "
               "ubuntu@{ip}:{dst} ".format(ip=ip, dst=dst,
                                           identity=utils.ssh_privkey(),
                                           filepath=filepath))
        ret = utils.get_command_output(cmd)
        if ret['status'] > 0:
            raise Exception("There was a problem copying ({0}) to the "
                            "container ({1}:{2}): {3}".format(
                                filepath, name, ip, ret['output']))
コード例 #5
0
    def run(cls, name, cmd, use_ssh=False, use_sudo=False, output_cb=None):
        """ run command in container

        :param str name: name of container
        :param str cmd: command to run
        """

        if use_ssh:
            ip = cls.ip(name)
            quoted_cmd = shlex.quote(cmd)
            wrapped_cmd = ("sudo -H -u {3} TERM=xterm256-color ssh -t -q "
                           "-l ubuntu -o \"StrictHostKeyChecking=no\" "
                           "-o \"UserKnownHostsFile=/dev/null\" "
                           "-o \"ControlMaster=auto\" "
                           "-o \"ControlPersist=600\" "
                           "-i {2} "
                           "{0} {1}".format(ip, quoted_cmd,
                                            utils.ssh_privkey(),
                                            utils.install_user()))
        else:
            ip = "-"
            quoted_cmd = cmd
            wrapped_cmd = []
            if use_sudo:
                wrapped_cmd.append("sudo")
            wrapped_cmd.append("lxc-attach -n {container_name} -- "
                               "{cmd}".format(container_name=name,
                                              cmd=cmd))
            wrapped_cmd = " ".join(wrapped_cmd)

        stdoutmaster, stdoutslave = pty.openpty()
        subproc = subprocess.Popen(wrapped_cmd, shell=True,
                                   stdout=stdoutslave,
                                   stderr=subprocess.PIPE)
        os.close(stdoutslave)
        decoder = codecs.getincrementaldecoder('utf-8')()

        def last_ten_lines(s):
            chunk = s[-1500:]
            lines = chunk.splitlines(True)
            return ''.join(lines[-10:]).replace('\r', '')

        decoded_output = ""
        try:
            while subproc.poll() is None:
                try:
                    b = os.read(stdoutmaster, 512)
                except OSError as e:
                    if e.errno != errno.EIO:
                        raise
                    break
                else:
                    final = False
                    if not b:
                        final = True
                    decoded_chars = decoder.decode(b, final)
                    if decoded_chars is None:
                        continue

                    decoded_output += decoded_chars
                    if output_cb:
                        ls = last_ten_lines(decoded_output)

                        output_cb(ls)
                    if final:
                        break
        finally:
            os.close(stdoutmaster)
            if subproc.poll() is None:
                subproc.kill()
            subproc.wait()

        errors = [l.decode('utf-8') for l in subproc.stderr.readlines()]
        if output_cb:
            output_cb(last_ten_lines(decoded_output))

        errors = ''.join(errors)

        if subproc.returncode == 0:
            return decoded_output.strip()
        else:
            raise ContainerRunException("Problem running {0} in container "
                                        "{1}:{2}".format(quoted_cmd, name, ip),
                                        subproc.returncode)
コード例 #6
0
    def run(cls, name, cmd, use_ssh=False, use_sudo=False, output_cb=None):
        """ run command in container

        :param str name: name of container
        :param str cmd: command to run
        """

        if use_ssh:
            ip = cls.ip(name)
            quoted_cmd = shlex.quote(cmd)
            wrapped_cmd = ("sudo -H -u {3} TERM=xterm256-color ssh -t -q "
                           "-l ubuntu -o \"StrictHostKeyChecking=no\" "
                           "-o \"UserKnownHostsFile=/dev/null\" "
                           "-o \"ControlMaster=auto\" "
                           "-o \"ControlPersist=600\" "
                           "-i {2} "
                           "{0} {1}".format(ip, quoted_cmd,
                                            utils.ssh_privkey(),
                                            utils.install_user()))
        else:
            ip = "-"
            quoted_cmd = cmd
            wrapped_cmd = []
            if use_sudo:
                wrapped_cmd.append("sudo")
            wrapped_cmd.append("lxc-attach -n {container_name} -- "
                               "{cmd}".format(container_name=name, cmd=cmd))
            wrapped_cmd = " ".join(wrapped_cmd)

        stdoutmaster, stdoutslave = pty.openpty()
        subproc = subprocess.Popen(wrapped_cmd,
                                   shell=True,
                                   stdout=stdoutslave,
                                   stderr=subprocess.PIPE)
        os.close(stdoutslave)
        decoder = codecs.getincrementaldecoder('utf-8')()

        def last_ten_lines(s):
            chunk = s[-1500:]
            lines = chunk.splitlines(True)
            return ''.join(lines[-10:]).replace('\r', '')

        decoded_output = ""
        try:
            while subproc.poll() is None:
                try:
                    b = os.read(stdoutmaster, 512)
                except OSError as e:
                    if e.errno != errno.EIO:
                        raise
                    break
                else:
                    final = False
                    if not b:
                        final = True
                    decoded_chars = decoder.decode(b, final)
                    if decoded_chars is None:
                        continue

                    decoded_output += decoded_chars
                    if output_cb:
                        ls = last_ten_lines(decoded_output)

                        output_cb(ls)
                    if final:
                        break
        finally:
            os.close(stdoutmaster)
            if subproc.poll() is None:
                subproc.kill()
            subproc.wait()

        errors = [l.decode('utf-8') for l in subproc.stderr.readlines()]
        if output_cb:
            output_cb(last_ten_lines(decoded_output))

        errors = ''.join(errors)

        if subproc.returncode == 0:
            return decoded_output.strip()
        else:
            raise ContainerRunException(
                "Problem running {0} in container "
                "{1}:{2}".format(quoted_cmd, name, ip), subproc.returncode)