def start(self, log, version, serverid, record_id, webuser): try: self.do_prev_deploy(log) self.do_checkout(version, log) self.do_post_deploy(log) for sid in serverid: try: device_info = DeviceInfo.objects.filter( id=int(sid)).values() host = device_info[0]['hostname'] auth_type = device_info[0]['auth_type'] connect_info = ConnectionInfo.objects.filter( hostname=host, auth_type=auth_type).values() user = connect_info[0]['username'] passwd = connect_info[0]['password'] port = connect_info[0]['port'] auth_info = '{user}@{host}:{port}'.format(user=user, host=host, port=port) auth_key = {auth_type: passwd} connect = Shell(auth_info, connect_timeout=5, connect_kwargs=auth_key) self.do_prev_release(log, connect) self.do_release(log, connect) self.do_post_release(log, connect) except Exception as e: send = Tailf() send.send_message(webuser, str(e)) self.end(serverid, record_id) except Exception as e: send = Tailf() send.send_message(webuser, str(e))
def run(self, command, run_mode=run_mode_remote, write=None, pty=False, exception=True, ws=False, webuser=None, **kwargs): try: if run_mode == self.run_mode_local: result = super(Shell, self).local(command, pty=pty, echo_stdin=True, warn=True, watchers=[say_yes()], env=self.custom_global_env, **kwargs) else: result = super(Shell, self).run(command, pty=pty, echo_stdin=True, warn=True, watchers=[say_yes()], env=self.custom_global_env, **kwargs) exited, stdout, stderr = result.exited, result.stdout, result.stderr if result.failed: message = '[%s@%s]# %s\n[ERROR] %s' % ( self.user, self.host, command, stdout + stderr) error_logger.error(message) else: message = '[%s@%s]# %s\n%s' % (self.user, self.host, command, stdout) if write: with open(write, 'a') as f: f.write(message) elif ws and webuser: message_in = '[%s@%s]# %s' % (self.user, self.host, command) websocket = Tailf() websocket.send_message(webuser, message_in) for m in stdout.split('\n'): websocket.send_message(webuser, m) return result except Exception as e: message = '[%s@%s]%s' % (self.user, self.host, e) error_logger.error(message) message = '[%s@%s]# %s\n[ERROR] %s' % (self.user, self.host, command, str(e)) if write: with open(write, 'a') as f: f.write(message) elif ws and webuser: message_in = '[%s@%s]# %s' % (self.user, self.host, command) message_out = '[ERROR] %s' % (str(e)) Tailf.send_message(webuser, message_in) for m in message_out.split('\n'): Tailf.send_message(webuser, m) result = Result(exited=-1, stderr=message, stdout=message) return result
def local_tailf(logfile, webuser, id): info_logger.info('[部署日志监控开始] 文件地址:%s 用户:%s 项目ID:%s' % (logfile, webuser, id)) redis = RedisObj() f = open(logfile, 'rt') f.seek(0, 0) while True: line = f.readline() if not line: is_stop = redis.get('deploy_' + str(webuser) + '_' + str(id)) if is_stop == '1': Tailf.send_message(webuser, '[INFO]文件监视结束..') f.close() info_logger.info('[部署日志监控结束] 文件地址:%s 用户:%s 项目ID:%s' % (logfile, webuser, id)) break else: time.sleep(0.2) continue Tailf.send_message(webuser, line)
def sync(self, wtype, local=None, remote=None, write=None, ws=False, webuser=None): try: if wtype == 'put': result = super(Shell, self).put(local, remote=remote) message = '[%s@%s]# [上传文件]\n[INFO]local:%s to [%s]:%s' % ( self.user, self.host, local, result.connection.host, remote) info_logger.info(message) if write: with open(write, 'a') as f: f.write(message) elif ws and webuser: websocket = Tailf() websocket.send_message(webuser, message) else: result = super(Shell, self).get(remote, local=local) message = '[%s@%s]# [下载文件]\n[INFO] [%s]:%s to local:%s' % ( self.user, self.host, result.connection.host, remote, local) info_logger.info(message) if write: with open(write, 'a') as f: f.write(message) elif ws and webuser: websocket = Tailf() websocket.send_message(webuser, message) return result except Exception as e: if wtype == 'put': message = '[%s@%s]# [上传文件]\n[ERROR][目标目录:%s][%s]' % ( self.user, self.host, remote, str(e)) error_logger.info(message) if write: with open(write, 'a') as f: f.write(message) elif ws and webuser: websocket = Tailf() websocket.send_message(webuser, message) else: message = '[%s@%s]# [下载文件]\n[ERROR][目标目录:%s][%s]' % ( self.user, self.host, remote, str(e)) error_logger.info(message) if write: with open(write, 'a') as f: f.write(message) elif ws and webuser: websocket = Tailf() websocket.send_message(webuser, message)
def start(self, log, version, serverid, record_id, webuser): try: self.do_prev_deploy(log) self.do_checkout(version, log) self.do_post_deploy(log) for sid in serverid: try: auth_info, auth_key = auth_init(sid) connect = Shell(auth_info, connect_timeout=5, connect_kwargs=auth_key) self.do_prev_release(log, connect) self.do_release(log, connect) self.do_post_release(log, connect) except Exception as e: send = Tailf() send.send_message(webuser, str(e)) self.end(serverid, record_id) except Exception as e: send = Tailf() send.send_message(webuser, str(e))
def start(self, log, version, serverid, record_id, webuser, start_time): self.start_time = start_time with open(log, 'a') as f: f.write('[INFO]版本: %s 执行用户: %s 开始时间: %s\n[INFO]本次部署日志路径: %s\n' % (version, webuser, start_time, log)) try: self.do_prev_deploy(log) self.do_checkout(version, log) self.do_post_deploy(log) for sid in serverid: if sid: auth_info, auth_key = auth_init(sid) if auth_info and auth_key: connect = Shell(auth_info, connect_timeout=5, connect_kwargs=auth_key) self.do_prev_release(log, connect) self.do_release(log, connect) self.do_post_release(log, connect) else: Tailf.send_message(webuser, '[ERROR]服务器ID%s已被删除,部署继续执行!' % sid) else: Tailf.send_message(webuser, '没有选择远程服务器!!!') self.end(serverid, record_id) except Exception as e: Tailf.send_message(webuser, str(e))
def run(self, command, run_mode=run_mode_remote, write=None, pty=False, webuser=None, **kwargs): try: stream_out = None stream_err = None if write: io_dict = {} file_name = os.path.basename(write) io = os.path.splitext(file_name)[0] stream_out = io_dict[io + '_out'] = StringIO() stream_err = io_dict[io + '_err'] = StringIO() message = '[%s@%s]# %s\n' % (self.user, self.host, command) file = open(write, 'a') file.write(message) stream_out = stream_err = file if run_mode == self.run_mode_local: result = super(Shell, self).local(command, pty=pty, warn=True, out_stream=stream_out, err_stream=stream_err, watchers=[say_yes()], env=self.custom_global_env, **kwargs) else: result = super(Shell, self).run(command, pty=pty, warn=True, out_stream=stream_out, err_stream=stream_err, watchers=[say_yes()], env=self.custom_global_env, **kwargs) exited, stdout, stderr = result.exited, result.stdout, result.stderr if result.failed: message = '[%s@%s]# %s\n[ERROR] %s' % (self.user, self.host, command, stdout + stdout) error_logger.error(message) if webuser: message_in = '[%s@%s]# %s' % (self.user, self.host, command) websocket = Tailf() websocket.send_message(webuser, message_in) message_out = stdout + stderr for m in message_out.split('\n'): websocket.send_message(webuser, m) return result except Exception as e: message = '[%s@%s]%s' % (self.user, self.host, e) error_logger.error(message) message = '[%s@%s]# %s\n[ERROR] %s' % (self.user, self.host, command, str(e)) if write: with open(write, 'a') as f: f.write(message) elif webuser: message_in = '[%s@%s]# %s' % (self.user, self.host, command) message_out = '[ERROR] %s' % (str(e)) Tailf.send_message(webuser, message_in) for m in message_out.split('\n'): Tailf.send_message(webuser, m) result = Result(exited=-1, stderr=message, stdout=message) return result
def run(self, id, log, version, serverid, record_id, webuser, start_time): info_logger.info('[部署任务开始] 开始时间:%s 记录ID:%s 部署版本:%s 用户:%s 项目ID:%s' % (start_time, record_id, version, webuser, id)) redis = RedisObj() redis.set('deploy_' + str(webuser) + '_' + str(id), '0') self.init(webuser, record_id, id) self.start_time = start_time with open(log, 'a') as f: f.write('[INFO]版本: %s 执行用户: %s 开始时间: %s\n[INFO]本次部署日志路径: %s\n' % (version, webuser, start_time, log)) try: self.do_prev_deploy(log) self.do_checkout(version, log) self.do_post_deploy(log) for sid in serverid: try: connect = connect_init(sid) connect.init_env(env=self.custom_global_env) self.do_prev_release(log, connect) self.do_release(log, connect) self.do_post_release(log, connect) except Exception as e: time.sleep(5) Tailf.send_message(webuser, '[ERROR] 服务器为空或ID %s 可能已被删除!' % sid) Tailf.send_message(webuser, '[ERROR] 错误信息:' % e) error_logger.error( '[部署任务错误] 开始时间:%s 记录ID:%s 部署版本:%s 用户:%s 项目ID:%s 信息:%s' % (start_time, record_id, version, webuser, id, e)) self.end(serverid, record_id) info_logger.info('[部署任务已结束] 记录ID:%s 部署版本:%s 用户:%s 项目ID:%s' % (record_id, version, webuser, id)) except Exception as e: Tailf.send_message(webuser, '[ERROR] 错误信息: %s' % e) error_logger.error( '[部署任务错误] 开始时间:%s 记录ID:%s 部署版本:%s 用户:%s 项目ID:%s 信息:%s' % (start_time, record_id, version, webuser, id, e)) finally: if self.localhost: self.localhost.close() # 关闭local_tailf死循环 redis.set('deploy_' + str(webuser) + '_' + str(id), '1')
def start(self, log, version, serverid, record_id, webuser, start_time): self.start_time = start_time try: self.do_prev_deploy(log) self.do_checkout(version, log) self.do_post_deploy(log) for sid in serverid: if sid: auth_info, auth_key = auth_init(sid) if auth_info and auth_key: connect = Shell(auth_info, connect_timeout=5, connect_kwargs=auth_key) self.do_prev_release(log, connect) self.do_release(log, connect) self.do_post_release(log, connect) else: Tailf.send_message(webuser, '[ERROR]服务器ID%s已被删除,部署继续执行!' % sid) else: Tailf.send_message(webuser, '没有选择远程服务器!!!') self.end(serverid, record_id) except Exception as e: Tailf.send_message(webuser, str(e))
def run(self, command, run_mode=run_mode_remote, write=None, pty=True, exception=True, ws=False, webuser=None, **kwargs): try: if run_mode == self.run_mode_local: result = super(Shell, self).local(command, pty=pty, warn=True, watchers=[say_yes()], env=self.custom_global_env, **kwargs) else: result = super(Shell, self).run(command, pty=pty, warn=True, watchers=[say_yes()], env=self.custom_global_env, **kwargs) if result.failed: exited, stdout, stderr = result.exited, '', result.stdout message = '[%s@%s]# %s\n[ERROR] %s' % (self.user, self.host, command, stderr) if write: with open(write, 'a') as f: f.write(message) elif ws and webuser: message_in = '[%s@%s]# %s' % (self.user, self.host, command) message_out = '[ERROR] %s' % (stderr) websocket = Tailf() websocket.send_message(webuser, message_in) websocket.send_message(webuser, message_out) elif exception: raise Exception(stderr) else: exited, stdout, stderr = 0, result.stdout, '' message = '[%s@%s]# %s\n%s' % (self.user, self.host, command, stdout) if write: with open(write, 'a') as f: f.write(message) elif ws and webuser: message_in = '[%s@%s]# %s' % (self.user, self.host, command) message_out = stdout websocket = Tailf() websocket.send_message(webuser, message_in) websocket.send_message(webuser, message_out) if exited != 0: message = '[%s@%s] 执行命令:%s 错误:%s, 错误信息:%s' % ( self.user, self.host, command, exited, stderr) error_logger.error(message) return result return result except Exception as e: message = '[%s@%s]%s' % (self.user, self.host, e) error_logger.error(message) message = '[%s@%s]# %s\n[ERROR] %s' % (self.user, self.host, command, str(e)) if write: with open(write, 'a') as f: f.write(message) elif ws and webuser: message_in = '[%s@%s]# %s' % (self.user, self.host, command) message_out = '[ERROR] %s' % (str(e)) websocket = Tailf() websocket.send_message(webuser, message_in) websocket.send_message(webuser, message_out) result = Result(exited=-1, stderr=message, stdout=message) return result