Ejemplo n.º 1
0
class DigitalOcean():
    def __init__(self, client_id, api_key):
        self.client_id = client_id
        self.api_key = api_key
        self.client = Client(self.client_id, self.api_key)

        # This list in outdated, fixme
        self.regions = {'ny1': 1, 'ams1': 2, 'sf1': 3, 'ny2': 4}

    def droplet_get(self, name):
        for droplet in self.client.show_active_droplets():
            if droplet.to_json()['name'] == name:
                return droplet.to_json()['id']
        return None

    def droplet_create(self, name, region):
        if self.droplet_get(name) is not None:
            return
        droplet = self.client.create_droplet(name, 66, 308287,
                                        self.regions[region])
        return droplet

    def droplet_destroy(self, name):
        if self.droplet_get(name) is None:
            return 0
        self.client.destroy_droplet(self.droplet_get(name))

    def droplet_list(self):
        droplets = self.client.show_active_droplets()
        for droplet in droplets:
            json = droplet.to_json()
            print json['name'] + " ",
            print json['status'] + " ",
            print json['ip_address']
 def __init__(self):
     self.client = Client('PUPdwX4Dwbl9Xc3lYrhEp',
                          'vYe2LKXyP1Dfa3lKX6ASg2vEhd6xJFt0HXpfE500n')
     self.images = []
     self.sizes = []
     self.regions = []
     self.refresh = True
Ejemplo n.º 3
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.º 4
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.º 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
    def __init__(self, client_id, api_key):
        self.client_id = client_id
        self.api_key = api_key
        self.client = Client(self.client_id, self.api_key)

        # This list in outdated, fixme
        self.regions = {'ny1': 1, 'ams1': 2, 'sf1': 3, 'ny2': 4}
Ejemplo n.º 7
0
def initialize(client_id = 'ce302478afc77902c6fb05ba4ca5683e',\
               api_key = 'e4a865a093fe5c2f704c194e91135b74'):
    client_id = client_id
    api_key = api_key

    client = Client(client_id, api_key)
    # if client != None:
    return client
Ejemplo n.º 8
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.º 9
0
def down():
    """
    Shutdown the load testing server.
    """
    client_key, api_key, login, droplet_ids = _read_server_list()

    if not droplet_ids:
        print 'No minnows have been mobilized.'
        return

    print 'Connecting to the ocean.'
    client = Client(client_key, api_key)

    for droplet_id in droplet_ids:
        res = client.destroy_droplet(droplet_id)
        print "Destroyed %s" % res

    _delete_server_list()
Ejemplo n.º 10
0
def down():
    """
    Shutdown the load testing server.
    """
    client_key, api_key, login, droplet_ids = _read_server_list()

    if not droplet_ids:
        print 'No minnows have been mobilized.'
        return

    print 'Connecting to the ocean.'
    client = Client(client_key, api_key)

    for droplet_id in droplet_ids:
        res = client.destroy_droplet(droplet_id)
        print "Destroyed %s" % res

    _delete_server_list()
Ejemplo n.º 11
0
def spawn(client, num_instances=5):
    client_id = 'ce302478afc77902c6fb05ba4ca5683e'
    api_key = 'e4a865a093fe5c2f704c194e91135b74'

    client = Client(client_id, api_key)
    print "HERE"
    images = client.images()
    sizes = client.sizes()
    print 'before create'
    for x in client.show_active_droplets():
        print x.id
    print 'after create'
    job_id = 'job'
    for x in range(num_instances):
        drop_name = job_id + '-' + str(x)
        client.create_droplet(name=drop_name, image_id=2393479, size_id=66)
Ejemplo n.º 12
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.º 13
0
def spawn(client, num_instances = 5):
    client_id = 'ce302478afc77902c6fb05ba4ca5683e'
    api_key = 'e4a865a093fe5c2f704c194e91135b74'


    client = Client(client_id, api_key)
    print "HERE"
    images = client.images()
    sizes = client.sizes()
    print 'before create'
    for x in client.show_active_droplets():
        print x.id
    print 'after create'
    job_id = 'job'
    for x in range(num_instances):
        drop_name = job_id + '-' + str(x)
        client.create_droplet(name=drop_name, image_id = 2393479, size_id=66)
