Пример #1
0
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
Пример #2
0
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))
Пример #3
0
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
Пример #4
0
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
Пример #5
0
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()
Пример #6
0
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
Пример #7
0
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
Пример #8
0
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
Пример #9
0
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
Пример #10
0
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()
Пример #11
0
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
Пример #12
0
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
Пример #13
0
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
Пример #14
0
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
Пример #15
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