def __deploy_database(rid, data): r = Release.query.filter(Release.id == rid).one() try: client = Client(app.config["SSH_HOST"], app.config["SSH_USER"], app.config["SSH_PASSWORD"]) if u"任务完成" not in client.exec_script("/root/csfscript/dump_data/dump_data.py", data): return r.is_deployed = True log_path = "%s/data_deploy_log/myapp.%s" % (app.root_path, datetime.now().strftime("%Y-%m-%d")) Release.add_log(r.id, log_path) except Exception, e: log.exception(e)
class DeploySsh: def __init__(self,timeout=3): self.client = Client(app.config["SSH_HOST"], app.config["SSH_USER"], app.config["SSH_PASSWORD"]) self.timeout = timeout def add(self, content): ret = self.client.exec_script("/root/csfscript/server_ssh/add_auth.py", content,timeout=self.timeout) return ret def delete(self, content): ret = self.client.exec_script("/root/csfscript/server_ssh/del_auth.py", content) return True if "ok" in ret else False def list(self, content): ret = self.client.exec_script("/root/csfscript/server_ssh/list_auth.py", content) return json.loads(ret)
def __deploy_database(rid, data): r = Release.query.filter(Release.id == rid).one() try: client = Client(app.config["SSH_HOST"], app.config["SSH_USER"], app.config["SSH_PASSWORD"]) if u"任务完成" not in client.exec_script( "/root/csfscript/dump_data/dump_data.py", data): return r.is_deployed = True log_path = "%s/data_deploy_log/myapp.%s" % ( app.root_path, datetime.now().strftime("%Y-%m-%d")) Release.add_log(r.id, log_path) except Exception, e: log.exception(e)
def get(self, ip): client = Client(app.config["SSH_IDC_HOST"], app.config["SSH_IDC_USER"], app.config["SSH_IDC_PASSWORD"]) def exec_script(path): info = client.exec_script(path, ip, False) return json.loads(info.replace("u'", "'").replace("'", "\"")) return self.succ( exec_script("/root/csfscript/host_info/get_ssh_info.py") ["host_ssh_info"].replace("\n", " ").replace(" ", " "))
class DeploySsh: def __init__(self, timeout=3): self.client = Client(app.config["SSH_HOST"], app.config["SSH_USER"], app.config["SSH_PASSWORD"]) self.timeout = timeout def add(self, content): ret = self.client.exec_script("/root/csfscript/server_ssh/add_auth.py", content, timeout=self.timeout) return ret def delete(self, content): ret = self.client.exec_script("/root/csfscript/server_ssh/del_auth.py", content) return True if "ok" in ret else False def list(self, content): ret = self.client.exec_script( "/root/csfscript/server_ssh/list_auth.py", content) return json.loads(ret)
def __update(submit): client = Client(app.config["SSH_IDC_HOST"], app.config["SSH_IDC_USER"], app.config["SSH_IDC_PASSWORD"]) def exec_script(path): info = client.exec_script(path, submit["ip"], False) return json.loads(info.replace("u'", "'").replace("'", "\"")) if not submit.__contains__("ip"): # 更新描述信息 idc_host = Database.to_cls(Idc_host, submit) return idc_host submit["ssh_info"] = exec_script( "/root/csfscript/host_info/get_ssh_info.py" )["host_ssh_info"].replace("\n", " ").replace(" ", " ") submit["system_time"] = datetime.strptime( exec_script("/root/csfscript/host_info/get_system_time.py") ["system_time"], "%Y-%m-%d %I:%M:%S %p") submit.update( exec_script("/root/csfscript/host_info/get_host_info.py")) host_interfaces = submit.pop("host_interfaces") host_disks = submit.pop("host_disks") for key in submit: if isinstance(submit[key], list): submit[key] = ",".join(submit[key]) idc_host = Database.to_cls(Idc_host, submit) idc_host.ps_info = [ Database.to_cls(Idc_host_ps, _dict) for _dict in exec_script( "/root/csfscript/host_info/get_ps_info.py") ] Idc_host_ps.query.filter(Idc_host_ps.idc_host_id == None).delete() idc_host.host_interfaces = [ Database.to_cls(Idc_host_interface, _dict) for _dict in host_interfaces ] idc_host.host_disks = [ Database.to_cls(Idc_host_disk, _dict) for _dict in host_disks ] return idc_host
def add(cls, _dicts, auto_commit=True): idc_host = Idc_host.query.filter(Idc_host.id == _dicts).one() client = Client(app.config["SSH_IDC_HOST"], app.config["SSH_IDC_USER"], app.config["SSH_IDC_PASSWORD"]) def exec_script(path): info = client.exec_script(path, idc_host.ip, False) return json.loads(info.replace("u'", "'").replace("'", "\"")) idc_host.ps_info = [ Database.to_cls(Idc_host_ps, _dict) for _dict in exec_script( "/root/csfscript/host_info/get_ps_info.py") ] Idc_host_ps.query.filter(Idc_host_ps.idc_host_id == None).delete() db.session.commit() return True
def __init__(self,timeout=3): self.client = Client(app.config["SSH_HOST"], app.config["SSH_USER"], app.config["SSH_PASSWORD"]) self.timeout = timeout
from bmp.apis.base import BaseApi from bmp.utils.ssh import Client class Idc_ssh_infoApi(BaseApi): route = ["/idc/ssh/<string:ip>"] def get(self, ip): client = Client(app.config["SSH_IDC_HOST"], app.config["SSH_IDC_USER"], app.config["SSH_IDC_PASSWORD"]) def exec_script(path): info = client.exec_script(path, ip, False) return json.loads(info.replace("u'", "'").replace("'", "\"")) return self.succ( exec_script("/root/csfscript/host_info/get_ssh_info.py") ["host_ssh_info"].replace("\n", " ").replace(" ", " ")) if __name__ == "__main__": client = Client(app.config["SSH_IDC_HOST"], app.config["SSH_IDC_USER"], app.config["SSH_IDC_PASSWORD"]) def exec_script(path): info = client.exec_script(path, "192.168.0.231", False) return json.loads(info.replace("u'", "'").replace("'", "\"")) exec_script("/root/csfscript/host_info/get_ssh_info.py" )["host_ssh_info"].split("\n")
def __init__(self, timeout=3): self.client = Client(app.config["SSH_HOST"], app.config["SSH_USER"], app.config["SSH_PASSWORD"]) self.timeout = timeout
"success": False, "error": "" } results.append(result) if Idc_host.query \ .filter(Idc_host.ip == _dict["ip"]) \ .filter(Idc_host.type_id == _dict["type_id"]) \ .count(): result["error"] = "%s 已经存在" % _dict["ip"] continue idc_host = Idc_host.__update(_dict) db.session.add(idc_host) result["success"] = True except Exception, e: traceback.print_exc() log.exception(e) db.session.commit() return results if __name__ == "__main__": client = Client(app.config["SSH_IDC_HOST"], app.config["SSH_IDC_USER"], app.config["SSH_IDC_PASSWORD"]) client.exec_script("/root/csfscript/host_info/get_host_info.py", "122.144.134.170", False)
_dicts = [_dicts] for _dict in _dicts: try: result = {"ip": _dict["ip"], "type_id": _dict["type_id"], "success": False, "error": ""} results.append(result) if Idc_host.query \ .filter(Idc_host.ip == _dict["ip"]) \ .filter(Idc_host.type_id == _dict["type_id"]) \ .count(): result["error"] = "%s 已经存在" % _dict["ip"] continue idc_host = Idc_host.__update(_dict) db.session.add(idc_host) result["success"] = True except Exception, e: traceback.print_exc() log.exception(e) db.session.commit() return results if __name__ == "__main__": client = Client(app.config["SSH_IDC_HOST"], app.config["SSH_IDC_USER"], app.config["SSH_IDC_PASSWORD"]) client.exec_script("/root/csfscript/host_info/get_host_info.py", "122.144.134.170", False)