root / branches / refactor_testable / gdhostc

Revision 58, 4.9 kB (checked in by docwhat, 3 years ago)

r1070@LC2000: docwhat | 2005-09-21T04:29:22.300345Z

  • Yay! It works. Just need to finish polishing.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to author date id revision
Line 
1#!/usr/bin/python
2## -*- coding: utf-8 -*-
3##
4## gdhostc -- Gerf Dynamic Host Client
5##
6## Copyright (C) 2001-2002,2005 Nick Borko & Christian H�e
7## This program is free software; you can redistribute it and/or modify
8## it under the terms of the GNU General Public License as published by
9## the Free Software Foundation; either version 2 of the License, or
10## (at your option) any later version.
11##
12## This program is distributed in the hope that it will be useful,
13## but WITHOUT ANY WARRANTY; without even the implied warranty of
14## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15## GNU General Public License for more details.
16##
17## You should have received a copy of the GNU General Public License
18## along with this program; if not, write to the Free Software
19## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20##
21
22import sys
23import socket
24import gdhost.client as client
25from gdhost.version import version_string
26
27## Version Check
28if sys.version_info >= (2, 2) and \
29   sys.version_info <  (2,3):
30    try:
31        import optik as optparse
32    except ImportError:
33        try:
34            import optparse
35        except ImportError:
36            print >> sys.stderr, "You must either install optik (http://optik.sf.net) or upgrade to python 2.3"
37elif sys.version_info >= (2,3):
38    import optparse
39else:
40    print >> sys.stderr, "This program requires python 2.2 with the optik module (http://optik.sf.net) or python 2.3 or greater"
41    sys.exit(1)
42
43parser = optparse.OptionParser(version=version_string,
44                               usage="%prog [options] command")
45parser.add_option( '-k', '--key', action="store",
46                        dest="key",
47                        help="Encryption Key (required)")
48parser.add_option( '-n', '--name', action="store",
49                   dest="name", default= socket.gethostname(),
50                   help="The dynamic host shortname" )
51parser.add_option( '-i', '--ip', action="store",
52                   dest='ip', default='auto',
53                   help="Your IP address or 'auto'" )
54parser.add_option( '-S', '--server', action="store",
55                   dest="server", default="gerf.net",
56                   help="The GDH server's dns name or ip address" )
57parser.add_option( '-P', '--port', action="store",
58                   dest="port", default=8888,
59                   help="The GDH server's port number" )
60parser.add_option( '-q', '--quiet', action="store_true",
61                   dest="quiet", default=False,
62                   help="Run quietly" )
63parser.add_option( '-d', '--debug', action="store_true",
64                   dest="debug", default=False,
65                   help="Turn on debugging" )
66
67if "__main__" == __name__:
68    options, command = parser.parse_args()
69    if command:
70        command = command[0].strip()
71        if command not in ('update','remove') :
72            parser.error('Command must be one of update or remove; not %s' % command[0])
73            sys.exit(1)
74
75    # Helpful display
76    if not command:
77        parser.print_help()
78        print 
79        print "current values:"
80        print "  KEY                = '%s'" % (options.key or '-required-')
81        print "  NAME               = '%s'" % options.name
82        print "  IP                 = '%s'" % options.ip
83        print "  SERVER             = '%s'" % options.server
84        print "  PORT               = '%s'" % options.port
85        sys.exit(1)
86
87    # Required values
88    if not options.key:
89        parser.error('You must set the encryption key (-k)')
90        sys.exit(1)
91
92    connection = client.Connection( server=options.server,
93                                    port=options.port )
94    try:
95        if command == 'update':
96            connection.update( key=options.key,
97                               name=options.name,
98                               ip=options.ip )
99        else:
100            connection.delete( key=options.key,
101                               name=options.name )
102    except client.ErrorConnection,err:
103        if options.debug:
104            raise
105        else:
106            print >> sys.stderr, "Unable to talk with server: %s" % str(err)
107            sys.exit(10)
108    except client.ErrorDenied:
109        if options.debug:
110            raise
111        else:
112            print >> sys.stderr, "The server rejected your request"
113            sys.exit(10)
114    except client.ErrorNoName:
115        if options.debug:
116            raise
117        else:
118            print >> sys.stderr, "The server didn't recognize the name %s" % options.name
119            sys.exit(10)
120    except:
121        if options.debug:
122            raise
123        print >> sys.stderr, "Unexpected Error!"
124        if options.debug:
125            print >> sys.stderr, "Plase file a bug at http://trac.gerf.org/gdhost with the above output"
126        else:
127            print >> sys.stderr, "Please rerun this command with --debug and file a bug at http://trac.gerf.org/gdhost"
128       
129    print "Successfully did a %s for %s!" % (command, options.name)
130    sys.exit(0)
Note: See TracBrowser for help on using the browser.