You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

541 lines
20 KiB

  1. #!/usr/bin/env python
  2. """
  3. DigitalOcean external inventory script
  4. ======================================
  5. Generates Ansible inventory of DigitalOcean Droplets.
  6. In addition to the --list and --host options used by Ansible, there are options
  7. for generating JSON of other DigitalOcean data. This is useful when creating
  8. droplets. For example, --regions will return all the DigitalOcean Regions.
  9. This information can also be easily found in the cache file, whose default
  10. location is /tmp/ansible-digital_ocean.cache).
  11. The --pretty (-p) option pretty-prints the output for better human readability.
  12. ----
  13. Although the cache stores all the information received from DigitalOcean,
  14. the cache is not used for current droplet information (in --list, --host,
  15. --all, and --droplets). This is so that accurate droplet information is always
  16. found. You can force this script to use the cache with --force-cache.
  17. ----
  18. Configuration is read from `digital_ocean.ini`, then from environment variables,
  19. and then from command-line arguments.
  20. Most notably, the DigitalOcean API Token must be specified. It can be specified
  21. in the INI file or with the following environment variables:
  22. export DO_API_TOKEN='abc123' or
  23. export DO_API_KEY='abc123'
  24. Alternatively, it can be passed on the command-line with --api-token.
  25. If you specify DigitalOcean credentials in the INI file, a handy way to
  26. get them into your environment (e.g., to use the digital_ocean module)
  27. is to use the output of the --env option with export:
  28. export $(digital_ocean.py --env)
  29. ----
  30. The following groups are generated from --list:
  31. - ID (droplet ID)
  32. - NAME (droplet NAME)
  33. - digital_ocean
  34. - image_ID
  35. - image_NAME
  36. - distro_NAME (distribution NAME from image)
  37. - region_NAME
  38. - size_NAME
  39. - status_STATUS
  40. For each host, the following variables are registered:
  41. - do_backup_ids
  42. - do_created_at
  43. - do_disk
  44. - do_features - list
  45. - do_id
  46. - do_image - object
  47. - do_ip_address
  48. - do_private_ip_address
  49. - do_kernel - object
  50. - do_locked
  51. - do_memory
  52. - do_name
  53. - do_networks - object
  54. - do_next_backup_window
  55. - do_region - object
  56. - do_size - object
  57. - do_size_slug
  58. - do_snapshot_ids - list
  59. - do_status
  60. - do_tags
  61. - do_vcpus
  62. - do_volume_ids
  63. -----
  64. ```
  65. usage: digital_ocean.py [-h] [--list] [--host HOST] [--all] [--droplets]
  66. [--regions] [--images] [--sizes] [--ssh-keys]
  67. [--domains] [--tags] [--pretty]
  68. [--cache-path CACHE_PATH]
  69. [--cache-max_age CACHE_MAX_AGE] [--force-cache]
  70. [--refresh-cache] [--env] [--api-token API_TOKEN]
  71. Produce an Ansible Inventory file based on DigitalOcean credentials
  72. optional arguments:
  73. -h, --help show this help message and exit
  74. --list List all active Droplets as Ansible inventory
  75. (default: True)
  76. --host HOST Get all Ansible inventory variables about a specific
  77. Droplet
  78. --all List all DigitalOcean information as JSON
  79. --droplets, -d List Droplets as JSON
  80. --regions List Regions as JSON
  81. --images List Images as JSON
  82. --sizes List Sizes as JSON
  83. --ssh-keys List SSH keys as JSON
  84. --domains List Domains as JSON
  85. --tags List Tags as JSON
  86. --pretty, -p Pretty-print results
  87. --cache-path CACHE_PATH
  88. Path to the cache files (default: .)
  89. --cache-max_age CACHE_MAX_AGE
  90. Maximum age of the cached items (default: 0)
  91. --force-cache Only use data from the cache
  92. --refresh-cache, -r Force refresh of cache by making API requests to
  93. DigitalOcean (default: False - use cache files)
  94. --env, -e Display DO_API_TOKEN
  95. --api-token API_TOKEN, -a API_TOKEN
  96. DigitalOcean API Token
  97. ```
  98. """
  99. # (c) 2013, Evan Wies <evan@neomantra.net>
  100. # (c) 2017, Ansible Project
  101. # (c) 2017, Abhijeet Kasurde <akasurde@redhat.com>
  102. #
  103. # Inspired by the EC2 inventory plugin:
  104. # https://github.com/ansible/ansible/blob/devel/contrib/inventory/ec2.py
  105. #
  106. # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
  107. from __future__ import (absolute_import, division, print_function)
  108. __metaclass__ = type
  109. ######################################################################
  110. import argparse
  111. import ast
  112. import os
  113. import re
  114. import requests
  115. import sys
  116. from time import time
  117. try:
  118. import ConfigParser
  119. except ImportError:
  120. import configparser as ConfigParser
  121. import json
  122. class DoManager:
  123. def __init__(self, api_token):
  124. self.api_token = api_token
  125. self.api_endpoint = 'https://api.digitalocean.com/v2'
  126. self.headers = {'Authorization': 'Bearer {0}'.format(self.api_token),
  127. 'Content-type': 'application/json'}
  128. self.timeout = 60
  129. def _url_builder(self, path):
  130. if path[0] == '/':
  131. path = path[1:]
  132. return '%s/%s' % (self.api_endpoint, path)
  133. def send(self, url, method='GET', data=None):
  134. url = self._url_builder(url)
  135. data = json.dumps(data)
  136. try:
  137. if method == 'GET':
  138. resp_data = {}
  139. incomplete = True
  140. while incomplete:
  141. resp = requests.get(url, data=data, headers=self.headers, timeout=self.timeout)
  142. json_resp = resp.json()
  143. for key, value in json_resp.items():
  144. if isinstance(value, list) and key in resp_data:
  145. resp_data[key] += value
  146. else:
  147. resp_data[key] = value
  148. try:
  149. url = json_resp['links']['pages']['next']
  150. except KeyError:
  151. incomplete = False
  152. except ValueError as e:
  153. sys.exit("Unable to parse result from %s: %s" % (url, e))
  154. return resp_data
  155. def all_active_droplets(self):
  156. resp = self.send('droplets/')
  157. return resp['droplets']
  158. def all_regions(self):
  159. resp = self.send('regions/')
  160. return resp['regions']
  161. def all_images(self, filter_name='global'):
  162. params = {'filter': filter_name}
  163. resp = self.send('images/', data=params)
  164. return resp['images']
  165. def sizes(self):
  166. resp = self.send('sizes/')
  167. return resp['sizes']
  168. def all_ssh_keys(self):
  169. resp = self.send('account/keys')
  170. return resp['ssh_keys']
  171. def all_domains(self):
  172. resp = self.send('domains/')
  173. return resp['domains']
  174. def show_droplet(self, droplet_id):
  175. resp = self.send('droplets/%s' % droplet_id)
  176. return resp['droplet']
  177. def all_tags(self):
  178. resp = self.send('tags')
  179. return resp['tags']
  180. class DigitalOceanInventory(object):
  181. ###########################################################################
  182. # Main execution path
  183. ###########################################################################
  184. def __init__(self):
  185. """Main execution path """
  186. # DigitalOceanInventory data
  187. self.data = {} # All DigitalOcean data
  188. self.inventory = {} # Ansible Inventory
  189. # Define defaults
  190. self.cache_path = '.'
  191. self.cache_max_age = 0
  192. self.use_private_network = False
  193. self.group_variables = {}
  194. # Read settings, environment variables, and CLI arguments
  195. self.read_settings()
  196. self.read_environment()
  197. self.read_cli_args()
  198. # Verify credentials were set
  199. if not hasattr(self, 'api_token'):
  200. msg = 'Could not find values for DigitalOcean api_token. They must be specified via either ini file, ' \
  201. 'command line argument (--api-token), or environment variables (DO_API_TOKEN)\n'
  202. sys.stderr.write(msg)
  203. sys.exit(-1)
  204. # env command, show DigitalOcean credentials
  205. if self.args.env:
  206. print("DO_API_TOKEN=%s" % self.api_token)
  207. sys.exit(0)
  208. # Manage cache
  209. self.cache_filename = self.cache_path + "/ansible-digital_ocean.cache"
  210. self.cache_refreshed = False
  211. if self.is_cache_valid():
  212. self.load_from_cache()
  213. if len(self.data) == 0:
  214. if self.args.force_cache:
  215. sys.stderr.write('Cache is empty and --force-cache was specified\n')
  216. sys.exit(-1)
  217. self.manager = DoManager(self.api_token)
  218. # Pick the json_data to print based on the CLI command
  219. if self.args.droplets:
  220. self.load_from_digital_ocean('droplets')
  221. json_data = {'droplets': self.data['droplets']}
  222. elif self.args.regions:
  223. self.load_from_digital_ocean('regions')
  224. json_data = {'regions': self.data['regions']}
  225. elif self.args.images:
  226. self.load_from_digital_ocean('images')
  227. json_data = {'images': self.data['images']}
  228. elif self.args.sizes:
  229. self.load_from_digital_ocean('sizes')
  230. json_data = {'sizes': self.data['sizes']}
  231. elif self.args.ssh_keys:
  232. self.load_from_digital_ocean('ssh_keys')
  233. json_data = {'ssh_keys': self.data['ssh_keys']}
  234. elif self.args.domains:
  235. self.load_from_digital_ocean('domains')
  236. json_data = {'domains': self.data['domains']}
  237. elif self.args.tags:
  238. self.load_from_digital_ocean('tags')
  239. json_data = {'tags': self.data['tags']}
  240. elif self.args.all:
  241. self.load_from_digital_ocean()
  242. json_data = self.data
  243. elif self.args.host:
  244. json_data = self.load_droplet_variables_for_host()
  245. else: # '--list' this is last to make it default
  246. self.load_from_digital_ocean('droplets')
  247. self.build_inventory()
  248. json_data = self.inventory
  249. if self.cache_refreshed:
  250. self.write_to_cache()
  251. if self.args.pretty:
  252. print(json.dumps(json_data, indent=2))
  253. else:
  254. print(json.dumps(json_data))
  255. ###########################################################################
  256. # Script configuration
  257. ###########################################################################
  258. def read_settings(self):
  259. """ Reads the settings from the digital_ocean.ini file """
  260. config = ConfigParser.ConfigParser()
  261. config_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'digital_ocean.ini')
  262. config.read(config_path)
  263. # Credentials
  264. if config.has_option('digital_ocean', 'api_token'):
  265. self.api_token = config.get('digital_ocean', 'api_token')
  266. # Cache related
  267. if config.has_option('digital_ocean', 'cache_path'):
  268. self.cache_path = config.get('digital_ocean', 'cache_path')
  269. if config.has_option('digital_ocean', 'cache_max_age'):
  270. self.cache_max_age = config.getint('digital_ocean', 'cache_max_age')
  271. # Private IP Address
  272. if config.has_option('digital_ocean', 'use_private_network'):
  273. self.use_private_network = config.getboolean('digital_ocean', 'use_private_network')
  274. # Group variables
  275. if config.has_option('digital_ocean', 'group_variables'):
  276. self.group_variables = ast.literal_eval(config.get('digital_ocean', 'group_variables'))
  277. def read_environment(self):
  278. """ Reads the settings from environment variables """
  279. # Setup credentials
  280. if os.getenv("DO_API_TOKEN"):
  281. self.api_token = os.getenv("DO_API_TOKEN")
  282. if os.getenv("DO_API_KEY"):
  283. self.api_token = os.getenv("DO_API_KEY")
  284. def read_cli_args(self):
  285. """ Command line argument processing """
  286. parser = argparse.ArgumentParser(description='Produce an Ansible Inventory file based on DigitalOcean credentials')
  287. parser.add_argument('--list', action='store_true', help='List all active Droplets as Ansible inventory (default: True)')
  288. parser.add_argument('--host', action='store', help='Get all Ansible inventory variables about a specific Droplet')
  289. parser.add_argument('--all', action='store_true', help='List all DigitalOcean information as JSON')
  290. parser.add_argument('--droplets', '-d', action='store_true', help='List Droplets as JSON')
  291. parser.add_argument('--regions', action='store_true', help='List Regions as JSON')
  292. parser.add_argument('--images', action='store_true', help='List Images as JSON')
  293. parser.add_argument('--sizes', action='store_true', help='List Sizes as JSON')
  294. parser.add_argument('--ssh-keys', action='store_true', help='List SSH keys as JSON')
  295. parser.add_argument('--domains', action='store_true', help='List Domains as JSON')
  296. parser.add_argument('--tags', action='store_true', help='List Tags as JSON')
  297. parser.add_argument('--pretty', '-p', action='store_true', help='Pretty-print results')
  298. parser.add_argument('--cache-path', action='store', help='Path to the cache files (default: .)')
  299. parser.add_argument('--cache-max_age', action='store', help='Maximum age of the cached items (default: 0)')
  300. parser.add_argument('--force-cache', action='store_true', default=False, help='Only use data from the cache')
  301. parser.add_argument('--refresh-cache', '-r', action='store_true', default=False,
  302. help='Force refresh of cache by making API requests to DigitalOcean (default: False - use cache files)')
  303. parser.add_argument('--env', '-e', action='store_true', help='Display DO_API_TOKEN')
  304. parser.add_argument('--api-token', '-a', action='store', help='DigitalOcean API Token')
  305. self.args = parser.parse_args()
  306. if self.args.api_token:
  307. self.api_token = self.args.api_token
  308. # Make --list default if none of the other commands are specified
  309. if (not self.args.droplets and not self.args.regions and
  310. not self.args.images and not self.args.sizes and
  311. not self.args.ssh_keys and not self.args.domains and
  312. not self.args.tags and
  313. not self.args.all and not self.args.host):
  314. self.args.list = True
  315. ###########################################################################
  316. # Data Management
  317. ###########################################################################
  318. def load_from_digital_ocean(self, resource=None):
  319. """Get JSON from DigitalOcean API """
  320. if self.args.force_cache and os.path.isfile(self.cache_filename):
  321. return
  322. # We always get fresh droplets
  323. if self.is_cache_valid() and not (resource == 'droplets' or resource is None):
  324. return
  325. if self.args.refresh_cache:
  326. resource = None
  327. if resource == 'droplets' or resource is None:
  328. self.data['droplets'] = self.manager.all_active_droplets()
  329. self.cache_refreshed = True
  330. if resource == 'regions' or resource is None:
  331. self.data['regions'] = self.manager.all_regions()
  332. self.cache_refreshed = True
  333. if resource == 'images' or resource is None:
  334. self.data['images'] = self.manager.all_images()
  335. self.cache_refreshed = True
  336. if resource == 'sizes' or resource is None:
  337. self.data['sizes'] = self.manager.sizes()
  338. self.cache_refreshed = True
  339. if resource == 'ssh_keys' or resource is None:
  340. self.data['ssh_keys'] = self.manager.all_ssh_keys()
  341. self.cache_refreshed = True
  342. if resource == 'domains' or resource is None:
  343. self.data['domains'] = self.manager.all_domains()
  344. self.cache_refreshed = True
  345. if resource == 'tags' or resource is None:
  346. self.data['tags'] = self.manager.all_tags()
  347. self.cache_refreshed = True
  348. def add_inventory_group(self, key):
  349. """ Method to create group dict """
  350. host_dict = {'hosts': [], 'vars': {}}
  351. self.inventory[key] = host_dict
  352. return
  353. def add_host(self, group, host):
  354. """ Helper method to reduce host duplication """
  355. if group not in self.inventory:
  356. self.add_inventory_group(group)
  357. if host not in self.inventory[group]['hosts']:
  358. self.inventory[group]['hosts'].append(host)
  359. return
  360. def build_inventory(self):
  361. """ Build Ansible inventory of droplets """
  362. self.inventory = {
  363. 'all': {
  364. 'hosts': [],
  365. 'vars': self.group_variables
  366. },
  367. '_meta': {'hostvars': {}}
  368. }
  369. # add all droplets by id and name
  370. for droplet in self.data['droplets']:
  371. for net in droplet['networks']['v4']:
  372. if net['type'] == 'public':
  373. dest = net['ip_address']
  374. else:
  375. continue
  376. self.inventory['all']['hosts'].append(dest)
  377. self.add_host(droplet['id'], dest)
  378. self.add_host(droplet['name'], dest)
  379. # groups that are always present
  380. for group in ('digital_ocean',
  381. 'region_' + droplet['region']['slug'],
  382. 'image_' + str(droplet['image']['id']),
  383. 'size_' + droplet['size']['slug'],
  384. 'distro_' + DigitalOceanInventory.to_safe(droplet['image']['distribution']),
  385. 'status_' + droplet['status']):
  386. self.add_host(group, dest)
  387. # groups that are not always present
  388. for group in (droplet['image']['slug'],
  389. droplet['image']['name']):
  390. if group:
  391. image = 'image_' + DigitalOceanInventory.to_safe(group)
  392. self.add_host(image, dest)
  393. if droplet['tags']:
  394. for tag in droplet['tags']:
  395. self.add_host(tag, dest)
  396. # hostvars
  397. info = self.do_namespace(droplet)
  398. self.inventory['_meta']['hostvars'][dest] = info
  399. def load_droplet_variables_for_host(self):
  400. """ Generate a JSON response to a --host call """
  401. host = int(self.args.host)
  402. droplet = self.manager.show_droplet(host)
  403. info = self.do_namespace(droplet)
  404. return {'droplet': info}
  405. ###########################################################################
  406. # Cache Management
  407. ###########################################################################
  408. def is_cache_valid(self):
  409. """ Determines if the cache files have expired, or if it is still valid """
  410. if os.path.isfile(self.cache_filename):
  411. mod_time = os.path.getmtime(self.cache_filename)
  412. current_time = time()
  413. if (mod_time + self.cache_max_age) > current_time:
  414. return True
  415. return False
  416. def load_from_cache(self):
  417. """ Reads the data from the cache file and assigns it to member variables as Python Objects """
  418. try:
  419. with open(self.cache_filename, 'r') as cache:
  420. json_data = cache.read()
  421. data = json.loads(json_data)
  422. except IOError:
  423. data = {'data': {}, 'inventory': {}}
  424. self.data = data['data']
  425. self.inventory = data['inventory']
  426. def write_to_cache(self):
  427. """ Writes data in JSON format to a file """
  428. data = {'data': self.data, 'inventory': self.inventory}
  429. json_data = json.dumps(data, indent=2)
  430. with open(self.cache_filename, 'w') as cache:
  431. cache.write(json_data)
  432. ###########################################################################
  433. # Utilities
  434. ###########################################################################
  435. @staticmethod
  436. def to_safe(word):
  437. """ Converts 'bad' characters in a string to underscores so they can be used as Ansible groups """
  438. return re.sub(r"[^A-Za-z0-9\-.]", "_", word)
  439. @staticmethod
  440. def do_namespace(data):
  441. """ Returns a copy of the dictionary with all the keys put in a 'do_' namespace """
  442. info = {}
  443. for k, v in data.items():
  444. info['do_' + k] = v
  445. return info
  446. ###########################################################################
  447. # Run the script
  448. DigitalOceanInventory()