コード例 #1
0
def turn_it_on(droplet: digitalocean.Droplet) -> bool:
    if droplet.status == "active":
        log.info("The Droplet '{!s}' Is Already Powered Up".format(droplet))
        return True
    elif droplet.status == "off":
        log.info("Powering Up {!s}".format(droplet))
        power_up_action_id = send_command(5, droplet,
                                          "power_on")["action"]["id"]
        power_up_action = send_command(5, droplet, "get_action",
                                       power_up_action_id)
        log.debug("power_up_action " + str(power_up_action) +
                  str(type(power_up_action)))
        power_up_outcome = wait_for_action(power_up_action, 3)
        log.debug("power_up_outcome " + str(power_up_outcome))
        if power_up_outcome:
            for i in range(5):
                time.sleep(2)
                droplet.load()  # refresh droplet data
                log.debug("droplet.status " + droplet.status)
                if droplet.status == "active":
                    log.info("Powered Back Up {!s}".format(droplet))
                    return True
            log.critical(
                "DID NOT POWER UP BUT REPORTED 'powered_up'=='True' " +
                str(droplet))
            return False

        log.critical("DID NOT POWER UP " + str(droplet))
        return False
    else:
        log.error("'droplet.status' SHOULD BE EITHER 'off' OR 'active'")
        return False
コード例 #2
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.')
コード例 #3
0
    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)))
コード例 #4
0
ファイル: digitalocean_service.py プロジェクト: pmp47/Cloud
        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
コード例 #5
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()
コード例 #6
0
def sleep_until_droplet_action_is_completed(droplet: digitalocean.Droplet,
                                            action_type: str) -> None:
    incomplete = True
    while incomplete:
        for action in droplet.get_actions():
            action.load()
            print(f"...[{action.type}]: {action.status}")
            if action.type == action_type and action.status == "completed":
                incomplete = False
                break
        if incomplete:
            time.sleep(5)
    droplet.load()
コード例 #7
0
ファイル: api.py プロジェクト: manikos/beauty-ocean
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
コード例 #8
0
ファイル: app.py プロジェクト: CapsLockHacks/OctoShark-Server
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")
コード例 #9
0
    def test_build_droplet_details_active(self):
        droplet = Droplet(name="test", image={'name': 'Ubuntu'},
                          status="active", ip_address="192.0.2.0",
                          size_slug="1gb", region={'name': 'nyc3'},
                          tags=['foo', 'bar'])
        indicator = DoIndicator.Indicator()
        sub_menu = indicator.build_droplet_details(droplet)

        ip = sub_menu.get_children()[0]
        image = sub_menu.get_children()[1]
        region = sub_menu.get_children()[2]
        size = sub_menu.get_children()[3]
        tags = sub_menu.get_children()[4]
        view = sub_menu.get_children()[6]
        power = sub_menu.get_children()[7]
        reboot = sub_menu.get_children()[8]

        self.assertEqual(len(sub_menu), 9)
        self.assertEqual(ip.get_label(), "IP: 192.0.2.0")
        self.assertEqual(region.get_label(), "Region: nyc3")
        self.assertEqual(image.get_label(), "Type: Ubuntu")
        self.assertEqual(size.get_label(), "Size: 1gb")
        self.assertEqual(tags.get_label(), "Tags: foo, bar")
        self.assertEqual(view.get_label(), "View on web...")
        self.assertEqual(power.get_label(), "Power off...")
        self.assertEqual(reboot.get_label(), "Reboot...")
    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)))
コード例 #11
0
def dummy_droplet():
    return Droplet(
        name="d_name",
        _log="a log here",
        token="token-here",
        id=1,
        ssh_keys=[SSHKey(id=1, name="ssh1"), SSHKey(id=1, name="ssh2")],
    )
コード例 #12
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
コード例 #13
0
ファイル: api.py プロジェクト: manikos/beauty-ocean
def poll_droplet(droplet: Droplet, poll_interval: int = 4) -> Droplet:
    """
    Given a new-born droplet (without an IP), poll every poll_interval seconds
    the DO API in order to find out when the droplet is ready.

    :param digitalocean.Droplet.Droplet droplet: instance
    :param int poll_interval: get droplet status every poll_interval seconds
    :return: digitalocean.Droplet.Droplet instance or None
    """
    action = droplet.get_action(droplet.action_ids[0])
    action.droplet_id = droplet.id
    result = action.wait(update_every_seconds=poll_interval)
    if result:
        # droplet is now fully created (with IP, tags etc)
        text = "Retrieving droplet data after boot..."
        with pong(text=cyan_text(text)):
            return droplet.load()
