def deployServices(self, env, services, phone_number): self.env = env self.services = services self.phone_number = phone_number result = [] obj = winconf.objects.filter(env=self.env).filter( servicename=self.services) for name in obj: stop = self.salt.cmd(name.hostname.saltname, 'cmd.run', ['net stop %s' % name.servicename]) result.append(stop) while True: task_list = self.salt.cmd( name.hostname.saltname, 'cmd.run', ['tasklist | findstr %s' % name.tasklist_name]) if not task_list[name.hostname.saltname]: print 'proccess has been exit.' break time.sleep(3) update = self.salt.cmd(name.hostname.saltname, 'cmd.run', [ 'svn up %s --username=%s --password=%s' % (name.localpath, name.username, name.password) ]) result.append(update) start = self.salt.cmd(name.hostname.saltname, 'cmd.run', ['net start %s' % name.servicename]) result.append(start) action = 'deploy ' + name.servicename logs(self.user, self.ip, action, result) servicename = name.servicename.strip('"') dingding_robo(name.hostname.saltname, servicename, start, self.user, self.phone_number) return result
def servicesAction(self, services, action, phone_number): self.services = services self.action = action self.phone_number = phone_number result = [] for s in self.services: sName, host = s.split(',') if action == 'start': getMes = self.salt.cmd(host, 'cmd.run', ['net start %s' % sName]) elif action == 'stop': getMes = self.salt.cmd(host, 'cmd.run', ['net stop %s' % sName]) elif action == 'restart': getMes = self.salt.cmd(host, 'cmd.run', ['net stop %s' % sName]) result.append(getMes) getMes = self.salt.cmd(host, 'cmd.run', ['net start %s' % sName]) result.append(getMes) info = self.action + ' ' + sName.strip('"') dingding_robo(host, info, getMes, self.user, self.phone_number) logs(self.user, self.ip, info, result) return result
def __nginx_backup(self, proxy_server, web_server, nginx_module, deploy_pillar): self.proxy_server = proxy_server self.web_server = web_server self.nginx_module = nginx_module self.deploy_pillar = deploy_pillar try: ##########nginx backup####### s, backup = commands.getstatusoutput("salt " + self.proxy_server + " state.sls queue=True " + self.nginx_module + " " + self.deploy_pillar) self.f.write(backup) self.f.flush() self.result.append(backup) if backup.find('Failed: 0') < 0: print "!!!!!!!!!!!!!!!!!! [backup server] ERROR !!!!!!!!!!!!!!!!!!!" return 1 self.f.write('\n\n\n\n\n') return 0 except Exception, e: print e self.f.write('error') self.f.close() dingding_robo(self.web_server, self.site, 'error', self.username, self.phone_number) logs(self.username, self.ip, self.site, 'Failed') return 1
def __svn_update(self, web_server, svn_path, svn_username, svn_password, svn_revision): self.web_server = web_server self.svn_path = svn_path self.svn_username = svn_username self.svn_password = svn_password self.svn_revision = svn_revision try: #####svn update######## update_cmd = 'svn update -r %s %s --username=%s --password=%s' % ( self.svn_revision, self.svn_path, self.svn_username, self.svn_password) print '-----svn----', update_cmd data = { 'client': 'local', 'tgt': self.web_server, 'fun': 'cmd.run', 'arg': update_cmd } update = salt_api.salt_cmd(data) print update if update['return'][0][self.web_server].find( 'Error') > 0 or update['return'][0][self.web_server].find( 'error') > 0: print "!!!!!!!!!!!!!!!!!! [update svn] ERROR !!!!!!!!!!!!!!!!!!!" return 1 except Exception, e: print e self.f.write('error') self.f.close() dingding_robo(self.web_server, self.site, 'error', self.username, self.phone_number) logs(self.username, self.ip, self.site, 'Failed') return 1
def salt_run_sls(login_user, svnrepo, projectname, salt_hostname, login_ip): errcode = 0 msg = 'ok' try: pillar = '''pillar=\"{'svnrepo': '%s', 'goprograme': '%s'}\"''' % ( svnrepo, projectname) pull_svn_cmd = "salt " + salt_hostname + " state.sls queue=True goservices.pull_svn " + pillar print 'pull_svn_cmd : ' print pull_svn_cmd s, result = commands.getstatusoutput(pull_svn_cmd) if result.find('Failed: 0') < 0: utils.logs(login_user, login_ip, 'pull svn ' + projectname, 'Failed') print 'result : ' print result errcode = 501 msg = u'salt执行不成功:\n' msg += result.split('\n')[0] else: utils.logs(login_user, login_ip, 'pull svn ' + projectname, 'Successful') except Exception as e: errcode = 500 msg = u'salt执行失败' return errcode, msg
def __svn_update(self, web_server, svn_path, svn_username, svn_password, svn_revision): self.web_server = web_server self.svn_path = svn_path self.svn_username = svn_username self.svn_password = svn_password self.svn_revision = svn_revision try: #####svn update######## update_cmd = '\'svn update -r %s %s --username=%s --password=%s \'' % ( self.svn_revision, self.svn_path, self.svn_username, self.svn_password) s, update = commands.getstatusoutput("salt " + self.web_server + " cmd.run " + update_cmd) self.f.write(update) self.f.flush() self.result.append(update) if update.find('Error') > 0 or update.find('error') > 0: print "!!!!!!!!!!!!!!!!!! [update svn] ERROR !!!!!!!!!!!!!!!!!!!" return 1 self.f.write('\n\n\n\n\n') return 0 except Exception, e: print e self.f.write('error') self.f.close() dingding_robo(self.web_server, self.site, 'error', self.username, self.phone_number) logs(self.username, self.ip, self.site, 'Failed') return 1
def deploy(self, site, action='svn', revision='HEAD'): self.site = site self.action = action self.revision = revision obj = webSite.objects.filter(env=self.env).filter(webSite=self.site) for info in obj: for host in info.checkUrl.values(): print host['host'] for m in info.state_module.values(): deploy_pillar = "pillar=\"{'deployserver':'" + host[ 'host'] + "', 'deployhost':'" + info.salt_pillar_host + "'}\"" nginx_backup = self.__nginx_backup(info.lb_server, host['host'], m['state_module'], deploy_pillar) if nginx_backup == 1: self.f.write('error') exit() if self.action in ['svn', 'revert']: svn_up = self.__svn_update(host['host'], info.svn_path, info.svn_username, info.svn_password, self.revision) if svn_up == 1: self.f.write('error') exit() elif self.action == 'recycle': pass recycle = self.__iis_recycle(host['host'], info.recycle_cmd, host['url'], self.action) if recycle == 1: self.f.write('error') exit() for m in info.state_module.values( ): ######nginx all online######## print m['state_module'] deploy_pillar = "pillar=\"{'deployserver':'none', 'deployhost':'none'}\"" status = self.__nginx_backup(info.lb_server, host['host'], m['state_module'], deploy_pillar) if status == 1: logs(self.username, self.ip, self.site, 'Failed') self.f.write('error') exit() self.f.write('done') self.f.flush() self.f.close() logs(self.username, self.ip, self.site, 'Successful') return self.result
def __iis_recycle(self, web_server, recycle_cmd, web_url, action): self.web_server = web_server self.recycle_cmd = recycle_cmd self.web_url = web_url self.action = action try: ####recycle iis####### data = { 'client': 'local', 'tgt': self.web_server, 'fun': 'cmd.run', 'arg': self.recycle_cmd } print '----------', self.web_server, self.recycle_cmd recycle = salt_api.salt_cmd(data) print recycle if recycle['return'][0][self.web_server].find('successfully') < 0: print "!!!!!!!!!!!!!!!!!! [recycle iis] ERROR !!!!!!!!!!!!!!!!!!" return 1 c = 6 while c > 1: print c r = requests.get(self.web_url, headers={'Host': self.site}, timeout=60, allow_redirects=False) c -= 1 if self.action == 'recycle': dingding_robo(self.web_server, 'recycle ' + self.site, 'success', self.username, self.phone_number) elif self.action == 'revert': dingding_robo(self.web_server, 'revert ' + self.site, 'success', self.username, self.phone_number) else: dingding_robo(self.web_server, self.site, 'success', self.username, self.phone_number) return 0 except Exception, e: print e self.f.write('error') self.f.close() if self.action == 'recycle': dingding_robo(self.web_server, self.site, 'is error', self.username, self.phone_number) elif self.action == 'revert': dingding_robo(self.web_server, self.site, 'is error', self.username, self.phone_number) else: dingding_robo(self.web_server, self.site, 'is error', self.username, self.phone_number) logs(self.username, self.ip, self.site, 'Failed') return 1
def deploy(self, site, action='svn', revision='HEAD'): self.site = site self.action = action self.revision = revision info = webSite.objects.filter(env=self.env).get(webSite=self.site) ip = [] for i in info.checkUrl.values(): ip.append(i['ip']) for host in info.checkUrl.values(): print host['host'] for m in info.state_module.values(): nginx_backup = self.__nginx_backup(ip, host['ip'], host['host'], m['state_module'], 0) if nginx_backup == 1: self.f.write('error') exit() if self.action in ['svn', 'revert']: svn_up = self.__svn_update(host['host'], info.svn_path, info.svn_username, info.svn_password, self.revision) if svn_up == 1: self.f.write('error') exit() elif self.action == 'recycle': pass recycle = self.__iis_recycle(host['host'], info.recycle_cmd, host['url'], self.action) if recycle == 1: self.f.write('error') exit() for m in info.state_module.values(): ######nginx all online######## status = self.__nginx_backup(ip, host['ip'], host['host'], m['state_module'], 1) if status == 1: logs(self.username, self.ip, self.site, 'Failed') self.f.write('error') exit() self.f.write('done') self.f.flush() self.f.close() logs(self.username, self.ip, self.site, 'Successful') return self.result
def command_exec(username, phone, ip, svc_name, cmd, zone, output): cmd_host = cmd_host_qcd if zone == 'qcd' else cmd_host_aws _cmd = 'svn co http://svn.65dg.me/svn/gotemplate /srv/gotemplate ' \ ' --non-interactive --username={username} --password={password} && ' \ 'svn co http://svn.65dg.me/svn/{svc_name} /srv/{svc_name} ' \ ' --non-interactive --username={username} --password={password} && ' \ 'mkdir -p {log_path} && ' \ 'cd /srv/{svc_name} && ' \ '/srv/{svc_name}/{cmd} -c /srv/gotemplate/{svc_name}/conf.ctmpl >> {output} 2>&1' \ ''.format(username=svn_username, password=svn_password, svc_name=svc_name, cmd=cmd, output=output, log_path=os.path.dirname(output)) print(_cmd) salt_api = SaltApi() data = { 'client': 'local', 'fun': 'cmd.run', 'tgt': cmd_host, 'arg': _cmd } result = salt_api.salt_cmd(data) if result != 0: result = result['return'] if not os.path.exists(os.path.dirname(output)): try: os.makedirs(os.path.dirname(output)) except Exception as e: print(e) logs(username, ip, cmd, e) dingding_robo(cmd_host, 'command job', ' error ', username, phone) return str(e) with open(output, 'w') as f: if isinstance(result, list): for r in result: if isinstance(r, dict): for k, v in r.items(): f.write('--> ') f.write(k) f.write('\n') f.write(v) else: f.write(str(r)) else: f.write(str(result)) logs(username, ip, cmd, 'running') dingding_robo(cmd_host, 'command job', result, username, phone) return result
def ack_job(request): user = request.user ip = request.META['REMOTE_ADDR'] env = request.POST['env'] jobIds = request.POST.getlist('jobIds', []) if not user.groups.filter(name__in=['admin', 'dba', 'disque']).exists(): logs(user, ip, 'ack job: %s , zone: %s' % (jobIds, env), 'permission denied') return HttpResponse(json.dumps({'errcode': 403}), content_type=default_content_type) if not (env in clientEnvMap.keys()): logs(user, ip, 'ack job: %s' % jobIds, 'unknown disque zone: %s' % env) return HttpResponse(json.dumps({ 'errcode': 400, 'msg': 'unknown disque zone:%s' % env }), content_type=default_content_type) if len(jobIds) == 0: logs(user, ip, 'ack job: zone-%s' % env, 'empty jobIds') return HttpResponse(json.dumps({ 'errcode': 400, 'msg': 'empty jobIds' }), content_type=default_content_type) jobIds = map(lambda x: x.encode('utf-8'), jobIds) print user, env, jobIds try: client = clientEnvMap[env] client.ack_job(*jobIds) except Exception as e: print e logs(user, ip, 'ack job: %s , zone: %s' % (jobIds, env), str(e)) return HttpResponse(json.dumps({ 'errcode': 400, 'msg': str(e) }), content_type=default_content_type) logs(user, ip, 'ack job: %s , zone: %s' % (jobIds, env), 'success') return HttpResponse(json.dumps({'errcode': 200}), content_type=default_content_type)
def __nginx_backup(self, hosts, block_ip, block_server, nginx_upstream, is_block): self.hosts = hosts self.block_ip = block_ip self.block_server = block_server self.nginx_upstream = nginx_upstream upstream_host = '' try: for ip in self.hosts: if is_block == 1: upstream_host += 'server %s; ' % ip else: if len(self.hosts) == 1: upstream_host += 'server %s; ' % ip elif ip != self.block_ip: upstream_host += 'server %s; ' % ip if not upstream_host: print 'not upstream_host!!!!' exit() for lb in nginx_api: url = "%s/upstream/%s" % (lb, self.nginx_upstream) r = requests.post(url, data=upstream_host) self.f.write('curl ' + url) self.f.write('\n\n\n\n\n') self.f.flush() if r.text != 'success': exit() r = requests.get(url) self.f.write(r.text) self.f.flush() self.f.write('\n\n\n\n\n') return 0 except Exception, e: print e self.f.write('error') self.f.close() dingding_robo(self.block_server, self.site, 'is error', self.username, self.phone_number) logs(self.username, self.ip, self.site, 'Failed') return 1
def kettle_run(user, ip, cmd_type, file_path, kettle_log_file, phone_number): salt_api = SaltApi() file_path = kettle_svn_path + file_path kettle_log_file = kettle_log_path + kettle_log_file if int(cmd_type) == 1: cmd = '%span.sh -file %s -logfile %s' % (kettle_install_dir, file_path, kettle_log_file) elif int(cmd_type) == 2: cmd = '%skitchen.sh -file %s -logfile %s' % ( kettle_install_dir, file_path, kettle_log_file) exists_file_cmd = 'ls %s' % file_path exists_file = { 'client': 'local', 'tgt': kettle_host, 'fun': 'cmd.run', 'arg': exists_file_cmd } file_result = salt_api.salt_cmd(exists_file) if file_result['return'][0][kettle_host] != file_path: logs(user, ip, cmd, 'not %s file.' % file_path) dingding_robo(kettle_host, 'kettle job', 'it is error', user, phone_number) return 0 data = { 'client': 'local', 'tgt': kettle_host, 'fun': 'cmd.run', 'arg': cmd } result = salt_api.salt_cmd(data) if result != 0: result = result['return'] logs(user, ip, cmd, 'running') dingding_robo(kettle_host, 'kettle job', result, user, phone_number) return result
def mautic_restart(request): user = request.user ip = request.META['REMOTE_ADDR'] host = request.POST['host'] try: if not user.groups.filter(name__in=['admin', 'mautic']).exists(): raise Exception('Permission Denied!') salt_api = SaltApi() data = { 'client': 'local', 'fun': 'cmd.run', 'tgt': host, 'arg': 'service php7.1-fpm restart', } result = salt_api.salt_cmd(data)['return'] logs(user, ip, host, 'service php7.1-fpm restart') except Exception as e: print e result = [{'restart mautic failed': str(e)}] return render(request, 'getdata.html', {'result': result})
result = [{'HandleTasks':'The task_id handle to failed!'}] elif content['ticket_type'] == 'webpage': try: data = { 'client':'local', 'tgt': webpage_host, 'fun':'cmd.script', 'arg':['salt://scripts/webpage.py','"%s"' % str(content['site_name'])] } salt_api.salt_cmd(data) except Exception, e: print e handle_result = 1 TicketTasks.objects.filter(tasks_id=task_id).update(state='5') TicketOperating.objects.create(operating_id=operating_id,handler=username,content=reply,result='3',submitter=content['owner']) logs(user=request.user,ip=request.META['REMOTE_ADDR'],action='handle ticket (%s)' % content['title'],result='failed') info = 'The "%s" order is failed,please check in %s host.' % (content['title'],host) dingding_robo(phone_number=phone_number,types=2,info=info) result = [{'HandleTasks':'The task_id handle to failed!'}] elif content['ticket_type'] == 'uat_jenkins': try: jenkins = existGitlabProject(content['jenkins_name'],content['uat_env']) print '-----jenkins---',jenkins if jenkins == 2: result = [{'HandleTasks': 'The project name is not exist.'}] handle_result = 1 elif jenkins == 3: result = [{'HandleTasks': 'jenkins creating is error.'}] handle_result = 1 elif jenkins == 4: result = [{'HandleTasks': 'svn repo creating is error.'}]
def ack_job(request): user = request.user ip = request.META['REMOTE_ADDR'] zone = request.POST['zone'] jobIds = request.POST.getlist('jobIds', []) if not user.groups.filter(name__in=['admin', 'dba', 'disque']).exists(): logs(user, ip, 'ack job: %s , zone: %s' % (jobIds, zone), 'permission denied') return HttpResponse(json.dumps({'errcode': 403}), content_type=DEFAULT_CONTENT_TYPE) try: clusterInfo = ClusterInfo.objects.get(name=zone) print clusterInfo.addr except ClusterInfo.DoesNotExist: logs(user, ip, 'ack job: %s' % jobIds, 'unknown disque zone: %s' % zone) return HttpResponse(json.dumps({ 'errcode': 400, 'msg': 'unknown disque zone:%s' % zone }), content_type=DEFAULT_CONTENT_TYPE) except ClusterInfo.MultipleObjectsReturned: logs(user, ip, 'ack job: %s' % jobIds, 'multi objects returned for zone: %s' % zone) return HttpResponse(json.dumps({ 'errcode': 400, 'msg': 'multi objects returned for zone:%s' % zone }), content_type=DEFAULT_CONTENT_TYPE) except Exception as e: print e logs(user, ip, 'ack job: %s , zone: %s' % (jobIds, zone), str(e)) return HttpResponse(json.dumps({ 'errcode': 400, 'msg': str(e) }), content_type=DEFAULT_CONTENT_TYPE) if len(jobIds) == 0: logs(user, ip, 'ack job: zone-%s' % zone, 'empty jobIds') return HttpResponse(json.dumps({ 'errcode': 400, 'msg': 'empty jobIds' }), content_type=DEFAULT_CONTENT_TYPE) jobIds = map(lambda x: x.encode('utf-8'), jobIds) print user, zone, jobIds try: addr = clusterInfo.addr.split(',') client = Client(addr) client.connect() client.ack_job(*jobIds) except Exception as e: print e logs(user, ip, 'ack job: %s , zone: %s' % (jobIds, zone), str(e)) return HttpResponse(json.dumps({ 'errcode': 400, 'msg': str(e) }), content_type=DEFAULT_CONTENT_TYPE) logs(user, ip, 'ack job: %s , zone: %s' % (jobIds, zone), 'success') return HttpResponse(json.dumps({'errcode': 200}), content_type=DEFAULT_CONTENT_TYPE)
mes = {host: repo_info} result.append(mes) notification_mes = 'It is successful.' else: notification_mes = 'It is error.' else: result.append(cmdinfo) notification_mes = 'It is error.' except Exception, e: print e result.append(cmdinfo) notification_mes = 'The system is error.....' dingding_robo(host, 'create svn repo "%s"' % svnName, notification_mes, username, request.POST['phone_number']) logs(username, ip, 'create svn repo "%s"' % svnName, notification_mes) return render(request, 'getdata.html', {'result': result}) @login_required @deny_resubmit(page_key='deploy_svn_add_user') def svnAddUser(request): username = request.user ip = request.META['REMOTE_ADDR'] env = request.POST['env'] host = request.POST['hostname'] svnName = request.POST['svnName'] svnPassword = request.POST['svnPassword'] result = []
def __iis_recycle(self, web_server, recycle_cmd, web_url, action): self.web_server = web_server self.recycle_cmd = recycle_cmd self.web_url = web_url self.action = action try: ####recycle iis####### s, recycle = commands.getstatusoutput("salt " + self.web_server + " cmd.run '" + self.recycle_cmd + "'") self.f.write(recycle) self.f.flush() self.result.append(recycle) if recycle.find('Fail') > 0 or recycle.find('fail') > 0: print "!!!!!!!!!!!!!!!!!! [recycle iis] ERROR !!!!!!!!!!!!!!!!!!" return 1 self.f.write('\n\n\n\n\n') i = 0 while i < 5: start_time = time.time() s, testUrl = commands.getstatusoutput("curl -H \"Host:" + self.site + "\" -I " + self.web_url) self.f.write(testUrl) self.f.flush() self.f.write('\n\n\n\n\n') print time.time() - start_time if time.time() - start_time < 2: break i = i + 1 if i == 5: print "!!!!!!!!!!!!!!!!!! [recycle iis] TIMEOUT !!!!!!!!!!!!!!!!!!" self.f.write('error') self.f.close() return 1 if self.action == 'recycle': dingding_robo(self.web_server, 'recycle ' + self.site, 'success', self.username, self.phone_number) elif self.action == 'revert': dingding_robo(self.web_server, 'revert ' + self.site, 'success', self.username, self.phone_number) else: dingding_robo(self.web_server, self.site, 'success', self.username, self.phone_number) return 0 except Exception, e: print e self.f.write('error') self.f.close() if self.action == 'recycle': dingding_robo(self.web_server, self.site, 'error', self.username, self.phone_number) elif self.action == 'revert': dingding_robo(self.web_server, self.site, 'error', self.username, self.phone_number) else: dingding_robo(self.web_server, self.site, 'error', self.username, self.phone_number) logs(self.username, self.ip, self.site, 'Failed') return 1
def add_job(request): user = request.user ip = request.META['REMOTE_ADDR'] zone = request.POST['zone'] queue = request.POST['queue_name'] # timeout_ms = request.POST['timeout_ms'] # replicate = request.POST['replicate'] # retry_sec = request.POST['retry_sec'] # delay_sec = request.POST['delay_sec'] # ttl_sec = request.POST['ttl_sec'] jobs = request.POST.getlist('jobs', []) # print user, env, queue, timeout_ms, replicate, retry_sec, delay_sec, ttl_sec print user, zone, queue print jobs if not user.groups.filter(name__in=['admin', 'dba', 'disque']).exists(): logs(user, ip, 'add job: %s - %s' % (zone, queue), 'permission denied') return HttpResponse(json.dumps({'errcode': 403}), content_type=DEFAULT_CONTENT_TYPE) try: clusterInfo = ClusterInfo.objects.get(name=zone) print clusterInfo.addr except ClusterInfo.DoesNotExist: logs(user, ip, 'add job: %s - %s' % (zone, queue), 'unknown disque zone: %s' % zone) return HttpResponse(json.dumps({ 'errcode': 400, 'msg': 'unknown disque zone:%s' % zone }), content_type=DEFAULT_CONTENT_TYPE) except ClusterInfo.MultipleObjectsReturned: logs(user, ip, 'add job: %s - %s' % (zone, queue), 'multi objects returned for zone: %s' % zone) return HttpResponse(json.dumps({ 'errcode': 400, 'msg': 'multi objects returned for zone:%s' % zone }), content_type=DEFAULT_CONTENT_TYPE) except Exception as e: print e logs(user, ip, 'add job: %s - %s' % (zone, queue), str(e)) return HttpResponse(json.dumps({ 'errcode': 400, 'msg': str(e) }), content_type=DEFAULT_CONTENT_TYPE) if (not queue) or len(queue) == 0: logs(user, ip, 'add job: %s - %s' % (zone, queue), 'empty queue name') return HttpResponse(json.dumps({ 'errcode': 400, 'msg': 'empty queue name' }), content_type=DEFAULT_CONTENT_TYPE) if len(jobs) == 0: logs(user, ip, 'add job: %s - %s' % (zone, queue), 'empty jobs') return HttpResponse(json.dumps({ 'errcode': 400, 'msg': 'empty jobs' }), content_type=DEFAULT_CONTENT_TYPE) jobs = map(lambda x: x.encode('utf-8'), jobs) jobIds = [] errJob = [] addr = clusterInfo.addr.split(',') client = Client(addr) client.connect() for job in jobs: try: print job jobId = client.add_job(queue, job) # jobId = client.add_job(queue, job, timeout=timeout_ms, replicate=replicate, delay=delay_sec, retry=retry_sec, ttl=ttl_sec) jobIds.append(jobId) except Exception as e: print e errJob.append(job) logs(user, ip, 'add job: %s - %s' % (zone, queue), 'success') return HttpResponse(json.dumps({ 'errcode': 200, 'jobIds': jobIds, 'failJobs': errJob }), content_type=DEFAULT_CONTENT_TYPE)
def deploy(self, site, action='svn', revision='HEAD', group=None): self.action = action self.revision = revision if group: obj = groupName.objects.get(group_name=group).member.values() else: obj = [] obj.append({'webSite': site}) for s in obj: self.site = s['webSite'] info = webSite.objects.filter(env=self.env).get(webSite=self.site) ip = [] zbx = ZabbixMonitor(zabbix_url, zabbix_user, zabbix_password, zabbix_userId) ## if the server is not normal. for h in info.checkUrl.values(): cpu = zbx.cpu_percent_usage(h['host']) print "host:%s------cpu:%s" % (h['host'], cpu) if float(cpu) > 95: self.f.write( 'The %s server has a problem. Please contact the devops...\n' % h['host']) self.f.write('error') self.f.flush() exit() ip.append(h['ip']) print '--------site--------------', info.webSite self.f.write('-----------------------%s---------------------\n' % info.webSite) self.f.flush() for host in info.checkUrl.values(): for m in info.state_module.values(): nginx_backup = self.__nginx_backup(ip, host['ip'], host['host'], m['state_module'], 0) if nginx_backup == 1: self.f.write( 'Step 1: blocking nginx traffic is failed.\n') exit() self.f.write('Step 1: blocking nginx traffic is sucessful.\n') self.f.flush() if self.action in ['svn', 'revert']: svn_up = self.__svn_update(host['host'], info.svn_path, info.svn_username, info.svn_password, self.revision) if svn_up == 1: self.f.write('error') exit() else: self.f.write('Step 2: svn update is sucessful.\n') self.f.flush() elif self.action == 'recycle': pass recycle = self.__iis_recycle(host['host'], info.recycle_cmd, host['url'], self.action) if recycle == 1: self.f.write('error') exit() elif action == 'recycle': self.f.write('Step 2: iis recycle is sucessful.\n') self.f.flush() else: self.f.write('Step 3: iis recycle is sucessful.\n') self.f.flush() for m in info.state_module.values( ): ######nginx all online######## status = self.__nginx_backup(ip, host['ip'], host['host'], m['state_module'], 1) if status == 1: logs(self.username, self.ip, self.site, 'Failed') self.f.write('error') exit() self.f.write('Last step: open nginx traffic is sucessful.\n') self.f.flush() logs(self.username, self.ip, self.site, 'Successful') self.f.write('done') self.f.flush() self.f.close() return self.result
def submit_tickets(request): title = request.POST['title'] ticket_type = request.POST['ticket_type'] handler = request.POST['handler'] owner = str(request.user) if ticket_type == 'go': function = request.POST['function'] hosts = request.POST.getlist('hosts') project = request.POST['project'] go_command = request.POST['go_command'] supervisor_name = request.POST['supervisor_name'] svn_repo = request.POST['svn_repo'] statsd = request.POST['statsd'] sentry = request.POST['sentry'] go_command = go_command + " -c /srv/gotemplate/%s/conf.ctmpl" % project salt_command = { "title": title, "ticket_type": ticket_type, "function": function, "hosts": hosts, "project": project, "svn_repo": svn_repo, "supervisor_name": supervisor_name, "go_command": go_command, "statsd": statsd, "sentry": sentry, "handler": handler, "owner": owner } elif ticket_type == 'webpage': print '--------site_name-----:', request.POST.getlist('site_name') site_name = request.POST.getlist('site_name') salt_command = { "title": title, "ticket_type": ticket_type, "site_name": site_name, "handler": handler, "owner": owner } try: salt_command = json.dumps(salt_command) ticket_type = TicketType.objects.get(type_name=ticket_type) handler = User.objects.get(username=handler) task_id = str(uuid.uuid1()) TicketTasks.objects.create(tasks_id=task_id, title=title, ticket_type=ticket_type, creator=request.user, content=salt_command, handler=handler, state='1') result = [{ 'TicketTasks': 'The %s order submitted to success!' % (task_id) }] logs(user=request.user, ip=request.META['REMOTE_ADDR'], action='add ticket (%s)' % title, result='successful') user = User.objects.get(username=handler) phone_number = UserProfile.objects.get(user=user).phone_number info = 'You have a message,please visit to workflow page.' dingding_robo(phone_number=phone_number, types=2, info=info) except Exception, e: print e result = [{'TicketTasks': 'The order submitted to failed!'}] logs(user=request.user, ip=request.META['REMOTE_ADDR'], action='add ticket (%s)' % title, result='failed')
@login_required @deny_resubmit(page_key='memcache') def flushMemcached(request): data = request.POST.getlist('mcName') project = 'memcache flush' username = request.user ip = request.META['REMOTE_ADDR'] saltCmd = LocalClient() result = [] for name in data: for info in memcache.objects.filter(memcacheName=name): try: cmd = saltCmd.cmd( info.saltMinion.saltname, 'cmd.run', ['echo "flush_all" | nc %s %s' % (info.ip, info.port)]) result.append(cmd) if cmd[info.saltMinion.saltname] == 'OK': msg = 'Success' else: msg = 'error' host = info.ip + ":" + info.port dingding_robo(host, project, msg, username, request.POST['phone_number']) print result except Exception, e: print e logs(username, ip, project, result) return render(request, 'getdata.html', {'result': result})
def add_job(request): user = request.user ip = request.META['REMOTE_ADDR'] env = request.POST['env'] queue = request.POST['queue_name'] # timeout_ms = request.POST['timeout_ms'] # replicate = request.POST['replicate'] # retry_sec = request.POST['retry_sec'] # delay_sec = request.POST['delay_sec'] # ttl_sec = request.POST['ttl_sec'] jobs = request.POST.getlist('jobs', []) # print user, env, queue, timeout_ms, replicate, retry_sec, delay_sec, ttl_sec print user, env, queue print jobs if not user.groups.filter(name__in=['admin', 'dba', 'disque']).exists(): logs(user, ip, 'add job: %s - %s' % (env, queue), 'permission denied') return HttpResponse(json.dumps({'errcode': 403}), content_type=default_content_type) if not (env in clientEnvMap.keys()): logs(user, ip, 'add job: %s - %s' % (env, queue), 'unknown disque zone:%s' % env) return HttpResponse(json.dumps({ 'errcode': 400, 'msg': 'unknown disque zone:%s' % env }), content_type=default_content_type) if (not queue) or len(queue) == 0: logs(user, ip, 'add job: %s - %s' % (env, queue), 'empty queue name') return HttpResponse(json.dumps({ 'errcode': 400, 'msg': 'emtpy queue name' }), content_type=default_content_type) if len(jobs) == 0: logs(user, ip, 'add job: %s - %s' % (env, queue), 'empty jobs') return HttpResponse(json.dumps({ 'errcode': 400, 'msg': 'empty jobs' }), content_type=default_content_type) jobs = map(lambda x: x.encode('utf-8'), jobs) client = clientEnvMap[env] jobIds = [] errJob = [] for job in jobs: try: print job jobId = client.add_job(queue, job) # jobId = client.add_job(queue, job, timeout=timeout_ms, replicate=replicate, delay=delay_sec, retry=retry_sec, ttl=ttl_sec) jobIds.append(jobId) except Exception as e: print e errJob.append(job) logs(user, ip, 'add job: %s - %s' % (env, queue), 'success') return HttpResponse(json.dumps({ 'errcode': 200, 'jobIds': jobIds, 'failJobs': errJob }), content_type=default_content_type)
def handle_tickets(request): task_id = request.POST['id'] submit = request.POST['submit'] reply = request.POST['reply'] operating_id = TicketTasks.objects.get(tasks_id=task_id) content = TicketTasks.objects.get(tasks_id=task_id).content content = json.loads(content) username = User.objects.get(username=content['handler']) phone_number = UserProfile.objects.get(user=username).phone_number handle_result = 0 if submit == 'reject': TicketTasks.objects.filter(tasks_id=task_id).update(state='4') TicketOperating.objects.create(operating_id=operating_id,handler=username,content=reply,result='2',submitter=content['owner']) logs(user=request.user,ip=request.META['REMOTE_ADDR'],action='handle ticket (%s)' % content['title'],result='successful') info = 'Your "%s" order be reject,please visit to workflow page.' % content['title'] owner = User.objects.get(username=content['owner']) owner_phone_number = UserProfile.objects.get(user=owner).phone_number dingding_robo(phone_number=owner_phone_number,types=2,info=info) result = [{'HandleTasks':'The task_id handle to success!'}] return render(request,'getdata.html',{'result':result}) if content['ticket_type'] == 'go': #'----------------------------ticket_type--------------------' for host in content['hosts']: data = { 'client':'local', 'tgt':host, 'fun':'state.sls', 'arg':['goservices.supervisor_submodule','pillar={"goprograme":"%s","svnrepo":"%s","supProgrameName":"%s","goRunCommand":"%s"}' % (content['project'],content['svn_repo'],content['supervisor_name'],content['go_command'])] } result = salt_api.salt_cmd(data) try: minion_host = minion.objects.get(saltname=host) supervisor_info = gostatus.objects.get(hostname=minion_host) supervisor_obj = xmlrpclib.Server('http://%s:%s@%s:%s/RPC2' % ( supervisor_info.supervisor_username, supervisor_info.supervisor_password, supervisor_info.supervisor_host, supervisor_info.supervisor_port)) if supervisor_obj.supervisor.getProcessInfo(content['supervisor_name']): deploy_result = 1 print '-------successful-----' except Exception, e: print e deploy_result = 0 handle_result = 1 TicketTasks.objects.filter(tasks_id=task_id).update(state='5') TicketOperating.objects.create(operating_id=operating_id,handler=username,content=reply,result='3',submitter=content['owner']) logs(user=request.user,ip=request.META['REMOTE_ADDR'],action='handle ticket (%s)' % content['title'],result='failed') info = 'The "%s" order is failed,please check in %s host.' % (content['title'],host) dingding_robo(phone_number=phone_number,types=2,info=info) result = [{'HandleTasks':'The task_id handle to failed!'}] print '------failed-------------' return render(request,'getdata.html',{'result':result}) #-------------------------new project----------------------------------- try: if deploy_result == 1: if gogroup.objects.filter(name=content['project']): print 'The %s project is existing!!' % content['project'] else: obj = gogroup(name=content['project']) obj.save() project = gogroup.objects.get(name=content['project']) obj = svn(username=svn_username, password=svn_password, repo=content['svn_repo'], localpath=go_local_path + content['project'], movepath=go_move_path + content['project'], revertpath=go_revert_path, executefile=go_local_path + content['project'] + '/' + content['project'] ,project=project) obj.save() #-------------------------gotemplate----------------------------------- project = gogroup.objects.get(name=content['project']) ip = minion_host.ip if GOTemplate.objects.filter(hostname=minion_host).filter(project=project).filter(env=1): print 'The %s gotemplate project is existing!!' % content['project'] else: obj = GOTemplate( username=svn_username, password=svn_password, repo=svn_gotemplate_repo, localpath=svn_gotemplate_local_path, env=1, hostname=minion_host, project=project) obj.save() #-------------------------goservices----------------------------------- if goservices.objects.filter(saltminion=minion_host).filter(group=project).filter(name=content['supervisor_name']).filter(env=1): print 'The %s goservice is existing!!' % content['supervisor_name'] else: obj = goservices( ip=ip, name=content['supervisor_name'] , env=1, group=project, saltminion=minion_host, owner=content['owner'], has_statsd=content['statsd'], has_sentry=content['sentry'], comment=content['function']) obj.save() except Exception, e: print e handle_result = 1 TicketTasks.objects.filter(tasks_id=task_id).update(state='5') TicketOperating.objects.create(operating_id=operating_id,handler=username,content=reply,result='3',submitter=content['owner']) logs(user=request.user,ip=request.META['REMOTE_ADDR'],action='handle ticket (%s)' % content['title'],result='failed') result = [{'HandleTasks':'The task_id handle to failed!'}]
def submit_tickets(request): tasks_info = '' title = request.POST['title'] ticket_type = request.POST['ticket_type'] handler = request.POST['handler'] owner = str(request.user) if ticket_type == 'go': function = request.POST['function'] hosts = request.POST.getlist('hosts') project = request.POST['project'] project = project.strip() go_command = request.POST['go_command'] supervisor_name = go_command.replace(" ", "_") svn_repo = svn_repo_url + project statsd = request.POST['statsd'] sentry = request.POST['sentry'] go_command = go_command + " -c /srv/gotemplate/%s/conf.ctmpl" % project salt_command = { "title":title, "ticket_type":ticket_type, "function":function, "hosts":hosts, "project":project, "svn_repo":svn_repo, "supervisor_name":supervisor_name, "go_command":go_command, "statsd":statsd, "sentry":sentry, "handler":handler, "owner":owner } ## create svn repo address if not gogroup.objects.filter(name=project): data = { 'client':'local', 'tgt': svn_host, 'fun':'cmd.script', 'arg':['salt://scripts/create_svn.sh',project] } salt_result = salt_api.salt_cmd(data) if salt_result['return'][0][svn_host]['stdout'] == 'ok': tasks_info = 'Your project svn_repo is %s.\n\n' % (svn_repo) else: tasks_info = 'Error creating svn repo(%s),please tell ops..\n\n' % (svn_repo) else: tasks_info = 'Your project svn_repo is %s.\n\n' % (svn_repo) elif ticket_type == 'webpage': print '--------site_name-----:',request.POST.getlist('site_name') site_name = request.POST.getlist('site_name') salt_command = { "title":title, "ticket_type":ticket_type, "site_name":site_name, "handler":handler, "owner":owner } elif ticket_type == 'uat_jenkins': print request.POST jenkins_name = request.POST['jenkins_name'] uat_env = request.POST['uat_env'] salt_command = { "title":title, "ticket_type":ticket_type, "uat_env": uat_env, "jenkins_name":jenkins_name, "handler":handler, "owner":owner } try: salt_command = json.dumps(salt_command) ticket_type = TicketType.objects.get(type_name=ticket_type) handler = User.objects.get(username=handler) task_id = str(uuid.uuid1()) TicketTasks.objects.create(tasks_id=task_id,title=title,ticket_type=ticket_type,creator=request.user,content=salt_command,handler=handler,state='1') tasks_info = tasks_info + 'The %s order submitted to success!' % task_id result = [{'TicketTasks':tasks_info}] logs(user=request.user,ip=request.META['REMOTE_ADDR'],action='add ticket (%s)' % title,result='successful') user = User.objects.get(username=handler) phone_number = UserProfile.objects.get(user=user).phone_number info = 'You have a message,please visit to workflow page.' dingding_robo(phone_number=phone_number,types=2,info=info) except Exception, e: print e result = [{'TicketTasks':'The order submitted to failed!'}] logs(user=request.user,ip=request.META['REMOTE_ADDR'],action='add ticket (%s)' % title,result='failed')
def deploy(self, site, action='svn', revision='HEAD', group=None): self.action = action self.revision = revision if group: obj = groupName.objects.get(group_name=group).member.values() else: obj = [] obj.append({'webSite': site}) for s in obj: self.site = s['webSite'] info = webSite.objects.filter(env=self.env).get(webSite=self.site) ip = [] for i in info.checkUrl.values(): ip.append(i['ip']) print '--------site--------------', info.webSite self.f.write('-----------------------%s---------------------\n' % info.webSite) self.f.flush() for host in info.checkUrl.values(): for m in info.state_module.values(): nginx_backup = self.__nginx_backup(ip, host['ip'], host['host'], m['state_module'], 0) if nginx_backup == 1: self.f.write( 'Step 1: blocking nginx traffic is failed.\n') exit() self.f.write('Step 1: blocking nginx traffic is sucessful.\n') self.f.flush() if self.action in ['svn', 'revert']: svn_up = self.__svn_update(host['host'], info.svn_path, info.svn_username, info.svn_password, self.revision) if svn_up == 1: self.f.write('error') exit() else: self.f.write('Step 2: svn update is sucessful.\n') self.f.flush() elif self.action == 'recycle': pass recycle = self.__iis_recycle(host['host'], info.recycle_cmd, host['url'], self.action) if recycle == 1: self.f.write('error') exit() elif action == 'recycle': self.f.write('Step 2: iis recycle is sucessful.\n') self.f.flush() else: self.f.write('Step 3: iis recycle is sucessful.\n') self.f.flush() for m in info.state_module.values( ): ######nginx all online######## status = self.__nginx_backup(ip, host['ip'], host['host'], m['state_module'], 1) if status == 1: logs(self.username, self.ip, self.site, 'Failed') self.f.write('error') exit() self.f.write('Last step: open nginx traffic is sucessful.\n') self.f.flush() logs(self.username, self.ip, self.site, 'Successful') self.f.write('done') self.f.flush() self.f.close() return self.result