diff options
author | bnewbold <bnewbold@robocracy.org> | 2012-07-25 18:11:56 -0700 |
---|---|---|
committer | bnewbold <bnewbold@robocracy.org> | 2012-07-25 18:12:19 -0700 |
commit | 437d29cb941f7db680b4665acae24bfdcdcaf708 (patch) | |
tree | 199ba5a81b2a8fb8c6b6918cb24f9b8edace9253 | |
parent | 3dbedae6d25dc4652f7ac1a1542e19e630b90894 (diff) | |
download | exmachina-437d29cb941f7db680b4665acae24bfdcdcaf708.tar.gz exmachina-437d29cb941f7db680b4665acae24bfdcdcaf708.zip |
add timezone setting hook
-rw-r--r-- | README | 2 | ||||
-rwxr-xr-x | exmachina.py | 22 | ||||
-rwxr-xr-x | test_exmachina.py | 16 |
3 files changed, 30 insertions, 10 deletions
@@ -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/<something> 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__': |