本文整理汇总了Python中dopy.manager.DoManager.all_active_droplets方法的典型用法代码示例。如果您正苦于以下问题:Python DoManager.all_active_droplets方法的具体用法?Python DoManager.all_active_droplets怎么用?Python DoManager.all_active_droplets使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dopy.manager.DoManager
的用法示例。
在下文中一共展示了DoManager.all_active_droplets方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_droplets_from_digital_ocean
# 需要导入模块: from dopy.manager import DoManager [as 别名]
# 或者: from dopy.manager.DoManager import all_active_droplets [as 别名]
def load_droplets_from_digital_ocean(self):
''' Use dopy to get droplet information from DigitalOcean and save data in cache files '''
manager = DoManager(self.client_id, self.api_key)
self.data['droplets'] = self.sanitize_list(manager.all_active_droplets())
self.index['host_to_droplet'] = self.build_index(self.data['droplets'], 'ip_address', 'id', False)
self.build_inventory()
self.write_to_cache()
示例2: load_droplets_from_digital_ocean
# 需要导入模块: from dopy.manager import DoManager [as 别名]
# 或者: from dopy.manager.DoManager import all_active_droplets [as 别名]
def load_droplets_from_digital_ocean(self):
''' Use dopy to get droplet information from DigitalOcean and save data in cache files '''
manager = DoManager(None, self.access_token, api_version=2)
self.data['droplets'] = manager.all_active_droplets()
self.index['host_to_droplet'] = self.build_index(self.data['droplets'], 'ip_address', 'id', False)
self.build_inventory()
self.write_to_cache()
示例3: DigitalOceanBase
# 需要导入模块: from dopy.manager import DoManager [as 别名]
# 或者: from dopy.manager.DoManager import all_active_droplets [as 别名]
class DigitalOceanBase(Inventory):
address_field = 'ip_address'
def __init__(self, settings_file='digital_ocean.ini'):
super(DigitalOceanBase, self).__init__()
self.config = Settings(prefix='DO', section='digital_ocean',
filename=settings_file)
self.data_params = {}
self.do = DoManager(self.config.get('client_id'),
self.config.get('api_key'))
@file_cache('/tmp/ansible-droplets-cache.json')
def get_data(self):
if not self.data:
self.data = self.do.all_active_droplets()
return self.data
@file_cache('/tmp/ansible-data-params-cache.json', timeout=3600)
def get_data_params(self):
if not self.data_params:
def _convert(data):
ret = {}
for datum in data:
# Ensure keys are always strings because JSON
# strings are always strings
ret[str(datum['id'])] = datum
return ret
self.data_params['regions'] = _convert(self.do.all_regions())
self.data_params['images'] = _convert(self.do.all_images())
self.data_params['ssh_keys'] = _convert(self.do.all_ssh_keys())
self.data_params['sizes'] = _convert(self.do.sizes())
return self.data_params
def get_data_param(self, host, part, find_key, display_template):
try:
datum = self.get_data_params()[part][str(host[find_key])]
except KeyError:
pass
else:
return (display_template.format(**datum), self.get_address(host))
示例4: load_all_data_from_digital_ocean
# 需要导入模块: from dopy.manager import DoManager [as 别名]
# 或者: from dopy.manager.DoManager import all_active_droplets [as 别名]
def load_all_data_from_digital_ocean(self):
''' Use dopy to get all the information from DigitalOcean and save data in cache files '''
manager = DoManager(None, self.access_token, api_version=2)
self.data = {}
self.data['droplets'] = manager.all_active_droplets()
self.data['regions'] = manager.all_regions()
self.data['images'] = manager.all_images(filter=None)
self.data['sizes'] = manager.sizes()
self.data['ssh_keys'] = manager.all_ssh_keys()
self.data['domains'] = manager.all_domains()
self.index = {}
self.index['region_to_name'] = self.build_index(self.data['regions'], 'name', 'slug')
self.index['size_to_name'] = self.build_index(self.data['sizes'], 'memory', 'slug')
self.index['image_to_name'] = self.build_index(self.data['images'], 'id', 'name')
self.index['image_to_distro'] = self.build_index(self.data['images'], 'id', 'distribution')
self.index['host_to_droplet'] = self.build_index(self.data['droplets'], 'ip_address', 'id', False)
self.build_inventory()
self.write_to_cache()
示例5: get_config
# 需要导入模块: from dopy.manager import DoManager [as 别名]
# 或者: from dopy.manager.DoManager import all_active_droplets [as 别名]
CHECK_FREQ = get_config(config, 'do_clean', 'check_freq', None, 300, integer=True)
# DONT touch those Droplets (use droplet ids). ex.
# DONT = [ 123456, 654321 ]
RAW_DONT = get_config(config, 'do_clean', 'dont', None, '')
try:
DONT = [ int(val.strip()) for val in RAW_DONT.split(',') if val]
except Exception as e:
print "Invalid format for DONT - %s" % e
sys.exit(1)
if __name__ == '__main__':
do = DoManager(client_id, api_key, api_version=api_version)
while True:
try:
droplets = do.all_active_droplets()
for droplet in droplets:
if droplet.get('id') in DONT:
# Don't kill meeeeee
print 'Not killing %s (%s) - part of the DONT' % (droplet.get('name'), droplet.get('id'))
continue
created = datetime.strptime(droplet.get('created_at'), "%Y-%m-%dT%H:%M:%SZ")
now = datetime.now()
diff = now - created
if diff.seconds > MAX_AGE:
print 'gonna destroy droplet - %s (%s: %s)' % (droplet.get('name'), droplet.get('id'), droplet.get('ip_address'))
do.destroy_droplet(droplet.get('id'))
print 'destroyed droplet - %s (%s: %s)' % (droplet.get('name'), droplet.get('id'), droplet.get('ip_address'))
else:
print 'droplet %s (%s: %s) is less than %s sec (age: %s sec)' % (droplet.get('name'), droplet.get('id'), droplet.get('ip_address'), MAX_AGE, diff.seconds)
print '.'
示例6: DigitalOceanInventory
# 需要导入模块: from dopy.manager import DoManager [as 别名]
# 或者: from dopy.manager.DoManager import all_active_droplets [as 别名]
#.........这里部分代码省略.........
help='Force refresh of cache by making API requests to DigitalOcean (default: False - use cache files)')
parser.add_argument('--env','-e', action='store_true', help='Display DO_API_TOKEN')
parser.add_argument('--api-token','-a', action='store', help='DigitalOcean API Token')
self.args = parser.parse_args()
if self.args.api_token:
self.api_token = self.args.api_token
# Make --list default if none of the other commands are specified
if (not self.args.droplets and not self.args.regions and
not self.args.images and not self.args.sizes and
not self.args.ssh_keys and not self.args.domains and
not self.args.all and not self.args.host):
self.args.list = True
###########################################################################
# Data Management
###########################################################################
def load_from_digital_ocean(self, resource=None):
'''Get JSON from DigitalOcean API'''
if self.args.force_cache:
return
# We always get fresh droplets
if self.is_cache_valid() and not (resource=='droplets' or resource is None):
return
if self.args.refresh_cache:
resource=None
if resource == 'droplets' or resource is None:
self.data['droplets'] = self.manager.all_active_droplets()
self.cache_refreshed = True
if resource == 'regions' or resource is None:
self.data['regions'] = self.manager.all_regions()
self.cache_refreshed = True
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']:
#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:
示例7: DigitalOceanInventory
# 需要导入模块: from dopy.manager import DoManager [as 别名]
# 或者: from dopy.manager.DoManager import all_active_droplets [as 别名]
#.........这里部分代码省略.........
self.args = parser.parse_args()
if self.args.api_token:
self.api_token = self.args.api_token
# Make --list default if none of the other commands are specified
if (
not self.args.droplets
and not self.args.regions
and not self.args.images
and not self.args.sizes
and not self.args.ssh_keys
and not self.args.domains
and not self.args.all
and not self.args.host
):
self.args.list = True
###########################################################################
# Data Management
###########################################################################
def load_from_digital_ocean(self, resource=None):
"""Get JSON from DigitalOcean API"""
if self.args.force_cache:
return
# We always get fresh droplets
if self.is_cache_valid() and not (resource == "droplets" or resource is None):
return
if self.args.refresh_cache:
resource = None
if resource == "droplets" or resource is None:
self.data["droplets"] = self.manager.all_active_droplets()
self.cache_refreshed = True
if resource == "regions" or resource is None:
self.data["regions"] = self.manager.all_regions()
self.cache_refreshed = True
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"]