Ejemplo n.º 14
0
def attack(url, n, c, **options):
    """
    Test the root url of this site.
    """
    client_key, api_key, login, droplet_ids = _read_server_list()
    client = Client(client_key, api_key)

    if not login or not client_key or not api_key:
        print "You need to create the minnows first with the up command"
        return
    if not droplet_ids or len(droplet_ids) == 0:
        print 'No minnows are ready to attack.'
        return

    headers = options.get('headers', '')
    csv_filename = options.get("csv_filename", '')

    if csv_filename:
        try:
            stream = open(csv_filename, 'w')
        except IOError, e:
            raise IOError(
                "Specified csv_filename='%s' is not writable. Check permissions or specify a different filename and try again."
                % csv_filename)
Ejemplo n.º 15
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.º 16
0
 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
Ejemplo n.º 17
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.º 18
0
 def __init__(self):
     self.client = Client('PUPdwX4Dwbl9Xc3lYrhEp', 'vYe2LKXyP1Dfa3lKX6ASg2vEhd6xJFt0HXpfE500n')
     self.images = []
     self.sizes = []
     self.regions = []
     self.refresh = True
Ejemplo n.º 19
0
 def droplet_ip(self):
     client = Client(self.CLIENT_ID, self.API_KEY)
     droplets = client.show_active_droplets()
     return [droplet.to_json() for droplet in droplets][-1]['ip_address']
Ejemplo n.º 20
0
    for x in client.images():
        print x.to_json()


def initialize(client_id = 'ce302478afc77902c6fb05ba4ca5683e',\
               api_key = 'e4a865a093fe5c2f704c194e91135b74'):
    client_id = client_id
    api_key = api_key

    client = Client(client_id, api_key)
    # if client != None:
    return client


if __name__ == "__main__":
    client_id = 'ce302478afc77902c6fb05ba4ca5683e'
    api_key = 'e4a865a093fe5c2f704c194e91135b74'

    client = Client(client_id, api_key)

    # destroy_all_droplets(client)
    #main()
    # for x in client.show_active_droplets():
    #     print x.name
    # print x.id
    # print_sizes(client)
    spawn(client, 4)
    # print_images()
    # print_sizes()
    # print_regions()
Ejemplo n.º 21
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.º 22
0
	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()
Ejemplo n.º 23
0
def show_sizes(client_key, api_key):
    print "Fetching Sizes..."
    client = Client(client_key, api_key)
    sizes = client.sizes()
    for s in sizes:
        print "Size id: %s has %s of memory" % (s.id, s.name[:-1])
Ejemplo n.º 24
0
 def __init__(self, client_id, api_key, name):
     from dop.client import Client
     self.name = name
     self.client = Client(client_id, api_key)
Ejemplo n.º 25
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.º 26
0
            dict1[option] = Config.get(section, option)
            if dict1[option] == -1:
                DebugPrint("skip: %s" % option)
        except:
            print("exception on %s!" % option)
            dict1[option] = None
    return dict1


clientID = configSectionMap("Access")['clientid']
apiKey = configSectionMap("Access")['apikey']

# print "Client ID: %s, API Key: %s" % (clientID, apiKey)
print "Client ID and API key retrieved."

client = Client(clientID, apiKey)
droplets = client.show_active_droplets()


def printOptions():
    print ""
    print "Options:"
    print "1) List Active Droplets"
    print "2) List Available Sizes"
    print "3) List Available Images"
    print "4) List Available Regions"
    print "5) Create Drop"
    print "6) Destroy Drop"
    print "7) Install HAProxy"
    print "8) Deploy code"
    print "10) Create Downpour"
Ejemplo n.º 27
0
 def create_droplet(self, name):
     """ step 1: creating droplet """
     client = Client(self.CLIENT_ID, self.API_KEY)
     droplet = client.create_droplet(name, 66, 1505699, 1)
     print droplet.to_json()['id']
Ejemplo n.º 28
0
def show_regions(client_key, api_key):
    print "Fetching Regions..."
    client = Client(client_key, api_key)
    regions = client.regions()
    for r in regions:
        print "Region id: %s is located in %s" % (r.id, r.name[:-1])
Ejemplo n.º 29
0
class DigitalOcean():

    def __init__(self):
        self.client = Client('PUPdwX4Dwbl9Xc3lYrhEp', 'vYe2LKXyP1Dfa3lKX6ASg2vEhd6xJFt0HXpfE500n')
        self.images = []
        self.sizes = []
        self.regions = []
        self.refresh = True

    def getRegions(self):
        if self.refresh or self.regions == []:
            self.regions = []
            self.regions = [Region(item, self.client) for item in self.client.regions()]
        return self.regions

    def getDroplets(self, prefix="", refresh=False):
        if refresh or self.refresh or self.droplets == []:
            self.droplets = []
            droplets = self.client.show_active_droplets()
            for item in droplets:
                self.droplets.append(Droplet(item, self.client))

        if prefix != "":
            result = []
            for item in self.droplets:
                nname = str(item.name).strip()
                if nname.find(prefix) == 0:
                    result.append(item)
            return result
        else:
            return self.droplets

    def getImages(self):
        if self.refresh or self.images == []:
            self.images = []
            self.images = [Image(item, self.client) for item in self.client.images()]
            self.images += [Image(item, self.client) for item in self.client.images(show_all=False)]
        return self.images

    def getSizes(self):
        if self.refresh or self.sizes == []:
            self.sizes = [Size(item, self.client) for item in self.client.sizes()]
        return self.sizes

    def getALL(self):
        self.getSizes()
        self.getImages()
        self.getDroplets()
        self.getRegions()

    def getImageFromName(self, name):
        images = self.getImages()
        for item in images:
            if item.name.find(name) != -1:
                return item
        return None

    def getImage(self, name):
        items = self.getImages()
        for item in items:
            if item.name.find(name) != -1:
                return item
        return None

    def getDroplet(self, name, refresh=False):
        items = self.getDroplets(refresh=refresh)
        for item in items:
            if item.name.find(name) == 0:
                return item
        return None

    def deleteDroplets(self, prefix):
        self.haltDroplets(prefix)
        for droplet in self.getDroplets(prefix):
            droplet.waitOff()
        for droplet in self.getDroplets(prefix):
            droplet.delete()
        if self.getDroplets(prefix) == []:
            return
        print "wait till all deleted"
        time.sleep(5)
        start = j.base.time.getTimeEpoch()
        now = start
        timeout = 60
        while now < start + timeout:
            droplets = self.getDroplets(prefix, refresh=True)
            if droplets == []:
                return
            time.sleep(1)
            print "wait till all deleted"
            now = j.base.time.getTimeEpoch()
        j.errorconditionhandler.raiseOperationalCritical(msgpub="Could not delete droplets with prefix %s" % self.prefix)

    def haltDroplets(self, prefix):
        for droplet in self.getDroplets(prefix):
            droplet.halt()

    def startDroplets(self, prefix):
        for droplet in self.getDroplets(prefix):
            droplet.start()

    def save(self, name="main"):
        items = {}
        items["images"] = self.images
        items["sizes"] = self.sizes
        items["regions"] = self.regions
        items["refresh"] = self.refresh

        txt = j.db.serializers.ujson.dumps(items)
        j.system.fs.writeFile("%s.json" % name, txt)

    def load(self, name="main"):
        txt = j.system.fs.fileGetContents("%s.json" % name)
        self.__dict__.update(j.db.serializers.ujson.loads(txt))

    def getMainSSHKey(self):
        key = self.client.all_ssh_keys()[0]
        return key.id

    def createMachine(self, name, size=66, image=350076, region=2, sshkey=0):
        """
        regions : 2=amsterdam
        """
        self.client.create_droplet(name=name, size_id=size, image_id=image, region_id=region, ssh_key_ids=sshkey, virtio=True)

    def changeRootPasswd(self, prefix, newPasswd):
        droplets = self.getDroplets(prefix)
        for droplet in droplets:
            droplet.changeRootPasswd(newPasswd)

    def createMachines(self, nr, prefix, size=66, image=350076, region=2, sshkey=0, rootPasswd=""):
        print "create machines with prefix: %s" % prefix
        names = []
        for i in range(nr):
            name = "%s-%s" % (prefix, i)
            names.append(name)
            self.createMachine(name, size, image, region, sshkey)

        print "wait till all created"
        start = j.base.time.getTimeEpoch()
        now = start
        timeout = 600
        time.sleep(40)
        while now < start + timeout:
            self.getDroplets(refresh=True)  # makes sure we read all machines again
            for name in names:
                dr = self.getDroplet(name)
                if dr.checkStatus() == "active":
                    names.remove(name)
            if names == []:
                break
            time.sleep(10)
            print "wait"
            now = j.base.time.getTimeEpoch()

        if name != []:
            j.errorconditionhandler.raiseOperationalCritical(msgpub="Could not create droplets %s" % names, category="digitalocean.create")

        self.droplets = []

        if rootPasswd != "":
            self.changeRootPasswd(prefix, rootPasswd)
