Пример #1
0
    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(self.client_id, self.api_key)

        self.data = {}
        self.data['droplets'] = self.sanitize_list(
            manager.all_active_droplets())
        self.data['regions'] = self.sanitize_list(manager.all_regions())
        self.data['images'] = self.sanitize_list(
            manager.all_images(filter=None))
        self.data['sizes'] = self.sanitize_list(manager.sizes())
        self.data['ssh_keys'] = self.sanitize_list(manager.all_ssh_keys())
        self.data['domains'] = self.sanitize_list(manager.all_domains())

        self.index = {}
        self.index['region_to_name'] = self.build_index(
            self.data['regions'], 'id', 'name')
        self.index['size_to_name'] = self.build_index(self.data['sizes'], 'id',
                                                      'name')
        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()
    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()
Пример #3
0
class DigitalOceanInventory(object):

    ###########################################################################
    # Main execution path
    ###########################################################################

    def __init__(self):
        ''' Main execution path '''

        # DigitalOceanInventory data
        self.data = {}  # All DigitalOcean data
        self.inventory = {}  # Ansible Inventory

        # Define defaults
        self.cache_path = '.'
        self.cache_max_age = 0
        self.use_private_network = False
        self.group_variables = {}

        # Read settings, environment variables, and CLI arguments
        self.read_settings()
        self.read_environment()
        self.read_cli_args()

        # Verify credentials were set
        if not hasattr(self, 'api_token'):
            sys.stderr.write(
                '''Could not find values for DigitalOcean api_token.
They must be specified via either ini file, command line argument (--api-token),
or environment variables (DO_API_TOKEN)\n''')
            sys.exit(-1)

        # env command, show DigitalOcean credentials
        if self.args.env:
            print("DO_API_TOKEN=%s" % self.api_token)
            sys.exit(0)

        # Manage cache
        self.cache_filename = self.cache_path + "/ansible-digital_ocean.cache"
        self.cache_refreshed = False

        if self.is_cache_valid():
            self.load_from_cache()
            if len(self.data) == 0:
                if self.args.force_cache:
                    sys.stderr.write(
                        '''Cache is empty and --force-cache was specified\n''')
                    sys.exit(-1)

        self.manager = DoManager(None, self.api_token, api_version=2)

        # Pick the json_data to print based on the CLI command
        if self.args.droplets:
            self.load_from_digital_ocean('droplets')
            json_data = {'droplets': self.data['droplets']}
        elif self.args.regions:
            self.load_from_digital_ocean('regions')
            json_data = {'regions': self.data['regions']}
        elif self.args.images:
            self.load_from_digital_ocean('images')
            json_data = {'images': self.data['images']}
        elif self.args.sizes:
            self.load_from_digital_ocean('sizes')
            json_data = {'sizes': self.data['sizes']}
        elif self.args.ssh_keys:
            self.load_from_digital_ocean('ssh_keys')
            json_data = {'ssh_keys': self.data['ssh_keys']}
        elif self.args.domains:
            self.load_from_digital_ocean('domains')
            json_data = {'domains': self.data['domains']}
        elif self.args.all:
            self.load_from_digital_ocean()
            json_data = self.data
        elif self.args.host:
            json_data = self.load_droplet_variables_for_host()
        else:  # '--list' this is last to make it default
            self.load_from_digital_ocean('droplets')
            self.build_inventory()
            json_data = self.inventory

        if self.cache_refreshed:
            self.write_to_cache()

        if self.args.pretty:
            print(json.dumps(json_data, sort_keys=True, indent=2))
        else:
            print(json.dumps(json_data))
        # That's all she wrote...

    ###########################################################################
    # Script configuration
    ###########################################################################

    def read_settings(self):
        ''' Reads the settings from the digital_ocean.ini file '''
        config = ConfigParser.SafeConfigParser()
        config.read(
            os.path.dirname(os.path.realpath(__file__)) + '/digital_ocean.ini')

        # Credentials
        if config.has_option('digital_ocean', 'api_token'):
            self.api_token = config.get('digital_ocean', 'api_token')

        # Cache related
        if config.has_option('digital_ocean', 'cache_path'):
            self.cache_path = config.get('digital_ocean', 'cache_path')
        if config.has_option('digital_ocean', 'cache_max_age'):
            self.cache_max_age = config.getint('digital_ocean',
                                               'cache_max_age')

        # Private IP Address
        if config.has_option('digital_ocean', 'use_private_network'):
            self.use_private_network = config.getboolean(
                'digital_ocean', 'use_private_network')

        # Group variables
        if config.has_option('digital_ocean', 'group_variables'):
            self.group_variables = ast.literal_eval(
                config.get('digital_ocean', 'group_variables'))

    def read_environment(self):
        ''' Reads the settings from environment variables '''
        # Setup credentials
        if os.getenv("DO_API_TOKEN"):
            self.api_token = os.getenv("DO_API_TOKEN")
        if os.getenv("DO_API_KEY"):
            self.api_token = os.getenv("DO_API_KEY")

    def read_cli_args(self):
        ''' Command line argument processing '''
        parser = argparse.ArgumentParser(
            description=
            'Produce an Ansible Inventory file based on DigitalOcean credentials'
        )

        parser.add_argument(
            '--list',
            action='store_true',
            help='List all active Droplets as Ansible inventory (default: True)'
        )
        parser.add_argument(
            '--host',
            action='store',
            help='Get all Ansible inventory variables about a specific Droplet'
        )

        parser.add_argument('--all',
                            action='store_true',
                            help='List all DigitalOcean information as JSON')
        parser.add_argument('--droplets',
                            '-d',
                            action='store_true',
                            help='List Droplets as JSON')
        parser.add_argument('--regions',
                            action='store_true',
                            help='List Regions as JSON')
        parser.add_argument('--images',
                            action='store_true',
                            help='List Images as JSON')
        parser.add_argument('--sizes',
                            action='store_true',
                            help='List Sizes as JSON')
        parser.add_argument('--ssh-keys',
                            action='store_true',
                            help='List SSH keys as JSON')
        parser.add_argument('--domains',
                            action='store_true',
                            help='List Domains as JSON')

        parser.add_argument('--pretty',
                            '-p',
                            action='store_true',
                            help='Pretty-print results')

        parser.add_argument('--cache-path',
                            action='store',
                            help='Path to the cache files (default: .)')
        parser.add_argument(
            '--cache-max_age',
            action='store',
            help='Maximum age of the cached items (default: 0)')
        parser.add_argument('--force-cache',
                            action='store_true',
                            default=False,
                            help='Only use data from the cache')
        parser.add_argument(
            '--refresh-cache',
            '-r',
            action='store_true',
            default=False,
            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 and os.path.isfile(self.cache_filename):
            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 = {
            'all': {
                'hosts': [],
                'vars': self.group_variables
            },
            '_meta': {
                'hostvars': {}
            }
        }

        # 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']

            self.inventory['all']['hosts'].append(dest)

            self.inventory[droplet['id']] = [dest]
            self.inventory[droplet['name']] = [dest]

            # groups that are always present
            for group in ('region_' + droplet['region']['slug'],
                          'image_' + str(droplet['image']['id']),
                          'size_' + droplet['size']['slug'], 'distro_' +
                          self.to_safe(droplet['image']['distribution']),
                          'status_' + droplet['status']):
                if group not in self.inventory:
                    self.inventory[group] = {'hosts': [], 'vars': {}}
                self.inventory[group]['hosts'].append(dest)

            # groups that are not always present
            for group in (droplet['image']['slug'], droplet['image']['name']):
                if group:
                    image = 'image_' + self.to_safe(group)
                    if image not in self.inventory:
                        self.inventory[image] = {'hosts': [], 'vars': {}}
                    self.inventory[image]['hosts'].append(dest)

            if droplet['tags']:
                for tag in droplet['tags']:
                    if tag not in self.inventory:
                        self.inventory[tag] = {'hosts': [], 'vars': {}}
                    self.inventory[tag]['hosts'].append(dest)

            # hostvars
            info = self.do_namespace(droplet)
            self.inventory['_meta']['hostvars'][dest] = info

    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)
        info = self.do_namespace(droplet)
        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)

    def do_namespace(self, data):
        ''' Returns a copy of the dictionary with all the keys put in a 'do_' namespace '''
        info = {}
        for k, v in data.items():
            info['do_' + k] = v
        return info
