def get_asset_dict(): info = {} sapi = saltapi.SaltAPI(url=SALT_API['url'], username=SALT_API['username'], password=SALT_API['password']) grains_obj = sapi.remote_noarg_execution('*', 'grains.items') for ret in grains_obj: saltid = ret['id'] hostname = ret['host'] os = ret['os'] osversion = ret['osrelease'] mem_total = ret['mem_total'] memory = int(mem_total) / 1024 cpu_model = ret['cpu_model'] cpu_core = ret['num_cpus'] disk = ret['capacity'] kernel = ret['kernel'] iptmp = ret['ip4_interfaces'] if ret['kernel'] == 'Linux': iptmp.pop('lo') lan_ip = iptmp.get('eth0')[0] else: iptmp2 = iptmp.get('Red Hat VirtIO Ethernet Adapter #2') lan_ip = iptmp2[0] wan_ip = ret['wan'] info[saltid] = [ hostname, osversion, os, memory, cpu_model, cpu_core, disk, kernel, lan_ip, wan_ip ] return info
def salt_command(request): if request.method == 'POST': a = request.POST.get('host') b = request.POST.get('command') c = request.POST.get('arg') sapi = saltapi.SaltAPI(url=SALT_API['url'], username=SALT_API['username'], password=SALT_API['password']) result = sapi.remote_execution(a, b, c) print(result) return render(request, 'deploy/salt_command.html', {'result': result}) return render(request, 'deploy/salt_command.html')
def salt_key_delete(request): ret = {'status': True} try: salt_id = request.POST.get('id') salt_name = request.POST.get('username') sapi = saltapi.SaltAPI(url=SALT_API['url'], username=SALT_API['username'], password=SALT_API['password']) sapi.delete_key(salt_name) minion_obj = SaltMinion.objects.get(id=salt_id, name=salt_name).delete() except Exception as e: ret['status'] = False return HttpResponse(json.dumps(ret))
def salt_key_update(request): ret = {'status': True} try: sapi = saltapi.SaltAPI(url=SALT_API['url'], username=SALT_API['username'], password=SALT_API['password']) minions, minions_pre = sapi.list_all_key() for accept_key in minions: SaltMinion.objects.get_or_create(name=accept_key, status=1) for deny_key in minions_pre: SaltMinion.objects.get_or_create(name=deny_key, status=2) except Exception as e: ret['status'] = False return HttpResponse(json.dumps(ret))