From bb0ec725b508e339ff790eb63c0bf193c14a20c7 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Wed, 11 Jul 2012 17:36:55 -0400 Subject: basic client library --- README | 4 +--- exmachina.py | 29 ++++++++++++++++++++++++++--- test.py | 16 ++++++++++++---- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/README b/README index c96fa0f..a8365f4 100644 --- a/README +++ b/README @@ -8,12 +8,10 @@ ### Status -Just a first commit... +Basic server and client functionality implemented. Very crude. TODO: -* re-implement python-augeas methods using API client-side * use /var/lib/exmachina/ as socket instead of /tmp/exmachina.sock? -* check/set permissions on socket after server opens it * tests and demonstrations ### Dependancies (server) diff --git a/exmachina.py b/exmachina.py index 254fc10..a72ac55 100755 --- a/exmachina.py +++ b/exmachina.py @@ -11,13 +11,13 @@ import logging import socket import subprocess import stat +import time import bjsonrpc import bjsonrpc.handlers import bjsonrpc.server import augeas -import time # TODO log = logging.getLogger(__name__) @@ -115,9 +115,32 @@ class ExMachinaHandler(bjsonrpc.handlers.BaseHandler): def initd_restart(self, servicename): return run_service(servicename, "restart") - + +class EmptyClass(): + pass + class ExMachinaClient(): - pass #TODO + + def __init__(self, socket_path = "/tmp/exmachina.sock"): + self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self.sock.connect(socket_path) + self.conn = bjsonrpc.connection.Connection(self.sock) + + self.augeas = EmptyClass() + self.initd = EmptyClass() + + self.augeas.save = self.conn.call.augeas_save + self.augeas.set = self.conn.call.augeas_set + self.augeas.setm = self.conn.call.augeas_setm + self.augeas.get = self.conn.call.augeas_get + self.augeas.match = self.conn.call.augeas_match + self.augeas.insert = self.conn.call.augeas_insert + self.augeas.move = self.conn.call.augeas_move + self.augeas.remove = self.conn.call.augeas_remove + self.initd.status = self.conn.call.initd_status + self.initd.start = self.conn.call.initd_start + self.initd.stop = self.conn.call.initd_stop + self.initd.restart = self.conn.call.initd_restart def run_server(socket_path="/tmp/exmachina.sock"): # TODO: check for root permissions, warn if not root diff --git a/test.py b/test.py index b9295b8..d780906 100755 --- a/test.py +++ b/test.py @@ -9,6 +9,8 @@ import bjsonrpc import bjsonrpc.connection import augeas +from exmachina import ExMachinaClient + # ============================================================================= # Command line handling def main(): @@ -17,16 +19,22 @@ def main(): sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(socket_path) + print "========= Testing low level connection" c = bjsonrpc.connection.Connection(sock) print "time: %s" % c.call.test_whattime() - print "files: %s" % c.call.test_listfiles() - print c.call.initd_status("bluetooth") print "/*: %s" % c.call.augeas_match("/*") - print "/files/*: %s" % c.call.augeas_match("/files/*") - print "/files/etc/*: %s" % c.call.augeas_match("/files/etc/*") print "/augeas/*: %s" % c.call.augeas_match("/augeas/*") + print "/etc/* files:" + for name in c.call.augeas_match("/files/etc/*"): + print "\t%s" % name + print c.call.initd_status("bluetooth") print "hostname: %s" % c.call.augeas_get("/files/etc/hostname/*") print "localhost: %s" % c.call.augeas_get("/files/etc/hosts/1/canonical") + print "========= Testing user client library" + client = ExMachinaClient() + print client.augeas.match("/files/etc/*") + print client.initd.restart("bluetooth") + if __name__ == '__main__': main() -- cgit v1.2.3