コード例 #14
0
    def _populate_droplet(droplet_id, base_droplet=None):
        """ Take a droplet ID from the mock list and turn it into a droplet"""
        if not base_droplet:
            return Droplet(**INSTANCE_MOCK_DATA[droplet_id])

        template = INSTANCE_MOCK_DATA[droplet_id]

        for key in template.keys():
            setattr(base_droplet, key, template[key])
        return base_droplet
コード例 #15
0
    def test_build_droplet_details_inactive(self):
        droplet = Droplet(name="test", image={'name': 'Ubuntu'},
                          status="inactive", ip_address="192.0.2.0",
                          size_slug="1gb", region={'name': 'nyc3'})
        indicator = DoIndicator.Indicator()
        sub_menu = indicator.build_droplet_details(droplet)

        power = sub_menu.get_children()[6]

        self.assertEqual(power.get_label(), "Power on...")
コード例 #16
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.")
コード例 #17
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.')
コード例 #18
0
 def droplet(id, action):
     token = os.getenv('access_token')
     droplet = Droplet(token=token, id=id)
     if action == 'status':
         droplet.load()
         click.echo(json.dumps({'status': droplet.status}))
     elif action == 'destroy':
         droplet.destroy()
コード例 #19
0
    def list_droplets(self):
        """
        List all droplets in the project
        """
        resources = self.list_resources()
        droplets = []
        for resource in resources:
            if not resource["urn"].startswith("do:droplet:"):
                continue
            droplet_id = resource["urn"].replace("do:droplet:", "")
            droplet = Droplet.get_object(api_token=self.token,
                                         droplet_id=droplet_id)
            droplets.append(droplet)

        return droplets
コード例 #20
0
def create_droplet_now(droplet_params) -> Droplet:
    """
    Given some droplet parameters, make an API call in order to create
    the Droplet.

    :param dict droplet_params: droplet (selected) parameters
    :return: digitalocean.Droplet instance
    """
    # create a Droplet instance
    bare_droplet = Droplet(**droplet_params)

    # initialize it (to get the id)
    pre_droplet = api.boot_droplet(bare_droplet)

    # poll it in order to know when it'll be ready-to-go
    print(config.green_text("Droplet initialized! Booting..."))
    droplet = api.poll_droplet(pre_droplet)
    print(config.green_text("Droplet has been created successfully! \(´▽`)/"))
    return droplet
コード例 #21
0
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
コード例 #22
0
 def dict_to_droplet(self, droplet_dict):
     droplet = Droplet()
     droplet_dict_copy = copy.deepcopy(droplet_dict)
     for key, value in droplet_dict_copy.items():
         setattr(droplet, key, value)
     return droplet
コード例 #23
0
from digitalocean import Droplet
from digitalocean import Image
from digitalocean import Region

DROPLETS_RESPONSE = [
    Droplet(
        id='12345678',
        name='test-droplet-1',
        locked=False,
        status='active',
        features=[],
        region=Region(slug='nyc1').__dict__,
        created_at='2021-03-03T21:29:35Z',
        image=Image(slug='ubuntu-18-04-x64').__dict__,
        size_slug='s-1vcpu-2gb',
        kernel=None,
        tags=[],
        volume_ids=[
            'dfa32d234-2418-112b-af81-0a584fe1449b9',
            'cca32d234-2418-112b-af81-12ab4fe1449b9',
        ],
        vpc_uuid='123445bc-dcd4-12e8-80bc-3dfea149fba1',
        ip_address='30.1.2.3',
        ip_v6_address='',
        private_ip_address='192.128.10.1',
    ),
]
コード例 #24
0
 def create_droplet(*args, **kwargs):
     droplet = Droplet(*args, **kwargs)
     droplet.create()
     return droplet
コード例 #25
0
from digitalocean import Droplet, Account, Manager
from time import sleep

token = "404b7a26a00b89c7dc85f431e293fd532d68edfaf3268728a95ed38833dee257"
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()
コード例 #26
0
def create_snapshot(droplet: digitalocean.Droplet, snapshot_name: str) -> None:
    droplet.take_snapshot(snapshot_name, power_off=True)
    droplet.load()
    sleep_until_droplet_action_is_completed(droplet, "snapshot")
コード例 #27
0
ファイル: vm.py プロジェクト: ctfctl/ctfctl
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
コード例 #28
0
import os
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,