class DigitalOceanInventory(object):

    ###########################################################################
    # Main execution path
    ###########################################################################

    def __init__(self):
        ''' Main execution path '''

        # DigitalOceanInventory data
        self.data = {}      # All DigitalOcean data
        self.inventory = {} # Ansible Inventory

        # Define defaults
        self.cache_path = '.'
        self.cache_max_age = 0
        self.use_private_network = False
        self.group_variables = {}

        # Read settings, environment variables, and CLI arguments
        self.read_settings()
        self.read_environment()
        self.read_cli_args()

        # Verify credentials were set
        if not hasattr(self, 'api_token'):
            print('''Could not find values for DigitalOcean api_token.
They must be specified via either ini file, command line argument (--api-token),
or environment variables (DO_API_TOKEN)''')
            sys.exit(-1)

        # env command, show DigitalOcean credentials
        if self.args.env:
            print("DO_API_TOKEN=%s" % self.api_token)
            sys.exit(0)

        # Manage cache
        self.cache_filename = self.cache_path + "/ansible-digital_ocean.cache"
        self.cache_refreshed = False

        if self.is_cache_valid:
            self.load_from_cache()
            if len(self.data) == 0:
                if self.args.force_cache:
                    print('''Cache is empty and --force-cache was specified''')
                    sys.exit(-1)

        self.manager = DoManager(None, self.api_token, api_version=2)

        # Pick the json_data to print based on the CLI command
        if self.args.droplets:
            self.load_from_digital_ocean('droplets')
            json_data = {'droplets': self.data['droplets']}
        elif self.args.regions:
            self.load_from_digital_ocean('regions')
            json_data = {'regions': self.data['regions']}
        elif self.args.images:
            self.load_from_digital_ocean('images')
            json_data = {'images': self.data['images']}
        elif self.args.sizes:
            self.load_from_digital_ocean('sizes')
            json_data = {'sizes': self.data['sizes']}
        elif self.args.ssh_keys:
            self.load_from_digital_ocean('ssh_keys')
            json_data = {'ssh_keys': self.data['ssh_keys']}
        elif self.args.domains:
            self.load_from_digital_ocean('domains')
            json_data = {'domains': self.data['domains']}
        elif self.args.all:
            self.load_from_digital_ocean()
            json_data = self.data
        elif self.args.host:
            json_data = self.load_droplet_variables_for_host()
        else:    # '--list' this is last to make it default
            self.load_from_digital_ocean('droplets')
            self.build_inventory()
            json_data = self.inventory

        if self.cache_refreshed:
            self.write_to_cache()

        if self.args.pretty:
            print(json.dumps(json_data, sort_keys=True, indent=2))
        else:
            print(json.dumps(json_data))
        # That's all she wrote...


    ###########################################################################
    # Script configuration
    ###########################################################################

    def read_settings(self):
        ''' Reads the settings from the digital_ocean.ini file '''
        config = ConfigParser.SafeConfigParser()
        config.read(os.path.dirname(os.path.realpath(__file__)) + '/digital_ocean.ini')

        # Credentials
        if config.has_option('digital_ocean', 'api_token'):
            self.api_token = config.get('digital_ocean', 'api_token')

        # Cache related
        if config.has_option('digital_ocean', 'cache_path'):
            self.cache_path = config.get('digital_ocean', 'cache_path')
        if config.has_option('digital_ocean', 'cache_max_age'):
            self.cache_max_age = config.getint('digital_ocean', 'cache_max_age')

        # Private IP Address
        if config.has_option('digital_ocean', 'use_private_network'):
            self.use_private_network = config.get('digital_ocean', 'use_private_network')

        # Group variables
        if config.has_option('digital_ocean', 'group_variables'):
            self.group_variables = ast.literal_eval(config.get('digital_ocean', 'group_variables'))

    def read_environment(self):
        ''' Reads the settings from environment variables '''
        # Setup credentials
        if os.getenv("DO_API_TOKEN"):
            self.api_token = os.getenv("DO_API_TOKEN")
        if os.getenv("DO_API_KEY"):
            self.api_token = os.getenv("DO_API_KEY")


    def read_cli_args(self):
        ''' Command line argument processing '''
        parser = argparse.ArgumentParser(description='Produce an Ansible Inventory file based on DigitalOcean credentials')

        parser.add_argument('--list', action='store_true', help='List all active Droplets as Ansible inventory (default: True)')
        parser.add_argument('--host', action='store', help='Get all Ansible inventory variables about a specific Droplet')

        parser.add_argument('--all', action='store_true', help='List all DigitalOcean information as JSON')
        parser.add_argument('--droplets','-d', action='store_true', help='List Droplets as JSON')
        parser.add_argument('--regions', action='store_true', help='List Regions as JSON')
        parser.add_argument('--images', action='store_true', help='List Images as JSON')
        parser.add_argument('--sizes', action='store_true', help='List Sizes as JSON')
        parser.add_argument('--ssh-keys', action='store_true', help='List SSH keys as JSON')
        parser.add_argument('--domains', action='store_true',help='List Domains as JSON')

        parser.add_argument('--pretty','-p', action='store_true', help='Pretty-print results')

        parser.add_argument('--cache-path', action='store', help='Path to the cache files (default: .)')
        parser.add_argument('--cache-max_age', action='store', help='Maximum age of the cached items (default: 0)')
        parser.add_argument('--force-cache', action='store_true', default=False, help='Only use data from the cache')
        parser.add_argument('--refresh-cache','-r', action='store_true', default=False,
                            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:
                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)
