Last active
July 6, 2016 08:01
-
-
Save paul-schwendenman/9108414 to your computer and use it in GitHub Desktop.
Digital Ocean API wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import digitalocean | |
import time | |
import os | |
import sys | |
try: | |
token = os.environ["DIGO_TOKEN"] | |
except KeyError: | |
print "Please set the environment variable DIGO_TOKEN" | |
sys.exit(1) | |
manager = digitalocean.Manager(token=token) | |
my_droplets = manager.get_all_droplets() | |
for droplet in my_droplets: | |
name = "Backup of {name} at {date}".format(name=droplet.name, date=time.strftime("%Y-%m-%d %H:%M")) | |
sys.stdout.write("Backup of {name}:".format(name=droplet.name)) | |
sys.stdout.flush() | |
resp = droplet.take_snapshot(name, return_dict=False, power_off=True) | |
sys.stdout.write("running... ") | |
sys.stdout.flush() | |
resp.wait(1) | |
sys.stdout.write("done.\n") | |
sys.stdout.write('Removing old snapshots\n') | |
my_images = manager.get_my_images() | |
my_images_sorted = sorted(my_images, lambda a, b: a.created_at > b.created_at) | |
for image in my_images_sorted[:-5]: | |
image.destroy() | |
sys.stdout.write('Transfering to NYC1\n') | |
my_images_sorted[-1].transfer('nyc3') | |
sys.stdout.write('Transfering to SFO1\n') | |
my_images_sorted[-1].transfer('sfo1') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python-digitalocean==1.8.1 | |
requests==2.10.0 | |
wheel==0.24.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment