Skip to content

Commit

Permalink
Add metadata servvice VMwareGuestInfoService
Browse files Browse the repository at this point in the history
VMwareGuestInfoService is a metadata service which uses VMware's
rpctool to extract guest metadata and userdata configured for machines
running on VMware hypervisors.

The implementation is similar to:
https://github.com/vmware/cloud-init-vmware-guestinfo

Supported features for the metadata service:
  * instance id
  * hostname
  * admin username
  * admin password
  * public SSH keys
  * userdata

Configuration options:
```ini

[vmwarequestinfo]
vmware_rpctool_path=%ProgramFiles%/VMware/VMware Tools/rpctool.exe

```

The VMware RPC tool used to query the instance metadata and userdata
needs to be present at the config option path.

Both json and yaml are supported as metadata formats.
The metadata / userdata can be encoded in base64, gzip or gzip+base64.

Example metadata in yaml format:

```yaml
instance-id: cloud-vm
local-hostname: cloud-vm
admin-username: cloud-username
admin-password: Passw0rd
public-keys-data: |
  ssh-key 1
  ssh-key 2
```

This metadata content needs to be sent as string in the guestinfo
dictionary, thus needs to be converted to base64 (it is recommended to
gzip it too).

To convert to gzip+base64 format:

```bash
cat metadata.yml | gzip.exe -9 | base64.exe -w0
```

Co-Authored-By: Rui Lopes <[email protected]>
Change-Id: I6a8430e87ee03d2e8fdd2685b05e60c5c0ffb5be
  • Loading branch information
ader1990 and rgl committed Jan 29, 2020
1 parent fd2c15b commit a77477e
Show file tree
Hide file tree
Showing 5 changed files with 496 additions and 0 deletions.
1 change: 1 addition & 0 deletions cloudbaseinit/conf/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'cloudbaseinit.conf.azure.AzureOptions',
'cloudbaseinit.conf.ovf.OvfOptions',
'cloudbaseinit.conf.packet.PacketOptions',
'cloudbaseinit.conf.vmwareguestinfo.VMwareGuestInfoConfigOptions',
)


Expand Down
46 changes: 46 additions & 0 deletions cloudbaseinit/conf/vmwareguestinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2020 Cloudbase Solutions Srl
# Copyright 2019 ruilopes.com
#
# 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.

"""Config options available for the VMware metadata service."""

from oslo_config import cfg

from cloudbaseinit.conf import base as conf_base


class VMwareGuestInfoConfigOptions(conf_base.Options):

"""Config options available for the VMware GuestInfo metadata service."""

def __init__(self, config):
super(VMwareGuestInfoConfigOptions, self).__init__(
config, group="vmwareguestinfo")
self._options = [
cfg.StrOpt(
'vmware_rpctool_path',
default="%ProgramFiles%/VMware/VMware Tools/rpctool.exe",
help='The local path where VMware rpctool is found')
]

def register(self):
"""Register the current options to the global ConfigOpts object."""
group = cfg.OptGroup(self.group_name,
title='VMware GuestInfo Options')
self._config.register_group(group)
self._config.register_opts(self._options, group=group)

def list(self):
"""Return a list which contains all the available options."""
return self._options
171 changes: 171 additions & 0 deletions cloudbaseinit/metadata/services/vmwareguestinfoservice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# Copyright 2020 Cloudbase Solutions Srl
# Copyright 2019 ruilopes.com
#
# 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.

import base64
import gzip
import io
import json
import os
import yaml

from oslo_log import log as oslo_logging

from cloudbaseinit import conf as cloudbaseinit_conf
from cloudbaseinit import exception
from cloudbaseinit.metadata.services import base
from cloudbaseinit.osutils import factory as osutils_factory

CONF = cloudbaseinit_conf.CONF
LOG = oslo_logging.getLogger(__name__)


class YamlParserConfigError(Exception):
"""Exception for Yaml parsing failures"""
pass


