Ejemplo n.º 1
0
class DOBackup:
	def __init__(self, api_key, client_id): 
		logger.debug("Initializing DOBackup object")
		self.client = Client(client_id, api_key)
		self.droplets = self.client.show_active_droplets()

	def shutdown(self, droplet):
		status = self.client.show_droplet(droplet.id) 
		if(status.status != "off"):
			self.client.shutdown_droplet(droplet.id) 

		i = 1
		while(status.status != "off"):
			logger.info("Waiting for droplet " + droplet.name + " to shutdown...")
			time.sleep(10)	
			status = self.client.show_droplet(droplet.id) 
			if(i > 15):
				return False
			i+=1
		return True

	def backup(self, droplet):
		if(self.shutdown(droplet) == False):
				sys.exit("Timeout while waiting for droplet " + droplet.name + " to shutdown")
		snapshot_name = droplet.name + "_" + datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ') 
		logger.info("Asking to create snapshot " + snapshot_name)
		self.client.snapshot_droplet(droplet.id, snapshot_name) 	

	def backup_all(self):
		for droplet in self.droplets:
			self.backup(droplet)
Ejemplo n.º 2
0
def up(count, client_key, api_key, login, size=66, region_id=1, ssh_keys=None):
    """
    Startup the load testing server.
    """
    count = int(count)
    client = Client(client_key, api_key)
    print 'Attempting to call up %i minnows.' % count

    if not ssh_keys:
        ssh_keys = [str(k.id) for k in client.all_ssh_keys()]

    droplet_ids = []
    droplets = []

    #25489 is ubuntu 12.04
    for i in range(0, count):
        droplet = client.create_droplet('minnow-%s' % i, size, 25489,
                                        region_id, ssh_keys)
        droplet_ids.append(droplet.id)

    print "Started droplets with ids: %s" % ','.join(
        [str(i) for i in droplet_ids])
    print "Waiting for minnows to wake up..."

    for drop_id in droplet_ids:
        droplet = client.show_droplet(drop_id)
        while (not droplet.status == 'active') or droplet.ip_address == -1:
            print '.'
            time.sleep(4)
            droplet = client.show_droplet(drop_id)
        droplets.append(droplet)
        print "Droplet id %s is ready" % drop_id

    print "The school of minnows has been assembled."
    _write_server_list(client_key, api_key, login, droplets)
    print "Arming the minnows with Apache Bench..."
    #TODO: Can't ssh into the servers for a bit...is there a better way to do this rather than
    #sleeping for an arbitrary amount of time?
    time.sleep(20)

    params = []
    for droplet in droplets:
        params.append({
            'droplet_id': droplet.id,
            'ip_address': droplet.ip_address,
            'login': login,
        })
    # Spin up processes for connecting to EC2 instances
    pool = Pool(len(params))
    pool.map(_install_apache_utils, params)
    return
Ejemplo n.º 3
0
def up(count, client_key, api_key, login, size = 66, region_id = 1, ssh_keys = None):
    """
    Startup the load testing server.
    """
    count = int(count)
    client = Client(client_key, api_key)
    print 'Attempting to call up %i minnows.' % count

    if not ssh_keys:
        ssh_keys = [str(k.id) for k in client.all_ssh_keys()]

    droplet_ids = []
    droplets = []

    #25489 is ubuntu 12.04 
    for i in range(0, count):
        droplet = client.create_droplet('minnow-%s' % i, size, 25489, region_id, ssh_keys)
        droplet_ids.append(droplet.id)

    print "Started droplets with ids: %s" % ','.join([str(i) for i in droplet_ids])
    print "Waiting for minnows to wake up..."

    for drop_id in droplet_ids:
        droplet = client.show_droplet(drop_id)
        while (not droplet.status == 'active') or droplet.ip_address == -1:
            print '.'
            time.sleep(4)
            droplet = client.show_droplet(drop_id)
        droplets.append(droplet)
        print "Droplet id %s is ready" % drop_id

    print "The school of minnows has been assembled."
    _write_server_list(client_key, api_key, login, droplets)
    print "Arming the minnows with Apache Bench..."
    #TODO: Can't ssh into the servers for a bit...is there a better way to do this rather than
    #sleeping for an arbitrary amount of time?
    time.sleep(20)

    
    params = []
    for droplet in droplets:
        params.append({
            'droplet_id': droplet.id,
            'ip_address': droplet.ip_address,
            'login': login,
        })
    # Spin up processes for connecting to EC2 instances
    pool = Pool(len(params))
    pool.map(_install_apache_utils, params)
    return
Ejemplo n.º 4
0
def status():
    """
    Report the status of the load testing servers.
    """
    client_key, api_key, login, droplet_ids = _read_server_list()
    client = Client(client_key, api_key)

    if len(droplet_ids) == 0:
        print 'No minnows have been mobilized.'
        return
    print "Getting status of minnows"
    for drop_id in droplet_ids:
        droplet = client.show_droplet(drop_id)
        print 'minnow %s: %s @ %s' % (droplet.id, droplet.status, droplet.ip_address)
