DataSourceCloudSigma.py 4.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# vi: ts=4 expandtab
#
#    Copyright (C) 2014 CloudSigma
#
#    Author: Kiril Vladimiroff <kiril.vladimiroff@cloudsigma.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License version 3, as
#    published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
from base64 import b64decode
Scott Moser's avatar
Scott Moser committed
19
import os
20 21
import re

22 23
from cloudinit.cs_utils import Cepko

24 25
from cloudinit import log as logging
from cloudinit import sources
26
from cloudinit import util
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

LOG = logging.getLogger(__name__)

VALID_DSMODES = ("local", "net", "disabled")


class DataSourceCloudSigma(sources.DataSource):
    """
    Uses cepko in order to gather the server context from the VM.

    For more information about CloudSigma's Server Context:
    http://cloudsigma-docs.readthedocs.org/en/latest/server_context.html
    """
    def __init__(self, sys_cfg, distro, paths):
        self.dsmode = 'local'
        self.cepko = Cepko()
        self.ssh_public_key = ''
        sources.DataSource.__init__(self, sys_cfg, distro, paths)

46 47
    def is_running_in_cloudsigma(self):
        """
48
        Uses dmi data to detect if this instance of cloud-init is running
49 50
        in the CloudSigma's infrastructure.
        """
Scott Moser's avatar
Scott Moser committed
51 52
        uname_arch = os.uname()[4]
        if uname_arch.startswith("arm") or uname_arch == "aarch64":
53
            # Disabling because dmi data on ARM processors
Scott Moser's avatar
Scott Moser committed
54 55 56
            LOG.debug("Disabling CloudSigma datasource on arm (LP: #1243287)")
            return False

57 58 59
        LOG.debug("determining hypervisor product name via dmi data")
        sys_product_name = util.read_dmi_data("system-product-name")
        if not sys_product_name:
60
            LOG.debug("system-product-name not available in dmi data")
61
            return False
62
        else:
63
            LOG.debug("detected hypervisor as %s", sys_product_name)
64
            return 'cloudsigma' in sys_product_name.lower()
65

66
        LOG.warn("failed to query dmi data for system product name")
67 68
        return False

69 70 71 72 73
    def get_data(self):
        """
        Metadata is the whole server context and /meta/cloud-config is used
        as userdata.
        """
74
        dsmode = None
75 76 77
        if not self.is_running_in_cloudsigma():
            return False

78 79 80
        try:
            server_context = self.cepko.all().result
            server_meta = server_context['meta']
81
        except Exception:
82 83 84
            # TODO: check for explicit "config on", and then warn
            # but since no explicit config is available now, just debug.
            LOG.debug("CloudSigma: Unable to read from serial port")
85
            return False
86 87 88 89 90 91 92 93

        dsmode = server_meta.get('cloudinit-dsmode', self.dsmode)
        if dsmode not in VALID_DSMODES:
            LOG.warn("Invalid dsmode %s, assuming default of 'net'", dsmode)
            dsmode = 'net'
        if dsmode == "disabled" or dsmode != self.dsmode:
            return False

94
        base64_fields = server_meta.get('base64_fields', '').split(',')
95
        self.userdata_raw = server_meta.get('cloudinit-user-data', "")
96 97
        if 'cloudinit-user-data' in base64_fields:
            self.userdata_raw = b64decode(self.userdata_raw)
98 99
        if 'cloudinit' in server_context.get('vendor_data', {}):
            self.vendordata_raw = server_context["vendor_data"]["cloudinit"]
100

101 102 103
        self.metadata = server_context
        self.ssh_public_key = server_meta['ssh_public_key']

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        return True

    def get_hostname(self, fqdn=False, resolve_ip=False):
        """
        Cleans up and uses the server's name if the latter is set. Otherwise
        the first part from uuid is being used.
        """
        if re.match(r'^[A-Za-z0-9 -_\.]+$', self.metadata['name']):
            return self.metadata['name'][:61]
        else:
            return self.metadata['uuid'].split('-')[0]

    def get_public_ssh_keys(self):
        return [self.ssh_public_key]

    def get_instance_id(self):
        return self.metadata['uuid']


123 124 125 126 127 128
class DataSourceCloudSigmaNet(DataSourceCloudSigma):
    def __init__(self, sys_cfg, distro, paths):
        DataSourceCloudSigma.__init__(self, sys_cfg, distro, paths)
        self.dsmode = 'net'


129 130 131 132
# Used to match classes to dependencies. Since this datasource uses the serial
# port network is not really required, so it's okay to load without it, too.
datasources = [
    (DataSourceCloudSigma, (sources.DEP_FILESYSTEM)),
133
    (DataSourceCloudSigmaNet, (sources.DEP_FILESYSTEM, sources.DEP_NETWORK)),
134 135 136 137 138 139 140 141
]


def get_datasource_list(depends):
    """
    Return a list of data sources that match this set of dependencies
    """
    return sources.list_from_depends(depends, datasources)