本文整理汇总了Python中dopy.manager.DoManager.show_droplet方法的典型用法代码示例。如果您正苦于以下问题:Python DoManager.show_droplet方法的具体用法?Python DoManager.show_droplet怎么用?Python DoManager.show_droplet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dopy.manager.DoManager
的用法示例。
在下文中一共展示了DoManager.show_droplet方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_droplet_variables_for_host
# 需要导入模块: from dopy.manager import DoManager [as 别名]
# 或者: from dopy.manager.DoManager import show_droplet [as 别名]
def load_droplet_variables_for_host(self):
'''Generate a JSON response to a --host call'''
host = self.to_safe(str(self.args.host))
if not host in self.index['host_to_droplet']:
# try updating cache
if not self.args.force_cache:
self.load_all_data_from_digital_ocean()
if not host in self.index['host_to_droplet']:
# host might not exist anymore
return {}
droplet = None
if self.cache_refreshed:
for drop in self.data['droplets']:
if drop['ip_address'] == host:
droplet = self.sanitize_dict(drop)
break
else:
# Cache wasn't refreshed this run, so hit DigitalOcean API
manager = DoManager(None, self.api_token, api_version=2)
droplet_id = self.index['host_to_droplet'][host]
droplet = self.sanitize_dict(manager.show_droplet(droplet_id))
if not droplet:
return {}
# Put all the information in a 'do_' namespace
info = {}
for k, v in droplet.items():
info['do_'+k] = v
return info
示例2: load_droplet_variables_for_host
# 需要导入模块: from dopy.manager import DoManager [as 别名]
# 或者: from dopy.manager.DoManager import show_droplet [as 别名]
def load_droplet_variables_for_host(self):
'''Generate a JSON response to a --host call'''
host = self.to_safe(str(self.args.host))
if not host in self.index['host_to_droplet']:
# try updating cache
if not self.args.force_cache:
self.load_all_data_from_digital_ocean()
if not host in self.index['host_to_droplet']:
# host might not exist anymore
return {}
droplet = None
if self.cache_refreshed:
for drop in self.data['droplets']:
if drop['ip_address'] == host:
droplet = self.sanitize_dict(drop)
break
else:
# Cache wasn't refreshed this run, so hit DigitalOcean API
manager = DoManager(self.client_id, self.api_key)
droplet_id = self.index['host_to_droplet'][host]
droplet = self.sanitize_dict(manager.show_droplet(droplet_id))
if not droplet:
return {}
# Put all the information in a 'do_' namespace
info = {}
for k, v in droplet.items():
info['do_'+k] = v
# Generate user-friendly variables (i.e. not the ID's)
if droplet.has_key('region_id'):
info['do_region'] = self.index['region_to_name'].get(droplet['region_id'])
if droplet.has_key('size_id'):
info['do_size'] = self.index['size_to_name'].get(droplet['size_id'])
if droplet.has_key('image_id'):
info['do_image'] = self.index['image_to_name'].get(droplet['image_id'])
info['do_distro'] = self.index['image_to_distro'].get(droplet['image_id'])
return info
示例3: DigitalOceanInventory
# 需要导入模块: from dopy.manager import DoManager [as 别名]
# 或者: from dopy.manager.DoManager import show_droplet [as 别名]
#.........这里部分代码省略.........
# add all droplets by id and name
for droplet in self.data['droplets']:
#when using private_networking, the API reports the private one in "ip_address".
if 'private_networking' in droplet['features'] and not self.use_private_network:
for net in droplet['networks']['v4']:
if net['type']=='public':
dest=net['ip_address']
else:
continue
else:
dest = droplet['ip_address']
dest = { 'hosts': [ dest ], 'vars': self.group_variables }
self.inventory[droplet['id']] = dest
self.inventory[droplet['name']] = dest
self.inventory['region_' + droplet['region']['slug']] = dest
self.inventory['image_' + str(droplet['image']['id'])] = dest
self.inventory['size_' + droplet['size']['slug']] = dest
image_slug = droplet['image']['slug']
if image_slug:
self.inventory['image_' + self.to_safe(image_slug)] = dest
else:
image_name = droplet['image']['name']
if image_name:
self.inventory['image_' + self.to_safe(image_name)] = dest
self.inventory['distro_' + self.to_safe(droplet['image']['distribution'])] = dest
self.inventory['status_' + droplet['status']] = dest
def load_droplet_variables_for_host(self):
'''Generate a JSON response to a --host call'''
host = int(self.args.host)
droplet = self.manager.show_droplet(host)
# Put all the information in a 'do_' namespace
info = {}
for k, v in droplet.items():
info['do_'+k] = v
return {'droplet': info}
###########################################################################
# Cache Management
###########################################################################
def is_cache_valid(self):
''' Determines if the cache files have expired, or if it is still valid '''
if os.path.isfile(self.cache_filename):
mod_time = os.path.getmtime(self.cache_filename)
current_time = time()
if (mod_time + self.cache_max_age) > current_time:
return True
return False
def load_from_cache(self):
''' Reads the data from the cache file and assigns it to member variables as Python Objects'''
try:
cache = open(self.cache_filename, 'r')
json_data = cache.read()
cache.close()
data = json.loads(json_data)
except IOError:
data = {'data': {}, 'inventory': {}}
self.data = data['data']
self.inventory = data['inventory']
def write_to_cache(self):
''' Writes data in JSON format to a file '''
data = { 'data': self.data, 'inventory': self.inventory }
json_data = json.dumps(data, sort_keys=True, indent=2)
cache = open(self.cache_filename, 'w')
cache.write(json_data)
cache.close()
###########################################################################
# Utilities
###########################################################################
def push(self, my_dict, key, element):
''' Pushed an element onto an array that may not have been defined in the dict '''
if key in my_dict:
my_dict[key].append(element)
else:
my_dict[key] = [element]
def to_safe(self, word):
''' Converts 'bad' characters in a string to underscores so they can be used as Ansible groups '''
return re.sub("[^A-Za-z0-9\-\.]", "_", word)
示例4: DigitalOceanInventory
# 需要导入模块: from dopy.manager import DoManager [as 别名]
# 或者: from dopy.manager.DoManager import show_droplet [as 别名]
#.........这里部分代码省略.........
if resource == "images" or resource is None:
self.data["images"] = self.manager.all_images(filter=None)
self.cache_refreshed = True
if resource == "sizes" or resource is None:
self.data["sizes"] = self.manager.sizes()
self.cache_refreshed = True
if resource == "ssh_keys" or resource is None:
self.data["ssh_keys"] = self.manager.all_ssh_keys()
self.cache_refreshed = True
if resource == "domains" or resource is None:
self.data["domains"] = self.manager.all_domains()
self.cache_refreshed = True
def build_inventory(self):
"""Build Ansible inventory of droplets"""
self.inventory = {}
# add all droplets by id and name
for droplet in self.data["droplets"]:
dest = droplet["ip_address"]
self.inventory[droplet["id"]] = [dest]
self.push(self.inventory, droplet["name"], dest)
self.push(self.inventory, "region_" + droplet["region"]["slug"], dest)
self.push(self.inventory, "image_" + str(droplet["image"]["id"]), dest)
self.push(self.inventory, "size_" + droplet["size"]["slug"], dest)
image_slug = droplet["image"]["slug"]
if image_slug:
self.push(self.inventory, "image_" + self.to_safe(image_slug), dest)
else:
image_name = droplet["image"]["name"]
if image_name:
self.push(self.inventory, "image_" + self.to_safe(image_name), dest)
self.push(self.inventory, "distro_" + self.to_safe(droplet["image"]["distribution"]), dest)
self.push(self.inventory, "status_" + droplet["status"], dest)
def load_droplet_variables_for_host(self):
"""Generate a JSON response to a --host call"""
host = int(self.args.host)
droplet = self.manager.show_droplet(host)
# Put all the information in a 'do_' namespace
info = {}
for k, v in droplet.items():
info["do_" + k] = v
return {"droplet": info}
###########################################################################
# Cache Management
###########################################################################
def is_cache_valid(self):
""" Determines if the cache files have expired, or if it is still valid """
if os.path.isfile(self.cache_filename):
mod_time = os.path.getmtime(self.cache_filename)
current_time = time()
if (mod_time + self.cache_max_age) > current_time:
return True
return False
def load_from_cache(self):
""" Reads the data from the cache file and assigns it to member variables as Python Objects"""
try:
cache = open(self.cache_filename, "r")
json_data = cache.read()
cache.close()
data = json.loads(json_data)
except IOError:
data = {"data": {}, "inventory": {}}
self.data = data["data"]
self.inventory = data["inventory"]
def write_to_cache(self):
""" Writes data in JSON format to a file """
data = {"data": self.data, "inventory": self.inventory}
json_data = json.dumps(data, sort_keys=True, indent=2)
cache = open(self.cache_filename, "w")
cache.write(json_data)
cache.close()
###########################################################################
# Utilities
###########################################################################
def push(self, my_dict, key, element):
""" Pushed an element onto an array that may not have been defined in the dict """
if key in my_dict:
my_dict[key].append(element)
else:
my_dict[key] = [element]
def to_safe(self, word):
""" Converts 'bad' characters in a string to underscores so they can be used as Ansible groups """
return re.sub("[^A-Za-z0-9\-\.]", "_", word)