def test_deploy_search_path_as_string(self): temp_conf_dir = os.path.join(integration.TMP, 'issue-8863') config_file_path = os.path.join(temp_conf_dir, 'cloud') deploy_dir_path = os.path.join(temp_conf_dir, 'test-deploy.d') try: for directory in (temp_conf_dir, deploy_dir_path): if not os.path.isdir(directory): os.makedirs(directory) default_config = sconfig.cloud_config(config_file_path) default_config['deploy_scripts_search_path'] = deploy_dir_path with salt.utils.fopen(config_file_path, 'w') as cfd: cfd.write(yaml.dump(default_config)) default_config = sconfig.cloud_config(config_file_path) # Our custom deploy scripts path was correctly added to the list self.assertIn( deploy_dir_path, default_config['deploy_scripts_search_path'] ) # And it's even the first occurrence as it should self.assertEqual( deploy_dir_path, default_config['deploy_scripts_search_path'][0] ) finally: if os.path.isdir(temp_conf_dir): shutil.rmtree(temp_conf_dir)
def process_cloud_config(self): self.cloud_config = config.cloud_config(self.options.cloud_config) # Store a temporary config dict with just the cloud settings so the # logging level can be retrieved in LogLevelMixIn.process_log_level() self.config = self.cloud_config if self.options.master_config is None: # No master config was provided from cli # Set the master configuration file path to the one provided in # the cloud's configuration or the default path. self.options.master_config = self.cloud_config.get( 'master_config', '/etc/salt/master' ) if self.options.vm_config is None: # No profiles config was provided from cli # Set the profiles configuration file path to the one provided in # the cloud's configuration or the default path. self.options.vm_config = self.cloud_config.get( 'vm_config', '/etc/salt/cloud.profiles' ) if self.options.providers_config is None: # No providers config was provided from cli # Set the profiles configuration file path to the one provided in # the cloud's configuration or the default path. self.options.providers_config = self.cloud_config.get( 'providers_config', '/etc/salt/cloud.providers' )
def test_includes_load(self): ''' Tests that cloud.{providers,profiles}.d directories are loaded, even if not directly passed in through path ''' config = sconfig.cloud_config(self.get_config_file_path('cloud')) self.assertIn('ec2-config', config['providers']) self.assertIn('ec2-test', config['profiles'])
def config(self): if not hasattr(self, "_config"): self._config = cloud_config( os.path.join( self.config_dir, "cloud.profiles.d", self.PROVIDER + ".conf" ) ) return self._config
def config(self): if not hasattr(self, "_config"): self._config = cloud_config( os.path.join( RUNTIME_VARS.TMP_CONF_DIR, "cloud.profiles.d", self.PROVIDER + ".conf", )) return self._config
def test_load_cloud_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_CLOUD_CONFIG'] = env_fpath # Should load from env variable, not the default configuration file config = sconfig.cloud_config('/etc/salt/cloud') 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_CLOUD_CONFIG'] = env_fpath config = sconfig.cloud_config(fpath) self.assertEqual(config['log_file'], fpath) finally: # Reset the environ os.environ.clear() os.environ.update(original_environ) if os.path.isdir(tempdir): shutil.rmtree(tempdir)
def test_load_cloud_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_CLOUD_CONFIG'] = env_fpath # Should load from env variable, not the default configuration file config = sconfig.cloud_config('/etc/salt/cloud') 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_CLOUD_CONFIG'] = env_fpath config = sconfig.cloud_config(fpath) self.assertEqual(config['log_file'], fpath) finally: # Reset the environ os.environ.clear() os.environ.update(original_environ) if os.path.isdir(tempdir): shutil.rmtree(tempdir)
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 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 test_cloud_config_deploy_scripts_search_path(self): ''' Tests the contents of the 'deploy_scripts_search_path' tuple to ensure that the correct deploy search paths are present. There should be two search paths reported in the tuple: ``/etc/salt/cloud.deploy.d`` and ``<path-to-salt-install>/salt/cloud/deploy``. The first element is usually ``/etc/salt/cloud.deploy.d``, but sometimes is can be something like ``/etc/local/salt/cloud.deploy.d``, so we'll only test against the last part of the path. ''' search_paths = sconfig.cloud_config('/etc/salt/cloud').get('deploy_scripts_search_path') etc_deploy_path = '/salt/cloud.deploy.d' deploy_path = '/salt/cloud/deploy' # Check cloud.deploy.d path is the first element in the search_paths tuple self.assertTrue(search_paths[0].endswith(etc_deploy_path)) # Check the second element in the search_paths tuple self.assertTrue(search_paths[1].endswith(deploy_path))
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 test_instance(self): ''' Tests creating and deleting an instance on vmware and installing salt ''' # create the instance profile = os.path.join(RUNTIME_VARS.FILES, 'conf', 'cloud.profiles.d', PROVIDER_NAME + '.conf') profile_config = cloud_config(profile) disk_datastore = profile_config['vmware-test']['devices']['disk'][ 'Hard disk 2']['datastore'] instance = self.run_cloud('-p vmware-test {0}'.format(INSTANCE_NAME), timeout=TIMEOUT) ret_str = '{0}:'.format(INSTANCE_NAME) disk_datastore_str = ' [{0}] {1}/Hard disk 2-flat.vmdk'.format( disk_datastore, INSTANCE_NAME) # check if instance returned with salt installed try: self.assertIn(ret_str, instance) self.assertIn( disk_datastore_str, instance, msg='Hard Disk 2 did not use the Datastore {0} '.format( disk_datastore)) except AssertionError: self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=TIMEOUT) raise # delete the instance delete = self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=TIMEOUT) ret_str = '{0}:\', \' True'.format(INSTANCE_NAME) # check if deletion was performed appropriately self.assertIn(ret_str, six.text_type(delete))
def get_salt_cloud_opts(): return config.cloud_config( settings.STACKDIO_CONFIG.salt_cloud_config )
def test_includes_load(self): '''cloud.{providers,profiles}.d directories are loaded even if not directly passed''' config = cloudconfig.cloud_config(self.get_config_file_path('cloud')) self.assertIn('ec2-config', config['providers']) self.assertIn('Ubuntu-13.04-AMD64', config['profiles'])
def test_includes_load(self): '''cloud.{providers,profiles}.d directories are loaded even if not directly passed''' config_path = os.path.join(integration.FILES, 'conf', 'cloud') config = cloudconfig.cloud_config(config_path) self.assertIn('ec2-config', config['providers']) self.assertIn('Ubuntu-13.04-AMD64', config['profiles'])
def add(server): 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'] lbendpoint = 'https://{0}.loadbalancers.api.rackspacecloud.com/v1.0/{1}'.format(region, tenant) sendpoint = 'https://{0}.servers.api.rackspacecloud.com/v2/{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 break if lb is None: lb = create() if ret is False: log.debug("Failed to create loadbalancer: {0}".format(loadbalancer)) return ret servers = requests.get('{0}/servers/detail?name={1}'.format(sendpoint, server), headers=headers).json().get('servers', []) if servers and servers[0]['name'] == server: serverjson = servers[0] private_ip = serverjson['addresses']['private'][0]['addr'] else: log.debug("Server doesn't exist: {0}".format(server)) return False nodes = requests.get('{0}/loadbalancers/{1}/nodes'.format(lbendpoint, lb['id']), headers=headers).json().get('nodes', []) for node in nodes: if node['address'] == private_ip: log.debug('Server already exists in loadbalancer: {0}'.format(server)) return None payload = json.dumps({ "nodes": [ { "address": private_ip, "port": 80, "condition": "ENABLED", "type":"PRIMARY" } ] }) if _wait_for_loadbalancer(lb['id'], lbendpoint, headers): ret = requests.post('{0}/loadbalancers/{1}/nodes'.format(lbendpoint, lb['id']), headers=headers, data=payload).json() return ret else: log.debug('Loadbalancer stuck not in Active: {0}'.format(loadbalancer)) return False
def config(self): if not hasattr(self, '_config'): self._config = cloud_config( os.path.join(self.config_dir, 'cloud.profiles.d', self.PROVIDER + '.conf')) return self._config