summaryrefslogtreecommitdiff
path: root/cloudinit
diff options
context:
space:
mode:
Diffstat (limited to 'cloudinit')
-rw-r--r--cloudinit/cs_utils.py99
-rw-r--r--cloudinit/settings.py1
-rw-r--r--cloudinit/sources/DataSourceCloudSigma.py91
3 files changed, 191 insertions, 0 deletions
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)