def ansible_rsync_web(self, remoteips, rsync_command, last_command, excludes): remotedir = "/data/wwwroot/" + self.siteid.replace('m', '') exclude = genxin_exclude_file(excludes) siteid = self.siteid.replace('m', '') for i in remoteips.split('\r\n'): print "i=" + i owner = "chown -R 400.400 /data/wwwroot/%s" % siteid unlock = "chattr -R -i /data/wwwroot/" lock = "chown -R 400.400 /data/wwwroot/%s && chattr -R +i /data/wwwroot/ && find /data/wwwroot/ -maxdepth 6 -type d -name 'Logs' | xargs -i chattr -R -i {}" % siteid if self.env == 'online': command_unlock = ssh_cmd(i, unlock) rsync_command_res = ssh_cmd(i, rsync_command) #执行推送代码前命令 obj = Server.objects.get(ssh_host=i) task = MyTask(gen_resource(obj)) print "源目录:%s" % self.pc_merge_dir print "目标目录:%s" % remotedir print "排除文件:%s" % exclude rsync_res = task.genxin_rsync( self.pc_merge_dir, remotedir, exclude) #将代码从CMDB本地目录推送到服务器目录,需要此机器可以公钥访问源站 if self.env == 'online': command_lock = ssh_cmd(i, lock) else: command_lock = ssh_cmd(i, owner) last_command_res = ssh_cmd(i, last_command) #执行代码推送后命令 print rsync_res return rsync_res
def business_conf_show(request): #1.获取ip,路径,文件 ips = request.GET.get('ip') print ips.split(" ") path = request.GET.get('path') file = request.GET.get('file') tmp_file = "/tmp/tmp_nginx.conf" if str(path[-1]) is not "/": path = path + "/" + file path = "cat %s" % path else: path = path + file path = "cat %s" % path # print path f = open(tmp_file, "wb") for ip in ips.split(" "): ip_title = "##########%s %s##########\r\n" % (ip, path) f.write(ip_title) res = ssh_cmd(ip, str(path)) for i in res: f.write(i) f.close() # print type(res) data = {'ip': ips} # return HttpResponse(res) return JsonResponse(data, safe=False)
def deploy_script(request): data = scriptrepo.objects.all() now = int(time.time()) log = scriptlog.objects.all() L = [i.sort_time for i in log] if L: try: now_log_time = max(L) L.remove(now_log_time) two_log_time = max(L) L.remove(two_log_time) three_log_time = max(L) except: log_data = [] else: log_one = scriptlog.objects.get(sort_time=now_log_time) log_two = scriptlog.objects.get(sort_time=two_log_time) log_three = scriptlog.objects.get(sort_time=three_log_time) log_data = [log_one, log_two, log_three] if request.method == 'POST': ll = request.POST.lists() num = len(ll) - 3 args_list = [] if num is not 0: for i in range(num): select_name = "select_script" + str(i) args = request.POST.get(select_name, '') args_list.append(args) customargs = " ".join(args_list) user = request.user ##日志记录 uuid = request.POST.get('command', '') command = scriptrepo.objects.get(pk=uuid).command host = scriptrepo.objects.get(pk=uuid).server_ip parameter = request.POST.get('parameter', '') command = command + " " + customargs + " " + parameter ##记录到日志里的操作,也要给ansible的参数 print command # return HttpResponse(command) sort_time = now ##日志记录 # groupname = "script_group" # inventory = script_inventory(uuid,groupname) ##ansible的参数 # playbook = "/etc/ansible/script_deploy.yml" # res = ansiblex_deploy(inventory,playbook,groupname,command) res = ssh_cmd(host, command) string = "" for i in res: string = string + i # res = [i.decode('utf-8') for i in res] logdata = scriptlog(user=user, command=command, result=string, sort_time=sort_time) logdata.save() return HttpResponseRedirect('/success/') return render(request, 'automation/script_deploy.html', locals())
def script_update_task(uuid, host, command): current_task.update_state(state="PROGRESS") res = ssh_cmd(host, command) string = "" for i in res: string = string + i data = scriptdeploy.objects.get(pk=uuid) now = int(time.time()) logdata = scriptlog(user=data.executive_user, name=data.name, memo=data.memo, command=command, result=string, sort_time=now, scriptdeploy=data) logdata.save() scriptdeploy.objects.filter(pk=uuid).update(status="已更新") return "123"
def shell(message): try: ip = message.text.split()[1].split('@')[0] cmd = message.text.split('@')[-1] if not isValidIp(ip): text = "IP格式错误" bot.sendMessage(chat_id=message.chat.id, text=text) return 9 except IndexError: text = "缺少参数!/shell IP地址@命令" bot.sendMessage(chat_id=message.chat.id, text=text) return 9 path = "[root@localhost ~]# " try: Server.objects.get(ssh_host=ip) except: text = "此IP:%s 不在CMDB记录中" % ip bot.sendMessage(chat_id=message.chat.id, text=text) return 8 try: res = ssh_cmd(ip, cmd) print res res = " ".join(res) text = path + '\r\n' + res except: text = "连接超时!" num = len(text) / 4096 if num == 0: bot.sendMessage(chat_id=message.chat.id, text=text) else: start = 0 for i in num: end = start + 4096 bot.sendMessage(chat_id=message.chat.id, text=text[start:end]) start += 4096 if len(text) % 4096 == 0: pass else: end = start + len(text) % 4096 bot.sendMessage(chat_id=message.chat.id, text=text[start:end])