def host_add(request): if not request.user.has_perm('cmdb.add_host'): raise PermissionDenied t = request.GET.get('t', '2') # 资产类型ID active = 'cmdb_host_1_active' if t == '1' else 'cmdb_host_2_active' context = {active: 'active'} if request.method == 'POST': # import ipdb;ipdb.set_trace() newhost = QueryDict(mutable=True) newhost.update(request.POST) newhost.update({'asset_type': t}) form = HostForm(newhost) if form.is_valid(): form.save() ref_url = request.POST.get('ref_url', '/') return redirect(ref_url) else: form = HostForm() ref_url = request.META.get('HTTP_REFERER', '/') # 来源URL,供POST成功后返回 context.update({'ref_url': ref_url}) context.update({'form': form}) html = 'host_2.html' return render(request, html, context)
def hosts(request, event_id): event = get_object_or_404(Event, id=event_id) form = HostForm(event=event, data=(request.POST or None)) if form.is_valid(): form.save() return redirect(event) template = "events/_hosts.html" if request.is_ajax() else "events/hosts.html" return render_to_response(template, locals(), context_instance=RequestContext(request))
def edit(request): '''Edit a host's information.''' # The request method used (GET, POST, etc) determines what dict 'context' # gets filled with. context = {} # If we have a non-empty POST request, that means # the user is submitting / loading the form. if request.POST: # 'update_form' is a hidden tag in our template's form. # If it exists in our POST, then the user is submitting the form. # Otherwise, the user is loading the form. if 'update_form' not in request.POST: host_ip = request.session['last-host'] host = get_object_or_404(Host, ipv4_address=host_ip) context['host'] = host form = HostForm(instance=host) else: # Retrieve IP addr user entered into form. # Use that IP to get respective Host object. host_ip = request.session['last-host'] host = get_object_or_404(Host, ipv4_address=host_ip) # Load form with provided information so we can save the form as # an object. (See forms.py and docs on ModelForms for more info) form = HostForm(request.POST, instance=host) # form.is_valid() checks that updated instance object's new attributes # are valid. Calling form.save() will actually save them to the db. if form.is_valid(): # Save attributes from form into those of a Host object. form.save() # Get ip addr of updated Host so we can direct user to its page # after editing. new_ip = request.session['last-host'] return HttpResponseRedirect('/hosts/search/?input_ip=%s' % new_ip) # Request method was empty, so user wants to create new Host. # Create empty form. else: if 'last-host' in request.session: host_ip = request.session['last-host'] host = get_object_or_404(Host, ipv4_address=host_ip) context['host'] = host form = HostForm(instance=host) else: form = HostForm() # Add new CSRF token and form to context. context.update(csrf(request)) context['form'] = form return render(request, 'hosts/edit_host.html', context)
def asset_add(request): temp_name = "cmdb/cmdb-header.html" if request.method == 'POST': form = HostForm(request.POST) if form.is_valid(): form.save() return redirect('asset_list') else: form = HostForm() return render(request, 'cmdb/asset_add.html', locals())
def asset_edit(request, id): status = 0 asset_types = ASSET_TYPE host = Host.objects.get(id=id) if request.method == 'POST': form = HostForm(request.POST, instance=host) if form.is_valid(): form.save() status = 1 else: status = 2 else: form = HostForm(instance=host) return render(request, 'cmdb/asset_edit.html', locals())
def host_add(request): """ 添加主机 """ uf = HostForm() projects = Project.objects.all() services = Service.objects.all() if request.method == 'POST': uf_post = HostForm(request.POST) physics = request.POST.get('physics', '') ip = request.POST.get('eth1', '') if Host.objects.filter(eth1=ip): emg = u'添加失败, 该IP %s 已存在!' % ip return my_render('assets/host_add.html', locals(), request) if uf_post.is_valid(): zw = uf_post.save(commit=False) # zw.mac = str(request.POST.getlist("mac")[0]).replace(':', '-').strip(" ") status = uf_post.cleaned_data['status'] if physics: physics_host = get_object_or_404(Host, eth1=physics) zw.vm = physics_host zw.type = 1 else: zw.type = 0 zw.save() uf_post.save_m2m() host = Host.objects.get(eth1=ip) server_info(host.uuid) smg = u'主机%s添加成功!' % ip return render_to_response('assets/host_add.html', locals(), context_instance=RequestContext(request)) return render_to_response('assets/host_add.html', locals(), context_instance=RequestContext(request))
def host_add(request): """ 添加主机 """ uf = HostForm() projects = Project.objects.all() services = Service.objects.all() if request.method == 'POST': uf_post = HostForm(request.POST) physics = request.POST.get('physics', '') ip = request.POST.get('eth1', '') if Host.objects.filter(eth1=ip): emg = u'添加失败, 该IP %s 已存在!' % ip return my_render('assets/host_add.html', locals(), request) if uf_post.is_valid(): zw = uf_post.save(commit=False) zw.mac = str(request.POST.getlist("mac")[0]).replace(':', '-').strip(" ") status = uf_post.cleaned_data['status'] if physics: physics_host = get_object_or_404(Host, eth1=physics) zw.vm = physics_host zw.type = 1 else: zw.type = 0 zw.save() uf_post.save_m2m() if zabbix_on and status == 1: zabbix_host_add(request) smg = u'主机%s添加成功!' % ip return render_to_response('assets/host_add.html', locals(), context_instance=RequestContext(request)) return render_to_response('assets/host_add.html', locals(), context_instance=RequestContext(request))
def add(request): template_name = 'add.html' context = RequestContext(request) dict = {} if request.POST: form = HostForm(request.POST) if form.is_valid(): form.save() dict['message'] = "Thank you for add a new host. We'll review it as quickly as we can and add it to the site" form = HostForm() else: form = HostForm() dict['form'] = form return render_to_response( template_name, dict, context, )
def add(request, template_name): if request.method == 'POST': form = HostForm(request.POST) if form.is_valid(): host_info = form.save() host_info.save() return HttpResponseRedirect('/assets/') #return HttpResponseRedirect('/') else: form = HostForm() return render_to_response(template_name, { 'user': request.user, 'form': form, })
def add(request, template_name): if request.method == 'POST': form = HostForm(request.POST) if form.is_valid(): host_info = form.save() host_info.save() return HttpResponseRedirect('/assets/') #return HttpResponseRedirect('/') else: form = HostForm() return render_to_response(template_name, {'user': request.user, 'form': form, } )
def host_register(request): gRegister = False if request.method == 'POST': host_form = HostForm(data=request.POST) if host_form.is_valid(): user = host_form.save() user.set_password(user.password) user.save() gRegister = True return HttpResponseRedirect('/hackathon/api/') else: print host_form.errors else: guide_form = HostForm() context = {'guide_form': guide_form, 'registered': gRegister} return render(request, 'hackathon/host.html', context)
def host_register(request): user = request.user if user.is_authenticated: if request.method == 'POST': host_form = HostForm(data=request.POST) if host_form.is_valid(): instance = host_form.save(commit=False) instance.user = request.user instance.save() return HttpResponseRedirect('/edit_userpage/') else: print host_form.errors else: return HttpResponseRedirect('/') guide_form = HostForm() context = {'guide_form': guide_form} return render(request, 'users/host.html', context)
def host_edit(request): """ 修改主机 """ uuid = request.GET.get('uuid') host = get_object_or_404(Host, uuid=uuid) uf = HostForm(instance=host) project_all = Project.objects.all() project_host = host.business.all() projects = [p for p in project_all if p not in project_host] service_all = Service.objects.all() service_host = host.service.all() services = [s for s in service_all if s not in service_host] username = request.user.username if request.method == 'POST': physics = request.POST.get('physics', '') uf_post = HostForm(request.POST, instance=host) if uf_post.is_valid(): zw = uf_post.save(commit=False) zw.mac = str(request.POST.getlist("mac")[0]).replace( ':', '-').strip(" ") request.POST = request.POST.copy() if physics: physics_host = get_object_or_404(Host, eth1=physics) request.POST['vm'] = physics_host.uuid if host.vm: if str(host.vm.eth1) != str(physics): zw.vm = physics_host else: zw.vm = physics_host zw.type = 1 else: request.POST['vm'] = '' zw.type = 0 zw.save() uf_post.save_m2m() new_host = get_object_or_404(Host, uuid=uuid) info = get_diff(uf_post.__dict__.get('initial'), request.POST) db_to_record(username, host, info) return HttpResponseRedirect('/assets/host_detail/?uuid=%s' % uuid) return render_to_response('assets/host_edit.html', locals(), context_instance=RequestContext(request))
def host_edit(request): """ 修改主机 """ uuid = request.GET.get('uuid') host = get_object_or_404(Host, uuid=uuid) uf = HostForm(instance=host) project_all = Project.objects.all() project_host = host.business.all() projects = [p for p in project_all if p not in project_host] service_all = Service.objects.all() service_host = host.service.all() services = [s for s in service_all if s not in service_host] username = request.user.username if request.method == 'POST': physics = request.POST.get('physics', '') uf_post = HostForm(request.POST, instance=host) if uf_post.is_valid(): zw = uf_post.save(commit=False) zw.mac = str(request.POST.getlist("mac")[0]).replace(':', '-').strip(" ") request.POST = request.POST.copy() if physics: physics_host = get_object_or_404(Host, eth1=physics) request.POST['vm'] = physics_host.uuid if host.vm: if str(host.vm.eth1) != str(physics): zw.vm = physics_host else: zw.vm = physics_host zw.type = 1 else: request.POST['vm'] = '' zw.type = 0 zw.save() uf_post.save_m2m() new_host = get_object_or_404(Host, uuid=uuid) info = get_diff(uf_post.__dict__.get('initial'), request.POST) db_to_record(username, host, info) return HttpResponseRedirect('/assets/host_detail/?uuid=%s' % uuid) return render_to_response('assets/host_edit.html', locals(), context_instance=RequestContext(request))
def ospcdeploy(request): extras = [] #hosts = get_object_or_404(Hosts,hostname=request.POST['hostname']) if request.method == 'POST': # submit to setting the roles of the hosts #logger.info("To update the Hostform.") post = request.POST if 'refresh' in post: form = HostForm() if 'update' in post: #logs = readlog() try: logger.info("To update the Hostform.") host = Hosts.objects.get(hostname=post['hostname']) form = HostForm(request.POST,instance=host) if form.is_valid(): logger.info("Hosts has been updated.") form.save() except Hosts.DoesNotExist: form = HostForm() logger.warning("Hostname doesnot exists.") #delete the certain items in the Hosts table. if 'delete' in post: try: logger.info("To delete the certain host in table.") host = Hosts.objects.get(hostname=post['hostname']) host.delete() except Hosts.DoesNotExist: #form = HostForm() logger.warning("Hostname doesnot exists.") form = HostForm() if post['hostname']: puppet_clean.delay(post['hostname']) logger.info("trigger puppetca clean %s" % post['hostname']) # make the config file in the project root for CC and NC (localrc and localnc) if 'config' in post: logger.info("To configure localrc and localnc.") hosts = Hosts.objects.exclude(role='').order_by('role') form = HostForm() if hosts.count() > 0: confs = configuration(hosts) extras = "-----------CC Config-----------\n" extras += confs[0] if len(confs) > 1: extras += "\n-----------NC Config-----------\n" for conf in confs[1:]: extras += conf # deployment phase can be divided into 2 parts: transportation and execution if 'deploy' in post: logs = None cc = [] nc = [] flag = 0 host_map = {} form = HostForm() logger.info("To deploy the client hosts.") hosts = Hosts.objects.exclude(role='').order_by('role') if hosts.count() > 0: for host in hosts: if host.role == '1': if os.path.exists(os.path.join(conf_path(host.hostname),'localrc')): logger.info("CC host %s" % host.hostname) cc.append(host.hostname) host_map["cc"] = cc else: flag = 1 logger.error("The config file %s does not exists, plz generate the config file first." % host.hostname ) logs = "The config file %s does not exists, plz generate the config file first." % host.hostname if host.role =='2': if os.path.exists(os.path.join(conf_path(host.hostname),'localrc')): logger.info("NC host %s" % host.hostname) nc.append(host.hostname) else: flag = 1 logger.error("The config file %s does not exists, plz generate the config file first." % host.hostname ) logs = "The config file %s does not exists, plz generate the config file first." % host.hostname if flag == 0: host_map["nc"] = nc deploy.delay(host_map) logger.info("tasks triggered.") else: logs = "There is no cc defined." else: logs = "No hosts got configured." if logs == None: logs = readlog() if 'single-config' in post: logger.info("To configure localrc and localnc for single host.") try: cc = Hosts.objects.get(role='1') host = Hosts.objects.filter(hostname=post['hostname']) confs = configuration(host, [cc.dhcp_ip,cc.static_ip]) if post['hostname'] == cc.hostname: extras = "-----------CC Config-----------\n" else: extras = "-----------NC Config-----------\n" extras += confs[0] except Hosts.DoesNotExist: extras = "Error: can\'t find CC" logger.error("CC doesnot exists.") form = HostForm() if 'single-deploy' in post: logger.info("To deploy compute node %s" % post['hostname']) form = HostForm() host_map = {} logs = None try: logger.info("Begin deploy compute node %s" % post['hostname']) host = Hosts.objects.get(hostname=post['hostname']) flag = 0 if host.role == '1': if os.path.exists(os.path.join(conf_path(host.hostname),'localrc')): logger.info("CC host %s" % host.hostname) cc = [] cc.append(host.hostname) host_map["cc"] = cc else: flag = 1 logger.error("The config file localrc does not exists, plz generate the config file first.") logs = "The config file localrc does not exists, plz generate the config file first." if host.role == '2': if os.path.exists(os.path.join(conf_path(host.hostname),'localrc')): host_map["nc"] = host.hostname nc = [] nc.append(host.hostname) host_map["nc"] = nc else: flag = 1 logger.error("The config file localrc does not exists, plz generate the config file first.") logs = "The config file localrc does not exists, plz generate the config file first." if flag == 0: deploy.delay(host_map) logger.info("single task triggered.") except Hosts.DoesNotExist: logs = "The host is invalid." if logs == None: logs = readlog() else: form = HostForm() # bind the object to table init_queryset = Hosts.objects.all() table = HostTable( init_queryset, order_by=request.GET.get('sort','hostname')) return render_to_response('ospcdeploy.html',locals(),RequestContext(request))