Esempio n. 1
0
    def push_template(self,
                      servername,
                      templatename,
                      destination,
                      template_params,
                      new_owner=None,
                      file_mode=700):
        # renders a template file and pushes it to the
        # target location on an external server
        # not implemented for writing to localhost at this time
        lock_fabric()
        env.key_filename = self.servers[servername]["ssh_key"]
        env.user = self.servers[servername]["ssh_user"]
        env.disable_known_hosts = True
        env.host_string = self.servers[servername]["hostname"]
        try:
            upload_template(
                templatename,
                destination,
                use_jinja=True,
                context=template_params,
                template_dir=self.conf["handyrep"]["templates_dir"],
                use_sudo=True)
            if file_mode:
                sudo("chmod %d %s" % (
                    file_mode,
                    destination,
                ), quiet=True)
            if new_owner:
                sudo("chown %s %s" % (
                    new_owner,
                    destination,
                ), quiet=True)
        except:
            self.log(
                'PLUGIN', 'could not push template %s to server %s - %s' %
                (templatename, servername, traceback.format_exc()), True)
            retdict = return_dict(
                False, "could not push template %s to server %s" % (
                    templatename,
                    servername,
                ))
        else:
            retdict = return_dict(True, "pushed template")
        finally:
            self.disconnect_and_unlock()

        return retdict
Esempio n. 2
0
    def sudorun(self, servername, commands, runas, passwd=""):
        # generic function to run one or more commands
        # as a specific remote user.  returns the results
        # of the last command run.  aborts when any
        # command fails
        env.key_filename = self.servers[servername]["ssh_key"]
        env.user = self.servers[servername]["ssh_user"]
        env.disable_known_hosts = True
        env.host_string = self.servers[servername]["hostname"]
        rundict = return_dict(True, "no commands provided", {"return_code" : None })
        if passwd is None:
            pgpasswd = ""
        else:
            pgpasswd = passwd

        for command in commands:
            try:
                with shell_env(PGPASSWORD=pgpasswd):
                    runit = sudo(command, user=runas, warn_only=True,pty=False)
                rundict.update({ "details" : runit ,
                    "return_code" : runit.return_code })
                if runit.succeeded:
                    rundict.update({"result":"SUCCESS"})
                else:
                    rundict.update({"result":"FAIL"})
                    break
            except Exception as ex:
                rundict = { "result" : "FAIL",
                    "details" : "connection failure: %s" % self.exstr(ex),
                    "return_code" : None }
                break
        
        disconnect_all()
        return rundict
Esempio n. 3
0
    def push_template(self, servername, templatename, destination, template_params, new_owner=None, file_mode=700):
        # renders a template file and pushes it to the
        # target location on an external server
        # not implemented for writing to localhost at this time
        env.key_filename = self.servers[servername]["ssh_key"]
        env.user = self.servers[servername]["ssh_user"]
        env.disable_known_hosts = True
        env.host_string = self.servers[servername]["hostname"]
        try:
            upload_template( templatename, destination, use_jinja=True, context=template_params, template_dir=self.conf["handyrep"]["templates_dir"], use_sudo=True )
            if file_mode:
                sudo("chmod %d %s" % (file_mode, destination,))
            if new_owner:
                sudo("chown %s %s" % (new_owner, destination,))
        except:
            retdict = return_dict(False, "could not push template %s to server %s" % (templatename, servername,))
        else:
            retdict = return_dict(True, "pushed template")
        finally:
            disconnect_all()

        return retdict
Esempio n. 4
0
    def sudorun(self, servername, commands, runas, passwd="", sshpass=None):
        # generic function to run one or more commands
        # as a specific remote user.  returns the results
        # of the last command run.  aborts when any
        # command fails
        lock_fabric()
        if sshpass:
            env.password = sshpass
        else:
            env.key_filename = self.servers[servername]["ssh_key"]

        env.user = self.servers[servername]["ssh_user"]
        env.disable_known_hosts = True
        env.host_string = self.servers[servername]["hostname"]
        rundict = return_dict(True, "no commands provided",
                              {"return_code": None})
        if passwd is None:
            pgpasswd = ""
        else:
            pgpasswd = passwd

        for command in commands:
            try:
                with shell_env(PGPASSWORD=pgpasswd):
                    runit = sudo(command,
                                 user=runas,
                                 warn_only=True,
                                 pty=False,
                                 quiet=True)
                rundict.update({
                    "details": runit,
                    "return_code": runit.return_code
                })
                if runit.succeeded:
                    rundict.update({"result": "SUCCESS"})
                else:
                    rundict.update({"result": "FAIL"})
                    break
            except Exception as ex:
                rundict = {
                    "result": "FAIL",
                    "details": "connection failure: %s" % self.exstr(ex),
                    "return_code": None
                }
                break

        self.disconnect_and_unlock()
        return rundict
Esempio n. 5
0
 def rd(self, success, details, extra={}):
     return return_dict(success, details, extra)
Esempio n. 6
0
 def rd(self, success, details, extra={}):
     return return_dict(success,details,extra)