Пример #1
0
def execute(command, to_filename=None):
    logger.debug("Trying to execute command: %s", command)
    commands = [c.strip() for c in re.split(ur'\|', command)]
    env = os.environ
    env["PATH"] = "/bin:/usr/bin:/sbin:/usr/sbin"

    to_file = None
    if to_filename:
        to_file = open(to_filename, 'wb')

    process = []
    for c in commands:
        try:
            process.append(subprocess.Popen(
                shlex.split(c),
                env=env,
                stdin=(process[-1].stdout if process else None),
                stdout=(to_file
                        if (len(process) == len(commands) - 1) and to_file
                        else subprocess.PIPE),
                stderr=(subprocess.PIPE)
            ))
        except OSError as e:
            return (1, "", "%s\n" % str(e))

        if len(process) >= 2:
            process[-2].stdout.close()
    stdout, stderr = process[-1].communicate()
    return (process[-1].returncode, stdout, stderr)
Пример #2
0
 def __init__(self, data, conf):
     logger.debug("Initializing driver %s: host=%s",
                  self.__class__.__name__, data.get("host"))
     self.data = data
     self.host = self.data.get("host", "localhost")
     self.local = is_local(self.host)
     self.conf = conf
Пример #3
0
 def __init__(self, data, conf):
     logger.debug("Initializing driver %s: host=%s",
                  self.__class__.__name__, data.get("host"))
     self.data = data
     self.host = self.data.get("host", "localhost")
     self.local = is_local(self.host)
     self.conf = conf
Пример #4
0
 def __init__(self, data, conf):
     super(File, self).__init__(data, conf)
     self.path = self.data["path"]
     logger.debug("File to get: %s", self.path)
     self.target_path = os.path.join(
         self.conf.target, self.host, self.path.lstrip("/"))
     logger.debug("File to save: %s", self.target_path)
Пример #5
0
 def __init__(self, data, conf):
     super(File, self).__init__(data, conf)
     self.path = self.data["path"]
     logger.debug("File to get: %s", self.path)
     self.target_path = os.path.join(
         self.conf.target, self.host, self.path.lstrip("/"))
     logger.debug("File to save: %s", self.target_path)
Пример #6
0
 def get(self, path, target_path):
     if not self.local:
         with fabric.api.settings(host_string=self.host):
             logger.debug("Getting remote file: %s %s", path, target_path)
             return fabric.api.get(path, target_path)
     else:
         logger.debug("Getting local file: cp -r %s %s", path, target_path)
         execute("mkdir -p %s" % os.path.dirname(target_path))
         return execute("cp -r %s %s" % (path, target_path))
Пример #7
0
 def command(self, command):
     out = CommandOut()
     if not self.local:
         with fabric.api.settings(host_string=self.host):
             logger.debug("Running remote command: "
                          "host: %s command: %s", self.host, command)
             output = fabric.api.run(command, pty=True)
             out.stdout = output
             out.return_code = output.return_code
             out.stderr = output.stderr
     else:
         logger.debug("Running local command: %s", command)
         out.return_code, out.stdout, out.stderr = execute(command)
     return out
Пример #8
0
 def sed(self, from_filename, to_filename, gz=False):
     sedscript = tempfile.NamedTemporaryFile()
     logger.debug("Sed script: %s", sedscript.name)
     for orig, new in self.subs.iteritems():
         logger.debug("Sed script: s/%s/%s/g", orig, new)
         sedscript.write("s/%s/%s/g\n" % (orig, new))
         sedscript.flush()
     command = " | ".join(filter(lambda x: x != "", [
         "cat %s" % from_filename,
         self.decompress(from_filename),
         "sed -f %s" % sedscript.name,
         self.compress(from_filename),
     ]))
     execute(command, to_filename=to_filename)
     sedscript.close()
Пример #9
0
 def get(self, path, target_path):
     try:
         if not self.local:
             with fabric.api.settings(host_string=self.host,
                                      timeout=2, warn_only=True):
                 logger.debug("Getting remote file: %s %s",
                              path, target_path)
                 return fabric.api.get(path, target_path)
         else:
             logger.debug("Getting local file: cp -r %s %s",
                          path, target_path)
             execute("mkdir -p %s" % os.path.dirname(target_path))
             return execute("cp -r %s %s" % (path, target_path))
     except Exception as e:
         logger.error("Error occured: %s", str(e))
