Example #1
0
def update_cpio(img_path, dir_path=None):
    tmp_dir = tempfile.mkdtemp(dir=dir_path)
    try:
        with subprocess.popen(["gunzip", "-c", img_path],
                              stdout=subprocess.PIPE) as proc:
            subprocess.call(["cpio", "-id"], stdin=proc.stdout, cwd=tmp_dir)
        yield tmp_dir
        tmp_dir_len = len(tmp_dir)
        with tempfile.NamedTemporaryFile(dir=dir_path) as new_img:
            with subprocess.popen(["cpio", "--format", "newc", "-o"],
                                  stdin=subprocess.PIPE,
                                  stdout=subprocess.PIPE,
                                  cwd=tmp_dir) as cpio:
                with subprocess.popen(["gzip", "-c"],
                                      stdin=cpio.stdout,
                                      stdout=new_img,
                                      cwd=tmp_dir):
                    for path, dirs, files in os.walk(tmp_dir):
                        for name in itertools.chain(dirs, files):
                            p_name = os.path.join(path, name)[tmp_dir_len + 1:]
                            cpio.stdin.write("{0}\n".format(p_name))
                    cpio.stdin.close()
            shutil.move(new_img.name, img_path)
            new_img.delete = False
    finally:
        shutil.rmtree(tmp_dir)
Example #2
0
def update_cpio(img_path, dir_path=None):
    tmp_dir = tempfile.mkdtemp(dir=dir_path)
    try:
        with subprocess.popen(
                ["gunzip", "-c", img_path],
                stdout=subprocess.PIPE) as proc:
            subprocess.call(
                ["cpio", "-id"], stdin=proc.stdout, cwd=tmp_dir)
        yield tmp_dir
        tmp_dir_len = len(tmp_dir)
        with tempfile.NamedTemporaryFile(dir=dir_path) as new_img:
            with subprocess.popen(
                    ["cpio", "--format", "newc", "-o"],
                    stdin=subprocess.PIPE,
                    stdout=subprocess.PIPE,
                    cwd=tmp_dir) as cpio:
                with subprocess.popen(
                        ["gzip", "-c"],
                        stdin=cpio.stdout,
                        stdout=new_img,
                        cwd=tmp_dir):
                    for path, dirs, files in os.walk(tmp_dir):
                        for name in itertools.chain(dirs, files):
                            p_name = os.path.join(path, name)[tmp_dir_len + 1:]
                            cpio.stdin.write("{0}\n".format(p_name))
                    cpio.stdin.close()
            shutil.move(new_img.name, img_path)
            new_img.delete = False
    finally:
        shutil.rmtree(tmp_dir)
Example #3
0
def archivate_cmd_output(archive, cmd, filename):
    suffix = ".{0}".format(os.path.basename(filename))
    with tempfile.NamedTemporaryFile(suffix=suffix) as f:
        with subprocess.popen(cmd, stdout=subprocess.PIPE) as process:
            shutil.copyfileobj(process.stdout, f)
        f.flush()
        archive.add(f.name, filename)
Example #4
0
    def restore(self):
        dump = self.archive.extractfile(self.filename)
        subprocess.call(["systemctl", "stop"] + self.services)
        subprocess.call(["sudo", "-u", "postgres", "dropdb", "--if-exists",
                         self.db])
        with subprocess.popen(["sudo", "-u", "postgres", "psql"],
                              stdin=subprocess.PIPE) as process:
            shutil.copyfileobj(dump, process.stdin)

        puppet.apply_task(self.db)
Example #5
0
def apply_patches(container, prefix, *patches, **kwargs):
    """Apply set of patches to a container's filesystem"""
    revert = kwargs.pop('revert', False)
    # TODO: review all logic here to apply all preprocessing steps to patches
    # beforehand
    tempdir = tempfile.mkdtemp(prefix='octane_docker_patches.')
    try:
        files = []
        for patch in patches:
            for fname in get_files_from_patch(patch):
                if fname.startswith(prefix):
                    files.append(fname[len(prefix) + 1:])
                else:
                    files.append(fname)
        files = [os.path.join(prefix, f) for f in files]
        get_files_from_docker(container, files, tempdir)
        prefix = os.path.dirname(files[0])  # FIXME: WTF?!
        direction = "-R" if revert else "-N"
        with subprocess.popen(
                ["patch", direction, "-p0", "-d", tempdir + "/" + prefix],
                stdin=subprocess.PIPE,
                ) as proc:
            for patch in patches:
                with open(patch) as p:
                    for line in p:
                        if line.startswith('+++'):  # FIXME: PLEASE!
                            try:
                                slash_pos = line.rindex('/', 4)
                                space_pos = line.index(' ', slash_pos)
                            except ValueError:
                                pass
                            else:
                                line = ('+++ ' +
                                        line[slash_pos + 1:space_pos] +
                                        '\n')
                        proc.stdin.write(line)
        put_files_to_docker(container, "/", tempdir)
    finally:
        shutil.rmtree(tempdir)
Example #6
0
def in_container(container, args, **popen_kwargs):
    """Create Popen object to run command defined by list args in container"""
    return subprocess.popen(["dockerctl", "shell", container] + args,
                            name=args[0],
                            **popen_kwargs)
Example #7
0
def popen(cmd, **kwargs):
    return subprocess.popen(cmd, popen_class=SSHPopen, **kwargs)
Example #8
0
def popen(cmd, **kwargs):
    return subprocess.popen(cmd, popen_class=SSHPopen, **kwargs)
Example #9
0
def get_mco_ping_status(node_id=None):
    cmd = ["mco", "rpc", "rpcutil", "ping", "--json"]
    if node_id is not None:
        cmd.extend(["-I", str(node_id)])
    with subprocess.popen(cmd, stdout=subprocess.PIPE) as proc:
        return json.load(proc.stdout)