Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 106 lines (86 sloc) 3.78 KB
#! /usr/bin/env python
from libcloud.types import Provider
from libcloud.providers import get_driver
from libcloud.deployment import MultiStepDeployment, ScriptDeployment, SSHKeyDeployment
from random import choice
import os, random, sys, ConfigParser
from email import Parser
# Fetch some values from the config file
config = ConfigParser.RawConfigParser()
config.read(os.path.expanduser('~/felicity/config/felicity.ini'))
# Where will we create a new VPS?
vpsprovider = config.get('Felicity', 'vpsprovider')
vpsprovider_driver = config.get(vpsprovider, 'driver')
# VPS API credentials
# Some providers don't have user attributes, only keys
# Bit of a hack right now
if vpsprovider not in [ 'Slicehost', 'Linode']:
user = config.get(vpsprovider, 'user')
key = config.get(vpsprovider, 'key')
# What type and size of VPS?
config_distro = config.get(vpsprovider, 'distro')
config_size = config.get(vpsprovider, 'size')
# Where should we send the report to?
email = config.get('Felicity', 'email')
server = 0
# Let's test to see if the request has come in via email or from CLI
def process_request():
if not sys.stdin.isatty():
# Probably come in via e-mail
headers = Parser.Parser().parse(sys.stdin)
if email in headers.get('From'):
global server
server = headers.get('Subject')
else:
raise SystemExit("Unauthorised.")
else:
# Come in from CLI. server name should be an arg.
server = sys.argv[1]
# Run some dependency checks
def dependency_check():
try:
open(os.path.expanduser('~/felicity/config/authorized_keys')).read()
except IOError:
raise SystemExit("You need an authorized_keys file in the config directory.")
try:
import fabric
except ImportError:
raise SystemExit("You need the Fabric Python library (apt-get install fabric)")
def main():
# Run some tests
dependency_check()
# handle inbound request
process_request()
# Make a new connection
Driver = get_driver( getattr(Provider, vpsprovider_driver) )
try:
conn = Driver(user, key)
except(NameError):
conn = Driver(key)
# We'll use the distro and size from the config ini
preferred_image = [image for image in conn.list_images() if config_distro in image.name]
preferred_size = [size for size in conn.list_sizes() if config_size in size.name]
# Task dispatcher to be run after provisioning the server
dispatch = [
SSHKeyDeployment(open(os.path.expanduser('~/felicity/config/authorized_keys')).read()),
ScriptDeployment("echo 'deb http://www.backports.org/debian lenny-backports main contrib non-free' >> /etc/apt/sources.list"),
ScriptDeployment("apt-get update"),
ScriptDeployment("apt-get -y install debian-archive-keyring"),
ScriptDeployment("apt-get update"),
ScriptDeployment("apt-get -y -t lenny-backports install duplicity"),
ScriptDeployment("echo 'postfix postfix/main_mailer_type select Internet Site' | debconf-set-selections"),
ScriptDeployment("echo 'postfix postfix/mailname string $HOSTNAME' | debconf-set-selections"),
ScriptDeployment("echo 'postfix postfix/destinations string localhost.localdomain, localhost' | debconf-set-selections"),
ScriptDeployment("apt-get -y install postfix mailx python-boto at git-core python-setuptools")
]
msd = MultiStepDeployment(dispatch)
# Create and deploy a new server now, and run the deployment steps defined above
print "Provisioning server and running deployment processes"
# Hostname of the new VPS
hostname = 'backup-restore-"%s"%d' % (server, random.randrange(0, 101, 2))
node = conn.deploy_node(name=hostname, image=preferred_image[0], size=preferred_size[0], deploy=msd)
print "Deploying scripts"
os.system('fab -H %s -u root deploy:server=%s' % (node.public_ip[0], server))
if __name__ == "__main__":
main()
sys.exit(0)