Skip to content

Commit 630345d

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Implement cloud-config ntp plugin"
2 parents 6828c7e + a9cdbb2 commit 630345d

3 files changed

Lines changed: 98 additions & 0 deletions

File tree

cloudbaseinit/plugins/common/userdataplugins/cloudconfigplugins/factory.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
'cloudconfigplugins.set_hostname.SetHostnamePlugin',
2929
'hostname': 'cloudbaseinit.plugins.common.userdataplugins.'
3030
'cloudconfigplugins.set_hostname.SetHostnamePlugin',
31+
'ntp': 'cloudbaseinit.plugins.common.userdataplugins.'
32+
'cloudconfigplugins.set_ntp.SetNtpPlugin',
3133
}
3234

3335

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2019 Cloudbase Solutions Srl
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
from oslo_log import log as oslo_logging
16+
17+
from cloudbaseinit.osutils import factory
18+
from cloudbaseinit.plugins.common.userdataplugins.cloudconfigplugins import (
19+
base
20+
)
21+
22+
LOG = oslo_logging.getLogger(__name__)
23+
24+
25+
class SetNtpPlugin(base.BaseCloudConfigPlugin):
26+
"""Change the NTP servers according to the cloud-config userdata.
27+
28+
Following keys can be present:
29+
30+
enabled: Whether to enable the NTP changes. Defaults to True.
31+
servers: A list of NTP servers. Defaults to empty list.
32+
pools: A list of NTP pool servers. Defaults to empty list.
33+
34+
Pools and servers lists are concatenated and applied to the NTP config.
35+
"""
36+
37+
def process(self, data):
38+
ntp_servers = []
39+
ntp_servers.extend(data.get('servers', []))
40+
ntp_servers.extend(data.get('pools', []))
41+
42+
if data.get('enabled', True) and ntp_servers:
43+
LOG.info("Changing NTP servers to %s." % ntp_servers)
44+
osutils = factory.get_os_utils()
45+
osutils.set_ntp_client_config(ntp_servers)
46+
47+
return False
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2019 Cloudbase Solutions Srl
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
import unittest
16+
17+
try:
18+
import unittest.mock as mock
19+
except ImportError:
20+
import mock
21+
22+
from oslo_config import cfg
23+
24+
from cloudbaseinit.plugins.common.userdataplugins.cloudconfigplugins import (
25+
set_ntp
26+
)
27+
28+
29+
CONF = cfg.CONF
30+
31+
32+
class SetNtpPluginTest(unittest.TestCase):
33+
34+
def setUp(self):
35+
self._set_ntp = set_ntp.SetNtpPlugin()
36+
37+
@mock.patch('cloudbaseinit.osutils.factory.get_os_utils')
38+
def _test_process(self, mock_data, mock_get_os_utils):
39+
mock_os_util = mock.MagicMock()
40+
mock_os_util.set_ntp_client_config.return_value = True
41+
mock_get_os_utils.return_value = mock_os_util
42+
result_process = self._set_ntp.process(mock_data)
43+
self.assertFalse(result_process)
44+
45+
def test_process_no_servers(self):
46+
self._test_process({})
47+
48+
def test_process_servers_pools(self):
49+
self._test_process({'servers': ['one', 'two'], 'pools': 'one'})

0 commit comments

Comments
 (0)