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']
Ejemplo n.º 2
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.º 3
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.º 4
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)
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.º 6
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.º 7
0
            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.º 8
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"
	print "11) Display Downpour"
	print "12) Destroy Downpour"
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.º 10
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.º 11
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')