Ejemplo n.º 5
0
def status():
    """
    Report the status of the load testing servers.
    """
    client_key, api_key, login, droplet_ids = _read_server_list()
    client = Client(client_key, api_key)

    if len(droplet_ids) == 0:
        print 'No minnows have been mobilized.'
        return
    print "Getting status of minnows"
    for drop_id in droplet_ids:
        droplet = client.show_droplet(drop_id)
        print 'minnow %s: %s @ %s' % (droplet.id, droplet.status,
                                      droplet.ip_address)
Ejemplo n.º 6
0
from dop.client import Client
import time
import sys
import yaml

if __name__ == '__main__':
  yaml = yaml.load(open('ansible-provision/secret_vars.yml','r'))
  client = Client(yaml['doid'], yaml['dokey'])
  #create a droplet
  conf = {'name': sys.argv[1], 
      'size_id': 66,
      'image_id': 3101918,
      'region_id': 3,
      'ssh_key_ids': ['20319']}
  droplet = client.create_droplet(**conf)
  is_active = False
  while not is_active:
    time.sleep(2)
    status = client.get_status(droplet.event_id)
    if status.percentage == '100':
      is_active = True
      print "Droplet Created."
      print client.show_droplet(droplet.id).__dict__
Ejemplo n.º 7
0
class DigitalOceanProvider(Provider):

    connection = None

    def __init__(self, client_id=None, api_key=None, **kwargs):
        """
        initializes connection object
        """
        self.connection = Client(client_id, api_key)
        assert self.connection is not None

    def status(self):
        instances = []
        droplets = self.connection.droplets()
        for droplet in droplets:
            instances.append(self.info(droplet))
        return instances

    def create(self, name=None, **kwargs):
        """
        return: aws instance object
        instance is booting so don't forget to cotton.fabextras.wait_for_shell()
        """
        zone_config = get_provider_zone_config()

        result = self.filter(name=name)
        if result:
            abort(red("VM name already in use"))


        size = {}
        fields = ['size_id', 'size_slug']
        for field in fields:
            if field in zone_config:
                size[field] = zone_config[field]

        image = {}
        fields = ['image_id', 'image_slug']
        for field in fields:
            if field in zone_config:
                image[field] = zone_config[field]

        region = {}
        fields = ['region_id', 'region_slug']
        for field in fields:
            if field in zone_config:
                region[field] = zone_config[field]

        droplet = self.connection.create_droplet(
            name=name,
            size=size,
            image=image,
            region=region,
            backups_enabled=zone_config['backups_enabled'],
            private_networking=zone_config['private_networking'],
            ssh_key_ids=map(lambda x: str(x), zone_config['ssh_key_ids'])
        )

        print("Waiting for instance to run",)
        while droplet.ip_address is None or droplet.status != 'active':
            sys.stdout.write(".")
            sys.stdout.flush()
            time.sleep(1)
            droplet = self.connection.show_droplet(droplet.droplet_id)
        print(" OK")

        return droplet

    def terminate(self, server):
        assert isinstance(server, Droplet)
        pprint.pprint(self.info(server))

        if env.force:
            sure = 'T'
        else:
            sure = prompt(red("Type 'T' to confirm termination"), default='N')

        if sure == 'T':
            self.connection.rename_droplet(server.droplet_id, '{}-terminating'.format(server.name))
#            time.sleep(1) #TODO: wait for droplet to be available
            while True:
                try:
                    self.connection.destroy_droplet(server.droplet_id)
                    break
                except:
                    sys.stdout.write(".")
                    sys.stdout.flush()
                    time.sleep(1)
                    continue
            print("Terminated")
        else:
            print("Aborting termination")

    def filter(self, **kwargs):
        """
        return: list of objects matching filter args
        typically provide should support filter 'name'='foo'
        """
        instances = []

        if 'name' in kwargs:
            name = kwargs['name']
            for droplet in self.connection.droplets():
                if droplet.name == name:
                    instances.append(droplet)
                    print("selected digital ocean droplet: {}".format(droplet.droplet_id))
        else:
            raise NotImplementedError()

        if not instances:
            print(yellow("Warning: {} not found!".format(name), bold=True))

        return instances

    def info(self, server):
        """
        returns dictionary with info about server
        """
        return server.to_json()

    def host_string(self, server):
        #TODO: where to select user/provisioning mode
        return self.info(server)["ip_address"]
