import re
import wsgiref.handlers
from google.appengine.api.appscale.ec2 import ec2
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
class Main(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
if user is not None:
response = "You are logged in as " + user.nickname() + ".
"
response += "Is that not you?
"
else:
response = "You are not logged in yet.
"
response += "Would you like to?
"
if users.is_current_user_capable("ec2_api"):
response += "You are authorized to use the EC2 API."
else:
response += "You are NOT authorized to use the EC2 API."
self.response.out.write(template.render('index.html',
{'tool': 'Welcome!',
'result': response}))
class DescribeInstances(webapp.RequestHandler):
def get(self):
response = ec2.ec2_describe_instances().replace("\n", "
")
if response == "":
response = "No instances are currently running."
self.response.out.write(template.render('index.html',
{'tool': 'EC2 Describe Instances',
'result': response}))
class RunInstances(webapp.RequestHandler):
def get(self):
opts = {"machine":"emi-2DA1127F", "t":"m1.large"}
response = ec2.ec2_run_instances(opts).replace("\n", "
")
self.response.out.write(template.render('index.html',
{'tool': 'EC2 Run Instances',
'result': response}))
class TerminateInstances(webapp.RequestHandler):
def get(self):
opts = {"ids":["i-54EA0A7A"]}
response = ec2.ec2_terminate_instances(opts).replace("\n", "
")
self.response.out.write(template.render('index.html',
{'tool': 'EC2 Terminate Instances',
'result': response}))
class AddKeypair(webapp.RequestHandler):
def get(self):
opts = {"key":"barbar"}
response = ec2.ec2_add_keypair(opts).replace("\n", "
")
self.response.out.write(template.render('index.html',
{'tool': 'EC2 Add Keypair',
'result': response}))
class DeleteKeypair(webapp.RequestHandler):
def get(self):
opts = {"key":"barbar"}
response = ec2.ec2_delete_keypair(opts).replace("\n", "
")
self.response.out.write(template.render('index.html',
{'tool': 'EC2 Delete Keypair',
'result': response}))
class DescribeAvailabilityZones(webapp.RequestHandler):
def get(self):
opts = {"euca":"verbose"} # for euca, to get the max boxen available
response = ec2.ec2_describe_availability_zones(opts).replace("\n", "
")
self.response.out.write(template.render('index.html',
{'tool': 'EC2 Describe Availability Zones',
'result': response}))
class DescribeImages(webapp.RequestHandler):
def get(self):
response = ec2.ec2_describe_images().replace("\n", "
")
self.response.out.write(template.render('index.html',
{'tool': 'EC2 Describe Images',
'result': response}))
class RebootInstances(webapp.RequestHandler):
def get(self):
opts = {"ids":["i-558E0968"]}
response = ec2.ec2_reboot_instances(opts).replace("\n", "
")
self.response.out.write(template.render('index.html',
{'tool': 'EC2 Reboot Instances',
'result': response}))
class TerminateAllInstances(webapp.RequestHandler):
def get(self):
response = ec2.ec2_describe_instances()
images_up = re.findall('\ti-[\d\w]+\t', response)
opts = {"ids":images_up}
response = ec2.ec2_terminate_instances(opts).replace("\n", "
")
self.response.out.write(template.render('index.html',
{'tool': 'Terminate all instances',
'result': response}))
class UploadCreds(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
response = "You are logged in as " + user.nickname() + ".
"
response += "Is that not you?
"
response += "Please upload your credentials:
"
response += "