#!/usr/bin/env python import os import warnings warnings.filterwarnings("ignore") import salt.client from salt.key import Key master_opts = salt.config.master_config( os.environ.get('SALT_MASTER_CONFIG', '/etc/salt/master')) salt_key = Key(master_opts) local = salt.client.LocalClient() minions = salt_key.list_keys()["minions"] ping_result = local.cmd('*', 'test.ping', timeout=30) for minion in minions: if not ping_result.get(minion): salt_key.delete_key(minion)
def delete_key(minion_id): mymanager = Key(opts) mymanager.delete_key(minion_id) return
old_hostname = sys.argv[1] new_hostname = sys.argv[2].lower() master_opts = salt.config.master_config( os.environ.get('SALT_MASTER_CONFIG', '/etc/salt/master')) salt_key = Key(master_opts) local = salt.client.LocalClient() salt_cmd('saltutil.sync_modules', name=old_hostname, timeout=30) salt_cmd('miscutils.set_hostname', [ new_hostname ], name=old_hostname, timeout=30) for i in xrange(60): minions = salt_key.list_keys().get("minions_pre") if len(minions) > 0 and new_hostname in minions: salt_key.accept(new_hostname) salt_key.delete_key(old_hostname) break sleep(2) #XXX: I won't like this, we have to wait and try twice !? sleep(10) try: salt_cmd('test.ping', name=new_hostname, timeout=60) except: salt_cmd('test.ping', name=new_hostname, timeout=60)