def test_load_master_config_from_environ_var(self): original_environ = os.environ.copy() tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR) try: env_root_dir = os.path.join(tempdir, "foo", "env") os.makedirs(env_root_dir) env_fpath = os.path.join(env_root_dir, "config-env") salt.utils.fopen(env_fpath, "w").write("root_dir: {0}\n" "log_file: {1}\n".format(env_root_dir, env_fpath)) os.environ["SALT_MASTER_CONFIG"] = env_fpath # Should load from env variable, not the default configuration file. config = sconfig.master_config("/etc/salt/master") self.assertEqual(config["log_file"], env_fpath) os.environ.clear() os.environ.update(original_environ) root_dir = os.path.join(tempdir, "foo", "bar") os.makedirs(root_dir) fpath = os.path.join(root_dir, "config") salt.utils.fopen(fpath, "w").write("root_dir: {0}\n" "log_file: {1}\n".format(root_dir, fpath)) # Let's set the environment variable, yet, since the configuration # file path is not the default one, ie, the user has passed an # alternative configuration file form the CLI parser, the # environment variable will be ignored. os.environ["SALT_MASTER_CONFIG"] = env_fpath config = sconfig.master_config(fpath) self.assertEqual(config["log_file"], fpath) os.environ.clear() os.environ.update(original_environ) finally: if os.path.isdir(tempdir): shutil.rmtree(tempdir)
def _get_local_grains(): """ Return the salt grains for this host that we are running on. If we support SELinux in the future this may need to be moved into a cthulhu RPC as the apache worker may not have the right capabilities to query all the grains. """ # Stash grains as an attribute of this function if not hasattr(_get_local_grains, 'grains'): # >> work around salt issue #11402 import __main__ as main main.__file__ = 'workaround' # << # Use salt to get an interesting subset of the salt grains (getting # everything is a bit slow) grains = {} c = master_config(config.get('cthulhu', 'salt_config_path')) l = _create_loader(c, 'grains', 'grain') funcs = l.gen_functions() for key in [k for k in funcs.keys() if k.startswith('core.')]: ret = funcs[key]() if isinstance(ret, dict): grains.update(ret) _get_local_grains.grains = grains else: grains = _get_local_grains.grains return grains
def test_issue_6714_parsing_errors_logged(self): try: tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR) test_config = os.path.join(tempdir, 'config') # Let's populate a master configuration file with some basic # settings salt.utils.fopen(test_config, 'w').write( 'root_dir: {0}\n' 'log_file: {0}/foo.log\n'.format(tempdir) + '\n\n\n' 'blah:false\n' ) with TestsLoggingHandler() as handler: # Let's load the configuration config = sconfig.master_config(test_config) for message in handler.messages: if message.startswith('ERROR:Error parsing configuration'): break else: raise AssertionError( 'No parsing error message was logged' ) finally: if os.path.isdir(tempdir): shutil.rmtree(tempdir)
def test_master_confd_inclusion(self): try: tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR) master_config = os.path.join(tempdir, 'master') master_confd = os.path.join(tempdir, 'master.d') os.makedirs(master_confd) # Let's populate a master configuration file with some basic # settings salt.utils.fopen(master_config, 'w').write('blah: false\n' 'root_dir: {0}\n' 'log_file: {1}\n'.format( tempdir, master_config)) # Now, let's populate an extra configuration file under master.d # Notice that above we've set blah as False and bellow as True. # Since the master.d files are loaded after the main configuration # file so overrides can happen, the final value of blah should be # True. extra_config = os.path.join(master_confd, 'extra.conf') salt.utils.fopen(extra_config, 'w').write('blah: true\n') # Let's load the configuration config = sconfig.master_config(master_config) self.assertEqual(config['log_file'], master_config) # As proven by the assertion below, blah is True self.assertTrue(config['blah']) finally: if os.path.isdir(tempdir): shutil.rmtree(tempdir)
def test_proper_path_joining(self): fpath = tempfile.mktemp() temp_config = 'root_dir: /\n'\ 'key_logfile: key\n' if salt.utils.is_windows(): temp_config = 'root_dir: c:\\\n'\ 'key_logfile: key\n' try: with salt.utils.fopen(fpath, 'w') as fp_: fp_.write(temp_config) config = sconfig.master_config(fpath) expect_path_join = os.path.join('/', 'key') expect_sep_join = '//key' if salt.utils.is_windows(): expect_path_join = os.path.join('c:\\', 'key') expect_sep_join = 'c:\\\\key' # os.path.join behavior self.assertEqual(config['key_logfile'], expect_path_join) # os.sep.join behavior self.assertNotEqual(config['key_logfile'], expect_sep_join) finally: if os.path.isfile(fpath): os.unlink(fpath)
def test_master_confd_inclusion(self): try: tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR) master_config = os.path.join(tempdir, 'master') master_confd = os.path.join(tempdir, 'master.d') os.makedirs(master_confd) # Let's populate a master configuration file with some basic # settings salt.utils.fopen(master_config, 'w').write( 'blah: false\n' 'root_dir: {0}\n' 'log_file: {1}\n'.format(tempdir, master_config) ) # Now, let's populate an extra configuration file under master.d # Notice that above we've set blah as False and bellow as True. # Since the master.d files are loaded after the main configuration # file so overrides can happen, the final value of blah should be # True. extra_config = os.path.join(master_confd, 'extra.conf') salt.utils.fopen(extra_config, 'w').write( 'blah: true\n' ) # Let's load the configuration config = sconfig.master_config(master_config) self.assertEqual(config['log_file'], master_config) # As proven by the assertion below, blah is True self.assertTrue(config['blah']) finally: if os.path.isdir(tempdir): shutil.rmtree(tempdir)
def setup_config(self): opts = config.master_config(self.get_config_file_path('master')) user = opts.get('user', 'root') opts['_minion_conf_file'] = opts['conf_file'] opts.update(config.minion_config(self.get_config_file_path('minion'))) # Over ride the user from the master config file opts['user'] = user if 'syndic_master' not in opts: self.error( "The syndic_master needs to be configured in the salt master " "config, EXITING!" ) from salt import utils # Some of the opts need to be changed to match the needed opts # in the minion class. opts['master'] = opts['syndic_master'] opts['master_ip'] = utils.dns_check(opts['master']) opts['master_uri'] = 'tcp://{0}:{1}'.format( opts['master_ip'], str(opts['master_port']) ) opts['_master_conf_file'] = opts['conf_file'] opts.pop('conf_file') return opts
def test_master_pillar_roots_glob(self): # Config file and stub pillar_roots. fpath = tempfile.mktemp() tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR) try: # Create some kown files. for f in 'abc': fpath = os.path.join(tempdir, f) salt.utils.fopen(fpath, 'w').write(f) salt.utils.fopen(fpath, 'w').write('pillar_roots:\n' ' base:\n' ' - {0}'.format( os.path.join(tempdir, '*'))) config = sconfig.master_config(fpath) base = config['pillar_roots']['base'] self.assertEqual( set(base), set([ os.path.join(tempdir, 'a'), os.path.join(tempdir, 'b'), os.path.join(tempdir, 'c') ])) finally: if os.path.isfile(fpath): os.unlink(fpath) if os.path.isdir(tempdir): shutil.rmtree(tempdir)
def __init__(self,main_config): self.opts=master_config(main_config) self.wheel=Wheel(self.opts) self.client=LocalClient() self.connected_minions_list=self.wheel.call_func('minions.connected') self.key_dict=self.wheel.call_func('key.list_all') self.total=len(self.key_dict['minions'])+len(self.key_dict['minions_pre'])+len(self.key_dict['minions_denied'])+len(self.key_dict['minions_rejected'])
def _master_opts(cfg='master'): cfg = os.environ.get( 'SALT_MASTER_CONFIG', __opts__.get('conf_file', os.path.join(salt.syspaths.CONFIG_DIR, cfg))) opts = config.master_config(cfg) return opts
def get_pillar(minion='*', skipped=None, saltenv='base', **kwargs): ''' Returns the compiled pillar either of a specific minion or just the global available pillars. This function assumes that no minion has the id ``*``. ''' _o = __opts__ try: fic = _o['config_dir'] + '/master' os.stat(fic) # may raise OSError mopts = config.master_config(fic) except (KeyError, OSError): # may not have master config in masterless setups mopts = copy.deepcopy(_o) id_, grains, _ = salt.utils.minions.get_minion_data(minion, mopts) if not grains: grains = {} grains = copy.deepcopy(grains) did = {'fqdn': minion, 'id': minion} # for d in [grains, mopts]: for d in [grains]: d.update(did) pillar = salt.pillar.Pillar(mopts, grains, id_, saltenv) if not skipped: skipped = ['master'] compiled_pillar = pillar.compile_pillar() for k in skipped: compiled_pillar.pop(k, None) return compiled_pillar
def _master_opts(cfg='master'): if 'conf_file' in __opts__: default_dir = os.path.dirname(__opts__['conf_file']) else: default_dir = __opts__['config_dir'], cfg = os.environ.get('SALT_MASTER_CONFIG', os.path.join(default_dir, cfg)) opts = config.master_config(cfg) return opts
def _master_opts(cfgdir=None, cfg=None): if not cfgdir: cfgdir = salt.syspaths.CONFIG_DIR if not cfg: cfg = os.environ.get('SALT_MASTER_CONFIG', 'master') cfg = os.path.join(cfgdir, cfg) opts = config.master_config(cfg) return opts
def test_load_client_config_from_environ_var(self): original_environ = os.environ.copy() try: tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR) env_root_dir = os.path.join(tempdir, 'foo', 'env') os.makedirs(env_root_dir) # Let's populate a master configuration file which should not get # picked up since the client configuration tries to load the master # configuration settings using the provided client configuration # file master_config = os.path.join(env_root_dir, 'master') with salt.utils.fopen(master_config, 'w') as fp_: fp_.write( 'blah: true\n' 'root_dir: {0}\n' 'log_file: {1}\n'.format(env_root_dir, master_config) ) os.environ['SALT_MASTER_CONFIG'] = master_config # Now the client configuration file env_fpath = os.path.join(env_root_dir, 'config-env') with salt.utils.fopen(env_fpath, 'w') as fp_: fp_.write( 'root_dir: {0}\n' 'log_file: {1}\n'.format(env_root_dir, env_fpath) ) os.environ['SALT_CLIENT_CONFIG'] = env_fpath # Should load from env variable, not the default configuration file config = sconfig.client_config(os.path.expanduser('~/.salt')) self.assertEqual(config['log_file'], env_fpath) self.assertTrue('blah' not in config) os.environ.clear() os.environ.update(original_environ) root_dir = os.path.join(tempdir, 'foo', 'bar') os.makedirs(root_dir) fpath = os.path.join(root_dir, 'config') with salt.utils.fopen(fpath, 'w') as fp_: fp_.write( 'root_dir: {0}\n' 'log_file: {1}\n'.format(root_dir, fpath) ) # Let's set the environment variable, yet, since the configuration # file path is not the default one, i.e., the user has passed an # alternative configuration file form the CLI parser, the # environment variable will be ignored. os.environ['SALT_MASTER_CONFIG'] = env_fpath config = sconfig.master_config(fpath) self.assertEqual(config['log_file'], fpath) os.environ.clear() os.environ.update(original_environ) finally: if os.path.isdir(tempdir): shutil.rmtree(tempdir)
def test_proper_path_joining(self): fpath = tempfile.mktemp() salt.utils.fopen(fpath, 'w').write("root_dir: /\n" "key_logfile: key\n") config = sconfig.master_config(fpath) # os.path.join behaviour self.assertEqual(config['key_logfile'], os.path.join('/', 'key')) # os.sep.join behaviour self.assertNotEqual(config['key_logfile'], '//key')
def setup_config(self): keys_config = config.master_config(self.get_config_file_path('master')) if self.options.gen_keys: # Since we're generating the keys, some defaults can be assumed # or tweaked keys_config['key_logfile'] = os.devnull keys_config['pki_dir'] = self.options.gen_keys_dir return keys_config
def _master_opts(cfg='master'): if 'conf_file' in __opts__: default_dir = os.path.dirname(__opts__['conf_file']) else: default_dir = __opts__['config_dir'], cfg = os.environ.get( 'SALT_MASTER_CONFIG', os.path.join(default_dir, cfg)) opts = config.master_config(cfg) return opts
def _master_opts(cfg="master"): if "conf_file" in __opts__: default_dir = os.path.dirname(__opts__["conf_file"]) else: default_dir = (__opts__["config_dir"], ) cfg = os.environ.get("SALT_MASTER_CONFIG", os.path.join(default_dir, cfg)) opts = config.master_config(cfg) opts["output"] = "quiet" return opts
def test_load_client_config_from_environ_var(self): original_environ = os.environ.copy() try: tempdir = tempfile.mkdtemp(dir=TMP) env_root_dir = os.path.join(tempdir, 'foo', 'env') os.makedirs(env_root_dir) # Let's populate a master configuration file which should not get # picked up since the client configuration tries to load the master # configuration settings using the provided client configuration # file master_config = os.path.join(env_root_dir, 'master') with salt.utils.fopen(master_config, 'w') as fp_: fp_.write( 'blah: true\n' 'root_dir: {0}\n' 'log_file: {1}\n'.format(env_root_dir, master_config) ) os.environ['SALT_MASTER_CONFIG'] = master_config # Now the client configuration file env_fpath = os.path.join(env_root_dir, 'config-env') with salt.utils.fopen(env_fpath, 'w') as fp_: fp_.write( 'root_dir: {0}\n' 'log_file: {1}\n'.format(env_root_dir, env_fpath) ) os.environ['SALT_CLIENT_CONFIG'] = env_fpath # Should load from env variable, not the default configuration file config = sconfig.client_config(os.path.expanduser('~/.salt')) self.assertEqual(config['log_file'], env_fpath) self.assertTrue('blah' not in config) os.environ.clear() os.environ.update(original_environ) root_dir = os.path.join(tempdir, 'foo', 'bar') os.makedirs(root_dir) fpath = os.path.join(root_dir, 'config') with salt.utils.fopen(fpath, 'w') as fp_: fp_.write( 'root_dir: {0}\n' 'log_file: {1}\n'.format(root_dir, fpath) ) # Let's set the environment variable, yet, since the configuration # file path is not the default one, i.e., the user has passed an # alternative configuration file form the CLI parser, the # environment variable will be ignored. os.environ['SALT_MASTER_CONFIG'] = env_fpath config = sconfig.master_config(fpath) self.assertEqual(config['log_file'], fpath) os.environ.clear() os.environ.update(original_environ) finally: if os.path.isdir(tempdir): shutil.rmtree(tempdir)
def setUp(self): master_config_path = self.get_config_file_path('master') master_config = config.master_config(master_config_path) if not os.path.exists(master_config['cachedir']): os.makedirs(master_config['cachedir']) if not os.path.exists(integration.TMP_CONF_DIR): os.makedirs(integration.TMP_CONF_DIR) self.local_client = client.LocalClient(mopts=master_config)
def test_load_master_config_from_environ_var(self): original_environ = os.environ.copy() tempdir = tempfile.mkdtemp(dir=TMP) try: env_root_dir = os.path.join(tempdir, 'foo', 'env') os.makedirs(env_root_dir) env_fpath = os.path.join(env_root_dir, 'config-env') with salt.utils.fopen(env_fpath, 'w') as fp_: fp_.write( 'root_dir: {0}\n' 'log_file: {1}\n'.format(env_root_dir, env_fpath) ) os.environ['SALT_MASTER_CONFIG'] = env_fpath # Should load from env variable, not the default configuration file. config = sconfig.master_config('{0}/master'.format(CONFIG_DIR)) self.assertEqual(config['log_file'], env_fpath) os.environ.clear() os.environ.update(original_environ) root_dir = os.path.join(tempdir, 'foo', 'bar') os.makedirs(root_dir) fpath = os.path.join(root_dir, 'config') with salt.utils.fopen(fpath, 'w') as fp_: fp_.write( 'root_dir: {0}\n' 'log_file: {1}\n'.format(root_dir, fpath) ) # Let's set the environment variable, yet, since the configuration # file path is not the default one, i.e., the user has passed an # alternative configuration file form the CLI parser, the # environment variable will be ignored. os.environ['SALT_MASTER_CONFIG'] = env_fpath config = sconfig.master_config(fpath) self.assertEqual(config['log_file'], fpath) os.environ.clear() os.environ.update(original_environ) finally: if os.path.isdir(tempdir): shutil.rmtree(tempdir)
def test_load_master_config_from_environ_var(self): original_environ = os.environ.copy() tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR) try: env_root_dir = os.path.join(tempdir, 'foo', 'env') os.makedirs(env_root_dir) env_fpath = os.path.join(env_root_dir, 'config-env') with salt.utils.fopen(env_fpath, 'w') as fp_: fp_.write( 'root_dir: {0}\n' 'log_file: {1}\n'.format(env_root_dir, env_fpath) ) os.environ['SALT_MASTER_CONFIG'] = env_fpath # Should load from env variable, not the default configuration file. config = sconfig.master_config('/etc/salt/master') self.assertEqual(config['log_file'], env_fpath) os.environ.clear() os.environ.update(original_environ) root_dir = os.path.join(tempdir, 'foo', 'bar') os.makedirs(root_dir) fpath = os.path.join(root_dir, 'config') with salt.utils.fopen(fpath, 'w') as fp_: fp_.write( 'root_dir: {0}\n' 'log_file: {1}\n'.format(root_dir, fpath) ) # Let's set the environment variable, yet, since the configuration # file path is not the default one, i.e., the user has passed an # alternative configuration file form the CLI parser, the # environment variable will be ignored. os.environ['SALT_MASTER_CONFIG'] = env_fpath config = sconfig.master_config(fpath) self.assertEqual(config['log_file'], fpath) os.environ.clear() os.environ.update(original_environ) finally: if os.path.isdir(tempdir): shutil.rmtree(tempdir)
def test_sha256_is_default_for_master(self): fpath = tempfile.mktemp() try: salt.utils.fopen(fpath, 'w').write("root_dir: /\n" "key_logfile: key\n") config = sconfig.master_config(fpath) self.assertEqual(config['hash_type'], 'sha256') finally: if os.path.isfile(fpath): os.unlink(fpath)
def enable(container): with open('/tmpwat', 'w') as what: print('what', file=what) config = sac.cloud_config('/etc/salt/cloud') master = sac.master_config('/etc/salt/master') api_key = config['providers']['my-nova']['nova']['api_key'] ident = config['providers']['my-nova']['nova']['identity_url'] tenant = config['providers']['my-nova']['nova']['tenant'] user = config['providers']['my-nova']['nova']['user'] region = config['providers']['my-nova']['nova']['compute_region'] headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } payload={ "auth": { "RAX-KSKEY:apiKeyCredentials": { "username": user, "apiKey": api_key } } } identity = requests.post(os.path.join(ident, 'tokens'), data=json.dumps(payload), headers=headers).json().get('access', {}) headers['X-Auth-Token'] = identity['token']['id'] catalog = identity['serviceCatalog'] endpoint = _get_endpoint('object-store', region, catalog) cdnendpoint = _get_endpoint('rax:object-cdn', region, catalog) containers = requests.get(endpoint, headers=headers).json() cont = None for c in containers: if c['name'] == container: cont = c log.debug('Container: \n{0}'.format(pprint.pformat(cont))) log.debug('Container: \n{0}'.format(pprint.pformat(headers))) log.debug('Container: \n{0}'.format(pprint.pformat(endpoint))) break if cont is None: log.debug("Container doesn't exist: {0}".format(container)) return False headers['X-Ttl'] = 900 headers['X-Cdn-Enabled'] = 'True' ret = requests.put('{0}/{1}'.format(cdnendpoint, container), headers=headers) if ret.status_code == 201: return True elif ret.status_code == 202: log.debug('Container already enabled: {0}'.format(container)) return None else: log.debug('Failed to enable container: {0}'.format(container)) return False
def test_common_prefix_stripping(self): tempdir = tempfile.mkdtemp() root_dir = os.path.join(tempdir, 'foo', 'bar') os.makedirs(root_dir) fpath = os.path.join(root_dir, 'config') salt.utils.fopen(fpath, 'w').write('root_dir: {0}\n' 'log_file: {1}\n'.format(root_dir, fpath)) config = sconfig.master_config(fpath) self.assertEqual(config['log_file'], fpath) shutil.rmtree(tempdir)
def test_proper_path_joining(self): fpath = tempfile.mktemp() open(fpath, 'w').write( "root_dir: /\n" "key_logfile: key\n" ) config = sconfig.master_config(fpath) # os.path.join behaviour self.assertEqual(config['key_logfile'], os.path.join('/', 'key')) # os.sep.join behaviour self.assertNotEqual(config['key_logfile'], '//key')
def test_sha256_is_default_for_master(self): fpath = tempfile.mktemp() try: salt.utils.fopen(fpath, 'w').write( "root_dir: /\n" "key_logfile: key\n" ) config = sconfig.master_config(fpath) self.assertEqual(config['hash_type'], 'sha256') finally: if os.path.isfile(fpath): os.unlink(fpath)
def test_proper_path_joining(self): fpath = tempfile.mktemp() try: salt.utils.fopen(fpath, "w").write("root_dir: /\n" "key_logfile: key\n") config = sconfig.master_config(fpath) # os.path.join behaviour self.assertEqual(config["key_logfile"], os.path.join("/", "key")) # os.sep.join behaviour self.assertNotEqual(config["key_logfile"], "//key") finally: if os.path.isfile(fpath): os.unlink(fpath)
def test_common_prefix_stripping(self): tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR) try: root_dir = os.path.join(tempdir, "foo", "bar") os.makedirs(root_dir) fpath = os.path.join(root_dir, "config") salt.utils.fopen(fpath, "w").write("root_dir: {0}\n" "log_file: {1}\n".format(root_dir, fpath)) config = sconfig.master_config(fpath) self.assertEqual(config["log_file"], fpath) finally: if os.path.isdir(tempdir): shutil.rmtree(tempdir)
def test_common_prefix_stripping(self): tempdir = tempfile.mkdtemp() root_dir = os.path.join(tempdir, 'foo', 'bar') os.makedirs(root_dir) fpath = os.path.join(root_dir, 'config') salt.utils.fopen(fpath, 'w').write( 'root_dir: {0}\n' 'log_file: {1}\n'.format(root_dir, fpath) ) config = sconfig.master_config(fpath) self.assertEqual(config['log_file'], fpath) shutil.rmtree(tempdir)
def test_proper_path_joining(self): fpath = tempfile.mktemp() try: with salt.utils.fopen(fpath, 'w') as fp_: fp_.write('root_dir: /\n' 'key_logfile: key\n') config = sconfig.master_config(fpath) # os.path.join behavior self.assertEqual(config['key_logfile'], os.path.join('/', 'key')) # os.sep.join behavior self.assertNotEqual(config['key_logfile'], '//key') finally: if os.path.isfile(fpath): os.unlink(fpath)
def test_common_prefix_stripping(self): tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR) try: root_dir = os.path.join(tempdir, 'foo', 'bar') os.makedirs(root_dir) fpath = os.path.join(root_dir, 'config') with salt.utils.fopen(fpath, 'w') as fp_: fp_.write('root_dir: {0}\n' 'log_file: {1}\n'.format(root_dir, fpath)) config = sconfig.master_config(fpath) self.assertEqual(config['log_file'], fpath) finally: if os.path.isdir(tempdir): shutil.rmtree(tempdir)
def test_proper_path_joining(self): fpath = tempfile.mktemp() try: with salt.utils.fopen(fpath, 'w') as fp_: fp_.write( 'root_dir: /\n' 'key_logfile: key\n' ) config = sconfig.master_config(fpath) # os.path.join behavior self.assertEqual(config['key_logfile'], os.path.join('/', 'key')) # os.sep.join behavior self.assertNotEqual(config['key_logfile'], '//key') finally: if os.path.isfile(fpath): os.unlink(fpath)
def test_common_prefix_stripping(self): tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR) try: root_dir = os.path.join(tempdir, 'foo', 'bar') os.makedirs(root_dir) fpath = os.path.join(root_dir, 'config') with salt.utils.fopen(fpath, 'w') as fp_: fp_.write( 'root_dir: {0}\n' 'log_file: {1}\n'.format(root_dir, fpath) ) config = sconfig.master_config(fpath) self.assertEqual(config['log_file'], fpath) finally: if os.path.isdir(tempdir): shutil.rmtree(tempdir)
def test_default_root_dir_included_in_config_root_dir(self): os.makedirs(os.path.join(TMP, 'tmp2')) tempdir = tempfile.mkdtemp(dir=os.path.join(TMP, 'tmp2')) try: root_dir = os.path.join(tempdir, 'foo', 'bar') os.makedirs(root_dir) fpath = os.path.join(root_dir, 'config') with salt.utils.files.fopen(fpath, 'w') as fp_: fp_.write('root_dir: {0}\n' 'log_file: {1}\n'.format(root_dir, fpath)) with patch('salt.syspaths.ROOT_DIR', TMP): config = sconfig.master_config(fpath) self.assertEqual(config['log_file'], fpath) finally: if os.path.isdir(tempdir): shutil.rmtree(tempdir)
def delete(): config = sac.cloud_config('/etc/salt/cloud') master = sac.master_config('/etc/salt/master') api_key = config['providers']['my-nova']['nova']['api_key'] ident = config['providers']['my-nova']['nova']['identity_url'] tenant = config['providers']['my-nova']['nova']['tenant'] user = config['providers']['my-nova']['nova']['user'] region = config['providers']['my-nova']['nova']['compute_region'] loadbalancer = master['loadbalancer']['name'] port = master['loadbalancer']['port'] lbendpoint = 'https://{0}.loadbalancers.api.rackspacecloud.com/v1.0/{1}'.format(region, tenant) headers = { 'Content-Type': 'application/json' } payload={ "auth": { "RAX-KSKEY:apiKeyCredentials": { "username": user, "apiKey": api_key } } } ret = requests.post(os.path.join(ident, 'tokens'), data=json.dumps(payload), headers=headers).json() headers['X-Auth-Token'] = ret['access']['token']['id'] loadbalancers = requests.get('{0}/loadbalancers'.format(lbendpoint), headers=headers).json().get('loadBalancers', {}) lb = None for l in loadbalancers: if l['name'] == loadbalancer: lb = l if lb is None: log.debug("Loadbalancer doesn't exist: {0}".format(loadbalancer)) return False if _wait_for_loadbalancer(lb['id'], lbendpoint, headers): ret = requests.delete('{0}/loadbalancers/{1}'.format(lbendpoint, lb['id']), headers=headers) else: log.debug("Loadbalancer stuck not in Active: {0}".format(loadbalancer)) return False if ret.status_code == 202: return True else: return False
def create(): config = sac.cloud_config('/etc/salt/cloud') master = sac.master_config('/etc/salt/master') api_key = config['providers']['my-nova']['nova']['api_key'] ident = config['providers']['my-nova']['nova']['identity_url'] tenant = config['providers']['my-nova']['nova']['tenant'] user = config['providers']['my-nova']['nova']['user'] region = config['providers']['my-nova']['nova']['compute_region'] loadbalancer = master['loadbalancer']['name'] port = master['loadbalancer']['port'] lbendpoint = 'https://{0}.loadbalancers.api.rackspacecloud.com/v1.0/{1}'.format(region, tenant) headers = { 'Content-Type': 'application/json' } payload={ "auth": { "RAX-KSKEY:apiKeyCredentials": { "username": user, "apiKey": api_key } } } ret = requests.post(os.path.join(ident, 'tokens'), data=json.dumps(payload), headers=headers).json() headers['X-Auth-Token'] = ret['access']['token']['id'] loadbalancers = requests.get('{0}/loadbalancers'.format(lbendpoint), headers=headers).json().get('loadBalancers', {}) for l in loadbalancers: if l['name'] == loadbalancer: log.debug('Loadbalancer exists: {0}'.format(loadbalancer)) return None payload = json.dumps({ "loadBalancer": { "name": loadbalancer, "port": port, "protocol": "HTTP", "virtualIps": [ {"type": "PUBLIC"} ] } }) ret = requests.post('{0}/loadbalancers'.format(lbendpoint), headers=headers, data=payload).json().get('loadBalancer', {}) _wait_for_loadbalancer(ret['id'], lbendpoint, headers) return ret
def setup_config(self): opts = config.master_config(self.get_config_file_path("master")) opts["_minion_conf_file"] = opts["conf_file"] opts.update(config.minion_config(self.get_config_file_path("minion"))) if "syndic_master" not in opts: self.error("The syndic_master needs to be configured in the salt master " "config, EXITING!") from salt import utils # Some of the opts need to be changed to match the needed opts # in the minion class. opts["master"] = opts["syndic_master"] opts["master_ip"] = utils.dns_check(opts["master"]) opts["master_uri"] = "tcp://{0}:{1}".format(opts["master_ip"], str(opts["master_port"])) opts["_master_conf_file"] = opts["conf_file"] opts.pop("conf_file") return opts
def create(container): config = sac.cloud_config('/etc/salt/cloud') master = sac.master_config('/etc/salt/master') api_key = config['providers']['my-nova']['nova']['api_key'] ident = config['providers']['my-nova']['nova']['identity_url'] tenant = config['providers']['my-nova']['nova']['tenant'] user = config['providers']['my-nova']['nova']['user'] region = config['providers']['my-nova']['nova']['compute_region'] headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } payload={ "auth": { "RAX-KSKEY:apiKeyCredentials": { "username": user, "apiKey": api_key } } } identity = requests.post(os.path.join(ident, 'tokens'), data=json.dumps(payload), headers=headers).json().get('access', {}) headers['X-Auth-Token'] = identity['token']['id'] catalog = identity['serviceCatalog'] endpoint = _get_endpoint('object-store', region, catalog) containers = requests.get(endpoint, headers=headers).json() for c in containers: if c['name'] == container: log.debug('Container already exists!: {1}'.format(region, container)) fire_event('enablecontainer', {'name': container}, 'salt/{0}/enablecontainer'.format(container)) return None ret = requests.put('{0}/{1}'.format(endpoint, container), headers=headers) if ret.status_code == 201: fire_event('enablecontainer', {'name': container}, 'salt/{0}/enablecontainer'.format(container)) return True else: log.debug('Failed to create container: {0}'.format(container)) return False
def setup_config(self): opts = config.master_config(self.get_config_file_path()) user = opts.get('user', 'root') opts['_minion_conf_file'] = opts['conf_file'] opts.update(config.minion_config(self.get_config_file_path('minion'))) # Override the user from the master config file opts['user'] = user # Override the name of the PID file. opts['pidfile'] = '/var/run/salt-syndic.pid' if not opts.get('syndic_master', None): self.error( 'The syndic_master needs to be configured in the salt master ' 'config, EXITING!' ) # Some of the opts need to be changed to match the needed opts # in the minion class. opts['master'] = opts.get('syndic_master', opts['master']) try: opts['master_ip'] = utils.dns_check( opts['master'], ipv6=opts['ipv6'] ) except exceptions.SaltSystemExit as exc: self.exit( status=exc.code, msg='{0}: {1}\n'.format(self.get_prog_name(), exc.message) ) opts['master_uri'] = 'tcp://{0}:{1}'.format( opts['master_ip'], str(opts['master_port']) ) opts['_master_conf_file'] = opts['conf_file'] opts.pop('conf_file') return opts
def setup_config(self): return config.master_config(self.get_config_file_path('master'))
def setUp(self): self.master_config = master_config(self.get_config_file_path('master')) for entry in ('root_dir', 'cachedir'): if not os.path.isdir(self.master_config[entry]): os.makedirs(self.master_config[entry])
def setUp(self): self.master_config = master_config(self.get_config_file_path('master'))
def _master_opts(): cfg = os.environ.get('SALT_MINION_CONFIG', os.path.join(salt.syspaths.CONFIG_DIR, 'master')) opts = config.master_config(cfg) return opts
def __init__(self): Base_Class.__init__(self) self.__opt = config.master_config('/etc/salt/master') self.__key = key.get_key(self.__opt) self.__Client = salt.client.LocalClient() self.timeout = 60
def __init__(self, settings): super(EventListener, self).__init__() salt_conf = settings.get('salt.master_config', '/etc/salt/master') salt_opts = salt_config.master_config(salt_conf) self.daemon = True self.event = MasterEvent(salt_opts['sock_dir'])
def __init__(self): self.options = DummyOptions() keys_config = master_config(self.get_config_file_path('master')) Key.__init__(self, keys_config)
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 __init__(self): self.opts = config.master_config('/etc/salt/master') self.key = key.get_key(self.opts) self.all_keys = self.key.list_keys()
def _master_opts(cfg='master'): cfg = os.environ.get( 'SALT_MASTER_CONFIG', __opts__.get('conf_file', os.path.join(__opts__['config_dir'], cfg))) opts = config.master_config(cfg) return opts