Пример #10
0
 def sed(self, from_filename, to_filename, gz=False):
     sedscript = tempfile.NamedTemporaryFile()
     logger.debug("Sed script: %s", sedscript.name)
     for orig, new in self.subs.iteritems():
         logger.debug("Sed script: s/%s/%s/g", orig, new)
         sedscript.write("s/%s/%s/g\n" % (orig, new))
         sedscript.flush()
     command = " | ".join(filter(lambda x: x != "", [
         "cat %s" % from_filename,
         self.decompress(from_filename),
         "sed -f %s" % sedscript.name,
         self.compress(from_filename),
     ]))
     execute(command, to_filename=to_filename)
     sedscript.close()
Пример #11
0
 def get(self, path, target_path):
     """target_path must be the directory where to put
     copied files or directories
     """
     try:
         if not self.local:
             with fabric.api.settings(host_string=self.host,
                                      timeout=2, warn_only=True):
                 logger.debug("Getting remote file: %s %s",
                              path, target_path)
                 execute("mkdir -p %s" % target_path)
                 return fabric.api.get(path, target_path)
         else:
             logger.debug("Getting local file: cp -r %s %s",
                          path, target_path)
             execute("mkdir -p %s" % target_path)
             return execute("cp -r %s %s" % (path, target_path))
     except Exception as e:
         logger.error("Error occured: %s", str(e))
Пример #12
0
 def get(self, path, target_path):
     """target_path must be the directory where to put
     copied files or directories
     """
     try:
         if not self.local:
             with fabric.api.settings(host_string=self.host,
                                      timeout=2,
                                      warn_only=True):
                 logger.debug("Getting remote file: %s %s", path,
                              target_path)
                 execute("mkdir -p %s" % target_path)
                 return fabric.api.get(path, target_path)
         else:
             logger.debug("Getting local file: cp -r %s %s", path,
                          target_path)
             execute("mkdir -p %s" % target_path)
             return execute("cp -r %s %s" % (path, target_path))
     except Exception as e:
         logger.error("Error occured: %s", str(e))
Пример #13
0
 def snapshot(self):
     logger.debug("Making snapshot")
     for obj_data in self.conf.objects:
         logger.debug("Dumping: %s", obj_data)
         driver = Driver.getDriver(obj_data, self.conf)
         driver.snapshot()
     logger.debug("Archiving dump directory: %s", self.conf.target)
     execute("tar zcf {0}.tgz -C {1} {2}"
             "".format(self.conf.target,
                       os.path.dirname(self.conf.target),
                       os.path.basename(self.conf.target)))
     execute("rm -r {0}".format(self.conf.target))
     with open(self.conf.lastdump, "w") as fo:
         fo.write("%s.tgz" % self.conf.target)
     return "%s.tgz" % self.conf.target
Пример #14
0
 def command(self, command):
     out = CommandOut()
     try:
         if not self.local:
             with fabric.api.settings(host_string=self.host,
                                      timeout=2, warn_only=True):
                 logger.debug("Running remote command: "
                              "host: %s command: %s", self.host, command)
                 output = fabric.api.run(command, pty=True)
                 out.stdout = output
                 out.return_code = output.return_code
                 out.stderr = output.stderr
         else:
             logger.debug("Running local command: %s", command)
             out.return_code, out.stdout, out.stderr = execute(command)
         logger.debug("Stderr: %s", out.stderr)
     except Exception as e:
         logger.error("Error occured: %s", str(e))
     return out
Пример #15
0
 def command(self, command):
     out = CommandOut()
     try:
         if not self.local:
             with fabric.api.settings(host_string=self.host,
                                      timeout=2,
                                      warn_only=True):
                 logger.debug(
                     "Running remote command: "
                     "host: %s command: %s", self.host, command)
                 output = fabric.api.run(command, pty=True)
                 out.stdout = output
                 out.return_code = output.return_code
                 out.stderr = output.stderr
         else:
             logger.debug("Running local command: %s", command)
             out.return_code, out.stdout, out.stderr = execute(command)
         logger.debug("Stderr: %s", out.stderr)
     except Exception as e:
         logger.error("Error occured: %s", str(e))
     return out
Пример #16
0
 def __init__(self, conf):
     logger.debug("Initializing snapshot manager")
     self.conf = conf