Esempio n. 1
0
    def resurrect(self, size=1, image=None, ssh_keys=None):
        version = self.next_version_number()

        tag = api.Tag(token=self.token, name=self.name)
        tag.create()  # create tag if not already created

        result = []
        for _ in range(0, size):
            server = api.Droplet(token=self.token,
                                 name=f"{self.name}{version}",
                                 region='ams3',
                                 size='s-1vcpu-1gb',
                                 private_networking=True,
                                 ipv6=True,
                                 backups=False,
                                 ssh_keys=ssh_keys or settings.DO_SSH_KEYS,
                                 image=image or settings.DO_OBJECT_IMAGE
                                 #  tags=[tag],
                                 )

            r = server.create()
            if r or r is None:
                tag.add_droplets([str(server.id)])

            result.append((server, r))
            version = version + 1

        return result
def add_droplets(num=1):
    snapshots = manager.get_all_snapshots()
    snapshots = [
        snapshot for snapshot in snapshots if snapshot.name == 'tbp-snapshot'
    ]
    snapshots.sort(key=lambda x: x.created_at, reverse=True)
    snapshot = snapshots[0]
    tag = digitalocean.Tag(token=manager.token, name='lb')
    tag.create()
    droplets = []
    for _ in range(num):
        droplet = digitalocean.Droplet(token=manager.token,
                                       region=main_droplet.region['slug'],
                                       size_slug='s-1vcpu-2gb',
                                       ssh_keys=keys,
                                       vpc_uuid=main_droplet.vpc_uuid,
                                       image=snapshot.id,
                                       name='tbp-worker',
                                       monitoring=True,
                                       backup=False)
        droplet.create()
        droplets.append(droplet)
        droplet_ids.add(droplet.id)
    tag.add_droplets([droplet.id for droplet in droplets])
    return droplets
Esempio n. 3
0
 def tag_droplet(self, droplet, tags):
     if not isinstance(tags, list):
         raise TypeError('tags argument has to be a list of tags')
     self._logger.info('droplet %s: set tags: %s', droplet.name,
                       ', '.join(tags))
     for tag in tags:
         tagger = digitalocean.Tag(token=self.do_token, name=tag)
         tagger.create()
         tagger.add_droplets(str(droplet.id))
    def create_or_get_tag(manager, tag_name):
        tag = digio.Tag(token=manager.token, name=tag_name)
        try:
            tag.load()
        except NotFoundError:
            tag.create()

        print(manager.get_all_tags())
        print(tag.name)
        return tag
Esempio n. 5
0
def set_snapshot_labels(ctx: Context,
                        snapshot_identifier: NewSnapshotIdentifier,
                        labels: Dict):
    for label, value in labels.items():
        tag_name = label + ":" + value
        tag = digitalocean.Tag(name=tag_name)

        # Create the tag if it does not exist yet.
        _create_missing_tag(tag)

        tag.add_snapshots(snapshot_identifier)
Esempio n. 6
0
    def test_remove_droplets(self):
        url = self.base_url + "tags/awesome/resources"
        responses.add(responses.DELETE,
                      url,
                      status=204,
                      content_type='application/json')

        droplet_tag = digitalocean.Tag(name='awesome', token=self.token)
        droplet_tag.remove_droplets(["9569411"])

        self.assertEqual(responses.calls[0].request.url,
                         self.base_url + "tags/awesome/resources")
Esempio n. 7
0
    def test_add_volume_snapshots(self):
        url = self.base_url + "tags/awesome/resources"
        responses.add(responses.POST,
                      url,
                      status=204,
                      content_type='application/json')

        tag = digitalocean.Tag(name='awesome', token=self.token)
        tag.add_snapshots(["9569411"])

        self.assertEqual(responses.calls[0].request.url,
                         self.base_url + "tags/awesome/resources")
Esempio n. 8
0
    def test_delete(self):
        responses.add(responses.DELETE,
                      self.base_url + "tags/awesome",
                      status=204,
                      content_type='application/json')

        droplet_tag = digitalocean.Tag(name='awesome', token=self.token)
        droplet_tag.delete()

        self.assertEqual(responses.calls[0].request.url,
                         self.base_url + "tags/awesome")
        self.assertEqual(droplet_tag.name, "awesome")
Esempio n. 9
0
    def test_update_tag(self):
        data = self.load_from_file('tags/updatetag.json')

        responses.add(responses.PUT,
                      self.base_url + "tags/awesome",
                      body=data,
                      status=200,
                      content_type='application/json')

        droplet_tag = digitalocean.Tag(name='awesome', token=self.token)
        droplet_tag.update_tag(name="extra-awesome")

        self.assertEqual(responses.calls[0].request.url,
                         self.base_url + "tags/awesome")
        self.assertEqual(droplet_tag.name, "extra-awesome")
