def start_cluster(): """ Start all nodes of the cluster """ conn = dodo.connect() # create master droplet_options = { 'name': do_master_droplet, 'size_id': do_size_id, 'image_id': do_image_id, 'region_id': do_region_id, 'ssh_key_ids': do_ssh_key_ids } conn.new_droplet(**droplet_options) # create nodes for i in range(cluster_size): droplet_options = { 'name': do_slave_prefix + str(i + 1), 'size_id': do_size_id, 'image_id': do_image_id, 'region_id': do_region_id, 'ssh_key_ids': do_ssh_key_ids } conn = dodo.connect() conn.new_droplet(**droplet_options) update_hosts()
def setUp(self): self.host = 'http://test.example.com' self.client_id = 'fake_client' self.api_key = 'fake_key' self.headers = {'User-Agent': dodo.USER_AGENT} self.params = {'api_key': self.api_key, 'client_id': self.client_id} self.conn = dodo.connect(self.host, self.client_id, self.api_key)
def _do_droplet_id(name): conn = dodo.connect() droplets = conn.droplets() for droplet in droplets: if droplet['name'] == name: return droplet['id'] return None
def droplet(droplet_id=None): # get droplet_id if not passed in if droplet_id is None: ds = droplet_ids() if len(ds) > 0: droplet_id = ds[0]['id'] else: return None conn = dodo.connect() droplet = conn.show_droplet(droplet_id) return droplet
def ssh_key_id(): """ Get the ID of the ssh_key up on DigitalOcean """ conn = dodo.connect() keys = [s for s in conn.ssh_keys() if s['name'] == ssh_key_name] if len(keys) > 0: ssh_key = conn.ssh_key(keys[0]['id']) if ssh_key and ssh_key['ssh_pub_key'] == public_key(): return ssh_key['id'] return None
def update_hosts(): """ Update local /private/hosts file with hosts and ip addresses of nodes """ conn = dodo.connect() droplets = conn.droplets() # empty out the current hosts file with open('private/hosts', 'w') as h: pass for droplet in droplets: # is it master or a slave node? if droplet['name']== do_master_droplet or _is_slave_name(droplet['name']): with open('private/hosts', 'a') as h: h.write('\t'.join([droplet['name'], droplet['ip_address']]) + '\n')
def destroy_cluster(): """ Destroy all nodes """ with open('private/hosts') as h: l = h.readline() while l: (name, ip) = l.strip().split('\t') if name == do_master_droplet or _is_slave_name(name): droplet_id = _do_droplet_id(name) print 'destroying droplet:', name, droplet_id conn = dodo.connect() conn.destroy_droplet(droplet_id=droplet_id) l = h.readline() update_hosts()
def droplet_ids(): """ return a list of droplets named <droplet_name> """ conn = dodo.connect() return [d for d in conn.droplets() if d['name'] == droplet_name]
def destroy_droplet(): d = droplet() if d: conn = dodo.connect() dstart = conn.destroy_droplet(d['id'])
def create_droplet(size=66, ssh_key=None, region=1, image=25306): conn = dodo.connect() dstart = conn.new_droplet(droplet_name, size, image, region, ssh_key) return droplet(dstart['id'])
def backup_droplet(): d = droplet() conn = dodo.connect() res = conn.snapshot_droplet(d['id'])