def get_image_creation_time(self, name, tag='latest'): images_response = json.loads(armada_api.get('images/{}'.format(name))) if images_response['status'] == 'ok': image_infos = json.loads(images_response['image_info']) name_with_tag = '{}:{}'.format(name, tag) image_info_for_tag = [image_info for image_info in image_infos if name_with_tag in image_info['RepoTags']] if image_info_for_tag: return datetime.utcfromtimestamp(int(image_info_for_tag[0]['Created'])).isoformat() return None
def get_image_creation_time(self, image_path, tag='latest'): images_response = json.loads( armada_api.get('images/{}'.format(image_path))) if images_response['status'] != 'ok': return None image_infos = json.loads(images_response['image_info']) name_with_tag = '{}:{}'.format(image_path, tag) for image_info in image_infos: repo_tags = image_info.get('RepoTags') or [] if name_with_tag in repo_tags: return datetime.utcfromtimestamp(int( image_info['Created'])).isoformat() return None
def save_container(ship, container_id, status, params=None): try: start_timestamp = kv_get('start_timestamp/{}'.format(container_id)) except: start_timestamp = None if status == 'crashed': service_name = params['microservice_name'] else: service_name = get_env(container_id, 'MICROSERVICE_NAME') params = json.loads( base64.b64decode( get_env(container_id, 'RESTART_CONTAINER_PARAMETERS'))) if not start_timestamp: start_timestamp = str(calendar.timegm(time.gmtime())) address = kv_get('ships/{}/ip'.format(ship)) or ship service_dict = { 'ServiceName': service_name, 'Status': status, 'container_id': container_id, 'params': params, 'start_timestamp': start_timestamp, 'ServiceID': container_id, 'Address': address } kv_set(create_consul_services_key(ship, service_name, container_id), service_dict)
def POST(self): try: # 'startsecs=0' is to avoid restarting consul after `consul leave`. os.system( 'sed -i \'/autorestart=true/cautorestart=false\' /etc/supervisor/conf.d/consul.conf' ) os.system('echo startsecs=0 >> /etc/supervisor/conf.d/consul.conf') os.system('supervisorctl update consul') # As 'supervisorctl update' will restart Consul, we have to wait for it to be running. deadline = time.time() + 15 while time.time() < deadline: try: get_current_datacenter() break except: time.sleep(1) deregister_services(gethostname()) os.system('consul leave') finally: post_data = json.loads(web.data() or '{}') runtime_settings_path = '/opt/armada/runtime_settings.json' if not post_data.get('keep-joined') and os.path.isfile( runtime_settings_path): with open(runtime_settings_path) as f: runtime_settings = json.load(f) runtime_settings['ships'] = [] runtime_settings['is_commander'] = True with open(runtime_settings_path, 'w') as f: json.dump(runtime_settings, f, sort_keys=True, indent=4) return self.status_ok({'message': 'Shutdown complete.'})
def POST(self): try: microservice_data = json.loads(web.data()) register_service_in_consul(microservice_data) result = {'microservice_data': microservice_data} except Exception as e: return self.status_exception('Could not register service.', e) return self.status_ok({'result': result})
def kv_get_recurse(key): query_result = consul_query('kv/{key}?recurse=true'.format(**locals())) if query_result is None: return None return { item['Key'].replace(key, ''): json.loads(base64.b64decode(item['Value'])) for item in query_result }
def get_container_parameters(container_id): url = 'http://localhost/env/{container_id}/RESTART_CONTAINER_PARAMETERS'.format(**locals()) response = requests.get(url) response.raise_for_status() output = response.json() if output['status'] == 'ok': container_parameters = json.loads(base64.b64decode(output['value']).decode()) return container_parameters return None
def docker_pull(docker_api, dockyard_address, image_name, image_tag): image_address = dockyard_address + '/' + image_name pull_output = list(docker_api.pull(image_address, tag=image_tag, insecure_registry=True, stream=True)) for event_json in pull_output: event = json.loads(event_json) error = _get_error_from_docker_pull_event(event) if error: raise DockerException('Cannot pull image {}:{}, error: {}'.format(image_address, image_tag, error))
def get_container_parameters(container_id): url = 'http://localhost/env/{container_id}/RESTART_CONTAINER_PARAMETERS'.format(**locals()) response = requests.get(url) response.raise_for_status() output = response.json() if output['status'] == 'ok': container_parameters = json.loads(base64.b64decode(output['value'])) return container_parameters return None
def get_image_creation_time(self, image_path, tag='latest'): image_name = split_image_path(image_path)[1] url = '{}/v2/{}/manifests/{}'.format(self.url, image_name, tag) manifests = _http_get(url, auth=self.auth).json() if 'history' not in manifests: return None return max([ json.loads(history['v1Compatibility'])['created'] for history in manifests['history'] ])
def POST(self): try: post_data = json.loads(web.data()) except Exception as e: return self.status_exception('API Run: Invalid input JSON.', e) try: short_container_id, service_endpoints = self._run_service(post_data) return self.status_ok({'container_id': short_container_id, 'endpoints': service_endpoints}) except Exception as e: return self.status_exception("Cannot run service", e)
def get_post_parameter(self, parameter_name): try: post_data = json.loads(web.data()) except: return None, 'Invalid input data - invalid json.' try: result = post_data[parameter_name] except: return None, "Invalid input data - no parameter '{0}'.".format(parameter_name) return result, None
def POST(self): try: post_data = json.loads(web.data()) except Exception as e: return self.status_exception('API Run: Invalid input JSON.', e) try: long_container_id = self._create_service(**post_data) return self.status_ok({'long_container_id': long_container_id}) except Exception as e: return self.status_exception("Cannot create service's container.", e)
def consul_query(query, consul_address=None): try: response = consul_get(query, consul_address) if response.status_code == 404: return None return json.loads(response.text) except Exception as e: if 'response' in locals() and hasattr(response, 'text'): logger.debug(response.text) logger.debug(e, exc_info=True) raise ConsulException(url=__get_consul_url(query, consul_address))
def _get_restart_parameters(self, container_id): try: docker_api = docker_client.api() docker_inspect = docker_api.inspect_container(container_id) for env_var in docker_inspect['Config']['Env']: env_key, env_value = (env_var.strip('"').split('=', 1) + [''])[:2] if env_key == 'RESTART_CONTAINER_PARAMETERS': return json.loads(base64.b64decode(env_value)) except NotFound: service_list = kv_list('ships/') for service in service_list: if service.split('/')[-1] == container_id: return kv_get(service).get('params')
def _get_restart_parameters(self, container_id): try: docker_api = docker_client.api() docker_inspect = docker_api.inspect_container(container_id) for env_var in docker_inspect['Config']['Env']: env_key, env_value = (env_var.strip('"').split('=', 1) + [''])[:2] if env_key == 'RESTART_CONTAINER_PARAMETERS': return json.loads(base64.b64decode(env_value).decode()) except NotFound: for service in get_services_by_ship(ship=None): if service.split('/')[-1] == container_id: return kv_get(service).get('params')
def POST(self): try: post_data = json.loads(web.data()) except Exception as e: return self.status_exception('API Run: Invalid input JSON.', e) try: short_container_id, service_endpoints = self._run_service( post_data) return self.status_ok({ 'container_id': short_container_id, 'endpoints': service_endpoints }) except Exception as e: return self.status_exception("Cannot run service", e)
def save_container(ship, container_id, status, params=None): try: start_timestamp = kv_get('start_timestamp/{}'.format(container_id)) except: start_timestamp = None if status == 'crashed': name = params['microservice_name'] else: name = get_env(container_id, 'MICROSERVICE_NAME') params = json.loads(base64.b64decode(get_env(container_id, 'RESTART_CONTAINER_PARAMETERS'))) if not start_timestamp: start_timestamp = str(calendar.timegm(time.gmtime())) address = kv_get('ships/{}/ip'.format(ship)) or ship service_dict = { 'ServiceName': name, 'Status': status, 'container_id': container_id, 'params': params, 'start_timestamp': start_timestamp, 'ServiceID': container_id, 'Address': address } kv_set('ships/{}/service/{}/{}'.format(ship, name, container_id), service_dict)
def kv_get(key): query_result = consul_query('kv/{key}'.format(**locals())) if query_result is None: return None return json.loads(base64.b64decode(query_result[0]['Value']))
def _multiset_difference(a, b): a_counter = Counter(json.dumps(x, sort_keys=True) for x in a) b_counter = Counter(json.dumps(x, sort_keys=True) for x in b) difference = a_counter - b_counter return [json.loads(x) for x in difference.elements()]
def kv_get_recurse(key): query_result = consul_query('kv/{key}?recurse=true'.format(**locals())) if query_result is None: return None return {item['Key'].replace(key,''): json.loads(base64.b64decode(item['Value'])) for item in query_result}
def get_image_creation_time(self, name, tag='latest'): url = '{}/v2/{}/manifests/{}'.format(self.url, name, tag) manifests = _http_get(url, auth=self.auth).json() if 'history' not in manifests: return None return max([json.loads(history['v1Compatibility'])['created'] for history in manifests['history']])