diff --git a/releasenotes/notes/ntp-config-51ed9d612132e2fa.yaml b/releasenotes/notes/ntp-config-51ed9d612132e2fa.yaml new file mode 100644 index 0000000000..772c30e45f --- /dev/null +++ b/releasenotes/notes/ntp-config-51ed9d612132e2fa.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + This fixes the issue with NTP configuration where a prefered server + provided by the user is added to the end of the file and the defaults + are not deleted. Here we add the prefered server to the top of the file. diff --git a/sahara/service/ntp_service.py b/sahara/service/ntp_service.py index 053e3b69c9..d65840e55f 100644 --- a/sahara/service/ntp_service.py +++ b/sahara/service/ntp_service.py @@ -78,8 +78,8 @@ def _configure_ntp_on_instance(instance, url): LOG.warning("Unable to configure NTP service") return - r.append_to_file( - "/etc/ntp.conf", "server {url}".format(url=url), + r.prepend_to_file( + "/etc/ntp.conf", "server {url} iburst\n".format(url=url), run_as_root=True) _restart_ntp(r) try: diff --git a/sahara/service/ops.py b/sahara/service/ops.py index cb1264b572..101215f8e5 100644 --- a/sahara/service/ops.py +++ b/sahara/service/ops.py @@ -298,6 +298,7 @@ def _provision_cluster(cluster_id): cluster = conductor.cluster_get(ctx, cluster_id) context.set_step_type(_("Engine: create cluster")) INFRA.create_cluster(cluster) + ntp_service.configure_ntp(cluster_id) # configure cluster cluster = c_u.change_cluster_status( @@ -309,7 +310,6 @@ def _provision_cluster(cluster_id): plugin.configure_cluster(cluster) # starting prepared and configured cluster - ntp_service.configure_ntp(cluster_id) cluster = c_u.change_cluster_status( cluster, c_u.CLUSTER_STATUS_STARTING) diff --git a/sahara/tests/unit/service/test_ntp_service.py b/sahara/tests/unit/service/test_ntp_service.py index 7a9d2ffe3b..838add7fb6 100644 --- a/sahara/tests/unit/service/test_ntp_service.py +++ b/sahara/tests/unit/service/test_ntp_service.py @@ -45,6 +45,9 @@ class FakeRemote(object): def append_to_file(self, file, text, run_as_root=False): return self.execute_command(file, run_as_root) + def prepend_to_file(self, file, text, run_as_root=False): + return self.execute_command(file, run_as_root) + def get_os_distrib(self): return self.execute_command('get_os_distrib') diff --git a/sahara/utils/ssh_remote.py b/sahara/utils/ssh_remote.py index c03dbf2c10..c734fae87d 100644 --- a/sahara/utils/ssh_remote.py +++ b/sahara/utils/ssh_remote.py @@ -301,6 +301,19 @@ def _append_file(sftp, remote_file, data, run_as_root): _append_fl(sftp, remote_file, data) +def _prepend_file(sftp, remote_file, data, run_as_root): + if run_as_root: + temp_file = 'temp-file-%s' % uuidutils.generate_uuid() + temp_remote_file = 'temp-remote-file-%s' % uuidutils.generate_uuid() + _write_fl(sftp, temp_file, data) + _execute_command( + 'cat %s > %s' % (remote_file, temp_remote_file)) + _execute_command( + 'cat %s %s > %s' % ( + temp_file, temp_remote_file, remote_file), run_as_root=True) + _execute_command('rm -f %s %s' % (temp_file, temp_remote_file)) + + def _write_file_to(remote_file, data, run_as_root=False): global _ssh @@ -331,6 +344,21 @@ def _append_to_files(files, run_as_root=False): _append_file(sftp, fl, data, run_as_root) +def _prepend_to_file(remote_file, data, run_as_root=False): + global _ssh + + _prepend_file(_ssh.open_sftp(), remote_file, data, run_as_root) + + +def _prepend_to_files(files, run_as_root=False): + global _ssh + + sftp = _ssh.open_sftp() + + for fl, data in six.iteritems(files): + _prepend_file(sftp, fl, data, run_as_root) + + def _read_file(sftp, remote_file): fl = sftp.file(remote_file, 'r') data = fl.read() @@ -878,6 +906,12 @@ class InstanceInteropHelper(remote.Remote): self._log_command(description) self._run_s(_append_to_files, timeout, description, files, run_as_root) + def prepend_to_file(self, r_file, data, run_as_root=False, timeout=None): + description = _('Prepending to file "%s"') % r_file + self._log_command(description) + self._run_s(_prepend_to_file, timeout, description, + r_file, data, run_as_root) + def read_file_from(self, remote_file, run_as_root=False, timeout=None): description = _('Reading file "%s"') % remote_file self._log_command(description)