def not_present(subset=None, show_ipv4=False): ''' Print a list of all minions that are NOT up according to Salt's presence detection (no commands will be sent) subset : None Pass in a CIDR range to filter minions by IP address. show_ipv4 : False Also show the IP address each minion is connecting from. CLI Example: .. code-block:: bash salt-run manage.not_present ''' connected = present(subset=None, show_ipv4=show_ipv4) key = salt.key.get_key(__opts__) keys = key.list_keys() # TODO: Need better way to handle key/node name difference for raet # In raet case node name is '<name>_<kind>' meanwhile the key name # is just '<name>'. So append '_minion' to the name to match. appen_kind = isinstance(key, salt.key.RaetKey) not_connected = [] for minion in keys[key.ACC]: if appen_kind: minion += '_minion' if minion not in connected and (subset is None or minion in subset): not_connected.append(minion) return not_connected
def ready(**kwargs): """ Wait for minions to respond. Compare test.ping results to either the list of all accepted keys or search criteria of cached pillar data. """ settings = { 'timeout': None, 'search': None, 'sleep': 6, 'exception': False } settings.update(kwargs) ret = {} end_time = None if settings['timeout']: end_time = time.time() + settings['timeout'] log.debug("end time: {}".format(end_time)) client = salt.client.LocalClient() while True: try: if settings['search']: results = client.cmd(settings['search'], 'test.ping', timeout=__opts__['timeout'], expr_form="compound") else: results = client.cmd('*', 'test.ping', timeout=__opts__['timeout']) except SaltClientError as client_error: print(client_error) return ret actual = set(results.keys()) if settings['search']: pillar_util = salt.utils.master.MasterPillarUtil(settings['search'], "compound", use_cached_grains=True, grains_fallback=False, opts=__opts__) cached = pillar_util.get_minion_pillar() expected = set(cached.keys()) else: key = salt.key.Key(__opts__) expected = set(key.list_keys()['minions']) if actual == expected: log.warn("All minions are ready") break log.warn("Waiting on {}".format(",".join(list(expected - actual)))) if end_time: if end_time < time.time(): log.warn("Timeout reached") if settings['exception']: raise RuntimeError("Timeout reached. {} seems to be down.".format(",".join(list(expected - actual)))) return False time.sleep(settings['sleep']) return True
def list_not_state(subset=None, show_ip=False): """ .. versionadded:: 2015.8.0 .. versionchanged:: 2019.2.0 Print a list of all minions that are NOT up according to Salt's presence detection (no commands will be sent to minions) subset : None Pass in a CIDR range to filter minions by IP address. show_ip : False Also show the IP address each minion is connecting from. CLI Example: .. code-block:: bash salt-run manage.list_not_state """ connected = list_state(subset=None, show_ip=show_ip) key = salt.key.get_key(__opts__) keys = key.list_keys() not_connected = [] for minion in keys[key.ACC]: if minion not in connected and (subset is None or minion in subset): not_connected.append(minion) return not_connected
def not_present(subset=None, show_ipv4=False): ''' Print a list of all minions that are NOT up according to Salt's presence detection (no commands will be sent) subset : None Pass in a CIDR range to filter minions by IP address. show_ipv4 : False Also show the IP address each minion is connecting from. CLI Example: .. code-block:: bash salt-run manage.not_present ''' ckminions = salt.utils.minions.CkMinions(__opts__) minions = ckminions.connected_ids(show_ipv4=show_ipv4, subset=subset) connected = dict(minions) if show_ipv4 else sorted(minions) key = salt.key.Key(__opts__) keys = key.list_keys() not_connected = [] for minion in keys['minions']: if minion not in connected: not_connected.append(minion) salt.output.display_output(not_connected, '', __opts__) return connected
def not_present(subset=None, show_ipv4=False): ''' Print a list of all minions that are NOT up according to Salt's presence detection (no commands will be sent) subset : None Pass in a CIDR range to filter minions by IP address. show_ipv4 : False Also show the IP address each minion is connecting from. CLI Example: .. code-block:: bash salt-run manage.not_present ''' ckminions = salt.utils.minions.CkMinions(__opts__) minions = ckminions.connected_ids(show_ipv4=show_ipv4, subset=subset) connected = dict(minions) if show_ipv4 else sorted(minions) key = salt.key.Key(__opts__) keys = key.list_keys() not_connected = [] for minion in keys['minions']: if minion not in connected: not_connected.append(minion) return connected
def list_not_state(subset=None, show_ip=False, show_ipv4=None): ''' .. versionadded:: 2015.8.0 .. versionchanged:: 2019.2.0 The 'show_ipv4' argument has been renamed to 'show_ip' as it now includes IPv6 addresses for IPv6-connected minions. Print a list of all minions that are NOT up according to Salt's presence detection (no commands will be sent to minions) subset : None Pass in a CIDR range to filter minions by IP address. show_ip : False Also show the IP address each minion is connecting from. CLI Example: .. code-block:: bash salt-run manage.list_not_state ''' show_ip = _show_ip_migration(show_ip, show_ipv4) connected = list_state(subset=None, show_ip=show_ip) key = salt.key.get_key(__opts__) keys = key.list_keys() not_connected = [] for minion in keys[key.ACC]: if minion not in connected and (subset is None or minion in subset): not_connected.append(minion) return not_connected
def ready(**kwargs): """ Wait for minions to respond. Compare test.ping results to either the list of all accepted keys or search criteria of cached pillar data. """ settings = { 'timeout': None, 'search': None, 'sleep': 6 } settings.update(kwargs) ret = {} #client = salt.client.get_local_client(__opts__['conf_file']) end_time = None if settings['timeout']: end_time = time.time() + settings['timeout'] log.debug("end time: {}".format(end_time)) client = salt.client.LocalClient() while True: try: if settings['search']: results = client.cmd(settings['search'], 'test.ping', timeout=__opts__['timeout'], expr_form="compound") else: results = client.cmd('*', 'test.ping', timeout=__opts__['timeout']) except SaltClientError as client_error: print(client_error) return ret actual = set(results.keys()) if settings['search']: pillar_util = salt.utils.master.MasterPillarUtil(settings['search'], "compound", use_cached_grains=True, grains_fallback=False, opts=__opts__) cached = pillar_util.get_minion_pillar() expected = set(cached.keys()) else: key = salt.key.Key(__opts__) expected = set(key.list_keys()['minions']) if actual == expected: log.warn("All minions are ready") break log.warn("Waiting on {}".format(",".join(list(expected - actual)))) if end_time: if end_time < time.time(): log.warn("Timeout reached") return False time.sleep(settings['sleep']) return True
def status(): client = salt.client.LocalClient(__opts__['conf_file']) minions = client.cmd('*', 'test.ping', timeout=__opts__['timeout']) key = salt.key.Key(__opts__) keys = key.list_keys() ret = {} ret['up'] = sorted(minions) ret['down'] = sorted(set(keys['minions']) - set(minions)) return ret
def accept(self,ip): __opts__ = salt.config.client_config('/etc/salt/master') key = salt.key.Key(__opts__) keys = key.list_keys() minionpres = keys['minions_pre'] if ip in minionpres: key.accept(ip) print "Add "+ip+" sucess!" else: print ip+" is not in minions_pre!"
def listkey(): import salt.config import salt.key __opts__ = salt.config.client_config('/etc/salt/master') key = salt.key.Key(__opts__) keys = key.list_keys() minionpres = keys['minions_pre'] ret = "" for ip in minionpres: ret += ip+"," return ret
def status(): __opts__ = salt.config.client_config('/etc/salt/master') client = salt.client.LocalClient() minions = client.cmd('*', 'test.ping', timeout=30) key = salt.key.Key(__opts__) keys = key.list_keys() ret = {} ret['up'] = sorted(minions) ret['down'] = sorted(set(keys['minions']) - set(minions)) return ret
def delkey(ip): import salt.config import salt.key __opts__ = salt.config.client_config('/etc/salt/master') key = salt.key.Key(__opts__) keys = key.list_keys() minions = keys['minions'] if ip in minions: key.delete_key(ip) msg = "Delete %s sucess!" % ip else: msg = "%s is not in minions!" % ip return msg
def down(): ''' Print a list of all the down or unresponsive salt minions ''' client = salt.client.LocalClient(__opts__['conf_file']) key = salt.key.Key(__opts__) minions = client.cmd('*', 'test.ping', timeout=__opts__['timeout']) keys = key.list_keys() ret = sorted(set(keys['minions'] + keys['minions_pre']) - set(minions)) for minion in ret: print(minion) return ret
def acceptkey(ip): import salt.config import salt.key __opts__ = salt.config.client_config('/etc/salt/master') key = salt.key.Key(__opts__) keys = key.list_keys() minionpres = keys['minions_pre'] if ip in minionpres: key.accept(ip) msg = "Add "+ip+" sucess!" else: msg = ip+" is not in minions_pre!" return msg
def acceptkey(ip): import salt.config import salt.key __opts__ = salt.config.client_config('/etc/salt/master') key = salt.key.Key(__opts__) keys = key.list_keys() minionpres = keys['minions_pre'] if ip in minionpres: key.accept(ip) msg = "Add " + ip + " sucess!" else: msg = ip + " is not in minions_pre!" return msg
def down(): ''' Print a list of all the down or unresponsive salt minions ''' client = salt.client.LocalClient(__opts__['conf_file']) minions = client.cmd('*', 'test.ping', timeout=__opts__['timeout']) key = salt.key.Key(__opts__) keys = key.list_keys() ret = sorted(set(keys['minions']) - set(minions)) for minion in ret: print(minion) return ret
def status(output=True): ''' Print the status of all known salt minions ''' client = salt.client.LocalClient(__opts__['conf_file']) minions = client.cmd('*', 'test.ping', timeout=__opts__['timeout']) key = salt.key.Key(__opts__) keys = key.list_keys() ret = {} ret['up'] = sorted(minions) ret['down'] = sorted(set(keys['minions']) - set(minions)) if output: salt.output.display_output(ret, '', __opts__) return ret
def status(output=True): """ Print the status of all known salt minions """ client = salt.client.LocalClient(__opts__["conf_file"]) minions = client.cmd("*", "test.ping", timeout=__opts__["timeout"]) key = salt.key.Key(__opts__) keys = key.list_keys() ret = {} ret["up"] = sorted(minions) ret["down"] = sorted(set(keys["minions"]) - set(minions)) if output: salt.output.display_output(ret, "", __opts__) return ret
def status(output=True): ''' Print the status of all known salt minions ''' client = salt.client.LocalClient(__opts__['conf_file']) minions = client.cmd('*', 'test.ping', timeout=__opts__['timeout']) key = salt.key.Key(__opts__) keys = key.list_keys() ret = {} ret['up'] = sorted(minions) ret['down'] = sorted(set(keys['minions']) - set(minions)) if output: print(yaml.safe_dump(ret, default_flow_style=False)) return ret
def global_setting(request): SITE_URL = settings.SITE_URL #nodegroups nodegroups = master_opts['nodegroups'] #所有minion分组 group_list = [] for group, group_value in nodegroups.items(): group_list.append(group) #salt-key key = salt.key.Key(master_opts) keys = key.list_keys() #所有已经认证的key(list) minions = keys['minions'] #file_roots file_roots = master_opts['file_roots']['base'][0] + '/' return locals()
def list_not_state(subset=None, show_ip=False, show_ipv4=None, state=None): ''' .. versionadded:: 2015.8.0 .. versionchanged:: Fluorine The 'show_ipv4' argument has been renamed to 'show_ip' as it now includes IPv6 addresses for IPv6-connected minions. Print a list of all minions that are NOT up according to Salt's presence detection (no commands will be sent to minions) subset : None Pass in a CIDR range to filter minions by IP address. show_ip : False Also show the IP address each minion is connecting from. state : 'available' Show minions being in specific state that is one of 'available', 'joined', 'allowed', 'alived' or 'reaped'. CLI Example: .. code-block:: bash salt-run manage.list_not_state ''' show_ip = _show_ip_migration(show_ip, show_ipv4) connected = list_state(subset=None, show_ip=show_ip, state=state) key = salt.key.get_key(__opts__) keys = key.list_keys() # TODO: Need better way to handle key/node name difference for raet # In raet case node name is '<name>_<kind>' meanwhile the key name # is just '<name>'. So append '_minion' to the name to match. appen_kind = isinstance(key, salt.key.RaetKey) not_connected = [] for minion in keys[key.ACC]: if appen_kind: minion += '_minion' if minion not in connected and (subset is None or minion in subset): not_connected.append(minion) return not_connected
def getHosts(self): # first get all the known minions opts = salt.config.master_config(None) key = salt.key.Key(opts) keys = key.list_keys() for minionGroup in keys.keys(): if minionGroup != "minions_rejected": for h in keys[minionGroup]: self.hosts[self._basename(h)] = [True] # now see which hosts ping if not self.client: self.client = salt.client.LocalClient() result = self.client.cmd("*", "test.ping", timeout=self.timeout) if not result: sys.stderr.write("failed to retrieve salt status of hosts\n") return for h in result: self.hosts[self._basename(h)] = ["ping"]
def status(output=True): ''' Print the status of all known salt minions CLI Example: .. code-block:: bash salt-run manage.status ''' client = salt.client.get_local_client(__opts__['conf_file']) minions = client.cmd('*', 'test.ping', timeout=__opts__['timeout']) key = salt.key.Key(__opts__) keys = key.list_keys() ret = {} ret['up'] = sorted(minions) ret['down'] = sorted(set(keys['minions']) - set(minions)) return ret
def list_not_state(subset=None, show_ipv4=False, state=None): """ Print a list of all minions that are NOT up according to Salt's presence detection (no commands will be sent to minions) subset : None Pass in a CIDR range to filter minions by IP address. show_ipv4 : False Also show the IP address each minion is connecting from. state : 'available' Show minions being in specific state that is one of 'available', 'joined', 'allowed', 'alived' or 'reaped'. .. versionadded:: 2015.8.0 CLI Example: .. code-block:: bash salt-run manage.list_not_state """ connected = list_state(subset=None, show_ipv4=show_ipv4, state=state) key = salt.key.get_key(__opts__) keys = key.list_keys() # TODO: Need better way to handle key/node name difference for raet # In raet case node name is '<name>_<kind>' meanwhile the key name # is just '<name>'. So append '_minion' to the name to match. appen_kind = isinstance(key, salt.key.RaetKey) not_connected = [] for minion in keys[key.ACC]: if appen_kind: minion += "_minion" if minion not in connected and (subset is None or minion in subset): not_connected.append(minion) return not_connected
def status(output=True): ''' Print the status of all known salt minions CLI Example: .. code-block:: bash salt-run manage.status ''' client = salt.client.get_local_client(__opts__['conf_file']) minions = client.cmd('*', 'test.ping', timeout=__opts__['timeout']) key = salt.key.Key(__opts__) keys = key.list_keys() ret = {} ret['up'] = sorted(minions) ret['down'] = sorted(set(keys['minions']) - set(minions)) if output: __progress__(ret) return ret
def status(output=True): """ Print the status of all known salt minions CLI Example: .. code-block:: bash salt-run manage.status """ ret = {} client = salt.client.get_local_client(__opts__["conf_file"]) try: minions = client.cmd("*", "test.ping", timeout=__opts__["timeout"]) except SaltClientError as client_error: print(client_error) return ret key = salt.key.Key(__opts__) keys = key.list_keys() ret["up"] = sorted(minions) ret["down"] = sorted(set(keys["minions"]) - set(minions)) return ret
def status(output=True): ''' Print the status of all known salt minions CLI Example: .. code-block:: bash salt-run manage.status ''' ret = {} client = salt.client.get_local_client(__opts__['conf_file']) try: minions = client.cmd('*', 'test.ping', timeout=__opts__['timeout']) except SaltClientError as client_error: print(client_error) return ret key = salt.key.Key(__opts__) keys = key.list_keys() ret['up'] = sorted(minions) ret['down'] = sorted(set(keys['minions']) - set(minions)) return ret
#!/usr/bin/python import salt.config import salt.utils.event import salt.key import salt.output __opts__ = salt.config.client_config('/etc/salt/master') key = salt.key.Key(__opts__) keys = key.list_keys() #print keys minionnum = len(keys['minions']) minionprenum = len(keys['minions_pre']) print minionnum,minionprenum s = "" i = 1 #for m in keys['minions']: #print m #print i,m #s+=m+"," #if i == 1200: # break #i = i + 1 #print s #keycli = salt.key.KeyCLI(__opts__) #print keycli.list_all() #print keycli.list_status("pre") #keycli.reject_all() #print keycli.list_status("pre")
def get_all_keys(master_opts): key = salt.key.Key(master_opts) keys = key.list_keys() return keys.get('minions', [])
#!/usr/bin/python import salt.config import salt.utils.event import salt.key import salt.output __opts__ = salt.config.client_config('/etc/salt/master') key = salt.key.Key(__opts__) keys = key.list_keys() #print keys minionnum = len(keys['minions']) minionprenum = len(keys['minions_pre']) print minionnum, minionprenum s = "" i = 1 #for m in keys['minions']: #print m #print i,m #s+=m+"," #if i == 1200: # break #i = i + 1 #print s #keycli = salt.key.KeyCLI(__opts__) #print keycli.list_all() #print keycli.list_status("pre") #keycli.reject_all() #print keycli.list_status("pre")
def _accepted_ids(self): key = salt.key.Key(config.master_config('/etc/salt/master')) minion_ids = key.list_keys()['minions'] ret = set(minion_ids) log.debug('Minion IDs in Salt key accepted: %s', ret) return ret
def key(): key = salt.key.Key(master_opts) keys = key.list_keys() #所有已经认证的key(list) minions = keys['minions']