Пример #5
0
class DigitalOceanInventory(object):

    ###########################################################################
    # Main execution path
    ###########################################################################

    def __init__(self):
        """ Main execution path """

        # DigitalOceanInventory data
        self.data = {}  # All DigitalOcean data
        self.inventory = {}  # Ansible Inventory

        # Define defaults
        self.cache_path = "."
        self.cache_max_age = 0

        # Read settings, environment variables, and CLI arguments
        self.read_settings()
        self.read_environment()
        self.read_cli_args()

        # Verify credentials were set
        if not hasattr(self, "api_token"):
            print """Could not find values for DigitalOcean api_token.
They must be specified via either ini file, command line argument (--api-token),
or environment variables (DO_API_TOKEN)"""
            sys.exit(-1)

        # env command, show DigitalOcean credentials
        if self.args.env:
            print "DO_API_TOKEN=%s" % self.api_token
            sys.exit(0)

        # Manage cache
        self.cache_filename = self.cache_path + "/ansible-digital_ocean.cache"
        self.cache_refreshed = False

        if self.is_cache_valid:
            self.load_from_cache()
            if len(self.data) == 0:
                if self.args.force_cache:
                    print """Cache is empty and --force-cache was specified"""
                    sys.exit(-1)

        self.manager = DoManager(None, self.api_token, api_version=2)

        # Pick the json_data to print based on the CLI command
        if self.args.droplets:
            self.load_from_digital_ocean("droplets")
            json_data = {"droplets": self.data["droplets"]}
        elif self.args.regions:
            self.load_from_digital_ocean("regions")
            json_data = {"regions": self.data["regions"]}
        elif self.args.images:
            self.load_from_digital_ocean("images")
            json_data = {"images": self.data["images"]}
        elif self.args.sizes:
            self.load_from_digital_ocean("sizes")
            json_data = {"sizes": self.data["sizes"]}
        elif self.args.ssh_keys:
            self.load_from_digital_ocean("ssh_keys")
            json_data = {"ssh_keys": self.data["ssh_keys"]}
        elif self.args.domains:
            self.load_from_digital_ocean("domains")
            json_data = {"domains": self.data["domains"]}
        elif self.args.all:
            self.load_from_digital_ocean()
            json_data = self.data
        elif self.args.host:
            json_data = self.load_droplet_variables_for_host()
        else:  # '--list' this is last to make it default
            self.load_from_digital_ocean("droplets")
            self.build_inventory()
            json_data = self.inventory

        if self.cache_refreshed:
            self.write_to_cache()

        if self.args.pretty:
            print json.dumps(json_data, sort_keys=True, indent=2)
        else:
            print json.dumps(json_data)
        # That's all she wrote...

    ###########################################################################
    # Script configuration
    ###########################################################################

    def read_settings(self):
        """ Reads the settings from the digital_ocean.ini file """
        config = ConfigParser.SafeConfigParser()
        config.read(os.path.dirname(os.path.realpath(__file__)) + "/digital_ocean.ini")

        # Credentials
        if config.has_option("digital_ocean", "api_token"):
            self.api_token = config.get("digital_ocean", "api_token")

        # Cache related
        if config.has_option("digital_ocean", "cache_path"):
            self.cache_path = config.get("digital_ocean", "cache_path")
        if config.has_option("digital_ocean", "cache_max_age"):
            self.cache_max_age = config.getint("digital_ocean", "cache_max_age")

    def read_environment(self):
        """ Reads the settings from environment variables """
        # Setup credentials
        if os.getenv("DO_API_TOKEN"):
            self.api_token = os.getenv("DO_API_TOKEN")
        if os.getenv("DO_API_KEY"):
            self.api_token = os.getenv("DO_API_KEY")

    def read_cli_args(self):
        """ Command line argument processing """
        parser = argparse.ArgumentParser(
            description="Produce an Ansible Inventory file based on DigitalOcean credentials"
        )

        parser.add_argument(
            "--list", action="store_true", help="List all active Droplets as Ansible inventory (default: True)"
        )
        parser.add_argument(
            "--host", action="store", help="Get all Ansible inventory variables about a specific Droplet"
        )

        parser.add_argument("--all", action="store_true", help="List all DigitalOcean information as JSON")
        parser.add_argument("--droplets", "-d", action="store_true", help="List Droplets as JSON")
        parser.add_argument("--regions", action="store_true", help="List Regions as JSON")
        parser.add_argument("--images", action="store_true", help="List Images as JSON")
        parser.add_argument("--sizes", action="store_true", help="List Sizes as JSON")
        parser.add_argument("--ssh-keys", action="store_true", help="List SSH keys as JSON")
        parser.add_argument("--domains", action="store_true", help="List Domains as JSON")

        parser.add_argument("--pretty", "-p", action="store_true", help="Pretty-print results")

        parser.add_argument("--cache-path", action="store", help="Path to the cache files (default: .)")
        parser.add_argument("--cache-max_age", action="store", help="Maximum age of the cached items (default: 0)")
        parser.add_argument("--force-cache", action="store_true", default=False, help="Only use data from the cache")
        parser.add_argument(
            "--refresh-cache",
            "-r",
            action="store_true",
            default=False,
            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"]:
            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)