def _user_data(self): """ Generate a shell script to initialize instance. Returns: str: shell script. """ # Initializes file with shebang commands = ["#!/usr/bin/env bash"] # Gets configuration file self._cat_config_file(commands) # Gets SSL ssl_cert_key if self._ssl_cert_crt and self._ssl_cert_key: self._cat_ssl_cert_files(commands) elif self._ssl_cert_crt or self._ssl_cert_key: # Needs both private and public keys raise _exc.HostConfigurationException( "Both 'ssl_cert_crt' and 'ssl_cert_key' are required") # Add initialization flag file commands.append('touch "%s"\n' % self._SH_FLAG) # Gets user bash script self._extend_init_script(commands) # Return final script return '\n'.join(commands).encode()
def _init_policy(self): """ Initialize IAM policy. This policy allow instance to: - Load FPGA bitstream. - Access to S3 buckets objects for read and write. Returns: str: 'policy' """ # Create a policy with _exception_handler(filter_error_codes='EntityAlreadyExists'): self._iam_client.create_policy(PolicyName=self._policy, PolicyDocument=_json_dumps( self.POLICY_DOCUMENT)) _get_logger().debug( _utl.gen_msg('created_named', 'policy', self._policy)) with _exception_handler(): response = self._iam_client.list_policies(Scope='Local', OnlyAttached=False, MaxItems=100) for policy_item in response['Policies']: if policy_item['PolicyName'] == self._policy: self._policy_arn = policy_item['Arn'] # 'policy' returns str is used set future object ID return 'policy' raise _exc.HostConfigurationException(gen_msg=('created_failed_named', 'IAM policy', self._policy))
def _check_host_id_arguments(self): """ Checks if enough arguments to start or get an instance. Raises: apyfal.exceptions.HostConfigurationException: Not enough arguments. """ if (self._client_id is None and self._instance_id is None and self._url is None): raise _exc.HostConfigurationException( "Need at least 'client_id', 'instance_id' or 'host_ip' " "argument. See documentation for more information.")
def __init__(self, host_type=None, region=None, *args, **kwargs): _Client.__init__(self, *args, **kwargs) self._metering_env = None self._host_type = host_type or self._config['host']['host_type'] self._region = region or self._config['host']['region'] # Accelerator executable is exclusive self._accelerator_lock = _Lock() # Need accelerator executable to run if not _cfg.accelerator_executable_available(): raise _exc.HostConfigurationException(gen_msg='no_host_found')
def _check_arguments(self, *arg_name): """ Check in attributes if arguments are set. Args: arg_name (str): Argument names to check. Raises: apyfal.exceptions.HostConfigurationException: A specified argument is None. """ for name in arg_name: if getattr(self, '_%s' % name) is None: raise _exc.HostConfigurationException( "Parameter '%s' is required %s" % (name, self._host_type))
def _init_image(self): """ Initializes image. """ # Checks if image exists and get its name with _exception_handler(gen_msg=('unable_find_from', 'image', self._image_id, 'Accelize')): image = self._nova_client.glance.find_image(self._image_id) try: self._image_name = image.name except AttributeError: raise _exc.HostConfigurationException(gen_msg=('unable_find_from', 'image', self._image_id, 'Accelize'))
def start(self, accelerator=None, accel_parameters=None, stop_mode=None): """ Start host if not already started. Needs "accel_client" or "accel_parameters". Args: accelerator (str): Name of the accelerator. accel_parameters (dict): Can override parameters from accelerator client. stop_mode (str or int): See "stop_mode" property for more information. """ # Check configuration if not self._url: raise _exc.HostConfigurationException(gen_msg='no_host_found') # Update stop mode self.stop_mode = stop_mode
def _set_accelerator_requirements(self, image_id=None, instance_type=None, *args, **kwargs): """ Configures instance with accelerator client parameters. 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. Raises: apyfal.exceptions.HostConfigurationException: Parameters are not valid. """ _Host._set_accelerator_requirements( self, request_to_server=not (instance_type and instance_type), *args, **kwargs) # For CSP, config env are in a region sub category try: self._config_env = self._config_env[self._region] except KeyError: if not image_id or not instance_type: raise _exc.HostConfigurationException( ("Region '%s' is not supported. " "Available regions are: %s") % (self._region, ', '.join(region for region in self._config_env if region != 'accelerator'))) # Gets some CSP configuration values from environment self._image_id = image_id or self._config_env.pop('image', None) self._instance_type = instance_type or self._config_env.pop( 'instancetype', None)