1- import json
2-
3- from typing import cast , Optional
1+ from typing import cast , Optional , Set
42
53from sebs .cache import Cache
64from sebs .faas .config import Config , Credentials , Resources
75from sebs .storage .minio import MinioConfig
8- from sebs .utils import serialize , LoggingHandlers
6+ from sebs .utils import LoggingHandlers
97
108
119class LocalCredentials (Credentials ):
@@ -28,41 +26,59 @@ def __init__(self, storage_cfg: Optional[MinioConfig] = None):
2826 self ._path : str = ""
2927 super ().__init__ (name = "local" )
3028 self ._storage = storage_cfg
31- self ._allocated_ports = set ()
29+ self ._allocated_ports : Set [ int ] = set ()
3230
3331 @property
3432 def storage_config (self ) -> Optional [MinioConfig ]:
3533 return self ._storage
3634
37- @property
38- def path (self ) -> str :
39- return self ._path
40-
4135 @property
4236 def allocated_ports (self ) -> set :
4337 return self ._allocated_ports
4438
4539 def serialize (self ) -> dict :
46- out = {
47- "allocated_ports" : list (self ._allocated_ports )
48- }
40+ out : dict = {}
41+ out ["allocated_ports" ] = list (self ._allocated_ports )
42+ if self ._storage is not None :
43+ out ["storage" ] = self ._storage .serialize ()
4944 return out
5045
5146 @staticmethod
52- def initialize (res : Resources , cfg : dict ):
53- pass
47+ def initialize (res : Resources , config : dict ):
5448
55- @staticmethod
56- def deserialize (config : dict , cache : Cache , handlers : LoggingHandlers ) -> Resources :
57- ret = LocalResources ()
58- ret ._path = config ["path" ]
49+ resources = cast (LocalResources , res )
5950 # Check for new config
6051 if "storage" in config :
61- ret ._storage = MinioConfig .deserialize (config ["storage" ])
62- ret .logging .info ("Using user-provided configuration of storage for local containers." )
52+ resources ._storage = MinioConfig .deserialize (config ["storage" ])
53+ resources .logging .info (
54+ "Using user-provided configuration of storage for local containers."
55+ )
6356
6457 if "allocated_ports" in config :
65- ret ._allocated_ports = set (config ["allocated_ports" ])
58+ resources ._allocated_ports = set (config ["allocated_ports" ])
59+
60+ def update_cache (self , cache : Cache ):
61+ super ().update_cache (cache )
62+ cache .update_config (
63+ val = list (self ._allocated_ports ), keys = ["local" , "resources" , "allocated_ports" ]
64+ )
65+ if self ._storage is not None :
66+ self ._storage .update_cache (["local" , "resources" , "storage" ], cache )
67+
68+ @staticmethod
69+ def deserialize (config : dict , cache : Cache , handlers : LoggingHandlers ) -> Resources :
70+ ret = LocalResources ()
71+
72+ cached_config = cache .get_config ("local" )
73+ # Load cached values
74+ if cached_config and "resources" in cached_config :
75+ LocalResources .initialize (ret , cached_config ["resources" ])
76+ ret .logging_handlers = handlers
77+ ret .logging .info ("Using cached resources for Local" )
78+ else :
79+ # Check for new config
80+ ret .logging_handlers = handlers
81+ LocalResources .initialize (ret , config )
6682
6783 return ret
6884
@@ -104,13 +120,8 @@ def deserialize(config: dict, cache: Cache, handlers: LoggingHandlers) -> Config
104120 return config_obj
105121
106122 def serialize (self ) -> dict :
107- with open (self .resources .path , "r+" ) as out :
108- config = json .load (out )
109- config ["deployment" ]["local" ].update (self .resources .serialize ())
110- out .seek (0 )
111- out .write (serialize (config ))
112-
113- return {}
123+ out = {"name" : "local" , "region" : self ._region , "resources" : self ._resources .serialize ()}
124+ return out
114125
115126 def update_cache (self , cache : Cache ):
116- pass
127+ self . resources . update_cache ( cache )
0 commit comments