Added userdata plugin for setting the hostname

Change-Id: I95fcc5e92806e0de345c02898bd4a4bcfe698399
This commit is contained in:
Ionut Hulub
2015-06-09 04:13:00 -07:00
parent fccf0e6f00
commit 12a6d8a9ab
8 changed files with 211 additions and 93 deletions

View File

@@ -42,6 +42,8 @@ TAG_REGEX = {
)
}
NO_REBOOT = 0
# important return values range
RET_START = 1001
RET_END = 1003

View File

@@ -12,57 +12,25 @@
# License for the specific language governing permissions and limitations
# under the License.
import platform
import re
from oslo_config import cfg
from oslo_log import log as oslo_logging
from cloudbaseinit.osutils import factory as osutils_factory
from cloudbaseinit.plugins.common import base
opts = [
cfg.BoolOpt('netbios_host_name_compatibility', default=True,
help='Truncates the hostname to 15 characters for Netbios '
'compatibility'),
]
CONF = cfg.CONF
CONF.register_opts(opts)
from cloudbaseinit.utils import hostname
LOG = oslo_logging.getLogger(__name__)
NETBIOS_HOST_NAME_MAX_LEN = 15
class SetHostNamePlugin(base.BasePlugin):
def execute(self, service, shared_data):
osutils = osutils_factory.get_os_utils()
metadata_host_name = service.get_host_name()
if not metadata_host_name:
LOG.debug('Hostname not found in metadata')
return base.PLUGIN_EXECUTION_DONE, False
metadata_host_name = metadata_host_name.split('.', 1)[0]
if (len(metadata_host_name) > NETBIOS_HOST_NAME_MAX_LEN and
CONF.netbios_host_name_compatibility):
new_host_name = metadata_host_name[:NETBIOS_HOST_NAME_MAX_LEN]
LOG.warn('Truncating host name for Netbios compatibility. '
'Old name: %(metadata_host_name)s, new name: '
'%(new_host_name)s' %
{'metadata_host_name': metadata_host_name,
'new_host_name': new_host_name})
else:
new_host_name = metadata_host_name
new_host_name = re.sub(r'-$', '0', new_host_name)
if platform.node().lower() == new_host_name.lower():
LOG.debug("Hostname already set to: %s" % new_host_name)
reboot_required = False
else:
LOG.info("Setting hostname: %s" % new_host_name)
reboot_required = osutils.set_host_name(new_host_name)
(_, reboot_required) = hostname.set_hostname(
osutils, metadata_host_name)
return base.PLUGIN_EXECUTION_DONE, reboot_required

View File

@@ -17,6 +17,7 @@ from oslo_config import cfg
from oslo_log import log as oslo_logging
import yaml
from cloudbaseinit.plugins.common import execcmd
from cloudbaseinit.plugins.common.userdataplugins import base
from cloudbaseinit.plugins.common.userdataplugins.cloudconfigplugins import (
factory
@@ -37,7 +38,6 @@ OPTS = [
CONF = cfg.CONF
CONF.register_opts(OPTS)
DEFAULT_ORDER_VALUE = 999
REBOOT = 1001
class CloudConfigError(Exception):
@@ -80,7 +80,7 @@ class CloudConfigPluginExecutor(object):
def execute(self):
"""Call each plugin, in the order requested by the user."""
reboot = 0
reboot = execcmd.NO_REBOOT
plugins = factory.load_plugins()
for plugin_name, value in self._expected_plugins:
method = plugins.get(plugin_name)
@@ -91,7 +91,7 @@ class CloudConfigPluginExecutor(object):
try:
requires_reboot = method(value)
if requires_reboot:
reboot = REBOOT
reboot = execcmd.RET_END
except Exception:
LOG.exception("Processing plugin %s failed", plugin_name)
return reboot

View File

@@ -22,6 +22,8 @@ PLUGINS = {
'cloudconfigplugins.write_files.WriteFilesPlugin',
'set_timezone': 'cloudbaseinit.plugins.common.userdataplugins.'
'cloudconfigplugins.set_timezone.SetTimezonePlugin',
'set_hostname': 'cloudbaseinit.plugins.common.userdataplugins.'
'cloudconfigplugins.set_hostname.SetHostnamePlugin',
}

View File

@@ -0,0 +1,38 @@
# Copyright 2015 Cloudbase Solutions Srl
#
# 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.
from oslo_log import log as oslo_logging
from cloudbaseinit.osutils import factory
from cloudbaseinit.plugins.common.userdataplugins.cloudconfigplugins import (
base
)
from cloudbaseinit.utils import hostname
LOG = oslo_logging.getLogger(__name__)
class SetHostnamePlugin(base.BaseCloudConfigPlugin):
"""Change the hostname for the underlying platform.
If the timezone is changed a restart will be required.
"""
def process(self, data):
LOG.info("Changing hostname to %r", data)
osutils = factory.get_os_utils()
_, reboot_required = hostname.set_hostname(osutils, data)
return reboot_required