| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | import sys |
|---|
| 23 | import socket |
|---|
| 24 | import gdhost.client as client |
|---|
| 25 | from gdhost.version import version_string |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | if 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" |
|---|
| 37 | elif sys.version_info >= (2,3): |
|---|
| 38 | import optparse |
|---|
| 39 | else: |
|---|
| 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 | |
|---|
| 43 | parser = optparse.OptionParser(version=version_string, |
|---|
| 44 | usage="%prog [options] command") |
|---|
| 45 | parser.add_option( '-k', '--key', action="store", |
|---|
| 46 | dest="key", |
|---|
| 47 | help="Encryption Key (required)") |
|---|
| 48 | parser.add_option( '-n', '--name', action="store", |
|---|
| 49 | dest="name", default= socket.gethostname(), |
|---|
| 50 | help="The dynamic host shortname" ) |
|---|
| 51 | parser.add_option( '-i', '--ip', action="store", |
|---|
| 52 | dest='ip', default='auto', |
|---|
| 53 | help="Your IP address or 'auto'" ) |
|---|
| 54 | parser.add_option( '-S', '--server', action="store", |
|---|
| 55 | dest="server", default="gerf.net", |
|---|
| 56 | help="The GDH server's dns name or ip address" ) |
|---|
| 57 | parser.add_option( '-P', '--port', action="store", |
|---|
| 58 | dest="port", default=8888, |
|---|
| 59 | help="The GDH server's port number" ) |
|---|
| 60 | parser.add_option( '-q', '--quiet', action="store_true", |
|---|
| 61 | dest="quiet", default=False, |
|---|
| 62 | help="Run quietly" ) |
|---|
| 63 | parser.add_option( '-d', '--debug', action="store_true", |
|---|
| 64 | dest="debug", default=False, |
|---|
| 65 | help="Turn on debugging" ) |
|---|
| 66 | |
|---|
| 67 | if "__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 | |
|---|
| 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 | |
|---|
| 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) |
|---|