def sid_update(request): if request.method == 'POST': mextra_vars = {} sextra_vars = {} appname = request.POST['Up_AppName'] lvsize = request.POST['Up_LvSize'] buffer_pool = request.POST['Up_Bf_Pool'] sidinfo_set = SidInfo.objects.filter(appname=appname) for sidinfo in sidinfo_set: host = HostInfo.objects.get(ip=sidinfo.hostip) host.idle = False host.save() if 'M' in sidinfo.sidname.replace(sidinfo.appname, ""): mextra_vars = extra_var_create(host, mextra_vars, "mysql_M_instance", sidinfo, buffer_pool, lvsize) else: sextra_vars = extra_var_create(host, sextra_vars, "mysql_S_instance", sidinfo, buffer_pool, lvsize) startime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') username = request.session.get("username", None) taskobj = sid_update_task.apply_async([mextra_vars, sextra_vars]) user_task = User_Task(username_id=username, star_time=startime, taskid=taskobj.id, hosts=mextra_vars["hosts"] + ":" + sextra_vars["hosts"], taskname="sid_update") user_task.save() return redirect('/hd_mesos/tasklist.html')
def software_installed(request): if request.method == 'POST': hosts = request.POST.getlist('host_assets') groupids = request.POST.getlist('grp_assets') if len(hosts)+len(groupids) > 0: softname = Software.objects.get(id = request.POST["Softname"]).softname for i in groupids: for host_group in Host_Group.objects.filter(group_id=i): hosts.append(host_group.ip_id) hosts = list(set(hosts)) flag = True filterlist = [] for hostip in hosts: hostinfo = HostInfo.objects.get(ip=hostip) if softname in hostinfo.software:#判断主机是否安装此软件 pass else: filterlist.append(hostip) if not hostinfo.idle: flag = False break if len(filterlist) > 0: if flag: for hostip in filterlist: hostinfo = HostInfo.objects.get(ip=hostip) hostinfo.idle = False hostinfo.save() hoststr = ":".join(filterlist) extra_vars = {} extra_vars["hosts"] = hoststr extra_vars["role"] = softname extra_vars["version"] = request.POST["SoftVersion"] extra_vars["flag_fact"] = "false" extra_vars["option"] = "install.yml" taskname = softname+"_install" username = request.session.get("username",None) startime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') softinfo = softname+"-"+request.POST["SoftVersion"] try: taskobj = install.apply_async([extra_vars,softinfo]) except Exception: for hostip in filterlist: hostinfo = HostInfo.objects.get(ip=hostip) hostinfo.idle = True hostinfo.save() user_task = User_Task(username_id = username,star_time= startime,taskid=taskobj.id,hosts=hoststr,taskname=taskname) user_task.save() return redirect('/hd_mesos/tasklist.html') else: return render(request,"mesos/hd_ansible/software/install.html",{"error":"目标主机有任务进行,请稍等"}) else: return render(request,"mesos/hd_ansible/software/install.html",{"error":"所选主机已全部安装此软件"}) else: return render(request,"mesos/hd_ansible/software/install.html",{"error":"请至少选择一个目标主机或主机组"})
def mysql_dump(request): if request.method == 'GET': return render(request, "mesos/hd_mysql/backup_mge/mysql-dump.html") elif request.method == 'POST': extra_vars = mysql_extra_vars(request) extra_vars["login_host"] = request.POST['login_host'] startime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') username = request.session.get("username", None) taskobj = db_dump.apply_async([extra_vars]) user_task = User_Task(username_id=username, star_time=startime, taskid=taskobj.id, hosts=extra_vars["hosts"], taskname="mysql_dump") user_task.save() return redirect('/hd_mesos/tasklist.html')
def roles_api(request, taskid=""): if request.method == 'POST': extra_vars = json.loads(request.body) playbook_path = "/opt/ansible/execute.yml" startime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') username = request.session.get("username", None) taskobj = com_playbook.apply_async([extra_vars, playbook_path]) user_task = User_Task(username_id=username, star_time=startime, taskid=taskobj.id, hosts=extra_vars["hosts"], taskname=extra_vars["roles"]) user_task.save() return HttpResponse(taskobj.id) if request.method == 'GET': try: task_result = TaskResult.objects.get(task_id=taskid) return HttpResponse(json.dumps(task_result.result)) except TaskResult.DoesNotExist: return HttpResponse("Pendding")
def script_exec(request, **kargs): if request.method == 'GET': grouplist = HostGroup.objects.all() return render(request, "mesos/hd_ansible/script/scriptexec.html", { "grouplist": grouplist, "scriptname": kargs["scriptname"] }) if request.method == 'POST': hosts = request.POST.getlist('host_assets') if 'all' in hosts: hosts.remove("all") hoststr = ":".join(hosts) extra_vars = {} extra_vars["hosts"] = hoststr script_name = request.POST["Name"] scriptinfo = ScriptInfo.objects.get(name=script_name) if scriptinfo.type == 1: extra_vars["script_file"] = scriptinfo.name + ".sh" extra_vars["script_path"] = os.path.join(SHELL_PATH, extra_vars["script_file"]) playbook_path = "/opt/ansible/script/execute.yml" elif scriptinfo.type == 2: playbook_path = os.path.join(PLAYBOOK_PATH, scriptinfo.name + ".yml") startime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') username = request.session.get("username", None) taskobj = com_playbook.apply_async([extra_vars, playbook_path]) user_task = User_Task(username_id=username, star_time=startime, taskid=taskobj.id, hosts=hoststr, taskname=script_name) user_task.save() if request.session.get('authmethod', None): return HttpResponse(taskobj.id) else: return redirect('/hd_mesos/tasklist/others')
def sid_add(request): if request.method == 'POST': mextra_vars = {} sextra_vars = {} for i in HostInfo.objects.get( ip=request.POST["Mhostip"]).software.split(","): #获取主库mysql版本 if "mysql" in i: master_mysql_version = i for j in HostInfo.objects.get( ip=request.POST["Shostip"]).software.split(","): #获取从库mysql版本 if "mysql" in j: slave_mysql_version = j mhost = HostInfo.objects.get(ip=request.POST["Mhostip"]) shost = HostInfo.objects.get(ip=request.POST["Shostip"]) if master_mysql_version == slave_mysql_version: #比较版本信息 if "mysql-5.6" in master_mysql_version: initshell = "/usr/local/mysql/scripts/mysql_install_db" elif "mysql-5.7" in master_mysql_version: initshell = "/usr/local/mysql/bin/mysqld --initialize-insecure" mport = Hostip_Port.objects.get( hostip=request.POST["Mhostip"]).port sport = Hostip_Port.objects.get( hostip=request.POST["Shostip"]).port mhostip_end = request.POST["Mhostip"].split(".")[-1].zfill(3) shostip_end = request.POST["Shostip"].split(".")[-1].zfill(3) mextra_vars["sid"] = request.POST[ "AppName"] + "_M_" + mhostip_end + str(mport) mextra_vars["hosts"] = request.POST["Mhostip"] mextra_vars["flag_fact"] = True mextra_vars["role"] = "mysql_M_instance" mextra_vars["option"] = "install.yml" mextra_vars["port"] = mport mextra_vars[ "sid_init"] = initshell + " --datadir=/mysql_data/" + mextra_vars[ "sid"] + "/data" mextra_vars["serverid"] = request.POST["Mhostip"].split( ".")[-1] + str(mport) mextra_vars["password"] = make_password(time.time())[-20:] mextra_vars["repluser"] = "******" + request.POST["Shostip"] mextra_vars["replpassword"] = make_password(time.time())[-20:] mextra_vars["data_size"] = request.POST["LvSize"] mextra_vars["buffer_pool"] = request.POST["Bf_Pool"] sextra_vars["sid"] = request.POST[ "AppName"] + "_S_" + shostip_end + str(sport) sextra_vars["hosts"] = request.POST["Shostip"] sextra_vars["flag_fact"] = True sextra_vars["role"] = "mysql_S_instance" sextra_vars["option"] = "install.yml" sextra_vars["port"] = sport sextra_vars[ "sid_init"] = initshell + " --datadir=/mysql_data/" + sextra_vars[ "sid"] + "/data" sextra_vars["serverid"] = request.POST["Shostip"].split( ".")[-1] + str(sport) sextra_vars["password"] = mextra_vars["password"] sextra_vars["replpassword"] = mextra_vars["replpassword"] sextra_vars["masterhost"] = mextra_vars["hosts"] sextra_vars["masterport"] = mextra_vars["port"] sextra_vars["data_size"] = request.POST["LvSize"] sextra_vars["buffer_pool"] = request.POST["Bf_Pool"] startime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') username = request.session.get("username", None) taskobj = sid_init.apply_async([mextra_vars, sextra_vars]) mhost.idle = False shost.idle = False mhost.save() shost.save() user_task = User_Task(username_id=username, star_time=startime, taskid=taskobj.id, hosts=request.POST["Mhostip"] + ":" + request.POST["Shostip"], taskname="repl_create") user_task.save() return redirect('/hd_mesos/tasklist.html') else: return render(request, "mesos/hd_mysql/sid_mge/sidlist.html", {"error": "实例创建失败,主从版本不一致!"})