summaryrefslogtreecommitdiffstats
path: root/pyanaconda/ntp.py
blob: 3542c0c435cbf7273f47c7af690867fa71044757 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#
# Copyright (C) 2012  Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.  You should have received a copy of the
# GNU General Public License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
# Red Hat Author(s): Vratislav Podzimek <vpodzime@redhat.com>
#

"""
Module facilitating the work with NTP servers and NTP daemon's configuration

"""

import re
import os
import tempfile
import shutil

from pyanaconda import iutil
from pyanaconda.threads import threadMgr, AnacondaThread

NTP_CONFIG_FILE = "/etc/chrony.conf"

#example line:
#server 0.fedora.pool.ntp.org iburst
SRV_LINE_REGEXP = re.compile(r"^\s*server\s*([-a-zA-Z.0-9]+)\s*[a-zA-Z]+\s*$")

class NTPconfigError(Exception):
    """Exception class for NTP related problems"""
    pass

def ntp_server_working(server):
    """
    Runs rdate to try to connect to the $server (timeout may take some time).

    @return: True if the given server is reachable and working, False otherwise

    """

    #FIXME: It would be nice to use execWithRedirect here, but it is not
    #       thread-safe and hangs if this function is called from threads.
    #       By using tee (and block-buffered pipes) it is also much slower.
    #we just need to know the exit status
    retc = os.system("rdate -p %s &>/dev/null" % server)

    return retc == 0

def get_servers_from_config(conf_file_path=NTP_CONFIG_FILE,
                            srv_regexp=SRV_LINE_REGEXP):
    """
    Goes through the chronyd's configuration file looking for lines starting
    with 'server'.

    @return: servers found in the chronyd's configuration
    @rtype: list

    """

    ret = list()

    try:
        with open(conf_file_path, "r") as conf_file:
            for line in conf_file:
                match = srv_regexp.match(line)
                if match:
                    ret.append(match.group(1))

    except IOError as ioerr:
        msg = "Cannot open config file %s for reading (%s)" % (conf_file_path,
                                                               ioerr.strerror)
        raise NTPconfigError(msg)

    return ret

def save_servers_to_config(servers, conf_file_path=NTP_CONFIG_FILE,
                           srv_regexp=SRV_LINE_REGEXP, out_file_path=None):
    """
    Replaces the servers defined in the chronyd's configuration file with
    the given ones. If the out_file is not None, then it is used for the
    resulting config.

    @type servers: iterable
    @param out_file_path: path to the file used for the resulting config

    """

    try:
        old_conf_file = open(conf_file_path, "r")

    except IOError as ioerr:
        msg = "Cannot open config file %s for reading (%s)" % (conf_file_path,
                                                               ioerr.strerror)
        raise NTPconfigError(msg)

    try:
        if out_file_path:
            new_conf_file = open(out_file_path, "w")
        else:
            (fildes, temp_path) = tempfile.mkstemp()
            new_conf_file = os.fdopen(fildes, "w")

    except IOError as ioerr:
        if out_file_path:
            msg = "Cannot open new config file %s "\
                  "for writing (%s)" % (out_file_path, ioerr.strerror)
        else:
            msg = "Cannot open temporary file %s "\
                  "for writing (%s)" % (temp_path, ioerr.strerror)

        raise NTPconfigError(msg)

    heading = "# These servers were defined in the installation:\n"

    #write info about the origin of the following lines
    new_conf_file.write(heading)

    #write new servers
    for server in servers:
        new_conf_file.write("server " + server + " iburst\n")

    #copy non-server lines from the old config and skip our heading
    for line in old_conf_file:
        if not srv_regexp.match(line) and line != heading:
            new_conf_file.write(line)

    old_conf_file.close()
    new_conf_file.close()

    if not out_file_path:
        try:
            stat = os.stat(conf_file_path)
            shutil.move(temp_path, conf_file_path)
            os.chmod(conf_file_path, stat.st_mode)

        except OSError as oserr:
            msg = "Cannot replace the old config with "\
                  "the new one (%s)" % (oserr.strerror)

            raise NTPconfigError(msg)

def one_time_sync(server, callback=None):
    """
    Synchronize the system time with a given NTP server. Note that this
    function is blocking and will not return until the time gets synced or
    querying server fails (may take some time before timeouting).

    @param server: NTP server
    @param callback: callback function to run after sync or failure
    @type callback: a function taking one boolean argument (success)
    @return: True if the sync was successful, False otherwise

    """

    ret = iutil.execWithRedirect("rdate", ["-s", server])

    success = ret == 0

    if callback is not None:
        callback(success)

    return success

def one_time_sync_async(server, callback=None):
    """
    Asynchronously synchronize the system time with a given NTP server. This
    function is non-blocking it starts a new thread for synchronization and
    returns. Use callback argument to specify the function called when the
    new thread finishes if needed.

    @param server: NTP server
    @param callback: callback function to run after sync or failure
    @type callback: a function taking one boolean argument (success)

    """

    thread_name = "AnaSyncTime_%s" % server
    if threadMgr.get(thread_name):
        #syncing with the same server running
        return

    threadMgr.add(AnacondaThread(name=thread_name, target=one_time_sync,
                                 args=(server, callback)))