Ejemplo n.º 30
0
 def __init__(self, client_id, api_key, name):
     from dop.client import Client
     self.name = name
     self.client = Client(client_id, api_key)
Ejemplo n.º 31
0
class DigitalOcean():
    def __init__(self):
        self.client = Client('PUPdwX4Dwbl9Xc3lYrhEp',
                             'vYe2LKXyP1Dfa3lKX6ASg2vEhd6xJFt0HXpfE500n')
        self.images = []
        self.sizes = []
        self.regions = []
        self.refresh = True

    def getRegions(self):
        if self.refresh or self.regions == []:
            self.regions = []
            self.regions = [
                Region(item, self.client) for item in self.client.regions()
            ]
        return self.regions

    def getDroplets(self, prefix="", refresh=False):
        if refresh or self.refresh or self.droplets == []:
            self.droplets = []
            droplets = self.client.show_active_droplets()
            for item in droplets:
                self.droplets.append(Droplet(item, self.client))

        if prefix != "":
            result = []
            for item in self.droplets:
                nname = str(item.name).strip()
                if nname.find(prefix) == 0:
                    result.append(item)
            return result
        else:
            return self.droplets

    def getImages(self):
        if self.refresh or self.images == []:
            self.images = []
            self.images = [
                Image(item, self.client) for item in self.client.images()
            ]
            self.images += [
                Image(item, self.client)
                for item in self.client.images(show_all=False)
            ]
        return self.images

    def getSizes(self):
        if self.refresh or self.sizes == []:
            self.sizes = [
                Size(item, self.client) for item in self.client.sizes()
            ]
        return self.sizes

    def getALL(self):
        self.getSizes()
        self.getImages()
        self.getDroplets()
        self.getRegions()

    def getImageFromName(self, name):
        images = self.getImages()
        for item in images:
            if item.name.find(name) != -1:
                return item
        return None

    def getImage(self, name):
        items = self.getImages()
        for item in items:
            if item.name.find(name) != -1:
                return item
        return None

    def getDroplet(self, name, refresh=False):
        items = self.getDroplets(refresh=refresh)
        for item in items:
            if item.name.find(name) == 0:
                return item
        return None

    def deleteDroplets(self, prefix):
        self.haltDroplets(prefix)
        for droplet in self.getDroplets(prefix):
            droplet.waitOff()
        for droplet in self.getDroplets(prefix):
            droplet.delete()
        if self.getDroplets(prefix) == []:
            return
        print "wait till all deleted"
        time.sleep(5)
        start = j.base.time.getTimeEpoch()
        now = start
        timeout = 60
        while now < start + timeout:
            droplets = self.getDroplets(prefix, refresh=True)
            if droplets == []:
                return
            time.sleep(1)
            print "wait till all deleted"
            now = j.base.time.getTimeEpoch()
        j.errorconditionhandler.raiseOperationalCritical(
            msgpub="Could not delete droplets with prefix %s" % self.prefix)

    def haltDroplets(self, prefix):
        for droplet in self.getDroplets(prefix):
            droplet.halt()

    def startDroplets(self, prefix):
        for droplet in self.getDroplets(prefix):
            droplet.start()

    def save(self, name="main"):
        items = {}
        items["images"] = self.images
        items["sizes"] = self.sizes
        items["regions"] = self.regions
        items["refresh"] = self.refresh

        txt = j.db.serializers.ujson.dumps(items)
        j.system.fs.writeFile("%s.json" % name, txt)

    def load(self, name="main"):
        txt = j.system.fs.fileGetContents("%s.json" % name)
        self.__dict__.update(j.db.serializers.ujson.loads(txt))

    def getMainSSHKey(self):
        key = self.client.all_ssh_keys()[0]
        return key.id

    def createMachine(self, name, size=66, image=350076, region=2, sshkey=0):
        """
        regions : 2=amsterdam
        """
        self.client.create_droplet(name=name,
                                   size_id=size,
                                   image_id=image,
                                   region_id=region,
                                   ssh_key_ids=sshkey,
                                   virtio=True)

    def changeRootPasswd(self, prefix, newPasswd):
        droplets = self.getDroplets(prefix)
        for droplet in droplets:
            droplet.changeRootPasswd(newPasswd)

    def createMachines(self,
                       nr,
                       prefix,
                       size=66,
                       image=350076,
                       region=2,
                       sshkey=0,
                       rootPasswd=""):
        print "create machines with prefix: %s" % prefix
        names = []
        for i in range(nr):
            name = "%s-%s" % (prefix, i)
            names.append(name)
            self.createMachine(name, size, image, region, sshkey)

        print "wait till all created"
        start = j.base.time.getTimeEpoch()
        now = start
        timeout = 600
        time.sleep(40)
        while now < start + timeout:
            self.getDroplets(
                refresh=True)  # makes sure we read all machines again
            for name in names:
                dr = self.getDroplet(name)
                if dr.checkStatus() == "active":
                    names.remove(name)
            if names == []:
                break
            time.sleep(10)
            print "wait"
            now = j.base.time.getTimeEpoch()

        if name != []:
            j.errorconditionhandler.raiseOperationalCritical(
                msgpub="Could not create droplets %s" % names,
                category="digitalocean.create")

        self.droplets = []

        if rootPasswd != "":
            self.changeRootPasswd(prefix, rootPasswd)