Ejemplo n.º 8
0
class DigitalOceanWrapper(object):
    REBUILD_EXISTING = False

    def __init__(self, client_id, api_key, name):
        from dop.client import Client
        self.name = name
        self.client = Client(client_id, api_key)

    def get_or_create_droplet(self, name):
        '''
        Find droplet by name or create new one.
        '''
        droplet = self.find_droplet(name)
        if droplet is not None and self.REBUILD_EXISTING:
            self.client.rebuild_droplet(droplet.id, droplet.image_id)
        if droplet is None:
            droplet = self.create_droplet(name)
        droplet_id = droplet.id
        while droplet.status in ['', 'new',]:
            print 'waiting...', droplet.to_json()
            time.sleep(5)
            droplet = self.client.show_droplet(droplet_id)
        return droplet

    def find_droplet(self, name):
        '''
        Find existing droplet by name.
        '''
        for droplet in self.client.show_active_droplets():
            if droplet.name == name:
                return droplet

    def create_droplet(self, name):
        '''
        Create new droplet with minimal disk and memory.
        '''
        print 'Creating new droplet...'
        size_id = [size.id for size in self.client.sizes()
                if size.name == u'512MB'][0]
        image_id = [image.id for image in self.client.images()
                if image.name == u'Ubuntu 12.04 x64'][0]
        region_id = [region.id for region in self.client.regions() if
                region.name == u'San Francisco 1'][0]
        ssh_keys = [str(key.id) for key in self.client.all_ssh_keys()]
        print size_id, image_id, region_id, ssh_keys
        droplet = self.client.create_droplet(name, size_id=size_id,
                image_id=image_id, region_id=region_id, ssh_key_ids=ssh_keys)
        return droplet

    def setup(self):
        '''
        Prepare droplet for deployment.
        '''
        import fabtools
        from fabtools import require
        droplet = self.get_or_create_droplet(self.name)
        print droplet.to_json()
        ip_address = droplet.ip_address
        with settings(host_string='root@{}'.format(ip_address)):
            run('uname -a')
            require.user('volkhin')
            require.sudoer('volkhin')
Ejemplo n.º 9
0
class DigitalOceanWrapper(object):
    REBUILD_EXISTING = False

    def __init__(self, client_id, api_key, name):
        from dop.client import Client
        self.name = name
        self.client = Client(client_id, api_key)

    def get_or_create_droplet(self, name):
        '''
        Find droplet by name or create new one.
        '''
        droplet = self.find_droplet(name)
        if droplet is not None and self.REBUILD_EXISTING:
            self.client.rebuild_droplet(droplet.id, droplet.image_id)
        if droplet is None:
            droplet = self.create_droplet(name)
        droplet_id = droplet.id
        while droplet.status in [
                '',
                'new',
        ]:
            print 'waiting...', droplet.to_json()
            time.sleep(5)
            droplet = self.client.show_droplet(droplet_id)
        return droplet

    def find_droplet(self, name):
        '''
        Find existing droplet by name.
        '''
        for droplet in self.client.show_active_droplets():
            if droplet.name == name:
                return droplet

    def create_droplet(self, name):
        '''
        Create new droplet with minimal disk and memory.
        '''
        print 'Creating new droplet...'
        size_id = [
            size.id for size in self.client.sizes() if size.name == u'512MB'
        ][0]
        image_id = [
            image.id for image in self.client.images()
            if image.name == u'Ubuntu 12.04 x64'
        ][0]
        region_id = [
            region.id for region in self.client.regions()
            if region.name == u'San Francisco 1'
        ][0]
        ssh_keys = [str(key.id) for key in self.client.all_ssh_keys()]
        print size_id, image_id, region_id, ssh_keys
        droplet = self.client.create_droplet(name,
                                             size_id=size_id,
                                             image_id=image_id,
                                             region_id=region_id,
                                             ssh_key_ids=ssh_keys)
        return droplet

    def setup(self):
        '''
        Prepare droplet for deployment.
        '''
        import fabtools
        from fabtools import require
        droplet = self.get_or_create_droplet(self.name)
        print droplet.to_json()
        ip_address = droplet.ip_address
        with settings(host_string='root@{}'.format(ip_address)):
            run('uname -a')
            require.user('volkhin')
            require.sudoer('volkhin')
Ejemplo n.º 10
0
from dop.client import Client
import time
import sys
import yaml

if __name__ == '__main__':
    yaml = yaml.load(open('ansible-provision/secret_vars.yml', 'r'))
    client = Client(yaml['doid'], yaml['dokey'])
    #create a droplet
    conf = {
        'name': sys.argv[1],
        'size_id': 66,
        'image_id': 3101918,
        'region_id': 3,
        'ssh_key_ids': ['20319']
    }
    droplet = client.create_droplet(**conf)
    is_active = False
    while not is_active:
        time.sleep(2)
        status = client.get_status(droplet.event_id)
        if status.percentage == '100':
            is_active = True
            print "Droplet Created."
            print client.show_droplet(droplet.id).__dict__