-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HYD-1304 Synchronize time on chroma nodes
Change-Id: Ia2a3cdeff204b0813f5579e455a5541e6897371b
- Loading branch information
Brian J. Murrell
committed
Aug 3, 2012
1 parent
b433f88
commit 7335875
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# | ||
# ======================================================== | ||
# Copyright (c) 2012 Whamcloud, Inc. All rights reserved. | ||
# ======================================================== | ||
|
||
|
||
"""NTP actions.""" | ||
|
||
from chroma_agent import shell | ||
from chroma_agent.plugins import ActionPlugin | ||
|
||
import os | ||
|
||
|
||
def unconfigure_ntp(args): | ||
args.node = "" | ||
configure_ntp(args) | ||
|
||
|
||
def configure_ntp(args): | ||
from tempfile import mkstemp | ||
tmp_f, tmp_name = mkstemp(dir = '/etc') | ||
f = open('/etc/ntp.conf', 'r') | ||
added_server = False | ||
for line in f.readlines(): | ||
if args.node == "": | ||
if line.startswith("server "): | ||
continue | ||
elif line.startswith("# Commented by chroma-agent: "): | ||
line = line[29:] | ||
else: | ||
if line.startswith("server "): | ||
if not added_server: | ||
os.write(tmp_f, "server %s\n" % args.node) | ||
added_server = True | ||
line = "# Commented by chroma-agent: %s" % line | ||
os.write(tmp_f, line) | ||
f.close() | ||
os.close(tmp_f) | ||
os.chmod(tmp_name, 0644) | ||
if not os.path.exists("/etc/ntp.conf.pre-chroma"): | ||
os.rename("/etc/ntp.conf", "/etc/ntp.conf.pre-chroma") | ||
os.rename(tmp_name, "/etc/ntp.conf") | ||
|
||
# make sure the time is very close before letting ntpd take over | ||
shell.run(['service', 'ntpd', 'stop']) | ||
shell.run(['service', 'ntpdate', 'restart']) | ||
# signal the process | ||
rc, stdout, stderr = shell.run(['service', 'ntpd', 'start']) | ||
|
||
|
||
class RsyslogPlugin(ActionPlugin): | ||
def register_commands(self, parser): | ||
p = parser.add_parser("configure-ntp", | ||
help="configure NTP server") | ||
p.add_argument("--node", required=True, | ||
help="NTP server") | ||
p.set_defaults(func=configure_ntp) | ||
|
||
p = parser.add_parser("unconfigure-ntp", | ||
help="unconfigure NTP server") | ||
p.set_defaults(func=unconfigure_ntp) |