Esempio n. 10
0
    def test_create(self):
        data = self.load_from_file('tags/single.json')

        responses.add(responses.POST,
                      self.base_url + "tags/",
                      body=data,
                      status=201,
                      content_type='application/json')

        droplet_tag = digitalocean.Tag(name='awesome', token=self.token)
        droplet_tag.create()

        self.assertEqual(responses.calls[0].request.url,
                         self.base_url + "tags/")
        self.assertEqual(droplet_tag.name, "awesome")
Esempio n. 11
0
    def test_load(self):
        data = self.load_from_file('tags/single.json')

        url = self.base_url + "tags/awesome"
        responses.add(responses.GET,
                      url,
                      body=data,
                      status=200,
                      content_type='application/json')

        droplet_tag = digitalocean.Tag(name='awesome', token=self.token)
        droplet_tag.load()

        self.assert_get_url_equal(responses.calls[0].request.url, url)
        self.assertEqual(droplet_tag.name, "awesome")
Esempio n. 12
0
    def test_remove_droplets(self):
        data = self.load_from_file('tags/resources.json')

        responses.add(responses.DELETE,
                      self.base_url + "tags/awesome/resources",
                      body=data,
                      status=201,
                      content_type='application/json')

        resource_id = json.loads(data)["resources"][0]["resource_id"]

        droplet_tag = digitalocean.Tag(name='awesome', token=self.token)
        droplet_tag.remove_droplets([resource_id])

        self.assertEqual(responses.calls[0].request.url,
                         self.base_url + "tags/awesome/resources")
Esempio n. 13
0
def add_tag_to_droplet(tag_name, droplet_to_add_tag):
    tag_done = False
    while tag_done is False:
        tag = digitalocean.Tag(token=token, name=tag_name)
        tag.add_droplets([droplet_to_add_tag.id])
        time.sleep(1)

        wrap_digitalocean_call(load_droplet, droplet_to_add_tag)
        if tag_name in droplet_to_add_tag.tags:
            tag_done = True
            logger.debug(
                f"Tag {tag_name} added succesfully to {droplet_to_add_tag.name}"
            )
        else:
            logger.debug(
                f"Tag {tag_name} not added succesfully to {droplet_to_add_tag.name}, trying again"
            )
Esempio n. 14
0
 def deploy_instance(self, name: str) -> None:
     log.info(f"digitalocean: Deploying instance with name {name}")
     launch_config = self.launch.copy()
     launch_config.update({
         "label": name,
         "hostname": name,
         "tag": f"scalr={self.filter}",
     })
     droplet = digitalocean.Droplet(
         name=name,
         region=launch_config["region"],
         image=launch_config["image"],
         size_slug=launch_config["size"],
         ssh_keys=launch_config["ssh_keys"],
         user_data=launch_config.get("user_data", ""),
         ipv6=launch_config.get("ipv6", False),
     )
     droplet.create()
     tag = digitalocean.Tag(name=f"scalr:{self.filter}")
     tag.create()
     tag.add_droplets([droplet.id])
     log.info(f"Creating droplet {name}")
Esempio n. 15
0
def add_tag(my_tag='', droplet_id=0):
    tag = digitalocean.Tag(token=uid, name=my_tag)
    tag.create()
    tag.add_droplets([droplet_id])
Esempio n. 16
0
droplet.create()

# wait for bringup
completed = False
while not completed:
    actions = droplet.get_actions()
    for action in actions:
        action.load()
        # Once it shows complete, droplet is up and running
        print(action.status)
        if action.status == u'completed':
            completed = True

droplet.load()
ip_address = droplet.ip_address
droplet_tag = digitalocean.Tag(name='moviepy', token=token)
if droplet_tag.load():
    print("loaded tag %s" % droplet_tag)
if droplet_tag.add_droplets(str(droplet.id)):
    print("added droplet %s to tag %s" % (droplet, droplet_tag))