Ejemplo n.º 32
0
def show_regions(client_key, api_key):
    print "Fetching Regions..."
    client = Client(client_key, api_key)
    regions = client.regions()
    for r in regions:
        print "Region id: %s is located in %s" % (r.id, r.name[:-1])
Ejemplo n.º 33
0
def show_sizes(client_key, api_key):
    print "Fetching Sizes..."
    client = Client(client_key, api_key)
    sizes = client.sizes()
    for s in sizes:
        print "Size id: %s has %s of memory" % (s.id, s.name[:-1])
Ejemplo n.º 34
0
        try:
            dict1[option] = Config.get(section, option)
            if dict1[option] == -1:
                DebugPrint("skip: %s" % option)
        except:
            print("exception on %s!" % option)
            dict1[option] = None
    return dict1

clientID = configSectionMap("Access")['clientid']
apiKey = configSectionMap("Access")['apikey']

# print "Client ID: %s, API Key: %s" % (clientID, apiKey)
print "Client ID and API key retrieved."

client = Client(clientID, apiKey)
droplets = client.show_active_droplets()

def printOptions():
	print ""
        print "Options:"
        print "1) List Active Droplets"
	print "2) List Available Sizes"
	print "3) List Available Images"
	print "4) List Available Regions"
	print "5) Create Drop"
	print "6) Destroy Drop"
	print "7) Install HAProxy"
	print "8) Deploy code"
	print "10) Create Downpour"
	print "11) Display Downpour"
Ejemplo n.º 35
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.º 36
0
digital ocean server setup and installing ipython notebook, and run website on web server. 
choose os you want, choose images you want. launch. 

# <markdowncell>

# user logs into digital ocean, chooses one of my snapshots (need to make snapshots of stuff i like.
# Makes server, runs script to install software. tick box on software you want. 
# 

# <codecell>

from dop.client import Client

# <codecell>

client = Client()

# <codecell>

ls

# <codecell>

%%bash
ipython nbconvert 'digzocean.ipynb'

# <codecell>

cat digzocean.html

# <codecell>