def run_as_handyrep(self, servername, commands): # runs a set of commands as the "handyrep" user # exiting when the first command fails # returns a dic with the results of the last command # run 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"] rundict = { "result": "SUCCESS", "details" : "no commands provided", "return_code" : None } for command in commands: try: runit = run(command, warn_only=True, 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" % exstr(ex), "return_code" : None } break self.disconnect_and_unlock() return rundict
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
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
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
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: 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
def run_as_handyrep(self, servername, commands): # runs a set of commands as the "handyrep" user # exiting when the first command fails # returns a dic with the results of the last command # run 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"] rundict = { "result": "SUCCESS", "details": "no commands provided", "return_code": None } for command in commands: try: runit = run(command, warn_only=True, 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" % exstr(ex), "return_code": None } break self.disconnect_and_unlock() return rundict
def disconnect_and_unlock(self): disconnect_all() lock_fabric(False) return True