print(ip_address)
print("waiting for ssh")
time.sleep(5)
exit(0)
# copy script and run it
import os
print("copy setup and run")
os.system("scp -o StrictHostKeyChecking=no setup.sh root@%s:~/" % ip_address)
os.system("ssh -o StrictHostKeyChecking=no root@%s ./setup.sh" % ip_address)
Esempio n. 17
0
def create_vm(config):
    # build the digital ocean manager object
    manager = digitalocean.Manager(token=config.get("DigitalOcean", "api_key"))

    # Create a VM and return a droplet object.
    droplet = digitalocean.Droplet(token=manager.token,
                                   name="recon-droplet",
                                   region="nyc1",
                                   image="ubuntu-16-04-x64",
                                   size_slug="512mb",
                                   ssh_keys=manager.get_all_sshkeys(),
                                   backups=False)
    print("Creating the droplet...")
    droplet.create()

    print("Waiting for the droplet to be active...")
    # Wait for the DO droplet to become active
    while droplet.status != "active":
        for i in range(30):
            # Overwrites the previous line with the next line, removing the dependency of progressbar2
            print(
                "Sleeping for {} seconds to wait for the droplet to become active."
                .format(30 - i),
                end="\r")
            time.sleep(1)
        droplet.load()

    # Show progress
    print()
    droplet.load()
    print("Droplet has been created with the address {}".format(
        droplet.ip_address))

    # Setup the SSH connection
    print()
    for i in range(30):
        print("Sleeping for {} seconds to wait for SSH to be ready...".format(
            30 - i),
              end="\r")
        time.sleep(1)
    print("SSH should now be ready...")
    droplet.load()
    ssh_key_filename = config.get("DigitalOcean", "ssh_key_filename")
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    print("Connecting to the droplet...")
    ssh.connect(droplet.ip_address,
                username="******",
                key_filename=ssh_key_filename)

    # wget the firewall config and swap in relevant IP addresses to allow connectivity to the droplet
    firewall_config = config.get("DigitalOcean", "firewall_config")
    source_ips = config.get("DigitalOcean", "source_ips")
    rpc_script = config.get("DigitalOcean", "rpc_script")
    rpc_service = config.get("DigitalOcean", "rpc_service")

    # build a comma separated list of ips to add to firewall config
    ip_str = ""
    for ip in json.loads(source_ips):
        if ip_str == "":
            ip_str += ip
        else:
            ip_str += "," + ip

    # grab the firewall config
    _, stdout, stderr = ssh.exec_command("wget {}".format(firewall_config))

    # replace SOURCE_IPS in the config with ip_str
    _, stdout, stderr = ssh.exec_command(
        "sed -i 's/SOURCE_IPS/{}/g' iptables.rules".format(ip_str))

    # grab the rpc script
    _, stdout, stderr = ssh.exec_command("wget {}".format(rpc_script))

    # grab the rpc service file
    _, stdout, stderr = ssh.exec_command("wget {}".format(rpc_service))

    # Configure the VM with the setup script
    print()
    setup_script = config.get("DigitalOcean", "setup_script")
    print("Setting up the droplet with the configuration script...")
    _, stdout, stderr = ssh.exec_command(
        "wget -O - {} | bash".format(setup_script))

    # Print the output of configuration
    for line in iter(lambda: stdout.readline(2048), ""):
        print(line)

    tag = digitalocean.Tag(token=manager.token, name="bounty")
    tag.create()
    tag.add_droplets([str(droplet.id)])

    print("Droplet Created.")

    print(" ID | IP Addr | Name | Tags")
    print("{0.id} | {0.ip_address} | {0.name} | {0.tags}".format(droplet))
    return droplet
Esempio n. 18
0
#!/usr/bin/env python3
from pathlib import Path
import digitalocean

# Get the key
with open(str(Path.home()) + "/.digitalocean/api.key", 'r') as r:
    token = r.read()[:-1]

# Set SSH keys
manager = digitalocean.Manager(token=token)
keys = manager.get_all_sshkeys()

# Create the droplet
droplet_name = input('What would you like to name the droplet?> ')
droplet = digitalocean.Droplet(token=token,
                               name=droplet_name,
                               region='sfo2',
                               image='ubuntu-18-04-x64',
                               size_slug='1gb',
                               ssh_keys=keys)
droplet.create()

# Tag the droplet
tag = digitalocean.Tag(token=token, name="bug-bounty")
tag.create()
tag.add_droplets([droplet.id])
print("Droplet " + droplet_name + " created!")
def tag_digitalocean_instance(droplet, tag):
    tag = digitalocean.Tag(token=DO_TOKEN, name=tag)
    tag.create()  # create tag if not already created
    tag.add_droplets(droplet.id)
Esempio n. 20
0
def tag_droplet(droplet_tag, droplet_id, secret_token):
    droplet_tag = digitalocean.Tag(token=secret_token, name=droplet_tag)
    droplet_tag.create()
    droplet_tag.add_droplets(droplet_id)