def _init_certificates(self): """ Initializes certificates paths """ # Defines SSL certificate path if not specified if self._ssl_cert_generate and self._ssl_cert_crt is not False: if not self._ssl_cert_crt: self._ssl_cert_crt = _cfg.APYFAL_CERT_CRT generated = True exists = _os_path.isfile(self._ssl_cert_crt) else: generated = False exists = False if not self._ssl_cert_key: self._ssl_cert_key = _cfg.APYFAL_CERT_KEY generated = True exists &= _os_path.isfile(self._ssl_cert_key) if exists: # Files already exists, don't need to generate them self._ssl_cert_generate = False elif generated: # Files needs to be generated, ensures directories exists for path in (self._ssl_cert_crt, self._ssl_cert_key): _utl.makedirs(_os_path.dirname(path), exist_ok=True) # Set HTTP/HTTPS as default depending on certificate if self._ssl_cert_crt: self._url = _utl.format_url(self._url, force_secure=True)
def __init__(self, host_type=None, config=None, host_ip=None, stop_mode=None, host_name_prefix=None, **_): # Default some attributes self._cache = {} self._accelerator = None self._stop_mode = None self._stopped = False self._config_env = {} self._config_section = 'host.%s' % self.NAME if self.NAME else 'host' self._host_name = None self._host_name_match = None # Read configuration from file self._config = _cfg.create_configuration(config) section = self._config[self._config_section] self._host_type = self._host_type_from_config(host_type, self._config) self._host_ip = host_ip or section['host_ip'] self._url = _utl.format_url(self._host_ip) self.stop_mode = (stop_mode or section['stop_mode'] or ('keep' if host_ip else 'term')) self._host_name_prefix = (host_name_prefix or section['host_name_prefix'] or '')
def iter_hosts(self, host_name_prefix=True): """ Iterates over accelerator hosts of current type. Args: host_name_prefix (bool or str): If True, use "host_name_prefix" from configuration, if False don't filter by prefix, if str, uses this str as prefix Returns: generator of dict: dicts contains attributes values of the host. """ # Prepares name validator if host_name_prefix is True: # Use configuration prefix host_name_prefix = self._host_name_prefix elif host_name_prefix is False: # Show instances with all prefixes host_name_prefix = '.*' if host_name_prefix not in ('', '.*'): # Adds separator host_name_prefix += '_' self._host_name_match = _re.compile( '%saccelize_\w*_\d{12}' % host_name_prefix).match # Prepares repr base information repr_base = "<%s.%s" % (self.__class__.__module__, self.__class__.__name__) + ' %s>' repr_list = [(name, attr.lstrip('_')) for name, attr in self._REPR] # Validates and yield hosts for host in self._iter_hosts(): # Completes host information host['host_type'] = self._host_type host['accelerator'] = host['host_name'].split( 'accelize_', 1)[1].rsplit('_', 1)[0] if 'public_ip' in host: host['url'] = _utl.format_url(host['public_ip']) # Adds host repr host['_repr'] = repr_base % (' '.join( "%s='%s'" % (name, host.get(attr)) for name, attr in repr_list if host.get(attr) is not None)) yield host
def url(self, url): """ URL of the accelerator host. Args: url (str): URL. """ # Check URL if not url: raise _exc.ClientConfigurationException("Host URL is not valid.") self._url = url = _utl.format_url(url, force_secure=bool( self._ssl_cert_crt)) # Updates endpoints for route in self._REST_API: self._endpoints[route] = url + self._REST_API[route]
def _session(self): """ Requests session Returns: requests.sessions.Session: Session """ session_kwargs = dict(max_retries=self._REQUEST_RETRIES) # Gets SSL certificate if self._ssl_cert_crt is None and _os_path.exists( _cfg.APYFAL_CERT_CRT): # Uses default certificate if not provided and not not False self._ssl_cert_crt = _cfg.APYFAL_CERT_CRT elif (self._ssl_cert_crt and (hasattr(self._ssl_cert_crt, 'read') or not _os_path.exists(self._ssl_cert_crt))): # Copies certificate locally if not reachable by local path ssl_cert_crt = _os_path.join(self._tmp_dir, str(_uuid())) _srg_copy(self._ssl_cert_crt, ssl_cert_crt) self._ssl_cert_crt = ssl_cert_crt # Enables certificates verification if self._ssl_cert_crt: session_kwargs['verify'] = self._ssl_cert_crt # Disables hostname verification if wildcard certificate from apyfal._certificates import \ get_host_names_from_certificate with open(self._ssl_cert_crt, 'rb') as crt_file: if get_host_names_from_certificate(crt_file.read()) == ['*']: session_kwargs['assert_hostname'] = False # Force url to use HTTPS self._url = _utl.format_url(self._url, force_secure=bool(self._ssl_cert_crt)) # Initializes session return _utl.http_session(**session_kwargs)
def test_format_url(): """Tests format_url""" from apyfal._utilities import format_url # Test: Empty values assert format_url('') is None assert format_url(None) is None # Test: Values without schemes assert format_url('127.0.0.1') == 'http://127.0.0.1' assert format_url('localhost') == 'http://localhost' assert format_url('accelize.com') == 'http://accelize.com' # Test: Values with schemes assert format_url('http://127.0.0.1') == 'http://127.0.0.1' assert format_url('http://localhost') == 'http://localhost' assert format_url('http://accelize.com') == 'http://accelize.com' # Test: Bad URL with pytest.raises(ValueError): format_url('http://accelize') # Test: Force HTTPS assert format_url('http://accelize.com', force_secure=True) == 'https://accelize.com' assert format_url('https://accelize.com', force_secure=True) == 'https://accelize.com'
def start(self, accelerator=None, accel_parameters=None, stop_mode=None, image_id=None, instance_type=None): """ Start instance if not already started. Create instance if necessary. Needs "accel_client" or "accel_parameters". Args: accelerator (str): Name of the accelerator. accel_parameters (dict): Can override parameters from accelerator client. image_id (str): Force the use of specified image ID. instance_type (str): Force the use of specified instance type. stop_mode (str or int): See "stop_mode" property for more information. """ # Updates stop mode self.stop_mode = stop_mode # Starts instance only if not already started if self._host_ip is None: # Get parameters from accelerator self._set_accelerator_requirements( accelerator=accelerator, accel_parameters=accel_parameters, image_id=image_id, instance_type=instance_type) # Checks CSP credential self._check_credential() # Creates and starts instance if not exists if self.instance_id is None: _get_logger().info("Configuring host on %s instance...", self._host_type) with self._stop_silently_on_exception(): self._create_instance() with self._stop_silently_on_exception(): self._instance, self._instance_id = \ self._start_new_instance() _get_logger().debug( _utl.gen_msg('created_named', 'instance', self._instance_id)) # If exists, starts it directly else: self._start_existing_instance(self._status()) # Waiting for instance provisioning with self._stop_silently_on_exception(): self._wait_instance_ready() # Update instance URL self._host_ip = self.host_ip self._url = _utl.format_url(self._host_ip, force_secure=bool(self._ssl_cert_crt)) # Waiting for the instance to boot self._wait_instance_boot() _get_logger().info("Host ready") # If Host IP exists exists, checks if reachable elif self.ALLOW_PORTS and not _utl.check_port(self.host_ip, 80): raise _exc.HostRuntimeException(gen_msg=('unable_reach_port', self.host_ip, 80))