class Vultr(object): """Handle all communication with the Vultr API.""" def __init__(self, api_key): """Initialize the Vultr connection.""" from vultr import Vultr as VultrAPI self._api_key = api_key self.data = None self.api = VultrAPI(self._api_key) @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self): """Use the data from Vultr API.""" self.data = self.api.server_list() def _force_update(self): """Use the data from Vultr API.""" self.data = self.api.server_list() def halt(self, subscription): """Halt a subscription (hard power off).""" self.api.server_halt(subscription) self._force_update() def start(self, subscription): """Start a subscription.""" self.api.server_start(subscription) self._force_update()
class Vultr: """Handle all communication with the Vultr API.""" def __init__(self, api_key): """Initialize the Vultr connection.""" self._api_key = api_key self.data = None self.api = VultrAPI(self._api_key) @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self): """Use the data from Vultr API.""" self.data = self.api.server_list() def _force_update(self): """Use the data from Vultr API.""" self.data = self.api.server_list() def halt(self, subscription): """Halt a subscription (hard power off).""" self.api.server_halt(subscription) self._force_update() def start(self, subscription): """Start a subscription.""" self.api.server_start(subscription) self._force_update()
def create(self, defn, check, allow_reboot, allow_recreate): self.set_common_state(defn) if self.subid is not None: return self.log_start("creating instance ...") self.log("dcid: " + str(defn.dcid)) self.log("osid: " + str(defn.osid)) self.log("vpsplanid: " + str(defn.vpsplanid)) self.log("snapshotid: " + str(defn.snapshotid)) self.log("label: " + str(defn.label)) vultr = Vultr(self.get_api_key()) snapshots = vultr.snapshot_list() if defn.snapshotid not in snapshots: raise Exception( "Unexpected Error: snapshot {} does not exist".format( defn.snapshotid)) server_create_output = vultr.server_create( dcid=defn.dcid, osid=defn.osid, vpsplanid=defn.vpsplanid, snapshotid=defn.snapshotid, enable_ipv6='yes', enable_private_network='yes', label=defn.label) subid = server_create_output['SUBID'] self.log("instance id: " + subid) server_info = vultr.server_list()[subid] while server_info[ 'status'] == 'pending' or server_info['server_state'] != 'ok': server_info = vultr.server_list()[subid] time.sleep(1) self.log_continue("[status: {} state: {}] ".format( server_info['status'], server_info['server_state'])) if server_info['status'] == 'active' and server_info[ 'server_state'] == 'ok': # vultr sets ok before locked when restoring snapshot. Need to make sure we're really ready. time.sleep(10) server_info = vultr.server_list()[subid] if server_info['status'] != 'active' or server_info[ 'server_state'] != 'ok': raise Exception("unexpected status: {}/{}".format( server_info['status'], server_info['server_state'])) self.subid = subid self.label = server_info['label'] self.log_start("generating new SSH keypair... ") key_name = "NixOps client key for {0}".format(self.subid) self._ssh_private_key, self._ssh_public_key = \ nixops.util.create_key_pair(key_name=key_name) self.public_ipv4 = server_info['main_ip'] self.log_end("{}".format(self.public_ipv4)) self.default_gateway = server_info['gateway_v4'] self.netmask = server_info['netmask_v4'] self.wait_for_ssh()