субота, 20 грудня 2014 р.

How to use Python Digital Ocean API

I am using Digital Ocean and can advice them as good hosting provider. But DO also have unpleasant feature - you cant turn on autobackup if you did not choose it before creating VM. Fortunately Digital Ocean admin panel have ability to snapshot VM (but with reboot :() and I will show you how to backup VM at once with DO API.
So at first, as usual, you need to setup python library:

pip install -U python-digitalocean

We will use python wrapper for DO API. It is easier than use REST API. So let me show you some trivial examples:


Output:

gray.my.com
blabla.my.net
nagios.my.com
test.my.com

token is you API key, you can see it in DO Admin panel. In consequence of script launching you will get list of your VMs my_droplets. Cool, hah? So you can just pass parameter which VM to show, reboot, etc:

>>> my_droplets[1].power_off()

First VM from your objects list will go to shutdown. Or maybe to see which kernel VM is using:

>>> my_droplets[1].kernel
{u'version': u'2.6.32-358.6.2.el6.i686', u'id': 145, u'name': u'CentOS 6.4 x32 vmlinuz-2.6.32-358.6.2.el6.i686 (145)'}

These are all available methods for droplet:

['action_ids', 'backup_ids', 'backups', 'change_kernel', 'create', 'created_at',
'destroy', 'disable_backups', 'disk', 'enable_backups', 'enable_ipv6', 'enable_private_networking',
'end_point', 'features', 'get_action', 'get_actions', 'get_data', 'get_events', 'get_kernel_available',
'get_object', 'get_snapshots', 'id', 'image', 'ip_address', 'ip_v6_address', 'ipv6', 'kernel', 'load',
'locked', 'memory', 'name', 'networks', 'power_cycle', 'power_off', 'power_on', 'private_ip_address',
'private_networking', 'reboot', 'rebuild', 'region', 'rename', 'reset_root_password', 'resize', 'restore',
'shutdown', 'size_slug', 'snapshot_ids', 'ssh_keys', 'status', 'take_snapshot', 'token', 'user_data', 'vcpus']

More complex example. Lets reboot all our VMs:


Last example is from author of this library https://github.com/koalalorenzo/python-digitalocean#shutdown-all-droplets

So lets decide our initial problem. I want to shapshot my VM nagios.my.com:


And launch it.

Look at my full script on Github https://github.com/ipeacocks/do_snapshot

Links:
https://github.com/koalalorenzo/python-digitalocean#shutdown-all-droplets
https://developers.digitalocean.com/

Немає коментарів:

Дописати коментар