def __init__(self, invoc): if 'KUBERNETES_SERVICE_HOST' in os.environ: config.load_incluster_config() else: config.load_kube_config() logging.getLogger().setLevel(agent_config.settings.loglevel) self._configuration = client.Configuration() self._api_instance = client.BatchV1Api( client.ApiClient(self._configuration)) self._api_instance_core = client.CoreV1Api( client.ApiClient(self._configuration)) configuration = stackl_client.Configuration() configuration.host = agent_config.settings.stackl_host api_client = stackl_client.ApiClient(configuration=configuration) self._stack_instance_api = stackl_client.StackInstancesApi( api_client=api_client) self._stack_fr_api = stackl_client.FunctionalRequirementsApi( api_client=api_client) self._invoc = invoc self._service = self._invoc.service self.hosts = self._invoc.hosts self._functional_requirement = self._invoc.functional_requirement self._functional_requirement_obj = self._stack_fr_api.get_functional_requirement_by_name( self._functional_requirement) self._stack_instance = self._stack_instance_api.get_stack_instance( self._invoc.stack_instance) self._output = None self._env_from = {} self._env_list = {} self._volumes = [] self._init_containers = [] self.stackl_namespace = agent_config.settings.stackl_namespace self.service_account = agent_config.settings.service_account self._secret_handler = None
def __init__(self): try: with open(get_config_path(), 'r+') as stackl_config: host = stackl_config.read() configuration = stackl_client.Configuration() configuration.host = host self.api_client = stackl_client.ApiClient( configuration=configuration) self.infrastructure_base_api = stackl_client.InfrastructureBaseApi( api_client=self.api_client) self.functional_requirements_api = stackl_client.FunctionalRequirementsApi( api_client=self.api_client) self.services_api = stackl_client.ServicesApi( api_client=self.api_client) self.sat_api = stackl_client.StackApplicationTemplatesApi( api_client=self.api_client) self.sit_api = stackl_client.StackInfrastructureTemplatesApi( api_client=self.api_client) self.stack_instances_api = stackl_client.StackInstancesApi( api_client=self.api_client) self.policy_templates_api = stackl_client.PolicyTemplatesApi( api_client=self.api_client) self.snapshot_api = stackl_client.SnapshotsApi( api_client=self.api_client) self.outputs_api = stackl_client.OutputsApi( api_client=self.api_client) except FileNotFoundError: click.echo("Config file not found, run `stackl connect` first") exit(1)
def parse(self, inventory, loader, path, cache): super(InventoryModule, self).parse(inventory, loader, path, cache) self._read_config_data(path) try: self.plugin = self.get_option('plugin') configuration = stackl_client.Configuration() configuration.host = self.get_option("host") api_client = stackl_client.ApiClient(configuration=configuration) api_instance = stackl_client.StackInstancesApi(api_client=api_client) stack_instance_name = self.get_option("stack_instance") stack_instance = api_instance.get_stack_instance(stack_instance_name) for service, si_service in stack_instance.services.items(): self.inventory.add_group(service) self.inventory.set_variable(service, "infrastructure_target", si_service.infrastructure_target) self.inventory.set_variable(service, "provisioning_parameters", si_service.provisioning_parameters) if si_service.hosts is not None: for host in si_service.hosts: self.inventory.add_host(host=host, group=service) self.inventory.set_variable(service, "ansible_user", si_service.connection_credentials.username) self.inventory.set_variable(service, "ansible_password", si_service.connection_credentials.password) self.inventory.set_variable(service, "ansible_become_password", si_service.connection_credentials.password) self.inventory.set_variable(service, "ansible_ssh_common_args", "-o StrictHostKeyChecking=no") else: self.inventory.add_host(host="kubernetes-" + service, group=service) except Exception as e: raise AnsibleParserError( 'All correct options required: {}'.format(e))
def get_config_path(): if len(str(Path.home())) == 0: config_path = os.getcwd() + os.sep + '.stackl' + os.sep + 'config' else: config_path = str(Path.home()) + os.sep + '.stackl' + os.sep + 'config' return config_path with open(get_config_path(), 'r+') as stackl_config: host = stackl_config.read() configuration = stackl_client.Configuration() configuration.host = host api_client = stackl_client.ApiClient(configuration=configuration) stack_instances_api = stackl_client.StackInstancesApi( api_client=api_client) if "STACKL_INSTANCE" in os.environ: stack_instance = stack_instances_api.get_stack_instance(os.environ['STACKL_INSTANCE']) ready = False while not ready: for status in stack_instance.status: if status.status == "FAILED": print(f"Stack instance {stack_instance.name}: service {status.service} on functional-requirement {status.functional_requirement} failed!") print(f"Error {status.error_message}") exit(1) elif status.status == "in_progress": print(f"Stack instance {stack_instance.name}: service {status.service} on functional-requirement {status.functional_requirement} not ready, still waiting") time.sleep(5)