def restore(self, image_id=None): """ This method allows you to restore a droplet with a previous image or snapshot. This will be a mirror copy of the image or snapshot to your droplet. Be sure you have backed up any necessary information prior to restore. :type image_id: int :param image_id: ID of the image you would like to use to rebuild your droplet with """ # https://api.digitalocean.com/droplets/[droplet_id]/restore/?image_id=[image_id]& # client_id=[your_client_id]&api_key=[your_api_key] url = "/droplets/%s/restore" % (str(self.id)) data = self._request(url,image_id=image_id) self.event_id = data['event_id'] log.info("Restoring: %d With: %d Event: %d" % (self.id, image_id, self.event_id)) self.update()
def create_snapshot(self, name=None): """ This method allows you to take a snapshot of the droplet once it has been powered off, which can later be restored or used to create a new droplet from the same image. Please be aware this may cause a reboot. :type name: string :param size: The NAME of the snapshot """ # https://api.digitalocean.com/droplets/[droplet_id]/snapshot/?name=[snapshot_name]& # client_id=[your_client_id]&api_key=[your_api_key] #should thread this in the future log.info("Powering off %d" % (self.id)) self.power_off() while self.status != 'off': self.update() url = "/droplets/%s/snapshot" % (str(self.id)) data = self._request(url, name=name) self.event_id = data['event_id'] log.info("Taking Snapshot: %d, Event: %d" % (self.id, self.event_id))
def _request(self, event, status_check=None, **kwds): if "client_id" not in kwds: kwds["client_id"] = self._client_id if "api_key" not in kwds: kwds["api_key"] = self._api_key headers = {"User-Agent": "doto/client"} for key, value in kwds.iteritems(): log.debug("%s = %s" % (key, value)) BASEURL = "https://api.digitalocean.com" response = requests.get(BASEURL + event, headers=headers, params=kwds) log.info("Getting " + event) log.debug(response.url) if response.status_code == 200: data = response.json() log.debug(data) if data["status"] == "ERROR": log.debug("Error with request: %s" % (data["message"])) error = "MSG: %s" % (data["message"]) raise DOError(error) if status_check: return response.status_code return data else: # error data = response.json() error = "Status code: %d MSG: %s" % (response.status_code, data["message"]) raise DOError(error)
def request(self, event, status_check=False, **kwds): if 'client_id' not in kwds: kwds['client_id'] = self.__client_id if 'api_key' not in kwds: kwds['api_key'] = self.__api_key headers = {'User-Agent': 'doto/client'} for key, value in kwds.iteritems(): log.debug("%s = %s" % (key, value)) response = requests.get(BASEURL + event, headers=headers, params=kwds) log.info('Getting ' + event) log.debug(response.url) if response.status_code == 200: data = response.json() log.debug(data) if data['status'] == 'ERROR': log.debug("Error with request: %s" % (data['message'])) error = "MSG: %s" % (data['message']) raise DOError(error) if status_check: return response.status_code return data else: #error # fixme data = dict(message="fake message") error = "Status code: %d MSG: %s" % (response.status_code, data['message']) raise DOError(error)
def create_snapshot(self,name=None): """ This method allows you to take a snapshot of the droplet once it has been powered off, which can later be restored or used to create a new droplet from the same image. Please be aware this may cause a reboot. :type name: string :param size: The NAME of the snapshot """ # https://api.digitalocean.com/droplets/[droplet_id]/snapshot/?name=[snapshot_name]& # client_id=[your_client_id]&api_key=[your_api_key] #should thread this in the future log.info("Powering off %d" % (self.id)) self.power_off() while self.status != 'off': self.update() url = "/droplets/%s/snapshot" % (str(self.id)) data = self._request(url,name=name) self.event_id = data['event_id'] log.info("Taking Snapshot: %d, Event: %d" % (self.id, self.event_id))
def rebuild(self, image_id=None, use_current=False): """ This method allows you to reinstall a droplet with a default image. This is useful if you want to start again but retain the same IP address for your droplet. :type image_id: int :param image_id: ID of the image you would like to use to rebuild your droplet with :type use_current: BOOL :param use_current: Use the current image_id of the droplet during rebuild process https://api.digitalocean.com/droplets/[droplet_id]/rebuild/?image_id=[image_id]& client_id=[your_client_id]&api_key=[your_api_key] """ if use_current: image_id = self.image_id url = "/droplets/%s/rebuild" % (str(self.id)) data = self._request(url, image_id=image_id) self.event_id = data["event_id"] self.update() log.info("Rebuild: %d With: %d Event: %d" % (self.id, image_id, self.event_id))
def rebuild(self, image_id=None): """ This method allows you to reinstall a droplet with a default image. This is useful if you want to start again but retain the same IP address for your droplet. :type image_id: int :param image_id: ID of the image you would like to use to rebuild your droplet with """ # https://api.digitalocean.com/droplets/[droplet_id]/rebuild/?image_id=[image_id]& # client_id=[your_client_id]&api_key=[your_api_key] url = "/droplets/%s/rebuild" % (str(self.id)) data = self._request(url,image_id=image_id) self.event_id = data['event_id'] log.info("Rebuild: %d With: %d Event: %d" % (self.id, image_id, self.event_id)) self.update()
def edit_record( self, record_id=None, record_type=None, data=None, name=None, priority=None, port=None, weight=None, ): """ This method edits an existing domain record. :type record_id: int :param record_id: ID of record you are trying to retrieve. :type record_type: String :param record_type: the type of record you would like to create. 'A', 'CNAME', 'NS', 'TXT', 'MX' or 'SRV'. :type data: String :param data: This is the value of the record. :type name: String :param name: Required for 'A', 'CNAME', 'TXT' and 'SRV' records otherwise optional :type priority: int :param priority: required for 'SRV' and 'MX' records otherwise optional :type port: int :param port: required for 'SRV' records otherwise optional :type weight: int :param weight: required for 'SRV' records. otherwise optional https://api.digitalocean.com/domains/[domain_id]/records/[record_id]/edit? client_id=[your_client_id]&api_key=[your_api_key] """ log.info("Editing Record: %d" % (record_id)) url = "/domains/%s/records/%d/edit" % (str(self.id), record_id) data = self._conn.request(url, record_type=record_type, data=data, name=name, priority=priority, port=port, weight=weight) log.debug(data) return data['record']
def record_destroy(self, record_id=None): """ This method deletes the specified domain record. https://api.digitalocean.com/domains/[domain_id]/records/[record_id]/destroy? client_id=[your_client_id]&api_key=[your_api_key] """ url = "domains/%s/records/%d/destroy" % (str(self.id),record_id) data = self._conn.request(url) log.info(data)
def destroy(self): """ This method allows you to destroy a domain. https://api.digitalocean.com/domains/[domain_id]/destroy/? client_id=[your_client_id]&api_key=[your_api_key] """ url = "/domains/%s/destroy" % (str(self.id)) data = self._conn.request(url) log.info(data)
def record_destroy(self, record_id=None): """ This method deletes the specified domain record. https://api.digitalocean.com/domains/[domain_id]/records/[record_id]/destroy? client_id=[your_client_id]&api_key=[your_api_key] """ url = "domains/%s/records/%d/destroy" % (str(self.id), record_id) data = self._conn.request(url) log.info(data)
def create_key_pair(self, ssh_key_name=None, dry_run=False): """ Method to cCreate a new key pair for your account. This will create the key pair and store public key on Digital Ocean's servers :type ssh_key_name: string :param ssh_key_name: The name of the new keypair :type dry_run: bool :param dry_run: Set to True if the operation should not actually run. :rtype: :class:`boto.ec2.keypair.KeyPair` :return: The newly created :class:`boto.ec2.keypair.KeyPair`. The material attribute of the new KeyPair object will contain the the unencrypted PEM encoded RSA private key. """ ssh_path = pjoin(expanduser('~'), '.ssh') if not os.path.isdir(ssh_path): os.makedirs(ssh_path) #store key file in ~/.ssh keyfile = pjoin(ssh_path, ssh_key_name) key = RSA.generate(2048,os.urandom) #public key with open(keyfile+'_rsa.pub','w') as f: f.write(key.exportKey('OpenSSH')) public_key = key.exportKey('OpenSSH') os.chmod(keyfile+'_rsa.pub', 0600) #private key with open(keyfile+'_rsa','w') as f: f.write(key.exportKey()) os.chmod(keyfile+'_rsa', 0600) # https://api.digitalocean.com/ssh_keys/new/?name=[ssh_key_name]&ssh_pub_key=[ssh_public_key]& # client_id=[your_client_id]&api_key=[your_api_key] data = self._conn.request("/ssh_keys/new/", name=ssh_key_name, ssh_pub_key=public_key) #include path to newly created file data['ssh_key']['path'] = keyfile+'_rsa' log.info(data['ssh_key']) return data['ssh_key']
def power_on(self): """ This method allows you to power on a previously powered off droplet. https://api.digitalocean.com/droplets/[droplet_id]/power_on/? client_id=[your_client_id]&api_key=[your_api_key] """ url = "/droplets/%s/power_on" % (str(self.id)) data = self._request(url) self.event_id = data["event_id"] log.info("Powering On: %d, Event: %d" % (self.id, self.event_id))
def power_on(self): """ This method allows you to power on a previously powered off droplet. """ # https://api.digitalocean.com/droplets/[droplet_id]/power_on/? # client_id=[your_client_id]&api_key=[your_api_key] url = "/droplets/%s/power_on" % (str(self.id)) data = self._request(url) self.event_id = data['event_id'] log.info("Powering On: %d, Event: %d" % (self.id, self.event_id))
def power_off(self): """ This method allows you to power off a droplet. This will turn off the droplet and then turn it back on. https://api.digitalocean.com/droplets/[droplet_id]/power_off/? client_id=[your_client_id]&api_key=[your_api_key] """ url = "/droplets/%s/power_off" % (str(self.id)) data = self._request(url) self.event_id = data["event_id"] log.info("Powering Off: %d, Event: %d" % (self.id, self.event_id))
def reboot(self): """ This method allows you to reboot a droplet. This is the preferred method to use if a server is not responding. """ # https://api.digitalocean.com/droplets/[droplet_id]/reboot/? # client_id=[your_client_id]&api_key=[your_api_key] url = "/droplets/%s/reboot" % (str(self.id)) data = self._request(url) self.event_id = data['event_id'] log.info("Rebooting: %d, Event: %d" % (self.id, self.event_id))
def get_ssh_key(self, ssh_key_id=None): """ Delete the SSH key from your account. :type ssh_key_id: int :param ssh_key_id: The ID of the public key """ # https://api.digitalocean.com/ssh_keys/[ssh_key_id]/destroy/? # client_id=[your_client_id]&api_key=[your_api_key] url = "/ssh_keys/%d" % (ssh_key_id) data = self._conn.request(url) log.info(data)
def power_off(self): """ This method allows you to power off a droplet. This will turn off the droplet and then turn it back on. """ # https://api.digitalocean.com/droplets/[droplet_id]/power_off/? # client_id=[your_client_id]&api_key=[your_api_key] url = "/droplets/%s/power_off" % (str(self.id)) data = self._request(url) self.event_id = data['event_id'] log.info("Powering Off: %d, Event: %d" % (self.id, self.event_id))
def shutdown(self): """ This method allows you to shutdown a droplet. The droplet will remain in your account. """ # https://api.digitalocean.com/droplets/[droplet_id]/shutdown/ # ?client_id=[your_client_id]&api_key=[your_api_key] url = "/droplets/%s/shutdown" % (str(self.id)) data = self._request(url) self.event_id = data['event_id'] log.info("Shutting Down: %d, Event: %d" % (self.id, self.event_id)) log.info("Droplet remains active in your account")
def get_all_droplets(self, status_check=False): # https://api.digitalocean.com/droplets/?client_id=[your_client_id]&api_key=[your_api_key] log.info("Get All Droplets") data = self._conn.request("/droplets",status_check) if status_check: return data #don't like this but will do for now for drop in data['droplets']: #convert dictionary to droplet objects drop['_client_id'] = self._client_id drop['_api_key'] = self._api_key #convert dictionary to droplet objects return [Droplet(**drop) for drop in data['droplets']]
def destroy(self, image_id=None): """ This method allows you to destroy an image. There is no way to restore a deleted image so be careful and ensure your data is properly backed up. :type image_id: int :param image_id: The ID of the image """ # https://api.digitalocean.com/images/[image_id]/destroy/? # client_id=[your_client_id]&api_key=[your_api_key] url = "/images/%s/destroy" % (str(self.id)) data = self._request(url) log.info(data)
def password_reset(self): """ This method will reset the root password for a droplet. Please be aware that this will reboot the droplet to allow resetting the password. https://api.digitalocean.com/droplets/[droplet_id]/password_reset/? client_id=[your_client_id]&api_key=[your_api_key] """ url = "/droplets/%s/password_reset" % (str(self.id)) data = self._request(url) self.event_id = data["event_id"] log.info("Resetting Password: %d, Event: %d" % (self.id, self.event_id)) log.info("Rebooting Droplet")
def power_cycle(self): """ This method allows you to power cycle a droplet. This will turn off the droplet and then turn it back on. """ # https://api.digitalocean.com/droplets/[droplet_id]/power_cycle/? # client_id=[your_client_id]&api_key=[your_api_key] url = "/droplets/%s/power_cycle" % (str(self.id)) data = self._request(url) self.event_id = data['event_id'] log.info("Power Cycle: %d, Event: %d" % (self.id, self.event_id))
def get_record(self,record_id=None): """ This method returns the specified domain record. :type record_id: int :param record_id: ID of record you are trying to retrieve. https://api.digitalocean.com/domains/[domain_id]/records/[record_id]? client_id=[your_client_id]&api_key=[your_api_key] """ log.info("Getting Record: %d" % (record_id)) url = "/domains/%s/records/%d" % (str(self.id), record_id) data = self._conn.request(url) log.debug(data) return data['record']
def get_all_records(self,table=False): """ This method returns all of your current domain records. https://api.digitalocean.com/domains/[domain_id]/records? client_id=[your_client_id]&api_key=[your_api_key] """ log.info("Getting Records") url = "/domains/%s/records" % (str(self.id)) data = self._conn.request(url) log.debug(data) if table: self._pprint_table(data['records']) return data['records']
def password_reset(self): """ This method will reset the root password for a droplet. Please be aware that this will reboot the droplet to allow resetting the password. """ # https://api.digitalocean.com/droplets/[droplet_id]/password_reset/? # client_id=[your_client_id]&api_key=[your_api_key] url = "/droplets/%s/password_reset" % (str(self.id)) data = self._request(url) self.event_id = data['event_id'] log.info("Resetting Password: %d, Event: %d" % (self.id, self.event_id)) log.info("Rebooting Droplet")
def get_record(self, record_id=None): """ This method returns the specified domain record. :type record_id: int :param record_id: ID of record you are trying to retrieve. https://api.digitalocean.com/domains/[domain_id]/records/[record_id]? client_id=[your_client_id]&api_key=[your_api_key] """ log.info("Getting Record: %d" % (record_id)) url = "/domains/%s/records/%d" % (str(self.id), record_id) data = self._conn.request(url) log.debug(data) return data['record']
def rename(self, name=None): """ This method renames the droplet to the specified name. :type name: str :param name: Name of the new droplet https://api.digitalocean.com/droplets/[droplet_id]/rename/? client_id=[your_client_id]&api_key=[your_api_key]&name=[name] """ url = "/droplets/%s/rename" % (str(self.id)) data = self._request(url, name=name) self.event_id = data["event_id"] log.info("Renaming: %d To: %s Event: %d" % (self.id, name, self.event_id)) self.update()
def get_all_records(self, table=False): """ This method returns all of your current domain records. https://api.digitalocean.com/domains/[domain_id]/records? client_id=[your_client_id]&api_key=[your_api_key] """ log.info("Getting Records") url = "/domains/%s/records" % (str(self.id)) data = self._conn.request(url) log.debug(data) if table: self._pprint_table(data['records']) return data['records']
def destroy(self, scrub_data=1): """ This method destroys one of your droplets - this is irreversible. :type scrub_data: bool :param scrub_data: An optional bool which will strictly write 0s to your prior partition to ensure that all data is completely erased. True by default """ # https://api.digitalocean.com/droplets/[droplet_id]/destroy/? # client_id=[your_client_id]&api_key=[your_api_key] url = "/droplets/%s/destroy" % (str(self.id)) data = self._request(url,scrub_data=scrub_data) self.event_id = data['event_id'] log.info("Destroying: %d, Event: %d" % (self.id, self.event_id))
def destroy(self, scrub_data=1): """ This method destroys one of your droplets - this is irreversible. :type scrub_data: bool :param scrub_data: An optional bool which will strictly write 0s to your prior partition to ensure that all data is completely erased. True by default """ # https://api.digitalocean.com/droplets/[droplet_id]/destroy/? # client_id=[your_client_id]&api_key=[your_api_key] url = "/droplets/%s/destroy" % (str(self.id)) data = self._request(url, scrub_data=scrub_data) self.event_id = data['event_id'] log.info("Destroying: %d, Event: %d" % (self.id, self.event_id))
def edit_record(self,record_id=None, record_type=None, data=None, name=None,priority=None,port=None,weight=None, ): """ This method edits an existing domain record. :type record_id: int :param record_id: ID of record you are trying to retrieve. :type record_type: String :param record_type: the type of record you would like to create. 'A', 'CNAME', 'NS', 'TXT', 'MX' or 'SRV'. :type data: String :param data: This is the value of the record. :type name: String :param name: Required for 'A', 'CNAME', 'TXT' and 'SRV' records otherwise optional :type priority: int :param priority: required for 'SRV' and 'MX' records otherwise optional :type port: int :param port: required for 'SRV' records otherwise optional :type weight: int :param weight: required for 'SRV' records. otherwise optional https://api.digitalocean.com/domains/[domain_id]/records/[record_id]/edit? client_id=[your_client_id]&api_key=[your_api_key] """ log.info("Editing Record: %d" % (record_id)) url = "/domains/%s/records/%d/edit" % (str(self.id), record_id) data = self._conn.request(url,record_type=record_type, data=data,name=name,priority=priority, port=port,weight=weight) log.debug(data) return data['record']
def rename(self, name=None): """ This method renames the droplet to the specified name. :type name: str :param name: Name of the new droplet """ # https://api.digitalocean.com/droplets/[droplet_id]/rename/? # client_id=[your_client_id]&api_key=[your_api_key]&name=[name] url = "/droplets/%s/rename" % (str(self.id)) data = self._request(url, name=name) self.event_id = data['event_id'] log.info("Renaming: %d To: %s Event: %d" % (self.id, name, self.event_id)) self.update()
def transfer_image(self, region_id=None): """ This method allows you to transfer an image to a specified region. :type image_id: int :param image_id: The ID of the image :type region_id: int :param region_id: The ID of the region to which you would like to transfer. """ # https://api.digitalocean.com/images/[image_id]/transfer/? # client_id=[your_client_id]&api_key=[your_api_key]®ion_id=[region_id] url = "/images/%s/transfer" % (str(self.id)) data = self._request(url,region_id=region_id) self.event_id = data['event_id'] log.info(data)
def transfer_image(self, region_id=None): """ This method allows you to transfer an image to a specified region. :type image_id: int :param image_id: The ID of the image :type region_id: int :param region_id: The ID of the region to which you would like to transfer. """ # https://api.digitalocean.com/images/[image_id]/transfer/? # client_id=[your_client_id]&api_key=[your_api_key]®ion_id=[region_id] url = "/images/%s/transfer" % (str(self.id)) data = self._request(url, region_id=region_id) self.event_id = data['event_id'] log.info(data)
def set_backups(self, flag=True): """ This method enables/disables automatic backups which run in the background daily to backup your droplet's data. :type flag: bool :param scrub_data: A bool which enables/disables backups https://api.digitalocean.com/droplets/[droplet_id]/enable_backups/? client_id=[your_client_id]&api_key=[your_api_key] """ backup_setting = "enable_backups" if flag else "disable_backups" url = "/droplets/%s/%s" % (str(self.id), backup_setting) data = self._request(url) self.event_id = data["event_id"] log.info("Destroying: %d, Event: %d" % (self.id, self.event_id))
def set_backups(self, flag=True): """ This method enables/disables automatic backups which run in the background daily to backup your droplet's data. :type flag: bool :param scrub_data: A bool which enables/disables backups """ # https://api.digitalocean.com/droplets/[droplet_id]/enable_backups/? # client_id=[your_client_id]&api_key=[your_api_key] backup_setting = "enable_backups" if flag else "disable_backups" url = "/droplets/%s/%s" % (str(self.id), backup_setting) data = self._request(url) self.event_id = data['event_id'] log.info("Destroying: %d, Event: %d" % (self.id, self.event_id))
def resize(self, size=None): """ This method allows you to resize a specific droplet to a different size. This will affect the number of processors and memory allocated to the droplet. :type size: int :param size: The new SIZE id of the droplet REQUIRES SNAPSHOT OF DROPLET """ # https://api.digitalocean.com/droplets/[droplet_id]/resize/?size_id=[size_id]& # client_id=[your_client_id]&api_key=[your_api_key] url = "/droplets/%s/resize" % (str(self.id)) data = self._request(url, size=size) self.event_id = data['event_id'] log.info("Resizing Droplet: %d, Event: %d" % (self.id, self.event_id)) log.info("Rebooting Droplet")
def resize(self,size=None): """ This method allows you to resize a specific droplet to a different size. This will affect the number of processors and memory allocated to the droplet. :type size: int :param size: The new SIZE id of the droplet REQUIRES SNAPSHOT OF DROPLET """ # https://api.digitalocean.com/droplets/[droplet_id]/resize/?size_id=[size_id]& # client_id=[your_client_id]&api_key=[your_api_key] url = "/droplets/%s/resize" % (str(self.id)) data = self._request(url,size=size) self.event_id = data['event_id'] log.info("Resizing Droplet: %d, Event: %d" % (self.id, self.event_id)) log.info("Rebooting Droplet")
def request(self, event, status_check=False, **kwds): if 'client_id' not in kwds: kwds['client_id'] = self.__client_id if 'api_key' not in kwds: kwds['api_key'] = self.__api_key headers = { 'User-Agent': 'doto/client' } for key, value in kwds.iteritems(): log.debug("%s = %s" % (key, value)) response = requests.get(BASEURL+event,headers=headers,params=kwds) log.info('Getting '+event) log.debug(response.url) if response.status_code == 200: data = response.json() log.debug(data) if data['status'] == 'ERROR': log.debug("Error with request: %s" % (data['message'])) error = "MSG: %s" % (data['message']) raise DOError(error) if status_check: return response.status_code return data else: #error # fixme data = dict(message="fake message") error = "Status code: %d MSG: %s" % (response.status_code, data['message']) raise DOError(error)
def get_image(self, image_id=None): """ This method displays the attributes of an image. :type image_id: int :param image_id: The ID of the image """ # https://api.digitalocean.com/images/[image_id]/? # client_id=[your_client_id]&api_key=[your_api_key] url = "/images/%d" % (image_id) data = self._conn.request(url) log.info(data) #don't like this but will do for now data['image']['_client_id'] = self._client_id data['image']['_api_key'] = self._api_key return Image(**data['image'])
def rebuild(self, image_id=None): """ This method allows you to reinstall a droplet with a default image. This is useful if you want to start again but retain the same IP address for your droplet. :type image_id: int :param image_id: ID of the image you would like to use to rebuild your droplet with """ # https://api.digitalocean.com/droplets/[droplet_id]/rebuild/?image_id=[image_id]& # client_id=[your_client_id]&api_key=[your_api_key] url = "/droplets/%s/rebuild" % (str(self.id)) data = self._request(url, image_id=image_id) self.event_id = data['event_id'] log.info("Rebuild: %d With: %d Event: %d" % (self.id, image_id, self.event_id)) self.update()
def restore(self, image_id=None): """ This method allows you to restore a droplet with a previous image or snapshot. This will be a mirror copy of the image or snapshot to your droplet. Be sure you have backed up any necessary information prior to restore. :type image_id: int :param image_id: ID of the image you would like to use to rebuild your droplet with """ # https://api.digitalocean.com/droplets/[droplet_id]/restore/?image_id=[image_id]& # client_id=[your_client_id]&api_key=[your_api_key] url = "/droplets/%s/restore" % (str(self.id)) data = self._request(url, image_id=image_id) self.event_id = data['event_id'] log.info("Restoring: %d With: %d Event: %d" % (self.id, image_id, self.event_id)) self.update()