#!/usr/bin/python



import libxml2
import sys
import readline

doc = libxml2.newDoc("1.0")

provider_credentials = doc.newChild(None, "provider_credentials", None)

ec2_credentials = provider_credentials.newChild(None, "ec2_credentials", None)

print "This information can be found in the Security Credentials page of your AWS account."
print "At the time this tool was written that page was browseable at this URL:"
print "  https://portal.aws.amazon.com/gp/aws/securityCredentials"
print

account_number = raw_input("AWS Account ID: ")
access_key = raw_input("Access Key ID: ")
secret_access_key = raw_input("Secret Access Key: ")

account_number_node = ec2_credentials.newChild(None, "account_number", account_number)
access_key = ec2_credentials.newChild(None, "access_key", access_key)
secret_access_key = ec2_credentials.newChild(None, "secret_access_key", secret_access_key)

print "The following files can be omitted if you do not intend to build S3 based images."
print "If this is the case, just hit enter for both of these questions."
print "Note that for Amazon generated certificates you only have one opportunity to download"
print "the key file.  If you cannot find this file you  must create a new certificate pair."

certificate_file = raw_input("Certificate file: ")
key_file = raw_input("Key file: ")

if len(certificate_file) > 0:
    x509_public = open(certificate_file).read().rstrip()
else:
    x509_public = ""

if len(key_file) > 0:
    x509_private = open(key_file).read().rstrip()
else:
    x509_private = ""

certificate_node = ec2_credentials.newChild(None, "certificate", x509_public)
key_node = ec2_credentials.newChild(None, "key", x509_private)

f = open("ec2_credentials.xml", "w")
f.write(str(doc))
f.close()

print "Credentials saved in 'ec2_credentials.xml'"
