Adrian Vladu 526d939240 Add support for packet json metadata service
Packet is a NYC-based infrastructure startup, focused on
reinventing how SaaS/PaaS companies go global with premium
bare metal and container hosting.

This commit adds the following features:

    - ssh keys addition
    - userdata execution
    - hostname setting

Partially implements: blueprint packet-metadata-suport
Co-Authored-By: Alexandru Coman <acoman@cloudbasesolutions.com>
Co-Authored-By: Stefan Caraiman <scaraiman@cloudbasesolutions.com>
Co-Authored-By: Paula Madalina Crismaru <pcrismaru@cloudbasesolutions.com>

Change-Id: I80b4f8b91fd8ef2b63735d5569a8009e34cee759
2019-10-21 15:18:41 +03:00

86 lines
2.9 KiB
Python

# Copyright 2016 Cloudbase Solutions Srl
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Metadata Service for Packet."""
import json
from cloudbaseinit import conf as cloudbaseinit_conf
from cloudbaseinit.metadata.services import base
from oslo_log import log as oslo_logging
CONF = cloudbaseinit_conf.CONF
LOG = oslo_logging.getLogger(__name__)
class PacketService(base.BaseHTTPMetadataService):
"""Metadata Service for Packet.
Packet is a NYC-based infrastructure startup, focused on reinventing
how SaaS/PaaS companies go global with premium bare metal and container
hosting.
"""
def __init__(self):
super(PacketService, self).__init__(
base_url=CONF.packet.metadata_base_url,
https_allow_insecure=CONF.packet.https_allow_insecure,
https_ca_bundle=CONF.packet.https_ca_bundle)
self._enable_retry = True
def _get_meta_data(self):
data = self._get_cache_data("metadata", decode=True)
if data:
return json.loads(data)
def load(self):
"""Load all the available information from the metadata service."""
super(PacketService, self).load()
try:
self._get_meta_data()
return True
except Exception:
LOG.debug('Metadata not found at URL \'%s\'' %
CONF.packet.metadata_base_url)
return False
def get_instance_id(self):
"""Get the identifier for the current instance.
The instance identifier provides an unique way to address an
instance into the current metadata provider.
"""
return self._get_meta_data().get("id")
def get_host_name(self):
"""Get the hostname for the current instance.
The hostname is the label assigned to the current instance used to
identify it in various forms of electronic communication.
"""
return self._get_meta_data().get("hostname")
def get_public_keys(self):
"""Get a list of space-stripped strings as public keys."""
meta_data = self._get_meta_data()
ssh_keys = meta_data.get("ssh_keys")
if not ssh_keys:
return []
return list(set((key.strip() for key in ssh_keys)))
def get_user_data(self):
"""Get the available user data for the current instance."""
return self._get_cache_data("userdata")