def get_minions(): token = get_token() data = {"fun": "key.list_all"} salt_api_object = SaltAPI(data, SALT_REST_URL, token) minions = salt_api_object.wheelrun( )["return"][0]["data"]["return"]["minions"] return minions
def ecs_init(tgt, func): data = {"client": "local", "tgt": tgt, "fun": "state.sls", "arg": func} token = get_token() salt_api_object = SaltAPI(data, SALT_REST_URL, token) res = salt_api_object.cmdrun() redisconn = RedisConn(REDIS_HOST, REDIS_PORT) key = "{}_{}".format(tgt, func) redisconn.set_key(key, json.dumps(res))
def get_minions(): data = { "tgt": "*", "fun": "key.list_all", } token = get_token() salt_api_object = SaltAPI(data, SALT_REST_URL, token) res = salt_api_object.wheelrun()["return"][0]["data"]["return"]["minions"] return res
def execute_script(tgt, script_name, args=[]): data = { "tgt": tgt, "fun": "cmd.script", "arg": ["salt://scripts/{}".format(script_name), " ".join(args)] } token = get_token() salt_api_object = SaltAPI(data, SALT_REST_URL, token) res = salt_api_object.cmdrun() return res
def scan_ecshost(minion_id): token = get_token() data = {"tgt": minion_id, "fun": "grains.items"} salt_api_object = SaltAPI(data, SALT_REST_URL, token) host_res = salt_api_object.cmdrun()["return"][0] ecshost = EcsHost.objects.get(minion_id=minion_id) ecshost.hostname = host_res[minion_id]["nodename"] ecshost.cpu_nums = host_res[minion_id]["num_cpus"] ecshost.memory = host_res[minion_id]["mem_total"] ecshost.save()
def update_host_cluster(hostname): token = get_token() data = { "tgt": hostname, "fun": "cmd.script", "arg": GET_JAVA_PROCESS_SCRIPT } salt_api_object = SaltAPI(data, SALT_REST_URL, token) modules = salt_api_object.cmdrun()["return"][0][hostname]["stdout"] module_list = [m.strip(".jar") for m in modules.split('\n')] return module_list
def reload_bind(): token = get_token(DNS_SERVER) data = { "client": "local", "tgt": DNS_SERVER, "fun": "cmd.run", "arg": "systemctl restart named" } salt_api_obj = SaltAPI(data, SALT_REST_URL, token) res = salt_api_obj.cmdrun() return res
def comment_domainname(regex_str): token = get_token(DNS_SERVER) data = { "client": "local", "tgt": DNS_SERVER, "fun": "file.comment", "arg": [DNS_CONF_FILE, regex_str] } salt_api_obj = SaltAPI(data, SALT_REST_URL, token) res = salt_api_obj.cmdrun() return res
def sed_domainname(srcstr, deststr): token = get_token(DNS_SERVER) data = { "client": "local", "tgt": DNS_SERVER, "fun": "file.sed", "arg": [DNS_CONF_FILE, srcstr, deststr] } salt_api_obj = SaltAPI(data, SALT_REST_URL, token) res = salt_api_obj.cmdrun() return res
def update_host_info(hostname): token = get_token() data = {"tgt": hostname, "fun": "grains.items"} salt_api_object = SaltAPI(data, SALT_REST_URL, token) host_res = salt_api_object.cmdrun()["return"][0] host = Host.objects.get(host_name=hostname) host.kernel = host_res[hostname]["kernelrelease"] host.osrelease = host_res[hostname]["osrelease"] host.os = host_res[hostname]["kernel"] host.cpu_nums = host_res[hostname]["num_cpus"] host.memory = host_res[hostname]["mem_total"] host.host_type = host_res[hostname]["virtual"] host.save()
def append_domainname(dns, domainname_type, ip_address): token = get_token(DNS_SERVER) dns_record = "{} IN {} {}".format(dns, domainname_type, ip_address) data = { "client": "local", "tgt": DNS_SERVER, "fun": "file.append", "arg": [DNS_CONF_FILE, dns_record], } salt_api_obj = SaltAPI(data, SALT_REST_URL, token) res = salt_api_obj.cmdrun() return res
def run_state(tgt, expr_form, midware): data = { "client": "local", "tgt": tgt, "fun": "state.sls", "arg": "{}".format(midware), "expr_form": expr_form, "kwargs": { "test": "True" } } token = get_token(tgt) salt_api_obj = SaltAPI(data, SALT_REST_URL, token) res = salt_api_obj.cmdrun() return res
def transfer_script(tgt, script_dir, script_name): data = { "tgt": tgt, "fun": "cp.get_file", "arg": [ "salt://scripts/{}".format(script_name), "{}/{}".format(script_dir, script_name) ], "expr_form": "list" } token = get_token() salt_api_object = SaltAPI(data, SALT_REST_URL, token) res = salt_api_object.cmdrun()["return"][0] return res
def update_host_ip(hostname): token = get_token() data = {"tgt": hostname, "fun": "grains.item", "arg": "ip4_interfaces"} minions = get_minions() if hostname in minions: salt_api_object = SaltAPI(data, SALT_REST_URL, token) ip_info_dict = salt_api_object.cmdrun( )["return"][0][hostname]["ip4_interfaces"] for k, v in ip_info_dict.items(): if k == "br0" or k == "eth0": if len(v) != 0: ip_info = v[0] gateway = "{}.1".format(".".join(ip_info.split(".")[0:3])) ip_segment = "{}.0/24".format(".".join(ip_info.split(".")[0:3])) ippool = IpPool.objects.filter(ip_address=ip_info) if not ippool.exists(): IpPool.objects.create(ip_address=ip_info, ip_type="local", gateway=gateway, ip_segment=ip_segment, host=Host.objects.get(host_name=hostname)) return 1 return 0
def get_jvm_pid(tgt): token = get_token() data = {"tgt": tgt, "fun": "cmd.script", "arg": GET_JAVA_PROCESS_SCRIPT} salt_api_object = SaltAPI(data, SALT_REST_URL, token) res = salt_api_object.cmdrun()["return"][0][tgt]["stdout"].split("\n") return res