Rename helm_drydock to drydock_provisioner
This commit is contained in:
116
drydock_provisioner/ingester/__init__.py
Normal file
116
drydock_provisioner/ingester/__init__.py
Normal file
@@ -0,0 +1,116 @@
|
||||
# Copyright 2017 AT&T Intellectual Property. All other rights reserved.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
# ingester - Ingest host topologies to define site design and
|
||||
# persist design to helm-drydock's statemgmt service
|
||||
|
||||
import logging
|
||||
import yaml
|
||||
import uuid
|
||||
import importlib
|
||||
|
||||
import drydock_provisioner.objects as objects
|
||||
import drydock_provisioner.objects.site as site
|
||||
import drydock_provisioner.objects.network as network
|
||||
import drydock_provisioner.objects.hwprofile as hwprofile
|
||||
import drydock_provisioner.objects.node as node
|
||||
import drydock_provisioner.objects.hostprofile as hostprofile
|
||||
|
||||
from drydock_provisioner.statemgmt import DesignState
|
||||
|
||||
class Ingester(object):
|
||||
|
||||
def __init__(self):
|
||||
self.logger = logging.getLogger("drydock.ingester")
|
||||
self.registered_plugins = {}
|
||||
|
||||
def enable_plugins(self, plugins=[]):
|
||||
"""
|
||||
enable_plugins
|
||||
|
||||
:params plugins: - A list of strings naming class objects denoting the ingester plugins to be enabled
|
||||
|
||||
Enable plugins that can be used for ingest_data calls. Each plugin should use
|
||||
drydock_provisioner.ingester.plugins.IngesterPlugin as its base class. As long as one
|
||||
enabled plugin successfully initializes, the call is considered successful. Otherwise
|
||||
it will throw an exception
|
||||
"""
|
||||
if len(plugins) == 0:
|
||||
self.log.error("Cannot have an empty plugin list.")
|
||||
|
||||
for plugin in plugins:
|
||||
try:
|
||||
(module, x, classname) = plugin.rpartition('.')
|
||||
|
||||
if module == '':
|
||||
raise Exception()
|
||||
mod = importlib.import_module(module)
|
||||
klass = getattr(mod, classname)
|
||||
new_plugin = klass()
|
||||
plugin_name = new_plugin.get_name()
|
||||
self.registered_plugins[plugin_name] = new_plugin
|
||||
except Exception as ex:
|
||||
self.logger.error("Could not enable plugin %s - %s" % (plugin, str(ex)))
|
||||
|
||||
if len(self.registered_plugins) == 0:
|
||||
self.logger.error("Could not enable at least one plugin")
|
||||
raise Exception("Could not enable at least one plugin")
|
||||
|
||||
|
||||
def ingest_data(self, plugin_name='', design_state=None, design_id=None, context=None, **kwargs):
|
||||
if design_state is None:
|
||||
self.logger.error("Ingester:ingest_data called without valid DesignState handler")
|
||||
raise ValueError("Invalid design_state handler")
|
||||
|
||||
# If no design_id is specified, instantiate a new one
|
||||
if 'design_id' is None:
|
||||
self.logger.error("Ingester:ingest_data required kwarg 'design_id' missing")
|
||||
raise ValueError("Ingester:ingest_data required kwarg 'design_id' missing")
|
||||
|
||||
design_data = design_state.get_design(design_id)
|
||||
|
||||
self.logger.debug("Ingester:ingest_data ingesting design parts for design %s" % design_id)
|
||||
|
||||
if plugin_name in self.registered_plugins:
|
||||
design_items = self.registered_plugins[plugin_name].ingest_data(**kwargs)
|
||||
self.logger.debug("Ingester:ingest_data parsed %s design parts" % str(len(design_items)))
|
||||
for m in design_items:
|
||||
if context is not None:
|
||||
m.set_create_fields(context)
|
||||
if type(m) is site.Site:
|
||||
design_data.set_site(m)
|
||||
elif type(m) is network.Network:
|
||||
design_data.add_network(m)
|
||||
elif type(m) is network.NetworkLink:
|
||||
design_data.add_network_link(m)
|
||||
elif type(m) is hostprofile.HostProfile:
|
||||
design_data.add_host_profile(m)
|
||||
elif type(m) is hwprofile.HardwareProfile:
|
||||
design_data.add_hardware_profile(m)
|
||||
elif type(m) is node.BaremetalNode:
|
||||
design_data.add_baremetal_node(m)
|
||||
design_state.put_design(design_data)
|
||||
return design_items
|
||||
else:
|
||||
self.logger.error("Could not find plugin %s to ingest data." % (plugin_name))
|
||||
raise LookupError("Could not find plugin %s" % plugin_name)
|
||||
"""
|
||||
ingest_data
|
||||
|
||||
params: plugin_name - Which plugin should be used for ingestion
|
||||
params: params - A map of parameters that will be passed to the plugin's ingest_data method
|
||||
|
||||
Execute a data ingestion using the named plugin (assuming it is enabled)
|
||||
"""
|
||||
|
||||
30
drydock_provisioner/ingester/plugins/__init__.py
Normal file
30
drydock_provisioner/ingester/plugins/__init__.py
Normal file
@@ -0,0 +1,30 @@
|
||||
# Copyright 2017 AT&T Intellectual Property. All other rights reserved.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
# Plugins to parse incoming topology and translate it to helm-drydock's
|
||||
# model representation
|
||||
|
||||
import logging
|
||||
|
||||
class IngesterPlugin(object):
|
||||
|
||||
def __init__(self):
|
||||
self.log = logging.Logger('ingester')
|
||||
return
|
||||
|
||||
def get_data(self):
|
||||
return "ingester_skeleton"
|
||||
|
||||
def ingest_data(self, **kwargs):
|
||||
return {}
|
||||
356
drydock_provisioner/ingester/plugins/yaml.py
Normal file
356
drydock_provisioner/ingester/plugins/yaml.py
Normal file
@@ -0,0 +1,356 @@
|
||||
# Copyright 2017 AT&T Intellectual Property. All other rights reserved.
|
||||
#
|
||||
# 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.
|
||||
|
||||
#
|
||||
# AIC YAML Ingester - This data ingester will consume a AIC YAML design
|
||||
# file
|
||||
#
|
||||
import yaml
|
||||
import logging
|
||||
|
||||
import drydock_provisioner.objects.fields as hd_fields
|
||||
|
||||
from drydock_provisioner import objects
|
||||
from drydock_provisioner.ingester.plugins import IngesterPlugin
|
||||
|
||||
class YamlIngester(IngesterPlugin):
|
||||
|
||||
def __init__(self):
|
||||
super(YamlIngester, self).__init__()
|
||||
self.logger = logging.getLogger('drydock.ingester.yaml')
|
||||
|
||||
def get_name(self):
|
||||
return "yaml"
|
||||
|
||||
"""
|
||||
AIC YAML ingester params
|
||||
|
||||
filenames - Array of absolute path to the YAML files to ingest
|
||||
|
||||
returns an array of objects from drydock_provisioner.model
|
||||
|
||||
"""
|
||||
def ingest_data(self, **kwargs):
|
||||
models = []
|
||||
|
||||
if 'filenames' in kwargs:
|
||||
# TODO validate filenames is array
|
||||
for f in kwargs.get('filenames'):
|
||||
try:
|
||||
file = open(f,'rt')
|
||||
contents = file.read()
|
||||
file.close()
|
||||
models.extend(self.parse_docs(contents))
|
||||
except OSError as err:
|
||||
self.logger.error(
|
||||
"Error opening input file %s for ingestion: %s"
|
||||
% (filename, err))
|
||||
continue
|
||||
elif 'content' in kwargs:
|
||||
models.extend(self.parse_docs(kwargs.get('content')))
|
||||
else:
|
||||
raise ValueError('Missing parameter "filename"')
|
||||
|
||||
return models
|
||||
|
||||
"""
|
||||
Translate a YAML string into the internal Drydock model
|
||||
"""
|
||||
def parse_docs(self, yaml_string):
|
||||
models = []
|
||||
|
||||
try:
|
||||
parsed_data = yaml.load_all(yaml_string)
|
||||
except yaml.YAMLError as err:
|
||||
raise ValueError("Error parsing YAML in %s: %s" % (f,err))
|
||||
|
||||
for d in parsed_data:
|
||||
kind = d.get('kind', '')
|
||||
if kind != '':
|
||||
if kind == 'Region':
|
||||
api_version = d.get('apiVersion', '')
|
||||
|
||||
if api_version == 'v1.0':
|
||||
model = objects.Site()
|
||||
|
||||
metadata = d.get('metadata', {})
|
||||
|
||||
# Need to add validation logic, we'll assume the input is
|
||||
# valid for now
|
||||
model.name = metadata.get('name', '')
|
||||
model.status = hd_fields.SiteStatus.Unknown
|
||||
model.source = hd_fields.ModelSource.Designed
|
||||
|
||||
spec = d.get('spec', {})
|
||||
|
||||
model.tag_definitions = objects.NodeTagDefinitionList()
|
||||
|
||||
tag_defs = spec.get('tag_definitions', [])
|
||||
|
||||
for t in tag_defs:
|
||||
tag_model = objects.NodeTagDefinition()
|
||||
tag_model.tag = t.get('tag', '')
|
||||
tag_model.type = t.get('definition_type', '')
|
||||
tag_model.definition = t.get('definition', '')
|
||||
|
||||
if tag_model.type not in ['lshw_xpath']:
|
||||
raise ValueError('Unknown definition type in ' \
|
||||
'NodeTagDefinition: %s' % (self.definition_type))
|
||||
model.tag_definitions.append(tag_model)
|
||||
|
||||
models.append(model)
|
||||
else:
|
||||
raise ValueError('Unknown API version %s of Region kind' %s (api_version))
|
||||
elif kind == 'NetworkLink':
|
||||
api_version = d.get('apiVersion', '')
|
||||
|
||||
if api_version == "v1.0":
|
||||
model = objects.NetworkLink()
|
||||
|
||||
metadata = d.get('metadata', {})
|
||||
spec = d.get('spec', {})
|
||||
|
||||
model.name = metadata.get('name', '')
|
||||
model.site = metadata.get('region', '')
|
||||
|
||||
bonding = spec.get('bonding', {})
|
||||
model.bonding_mode = bonding.get('mode',
|
||||
hd_fields.NetworkLinkBondingMode.Disabled)
|
||||
|
||||
# How should we define defaults for CIs not in the input?
|
||||
if model.bonding_mode == hd_fields.NetworkLinkBondingMode.LACP:
|
||||
model.bonding_xmit_hash = bonding.get('hash', 'layer3+4')
|
||||
model.bonding_peer_rate = bonding.get('peer_rate', 'fast')
|
||||
model.bonding_mon_rate = bonding.get('mon_rate', '100')
|
||||
model.bonding_up_delay = bonding.get('up_delay', '200')
|
||||
model.bonding_down_delay = bonding.get('down_delay', '200')
|
||||
|
||||
model.mtu = spec.get('mtu', None)
|
||||
model.linkspeed = spec.get('linkspeed', None)
|
||||
|
||||
trunking = spec.get('trunking', {})
|
||||
model.trunk_mode = trunking.get('mode', hd_fields.NetworkLinkTrunkingMode.Disabled)
|
||||
model.native_network = trunking.get('default_network', None)
|
||||
|
||||
models.append(model)
|
||||
else:
|
||||
raise ValueError('Unknown API version of object')
|
||||
elif kind == 'Network':
|
||||
api_version = d.get('apiVersion', '')
|
||||
|
||||
if api_version == "v1.0":
|
||||
model = objects.Network()
|
||||
|
||||
metadata = d.get('metadata', {})
|
||||
spec = d.get('spec', {})
|
||||
|
||||
model.name = metadata.get('name', '')
|
||||
model.site = metadata.get('region', '')
|
||||
|
||||
model.cidr = spec.get('cidr', None)
|
||||
model.allocation_strategy = spec.get('allocation', 'static')
|
||||
model.vlan_id = spec.get('vlan_id', None)
|
||||
model.mtu = spec.get('mtu', None)
|
||||
|
||||
dns = spec.get('dns', {})
|
||||
model.dns_domain = dns.get('domain', 'local')
|
||||
model.dns_servers = dns.get('servers', None)
|
||||
|
||||
ranges = spec.get('ranges', [])
|
||||
model.ranges = []
|
||||
|
||||
for r in ranges:
|
||||
model.ranges.append({'type': r.get('type', None),
|
||||
'start': r.get('start', None),
|
||||
'end': r.get('end', None),
|
||||
})
|
||||
|
||||
routes = spec.get('routes', [])
|
||||
model.routes = []
|
||||
|
||||
for r in routes:
|
||||
model.routes.append({'subnet': r.get('subnet', None),
|
||||
'gateway': r.get('gateway', None),
|
||||
'metric': r.get('metric', None),
|
||||
})
|
||||
models.append(model)
|
||||
elif kind == 'HardwareProfile':
|
||||
api_version = d.get('apiVersion', '')
|
||||
|
||||
if api_version == 'v1.0':
|
||||
metadata = d.get('metadata', {})
|
||||
spec = d.get('spec', {})
|
||||
|
||||
model = objects.HardwareProfile()
|
||||
|
||||
# Need to add validation logic, we'll assume the input is
|
||||
# valid for now
|
||||
model.name = metadata.get('name', '')
|
||||
model.site = metadata.get('region', '')
|
||||
model.source = hd_fields.ModelSource.Designed
|
||||
|
||||
model.vendor = spec.get('vendor', None)
|
||||
model.generation = spec.get('generation', None)
|
||||
model.hw_version = spec.get('hw_version', None)
|
||||
model.bios_version = spec.get('bios_version', None)
|
||||
model.boot_mode = spec.get('boot_mode', None)
|
||||
model.bootstrap_protocol = spec.get('bootstrap_protocol', None)
|
||||
model.pxe_interface = spec.get('pxe_interface', None)
|
||||
|
||||
model.devices = objects.HardwareDeviceAliasList()
|
||||
|
||||
device_aliases = spec.get('device_aliases', {})
|
||||
|
||||
for d in device_aliases:
|
||||
dev_model = objects.HardwareDeviceAlias()
|
||||
dev_model.source = hd_fields.ModelSource.Designed
|
||||
dev_model.alias = d.get('alias', None)
|
||||
dev_model.bus_type = d.get('bus_type', None)
|
||||
dev_model.dev_type = d.get('dev_type', None)
|
||||
dev_model.address = d.get('address', None)
|
||||
model.devices.append(dev_model)
|
||||
|
||||
models.append(model)
|
||||
elif kind == 'HostProfile' or kind == 'BaremetalNode':
|
||||
api_version = d.get('apiVersion', '')
|
||||
|
||||
if api_version == "v1.0":
|
||||
model = None
|
||||
|
||||
if kind == 'HostProfile':
|
||||
model = objects.HostProfile()
|
||||
else:
|
||||
model = objects.BaremetalNode()
|
||||
|
||||
metadata = d.get('metadata', {})
|
||||
spec = d.get('spec', {})
|
||||
|
||||
model.name = metadata.get('name', '')
|
||||
model.site = metadata.get('region', '')
|
||||
model.source = hd_fields.ModelSource.Designed
|
||||
|
||||
model.parent_profile = spec.get('host_profile', None)
|
||||
model.hardware_profile = spec.get('hardware_profile', None)
|
||||
|
||||
oob = spec.get('oob', {})
|
||||
|
||||
model.oob_type = oob.get('type', None)
|
||||
model.oob_network = oob.get('network', None)
|
||||
model.oob_account = oob.get('account', None)
|
||||
model.oob_credential = oob.get('credential', None)
|
||||
|
||||
storage = spec.get('storage', {})
|
||||
model.storage_layout = storage.get('layout', 'lvm')
|
||||
|
||||
bootdisk = storage.get('bootdisk', {})
|
||||
model.bootdisk_device = bootdisk.get('device', None)
|
||||
model.bootdisk_root_size = bootdisk.get('root_size', None)
|
||||
model.bootdisk_boot_size = bootdisk.get('boot_size', None)
|
||||
|
||||
partitions = storage.get('partitions', [])
|
||||
model.partitions = objects.HostPartitionList()
|
||||
|
||||
for p in partitions:
|
||||
part_model = objects.HostPartition()
|
||||
|
||||
part_model.name = p.get('name', None)
|
||||
part_model.source = hd_fields.ModelSource.Designed
|
||||
part_model.device = p.get('device', None)
|
||||
part_model.part_uuid = p.get('part_uuid', None)
|
||||
part_model.size = p.get('size', None)
|
||||
part_model.mountpoint = p.get('mountpoint', None)
|
||||
part_model.fstype = p.get('fstype', 'ext4')
|
||||
part_model.mount_options = p.get('mount_options', 'defaults')
|
||||
part_model.fs_uuid = p.get('fs_uuid', None)
|
||||
part_model.fs_label = p.get('fs_label', None)
|
||||
|
||||
model.partitions.append(part_model)
|
||||
|
||||
interfaces = spec.get('interfaces', [])
|
||||
model.interfaces = objects.HostInterfaceList()
|
||||
|
||||
for i in interfaces:
|
||||
int_model = objects.HostInterface()
|
||||
|
||||
int_model.device_name = i.get('device_name', None)
|
||||
int_model.network_link = i.get('device_link', None)
|
||||
int_model.primary_netowrk = i.get('primary', False)
|
||||
|
||||
int_model.hardware_slaves = []
|
||||
slaves = i.get('slaves', [])
|
||||
|
||||
for s in slaves:
|
||||
int_model.hardware_slaves.append(s)
|
||||
|
||||
int_model.networks = []
|
||||
networks = i.get('networks', [])
|
||||
|
||||
for n in networks:
|
||||
int_model.networks.append(n)
|
||||
|
||||
model.interfaces.append(int_model)
|
||||
|
||||
node_metadata = spec.get('metadata', {})
|
||||
metadata_tags = node_metadata.get('tags', [])
|
||||
model.tags = []
|
||||
|
||||
for t in metadata_tags:
|
||||
model.tags.append(t)
|
||||
|
||||
owner_data = node_metadata.get('owner_data', {})
|
||||
model.owner_data = {}
|
||||
|
||||
for k, v in owner_data.items():
|
||||
model.owner_data[k] = v
|
||||
|
||||
model.rack = node_metadata.get('rack', None)
|
||||
|
||||
if kind == 'BaremetalNode':
|
||||
addresses = spec.get('addressing', [])
|
||||
|
||||
if len(addresses) == 0:
|
||||
raise ValueError('BaremetalNode needs at least' \
|
||||
' 1 assigned address')
|
||||
|
||||
model.addressing = objects.IpAddressAssignmentList()
|
||||
|
||||
for a in addresses:
|
||||
assignment = objects.IpAddressAssignment()
|
||||
|
||||
address = a.get('address', '')
|
||||
if address == 'dhcp':
|
||||
assignment.type = 'dhcp'
|
||||
assignment.address = None
|
||||
assignment.network = a.get('network')
|
||||
|
||||
model.addressing.append(assignment)
|
||||
elif address != '':
|
||||
assignment.type = 'static'
|
||||
assignment.address = a.get('address')
|
||||
assignment.network = a.get('network')
|
||||
|
||||
model.addressing.append(assignment)
|
||||
else:
|
||||
self.log.error("Invalid address assignment %s on Node %s"
|
||||
% (address, self.name))
|
||||
models.append(model)
|
||||
else:
|
||||
raise ValueError('Unknown API version %s of Kind HostProfile' % (api_version))
|
||||
else:
|
||||
self.log.error(
|
||||
"Error processing document in %s, no kind field"
|
||||
% (f))
|
||||
continue
|
||||
|
||||
return models
|
||||
13
drydock_provisioner/ingester/readme.md
Normal file
13
drydock_provisioner/ingester/readme.md
Normal file
@@ -0,0 +1,13 @@
|
||||
# Ingester #
|
||||
|
||||
Ingester is a pluggable consumer of site design data. It
|
||||
will support consuming data in different formats from
|
||||
different sources.
|
||||
|
||||
Each ingester plugin should be able source data
|
||||
based on user-provided parameters and parse that data
|
||||
into the Drydock internal model (drydock_provisioner.model).
|
||||
|
||||
Each plugin does not need to support every type of design
|
||||
data as a single site design may be federated from multiple
|
||||
sources.
|
||||
Reference in New Issue
Block a user