Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requested Changes #8

Merged
merged 5 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 46 additions & 23 deletions cloudinit/sources/DataSourceVultr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@
# https://www.vultr.com/metadata/

import json
import re

from cloudinit import log as log
from cloudinit import sources
from cloudinit import util

import cloudinit.sources.helpers.vultr as vultr

LOGGER = log.getLogger(__name__)
LOG = log.getLogger(__name__)
BUILTIN_DS_CONFIG = {
'url': 'http://169.254.169.254',
'retries': 30,
'timeout': 2,
'wait': 2
}
CONFIG = BUILTIN_DS_CONFIG.copy()


class DataSourceVultr(sources.DataSource):
Expand All @@ -33,36 +31,35 @@ def __init__(self, sys_cfg, distro, paths):
self.ds_cfg = util.mergemanydict([
util.get_cfg_by_path(sys_cfg, ["datasource", "Vultr"], {}),
BUILTIN_DS_CONFIG])
CONFIG['url'] = self.ds_cfg.get(
BUILTIN_DS_CONFIG['url'] = self.ds_cfg.get(
'url', BUILTIN_DS_CONFIG['url'])
CONFIG['retries'] = self.ds_cfg.get(
BUILTIN_DS_CONFIG['retries'] = self.ds_cfg.get(
'retries', BUILTIN_DS_CONFIG['retries'])
CONFIG['timeout'] = self.ds_cfg.get(
BUILTIN_DS_CONFIG['timeout'] = self.ds_cfg.get(
'timeout', BUILTIN_DS_CONFIG['timeout'])
CONFIG['wait'] = self.ds_cfg.get(
BUILTIN_DS_CONFIG['wait'] = self.ds_cfg.get(
'wait', BUILTIN_DS_CONFIG['wait'])

# Initiate data and check if Vultr
def _get_data(self):
LOGGER.info("Detecting if machine is a Vultr instance")
LOG.debug("Detecting if machine is a Vultr instance")
if not vultr.is_vultr():
LOGGER.info("Machine is not a Vultr instance")
LOG.debug("Machine is not a Vultr instance")
return False

LOGGER.info("Machine is a Vultr instance")
LOG.debug("Machine is a Vultr instance")

config = vultr.generate_config(CONFIG)
config = vultr.generate_config(BUILTIN_DS_CONFIG)

# Dump vendor config so diagnosing failures is manageable
LOGGER.info("Vultr Vendor Config:")
LOGGER.info(json.dumps(config))
LOG.debug("Vultr Vendor Config:")
LOG.debug(json.dumps(config))

md = self.get_metadata()

self.metadata_full = md
self.metadata['instanceid'] = self.metadata_full['instanceid']
self.metadata['local-hostname'] = re.sub(
r'\W+', '', self.metadata_full['hostname'])
self.metadata['local-hostname'] = self.metadata_full['hostname']

# Default hostname is "vultr"
if self.metadata['local-hostname'] == "":
Expand All @@ -75,17 +72,29 @@ def _get_data(self):
self.vendordata_raw = "#cloud-config\n%s" % json.dumps(config)

# Dump some data so diagnosing failures is manageable
LOGGER.info("SUBID: %s", self.metadata['instanceid'])
LOGGER.info("Hostname: %s", self.metadata['local-hostname'])
LOG.debug("SUBID: %s", self.metadata['instanceid'])
LOG.debug("Hostname: %s", self.metadata['local-hostname'])
if self.userdata_raw is not None:
LOGGER.info("User-Data:")
LOGGER.info(self.userdata_raw)
LOG.debug("User-Data:")
LOG.debug(self.userdata_raw)

return True

# Get the metadata by flag
def get_metadata(self):
return vultr.get_metadata(CONFIG)
return vultr.get_cached_metadata(BUILTIN_DS_CONFIG)

# Compare subid as instance id
def check_instance_id(self, sys_cfg):
if not vultr.is_vultr():
return None

# Baremetal has no way to implement this in local
if vultr.is_baremetal():
return None

subid = vultr.get_sysinfo()['subid']
return sources.instance_id_matches_system_uuid(subid)

# Currently unsupported
@property
Expand All @@ -95,12 +104,12 @@ def launch_index(self):
# Write the base configs every time. These are subject to change
@property
def network_config(self):
config = vultr.generate_network_config(CONFIG)
config = vultr.generate_network_config(BUILTIN_DS_CONFIG)
config_raw = json.dumps(config)

# Dump network config so diagnosing failures is manageable
LOGGER.info("Generated Network:")
LOGGER.info(config_raw)
LOG.debug("Generated Network:")
LOG.debug(config_raw)

return config

Expand All @@ -115,4 +124,18 @@ def network_config(self):
def get_datasource_list(depends):
return sources.list_from_depends(depends, datasources)


if __name__ == "__main__":
import sys

if not vultr.is_vultr():
print("Machine is not a Vultr instance")
sys.exit(1)

config = vultr.generate_config(BUILTIN_DS_CONFIG)
sysinfo = vultr.get_sysinfo()

print(json.dumps(sysinfo, indent=1))
print(json.dumps(config, indent=1))

# vi: ts=4 expandtab
Loading