Beispiel #1
0
def create_droplets():
    """
    Create three new DigitalOcean droplets -
    node-1, node-2, node-3
    """
    manager = Manager(token=DIGITAL_OCEAN_ACCESS_TOKEN)
    # Get ALL SSH keys
    keys = manager.get_all_sshkeys()
    # Get single SSH key
    # all_keys = manager.get_all_sshkeys()
    # keys = []
    # for key in all_keys:
    #     if key.name == '<ADD_YOUR_KEY_NAME_HERE>':
    #         keys.append(key)
    for num in range(3):
        node = f'node-{num + 1}'
        droplet = Droplet(
            token=DIGITAL_OCEAN_ACCESS_TOKEN,
            name=node,
            region='nyc3',
            image='ubuntu-16-04-x64',
            size_slug='4gb',
            tags=[node],
            ssh_keys=keys,
        )
        droplet.create()
        print(f'{node} has been created.')
    def launch(self,
               name='kaggle-droplet',
               region='nyc2',
               image='ubuntu-16-04-x64',
               size='512mb',
               user=None,
               password=None,
               authorized_key='id_rsa',
               hdf=False):
        """Launch DigitalOcean droplet instance"""

        user_data = self.update_config_script(user=user,
                                              password=password,
                                              authorized_key=authorized_key,
                                              hdf=hdf)

        droplet = Droplet(token=self.api_token,
                          name=name,
                          region=region,
                          image=image,
                          size_slug=size,
                          backups=False,
                          ssh_keys=self.ssh_id,
                          user_data=user_data)
        droplet.create()

        self.droplet['id'] = droplet.id
        while not self.droplet.get('ip_address', None):
            self.droplet['ip_address'] = droplet.load().ip_address
        self.update_ssh()
        pprint.pprint(('Name: {}'.format(droplet.name), 'Image: {}'.format(
            droplet.image.get('slug')), 'Memory: {}'.format(droplet.size_slug),
                       'Disk Size: {}'.format(droplet.disk),
                       'IP Address: {}'.format(droplet.ip_address)))
Beispiel #3
0
        def Create(digo,
                   name: str,
                   tag: str,
                   open_ssh_key: str,
                   image='docker-18-04',
                   price=0,
                   user_data='echo hello world',
                   autoBackups=False,
                   useMonitoring=False,
                   ipv6=False):
            """Creates a Droplet.
			Args:
				digo (DigitalOcean): Instantiated DigitalOcean credential object.
				name (str): DNS valid name.
				tag (str): Project tag?
				open_ssh_key (str): 'ssh-rsa AAGSDG...'
				image (str): OS image.
				price (int): Machine hardware designated by price.
				autoBackups (bool): True to use auto backup feature.
				useMonitoring (bool): True to use DO Agent to send out monitoring data.
				ipv6 (bool): True to assign ipv6 address.
			Returns:
				SSHKey:
				Droplet:
			"""

            if not DigitalOcean.Droplet.Utils.isValidNameorTag(name):
                raise ValueError('invalid host name')
            if not DigitalOcean.Droplet.Utils.isValidNameorTag(tag):
                raise ValueError('invalid tag')

            #add the ssh key
            try:
                key = DigitalOcean.Security.Add_SSHKey(digo, tag, open_ssh_key)
            except Exception as ex:
                #already exists
                keys = DigitalOcean.Security.ListSSHKeys(digo)
                for key in keys:
                    if key.public_key == open_ssh_key:
                        break

            droplet = Droplet(
                token=digo.access_token,
                name=name,
                region=digo.region,
                image=image,
                size_slug=DigitalOcean.Droplet.sizes_by_price[price],
                tags=[tag],
                ssh_keys=[key],
                backups=autoBackups,  #???
                user_data=user_data,  #command to run?
                monitoring=
                useMonitoring,  #https://www.digitalocean.com/docs/monitoring/overview/
                ipv6=ipv6)
            droplet.create()

            return key, droplet
Beispiel #4
0
 def droplet_new(name, region, size, image):
     """
     Create a new droplet
     """
     token = os.getenv('access_token')
     kwargs = {'token': token, 'name': name, 'region': region, 'size': size,
               'image': image}
     droplet = Droplet(**kwargs)
     droplet.create()
Beispiel #5
0
def boot_droplet(droplet: Droplet) -> Droplet:
    """
    API call to DigitalOcean's API v2 in order to initialize the droplet.
    This function will just "hit the switch-on button" on the server.
    During boot, the droplet is not considered as "created" yet.

    :param digitalocean.Droplet.Droplet droplet: instance
    :return: an instance of digitalocean.Droplet.Droplet
    """
    with pong(text=cyan_text("Your droplet is initializing...")):
        droplet.create()

    # droplet, now, has an id but not yet been fully created
    return droplet
