def _online_go(name, host): svc = goservices.objects.filter(name=name).first() nvs = svn.objects.get(project=svc.group) if host.find(salt_location) > 0: print '-----salt_location:', salt_location salt_run = SaltApi(salt_location) else: salt_run = SaltApi() data = { 'client': 'local_async', 'fun': 'state.sls', 'tgt': host, 'arg': [ 'goservices.supervisor_submodule', 'pillar={' '"goprograme":"%s",' '"svnrepo":"%s",' '"supProgrameName":"%s",' '"goRunCommand":"%s -c /srv/gotemplate/%s/conf.ctmpl"}' % (nvs.project, nvs.repo, name, ' '.join( name.split('_')), nvs.project) ] } result = salt_run.salt_cmd(data) jid = result['return'][0]['jid'] print '------jid------:', jid jid_data = {'client': 'runner', 'fun': 'jobs.exit_success', 'jid': jid} while 1: jid_result = salt_run.salt_cmd(jid_data) print '----jid_result----', jid_result['return'][0][host] if jid_result['return'][0][host]: break else: time.sleep(10) print '---jid_result-----', jid_result return result
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 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 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})
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!'}] 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.'}]
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']: try: if host.find(salt_location) > 0: print '-----salt_location:', salt_location salt_run = SaltApi(salt_location) else: salt_run = SaltApi() data = { 'client': 'local_async', '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_run.salt_cmd(data) jid = result['return'][0]['jid'] print '------jid------:', jid jid_data = { 'client': 'runner', 'fun': 'jobs.exit_success', 'jid': jid } tag = True while tag: jid_result = salt_run.salt_cmd(jid_data) print '----jid_result----', jid_result['return'][0][host] if jid_result['return'][0][host]: tag = False else: time.sleep(10) print '---jid_result-----', jid_result 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 + content['project'], localpath=svn_gotemplate_local_path + content['project'], 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!'}]