def server_collects(tgt='*', server_id=0): server_list = SaltServer.objects.all() contexts = {'server_list': server_list, 'server_id': server_id} try: try: salt_server = SaltServer.objects.get(id=server_id) except Exception as e: # id不存在时返回第一个 salt_server = SaltServer.objects.all()[0] except Exception as e: contexts.update({'error': e}) try: sapi = SaltAPI(url=salt_server.url, username=salt_server.username, password=salt_server.password) print salt_server.url grains = sapi.SaltCmd(tgt=tgt, fun='grains.items', client='local')['return'][0] minions = sapi.key_list('manage.status', client='runner')['return'][0] if salt_server and grains: for i in grains.keys(): try: server = Servers.objects.get(local_ip=i) except: server = Servers() # if sapi.SaltCmd(tgt=i,fun='test.ping',client='local')['return'][0]: if i in minions['up']: minions_status = '0' else: minions_status = '1' server.hostname = grains[i]['host'] server.local_ip = grains[i]['id'] server.OS = "%s %s" % (grains[i]['os'], grains[i]['osrelease']) server.Mem = grains[i]['mem_total'] server.Cpus = grains[i]['num_cpus'] server.Cpu_type = grains[i]['cpu_model'] server.minion_id = grains[i]['id'] server.app = grains[i]['virtual'] server.server_status = minions_status server.save() contexts.update({ 'success': u'%s 收集成功' % tgt, 'server_id': salt_server.id }) if not grains: contexts.update({'error': u'%s 主机不存在或者离线' % tgt}) except Exception as e: contexts.update({'error': '%s %s' % (tgt, e)}) return contexts
def service_fun(request, server_id): url = SaltServer.objects.values('url').filter(id=server_id)[0]['url'] username = SaltServer.objects.values('username').filter( id=server_id)[0]['username'] password = SaltServer.objects.values('password').filter( id=server_id)[0]['password'] if request.method == 'GET': active = request.GET.get('active') project_id = request.GET.get('project_id') script = SvnProject.objects.values('script').filter( id=project_id)[0]['script'] tgt = SvnProject.objects.values('host').filter( id=project_id)[0]['host'] arg = '' if active == 'stop': arg = script + ' stop' elif active == 'start': arg = script + ' start' elif active == 'status': arg = script + ' status' elif active == 'restart': arg = script + ' restart' try: sapi = SaltAPI(url=url, username=username, password=password) info = sapi.SaltCmd(tgt=tgt, fun='cmd.run', client='local', arg=arg)['return'][0][tgt] result = {'ret': True, 'msg': u'%s' % info} except Exception as e: result = {'error': str(e)} fh = files_history() fh.username = request.session.get('username') fh.active = '应用操作' fh.active_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) fh.remote_server = SvnProject.objects.filter(id=project_id)[0].host fh.path = result['msg'] fh.save() return JsonResponse(result, safe=False)
def state_exec(request, server_id): ret = state = '' server_list = SaltServer.objects.all() try: salt_server = SaltServer.objects.get(id=server_id) except: # id不存在时返回第一个 salt_server = SaltServer.objects.all() if salt_server: salt_server = salt_server[0] else: return render(request, 'saltstack/state.html', {'apiinfo': u'请先添加SaltServer API'}) sapi = SaltAPI(url=salt_server.url, username=salt_server.username, password=salt_server.password) # envs = sapi.SaltRun(client='runner', fun='fileserver.envs')['return'][0] envs = glob_config("salt_api", "envs").split(',') minions = sapi.key_list( 'key.list_all')['return'][0]['data']['return']['minions'] env = envs[0] minion_group = MinionGroup.objects.all() context = { 'minions': minions, "minion_group": minion_group, 'ret': ret, 'envs': envs, 'env': env, 'state': state, 'salt_server': salt_server, 'server_list': server_list, 'url': 'state_exec' } if request.method == 'POST': try: if request.POST.get('tgt'): minion = request.POST.get('tgt') else: minion = minions[0] if request.POST.get('env'): env = request.POST.get('env') state = request.POST.get('state') if minion and state: ret = sapi.SaltCmd(tgt=minion, fun='state.sls', client='local', arg=state, arg1='saltenv=%s' % env)['return'][0] roots = sapi.SaltRun( client='wheel', fun='file_roots.list_roots')['return'][0]['data']['return'] dirs = roots[env][0] states = [] for root, dirs in dirs.items( ): # root="/srv/salt/prod/" dirs={"init":{"epel.sls":"f",}} for dir, files in dirs.items( ): # dir='init' or 'top.sls' files={"epel.sls":"f",} if dir == '.svn': dir = '.svn' elif files == "f" and dir.endswith('.sls'): states.append(dir[0:-4]) elif isinstance(files, dict): for sls, f in files.items(): if f == 'f' and sls.endswith('.sls'): states.append('%s.%s' % (dir, sls[0:-4])) result = sorted(states) if result and not state: state = result[0] context.update({ 'minion': minion, 'ret': ret, 'states': result, 'env': env, 'state': state }) except Exception as e: context.update({'error': e}) fh.username = request.session.get('username') fh.active = u'state 编排' fh.active_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) fh.remote_server = '' fh.path = state fh.save() return render(request, 'saltstack/state.html', context)
def cmd_exec(request, server_id): server_list = SaltServer.objects.all() try: salt_server = SaltServer.objects.get(id=server_id) except: # id不存在时返回第一个 salt_server = SaltServer.objects.all() if salt_server: salt_server = salt_server[0] else: return render(request, 'saltstack/command.html', {'apiinfo': u'请先添加SaltServer API'}) sapi = SaltAPI(url=salt_server.url, username=salt_server.username, password=salt_server.password) minion_group = MinionGroup.objects.all() minions = sapi.key_list( 'key.list_all')['return'][0]['data']['return']['minions'] # minions = MinionGroup.objects.minions.all() tgt = info = cmd_exec_result = minion_list = '' context = { 'salt_server': salt_server, 'server_list': server_list, 'minions': minions, 'url': 'cmd_exec' } exec_module = "cmd.run" if request.method == 'POST': try: ip_list = request.POST.get('minion') cmd_args = request.POST.get('arg') group = request.POST.get('group') if group: minions = MinionGroup.objects.get( groupname=group).minions.all() else: minions = MinionGroup.objects.get( groupname="All").minions.all() if not cmd_args: info = '请输入执行命令!' elif ip_list: tgt = ip_list cmd_exec_result = sapi.SaltCmd(tgt, exec_module, client='local', arg=cmd_args, expr_form='list')['return'][0] else: for i in minions.values('minion'): minion_list += i['minion'] + ',' tgt = minion_list.strip(',') cmd_exec_result = sapi.SaltCmd(tgt, exec_module, client='local', arg=cmd_args, expr_form='list')['return'][0] context.update({ 'cmd_exec_result': cmd_exec_result, 'info': info, 'arg': cmd_args, 'tgt': tgt, 'group': group, 'minions': minions }) except Exception as e: if request.POST.get('minion'): tgt = request.POST.get('minion') if request.POST.get('minions'): tgt = request.POST.get('minions') cmd_exec_result = e context.update({'error': e}) ##添加审计记录 fh.username = request.session.get('username') fh.active = 'exec command' fh.active_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) fh.remote_server = tgt fh.path = cmd_exec_result fh.save() context.update({'minion_group': minion_group}) return render(request, 'saltstack/command.html', context)
def saltinfo(tgt): sapi = SaltAPI(url, username, password) grains = sapi.SaltCmd(tgt=tgt, fun='grains.items', client='local')['return'][0] disk = sapi.SaltCmd(tgt=tgt, fun='disk.usage', client='local')['return'][0] info = sapi.SaltCmd(tgt=tgt, fun='cmd.run', client='local', arg='uptime')['return'][0] hostname = grains[tgt]['host'] ip = grains[tgt]['ip4_interfaces'][device0][0] OS = grains[tgt]['os'] + ' ' + grains[tgt]['osrelease'] + '.' + grains[ tgt]['osarch'] disk_all = float(disk[tgt]['/']['1K-blocks']) / 1000 / 1000 disk_free = float(disk[tgt]['/']['available']) / 1000 / 1000 disk_used = float(disk[tgt]['/']['used']) / 1000 / 1000 disk_used_p = disk[tgt]['/']['capacity'] Manufacturer = grains[tgt]['manufacturer'] saltstack_version = grains[tgt]['saltversion'] mem_total = grains[tgt]['mem_total'] cpu_logical_cores = grains[tgt]['num_cpus'] cpu_model = grains[tgt]['cpu_model'] uptime = info[tgt].split(',')[0] loadavg = info[tgt].split(',')[-3:] loadavg_1 = loadavg[0].strip(' load average:') loadavg_5 = loadavg[1] loadavg_15 = loadavg[2] login_user_num = sapi.SaltCmd(tgt=tgt, fun='cmd.run', client='local', arg='who |wc -l')['return'][0][tgt] processs = sapi.SaltCmd(tgt=tgt, fun='cmd.run', client='local', arg="ps -A -ostat")['return'][0][tgt] S = R = Z = 0 for process in processs: if 'S' in process: S += 1 elif 'R' in process: R += 1 elif 'Z' in process: Z += 1 LocalData = { 'uptime': uptime, 'ip': ip, 'hostname': hostname, 'os': OS, 'disk_all': disk_all, 'disk_free': disk_free, 'disk_used': disk_used, 'disk_used_p': disk_used_p, 'loadavg_1': loadavg_1, 'loadavg_5': loadavg_5, 'loadavg_15': loadavg_15, 'salt_version': saltstack_version, 'mem_total': mem_total, 'cpu_physical_num': cpu_model, 'cpu_logical_cores': cpu_logical_cores, 'process_num': S + R + Z, 'process_S': S, 'process_R': R, 'process_Z': Z, 'login_user_num': login_user_num, 'Manufacturer': Manufacturer, } return LocalData
def checkout(request, server_id): server_list = SaltServer.objects.all() try: salt_server = SaltServer.objects.get(id=server_id) except: # id不存在时返回第一个 salt_server = SaltServer.objects.all()[0] try: project_id = request.GET.get('project_id') except: project_id = '' project_list = SvnProject.objects.filter( salt_server=salt_server).order_by('host') contexts = { 'server_list': server_list, 'salt_server': salt_server, 'project_list': project_list } if project_list and project_id: try: sapi = SaltAPI(url=salt_server.url, username=salt_server.username, password=salt_server.password) result = sapi.SaltRun(client='runner', fun='manage.status') contexts['minions_up'] = result['return'][0]['up'] #刷新页面检测并更新项目状态 project = SvnProject.objects.filter(id=project_id)[0] path = project.path + '/' + project.target svn_info = sapi.SaltCmd( client='local', tgt=project.host, fun='svn.info', arg=path, arg1='fmt=dict')['return'][0][project.host][0] if isinstance(svn_info, dict): if project.url == svn_info['URL']: project.status = u"已发布" project.info = u"最近修改时间:%s\n最近修改版本:%s\n最新版本:%s" % ( svn_info["Last Changed Date"][0:20], svn_info["Last Changed Rev"], svn_info["Revision"]) else: project.status = u"冲突" project.info = u"SVN路径不匹配:\n本地SVN为'%s'\n项目SVN为'%s'" % ( svn_info['URL'], project.url) else: #根路径不存在时创建 if not sapi.SaltCmd( client='local', tgt=project.host, fun='file.directory_exists', arg=project.path)['return'][0][project.host]: sapi.SaltCmd(client='local', tgt=project.host, fun='file.mkdir', arg=project.path) #目录未被版本控制,可能SVN未安装 if not sapi.SaltCmd( client='local', tgt=project.host, fun='pkg.version', arg='subversion')['return'][0][project.host]: sapi.SaltCmd(client='local', tgt=project.host, fun='pkg.install', arg='subversion') #签出项目、获取信息并存入库 sapi.SaltCmd(client='local', tgt=project.host, fun='svn.checkout', arg=project.path, arg0='target=%s' % project.target, arg1='remote=%s' % project.url, arg2='username=%s' % project.username, arg3='password=%s' % project.password) #签出项目后更新项目状态 svn_info = sapi.SaltCmd( client='local', tgt=project.host, fun='svn.info', arg=path, arg1='fmt=dict')['return'][0][project.host][0] project.status = u"已发布" project.info = u"最近修改时间:%s\n最近修改版本:%s\n最新版本:%s" % ( svn_info["Last Changed Date"][0:20], svn_info["Last Changed Rev"], svn_info["Revision"]) project.save() result = {'ret': True, 'msg': u'检出成功'} except Exception as error: result = {'ret': False, 'msg': u'错误:项目目录冲突'} fh = files_history() fh.username = request.session.get('username') fh.active = 'svn checkout' fh.active_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) fh.remote_server = SvnProject.objects.filter(id=project_id)[0].host fh.path = result['msg'] fh.save() return JsonResponse(result, safe=False)
def deploy_fun(request, server_id): #SVN功能按钮 if request.is_ajax() and request.method == 'GET': tgt = request.GET.get('tgt', '') path = request.GET.get('path', '').encode("utf-8") active = request.GET.get('active', '') project_id = request.GET.get('project_id', '') try: salt_server = SaltServer.objects.get(id=server_id) sapi = SaltAPI(url=salt_server.url, username=salt_server.username, password=salt_server.password) if project_id: project = SvnProject.objects.get(id=project_id) #指定项目 path = project.path + '/' + project.target tgt = project.host else: project = None #项目不存在 projects = SvnProject.objects.filter(host=tgt) for p in projects: if path.startswith(p.path + '/' + p.target): #项目子目录 project = p #SvnProject里没有记录时自动创建,但密码需要在后台设置 if not project: svn_info = sapi.SaltCmd(client='local', tgt=tgt, fun='svn.info', arg=path, arg1='fmt=dict')['return'][0][tgt][0] if isinstance(svn_info, dict): result = { 'ret': False, 'msg': u'SVN项目不存在,请在后台页面添加!', 'add': True } else: result = {'ret': False, 'msg': u'错误:%s' % svn_info} #提交 elif active == 'commit' or active == 'update': status = sapi.SaltCmd(client='local', tgt=tgt, fun='svn.status', arg=path)['return'][0][tgt] for s in status.split('\n'): l = s.split(' ') if l[0] == '?': sapi.SaltCmd(client='local', tgt=tgt, fun='svn.add', arg=path, arg0=path + '/' + l[-1], arg2='username=%s' % project.username, arg3='password=%s' % project.password) elif l[0] == '!': sapi.SaltCmd(client='local', tgt=tgt, fun='svn.remove', arg=path, arg0=path + '/' + l[-1], arg2='username=%s' % project.username, arg3='password=%s' % project.password) ci = sapi.SaltCmd(client='local', tgt=tgt, fun='svn.commit', arg=path, arg1='msg=commit from %s' % tgt, arg2='username=%s' % project.username, arg3='password=%s' % project.password)['return'][0][tgt] result = {'ret': True, 'msg': u"提交成功!\n%s" % ci} #更新(先提交否则会冲突) if active == 'update': up = sapi.SaltCmd(client='local', tgt=tgt, fun='svn.update', arg=path, arg2='username=%s' % project.username, arg3='password=%s' % project.password)['return'][0][tgt] result = { 'ret': True, 'msg': u"提交成功!\n%s\n更新成功!\n%s" % (ci, up) } ##更新完成后更新项目状态 svn_info = sapi.SaltCmd( client='local', tgt=project.host, fun='svn.info', arg=path, arg1='fmt=dict')['return'][0][project.host][0] if isinstance(svn_info, dict): if project.url == svn_info['URL']: project.status = u"已发布" project.info = u"最近修改时间:%s\n最近修改版本:%s\n最新版本:%s" % ( svn_info["Last Changed Date"][0:20], svn_info["Last Changed Rev"], svn_info["Revision"]) else: project.status = u"冲突" project.info = u"SVN路径不匹配:\n本地SVN为'%s'\n项目SVN为'%s'" % ( svn_info['URL'], project.url) project.save() except Exception as e: result = {'ret': False, 'msg': u'错误:%s' % e} fh = files_history() fh.username = request.session.get('username') fh.active = 'svn update' fh.active_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) fh.remote_server = SvnProject.objects.filter(id=project_id)[0].host fh.path = result['msg'] fh.save() return JsonResponse(result, safe=False)