Beispiel #6
0
def create():
	
	user = getpass.getuser()
	user_ssh_key = open('/home/{}/.ssh/id_rsa.pub'.format(user)).read()
	key = SSHKey(token='bb7f9e5b82a17b7304efde1b9cd886fc329f09340fa172c3c27d890b099c25cb',
				 name='uniquehostname',
				 public_key=user_ssh_key)

	try:
		key.create()
	except:
		pass

	print ("key stored in Digital Ocean account")
	print (key.name)

	# Create Droplet
	# get all ssh keys stored in the digitalocean account
	keys = manager.get_all_sshkeys()

	# droplet = Droplet(token='bb7f9e5b82a17b7304efde1b9cd886fc329f09340fa172c3c27d890b099c25cb',
	# 							name=request.args.get('name'),
	# 							region='blr1', # Bangalore
	# 							image='docker-16-04', # Docker
	# 							size_slug='512mb',  # '512mb'
	# 							ssh_keys=keys, #Automatic conversion
	# 							backups=False)


	# from chrome extension

	repo_url = request.args.get('giturl')
	droplet = Droplet(token='bb7f9e5b82a17b7304efde1b9cd886fc329f09340fa172c3c27d890b099c25cb',
						name=request.args.get('name'),
						region=request.args.get('region'),
						image='docker-16-04',
						size_slug=request.args.get('size'),
						ssh_keys=keys,
						backups=False)

	droplet.create()
	
	thread = Thread(target=commandrun, args=[droplet])
	thread.start()

	print ("droplet id {}".format(droplet.id))
	print ("DO Created & ssh tested")
	return ("DO Created & ssh tested")
    def launch(self, name='kaggle-droplet', region='nyc2', image='ubuntu-16-04-x64', size='512mb', user=None,
               password=None, authorized_key='id_rsa', hdf=False):
        """Launch DigitalOcean droplet instance"""

        user_data = self.update_config_script(user=user, password=password, authorized_key=authorized_key, hdf=hdf)

        droplet = Droplet(token=self.api_token, name=name, region=region, image=image, size_slug=size, backups=False,
                          ssh_keys=self.ssh_id, user_data=user_data)
        droplet.create()

        self.droplet['id'] = droplet.id
        while not self.droplet.get('ip_address', None):
            self.droplet['ip_address'] = droplet.load().ip_address
        self.update_ssh()
        pprint.pprint(('Name: {}'.format(droplet.name), 'Image: {}'.format(droplet.image.get('slug')),
                       'Memory: {}'.format(droplet.size_slug), 'Disk Size: {}'.format(droplet.disk),
                       'IP Address: {}'.format(droplet.ip_address)))
Beispiel #8
0
def create_vpn_droplet():
    conf_dict = load_config()
    token = conf_dict['token']
    mngr = manager(conf_dict)
    ssh_key = mngr.get_ssh_key(conf_dict['sshKeyId'])

    droplet = Droplet(token=token,
                name=('vpn-%s' % time_ns()),
                region=conf_dict['region'],
                image='openvpn-18-04',
                size_slug='s-1vcpu-1gb',
                ssh_keys=[ssh_key],
                tags=['vpn'],
                backups=False)
    droplet.create()
    print("Droplet: %s(id:%s) created. Ip addr: %s" % (droplet.name, droplet.id, droplet.ip_address))
    return droplet
Beispiel #9
0
def create_droplets(ctx):
    """
    Create three new DigitalOcean droplets -
    node-1, node-2, node-3
    """
    manager = Manager(token=DIGITAL_OCEAN_ACCESS_TOKEN)
    keys = manager.get_all_sshkeys()
    for num in range(3):
        node = f"node-{num + 1}"
        droplet = Droplet(
            token=DIGITAL_OCEAN_ACCESS_TOKEN,
            name=node,
            region="nyc3",
            image="ubuntu-20-04-x64",
            size="s-2vcpu-4gb",
            tags=[node],
            ssh_keys=keys,
        )
        droplet.create()
        print(f"{node} has been created.")
Beispiel #10
0
def create_droplets(ctx):
    """
    Create three new DigitalOcean droplets -
    node-1, node-2, node-3
    """
    manager = Manager(token=DIGITAL_OCEAN_ACCESS_TOKEN)
    keys = manager.get_all_sshkeys()
    for num in range(3):
        node = f'node-{num + 1}'
        droplet = Droplet(
            token=DIGITAL_OCEAN_ACCESS_TOKEN,
            name=node,
            region='nyc3',
            image='ubuntu-20-04-x64',
            size_slug='4gb',
            tags=[node],
            ssh_keys=keys,
        )
        droplet.create()
        print(f'{node} has been created.')
