def start_application_kubernetes(self, deployment: dict): deployment_id = Deployment.id(deployment) connector = initialize_connector(kubernetes_cli_connector, self.job, deployment) module_content = Deployment.module_content(deployment) deployment_owner = Deployment.owner(deployment) docker_compose = module_content['docker-compose'] registries_auth = self.private_registries_auth(deployment) result, services = connector.start( docker_compose=docker_compose, stack_name=Deployment.uuid(deployment), env=get_env(deployment), files=module_content.get('files'), registries_auth=registries_auth) self.job.set_status_message(result) self.create_deployment_parameter( deployment_id=deployment_id, user_id=deployment_owner, param_name=DeploymentParameter.HOSTNAME['name'], param_value=self.get_hostname(), param_description=DeploymentParameter.HOSTNAME['description']) application_params_update(self.api_dpl, deployment, services)
def stop_application_kubernetes(self): connector = initialize_connector(kubernetes_cli_connector, self.job, self.deployment) result = connector.stop(stack_name=Deployment.uuid(self.deployment)) self.job.set_status_message(result)
def get_update_params_docker_stack(self, deployment, registries_auth): module_content = Deployment.module_content(deployment) kwargs = { 'env': get_env(deployment), 'files': module_content.get('files'), 'stack_name': Deployment.uuid(deployment), 'docker_compose': module_content['docker-compose'], 'registries_auth': registries_auth } return kwargs
def stop_application(self): if Deployment.is_compatibility_docker_compose(self.deployment): connector = initialize_connector(docker_compose_cli_connector, self.job, self.deployment) else: connector = initialize_connector(docker_cli_connector, self.job, self.deployment) result = connector.stop(stack_name=Deployment.uuid(self.deployment), docker_compose=Deployment.module_content( self.deployment)['docker-compose']) self.job.set_status_message(result)
def get_application_state(self): stack_name = Deployment.uuid(self.deployment) if Deployment.is_compatibility_docker_compose(self.deployment): module_content = Deployment.module_content(self.deployment) compose_file = module_content['docker-compose'] connector = initialize_connector(docker_compose_cli_connector, self.job, self.deployment) services = connector.stack_services(stack_name, compose_file) else: connector = initialize_connector(docker_cli_connector, self.job, self.deployment) services = connector.stack_services(stack_name) application_params_update(self.api_dpl, self.deployment, services)
def get_update_params_docker_service(self, deployment, registries_auth): module_content = Deployment.module_content(deployment) restart_policy = module_content.get('restart-policy', {}) module_ports = module_content.get('ports') kwargs = { 'service_name': Deployment.uuid(deployment), 'env': get_env(deployment), 'image': module_content['image'], 'mounts_opt': module_content.get('mounts'), 'cpu_ratio': module_content.get('cpus'), 'memory': module_content.get('memory'), 'ports_opt': module_ports, 'registries_auth': registries_auth, 'restart_policy_condition': restart_policy.get('condition'), 'restart_policy_delay': restart_policy.get('delay'), 'restart_policy_max_attempts': restart_policy.get('max-attempts'), 'restart_policy_window': restart_policy.get('window') } return kwargs
def fetch_log(self, deployment_log): service_name = deployment_log['service'] last_timestamp = deployment_log.get('last-timestamp') since = deployment_log.get('since') lines = deployment_log.get('lines', 200) deployment_id = deployment_log['parent'] deployment = self.api_dpl.get(deployment_id) deployment_uuid = Deployment.uuid(deployment) tmp_since = last_timestamp or since if Deployment.is_application_kubernetes(deployment): connector = initialize_connector(kubernetes_cli_connector, self.job, deployment) since_opt = ['--since-time', tmp_since] if tmp_since else [] list_opts = [ service_name, '--timestamps=true', '--tail', str(lines), '--namespace', deployment_uuid ] + since_opt else: is_docker_compose = Deployment.is_compatibility_docker_compose( deployment) if is_docker_compose: connector = initialize_connector(docker_compose_cli_connector, self.job, deployment) no_trunc = [] else: connector = initialize_connector(docker_cli_connector, self.job, deployment) no_trunc = ['--no-trunc'] if Deployment.is_application(deployment): if is_docker_compose: log.info(deployment_id) log.info(service_name) docker_service_name = self.api_dpl.get_parameter( deployment_id, service_name, service_name + '.service-id') else: docker_service_name = deployment_uuid + '_' + service_name else: docker_service_name = deployment_uuid since_opt = ['--since', tmp_since] if tmp_since else [] list_opts = ['-t'] + no_trunc + since_opt + [docker_service_name] result = connector.log(list_opts).strip().split('\n')[:lines] new_last_timestamp = DeploymentLogFetchJob.extract_last_timestamp( result) update_deployment_log = {'log': result} if new_last_timestamp: update_deployment_log['last-timestamp'] = new_last_timestamp self.api.edit(deployment_log['id'], update_deployment_log)
def get_application_kubernetes_state(self): connector = initialize_connector(kubernetes_cli_connector, self.job, self.deployment) stack_name = Deployment.uuid(self.deployment) services = connector.stack_services(stack_name) application_params_update(self.api_dpl, self.deployment, services)
def start_component(self, deployment: dict): connector = initialize_connector(docker_connector, self.job, deployment) deployment_id = Deployment.id(deployment) node_instance_name = Deployment.uuid(deployment) deployment_owner = Deployment.owner(deployment) module_content = Deployment.module_content(deployment) restart_policy = module_content.get('restart-policy', {}) # create deployment parameters (with empty values) for all port mappings module_ports = module_content.get('ports') for port in (module_ports or []): target_port = port.get('target-port') protocol = port.get('protocol', 'tcp') if target_port is not None: self.create_deployment_parameter( deployment_id=deployment_id, user_id=deployment_owner, param_name="{}.{}".format(protocol, str(target_port)), param_description="mapping for {} port {}".format( protocol, str(target_port)), node_id=node_instance_name) registries_auth = self.private_registries_auth(deployment) _, service = connector.start( service_name=node_instance_name, image=module_content['image'], env=get_env(deployment), mounts_opt=module_content.get('mounts'), ports_opt=module_ports, cpu_ratio=module_content.get('cpus'), memory=module_content.get('memory'), restart_policy_condition=restart_policy.get('condition'), restart_policy_delay=restart_policy.get('delay'), restart_policy_max_attempts=restart_policy.get('max-attempts'), restart_policy_window=restart_policy.get('window'), registry_auth=registries_auth[0] if registries_auth else None) # FIXME: get number of desired replicas of Replicated service from deployment. 1 for now. desired = 1 deployment_parameters = ( (DeploymentParameter.SERVICE_ID, connector.extract_vm_id(service)), (DeploymentParameter.HOSTNAME, self.get_hostname()), (DeploymentParameter.REPLICAS_DESIRED, str(desired)), (DeploymentParameter.REPLICAS_RUNNING, '0'), (DeploymentParameter.CURRENT_DESIRED, ''), (DeploymentParameter.CURRENT_STATE, ''), (DeploymentParameter.CURRENT_ERROR, ''), (DeploymentParameter.RESTART_EXIT_CODE, ''), (DeploymentParameter.RESTART_ERR_MSG, ''), (DeploymentParameter.RESTART_TIMESTAMP, ''), (DeploymentParameter.RESTART_NUMBER, ''), (DeploymentParameter.CHECK_TIMESTAMP, ''), ) for deployment_parameter, value in deployment_parameters: self.create_deployment_parameter( param_name=deployment_parameter['name'], param_value=value, param_description=deployment_parameter['description'], deployment_id=deployment_id, node_id=node_instance_name, user_id=deployment_owner) # immediately update any port mappings that are already available ports_mapping = connector.extract_vm_ports_mapping(service) self.api_dpl.update_port_parameters(deployment, ports_mapping)