def change_vaultpwd(self, request): ''' 修改用户vault密码 ''' result = self._is_login(request) if result[0]: editer = result[1] else: return HttpResponseRedirect(reverse('login')) username = request.GET.get('username') if request.method == 'GET': form = Form_Login() return render( request, 'user_chgvltpwd.html', { 'form': form, 'username': username, 'login_user': editer, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) else: form = Form_Login(request.POST) if form.is_valid(): user_mess_dict = {} user_mess_dict['currvaultpassword'] = request.POST.get( 'currvaultpassword') user_mess_dict['vaultpassword'] = request.POST.get( 'vaultpassword') user_mess_dict['vaultpassword-confirm'] = request.POST.get( 'vaultpassword-confirm') user_mess_dict['lastediter'] = editer user_mess_dict['username'] = username result = self.usermanager_api.change_vaultpwd(user_mess_dict) if not result[0]: error_message = self.username + ' 修改用户' + username + '的vault密码失败,提交时发生错误,原因:' + result[ 1] self.logger.error(error_message) http_referer = self.uri_api.get_httpreferer(username, no=-1) return render( request, 'result.html', { 'error_message': error_message, 'http_referer': http_referer, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) else: self.logger.info(self.username + ' 编辑用户' + username + '的vault密码,提交并保存成功') return HttpResponseRedirect(reverse('logout'))
def edit(self, request): result = self._is_login(request) if result[0]: username = result[1] else: return HttpResponseRedirect(reverse('login')) vault_password = request.session['vault_password'] if request.method == 'GET': html_code = self.ansible_option_api.edit_get( username, vault_password) self.logger.info(self.username + ' 编辑用户' + username + '的ansible配置,查询成功') return render( request, 'option_edit.html', { 'html_code': html_code, 'login_user': username, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) else: form = Form_Login(request.POST) if form.is_valid(): field_list = self.ansible_option_api.init_parm(username) user_mess_dict = {} for field in field_list: try: value = request.POST.get(field) if value: user_mess_dict[field] = value except: pass result = self.ansible_option_api.edit_post( username, vault_password, user_mess_dict) if not result[0]: error_message = self.username + ' 编辑用户' + username + '的ansible配置失败,提交保存时发生错误,原因:' + result[ 1] self.logger.error(error_message) http_referer = self.uri_api.get_httpreferer(username, no=-1) return render( request, 'result.html', { 'error_message': error_message, 'http_referer': http_referer, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) else: self.logger.info(self.username + ' 编辑用户' + username + '的ansible配置,提交保存成功') return HttpResponseRedirect(reverse('ansible_option'))
def edit(self, request): result = self._is_login(request) if result[0]: username = result[1] else: return HttpResponseRedirect(reverse('login')) vault_password = request.session['vault_password'] name = request.GET.get('name') if request.method == 'GET': html_code = self.inventory_api.edit_get(username, vault_password, name) if not result[0]: error_message = self.username + ' 编辑用户' + username + '的名为' + name + '的主机失败,查询时发生错误,原因:' + result[ 1] self.logger.error(error_message) else: self.logger.info(self.username + ' 编辑用户' + username + '的名为' + name + '的主机,查询成功') return render( request, 'inve_edit.html', { 'html_code': html_code, 'login_user': username, 'name': name, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) else: form = Form_Login(request.POST) if form.is_valid(): user_mess_dict = self.post_handle(request) result = self.inventory_api.edit_post(username, name, vault_password, user_mess_dict) if not result[0]: error_message = self.username + ' 编辑用户' + username + '的名为' + name + '的主机失败,提交保存时发生错误,原因:' + result[ 1] self.logger.error(error_message) http_referer = self.uri_api.get_httpreferer(username, no=-1) return render( request, 'result.html', { 'error_message': error_message, 'http_referer': http_referer, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) else: self.logger.info(self.username + ' 编辑用户' + username + '的名为' + name + '的主机,提交保存成功') return HttpResponseRedirect(reverse('inventory_list'))
def add(self, request): result = self._is_login(request) if result[0]: username = result[1] else: return HttpResponseRedirect(reverse('login')) self.ansible_yaml_api = Manager_Yaml(username, mongoclient=self.mongoclient, redisclient=self.redisclient) if request.method == 'GET': form = Form_Login() return render( request, 'yaml_add.html', { 'login_user': username, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) else: form = Form_Login(request.POST) if form.is_valid(): name = request.POST.get('name') content = request.POST['content'] yaml_tpye = 'main' # request.POST['yaml_tpye'] file_type = request.POST['file_type'] describe = request.POST['describe'] result = self.ansible_yaml_api.add(content, name, yaml_tpye=yaml_tpye, file_type=file_type, describe=describe) if not result[0]: error_message = self.username + ' 为用户' + username + '新增ansible yaml文件时失败,提交保存时发生错误,原因:' + result[ 1] self.logger.error(error_message) http_referer = self.uri_api.get_httpreferer(username, no=-1) return render( request, 'result.html', { 'error_message': error_message, 'http_referer': http_referer, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) else: self.logger.info(self.username + ' 为用户' + username + '新增ansible yaml文件,提交保存成功') return HttpResponseRedirect(reverse('ansible_yaml'))
def add(self, request): ''' 新增用户 ''' result = self._is_login(request) if result[0]: creater = result[1] else: return HttpResponseRedirect(reverse('login')) if request.method == 'GET': form = Form_Login() return render( request, 'user_add.html', { 'form': form, 'login_user': creater, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) else: form = Form_Login(request.POST) if form.is_valid(): post_key_list = [ 'username', 'name', 'contact', 'password', 'password-confirm', 'vaultpassword', 'vaultpassword-confirm' ] user_mess_dict = {} for key in post_key_list: user_mess_dict[key] = request.POST.get(key) user_mess_dict['creater'] = creater result = self.usermanager_api.create(user_mess_dict) if not result[0]: error_message = creater + ' 新增用户' + user_mess_dict[ 'username'] + ',提交失败,原因:' + result[1] self.logger.info(error_message) http_referer = self.uri_api.get_httpreferer(creater, no=-2) return render( request, 'result.html', { 'error_message': error_message, 'http_referer': http_referer, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) else: self.logger.info(creater + ' 新增用户' + user_mess_dict['username'] + ',提交并保存成功') return HttpResponseRedirect(reverse('user_list'))
def adhoc(self, request): result = self._is_login(request) if result[0]: username = result[1] else: return HttpResponseRedirect(reverse('login')) if request.method == 'GET': form = Form_Login() group_list = self.inventory_api.get_grouplist(username) return render( request, 'exec_adhoc.html', { 'login_user': username, 'nav_html': self.nav_html, 'group_list': group_list, 'lately_whereabouts': self.latelywhere_html }) else: form = Form_Login(request.POST) if form.is_valid(): name = request.POST.get('name') pattern_list = request.POST.getlist('inve_group') module_name = request.POST['module'] argv = request.POST['argv'] describe = request.POST['describe'] exec_api = Exec_Tasks(mongoclient=self.mongoclient, redisclient=self.redisclient) vault_password = request.session['vault_password'] result = exec_api.adhoc(username, name, vault_password, pattern_list, module_name, argv, describe) if not result[0]: error_message = self.username + ' 下发ansible临时任务失败,任务名为' + name + ',原因:' + result[ 1] self.logger.error(error_message) http_referer = self.uri_api.get_httpreferer(username, no=-1) return render( request, 'result.html', { 'error_message': error_message, 'http_referer': http_referer, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) else: self.logger.info(self.username + ' 下发ansible临时任务成功,任务名为' + name) return HttpResponseRedirect(reverse('ansible_report'))
def import_file(self, request): result = self._is_login(request) if result[0]: username = result[1] else: return HttpResponseRedirect(reverse('login')) self.ansible_yaml_api = Manager_Yaml(username, mongoclient=self.mongoclient, redisclient=self.redisclient) if request.method == 'GET': form = Form_Login() return render( request, 'yaml_import.html', { 'login_user': username, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) else: form = Form_Login(request.POST) if form.is_valid(): name = request.POST.get('name') describe = request.POST['describe'] # yaml_tpye = request.POST['yaml_tpye'] yaml_tpye = 'main' try: this_path = request.POST.get('path') if this_path is not None: result = self.ansible_yaml_api.import_path( this_path, name, yaml_tpye=yaml_tpye, describe=describe) else: upload_file = request.FILES['file'] if upload_file is not None: result = self.ansible_yaml_api.import_upload( upload_file, name, yaml_tpye=yaml_tpye, describe=describe) except: upload_file = request.FILES['file'] if upload_file is not None: result = self.ansible_yaml_api.import_upload( upload_file, name, yaml_tpye=yaml_tpye, describe=describe) if not result[0]: error_message = self.username + ' 为用户' + username + '上传导入ansible yaml时失败,提交保存时发生错误,原因:' + result[ 1] self.logger.error(error_message) http_referer = self.uri_api.get_httpreferer(username, no=-1) return render( request, 'result.html', { 'error_message': error_message, 'http_referer': http_referer, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) else: self.logger.info(self.username + ' 为用户' + username + '上传导入ansible yaml,提交保存成功') return HttpResponseRedirect(reverse('ansible_yaml'))
def edit(self, request): result = self._is_login(request) if result[0]: username = result[1] else: return HttpResponseRedirect(reverse('login')) uuid_str = request.GET.get('uuid') self.ansible_yaml_api = Manager_Yaml(username, mongoclient=self.mongoclient, redisclient=self.redisclient) result = self.ansible_yaml_api.detail(uuid_str, isedit=True) if not result[0]: error_message = self.username + ' 编辑用户' + username + '的uuid为' + uuid_str + '的ansible yaml失败,查询时发生错误,原因:' + result[ 1] self.logger.error(error_message) http_referer = self.uri_api.get_httpreferer(username, no=-1) return render( request, 'result.html', { 'error_message': error_message, 'http_referer': http_referer, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) if request.method == 'GET': content_dict = result[1].get('content', {}) roles_content = content_dict.get('roles', '') main_content = content_dict.get('main', '') include_content = content_dict.get('include', '') name = result[1].get('name', '') describe = result[1].get('describe', '') yaml_tpye = result[1].get('type', '') file_type = result[1].get('file_type', '') self.logger.info(self.username + ' 编辑用户' + username + '的uuid为' + uuid_str + '的ansible yaml,查询成功') return render( request, 'yaml_edit.html', { 'login_user': username, 'nav_html': self.nav_html, 'main': main_content, 'roles': roles_content, 'include': include_content, 'name': name, 'describe': describe, 'yaml_tpye': yaml_tpye, 'file_type': file_type, 'uuid': uuid_str, 'lately_whereabouts': self.latelywhere_html }) else: form = Form_Login(request.POST) if form.is_valid(): content_dict = result[1]['content'] file_list = get_allkey(content_dict) file_list = dimension_multi2one(file_list) new_content_dict = {} for filename in file_list: file_content = request.POST.get(filename, '') if file_content: new_content_dict[filename] = file_content name = request.POST.get('name', '') describe = request.POST.get('describe', '') result = self.ansible_yaml_api.edit(uuid_str, new_content_dict, describe=describe, name=name) if not result[0]: error_message = self.username + ' 编辑用户' + username + '的uuid为' + uuid_str + '的ansible yaml失败,提交保存时发生错误,原因:' + result[ 1] self.logger.error(error_message) http_referer = self.uri_api.get_httpreferer(username, no=-1) return render( request, 'result.html', { 'error_message': error_message, 'http_referer': http_referer, 'nav_html': self.nav_html, 'uuid': uuid_str, 'lately_whereabouts': self.latelywhere_html }) else: self.logger.info(self.username + ' 编辑用户' + username + '的uuid为' + uuid_str + '的ansible yaml提交保存成功') return HttpResponseRedirect(reverse('ansible_yaml'))
def edit(self, request): ''' 编辑用户 ''' result = self._is_login(request) if result[0]: editer = result[1] else: return HttpResponseRedirect(reverse('login')) username = request.GET.get('username') http_referer = self.uri_api.get_httpreferer(username, no=-2) result = self.usermanager_api.detail(username) if result[0]: data_dict = result[1] if not data_dict: error_message = self.username + ' 编辑用户' + username + '基础信息失败,原因:用户不存在' self.logger.warning(error_message) return render( request, 'result.html', { 'error_message': error_message, 'http_referer': http_referer, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) else: error_message = self.username + ' 编辑用户' + username + '基础信息失败,查询用户信息时发生错误,原因:' + result[ 1] self.logger.error(error_message) return render( request, 'result.html', { 'error_message': error_message, 'http_referer': http_referer, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) if request.method == 'GET': return render( request, 'user_edit.html', { 'data_dict': data_dict, 'username': username, 'login_user': editer, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) else: form = Form_Login(request.POST) if form.is_valid(): post_key_list = ['name', 'contact'] user_mess_dict = {} for key in post_key_list: user_mess_dict[key] = request.POST.get(key) user_mess_dict['username'] = request.GET.get('username') user_mess_dict['lastediter'] = editer result = self.usermanager_api.edit(user_mess_dict) if not result[0]: error_message = self.username + ' 编辑用户' + username + '基础信息失败,提交时发生错误,原因:' + result[ 1] self.logger.error(error_message) http_referer = self.uri_api.get_httpreferer(username, no=-1) return render( request, 'result.html', { 'error_message': error_message, 'http_referer': http_referer, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) else: self.logger.info(self.username + ' 编辑用户' + username + '基础信息,提交并保存成功') return HttpResponseRedirect(reverse('index'))
def edit(self, request): ''' 编辑用户的privacy数据 ''' result = self._is_login(request) if result[0] : username = result[1] else : return HttpResponseRedirect(reverse('login')) vault_password = request.session['vault_password'] http_referer = 'detail' result = self.privacy_api.get(username, vault_password=vault_password, force=True) if result[0] : data_dict = result[1] error_message = '' else : data_dict = {} error_message = result[1] if not data_dict or data_dict == {} : ranges = range(0, 10) else : ranges = range(0, 5) if request.method == 'GET' : form = Form_Login() if error_message : error_message = self.username + ' 编辑用户' + username + '的机密数据失败,查询时发生错误,原因:' + result[1] self.logger.error(error_message) else : self.logger.info(self.username + ' 编辑用户' + username + '的机密数据,查询数据成功') return render(request, 'privacy_edit.html', {'data_dict': data_dict, 'login_user':username, 'error_message':error_message, 'form':form, 'new_list':list(ranges), 'nav_html':self.nav_html, 'lately_whereabouts':self.latelywhere_html}) else: form = Form_Login(request.POST) if form.is_valid(): update_dict = {} for key , vaule in data_dict.items() : keys = request.POST.get('key:' + key) vaules = request.POST.get('vaule:' + key + ':' + vaule) if not (keys == '' or not keys) : new_key = keys else : new_key = key if not (vaules == '' or not vaules) : new_vaule = vaules else : new_vaule = vaule if new_key in update_dict : error_message = self.username + ' 编辑用户' + username + '的机密数据失败,原因:键' + new_key + '出现重复' self.logger.error(error_message) return render(request, 'result.html', {'error_message' : error_message, 'http_referer':http_referer, 'nav_html':self.nav_html, 'lately_whereabouts':self.latelywhere_html}) update_dict[new_key] = new_vaule for i in ranges : keys = request.POST.get('key:' + str(i)) if not (keys == '' or not keys) : if keys in update_dict : error_message = self.username + ' 编辑用户' + username + '的机密数据失败,原因:键' + new_key + '出现重复' self.logger.error(error_message) return render(request, 'result.html', {'error_message' : error_message, 'http_referer':http_referer, 'nav_html':self.nav_html, 'lately_whereabouts':self.latelywhere_html}) vaules = request.POST.get('vaule:' + str(i)) if keys == vaules : error_message = self.username + ' 编辑用户' + username + '的机密数据失败,原因:键和值不能重复' self.logger.error(error_message) return render(request, 'result.html', {'error_message' : error_message, 'http_referer':http_referer, 'nav_html':self.nav_html, 'lately_whereabouts':self.latelywhere_html}) update_dict[keys] = vaules result = self.privacy_api.save(username, update_dict, vault_password) if not result[0] : error_message = self.username + ' 编辑用户' + username + '的机密数据失败,提交数据时发生错误,原因:' + result[1] self.logger.error(error_message) return render(request, 'result.html', {'error_message' : error_message, 'http_referer':http_referer, 'nav_html':self.nav_html, 'lately_whereabouts':self.latelywhere_html}) else : self.logger.info(self.username + ' 编辑用户' + username + '的机密数据,提交并保存成功') return HttpResponseRedirect(reverse('privacy_detail'))
def playbook(self, request): result = self._is_login(request) if result[0]: username = result[1] else: return HttpResponseRedirect(reverse('login')) if request.method == 'GET': form = Form_Login() self.ansible_yaml_api = Manager_Yaml(username, mongoclient=self.mongoclient, redisclient=self.redisclient) abs_list = self.ansible_yaml_api.get_abs() new_dict = {} for name_dict in abs_list: d_type = name_dict['type'] if d_type in ('main', 'full_roles'): new_dict[name_dict['uuid']] = name_dict['name'] group_list = self.inventory_api.get_grouplist(username) return render( request, 'exec_playbook.html', { 'login_user': username, 'nav_html': self.nav_html, 'group_list': group_list, 'yaml_file': new_dict, 'lately_whereabouts': self.latelywhere_html }) else: form = Form_Login(request.POST) if form.is_valid(): name = request.POST.get('name') pattern_list = request.POST.getlist('inve_group') uuidstr = request.POST['uuid'] describe = request.POST['describe'] exec_api = Exec_Tasks(mongoclient=self.mongoclient, redisclient=self.redisclient) vault_password = request.session['vault_password'] result = exec_api.playbook(username, name, vault_password, pattern_list, uuidstr, describe) if not result[0]: error_message = self.username + ' 下发ansible playbook任务失败,任务名为' + name + ',原因:' + result[ 1] self.logger.error(error_message) http_referer = self.uri_api.get_httpreferer(username, no=-1) return render( request, 'result.html', { 'error_message': error_message, 'http_referer': http_referer, 'nav_html': self.nav_html, 'lately_whereabouts': self.latelywhere_html }) else: self.logger.info(self.username + ' 下发ansible playbook任务成功,任务名为' + name) return HttpResponseRedirect(reverse('ansible_report'))