From a6367969dc6a858e78e62a7fe1ad62d163fb6803 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Tue, 8 May 2012 04:47:12 +0200 Subject: [PATCH] API --- console/master/dropper.py | 74 ++++++++++++++++++++++++++++++++- frontend/api.local.php | 24 +++++++++-- frontend/classes/class.node.php | 11 +++++ 3 files changed, 104 insertions(+), 5 deletions(-) diff --git a/console/master/dropper.py b/console/master/dropper.py index 0ddae6b..6519607 100644 --- a/console/master/dropper.py +++ b/console/master/dropper.py @@ -1,7 +1,9 @@ #!/usr/bin/env python -import paramiko, socket, sys, termios, tty, select +import paramiko, socket, sys, termios, tty, select, urllib, urllib2, json +key = "abcde" +endpoint = "http://cvm.local/api.local.php" def posix_shell(chan): oldtty = termios.tcgetattr(sys.stdin) @@ -17,7 +19,7 @@ def posix_shell(chan): try: buff = chan.recv(1024) if len(buff) == 0: - print '\r\nYou have been logged out of your container.\r\n', + print '\r\nYou have been logged out of your container. Goodbye!\r\n', break sys.stdout.write(buff) sys.stdout.flush() @@ -31,6 +33,74 @@ def posix_shell(chan): finally: termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty) + +def api_request(parameters, method="GET"): + if method == "GET": + querystring = urllib.urlencode(parameters) + req = urllib2.Request(endpoint + "?" + querystring) + response = urllib2.urlopen(req) + return json.loads(response.read()) + + +print "#############################################################" +print "### CVM OpenVZ shell dropper ###" +print "#############################################################" +print "" +print "Please enter your VPS panel login details to continue." +print "" +username = raw_input("Username: ") +password = raw_input("Password: ") +print "" + +auth_result = api_request({ + 'key': key, + 'action': "verify_user", + 'username': username, + 'password': password +}) + +if auth_result['data']['correct'] == True: + vpslist = api_request({ + 'key': key, + 'action': "list_vps", + 'userid': auth_result["data"]["userid"] + }) + + print "Select the container you wish to log in to." + print "" + + i = 1 + vpsmap = {} + nodelist = [] + nodemap = {} + + for vps in vpslist["data"]: + vpsmap[i] = vps + + if vps["NodeId"] not in nodelist: + nodelist.append(vps["NodeId"]) + + i += 1 + + for node in nodelist: + nodemap[node] = api_request({ + 'key': key, + 'action': "node_info", + 'nodeid': node + })['data'] + + for key, vps in vpsmap.items(): + node = nodemap[vps['NodeId']] + print "%s. %s (%s [%s], %s)" % (key, vps['Hostname'], node['Name'], node['Hostname'], node['PhysicalLocation']) + + print "" + choice = raw_input("Make your choice: ") + + exit(0) +else: + print "The supplied login details are invalid." + print "Your session will now be closed." + exit(1) diff --git a/frontend/api.local.php b/frontend/api.local.php index 8eaf874..80c1211 100644 --- a/frontend/api.local.php +++ b/frontend/api.local.php @@ -29,12 +29,18 @@ if(isset($_GET['key']) && $_GET['key'] == $settings['local_api_key']) if($sUser->VerifyPassword($_GET['password']) === true) { - $return_object = true; + $return_object = array( + 'correct' => true, + 'userid' => $sUser->sId + ); $return_success = true; } else { - $return_object = false; + $return_object = array( + 'correct' => false, + 'userid' => 0 + ); $return_success = true; } } @@ -63,13 +69,25 @@ if(isset($_GET['key']) && $_GET['key'] == $settings['local_api_key']) } $return_object = $sContainers; - $return_status = true; + $return_success = true; } break; case "vps_info": // TODO: return VPS info break; + + case "node_info": + try + { + $sNode = new Node($_GET['nodeid']); + $return_object = $sNode->Export(); + } + catch (NotFoundException $e) + { + // Silently pass. + } + break; } } else diff --git a/frontend/classes/class.node.php b/frontend/classes/class.node.php index bb3c901..68421ac 100644 --- a/frontend/classes/class.node.php +++ b/frontend/classes/class.node.php @@ -36,6 +36,17 @@ class Node extends CPHPDatabaseRecordClass ) ); + public $prototype_export = array( + 'Name', + 'Hostname', + 'Port', + 'User', + 'PhysicalLocation', + 'PrivateKey', + 'PublicKey', + 'HasCustomkey' + ); + public $ssh = null; protected function EventConstructed()