def _get_cluster_from_build(self, build): cluster_cache_key = 'cluster-{}-{}'.format(self.name, build['number']) cluster = cache.get(cluster_cache_key) if cluster is None: logger.debug('Build {} missing in cache'.format(cluster_cache_key)) # Prepare build parameters _parameters = [ d for d in build.get('actions', []) if d.get('parameters', []) ] parameters = _parameters[0].get('parameters', []) if _parameters else [] # Try to determine stack name on backend stack_name = '' if build['result'] in ['SUCCESS'] and build.get('description'): stack_name = build['description'].split(' ')[0] # Try to determine cluster_id cluster_uuid_parameter = self.job_parameter_map['cluster_uuid'] _cluster_id = [ p.get('value', '') for p in parameters if p.get('name', '') == cluster_uuid_parameter ] cluster_id = _cluster_id[0] if _cluster_id else None # Try to determine cluster state if build['result']: try: state = STATE_MAP[build['result']] except KeyError: logger.exception('{} is not valid cluster state'.format( build['result'])) state = config.get('CLUSTER_UNKNOWN_STATE') else: state = config.get('CLUSTER_PROVISIONING_STATE') cluster = { 'key': cluster_cache_key, 'name': stack_name, 'id': cluster_id, 'state': state, 'metadata': { 'build_number': build['number'], 'build_timestamp': build['timestamp'], 'build_estimated_duration': build['estimatedDuration'] } } if cluster['state'] != config.get('CLUSTER_PROVISIONING_STATE'): cache.set(cluster_cache_key, cluster, timeout=self.cache_timeout) return cluster
def _get_by_build_number(self): cluster_build_number = self._get_build_number() # Cannot get by build_number if there is no build_number on self.cluster if not cluster_build_number: return {} # Try to get the data from cache cluster_cache_key = 'cluster-{}-{}'.format(self.name, cluster_build_number) cluster = cache.get(cluster_cache_key) if cluster: return cluster # Get build info for the given job ID (build_number) build = self.client.get_build_info(self.provision_job_name, int(cluster_build_number)) cluster = self._get_cluster_from_build(build) return cluster or {}