add template related function to template file
Change-Id: I73419c3328e3af840d2e6f46e73622adbcfc8150
This commit is contained in:
218
code/daisy/daisy/api/v1/host_template.py
Executable file → Normal file
218
code/daisy/daisy/api/v1/host_template.py
Executable file → Normal file
@@ -24,8 +24,7 @@ from webob.exc import HTTPConflict
|
||||
from webob.exc import HTTPForbidden
|
||||
from webob.exc import HTTPNotFound
|
||||
from webob import Response
|
||||
|
||||
# import json
|
||||
import json
|
||||
|
||||
from daisy.api import policy
|
||||
import daisy.api.v1
|
||||
@@ -39,11 +38,7 @@ from daisy import notifier
|
||||
import daisy.registry.client.v1.api as registry
|
||||
from daisy.registry.api.v1 import template
|
||||
|
||||
try:
|
||||
import simplejson as json
|
||||
except ImportError:
|
||||
import json
|
||||
|
||||
import daisy.api.backends.common as daisy_cmn
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
_ = i18n._
|
||||
@@ -137,7 +132,7 @@ class Controller(controller.BaseController):
|
||||
raise HTTPNotFound(msg)
|
||||
|
||||
@utils.mutating
|
||||
def add_template(self, req, host_template):
|
||||
def add_host_template(self, req, host_template):
|
||||
"""
|
||||
Adds a new cluster template to Daisy.
|
||||
|
||||
@@ -148,10 +143,10 @@ class Controller(controller.BaseController):
|
||||
"""
|
||||
self._enforce(req, 'add_host_template')
|
||||
|
||||
host_template = registry.add_host_template_metadata(
|
||||
host_template_meta = registry.add_host_template_metadata(
|
||||
req.context, host_template)
|
||||
|
||||
return {'host_template': template}
|
||||
return {'host_template': host_template_meta}
|
||||
|
||||
@utils.mutating
|
||||
def update_host_template(self, req, template_id, host_template):
|
||||
@@ -207,11 +202,22 @@ class Controller(controller.BaseController):
|
||||
|
||||
return {'host_template': host_template}
|
||||
|
||||
# TODO:move the additional deletion of
|
||||
# the hwm* to host_to_template extension
|
||||
def _filter_params(self, host_meta):
|
||||
for key in host_meta.keys():
|
||||
if key == "id" or key == "updated_at" or key == "deleted_at" or \
|
||||
key == "created_at" or key == "deleted":
|
||||
key == "created_at" or key == "deleted" or \
|
||||
key == "hwm_id" or key == "hwm_ip":
|
||||
del host_meta[key]
|
||||
if "config_set_id" in host_meta:
|
||||
host_meta['config_set_id'] = ""
|
||||
if "tecs_version_id" in host_meta:
|
||||
host_meta['tecs_version_id'] = ""
|
||||
|
||||
if "os_version_id" in host_meta:
|
||||
del host_meta['os_version_id']
|
||||
|
||||
if "memory" in host_meta:
|
||||
del host_meta['memory']
|
||||
|
||||
@@ -251,6 +257,39 @@ class Controller(controller.BaseController):
|
||||
assigned_network['ip'] = ""
|
||||
return host_meta
|
||||
|
||||
def _judge_ssh_host(self, req, cluster_id, host_id):
|
||||
ssh_host_flag = False
|
||||
kwargs = {}
|
||||
nodes = registry.get_hosts_detail(req.context, **kwargs)
|
||||
for node in nodes:
|
||||
if node.get("hwm_id"):
|
||||
daisy_cmn.check_discover_state_with_hwm(req, node)
|
||||
else:
|
||||
daisy_cmn.check_discover_state_with_no_hwm(req, node)
|
||||
ssh_hosts_list = []
|
||||
for node in nodes:
|
||||
if node['discover_state'] and 'SSH' in node['discover_state']:
|
||||
ssh_hosts_list.append(node)
|
||||
if not ssh_hosts_list:
|
||||
return ssh_host_flag
|
||||
for ssh_host in ssh_hosts_list:
|
||||
if host_id == ssh_host['id'] and ssh_host[
|
||||
'discover_state'] == 'SSH:DISCOVERING':
|
||||
msg = (_(
|
||||
'host %s is in DISCOVERING status,please wait') % host_id)
|
||||
raise HTTPBadRequest(explanation=msg)
|
||||
if host_id == ssh_host['id'] and ssh_host[
|
||||
'discover_state'] == 'SSH:DISCOVERY_FAILED':
|
||||
msg = (_(
|
||||
'host %s discover faild ,please make sure success')
|
||||
% host_id)
|
||||
raise HTTPBadRequest(explanation=msg)
|
||||
if host_id == ssh_host['id'] and ssh_host[
|
||||
'discover_state'] == 'SSH:DISCOVERY_SUCCESSFUL':
|
||||
ssh_host_flag = True
|
||||
break
|
||||
return ssh_host_flag
|
||||
|
||||
@utils.mutating
|
||||
def get_host_template_detail(self, req, template_id):
|
||||
"""
|
||||
@@ -368,6 +407,19 @@ class Controller(controller.BaseController):
|
||||
if not host_template.get('cluster_name', None):
|
||||
msg = "cluster name is null"
|
||||
raise HTTPNotFound(explanation=msg)
|
||||
if host_template.get('host_id', None):
|
||||
self.get_host_meta_or_404(req, host_template['host_id'])
|
||||
else:
|
||||
msg = "host id which need to template instantiate can't be null"
|
||||
raise HTTPBadRequest(explanation=msg)
|
||||
host_id = host_template['host_id']
|
||||
orig_host_meta = registry.get_host_metadata(req.context, host_id)
|
||||
if orig_host_meta.get("hwm_ip", None):
|
||||
msg = "hwm host forbidden to use template"
|
||||
LOG.error(msg)
|
||||
raise HTTPForbidden(explanation=msg,
|
||||
request=req,
|
||||
content_type="text/plain")
|
||||
params = {'filters': {'cluster_name': host_template['cluster_name']}}
|
||||
templates = registry.host_template_lists_metadata(
|
||||
req.context, **params)
|
||||
@@ -385,12 +437,6 @@ class Controller(controller.BaseController):
|
||||
explanation=msg,
|
||||
request=req,
|
||||
content_type="text/plain")
|
||||
if host_template.get('host_id', None):
|
||||
self.get_host_meta_or_404(req, host_template['host_id'])
|
||||
else:
|
||||
msg = "host_id is not null"
|
||||
raise HTTPBadRequest(explanation=msg)
|
||||
host_id = host_template['host_id']
|
||||
params = {'filters': {'name': host_template['cluster_name']}}
|
||||
clusters = registry.get_clusters_detail(req.context, **params)
|
||||
if clusters and clusters[0]:
|
||||
@@ -408,47 +454,116 @@ class Controller(controller.BaseController):
|
||||
if role_name['name'] in host_role_list:
|
||||
role_id_list.append(role_name['id'])
|
||||
host_template_used['role'] = role_id_list
|
||||
if 'name' in host_template_used:
|
||||
host_template_used.pop('name')
|
||||
if 'dmi_uuid' in host_template_used:
|
||||
host_template_used.pop('dmi_uuid')
|
||||
if 'ipmi_user' in host_template_used:
|
||||
host_template_used.pop('ipmi_user')
|
||||
if 'ipmi_passwd' in host_template_used:
|
||||
host_template_used.pop('ipmi_passwd')
|
||||
if 'ipmi_addr' in host_template_used:
|
||||
host_template_used.pop('ipmi_addr')
|
||||
ignore_common_key_list = ["name", "dmi_uuid", "ipmi_addr"]
|
||||
for ignore_key in ignore_common_key_list:
|
||||
if host_template_used.get(ignore_key, None):
|
||||
host_template_used.pop(ignore_key)
|
||||
ssh_host_flag = self._judge_ssh_host(req,
|
||||
host_template_used['cluster'],
|
||||
host_id)
|
||||
ignore_ssh_key_list = ["root_disk", "root_lv_size",
|
||||
"swap_lv_size", "isolcpus",
|
||||
"os_version_file", "os_version_id",
|
||||
"root_pwd", "hugepages", "hugepagesize"]
|
||||
if ssh_host_flag:
|
||||
for ignore_key in ignore_ssh_key_list:
|
||||
if host_template_used.get(ignore_key, None):
|
||||
host_template_used.pop(ignore_key)
|
||||
daisy_cmn.add_ssh_host_to_cluster_and_assigned_network(
|
||||
req,
|
||||
host_template_used['cluster'],
|
||||
host_id)
|
||||
else:
|
||||
if not host_template_used.get("root_disk", None):
|
||||
raise HTTPBadRequest(
|
||||
explanation="ssh host template can't be used by pxe host")
|
||||
host_template_used['os_status'] = "init"
|
||||
host_template_used['messages'] = ""
|
||||
host_template_used['os_progress'] = 0
|
||||
host_template_used['description'] = ""
|
||||
host_template_interfaces = host_template_used.get('interfaces', None)
|
||||
if host_template_interfaces:
|
||||
template_ether_interface = [
|
||||
interface for interface in host_template_interfaces if
|
||||
interface['type'] == "ether"]
|
||||
template_ether_interface = [interface for interface in
|
||||
host_template_interfaces if
|
||||
interface['type'] == "ether"]
|
||||
template_bond_interface = [interface for interface in
|
||||
host_template_interfaces if
|
||||
interface['type'] == "bond"]
|
||||
orig_host_meta = registry.get_host_metadata(req.context, host_id)
|
||||
orig_host_interfaces = orig_host_meta.get('interfaces', None)
|
||||
temp_orig_host_interfaces = [
|
||||
interface for interface in orig_host_interfaces if
|
||||
interface['type'] == "ether"]
|
||||
temp_orig_host_interfaces = [interface for interface in
|
||||
orig_host_interfaces if
|
||||
interface['type'] == "ether"]
|
||||
if len(temp_orig_host_interfaces) != len(template_ether_interface):
|
||||
msg = (_('host_id %s does not match the host_id host_template '
|
||||
msg = (_('host_id %s number of interface '
|
||||
'does not match host template'
|
||||
'%s.') % (host_id,
|
||||
host_template['host_template_name']))
|
||||
raise HTTPBadRequest(explanation=msg)
|
||||
interface_match_flag = 0
|
||||
host_template_interfaces = \
|
||||
filter(lambda interface: 'vlan' != interface['type'],
|
||||
host_template_interfaces)
|
||||
for host_template_interface in host_template_interfaces:
|
||||
if host_template_interface['type'] == 'ether':
|
||||
for orig_host_interface in orig_host_interfaces:
|
||||
if orig_host_interface[
|
||||
'pci'] == host_template_interface['pci']:
|
||||
if orig_host_interface['pci'] ==\
|
||||
host_template_interface['pci']:
|
||||
interface_match_flag += 1
|
||||
host_template_interface[
|
||||
'mac'] = orig_host_interface['mac']
|
||||
if 'ip' in host_template_interface:
|
||||
host_template_interface['mac'] =\
|
||||
orig_host_interface['mac']
|
||||
if host_template_interface.has_key('ip') and\
|
||||
ssh_host_flag:
|
||||
host_template_interface['ip'] =\
|
||||
orig_host_interface['ip']
|
||||
else:
|
||||
host_template_interface.pop('ip')
|
||||
if interface_match_flag != len(template_ether_interface):
|
||||
msg = (_('host_id %s does not match the host '
|
||||
'host_template %s.') % (
|
||||
host_id, host_template['host_template_name']))
|
||||
raise HTTPBadRequest(explanation=msg)
|
||||
if orig_host_interface.get('assigned_networks',
|
||||
None) and ssh_host_flag:
|
||||
host_template_interface['assigned_networks']\
|
||||
= orig_host_interface['assigned_networks']
|
||||
if host_template_interface['type'] == 'bond':
|
||||
for orig_host_interface in orig_host_interfaces:
|
||||
if orig_host_interface['name'] ==\
|
||||
host_template_interface['name']:
|
||||
if ssh_host_flag:
|
||||
interface_match_flag += 1
|
||||
interface_list = ["mac", "slave1", "slave2",
|
||||
"ip"]
|
||||
for interface_key in interface_list:
|
||||
if host_template_interface.get(
|
||||
interface_key, None) and ssh_host_flag:
|
||||
host_template_interface[interface_key]\
|
||||
= orig_host_interface[interface_key]
|
||||
if host_template_interface.has_key('ip')\
|
||||
and not ssh_host_flag:
|
||||
host_template_interface.pop('ip')
|
||||
if orig_host_interface.get('assigned_networks',
|
||||
None) and ssh_host_flag:
|
||||
host_template_interface['assigned_networks']\
|
||||
= orig_host_interface['assigned_networks']
|
||||
if host_template_interface['type'] == 'vlan':
|
||||
host_template_interfaces.remove(host_template_interface)
|
||||
if ssh_host_flag:
|
||||
vlan_interfaces = []
|
||||
for orig_host_interface in orig_host_interfaces:
|
||||
if orig_host_interface['type'] == 'vlan':
|
||||
vlan_interfaces.append(orig_host_interface)
|
||||
host_template_interfaces.extend(vlan_interfaces)
|
||||
if interface_match_flag != (len(
|
||||
template_ether_interface) + len(
|
||||
template_bond_interface)):
|
||||
msg = (_('ssh discover host_id '
|
||||
'interface %s does not match the '
|
||||
'host_template %s.') % (
|
||||
host_id, host_template['host_template_name']))
|
||||
raise HTTPBadRequest(explanation=msg)
|
||||
else:
|
||||
if interface_match_flag != len(template_ether_interface):
|
||||
msg = (_('host_id %s interface does not match the '
|
||||
'host_template %s.') % (
|
||||
host_id, host_template['host_template_name']))
|
||||
raise HTTPBadRequest(explanation=msg)
|
||||
host_template_used['interfaces'] = str(host_template_interfaces)
|
||||
host_template = registry.update_host_metadata(
|
||||
req.context, host_id, host_template_used)
|
||||
@@ -489,10 +604,17 @@ class Controller(controller.BaseController):
|
||||
raise HTTPNotFound(explanation=msg)
|
||||
else:
|
||||
host_templates[0]['hosts'] = json.dumps(template_param)
|
||||
host_template = registry.update_host_template_metadata(
|
||||
req.context, host_templates[0]['id'],
|
||||
host_templates[0])
|
||||
return {"host_template": host_template}
|
||||
host_template_meta = \
|
||||
registry.update_host_template_metadata(
|
||||
req.context, host_templates[0]['id'],
|
||||
host_templates[0])
|
||||
if host_template.get('template_id', None):
|
||||
template = {'hosts': host_templates[0]['hosts']}
|
||||
registry.update_template_metadata(
|
||||
req.context,
|
||||
host_template['template_id'],
|
||||
template)
|
||||
return {"host_template": host_template_meta}
|
||||
else:
|
||||
msg = "host template cluster name %s is null" % host_template[
|
||||
'cluster_name']
|
||||
|
||||
158
code/daisy/daisy/api/v1/networks.py
Executable file → Normal file
158
code/daisy/daisy/api/v1/networks.py
Executable file → Normal file
@@ -32,6 +32,7 @@ from daisy.api.v1 import filters
|
||||
from daisy.common import exception
|
||||
from daisy.common import utils
|
||||
from daisy.common import wsgi
|
||||
from daisy.api import common
|
||||
from daisy import i18n
|
||||
from daisy import notifier
|
||||
import daisy.registry.client.v1.api as registry
|
||||
@@ -59,7 +60,8 @@ SUPPORT_NETWORK_TYPE = (
|
||||
'MANAGEMENT',
|
||||
'EXTERNAL',
|
||||
'DEPLOYMENT',
|
||||
'HEARTBEAT')
|
||||
'HEARTBEAT',
|
||||
'OUTBAND')
|
||||
SUPPORT_NETWORK_TEMPLATE_TYPE = ('custom', 'template', 'default', 'system')
|
||||
SUPPORT_ML2_TYPE = ('ovs', 'sriov(direct)', 'sriov(macvtap)',
|
||||
'ovs,sriov(direct)', 'ovs,sriov(macvtap)')
|
||||
@@ -152,24 +154,6 @@ class Controller(controller.BaseController):
|
||||
params[PARAM] = req.params.get(PARAM)
|
||||
return params
|
||||
|
||||
def validate_ip_format(self, ip_str):
|
||||
'''
|
||||
valid ip_str format = '10.43.178.9'
|
||||
invalid ip_str format : '123. 233.42.12', spaces existed in field
|
||||
'3234.23.453.353', out of range
|
||||
'-2.23.24.234', negative number in field
|
||||
'1.2.3.4d', letter in field
|
||||
'10.43.1789', invalid format
|
||||
'''
|
||||
valid_fromat = False
|
||||
if ip_str.count('.') == 3 and all(num.isdigit() and 0 <= int(
|
||||
num) < 256 for num in ip_str.rstrip().split('.')):
|
||||
valid_fromat = True
|
||||
if not valid_fromat:
|
||||
msg = (_("%s invalid ip format!") % ip_str)
|
||||
LOG.warn(msg)
|
||||
raise HTTPForbidden(msg)
|
||||
|
||||
def _ip_into_int(self, ip):
|
||||
"""
|
||||
Switch ip string to decimalism integer..
|
||||
@@ -245,92 +229,31 @@ class Controller(controller.BaseController):
|
||||
(network_meta['network_type'], network_name))
|
||||
raise HTTPConflict(msg, request=req, content_type="text/plain")
|
||||
|
||||
def _valid_network_range(self, req, network_meta):
|
||||
if (('vlan_start' in network_meta and 'vlan_end' not in
|
||||
network_meta) or (
|
||||
'vlan_start' not in network_meta and
|
||||
'vlan_end' in network_meta)):
|
||||
msg = "vlan-start and vlan-end must be appeared "\
|
||||
"at the same time"
|
||||
LOG.error(msg)
|
||||
raise HTTPBadRequest(explanation=msg, request=req)
|
||||
if 'vlan_start' in network_meta:
|
||||
if not (int(network_meta['vlan_start']) >= 1 and
|
||||
int(network_meta['vlan_start']) <= 4094):
|
||||
msg = "vlan_start must be a integer in 1~4096"
|
||||
LOG.error(msg)
|
||||
raise HTTPBadRequest(explanation=msg, request=req)
|
||||
if 'vlan_end' in network_meta:
|
||||
if not (int(network_meta['vlan_end']) >= 1 and
|
||||
int(network_meta['vlan_end']) <= 4094):
|
||||
msg = "vlan_end must be a integer in 1~4096"
|
||||
LOG.error(msg)
|
||||
raise HTTPBadRequest(explanation=msg, request=req)
|
||||
if int(network_meta['vlan_start']) > int(network_meta['vlan_end']):
|
||||
msg = "vlan_start must be less than vlan_end"
|
||||
LOG.error(msg)
|
||||
raise HTTPBadRequest(explanation=msg, request=req)
|
||||
|
||||
if (('vni_start' in network_meta and 'vni_end' not in
|
||||
network_meta) or (
|
||||
'vni_start' not in network_meta and
|
||||
'vni_end' in network_meta)):
|
||||
|
||||
msg = "vni_start and vni_end must be appeared at the same time"
|
||||
LOG.error(msg)
|
||||
raise HTTPBadRequest(explanation=msg, request=req)
|
||||
if 'vni_start' in network_meta:
|
||||
if not (int(network_meta['vni_start']) >= 1 and
|
||||
int(network_meta['vni_start']) <= 16777216):
|
||||
msg = "vni_start must be a integer in 1~16777216"
|
||||
LOG.error(msg)
|
||||
raise HTTPBadRequest(explanation=msg, request=req)
|
||||
if 'vni_end' in network_meta:
|
||||
if not (int(network_meta['vni_end']) >= 1 and
|
||||
int(network_meta['vni_end']) <= 16777216):
|
||||
msg = "vni_end must be a integer in 1~16777216"
|
||||
LOG.error(msg)
|
||||
raise HTTPBadRequest(explanation=msg, request=req)
|
||||
if int(network_meta['vni_start']) > int(network_meta['vni_end']):
|
||||
msg = "vni_start must be less than vni_end"
|
||||
LOG.error(msg)
|
||||
raise HTTPBadRequest(explanation=msg, request=req)
|
||||
|
||||
if (('gre_id_start' in network_meta and 'gre_id_end' not in
|
||||
network_meta) or (
|
||||
'gre_id_start' not in network_meta and
|
||||
'gre_id_end' in network_meta)):
|
||||
msg = "gre_id_start and gre_id_end must"\
|
||||
"be appeared at the same time"
|
||||
LOG.error(msg)
|
||||
raise HTTPBadRequest(explanation=msg, request=req)
|
||||
if 'gre_id_start' in network_meta:
|
||||
if not (int(network_meta['gre_id_start']) >= 1 and
|
||||
int(network_meta['gre_id_start']) <= 4094):
|
||||
msg = "gre_id_start must be a integer in 1~4094"
|
||||
LOG.error(msg)
|
||||
raise HTTPBadRequest(explanation=msg, request=req)
|
||||
if 'gre_id_end' in network_meta:
|
||||
if not (int(network_meta['gre_id_end']) >= 1 and
|
||||
int(network_meta['gre_id_end']) <= 4094):
|
||||
msg = "gre_id_end must be a integer in 1~4094"
|
||||
LOG.error(msg)
|
||||
raise HTTPBadRequest(explanation=msg, request=req)
|
||||
if int(network_meta['gre_id_start']) >\
|
||||
int(network_meta['gre_id_end']):
|
||||
msg = "gre_id_start must be less than gre_id_end"
|
||||
LOG.error(msg)
|
||||
raise HTTPBadRequest(explanation=msg, request=req)
|
||||
def _verify_uniqueness_of_network_custom_name(
|
||||
self, req, network_list, network_meta, is_update=False):
|
||||
"""custom name of network in cluster must be unique"""
|
||||
custom_name = network_meta['custom_name']
|
||||
for network in network_list['networks']:
|
||||
if is_update and custom_name == network.get('custom_name', None) \
|
||||
and network_meta['id'] == network['id']:
|
||||
return
|
||||
network_custom_name_list = [network['custom_name'] for network
|
||||
in network_list['networks'] if
|
||||
network.get('custom_name', None)]
|
||||
if custom_name in network_custom_name_list:
|
||||
msg = _("Custom name %s of network already exits in the "
|
||||
"cluster." % custom_name)
|
||||
raise HTTPConflict(msg, request=req, content_type="text/plain")
|
||||
|
||||
def _verify_heartbeat_network(self, req, network_list, network_meta):
|
||||
heartbeat_networks = [
|
||||
network for network in network_list['networks'] if network.get(
|
||||
'network_type',
|
||||
None) and network['network_type'] == "HEARTBEAT"]
|
||||
if len(heartbeat_networks) >= 2:
|
||||
if len(heartbeat_networks) >= 3:
|
||||
raise HTTPBadRequest(
|
||||
explanation="HEARTBEAT network plane number must be "
|
||||
"less than two",
|
||||
"less than three",
|
||||
request=req)
|
||||
|
||||
@utils.mutating
|
||||
@@ -350,6 +273,9 @@ class Controller(controller.BaseController):
|
||||
network_list = self.detail(req, cluster_id)
|
||||
self._verify_uniqueness_of_network_name(
|
||||
req, network_list, network_meta)
|
||||
if network_meta.get('custom_name', None):
|
||||
self._verify_uniqueness_of_network_custom_name(
|
||||
req, network_list, network_meta)
|
||||
if 'network_type' in network_meta and network_meta[
|
||||
'network_type'] == "HEARTBEAT":
|
||||
self._verify_heartbeat_network(req, network_list, network_meta)
|
||||
@@ -382,7 +308,7 @@ class Controller(controller.BaseController):
|
||||
explanation="unsupported capability type",
|
||||
request=req)
|
||||
|
||||
self._valid_network_range(req, network_meta)
|
||||
common.valid_network_range(req, network_meta)
|
||||
|
||||
if network_meta.get('ip_ranges', None) and \
|
||||
eval(network_meta['ip_ranges']):
|
||||
@@ -403,7 +329,7 @@ class Controller(controller.BaseController):
|
||||
msg = (_("Wrong CIDR format."))
|
||||
LOG.warn(msg)
|
||||
raise HTTPForbidden(msg)
|
||||
self.validate_ip_format(cidr_division[0])
|
||||
utils.validate_ip_format(cidr_division[0])
|
||||
|
||||
ip_ranges = eval(network_meta['ip_ranges'])
|
||||
last_ip_range_end = 0
|
||||
@@ -418,8 +344,8 @@ class Controller(controller.BaseController):
|
||||
raise HTTPForbidden(msg)
|
||||
ip_start = ip_pair['start']
|
||||
ip_end = ip_pair['end']
|
||||
self.validate_ip_format(ip_start) # check ip format
|
||||
self.validate_ip_format(ip_end)
|
||||
utils.validate_ip_format(ip_start) # check ip format
|
||||
utils.validate_ip_format(ip_end)
|
||||
|
||||
if not self._is_in_network_range(ip_start, cidr):
|
||||
msg = (
|
||||
@@ -484,7 +410,7 @@ class Controller(controller.BaseController):
|
||||
gateway = network_meta['gateway']
|
||||
cidr = network_meta['cidr']
|
||||
|
||||
self.validate_ip_format(gateway)
|
||||
utils.validate_ip_format(gateway)
|
||||
return_flag = self._is_in_network_range(gateway, cidr)
|
||||
if not return_flag:
|
||||
msg = (
|
||||
@@ -494,11 +420,13 @@ class Controller(controller.BaseController):
|
||||
(gateway, cidr)))
|
||||
raise HTTPBadRequest(explanation=msg)
|
||||
|
||||
if network_meta.get('cluster_id') and network_meta.get('gateway'):
|
||||
if network_meta.get('network_type', None) != "DATAPLANE" and \
|
||||
network_meta.get('cluster_id') and network_meta.get('gateway'):
|
||||
networks = registry.get_networks_detail(req.context, cluster_id)
|
||||
gateways = [network['gateway'] for network in networks
|
||||
if network['name'] != network_meta['name'] and
|
||||
network['gateway']]
|
||||
network['gateway'] and
|
||||
network['network_type'] != "DATAPLANE"]
|
||||
if gateways:
|
||||
msg = (_('More than one gateway found in cluster.'))
|
||||
LOG.error(msg)
|
||||
@@ -666,7 +594,7 @@ class Controller(controller.BaseController):
|
||||
explanation="unsupported capability type",
|
||||
request=req)
|
||||
|
||||
self._valid_network_range(req, network_meta)
|
||||
common.valid_network_range(req, network_meta)
|
||||
|
||||
network_name = network_meta.get('name', None)
|
||||
cluster_id = orig_network_meta['cluster_id']
|
||||
@@ -679,6 +607,12 @@ class Controller(controller.BaseController):
|
||||
network_list = self.detail(req, cluster_id)
|
||||
self._verify_uniqueness_of_network_name(
|
||||
req, network_list, network_updated, True)
|
||||
if network_meta.get('custom_name', None) and cluster_id:
|
||||
network_list = self.detail(req, cluster_id)
|
||||
network_updated = copy.deepcopy(network_meta)
|
||||
network_updated['id'] = network_id
|
||||
self._verify_uniqueness_of_network_custom_name(
|
||||
req, network_list, network_updated, True)
|
||||
|
||||
cidr = network_meta.get('cidr', orig_network_meta['cidr'])
|
||||
vlan_id = network_meta.get('vlan_id', orig_network_meta['vlan_id'])
|
||||
@@ -691,7 +625,7 @@ class Controller(controller.BaseController):
|
||||
msg = (_("Wrong CIDR format."))
|
||||
LOG.warn(msg)
|
||||
raise HTTPForbidden(msg)
|
||||
self.validate_ip_format(cidr_division[0])
|
||||
utils.validate_ip_format(cidr_division[0])
|
||||
|
||||
if cidr and vlan_id and cluster_id:
|
||||
networks = registry.get_networks_detail(req.context, cluster_id)
|
||||
@@ -731,8 +665,8 @@ class Controller(controller.BaseController):
|
||||
raise HTTPForbidden(msg)
|
||||
ip_start = ip_pair['start']
|
||||
ip_end = ip_pair['end']
|
||||
self.validate_ip_format(ip_start) # check ip format
|
||||
self.validate_ip_format(ip_end)
|
||||
utils.validate_ip_format(ip_start) # check ip format
|
||||
utils.validate_ip_format(ip_end)
|
||||
|
||||
if not self._is_in_network_range(ip_start, cidr):
|
||||
msg = (
|
||||
@@ -778,7 +712,7 @@ class Controller(controller.BaseController):
|
||||
orig_network_meta['cidr']):
|
||||
gateway = network_meta.get('gateway', orig_network_meta['gateway'])
|
||||
cidr = network_meta.get('cidr', orig_network_meta['cidr'])
|
||||
self.validate_ip_format(gateway)
|
||||
utils.validate_ip_format(gateway)
|
||||
return_flag = self._is_in_network_range(gateway, cidr)
|
||||
if not return_flag:
|
||||
msg = (
|
||||
@@ -789,11 +723,13 @@ class Controller(controller.BaseController):
|
||||
raise HTTPBadRequest(explanation=msg)
|
||||
|
||||
# allow one gateway in one cluster
|
||||
if network_meta.get('cluster_id') and (network_meta.get('gateway')):
|
||||
if network_meta.get('network_type', None) != "DATAPLANE" and \
|
||||
network_meta.get('cluster_id') and network_meta.get('gateway'):
|
||||
networks = registry.get_networks_detail(req.context, cluster_id)
|
||||
gateways = [network['gateway'] for network in networks
|
||||
if network['name'] != orig_network_meta['name'] and
|
||||
network['gateway']]
|
||||
network['gateway'] and
|
||||
network['network_type'] != "DATAPLANE"]
|
||||
if gateways:
|
||||
msg = (_('More than one gateway found in cluster.'))
|
||||
LOG.error(msg)
|
||||
|
||||
@@ -26,7 +26,7 @@ from webob.exc import HTTPNotFound
|
||||
from webob import Response
|
||||
import copy
|
||||
import json
|
||||
|
||||
import subprocess
|
||||
from daisy.api import policy
|
||||
import daisy.api.v1
|
||||
from daisy.api.v1 import controller
|
||||
@@ -265,6 +265,16 @@ class Controller(controller.BaseController):
|
||||
self._del_general_params(cinder_volume)
|
||||
return cinder_volumes
|
||||
|
||||
def _get_optical_switchs(self, req, role):
|
||||
optical_switch_params = {'filters': {'role_id': role['id']}}
|
||||
optical_switchs = registry.list_optical_switch_metadata(
|
||||
req.context, **optical_switch_params)
|
||||
for optical_switch in optical_switchs:
|
||||
if optical_switch.get('role_id', None):
|
||||
optical_switch['role_id'] = role['name']
|
||||
self._del_general_params(optical_switch)
|
||||
return optical_switchs
|
||||
|
||||
def _get_services_disk(self, req, role):
|
||||
params = {'filters': {'role_id': role['id']}}
|
||||
services_disk = registry.list_service_disk_metadata(
|
||||
@@ -289,6 +299,7 @@ class Controller(controller.BaseController):
|
||||
self._enforce(req, 'export_db_to_json')
|
||||
cinder_volume_list = []
|
||||
service_disk_list = []
|
||||
optical_switch_list = []
|
||||
template_content = {}
|
||||
template_json = {}
|
||||
template_id = ""
|
||||
@@ -317,11 +328,14 @@ class Controller(controller.BaseController):
|
||||
cinder_volume_list += cinder_volumes
|
||||
services_disk = self._get_services_disk(req, role)
|
||||
service_disk_list += services_disk
|
||||
optical_switchs = self._get_optical_switchs(req, role)
|
||||
optical_switch_list += optical_switchs
|
||||
|
||||
if role.get('config_set_id', None):
|
||||
config_set = registry.get_config_set_metadata(
|
||||
req.context, role['config_set_id'])
|
||||
role['config_set_id'] = config_set['name']
|
||||
if config_set.get("config", None):
|
||||
role['config_set'] = config_set['config']
|
||||
del role['cluster_id']
|
||||
del role['status']
|
||||
del role['progress']
|
||||
@@ -351,10 +365,13 @@ class Controller(controller.BaseController):
|
||||
del cluster['nodes']
|
||||
self._del_general_params(cluster)
|
||||
self._del_cluster_params(cluster)
|
||||
cluster['tecs_version_id'] = ""
|
||||
template_content['cluster'] = cluster
|
||||
template_content['cluster_name'] = cluster_name
|
||||
template_content['roles'] = roles
|
||||
template_content['networks'] = networks
|
||||
template_content['cinder_volumes'] = cinder_volume_list
|
||||
template_content['optical_switchs'] = optical_switch_list
|
||||
template_content['services_disk'] = service_disk_list
|
||||
template_json['content'] = json.dumps(template_content)
|
||||
template_json['type'] = 'tecs'
|
||||
@@ -373,7 +390,7 @@ class Controller(controller.BaseController):
|
||||
template_list = registry.template_lists_metadata(
|
||||
req.context, **template_params)
|
||||
if template_list:
|
||||
registry.update_template_metadata(
|
||||
update_template = registry.update_template_metadata(
|
||||
req.context, template_list[0]['id'], template_json)
|
||||
template_id = template_list[0]['id']
|
||||
else:
|
||||
@@ -459,6 +476,24 @@ class Controller(controller.BaseController):
|
||||
% template_cinder_volume['role_id']
|
||||
raise HTTPBadRequest(explanation=msg, request=req)
|
||||
|
||||
def _import_optical_switchs_to_db(self, req,
|
||||
template_optical_switchs, roles):
|
||||
for template_optical_switch in template_optical_switchs:
|
||||
has_template_role = False
|
||||
for role in roles:
|
||||
if template_optical_switch['role_id'] == role['name']:
|
||||
has_template_role = True
|
||||
template_optical_switch['role_id'] = role['id']
|
||||
break
|
||||
if has_template_role:
|
||||
registry.add_optical_switch_metadata(req.context,
|
||||
template_optical_switch)
|
||||
else:
|
||||
msg = "can't find role %s in new cluster when\
|
||||
import optical_switchs from template"\
|
||||
% template_optical_switch['role_id']
|
||||
raise HTTPBadRequest(explanation=msg, request=req)
|
||||
|
||||
def _import_services_disk_to_db(self, req,
|
||||
template_services_disk, roles):
|
||||
for template_service_disk in template_services_disk:
|
||||
@@ -479,11 +514,14 @@ class Controller(controller.BaseController):
|
||||
|
||||
@utils.mutating
|
||||
def import_template_to_db(self, req, template):
|
||||
"""
|
||||
create cluster
|
||||
"""
|
||||
cluster_id = ""
|
||||
template_cluster = {}
|
||||
cluster_meta = {}
|
||||
template_meta = copy.deepcopy(template)
|
||||
template_name = template_meta.get('name', None)
|
||||
template_name = template_meta.get('template_name', None)
|
||||
cluster_name = template_meta.get('cluster', None)
|
||||
template_params = {'filters': {'name': template_name}}
|
||||
template_list = registry.template_lists_metadata(
|
||||
@@ -564,14 +602,14 @@ class Controller(controller.BaseController):
|
||||
network_exist = 'false'
|
||||
for network in networks:
|
||||
if template_content_network['name'] == network['name']:
|
||||
registry.update_network_metadata(
|
||||
update_network_meta = registry.update_network_metadata(
|
||||
req.context, network['id'],
|
||||
template_content_network)
|
||||
network_exist = 'true'
|
||||
|
||||
if network_exist == 'false':
|
||||
template_content_network['cluster_id'] = cluster_id
|
||||
registry.add_network_metadata(
|
||||
add_network_meta = registry.add_network_metadata(
|
||||
req.context, template_content_network)
|
||||
|
||||
params = {'filters': {'cluster_id': cluster_id}}
|
||||
@@ -582,7 +620,7 @@ class Controller(controller.BaseController):
|
||||
del template_content_role['config_set_id']
|
||||
for role in roles:
|
||||
if template_content_role['name'] == role['name']:
|
||||
registry.update_role_metadata(
|
||||
update_role_meta = registry.update_role_metadata(
|
||||
req.context, role['id'], template_content_role)
|
||||
role_exist = 'true'
|
||||
|
||||
@@ -593,6 +631,9 @@ class Controller(controller.BaseController):
|
||||
|
||||
self._import_cinder_volumes_to_db(
|
||||
req, template_content['cinder_volumes'], roles)
|
||||
if 'optical_switchs' in template_content:
|
||||
self._import_optical_switchs_to_db(
|
||||
req, template_content['optical_switchs'], roles)
|
||||
self._import_services_disk_to_db(req,
|
||||
template_content['services_disk'],
|
||||
roles)
|
||||
@@ -613,6 +654,16 @@ class Controller(controller.BaseController):
|
||||
try:
|
||||
template = registry.template_detail_metadata(
|
||||
req.context, template_id)
|
||||
if template.get("tecs_version_id", None):
|
||||
template['tecs_version_id'] = ""
|
||||
obj = subprocess.Popen("which daisy-manage >/dev/null "
|
||||
"&& daisy-manage db_version",
|
||||
shell=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE)
|
||||
(stdoutput, erroutput) = obj.communicate()
|
||||
if stdoutput:
|
||||
template['version'] = stdoutput.strip('\n')
|
||||
return {'template': template}
|
||||
except exception.NotFound as e:
|
||||
msg = (_("Failed to find template: %s") %
|
||||
|
||||
Reference in New Issue
Block a user