From 437d29cb941f7db680b4665acae24bfdcdcaf708 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Wed, 25 Jul 2012 18:11:56 -0700 Subject: add timezone setting hook --- README | 2 -- exmachina.py | 22 ++++++++++++++++++---- test_exmachina.py | 16 ++++++++++++---- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/README b/README index 518cfa6..9d5d528 100644 --- a/README +++ b/README @@ -17,8 +17,6 @@ Features: * call init.d service scripts: status, start, stop, restart TODO: -* add apt-get package installation/removal methods -* add/handle one or two common non-augeas configuration methods (eg, timezone?) * use /var/lib/exmachina/ as socket instead of /tmp/exmachina.sock? ### Dependancies (server) diff --git a/exmachina.py b/exmachina.py index 60c0771..21de493 100755 --- a/exmachina.py +++ b/exmachina.py @@ -31,11 +31,11 @@ client in the same way. The init_test.sh script demonstrates this mechanism. import os import sys import grp +import shutil import argparse import logging import socket import subprocess -import stat import time import base64 @@ -55,7 +55,7 @@ def execute_service(servicename, action, timeout=10): script = "/etc/init.d/" + os.path.split(servicename)[1] if not os.path.exists(script): - return "ERROR: so such service" + raise ValueError("so such service: %s" % servicename) command_list = [script, action] log.info("executing: %s" % command_list) @@ -75,6 +75,7 @@ def execute_service(servicename, action, timeout=10): (timeout, command_list)) stdout, stderr = proc.communicate() + # TODO: should raise exception here if proc.returncode != 0? return stdout, stderr, proc.returncode def execute_apt(packagename, action, timeout=120, aptargs=['-q', '-y']): @@ -182,8 +183,19 @@ class ExMachinaHandler(bjsonrpc.handlers.BaseHandler): def set_timezone(self, tzname): if not self.secret_key: log.info("reset timezone to %s" % tzname) - pass - + tzname = tzname.strip() + tzpath = os.path.join("/usr/share/zoneinfo", tzname) + try: + os.stat(tzpath) + except OSError: + # file not found + raise ValueError("timezone not valid: %s" % tzname) + shutil.copy( + os.path.join("/usr/share/zoneinfo", tzname), + "/etc/localtime") + with open("/etc/timezone", "w") as tzfile: + tzfile.write(tzname + "\n") + return "timezone changed to %s" % tzname # ------------- init.d Service Control ----------------- def initd_status(self, servicename): @@ -249,6 +261,7 @@ class ExMachinaClient(): self.augeas = EmptyClass() self.initd = EmptyClass() self.apt = EmptyClass() + self.misc = EmptyClass() self.augeas.save = self.conn.call.augeas_save self.augeas.set = self.conn.call.augeas_set @@ -265,6 +278,7 @@ class ExMachinaClient(): self.apt.install = self.conn.call.apt_install self.apt.update = self.conn.call.apt_update self.apt.remove = self.conn.call.apt_remove + self.misc.set_timezone = self.conn.call.set_timezone def close(self): self.sock.close() diff --git a/test_exmachina.py b/test_exmachina.py index ec73def..e8d239d 100755 --- a/test_exmachina.py +++ b/test_exmachina.py @@ -20,13 +20,11 @@ at the same time: """ import sys -import optparse -import logging import socket import bjsonrpc import bjsonrpc.connection -import augeas +from bjsonrpc.exceptions import ServerError from exmachina import ExMachinaClient @@ -62,12 +60,22 @@ def main(): client = ExMachinaClient(secret_key=secret_key) print client.augeas.match("/files/etc/*") #print client.initd.restart("bluetooth") - print client.initd.status("greentooth") + try: + print client.initd.status("greentooth") + print "ERROR: should have failed above!" + except ServerError: + print "(got expected error, good!)" print "(expect Error on the above line)" print client.initd.status("bluetooth") print client.apt.install("pkg_which_does_not_exist") print client.apt.remove("pkg_which_does_not_exist") #print client.apt.update() # can be slow... + #print client.misc.set_timezone("UTC") # don't clobber system... + try: + print client.misc.set_timezone("whoopie") # should be an error + print "ERROR: should have failed above!" + except ServerError: + print "(got expected error, good!)" client.close() if __name__ == '__main__': -- cgit v1.2.3