class VMwareGuestInfoService(base.BaseMetadataService):
def __init__(self):
super(VMwareGuestInfoService, self).__init__()
self._rpc_tool_path = None
self._osutils = osutils_factory.get_os_utils()
self._meta_data = {}
self._user_data = None

@staticmethod
def _parse_data(raw_data):
"""Parse data as json. Fallback to yaml if json parsing fails"""

try:
return json.loads(raw_data)
except (TypeError, ValueError, AttributeError):
loader = getattr(yaml, 'CLoader', yaml.Loader)
try:
return yaml.load(raw_data, Loader=loader)
except (TypeError, ValueError, AttributeError):
raise YamlParserConfigError("Invalid yaml data provided.")

@staticmethod
def _decode_data(raw_data, is_base64, is_gzip):
"""Decode raw_data from base64 / ungzip"""
if not raw_data:
return

if is_base64:
raw_data = base64.b64decode(raw_data)

if is_gzip:
with gzip.GzipFile(fileobj=io.BytesIO(raw_data), mode='rb') as dt:
raw_data = dt.read()

return raw_data

def _get_guestinfo_value(self, key):
rpc_command = 'info-get guestinfo.%s' % key
data, stderr, exit_code = self._osutils.execute_process([
self._rpc_tool_path,
rpc_command
])
if exit_code:
LOG.debug(
'Failed to execute "%(rpctool_path)s \'%(rpc_command)s\'" '
'with exit code: %(exit_code)s\nstdout: '
'%(stdout)s\nstderr: %(stderr)s' % {
'rpctool_path': self._rpc_tool_path,
'rpc_command': rpc_command, 'exit_code': exit_code,
'stdout': data, 'stderr': stderr})
return

return data

def _get_guest_data(self, key):
is_base64 = False
is_gzip = False
encoding_plain_text = 'plaintext'
raw_data = self._get_guestinfo_value(key)
raw_encoding = self._get_guestinfo_value("%s.encoding" % key)

if not raw_encoding or not raw_encoding.strip():
raw_encoding = encoding_plain_text

encoding = raw_encoding.strip()
if isinstance(encoding, bytes):
encoding = encoding.decode("utf-8")

if encoding in ('gzip+base64', 'gz+b64'):
is_gzip = True
is_base64 = True
elif encoding in ('base64', 'b64'):
is_base64 = True
elif encoding != encoding_plain_text:
raise exception.CloudbaseInitException(
"Encoding %s not supported" % encoding)

LOG.debug("Decoding key %s: encoding %s", key, encoding)
return self._decode_data(raw_data, is_base64, is_gzip)

def load(self):
super(VMwareGuestInfoService, self).load()

if not CONF.vmwareguestinfo.vmware_rpctool_path:
LOG.info("rpctool_path is empty. "
"Please provide a value for VMware rpctool path.")
return False

self._rpc_tool_path = os.path.abspath(
os.path.expandvars(CONF.vmwareguestinfo.vmware_rpctool_path))

if not os.path.exists(self._rpc_tool_path):
LOG.info("%s does not exist. "
"Please provide a valid value for VMware rpctool path."
% self._rpc_tool_path)
return False

self._meta_data = self._parse_data(self._get_guest_data('metadata'))
if not isinstance(self._meta_data, dict):
LOG.warning("Instance metadata is not a dictionary.")
self._meta_data = {}

self._user_data = self._get_guest_data('userdata')

if self._meta_data or self._user_data:
return True

def _get_data(self, path):
pass

def get_instance_id(self):
return self._meta_data.get('instance-id')

def get_user_data(self):
return self._user_data

def get_host_name(self):
return self._meta_data.get('local-hostname')

def get_public_keys(self):
public_keys = []
public_keys_data = self._meta_data.get('public-keys-data')

if public_keys_data:
public_keys = public_keys_data.splitlines()

return list(set((key.strip() for key in public_keys)))

def get_admin_username(self):
return self._meta_data.get('admin-username')

def get_admin_password(self):
return self._meta_data.get('admin-password')
Loading

0 comments on commit a77477e

Please sign in to comment.