class APIShifterImageHandler(ShifterImageHandler, Configurable):
    """Request handler that calls external shifter API"""

    shifter_api_host = Unicode(
        os.environ.get("SHIFTER_API_HOST"),
        help="Hostname of the shifter api").tag(config=True)

    shifter_api_token = Unicode(
        os.environ.get("SHIFTER_API_TOKEN"),
        help="Secret token to access shifter api").tag(config=True)

    # fetch available shifter images from the external API
    # returns list of images or empty list
    # cache results to limit to 1 request per 60 seconds
    @cached(60, key_builder=key_builder, serializer=NullSerializer())
    async def _get(self, user):
        client = httpclient.AsyncHTTPClient()
        try:
            response = await client.fetch(
                f"{self.shifter_api_host}/list/{user}",
                headers={"Authorization": self.shifter_api_token})
        except Exception as e:
            print("Error: %s" % e)
            return []
        else:
            doc = escape.json_decode(response.body)
            images = list()
            for entry in doc.get("images", []):
                env = entry.get("ENV", [])
                if env and "NERSC_JUPYTER_IMAGE=YES" in env:
                    images += entry.get("tag", [])
            return images
 def __init__(
     self,
     serializer: Optional[BaseSerializer] = None,
     name: str = 'smc',
     size: int = 1024,
     **kwargs,
 ):
     super().__init__(**kwargs)
     self.serializer = serializer or NullSerializer()
     self._cache = SharedMemoryDict(name, size)
     self._handlers: Dict[str, TimerHandle] = {}
Esempio n. 3
0
class APIShifterImageHandler(ShifterImageHandler):

    def initialize(self, shifter_api_token, shifter_api_host):
        self.shifter_api_token = shifter_api_token
        self.shifter_api_host = shifter_api_host

    @cached(60, key_builder=key_builder, serializer=NullSerializer())
    async def _get(self, user):
        client = httpclient.AsyncHTTPClient()
        try:
            response = await client.fetch(f"{self.shifter_api_host}/list/{user}",
                headers={"Authorization": self.shifter_api_token})
        except Exception as e:
            print("Error: %s" % e)
            return False
        else:
            doc = escape.json_decode(response.body)
            images = list()
            for entry in doc.get("images", []):
                env = entry.get("ENV", [])
                if env and "NERSC_JUPYTER_IMAGE=YES" in env:
                    images += entry.get("tag", [])
            return images
Esempio n. 4
0
 async def test_multi_set_multi_get_types(self, memory_cache, obj):
     memory_cache.serializer = NullSerializer()
     assert await memory_cache.multi_set([(pytest.KEY, obj)]) is True
     assert (await memory_cache.multi_get([pytest.KEY]))[0] is obj
Esempio n. 5
0
 async def test_add_get_types(self, memory_cache, obj):
     memory_cache.serializer = NullSerializer()
     assert await memory_cache.add(pytest.KEY, obj) is True
     assert await memory_cache.get(pytest.KEY) is obj
Esempio n. 6
0
 def __init__(self, serializer=None, **kwargs):
     super().__init__(**kwargs)
     self.serializer = serializer or NullSerializer()
Esempio n. 7
0
 def test_loads(self):
     assert NullSerializer().loads("hi") is "hi"
Esempio n. 8
0
 def test_set_types(self, obj):
     assert NullSerializer().dumps(obj) is obj
Esempio n. 9
0
 def test_init(self):
     serializer = NullSerializer()
     assert isinstance(serializer, BaseSerializer)
     assert serializer.DEFAULT_ENCODING == 'utf-8'
     assert serializer.encoding == 'utf-8'
Esempio n. 10
0
 def __init__(self, max_ttl=MEMORY_CACHE_MAX_TTL, **kwargs):
     self.max_ttl = max_ttl
     super().__init__(**kwargs)
     self.serializer = NullSerializer()