def create_droplet(in_token):
    start = timer()
    key = create_rsa()
    print("[+] Creating {} Droplet ... ".format(name))
    new_droplet = Droplet(token=in_token,
                          name=name,
                          ssh_keys=key,
                          image="ubuntu-14-04-x64",
                          region='nyc3',
                          size='s-6vcpu-16gb')
    new_droplet.create()

    print("[+] Waiting Creation ... ", end=" ")
    actions = new_droplet.get_actions()
    for action in actions:
        action.load()
        action.wait()
    print("Done {}".format(timer() - start))

    new_droplet = new_droplet.load()

    print("[+] {} with ip {}".format(name, new_droplet.ip_address))

    return new_droplet
Beispiel #12
0
 def create_droplet(*args, **kwargs):
     droplet = Droplet(*args, **kwargs)
     droplet.create()
     return droplet
Beispiel #13
0
from digitalocean import Manager, Droplet
from time import sleep
from time import time
import paramiko

connection = Manager(token=os.environ["DO_TOKEN"])
key = []
key.append(connection.get_ssh_key("18830428"))
for i in connection.get_my_images():
    if i.name == os.environ["IMAGE_NAME"]:
        image = connection.get_image(i.id).id
        break
srv = Droplet(token=os.environ["DO_TOKEN"], image=image, size="s-1vcpu-1gb", region="fra1", ssh_keys=key, \
              name=os.environ["TRAVIS_COMMIT"], user_data="#!/bin/bash\nsed -i 's%PasswordAuthentication no%PasswordAuthentication yes%g' /etc/ssh/sshd_config\nservice sshd restart\n")
start = time()
srv.create()
for i in range(1, 12):
    if srv.get_actions()[0].status == 'completed':
        stop = time()
        print(
            "Instance provisioninng took {}. Saving envs to run_instance_envs."
            .format(stop - start))
        start = time()
        ip_addr = Droplet.get_object(api_token=os.environ["DO_TOKEN"],
                                     droplet_id=srv.id).ip_address
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        while (True):
            try:
                client.connect(ip_addr,
                               username="******",
Beispiel #14
0
def create(name, cpu=1, ram=1, wait=True, bootstrap_vm=True):
    """Create a new vm and return its Droplet object.

    params:
    - name: string name of the vm

    optional:
    - cpu: int number of CPUs (default: 1)
    - ram: int amount of RAM in gigabyte (default: 1)
    - wait: bool whether to wait for completion
    - bootstrap_vm: bool whether to bootstrap the node

    return:
      Droplet object of the new VM
    """
    label = f'Creating vm {name} (takes several minutes)...'
    print(f'[+] {label}', end='', flush=True)

    keys = manager.get_all_sshkeys()
    droplet = Droplet(token=TOKEN,
                      name=name,
                      region='fra1',
                      image='debian-9-x64',
                      distro='debian',
                      size_slug=f's-{cpu}vcpu-{ram}gb',
                      ssh_keys=keys,
                      backups=False)
    droplet.create()

    # handle create action
    actions = droplet.get_actions()
    create_action = list(
        filter(lambda action: action.type == 'create', actions))[0]
    if wait:
        try:
            droplet_action_wait(create_action,
                                callback=print_status_cycle(labal=label),
                                timeout=vm_bootstrap_timeout)
        except timeout as e:
            print(f'\n[-] Failed, retrying: {e}')
            return create(name, cpu, ram, wait, bootstrap_vm)

    # print droplet infos
    droplet.load()
    print('done')
    print(f'{name}: {create_action.status} -> {droplet.ip_address}')

    if bootstrap_vm:
        uname = ''
        status = print_status_cycle('Booting (takes several minutes)...')
        while 'Linux' not in uname:
            status()
            try:
                uname, stderr = ssh(droplet,
                                    'uname -s',
                                    timeout=2,
                                    print_streams=False,
                                    print_errors=False)
            except Exception:
                # TODO: ignores Permission denied (publickey)
                pass
            sleep(0.20)
        success = bootstrap(droplet)
        if not success:
            destroy(droplet, ask=False)
            droplet = create(name, cpu, ram, wait, bootstrap_vm)

    return droplet
account = Account.get_object( token )
keys_list = account.get_data( "account/keys" )

# get ssh key id
keys_id_list = []
for key in keys_list[ 'ssh_keys' ]:
    keys_id_list.append( key[ 'id' ] )

droplet = Droplet( token = token,
                   ssh_keys = keys_id_list,
                   name = "Server1",
                   region = "nyc2",
                   image = "ubuntu-14-04-x64",
                   size_slug = "512mb",
                   backups = True )
droplet.create()
print "droplet created."

# waiting for server to boot
#print "Waiting for server to boot, this may take a while..."
#actions = droplet.get_actions()
#for action in actions:
#    action.wait()

# reload the droplet and get the ip address
while not droplet.ip_address:
    droplet.load()
with open( "inventory_DO", "w+" ) as f:
    command = "node0 ansible_ssh_host=%s ansible_ssh_user=root ansible_ssh_private_key_file=node0.key" % droplet.ip_address
    print >> f, command
print "inventory file created."