def view_project(request, project_name): if not project_name: raise Exception("project name required!") user = request.user project = Project.objects.get(name=project_name) if (User.objects.get(username='******').has_perm( 'access_proj', project) == False): if (user.has_perm('access_proj', project) == False): raise PopupException( 'Sorry, you have not right to access the project!') if not project: raise Exception("project not exits!") if request.method == 'POST': if (user.has_perm('config_proj', project) == False): raise PopupException( 'Sorry, you have not right to config the project!') hostcontent = request.POST.get("hostcontent") savefilecontent(project_name, hostcontent) if project: templates = JobTemplate.objects.filter(project=project) hostcontent = getfilecontent(project_name) return render( "ansible/view_project.html", request, { 'project': project, 'templates': templates, 'hostcontent': hostcontent, 'request': request, }) else: return HttpResponseRedirect(urlresolvers.reverse('list_projects'))
def credential(request, username): ''' user's credential , for ansible jobs ''' user = User.objects.get(username=username) if (request.user != user): raise PopupException( 'Sorry, you have not right to see the cretential!') if request.method == 'POST': ssh_password = request.POST.get('ssh_password') ssh_key = request.POST.get('id_dsa_pub') profile = Profile.objects.get_or_create(user=request.user)[0] if ssh_password == '': profile.ssh_password = '' else: profile.ssh_password = AESencrypt("pass34", ssh_password) profile.ssh_key = ssh_key profile.save() try: profile = Profile.objects.get(user=request.user) except: profile = None return render("account/credential.html", request, { 'username': username, 'profile': profile, })
def save_template(request, project_name): user = request.user project = Project.objects.get(name=project_name) if (user.has_perm('config_proj', project) == False): raise PopupException( 'Sorry, you have not right to config the project!') if request.method == 'POST': template_name = request.POST.get('template_name') inventory = request.POST.get('inventory') var_file = request.POST.get('var_files') tasks = request.POST.get('tasks') hosts = request.POST.get('hosts') user = request.POST.get('user') use_sudo = request.POST.get('use_sudo') forks = request.POST.get('forks') limit = request.POST.get('limit') extra_vars = request.POST.get('extra_vars') email = request.POST.get('email') if use_sudo: sudo_user = request.POST.get('sudo_user') template_pk = request.POST.get('template_pk') LOG.debug("template_pk=%s" % template_pk) if template_pk is not None and template_pk != "None" and template_pk != "": template = JobTemplate.objects.get(pk=template_pk) else: template = JobTemplate() template.name = template_name template.project = project template.inventory = inventory template.playbook = tasks template.hosts = hosts template.user = user template.created_by = request.user template.use_sudo = use_sudo if use_sudo: template.sudo_user = sudo_user # template.forks = forks # template.limit = limit # template.extra_vars = extra_vars template.vars_files = var_file template.email = email template.save() return HttpResponseRedirect( urlresolvers.reverse('view_project', kwargs={'project_name': project_name}))
def explore(request, project_name): user = request.user project = Project.objects.get(name=project_name) if (User.objects.get(username='******').has_perm( 'config_proj', project) == False): if (user.has_perm('config_proj', project) == False): raise PopupException( 'Sorry, you have not right to config the project!') return render("ansible/explore.html", request, { 'request': request, 'project': project })
def get_profile(job_pk): job = Job.objects.get(pk=job_pk) user = job.created_by #user = User.objects.get(username='******') try: profile = Profile.objects.get(user=user) except: # user = User.objects.get(username='******') # profile = Profile.objects.get(user = user) raise PopupException( 'you need to set credential, if you want to execute the task, you need to paste your SSH Password, SSH Pub Keys (DSA)' ) return profile
def schedule(request, project_name): user = request.user project = Project.objects.get(name=project_name) if (user.has_perm('execute_proj', project) == False): raise PopupException( 'Sorry, you have not right to execute the project!') jobs = Job.objects.filter(project=project).filter( status='pending').exclude(countdown=0) return render("ansible/schedule.html", request, { 'project': project, 'jobs': jobs, 'request': request, })
def manage_project(request, project_id=""): user = request.user id = project_id project = Project.objects.get(pk=id) if (user.has_perm('manage_proj', project) == False): raise PopupException( 'Sorry, you have not right to manage the project!') user_perms = get_users_with_perms(project, attach_perms=True) allusers = json.dumps(list(get_allusers())) page_errors = None if request.method == 'POST': for key in request.POST: value = request.POST[key] for key, value in request.POST.iteritems(): m = re.match(r'.+_proj@', key) if m: keylist = re.split(r'@', key) keyperm = keylist[0] keyuser = User.objects.get(pk=keylist[1]) if user_perms.has_key(keyuser): if keyperm in user_perms[keyuser]: user_perms[keyuser].remove(keyperm) else: assign_perm(keyperm, keyuser, project) else: assign_perm(keyperm, keyuser, project) for key, value in user_perms.items(): if value != []: for item in value: remove_perm(item, key, project) return HttpResponseRedirect(urlresolvers.reverse('list_projects')) else: try: project = Project.objects.get(id=id) except Exception: raise Http404 return render( "ansible/manage_project.html", request, { 'action': urlresolvers.reverse('manage_project', kwargs={'project_id': id}), 'project': project, 'user_perms': user_perms, 'allusers': allusers, 'page_errors': page_errors, 'request': request, })
def delete_job(request, project_name): user = request.user project = Project.objects.get(name=project_name) if (user.has_perm('execute_proj', project) == False): raise PopupException( 'Sorry, you have not right to manage the project!') data = False try: job_pk = request.GET.get('job_pk') Job.objects.get(pk=job_pk).delete() data = True except Exception: data = False return render_json(data)
def delete_template(request, project_name): user = request.user project = Project.objects.get(name=project_name) if (user.has_perm('config_proj', project) == False): raise PopupException( 'Sorry, you have not right to config the project!') data = False try: template_pk = request.GET.get('template_pk') JobTemplate.objects.get(pk=template_pk).delete() data = True except Exception: data = False return render_json(data)
def deploykey(request, project_name): user = request.user project = Project.objects.get(name=project_name) if (user.has_perm('execute_proj', project) == False): raise PopupException( 'Sorry, you have not right to execute the project!') result = [] page_errors = None if request.method == 'POST': inventory = request.POST.get('inventory') hosts = request.POST.get('hosts') user = request.POST.get('user') password = request.POST.get('password') key = request.POST.get('sshkey') if inventory == '<--None-->': file_content = hosts if file_content.count(','): file_content = '\n'.join(file_content.split(',')) elif file_content.count(';'): file_content = '\n'.join(file_content.split(';')) else: file_content = '\n'.join(file_content.split(' ')) file_name = 'hosts' inventory = create_tmp_task_file(project_name, file_name, file_content) inventory = get_inventory_local_path(project_name, inventory) hostlist = get_hosts(inventory, hosts) result = get_sshkey_deploy(hostlist, user, password, key) results = {} for i in range(len(result)): results[i] = result[i] results = [results] return render_json(results) return render( "ansible/deploykey.html", request, { 'project': project, 'result': result, 'page_errors': page_errors, 'request': request, })
def edit_project(request, project_id=""): id = project_id user = request.user project = Project.objects.get(pk=id) if (user.has_perm('manage_proj', project) == False): raise PopupException( 'Sorry, you have not right to manage the project!') page_errors = None if request.method == 'POST': name = request.POST.get("name") desc = request.POST.get("description") scmtype = request.POST.get("scmtype") scmurl = request.POST.get("scmurl") group = request.POST.get("group") if not name: raise Exception("project name required!") # project = Project.objects.get(pk = id) if project: project.name = name project.description = desc project.created_by = request.user project.scmtype = scmtype project.scmurl = scmurl project.group = group project.save() else: raise Exception("Project is not existed!") return HttpResponseRedirect(urlresolvers.reverse('list_projects')) else: try: project = Project.objects.get(id=id) except Exception: raise Http404 return render( "ansible/add_project.html", request, { 'action': urlresolvers.reverse('edit_project', kwargs={'project_id': id}), 'project': project, 'page_errors': page_errors, 'request': request, })
def delete_project(request, project_name): user = request.user project = Project.objects.get(name=project_name) if (user.has_perm('manage_proj', project) == False): raise PopupException( 'Sorry, you have not right to manage the project!') data = False try: Job.objects.filter(project=project).delete() JobTemplate.objects.filter(project=project).delete() Package.objects.filter(project=project).delete() Project.objects.get(name=project_name).delete() delete_project_dir(project_name) data = True except Exception: data = False return render_json(data)
def restart_job(request, project_name, job_pk): user = request.user project = Project.objects.get(name=project_name) if (user.has_perm('execute_proj', project) == False): raise PopupException( 'Sorry, you have not right to execute the project!') job_current = Job.objects.get(pk=job_pk) now = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())) job = Job() job_today_id = job.get_id() job.name = '-'.join(['job', now, job_today_id]) job.created_by = request.user job.project = project job.playbook = job_current.playbook job.inventory = job_current.inventory job.extra_vars = job_current.extra_vars job.forks = job_current.forks job.limit = job_current.limit job.email = job_current.email job.use_sudo = job_current.use_sudo job.sudo_user = job_current.sudo_user job.sudo_password = job_current.sudo_password job.save() job_pk = job.pk job.start() return HttpResponseRedirect( urlresolvers.reverse('job_execute_result', kwargs={ 'project_name': project_name, 'job_pk': job_pk }))
def execute_script(request, project_name): project = Project.objects.get(name=project_name) if request.method == 'POST': if (request.user.has_perm('execute_proj', project) == False): raise PopupException( 'Soarry, you have not right to execute the project!') inventory = request.POST.get('inventory') script = request.POST.get('script') args = request.POST.get('args') hosts = request.POST.get('hosts') user = request.POST.get('user') use_sudo = request.POST.get('use_sudo') forks = request.POST.get('forks') limit = request.POST.get('limit') sudo_password = request.POST.get('sudo_password') sudo_user = request.POST.get('sudo_user') script_file_name = "%s.sh" % uuid.uuid1() script_content = script script_path = create_tmp_script_file(project_name, script_file_name, script_content) file_name = "%s.yml" % uuid.uuid1() file_content = "- hosts : %s \n" % hosts # file_content += " sudo : yes\n" file_content += " user : %s \n" % user if sudo_user: file_content += " sudo : yes\n" file_content += " sudo_user : %s \n" % sudo_user file_content += " tasks : \n" file_content += " - name: execute script \n" file_content += " action: script %s %s \n" % (script_path, args) file_path = create_tmp_task_file(project_name, file_name, file_content) now = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())) job = Job() now = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())) job_today_id = job.get_id() job.name = '-'.join(['job', now, job_today_id]) job.created_by = request.user job.project = project job.playbook = file_path job.inventory = get_inventory_local_path(project_name, inventory) if forks: job.forks = forks if limit: job.limit = limit job.use_sudo = use_sudo if sudo_user: job.sudo_user = sudo_user if sudo_password: job.sudo_password = sudo_password job.save() job_pk = job.pk job.start() return HttpResponseRedirect( urlresolvers.reverse('job_execute_result', kwargs={ 'project_name': project_name, 'job_pk': job_pk })) return render("ansible/execute_script.html", request, { 'request': request, 'project': project })
def execute_scp(request, project_name): project = Project.objects.get(name=project_name) if request.method == 'POST': if (request.user.has_perm('execute_proj', project) == False): raise PopupException( 'Soarry, you have not right to execute the project!') inventory = request.POST.get('inventory') hosts = request.POST.get('hosts') user = request.POST.get('user') use_sudo = request.POST.get('use_sudo') forks = request.POST.get('forks') limit = request.POST.get('limit') sudo_password = request.POST.get('sudo_password') sudo_user = request.POST.get('sudo_user') owner = request.POST.get('owner') group = request.POST.get('group') mode = request.POST.get('mode') dest = request.POST.get('dest') file_obj = request.FILES.get('src', None) handle_uploaded_file(file_obj) script_file_name = "%s.sh" % uuid.uuid1() script_content = "" script_path = create_tmp_script_file(project_name, script_file_name, script_content) src_path = settings.TMP_FILE dest_path = dest file_name = "%s.yml" % uuid.uuid1() file_content = "- hosts : %s \n" % hosts file_content += " user : %s \n" % user if sudo_user: file_content += " sudo : yes\n" file_content += " sudo_user : %s \n" % sudo_user file_content += " tasks : \n" file_content += " - name: execute script \n" file_content += " action: copy src=%s dest=%s owner=%s group=%s mode=%d\n" % ( src_path, dest_path, owner, group, int(mode)) file_path = create_tmp_task_file(project_name, file_name, file_content) now = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())) job = Job() now = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())) job_today_id = job.get_id() job.name = '-'.join(['job', now, job_today_id]) job.created_by = request.user job.project = project job.playbook = file_path job.inventory = get_inventory_local_path(project_name, inventory) job.forks = forks if limit: job.limit = limit job.use_sudo = use_sudo if sudo_password: job.sudo_password = sudo_password if sudo_user: job.sudo_user = sudo_user job.save() job_pk = job.pk job.start() return HttpResponseRedirect( urlresolvers.reverse('job_execute_result', kwargs={ 'project_name': project_name, 'job_pk': job_pk })) return render("ansible/execute_scp.html", request, { 'request': request, 'project': project })
def execute_playbook(request, project_name, template_pk=None): project = Project.objects.get(name=project_name) template = JobTemplate() plist = list(project.package.values('version', 'date')) packagelist = [] for item in plist: array = {} array['version'] = item['version'] array['date'] = item['date'].strftime("%Y-%m-%d-%H:%M:%S") packagelist.append(array) package_json = json.dumps(packagelist) pmax = project.package.annotate().order_by('-date')[:1] if template_pk: template = JobTemplate.objects.get(pk=template_pk) if request.method == 'POST': if (request.user.has_perm('execute_proj', project) == False): raise PopupException( 'Soarry, you have not right to execute the project!') save = request.POST.get('save') inventory = request.POST.get('inventory') var_files = request.POST.get('var_files') tasks = request.POST.get('tasks') hosts = request.POST.get('hosts') user = request.POST.get('user') use_sudo = request.POST.get('use_sudo') sudo_user = request.POST.get('sudo_user') forks = request.POST.get('forks') limit = request.POST.get('limit') extra_vars = request.POST.get('extra_vars') sudo_password = request.POST.get('sudo_password') package_version = request.POST.get('packageselect') email = request.POST.get('email') timer = request.POST.get('timer') #package_file = project_name + "_" + package_version + ".tar.gz" if inventory == '<--None-->': file_content = request.POST.get('hosts') if file_content.count(','): file_content = '\n'.join(file_content.split(',')) elif file_content.count(';'): file_content = '\n'.join(file_content.split(';')) else: file_content = '\n'.join(file_content.split(' ')) file_name = 'hosts' inventory = create_tmp_task_file(project_name, file_name, file_content) real_tasks = "" for t in tasks.splitlines(): real_tasks = real_tasks + " - include : " + get_playbook_local_path( project_name, t.strip()) + "\n" file_name = "%s.yml" % uuid.uuid1() file_content = "- hosts : %s \n" % hosts #file_content += " sudo : yes\n" file_content += " user : %s \n" % user if sudo_user: file_content += " sudo : yes\n" file_content += " sudo_user : %s \n" % sudo_user if package_version == "": if pmax: package_version = str(pmax[0].version) if package_version != "": package_file = project_name + "_" + package_version + ".tar.gz" file_content += " vars :\n" file_content += " package : %s \n" % package_file if var_files is not None and var_files != "": real_var_files = "" for f in var_files.splitlines(): real_var_files = real_var_files + " - " + get_vars_local_path( project_name, f.strip()) + "\n" file_content += " vars_files:\n" file_content += real_var_files file_content += " tasks : \n" file_content += real_tasks file_path = create_tmp_task_file(project_name, file_name, file_content) countdown = 0 now = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time())) if timer: execute_date = datetime.datetime.strptime(timer, "%d/%m/%Y %H:%M:%S") if execute_date > datetime.datetime.now(): countdown = (execute_date - datetime.datetime.now()).seconds else: execute_date = datetime.datetime.now() job = Job() job_today_id = job.get_id() job.name = '-'.join(['job', now, job_today_id]) job.created_by = request.user job.project = project job.playbook = file_path job.inventory = get_inventory_local_path(project_name, inventory) job.extra_vars = extra_vars if forks: job.forks = forks if limit: job.limit = limit job.use_sudo = use_sudo if sudo_user: job.sudo_user = sudo_user if sudo_password: job.sudo_password = sudo_password if email != "": job.email = email job.countdown = countdown job.execute_date = execute_date job.save() job_pk = job.pk job.start() return HttpResponseRedirect( urlresolvers.reverse('job_execute_result', kwargs={ 'project_name': project_name, 'job_pk': job_pk })) return render( "ansible/execute_playbook.html", request, { 'request': request, 'project': project, 'package_json': package_json, 'template': template, 'template_pk': template_pk, })