def web_front_domain(self, domains, remoteips, classify, isphone=None): server_names = domains.replace('\r\n', " ") if classify == "front": remote_dir = "/usr/local/nginx/conf/vhost/" remote_nginx_file = self.siteid + ".conf" siteid = self.siteid.replace('f', '') if "f" in self.siteid: if isphone: local_nginx_file = "front_fu.conf" else: local_nginx_file = "front_fu_phone.conf" else: if isphone == True: local_nginx_file = "front_zhu.conf" else: local_nginx_file = "front_zhu_phone.conf" print local_nginx_file resource = gen_resource([ Server.objects.get(ssh_host=i) for i in remoteips.split('\r\n') ]) playtask = MyPlayTask(resource) playtask.rsync_nginx_conf(local_nginx_file, remote_dir, remote_nginx_file, siteid, server_names) elif classify == "agent": remote_dir = "/usr/local/nginx/conf/vhost/" if self.env == "huidu": remote_nginx_file = self.siteid + "_huidu.conf" local_nginx_file = "agent_huidu.conf" else: remote_nginx_file = self.siteid + ".conf" local_nginx_file = "agent.conf" #resource = gen_resource(Server.objects.get(ssh_host='10.10.240.20')) resource = gen_resource( Server.objects.get(ssh_host='47.89.30.192')) playtask = MyPlayTask(resource) playtask.rsync_nginx_conf(local_nginx_file, remote_dir, remote_nginx_file, self.siteid, server_names) else: remote_dir = "/usr/local/nginx/conf/vhost/" remote_nginx_file = self.siteid + ".conf" siteid = self.siteid.replace('f', '') local_nginx_file = "backend.conf"
def pings(host): resource = gen_resource(host) ansible_instance = MyTask(resource) res = {} print ansible_instance.check_vm() if ansible_instance.check_vm()['hosts'][host.ssh_host].has_key('ping'): res = {host.ssh_host: "pong"} Asset.objects.filter(pk=host.asset.uuid).update(mark=True) else: res[host.ssh_host] = "fail" Asset.objects.filter(pk=host.asset.uuid).update(mark=False) return res
def change_backend_second(host_ip, include_name, status): resource = gen_resource(Server.objects.get(ssh_host=host_ip)) if status: module_args = 'dest="/usr/local/nginx/conf/nginx.conf" regexp="#include vhost/%s.conf" line=" include vhost/%s.conf;"' % ( include_name, include_name) else: module_args = 'dest="/usr/local/nginx/conf/nginx.conf" regexp="include vhost/%s.conf" line=" #include vhost/%s.conf;"' % ( include_name, include_name) print(module_args) shell_args = 'service nginx reload' mytask = MyRunner(resource) res = mytask.run('lineinfile', module_args) res = mytask.run('shell', shell_args) return res
def nginx_white_copy(rss, tfn, fp, wl, isreload, **kw): """tfn is mean template file name, rss is mean remote servers, fp is mean file path and file name, wl is mean nginx white list, isreload is mean nginx service reload """ status = False servers = [] for i in rss.split('\r\n'): print i data = Server.objects.filter(ssh_host=i) if len(data) == 0: print "CMDB中没有此服务器信息:%s,已跳过!" % i continue elif len(data) > 1: print "CMDB中有多条服务器信息:%s,已跳过!" % i continue else: if not ssh_check(i): print "服务器%s不可用,已跳过!" % i continue else: servers.append(Server.objects.get(ssh_host=i)) if not servers: return "没有可用的服务器,任务结束" if isreload: status = True playtask = MyPlayTask(gen_resource(servers)) if "server_name" in kw: print "执行蛮牛后台白名单" server_name = kw["server_name"] siteid = kw["siteid"] res = playtask.rsync_nginx_white_conf(tfn, fp, wl, status, server_name, siteid) else: res = playtask.rsync_nginx_white_conf(tfn, fp, wl, status) if res == 0: print "结果:成功!" elif res == 1: print "结果:执行错误!" else: print "结果:主机不可用!" return res
def system_init(request): pubkey_data = publickey.objects.all() zabbix_data = zabbixagent.objects.all() if request.method == 'POST': check_box_list = request.POST.getlist('check_box_list') server_list = [ x for x in request.POST.get('servers').split('\r\n') if x ] obj_list = [ Server.objects.get(ssh_host=host) for host in server_list if host ] resource = gen_resource(obj_list) ansible_instance = MyTask(resource) ansible_playtask = MyPlayTask(resource) if "pubkey" in check_box_list: pubkey_uuid = request.POST.get('public_key') pubkey_use_obj = publickey.objects.get(pk=pubkey_uuid) user = '******' key = pubkey_use_obj.pubkey ansible_auth = ansible_instance.push_key(user, key) if "selinux" in check_box_list: selinux_state = request.POST.get('selinux_state', '') ansible_selinux = ansible_instance.set_selinux(selinux_state) if "install_zabbix_agent" in check_box_list: zabbix_uuid = request.POST.get('zabbix_agent', '') zabbix_obj = zabbixagent.objects.get(pk=zabbix_uuid) version = zabbix_obj.version listenip = zabbix_obj.listenip listenport = zabbix_obj.listenport server = zabbix_obj.server serveractive = zabbix_obj.serveractive zabbix_agent_install = ansible_playtask.install_zabbix_agent( version, server, listenport, listenip, serveractive) return HttpResponseRedirect('/allow/welcome/') return render(request, 'assets/system_init.html', locals())
def asset_ansible_update(obj_list, asset_type): resource = gen_resource(obj_list) ansible_instance = MyRunner(resource) ansible_asset_info = ansible_instance.run(module_name='setup', module_args='filter=*') for asset in obj_list: try: setup_info = ansible_asset_info["hosts"][ asset.ssh_host]['ansible_facts'] except KeyError, e: continue else: try: asset_info = get_ansible_asset_info(asset.ssh_host, setup_info) print asset_info hostname, memory, disk, cpu_type, cpu_counts, cpu_cores, asset_type, system_type, system_version, system_kernel, nic = asset_info cpu_dic = { "cpu_model": cpu_type, "cpu_count": cpu_counts, "cpu_core_count": cpu_cores } nic_list = [] for k, v in nic.iteritems(): k = {} k["name"] = v[0] k["macaddress"] = v[1] k["ipaddress"] = v[2] k["netmask"] = v[-1] nic_list.append(k) asset_dic = { "name": hostname, "RAM_total": memory, "Disk_total": disk, "os_type": asset_type, "os_version": system_type, "os_release": system_version, "os_kernel": system_kernel, } # print asset_dic print nic_list # print cpu_dic cpu_data = asset.asset.cpu try: nic_obj_all = asset.asset.nic_set.all() for i in nic_obj_all: for j in nic_list: if j['name'] == i.name: ansible_record_nic(i, j) except: dddfff = [x.macaddress for x in asset.asset.wangka.all()] for i in nic_list: if i['macaddress'] not in dddfff: aa = NIC(asset=asset.asset, name=i['name'], macaddress=i['macaddress'], ipaddress=i['ipaddress'], netmask=i['netmask'], memo=u"内网") aa.save() ansible_record_cpu(cpu_data, cpu_dic) ansible_record(asset, asset_dic) except Exception as e: traceback.print_exc()