def on_commits(self, message): wi = Deployer(task_id=self.room) if 'branch' not in message: emit('commits', {'event': 'error:commits', 'data': {'message': 'invalid branch'}}, room=self.room) else: commits = wi.list_commit(message['branch']) emit('repo', {'event': 'repo:branches', 'data': commits}, room=self.room)
def on_tags(self, message=None): wi = Deployer(project_id=self.room) try: tags = wi.list_tag() emit('tags', {'event': 'tags', 'data': tags}, room=self.room) except Exception as e: emit('tags', {'event': 'error', 'data': {'message': e.message}}, room=self.room)
def on_commits(self, message=None): wi = Deployer(project_id=self.room) if 'branch' not in message: emit('commits', { 'event': 'error', 'data': { 'message': 'invalid branch' } }, room=self.room) else: try: commits = wi.list_commit(message['branch']) emit('commits', { 'event': 'commits', 'data': commits }, room=self.room) except Exception as e: emit('commits', { 'event': 'error', 'data': { 'message': e.message } }, room=self.room)
def on_branches(self, message=None): wi = Deployer(project_id=self.room) try: branches = wi.list_branch() emit('branches', {'event': 'branches', 'data': branches}, room=self.room) except Exception as e: emit('branches', {'event': 'error', 'data': {'message': e.message}}, room=self.room)
def logs(self, task): task_info = TaskModel().get_by_id(self.task_id) if task_info.status not in [ TaskModel.status_doing, TaskModel.status_success, TaskModel.status_fail ]: emit('console', {'event': 'console', 'data': ''}, room=self.room) return True deployer = Deployer(task_id=self.room) for log in deployer.logs(): log = RecordModel.logs(**log) if log['stage'] == RecordModel.stage_end: cmd = 'success' if log[ 'status'] == RecordModel.status_success else 'fail' msg = log['host'] + ' 部署完成!' if log[ 'status'] == RecordModel.status_success else log[ 'host'] + Code.code_msg[Code.deploy_fail] emit(cmd, { 'event': 'finish', 'data': { 'host': log['host'], 'message': msg } }, room=self.room) else: emit('console', { 'event': 'console', 'data': log }, room=self.room) deployer.end(success=task_info.status == TaskModel.status_success, update_status=False)
def on_deploy(self, message): self.task_info = TaskModel(id=self.room).item() if self.task_info['status'] in [TaskModel.status_pass, TaskModel.status_fail]: wi = Deployer(task_id=self.room, console=True) ret = wi.walle_deploy() else: emit('console', {'event': 'task:forbidden', 'data': self.task_info}, room=self.room)
def logs(self, task): deployer = Deployer(task_id=self.room) for log in deployer.logs(): log = RecordModel.logs(**log) emit('console', {'event': 'console', 'data': log}, room=self.room) task_info = TaskModel().get_by_id(self.task_id) deployer.end(success=task_info.status == TaskModel.status_success, update_status=False)
def on_tags(self, message): wi = Deployer(task_id=self.room) tags = wi.list_tag() emit('console', { 'event': 'repo:branches', 'data': tags }, room=self.room)
def websocket(self, task_id=None): task_id = 12 wi = Deployer(task_id) ret = wi.walle_deploy() record = RecordModel().fetch(task_id) return self.render_json(data={ 'command': ret, 'record': record, })
def on_message(self, incoming): print 'message received %s' % incoming text = json.loads(incoming).get('text', None) task_id = text if text else 'Sorry could you repeat?' wi = Deployer(task_id, websocket=self) current_app.logger.info(current_user.id) ret = wi.walle_deploy() response = json.dumps(dict(output='receive: {0}'.format(task_id))) self.write_message(response)
def branches(self, project_id=None): """ fetch project list or one item /tag/ :return: """ wi = Deployer(project_id=project_id) branches = wi.list_branch() return self.render_json(data={ 'branches': branches, })
def tags(self, project_id=None): """ fetch project list or one item /tag/ :return: """ wi = Deployer(project_id=project_id) tag_list = wi.list_tag() tags = tag_list.stdout.strip().split('\n') return self.render_json(data={ 'tags': tags, })
def commits(self, project_id): """ fetch project list or one item /tag/ :return: """ branch = request.args.get('branch', '') wi = Deployer(project_id=project_id) commits = wi.list_commit(branch) return self.render_json(data={ 'branches': commits, })
def on_deploy(self, message): if self.task_info['status'] in [ TaskModel.status_pass, TaskModel.status_fail ]: wi = Deployer(task_id=self.room, console=True) if self.task_info['is_rollback']: wi.walle_rollback() else: wi.walle_deploy() else: emit('console', { 'event': 'forbidden', 'data': self.task_info }, room=self.room)
def logs(self, task): task_info = TaskModel().get_by_id(self.task_id) if task_info.status not in [ TaskModel.status_doing, TaskModel.status_success, TaskModel.status_fail ]: emit('console', {'event': 'console', 'data': ''}, room=self.room) return True deployer = Deployer(task_id=self.room) for log in deployer.logs(): log = RecordModel.logs(**log) emit('console', {'event': 'console', 'data': log}, room=self.room) deployer.end(success=task_info.status == TaskModel.status_success, update_status=False)
def commits(self, project_id): """ fetch project list or one item /tag/ :return: """ current_app.logger.info('args----------------') current_app.logger.info(project_id) current_app.logger.info(request.args) branch = request.args.get('branch', '') wi = Deployer(project_id=project_id) commits = wi.list_commit(branch) return self.render_json(data={ 'commits': commits, })
def post(self): """ fetch role list or one role :return: """ super(DeployAPI, self).post() task_id = request.form['task_id'] if not task_id or not task_id.isdigit(): return self.render_json(code=-1) wi = Deployer(task_id) ret = wi.walle_deploy() record = TaskRecordModel().fetch(task_id) return self.render_json(data={ 'command': '', 'record': record, })
def put(self,task_id): """ update deploy /deploy/<int:task_id> """ try: current_app.logger.info('-----------start a deploy with gitlab runner--------------') current_app.logger.info(task_id) task_info = TaskModel(id=task_id).item() wi = Deployer(task_id=task_id, console=False, api_trigger=True) deploy_status = False if task_info['is_rollback']: deploy_status = wi.walle_rollback() else: deploy_status = wi.walle_deploy() current_app.logger.info('-----------end deploy with gitlab runner--------------') return self.render_json(data=deploy_status) except Exception as e: current_app.logger.info(e) return self.render_error(code=2001, message='发布失败')
def detection(self, project_id): """ :param project_id: :return: """ # walle LOCAL_SERVER_USER => walle user # show ssh_rsa.pub # LOCAL_SERVER_USER => git # LOCAL_SERVER_USER => target_servers # webroot is directory # remote release directory errors = Deployer(project_id=project_id).project_detection() message = '' if not errors: message = '配置检测通过,恭喜:)开始你的上线之旅吧' return self.render_json(data=errors, message=message)
def on_deploy(self, message): emit('console', {'event': 'task:console', 'data': {}}, room=self.room) from walle.service.deployer import Deployer wi = Deployer(task_id=self.room) ret = wi.walle_deploy()
def on_branches(self, message): wi = Deployer(task_id=self.room) branches = wi.list_branch() emit('branches', {'event': 'repo:branches', 'data': branches}, room=self.room)