def __get_os_credentials(os_conn_config): """ Returns an object containing all of the information required to access OpenStack APIs :param os_conn_config: The configuration holding the credentials :return: an OSCreds instance """ config = os_conn_config.get('connection') if not config: raise Exception('Invalid connection configuration') proxy_settings = None http_proxy = config.get('http_proxy') if http_proxy: tokens = re.split(':', http_proxy) ssh_proxy_cmd = config.get('ssh_proxy_cmd') proxy_settings = ProxySettings(host=tokens[0], port=tokens[1], ssh_proxy_cmd=ssh_proxy_cmd) else: if 'proxy_settings' in config: host = config['proxy_settings'].get('host') port = config['proxy_settings'].get('port') if host and host != 'None' and port and port != 'None': proxy_settings = ProxySettings(**config['proxy_settings']) if proxy_settings: config['proxy_settings'] = proxy_settings else: if config.get('proxy_settings'): del config['proxy_settings'] return OSCreds(**config)
def test_minimum_kwargs(self): proxy_settings = ProxySettings(**{'host': 'foo', 'port': 1234}) self.assertEqual('foo', proxy_settings.host) self.assertEqual('1234', proxy_settings.port) self.assertEqual('foo', proxy_settings.https_host) self.assertEqual('1234', proxy_settings.https_port) self.assertIsNone(proxy_settings.ssh_proxy_cmd)
def test_proxy_settings_obj_kwargs(self): proxy_settings = ProxySettings(host='foo', port=1234) os_creds = OSCreds( **{ 'username': '******', 'password': '******', 'auth_url': 'http://foo.bar:5000/v2', 'project_name': 'hello', 'proxy_settings': proxy_settings, 'region_name': 'test_region', 'user_domain_id': 'domain1', 'user_domain_name': 'domain2', 'project_domain_id': 'domain3', 'project_domain_name': 'domain4' }) self.assertEqual('foo', os_creds.username) self.assertEqual('bar', os_creds.password) self.assertEqual('http://foo.bar:5000/v3', os_creds.auth_url) self.assertEqual('hello', os_creds.project_name) self.assertEqual(3, os_creds.identity_api_version) self.assertEqual(2, os_creds.image_api_version) self.assertEqual(2, os_creds.compute_api_version) self.assertEqual(1, os_creds.heat_api_version) self.assertEqual(cinder_utils.VERSION_2, os_creds.volume_api_version) self.assertEqual(1, os_creds.magnum_api_version) self.assertEqual('domain1', os_creds.user_domain_id) self.assertEqual('domain2', os_creds.user_domain_name) self.assertEqual('domain3', os_creds.project_domain_id) self.assertEqual('domain4', os_creds.project_domain_name) self.assertEqual('public', os_creds.interface) self.assertFalse(os_creds.cacert) self.assertEqual('foo', os_creds.proxy_settings.host) self.assertEqual('1234', os_creds.proxy_settings.port) self.assertIsNone(os_creds.proxy_settings.ssh_proxy_cmd) self.assertEqual('test_region', os_creds.region_name)
def test_minimum(self): proxy_settings = ProxySettings(host='foo', port=1234) self.assertEqual('foo', proxy_settings.host) self.assertEqual('1234', proxy_settings.port) self.assertEqual('foo', proxy_settings.https_host) self.assertEqual('1234', proxy_settings.https_port) self.assertIsNone(proxy_settings.ssh_proxy_cmd)
def test_proxy_settings_obj(self): proxy_settings = ProxySettings(host='foo', port=1234) os_creds = OSCreds(username='******', password='******', auth_url='http://foo.bar:5000/', project_name='hello', proxy_settings=proxy_settings) self.assertEqual('foo', os_creds.username) self.assertEqual('bar', os_creds.password) self.assertEqual('http://foo.bar:5000/v3', os_creds.auth_url) self.assertEqual('hello', os_creds.project_name) self.assertEqual(3, os_creds.identity_api_version) self.assertEqual(2, os_creds.image_api_version) self.assertEqual(2, os_creds.compute_api_version) self.assertEqual(1, os_creds.heat_api_version) self.assertEqual(cinder_utils.VERSION_2, os_creds.volume_api_version) self.assertEqual(1, os_creds.magnum_api_version) self.assertEqual('default', os_creds.user_domain_id) self.assertEqual('Default', os_creds.user_domain_name) self.assertEqual('default', os_creds.project_domain_id) self.assertEqual('Default', os_creds.project_domain_name) self.assertEqual('public', os_creds.interface) self.assertFalse(os_creds.cacert) self.assertEqual('foo', os_creds.proxy_settings.host) self.assertEqual('1234', os_creds.proxy_settings.port) self.assertIsNone(os_creds.proxy_settings.ssh_proxy_cmd) self.assertIsNone(os_creds.region_name) creds_dict = os_creds.to_dict() creds_from_dict = OSCreds(**creds_dict) self.assertEqual(os_creds, creds_from_dict)
def test_all(self): proxy_settings = ProxySettings(host='foo', port=1234, https_host='bar', https_port=2345, ssh_proxy_cmd='proxy command') self.assertEqual('foo', proxy_settings.host) self.assertEqual('1234', proxy_settings.port) self.assertEqual('bar', proxy_settings.https_host) self.assertEqual('2345', proxy_settings.https_port) self.assertEqual('proxy command', proxy_settings.ssh_proxy_cmd)
def test_all_kwargs(self): proxy_settings = ProxySettings( **{ 'host': 'foo', 'port': 1234, 'https_host': 'bar', 'https_port': 2345, 'ssh_proxy_cmd': 'proxy command' }) self.assertEqual('foo', proxy_settings.host) self.assertEqual('1234', proxy_settings.port) self.assertEqual('bar', proxy_settings.https_host) self.assertEqual('2345', proxy_settings.https_port) self.assertEqual('proxy command', proxy_settings.ssh_proxy_cmd)
def main(parsed_args): """ Uses ansible_utils for applying Ansible Playbooks to machines with a private key """ logging.basicConfig(level=logging.DEBUG) logger.info('Starting Playbook Runner') proxy_settings = None if parsed_args.http_proxy: tokens = re.split(':', parsed_args.http_proxy) proxy_settings = ProxySettings(host=tokens[0], port=tokens[1], ssh_proxy_cmd=parsed_args.ssh_proxy_cmd) # Ensure can get an SSH client ssh = ansible_utils.ssh_client(parsed_args.ip_addr, parsed_args.host_user, private_key_filepath=parsed_args.priv_key, proxy_settings=proxy_settings) if ssh: ssh.close() env = Environment(loader=FileSystemLoader( searchpath=os.path.dirname(parsed_args.env_file))) template = env.get_template(os.path.basename(parsed_args.env_file)) env_dict = dict() if parsed_args.vars: env_dict = ast.literal_eval(parsed_args.vars) output = template.render(**env_dict) variables = yaml.load(output) if not variables.get('env_file'): variables['env_file'] = parsed_args.env_file ansible_utils.apply_playbook(parsed_args.playbook, [parsed_args.ip_addr], parsed_args.host_user, ssh_priv_key_file_path=parsed_args.priv_key, password=parsed_args.password, variables=variables, proxy_setting=proxy_settings)
import logging from snaps.config.vm_inst import VmInstanceConfig logging.basicConfig(level=logging.INFO) # Credentials from snaps.openstack.os_credentials import OSCreds, ProxySettings proxy_settings = ProxySettings( host='10.197.123.27', port='3128', ssh_proxy_cmd='/usr/local/bin/corkscrew 10.197.123.27 3128 %h %p') os_creds = OSCreds(username='******', password='******', auth_url='http://192.168.67.10:5000/v2.0/', project_name='admin', proxy_settings=proxy_settings) # Images from snaps.openstack.create_image import OpenStackImage from snaps.config.image import ImageConfig image_settings = ImageConfig( name='cirros-test', image_user='******', img_format='qcow2', url='http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img') image = OpenStackImage(os_creds, image_settings)
def get_credentials(os_env_file=None, proxy_settings_str=None, ssh_proxy_cmd=None, dev_os_env_file=None, overrides=None): """ Returns the OpenStack credentials object. It first attempts to retrieve them from a standard OpenStack source file. If that file is None, it will attempt to retrieve them with a YAML file. :param os_env_file: the OpenStack source file :param proxy_settings_str: proxy settings string <host>:<port> (optional) :param ssh_proxy_cmd: the SSH proxy command for your environment (optional) :param dev_os_env_file: the YAML file to retrieve both the OS credentials and proxy settings :param overrides: dict() containing values to override the credentials found and passed in. :return: the SNAPS credentials object """ if os_env_file: logger.debug('Reading RC file - ' + os_env_file) config = file_utils.read_os_env_file(os_env_file) proj_name = config.get('OS_PROJECT_NAME') if not proj_name: proj_name = config.get('OS_TENANT_NAME') proxy_settings = None if proxy_settings_str: tokens = re.split(':', proxy_settings_str) proxy_settings = ProxySettings(host=tokens[0], port=tokens[1], ssh_proxy_cmd=ssh_proxy_cmd) https_cacert = None if config.get('OS_CACERT'): https_cacert = config.get('OS_CACERT') elif config.get('OS_INSECURE'): https_cacert = False interface = 'public' if config.get('OS_INTERFACE'): interface = config.get('OS_INTERFACE') creds_dict = { 'username': config['OS_USERNAME'], 'password': config['OS_PASSWORD'], 'auth_url': config['OS_AUTH_URL'], 'project_name': proj_name, 'identity_api_version': config.get('OS_IDENTITY_API_VERSION'), 'image_api_version': config.get('OS_IMAGE_API_VERSION'), 'network_api_version': config.get('OS_NETWORK_API_VERSION'), 'compute_api_version': config.get('OS_COMPUTE_API_VERSION'), 'heat_api_version': config.get('OS_HEAT_API_VERSION'), 'user_domain_id': config.get('OS_USER_DOMAIN_ID'), 'user_domain_name': config.get('OS_USER_DOMAIN_NAME'), 'project_domain_id': config.get('OS_PROJECT_DOMAIN_ID'), 'project_domain_name': config.get('OS_PROJECT_DOMAIN_NAME'), 'volume_api_version': config.get('OS_VOLUME_API_VERSION'), 'interface': interface, 'proxy_settings': proxy_settings, 'cacert': https_cacert, 'region_name': config.get('OS_REGION_NAME') } else: logger.info('Reading development os_env file - ' + dev_os_env_file) config = file_utils.read_yaml(dev_os_env_file) proxy_settings = None proxy_str = config.get('http_proxy') if proxy_str: tokens = re.split(':', proxy_str) proxy_settings = ProxySettings( host=tokens[0], port=tokens[1], ssh_proxy_cmd=config.get('ssh_proxy_cmd')) creds_dict = { 'username': config['username'], 'password': config['password'], 'auth_url': config['os_auth_url'], 'project_name': config['project_name'], 'identity_api_version': config.get('identity_api_version'), 'image_api_version': config.get('image_api_version'), 'network_api_version': config.get('network_api_version'), 'compute_api_version': config.get('compute_api_version'), 'heat_api_version': config.get('heat_api_version'), 'user_domain_id': config.get('user_domain_id'), 'user_domain_name': config.get('user_domain_name'), 'project_domain_id': config.get('project_domain_id'), 'project_domain_name': config.get('project_domain_name'), 'volume_api_version': config.get('volume_api_version'), 'interface': config.get('interface'), 'proxy_settings': proxy_settings, 'cacert': config.get('cacert'), 'region_name': config.get('region_name') } if overrides and isinstance(overrides, dict): creds_dict.update(overrides) for key, value in creds_dict.items(): if value is not None and isinstance(value, str): creds_dict[key] = value.replace('"', '').replace('\'', '') os_creds = OSCreds(**creds_dict) logger.info('OS Credentials = %s', os_creds.__str__) return os_creds
def test_port_only_kwargs(self): with self.assertRaises(ProxySettingsError): ProxySettings(**{'port': 1234})
def test_port_only(self): with self.assertRaises(ProxySettingsError): ProxySettings(port=1234)
def test_host_only_kwargs(self): with self.assertRaises(ProxySettingsError): ProxySettings(**{'host': 'foo'})
def test_host_only(self): with self.assertRaises(ProxySettingsError): ProxySettings(host='foo')
def test_empty_kwargs(self): with self.assertRaises(ProxySettingsError): ProxySettings(**dict())
def test_no_params(self): with self.assertRaises(ProxySettingsError): ProxySettings()