<s> import docker <EOL> import json <EOL> from peewee import ( Model , SqliteDatabase , ForeignKeyField , CharField , OperationalError , <EOL> sort_models_topologically , DoesNotExist ) <EOL> from functools import wraps <EOL> GANTRY_METADATA_FILE = '<STR_LIT>' <EOL> cached_metadata = None <EOL> db = SqliteDatabase ( GANTRY_METADATA_FILE ) <EOL> class BaseModel ( Model ) : <EOL> class Meta : <EOL> database = db <EOL> class Component ( BaseModel ) : <EOL> name = CharField ( index = True ) <EOL> class ComponentField ( BaseModel ) : <EOL> component = ForeignKeyField ( Component ) <EOL> key = CharField ( index = True ) <EOL> value = CharField ( ) <EOL> class Container ( BaseModel ) : <EOL> docker_id = CharField ( index = True ) <EOL> component = ForeignKeyField ( Component , null = True ) <EOL> class ContainerField ( BaseModel ) : <EOL> container = ForeignKeyField ( Container ) <EOL> key = CharField ( index = True ) <EOL> value = CharField ( ) <EOL> class Meta : <EOL> database = db <EOL> indexes = ( <EOL> ( ( '<STR_LIT>' , '<STR_LIT:key>' ) , True ) , <EOL> ) <EOL> all_models = [ Component , ComponentField , Container , ContainerField ] <EOL> def _initialze_db ( ) : <EOL> for model in sort_models_topologically ( all_models ) : <EOL> try : <EOL> model . select ( ) . get ( ) <EOL> except OperationalError as exc : <EOL> model . create_table ( ) <EOL> except DoesNotExist : <EOL> pass <EOL> def db_access ( to_wrap ) : <EOL> @ wraps ( to_wrap ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> _initialze_db ( ) <EOL> try : <EOL> return to_wrap ( * args , ** kwargs ) <EOL> finally : <EOL> if not db . is_closed ( ) : <EOL> db . close ( ) <EOL> return wrapper <EOL> def getContainerStatus ( container ) : <EOL> """<STR_LIT>""" <EOL> return _getContainerField ( container , '<STR_LIT:status>' , default = '<STR_LIT>' ) <EOL> def setContainerStatus ( container , status ) : <EOL> """<STR_LIT>""" <EOL> _setContainerField ( container , '<STR_LIT:status>' , status ) <EOL> @ db_access <EOL> def getContainerComponent ( container ) : <EOL> """<STR_LIT>""" <EOL> container_record = _upsertContainerRecord ( container ) <EOL> return container_record . component and container_record . component . name <EOL> @ db_access <EOL> def setContainerComponent ( container , component_name ) : <EOL> """<STR_LIT>""" <EOL> component = _upsertComponentRecord ( component_name ) <EOL> container_record = _upsertContainerRecord ( container ) <EOL> container_record . component = component <EOL> container_record . save ( ) <EOL> def _getContainerId ( container_or_id ) : <EOL> return container_or_id [ '<STR_LIT>' ] if isinstance ( container_or_id , dict ) else container_or_id <EOL> @ db_access <EOL> def removeContainerMetadata ( container ) : <EOL> found = _upsertContainerRecord ( container ) <EOL> found . delete_instance ( recursive = True ) <EOL> def _getContainerFieldRecord ( container , field ) : <EOL> try : <EOL> return ( ContainerField <EOL> . select ( ) <EOL> . join ( Container ) <EOL> . where ( Container . docker_id == container , ContainerField . key == field ) <EOL> . get ( ) ) <EOL> except ContainerField . DoesNotExist : <EOL> return None <EOL> def _upsertContainerRecord ( container ) : <EOL> container_id = _getContainerId ( container ) <EOL> try : <EOL> return ( Container <EOL> . select ( ) <EOL> . where ( Container . docker_id == container_id ) <EOL> . get ( ) ) <EOL> except Container . DoesNotExist : <EOL> return Container . create ( docker_id = container_id ) <EOL> @ db_access <EOL> def _getContainerField ( container , field , default ) : <EOL> """<STR_LIT>""" <EOL> container_id = _getContainerId ( container ) <EOL> found = _getContainerFieldRecord ( container_id , field ) <EOL> return found . value if found else default <EOL> @ db_access <EOL> def _setContainerField ( container , field , value ) : <EOL> """<STR_LIT>""" <EOL> container_id = _getContainerId ( container ) <EOL> found = _getContainerFieldRecord ( container_id , field ) <EOL> if found is not None : <EOL> found . value = value <EOL> found . save ( ) <EOL> else : <EOL> container_record = _upsertContainerRecord ( container_id ) <EOL> ContainerField . create ( container = container_record , key = field , value = value ) <EOL> def _upsertComponentRecord ( component ) : <EOL> try : <EOL> return ( Component <EOL> . select ( ) <EOL> . where ( Component . name == component ) <EOL> . get ( ) ) <EOL> except Component . DoesNotExist : <EOL> return Component . create ( name = component ) <EOL> def _getComponentFieldRecord ( component_name , field ) : <EOL> try : <EOL> return ( ComponentField <EOL> . select ( ) <EOL> . join ( Component ) <EOL> . where ( Component . name == component_name , ComponentField . key == field ) <EOL> . get ( ) ) <EOL> except ComponentField . DoesNotExist : <EOL> return None <EOL> @ db_access <EOL> def getComponentField ( component_name , field , default ) : <EOL> """<STR_LIT>""" <EOL> found = _getComponentFieldRecord ( component_name , field ) <EOL> return found . value if found else default <EOL> @ db_access <EOL> def setComponentField ( component_name , field , value ) : <EOL> """<STR_LIT>""" <EOL> found = _getComponentFieldRecord ( component_name , field ) <EOL> if found is not None : <EOL> found . value = value <EOL> found . save ( ) <EOL> else : <EOL> component = _upsertComponentRecord ( component_name ) <EOL> ComponentField . create ( component = component , key = field , value = value ) </s>
