def setup_service( service: str, instance: str, clients: marathon_tools.MarathonClients, job_config: marathon_tools.MarathonServiceConfig, marathon_apps_with_clients: Sequence[Tuple[MarathonApp, MarathonClient]], soa_dir: str, ) -> Tuple[int, str, Optional[float]]: """Setup the service instance given and attempt to deploy it, if possible. Doesn't do anything if the service is already in Marathon and hasn't changed. If it's not, attempt to find old instances of the service and bounce them. :param service: The service name to setup :param instance: The instance of the service to setup :param clients: A MarathonClients object :param job_config: The service instance's configuration dict :returns: A tuple of (status, output, bounce_in_seconds) to be used with send_sensu_event""" log.info("Setting up instance %s for service %s", instance, service) try: marathon_app_dict = job_config.format_marathon_app_dict() except NoDockerImageError: error_msg = ( "Docker image for {0}.{1} not in deployments.json. Exiting. Has Jenkins deployed it?\n" ).format( service, instance, ) log.error(error_msg) return (1, error_msg, None) full_id = marathon_app_dict['id'] service_namespace_config = marathon_tools.load_service_namespace_config( service=service, namespace=job_config.get_nerve_namespace(), soa_dir=soa_dir, ) log.info("Desired Marathon instance id: %s", full_id) return deploy_service( service=service, instance=instance, marathon_jobid=full_id, config=marathon_app_dict, clients=clients, marathon_apps_with_clients=marathon_apps_with_clients, bounce_method=job_config.get_bounce_method(), drain_method_name=job_config.get_drain_method( service_namespace_config), drain_method_params=job_config.get_drain_method_params( service_namespace_config), nerve_ns=job_config.get_nerve_namespace(), registrations=job_config.get_registrations(), bounce_health_params=job_config.get_bounce_health_params( service_namespace_config), soa_dir=soa_dir, job_config=job_config, bounce_margin_factor=job_config.get_bounce_margin_factor(), )
def marathon_job_status( service: str, instance: str, job_config: marathon_tools.MarathonServiceConfig, marathon_apps_with_clients: List[Tuple[MarathonApp, MarathonClient]], verbose: int, ) -> MutableMapping[str, Any]: job_status_fields: MutableMapping[str, Any] = { "app_statuses": [], "app_count": len(marathon_apps_with_clients), "desired_state": job_config.get_desired_state(), "bounce_method": job_config.get_bounce_method(), "expected_instance_count": job_config.get_instances(), "active_shas": list(get_active_shas_for_marathon_apps(marathon_apps_with_clients)), } try: desired_app_id = job_config.format_marathon_app_dict()["id"] except NoDockerImageError: error_msg = "Docker image is not in deployments.json." job_status_fields["error_message"] = error_msg return job_status_fields job_status_fields["desired_app_id"] = desired_app_id deploy_status_for_desired_app = None dashboard_links = get_marathon_dashboard_links( settings.marathon_clients, settings.system_paasta_config) tasks_running = 0 for app, marathon_client in marathon_apps_with_clients: deploy_status = marathon_tools.get_marathon_app_deploy_status( marathon_client, app) app_status = marathon_app_status( app, marathon_client, dashboard_links.get(marathon_client) if dashboard_links else None, deploy_status, list_tasks=verbose > 0, ) job_status_fields["app_statuses"].append(app_status) if app.id.lstrip("/") == desired_app_id.lstrip("/"): deploy_status_for_desired_app = marathon_tools.MarathonDeployStatus.tostring( deploy_status) tasks_running += app.tasks_running job_status_fields["deploy_status"] = (deploy_status_for_desired_app or "Waiting for bounce") job_status_fields["running_instance_count"] = tasks_running if verbose > 0: autoscaling_info = get_autoscaling_info(marathon_apps_with_clients, job_config) if autoscaling_info is not None: autoscaling_info_dict = autoscaling_info._asdict() for field in ("current_utilization", "target_instances"): if autoscaling_info_dict[field] is None: del autoscaling_info_dict[field] job_status_fields["autoscaling_info"] = autoscaling_info_dict return job_status_fields