Teach Saltstack Web Management 1 (Module Management)

Write in the title: I touched Puppet in 2011 and lamented that Americans have a higher understanding of automation than we are.In 2015, I touched Saltstack and exclaimed again.Saltstack didn't have Web management like Puppet Foreman at that time, and since then I've had a small goal, I want to copy Foreman...

The ####Saltstack Web is divided into several pieces, which are illustrated below in comparison to Puppet Foreman:






##1. Environment

  • Master and traversal are on the same server (Saltstack has no corresponding API to remotely operate models, not studied yet)
  • Django Run Version 1.6.1
  • Python Run Version 2.7.1

##2. Eating process

Idea: Write the corresponding module in the altstack master, put it in the corresponding directory according to a certain format, write the class name, path, sls name into Mysql through traversal, and then show it through the Web.First, take out the list of all local modules to form a list of local modules.The number of local module lists is then compared with the number of module lists stored in the remote database.More local, more remote database, less local, Delete remote database.
####1. Import module code as follows (adjust directory as appropriate)

def salt_model_import(request):
    if request.method == 'POST':
        os_list = []
        add_count = 0
        del_count = 0
        try:
            for i in os.listdir('/srv/salt/'):
                """"Traverse through a first-level directory to determine if it is a directory"""
                if os.path.isdir('/srv/salt/%s/'%i):
                    for ii in os.listdir('/srv/salt/%s/'%i):
                        """"Traverse the secondary directory to determine if the directory is a file and is sls Ending"""
                        if os.path.isfile('/srv/salt/%s/%s' %(i,ii)) and str('/srv/salt/%s/%s' %(i,ii)).endswith('sls'):
                            os_list.append('%s.%s' %(i,ii.split('.')[0]))
                else:
                    pass
            for i in os.listdir('/srv/salt/'):
                """"Traverse through a first-level directory to determine if it is a directory"""
                if os.path.isdir('/srv/salt/%s/'%i):
                    for ii in os.listdir('/srv/salt/%s/'%i):
                        """"Traverse the secondary directory to determine if the directory is a file and is sls Ending"""
                        if os.path.isfile('/srv/salt/%s/%s' %(i,ii)) and str('/srv/salt/%s/%s' %(i,ii)).endswith('sls'):
                            """Pair against database content"""
                            if len(os_list) > Salt_model.objects.all().count():
                                if Salt_model.objects.filter(salt_model_name=i, salt_model_sls_name=ii, salt_model_dir='/srv/salt/%s/%s' %(i,ii)):
                                    pass
                                else:
                                    Salt_model(salt_model_name=i, salt_model_sls_name=ii, salt_model_dir='/srv/salt/%s/%s' %(i,ii)).save()
                                    add_count = add_count + 1
                            elif len(os_list) < Salt_model.objects.all().count():
                                for i in Salt_model.objects.all():
                                    if '%s.%s'%(i.salt_model_name,i.salt_model_sls_name.split('.')[0]) not in os_list:
                                        Salt_model.objects.filter(salt_model_name=i.salt_model_name, salt_model_sls_name=i.salt_model_sls_name).delete()
                                        del_count = del_count + 1
                            else:
                                pass
                        else:
                            pass
                else:
                    pass
            return HttpResponse('{"status":1,"add_action":%s,"del_action":%s}' % (add_count, del_count))
        except Exception as err:
            return HttpResponse('{"status":0,"err":%s}' % err)
    else:
        return HttpResponse('{"status":0}')

The data results are stored as follows:

####2. The module group code is as follows (adjust the directory to suit your needs)

Idea: The database already stores the corresponding module names and paths, so we just need to use the concept of module groups to let the target host execute the corresponding modules one by one through State.sls.

def salt_models_group_edit(request):
    if request.method == 'GET':
        edit_group_id = request.GET['edit_group_id']
        salt_models_group = Salt_models_group.objects.get(id = edit_group_id)
        salt_models_group_member_list = salt_models_group.salt_models_group_member.split(',')
        salt_model = Salt_model.objects.all().order_by('salt_model_name')
        salt_model_list = []
        for i in salt_model:
            salt_model_list.append('%s.%s'%(i.salt_model_name,i.salt_model_sls_name.split(".")[0]))
        list = []
        for ii in salt_model_list:
            if ii in salt_models_group_member_list:
                list.append('<option selected>%s</option>' % str(ii))
            else:
                list.append('<option>%s</option>' % str(ii))
        return render_to_response("mysalt/salt_models_group_edit.html",{'list':list},context_instance=RequestContext(request))
    else:
        edit_group_id = request.META['QUERY_STRING'].split('=')[-1]
        salt_models_select_list = request.POST.getlist('salt_models_select',[])
        salt_models_group = Salt_models_group.objects.get(id = edit_group_id)
        salt_models_group_member_list = salt_models_group.salt_models_group_member.split(',')
        for i in salt_models_select_list:
            if i not in salt_models_group_member_list:
                salt_models_group_member_list.append(i)
        for i in salt_models_group_member_list:
            if i not in salt_models_select_list:
                salt_models_group_member_list.remove(i)
        Salt_models_group.objects.filter(id=edit_group_id).update(salt_models_group_member=(','.join(salt_models_group_member_list)).lstrip(','))
        return HttpResponseRedirect('/cmdb/salt_models_group/')

def salt_models_group_add (request):
    """1 To add success, 0 is unsuccessful"""
    if request.method == 'POST':       
        salt_models_group_name = request.POST['salt_models_group_name']
        Salt_models_group(salt_models_group_name = salt_models_group_name).save()
        return HttpResponse('{"status":1}')
    else:
        return HttpResponse('{"status":0}')

def salt_models_group_del (request):
    """1 To add success, 0 is unsuccessful"""
    if 'salt_models_group' in request.GET:
        salt_models_group = request.GET['salt_models_group']
        Salt_models_group.objects.filter(salt_models_group_name = salt_models_group).delete()
        return HttpResponse('{"status":1}')
    else:
        return HttpResponse('{"status":0}')

The data results are stored as follows:

Fifteen original articles were published. Approved 0. Visits 102
Private letter follow

Tags: Database saltstack Django Python

Posted on Wed, 15 Jan 2020 17:44:33 -0800 by hoyo