def main(argv=sys.argv): log = logging.getLogger('heat-config') handler = logging.StreamHandler(sys.stderr) handler.setFormatter( logging.Formatter( '[%(asctime)s] (%(name)s) [%(levelname)s] %(message)s')) log.addHandler(handler) log.setLevel('DEBUG') prepare_dir(WORKING_DIR) os.chdir(WORKING_DIR) c = json.load(sys.stdin) opts = salt.config.minion_config(SALT_MINION_CONFIG) opts['file_roots'] = {'base': [WORKING_DIR]} opts['file_client'] = 'local' opts['local'] = 'local' opts['fun'] = 'state.sls' opts['arg'] = [c['id']] for input in c['inputs']: key = input['name'] opts[key] = input.get('value', '') state_file = '%s.sls' % c['id'] config = c.get('config', '') if isinstance(config, dict): yaml_config = yaml.safe_dump(config, default_flow_style=False) else: yaml_config = config fn = os.path.join(WORKING_DIR, state_file) with os.fdopen(os.open(fn, os.O_CREAT | os.O_WRONLY, 0o700), 'w') as f: f.write(yaml_config.encode('utf-8')) caller = salt.cli.caller.Caller.factory(opts) log.debug('Applying Salt state %s' % state_file) stdout, stderr = None, None ret = {} try: ret = caller.call() except SaltInvocationError as err: log.error( 'Salt invocation error while applying Salt sate %s' % state_file) stderr = err if ret: log.info('Results: %s' % ret) output = yaml.safe_dump(ret['return']) # returncode of 0 means there were successfull changes if ret['retcode'] == 0: log.info('Completed applying salt state %s' % state_file) stdout = output else: # Salt doesn't always return sane return codes so we have to check # individual results runfailed = False for state, data in ret['return'].items(): if not data['result']: runfailed = True break if runfailed: log.error('Error applying Salt state %s. [%s]\n' % (state_file, ret['retcode'])) stderr = output else: ret['retcode'] = 0 stdout = output response = {} for output in c.get('outputs', []): output_name = output['name'] response[output_name] = ret.get(output_name) response.update({ 'deploy_stdout': stdout, 'deploy_stderr': stderr, 'deploy_status_code': ret['retcode'], }) json.dump(response, sys.stdout)
import sys parser = argparse.ArgumentParser(description='Check if minions are online.') parser.add_argument('hostname', help='The name of the minion to be checked') args = parser.parse_args() hostname = args.hostname opts = salt.config.minion_config("/etc/salt/minion") opts['doc'] = False opts['grains_run'] = False opts['raw_out'] = False opts['json_out'] = True opts['txt_out'] = False opts['yaml_out'] = False opts['color'] = True opts['root_dir'] = None opts['fun'] = "publish.publish" opts['returner'] = None opts['arg'] = (hostname, "test.ping") caller = salt.cli.caller.Caller(opts) result = caller.call() if result.get("return").get(hostname) is True: sys.stdout.write("OK: minion %s is online\n" % hostname) sys.exit(0) else: sys.stderr.write("CRITICAL: minion %s is not online!\n" % hostname) sys.exit(2)
def main(argv=sys.argv): log = logging.getLogger('heat-config') handler = logging.StreamHandler(sys.stderr) handler.setFormatter( logging.Formatter( '[%(asctime)s] (%(name)s) [%(levelname)s] %(message)s')) log.addHandler(handler) log.setLevel('DEBUG') prepare_dir(WORKING_DIR) os.chdir(WORKING_DIR) c = json.load(sys.stdin) opts = salt.config.minion_config(SALT_MINION_CONFIG) opts['file_roots'] = {'base': [WORKING_DIR]} opts['file_client'] = 'local' opts['local'] = 'local' opts['fun'] = 'state.sls' opts['arg'] = [c['id']] for input in c['inputs']: key = input['name'] opts[key] = input.get('value', '') state_file = '%s.sls' % c['id'] config = c.get('config', '') if isinstance(config, dict): yaml_config = yaml.safe_dump(config, default_flow_style=False) else: yaml_config = config fn = os.path.join(WORKING_DIR, state_file) with os.fdopen(os.open(fn, os.O_CREAT | os.O_WRONLY, 0o700), 'w') as f: f.write(yaml_config) caller = salt.cli.caller.Caller.factory(opts) log.debug('Applying Salt state %s' % state_file) stdout, stderr = None, None ret = {} try: ret = caller.call() except exceptions.SaltInvocationError as err: log.error('Salt invocation error while applying Salt sate %s' % state_file) stderr = err if ret: log.info('Results: %s' % ret) output = yaml.safe_dump(ret['return']) # returncode of 0 means there were successful changes if ret['retcode'] == 0: log.info('Completed applying salt state %s' % state_file) stdout = output else: # Salt doesn't always return sane return codes so we have to check # individual results runfailed = False for state, data in ret['return'].items(): if not data['result']: runfailed = True break if runfailed: log.error('Error applying Salt state %s. [%s]\n' % (state_file, ret['retcode'])) stderr = output else: ret['retcode'] = 0 stdout = output response = {} for output in c.get('outputs', []): output_name = output['name'] response[output_name] = ret.get(output_name) response.update({ 'deploy_stdout': stdout, 'deploy_stderr': stderr, 'deploy_status_code': ret['retcode'], }) json.dump(response, sys.stdout)
parser = argparse.ArgumentParser(description='Check if minions are online.') parser.add_argument('hostname', help='The name of the minion to be checked') args = parser.parse_args() hostname = args.hostname opts = salt.config.minion_config("/etc/salt/minion") opts['doc'] = False opts['grains_run'] = False opts['raw_out'] = False opts['json_out'] = True opts['txt_out'] = False opts['yaml_out'] = False opts['color'] = True opts['root_dir'] = None opts['fun'] = "publish.publish" opts['returner'] = None opts['arg'] = (hostname, "test.ping") caller = salt.cli.caller.Caller(opts) result = caller.call() if result.get("return").get(hostname) is True: sys.stdout.write("OK: minion %s is online\n" % hostname) sys.exit(0) else: sys.stderr.write("CRITICAL: minion %s is not online!\n" % hostname) sys.exit(2)