summaryrefslogtreecommitdiff
path: root/cloudinit
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2014-02-12 16:53:05 -0500
committerScott Moser <smoser@ubuntu.com>2014-02-12 16:53:05 -0500
commit0b0bb4721c61015e3fce9b4030bcb69b9da5c368 (patch)
tree5cda2535587b0c8e357ab4ed0fce920d2be3939f /cloudinit
parent20305aea1eac724069e0bfaaf976ec5caa8c2439 (diff)
parent5d2a31bd66fc5fc10901e30a2b9c79c7f4d1a172 (diff)
downloadvyos-cloud-init-0b0bb4721c61015e3fce9b4030bcb69b9da5c368.tar.gz
vyos-cloud-init-0b0bb4721c61015e3fce9b4030bcb69b9da5c368.zip
merge from trunk
Diffstat (limited to 'cloudinit')
-rw-r--r--cloudinit/config/cc_ssh_import_id.py3
-rw-r--r--cloudinit/cs_utils.py99
-rw-r--r--cloudinit/settings.py1
-rw-r--r--cloudinit/sources/DataSourceCloudSigma.py91
-rw-r--r--cloudinit/stages.py36
5 files changed, 218 insertions, 12 deletions
diff --git a/cloudinit/config/cc_ssh_import_id.py b/cloudinit/config/cc_ssh_import_id.py
index 50d96e15..76c1663d 100644
--- a/cloudinit/config/cc_ssh_import_id.py
+++ b/cloudinit/config/cc_ssh_import_id.py
@@ -26,9 +26,8 @@ from cloudinit import distros as ds
from cloudinit import util
import pwd
-# The ssh-import-id only seems to exist on ubuntu (for now)
# https://launchpad.net/ssh-import-id
-distros = ['ubuntu']
+distros = ['ubuntu', 'debian']
def handle(_name, cfg, cloud, log, args):
diff --git a/cloudinit/cs_utils.py b/cloudinit/cs_utils.py
new file mode 100644
index 00000000..4e53c31a
--- /dev/null
+++ b/cloudinit/cs_utils.py
@@ -0,0 +1,99 @@
+# 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/>.
+"""
+cepko implements easy-to-use communication with CloudSigma's VMs through
+a virtual serial port without bothering with formatting the messages
+properly nor parsing the output with the specific and sometimes
+confusing shell tools for that purpose.
+
+Having the server definition accessible by the VM can ve useful in various
+ways. For example it is possible to easily determine from within the VM,
+which network interfaces are connected to public and which to private network.
+Another use is to pass some data to initial VM setup scripts, like setting the
+hostname to the VM name or passing ssh public keys through server meta.
+
+For more information take a look at the Server Context section of CloudSigma
+API Docs: http://cloudsigma-docs.readthedocs.org/en/latest/server_context.html
+"""
+import json
+import platform
+
+import serial
+
+SERIAL_PORT = '/dev/ttyS1'
+if platform.system() == 'Windows':
+ SERIAL_PORT = 'COM2'
+
+
+class Cepko(object):
+ """
+ One instance of that object could be use for one or more
+ queries to the serial port.
+ """
+ request_pattern = "<\n{}\n>"
+
+ def get(self, key="", request_pattern=None):
+ if request_pattern is None:
+ request_pattern = self.request_pattern
+ return CepkoResult(request_pattern.format(key))
+
+ def all(self):
+ return self.get()
+
+ def meta(self, key=""):
+ request_pattern = self.request_pattern.format("/meta/{}")
+ return self.get(key, request_pattern)
+
+ def global_context(self, key=""):
+ request_pattern = self.request_pattern.format("/global_context/{}")
+ return self.get(key, request_pattern)
+
+
+class CepkoResult(object):
+ """
+ CepkoResult executes the request to the virtual serial port as soon
+ as the instance is initialized and stores the result in both raw and
+ marshalled format.
+ """
+ def __init__(self, request):
+ self.request = request
+ self.raw_result = self._execute()
+ self.result = self._marshal(self.raw_result)
+
+ def _execute(self):
+ connection = serial.Serial(SERIAL_PORT)
+ connection.write(self.request)
+ return connection.readline().strip('\x04\n')
+
+ def _marshal(self, raw_result):
+ try:
+ return json.loads(raw_result)
+ except ValueError:
+ return raw_result
+
+ def __len__(self):
+ return self.result.__len__()
+
+ def __getitem__(self, key):
+ return self.result.__getitem__(key)
+
+ def __contains__(self, item):
+ return self.result.__contains__(item)
+
+ def __iter__(self):
+ return self.result.__iter__()
diff --git a/cloudinit/settings.py b/cloudinit/settings.py
index 7be2199a..7b0b18e7 100644
--- a/cloudinit/settings.py
+++ b/cloudinit/settings.py
@@ -37,6 +37,7 @@ CFG_BUILTIN = {
'OVF',
'MAAS',
'Ec2',
+ 'CloudSigma',
'CloudStack',
'SmartOS',
# At the end to act as a 'catch' when none of the above work...
diff --git a/cloudinit/sources/DataSourceCloudSigma.py b/cloudinit/sources/DataSourceCloudSigma.py
new file mode 100644
index 00000000..78acd8a4
--- /dev/null
+++ b/cloudinit/sources/DataSourceCloudSigma.py
@@ -0,0 +1,91 @@
+# 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/>.
+import re
+
+from cloudinit import log as logging
+from cloudinit import sources
+from cloudinit import util
+from cloudinit.cs_utils import Cepko
+
+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)
+
+ def get_data(self):
+ """
+ Metadata is the whole server context and /meta/cloud-config is used
+ as userdata.
+ """
+ try:
+ server_context = self.cepko.all().result
+ server_meta = server_context['meta']
+ self.userdata_raw = server_meta.get('cloudinit-user-data', "")
+ self.metadata = server_context
+ self.ssh_public_key = server_meta['ssh_public_key']
+
+ if server_meta.get('cloudinit-dsmode') in VALID_DSMODES:
+ self.dsmode = server_meta['cloudinit-dsmode']
+ except:
+ util.logexc(LOG, "Failed reading from the serial port")
+ return False
+ 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']
+
+
+# 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)),
+ (DataSourceCloudSigma, (sources.DEP_FILESYSTEM, sources.DEP_NETWORK)),
+]
+
+
+def get_datasource_list(depends):
+ """
+ Return a list of data sources that match this set of dependencies
+ """
+ return sources.list_from_depends(depends, datasources)
diff --git a/cloudinit/stages.py b/cloudinit/stages.py
index 593b72a2..7acd3355 100644
--- a/cloudinit/stages.py
+++ b/cloudinit/stages.py
@@ -632,7 +632,6 @@ class Modules(object):
return mostly_mods
def _run_modules(self, mostly_mods):
- d_name = self.init.distro.name
cc = self.init.cloudify()
# Return which ones ran
# and which ones failed + the exception of why it failed
@@ -646,15 +645,6 @@ class Modules(object):
if not freq in FREQUENCIES:
freq = PER_INSTANCE
- worked_distros = set(mod.distros)
- worked_distros.update(
- distros.Distro.expand_osfamily(mod.osfamilies))
-
- if (worked_distros and d_name not in worked_distros):
- LOG.warn(("Module %s is verified on %s distros"
- " but not on %s distro. It may or may not work"
- " correctly."), name, list(worked_distros),
- d_name)
# Use the configs logger and not our own
# TODO(harlowja): possibly check the module
# for having a LOG attr and just give it back
@@ -686,6 +676,32 @@ class Modules(object):
def run_section(self, section_name):
raw_mods = self._read_modules(section_name)
mostly_mods = self._fixup_modules(raw_mods)
+ d_name = self.init.distro.name
+
+ skipped = []
+ forced = []
+ overridden = self.cfg.get('unverified_modules', [])
+ for (mod, name, _freq, _args) in mostly_mods:
+ worked_distros = set(mod.distros)
+ worked_distros.update(
+ distros.Distro.expand_osfamily(mod.osfamilies))
+
+ # module does not declare 'distros' or lists this distro
+ if not worked_distros or d_name in worked_distros:
+ continue
+
+ if name in overridden:
+ forced.append(name)
+ else:
+ skipped.append(name)
+
+ if skipped:
+ LOG.info("Skipping modules %s because they are not verified "
+ "on distro '%s'. To run anyway, add them to "
+ "'unverified_modules' in config.", skipped, d_name)
+ if forced:
+ LOG.info("running unverified_modules: %s", forced)
+
return self._run_modules(mostly_mods)