Adam Dobrawy has proposed merging ~adobrawy/cloud-init:rbx-datasource into
cloud-init:master.
Requested reviews:
cloud-init commiters (cloud-init-dev)
For more details, see:
https://code.launchpad.net/~adobrawy/cloud-init/+git/cloud-init/+merge/354679
--
Your team cloud-init commiters is requested to review the proposed merge of
~adobrawy/cloud-init:rbx-datasource into cloud-init:master.
diff --git a/cloudinit/sources/DataSourceRbxCloud.py b/cloudinit/sources/DataSourceRbxCloud.py
new file mode 100644
index 000..736b860
--- /dev/null
+++ b/cloudinit/sources/DataSourceRbxCloud.py
@@ -0,0 +1,343 @@
+# vi: ts=4 expandtab
+# Copyright (C) 2016 Warsaw Data Center
+#
+# Author: Malwina Leis
+# Author: Grzegorz Brzeski
+#
+# This file is part of cloud-init. See LICENSE file for license information.
+'''Datasource for rootbox / hyperone cloud platforms'''
+import errno
+import os
+import os.path
+
+import socket
+
+from cloudinit import log as logging
+from cloudinit import sources
+from cloudinit import util
+from cloudinit.netinfo import netdev_info
+
+LOG = logging.getLogger(__name__)
+
+
+def _read_file(filepath):
+try:
+content = util.load_file(filepath).strip()
+except IOError:
+util.logexc(LOG, 'Failed accessing file: ' + filepath)
+return None
+return content
+
+
+def _get_meta_data(filepath):
+content = _read_file(filepath)
+if not content:
+return None
+
+try:
+content = util.load_json(content)
+except Exception:
+util.logexc(LOG, 'Failed parsing meta data file from json.')
+return None
+
+return content
+
+
+def read_user_data_callback(mount_dir, distro):
+'''
+Description:
+This callback will be applied by util.mount_cb() on the mounted
+file.
+
+Input:
+mount_dir - Mount directory
+
+Returns:
+User Data
+
+'''
+
+meta_data = _get_meta_data(os.path.join(mount_dir, 'cloud.json'))
+user_data = _read_file(os.path.join(mount_dir, 'user.data'))
+additional_metadata = meta_data['additionalMetadata']
+
+data = {
+'userdata': user_data,
+'metadata': {
+'instance-id': meta_data['vm']['_id'],
+# This puts keys also for root
+# 'public-keys': meta_data['additionalMetadata']['sshKeys'],
+'local-hostname': meta_data['vm']['name']
+},
+'cfg': {
+'ssh_pwauth': True,
+'disable_root': True,
+'system_info': {
+'default_user': {
+'name': additional_metadata.get('username', 'guru'),
+'gecos': additional_metadata.get('username', 'guru'),
+'sudo': ['ALL=(ALL) NOPASSWD:ALL'],
+'lock_passwd': False,
+'ssh_authorized_keys': additional_metadata['sshKeys'],
+'shell': '/bin/bash'
+}
+},
+'runcmd': []
+}
+}
+
+hosts_path = '/etc/hosts'
+hosts = _read_file(hosts_path)
+if hosts:
+data['cfg']['manage_etc_hosts'] = False
+LOG.debug('/etc/hosts exists - set manage_etc_hosts to False')
+else:
+data['cfg']['manage_etc_hosts'] = True
+LOG.debug('/etc/hosts does not exists - set manage_etc_hosts to True')
+
+if meta_data['netadp']:
+netdata = generate_eni(meta_data['netadp'], distro)
+data['metadata']['network-interfaces'] = netdata['eni']
+data['cfg']['runcmd'] = netdata['cmd']
+
+username = meta_data['additionalMetadata'].get('username', 'guru')
+password = meta_data['additionalMetadata']['password']['sha512']
+data['cfg']['runcmd'].append("echo '{username}:{password}' | chpasswd -e"
+ .format(username=username,
+ password=password))
+
+LOG.debug('returning DATA object:')
+LOG.debug(data)
+
+return data
+
+
+def generate_eni(netadp, distro):
+LOG.debug("RBX: generate_eni")
+LOG.debug(netadp)
+
+netdevices = netdev_info()
+
+ENI = []
+CMD = []
+
+ARPING = "arping -c 2 -S "
+ARPING_RHEL = "arping -c 2 -s "
+
+LOG.debug("Generating eni for distro: %s", distro)
+if distro == 'rhel':
+ARPING = ARPING_RHEL
+
+ENI.append('auto lo')
+ENI.append('iface lo inet loopback')
+ENI.append('')
+
+default_nic = [
+n
+for n in sorted(netadp, key=lambda k: k['_id'])
+if n.get('default_gw') == "true"
+]
+if (not default_nic) and netadp:
+default_nic = [sorted(netadp, key=lambda k: k['_id'])[0]]
+LOG.debug("Default interface mac: %s",
+ default_nic[0].get('macaddress'))
+else:
+LOG.debug("No netadp to configure")
+return None
+LOG.debug("Default nic: %s", default_nic[