Skip to content

Commit

Permalink
Code is more PEP8-capable and PEP257-capable (#83)
Browse files Browse the repository at this point in the history
* Update ec2inventory.py

-More PEP 8 changes
-Triple quote docstrings according to PEP 257
-Minor other changes

* Update main.py

-PEP 8 changes on grouping import statements
-Minor other changes
  • Loading branch information
alanyee authored and costimuraru committed Oct 18, 2019
1 parent fdd22f4 commit 03abc80
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
57 changes: 28 additions & 29 deletions src/ops/inventory/ec2inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
# OF ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.

import boto
import json
import re
import sys
import os

import boto
from boto import ec2
from boto.pyami.config import Config

Expand Down Expand Up @@ -59,7 +60,7 @@ def find_bastion_box(self, conn):
Find ips for the bastion box
"""

if not len(self.bastion_filters.values()):
if not self.bastion_filters.values():
return

self.bastion_filters['instance-state-name'] = 'running'
Expand All @@ -70,14 +71,15 @@ def find_bastion_box(self, conn):
return instance.ip_address

def do_api_calls_update_cache(self):
''' Do API calls to each region, and save data in cache files '''
""" Do API calls to each region, and save data in cache files """

for region in self.regions:
self.get_instances_by_region(region)

def get_instances_by_region(self, region):
''' Makes an AWS EC2 API call to the list of instances in a particular
region '''
"""Makes an AWS EC2 API call to the list of instances in a particular
region
"""

try:
cfg = Config()
Expand All @@ -93,10 +95,9 @@ def get_instances_by_region(self, region):
# connect_to_region will fail "silently" by returning None if the
# region name is wrong or not supported
if conn is None:
print(
sys.exit(
"region name: {} likely not supported, or AWS is down. "
"connection to region failed.".format(region))
sys.exit(1)

reservations = conn.get_all_instances(filters=self.filters)

Expand All @@ -119,21 +120,19 @@ def sort_key(instance):
"{}, configure it with 'aws configure --profile {}'".format(e.message, self.boto_profile))

except boto.exception.BotoServerError as e:
print(e)
sys.exit(1)
sys.exit(e)

def get_instance(self, region, instance_id):
''' Gets details about a specific instance '''
""" Gets details about a specific instance """
conn = ec2.connect_to_region(region)

# connect_to_region will fail "silently" by returning None if the
# region name is wrong or not supported
if conn is None:
print(
sys.exit(
"region name: %s likely not supported, or AWS is down. "
"connection to region failed." % region
)
sys.exit(1)

reservations = conn.get_all_instances([instance_id])
for reservation in reservations:
Expand Down Expand Up @@ -172,15 +171,13 @@ def add_instance(self, bastion_ip, instance, region):
for grouping in set(self.group_callbacks):
given_groups = grouping(instance)
for group in given_groups:
if not group:
continue
self.push(self.inventory, group, dest)
if group:
self.push(self.inventory, group, dest)

# Group by all tags
for tag in instance.tags.values():
if not tag:
continue
self.push(self.inventory, tag, dest)
if tag:
self.push(self.inventory, tag, dest)

# Inventory: Group by region
self.push(self.inventory, region, dest)
Expand Down Expand Up @@ -242,9 +239,9 @@ def get_host_info_dict_from_instance(self, instance):
return instance_vars

def get_host_info(self):
''' Get variables about a specific host '''
""" Get variables about a specific host """

if len(self.index) == 0:
if not self.index:
# Need to load index from cache
self.load_index_from_cache()

Expand All @@ -262,8 +259,9 @@ def get_host_info(self):
self.get_host_info_dict_from_instance(instance), True)

def push(self, my_dict, key, element):
''' Push an element onto an array that may not have been defined in
the dict '''
""" Push an element onto an array that may not have been defined in
the dict
"""
if key == element:
return
group_info = my_dict.setdefault(key, [])
Expand All @@ -276,7 +274,7 @@ def push(self, my_dict, key, element):
group_info.append(element)

def push_group(self, my_dict, key, element):
''' Push a group as a child of another group. '''
""" Push a group as a child of another group. """
parent_group = my_dict.setdefault(key, {})
if not isinstance(parent_group, dict):
parent_group = my_dict[key] = {'hosts': parent_group}
Expand All @@ -285,19 +283,20 @@ def push_group(self, my_dict, key, element):
child_groups.append(element)

def to_safe(self, word):
''' Converts 'bad' characters in a string to underscores so they can be
used as Ansible groups '''
""" Converts 'bad' characters in a string to underscores so they can be
used as Ansible groups
"""

return re.sub(r"[^A-Za-z0-9\-]", "_", word)

def json_format_dict(self, data, pretty=True):
''' Converts a dict to a JSON object and dumps it as a formatted
string '''
""" Converts a dict to a JSON object and dumps it as a formatted
string
"""

if pretty:
return json.dumps(data, sort_keys=True, indent=2)
else:
return json.dumps(data)
return json.dumps(data)

def group_by_tag(self, param):
self.group(lambda instance: [instance.tags.get(param, 'no-' + param)])
Expand Down
10 changes: 4 additions & 6 deletions src/ops/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import logging
import os

from .cli.config_generator import ConfigGeneratorParserConfig, ConfigGeneratorRunner
from simpledi import Container, auto, cache, instance, ListInstanceProvider

from .cli.config_generator import ConfigGeneratorParserConfig, ConfigGeneratorRunner
from .cli.config import ClusterConfigGenerator, ClusterConfig
from .cli.inventory import InventoryParserConfig
from .cli.inventory import InventoryRunner
Expand Down Expand Up @@ -155,18 +155,16 @@ def run(args=None):

if isinstance(output, int):
return output
else:
ret = app_container.execute(output)
sys.exit(ret)
ret = app_container.execute(output)
sys.exit(ret)


def get_cluster_config_path(root_dir, console_args):
""" Return config path + root_dir if path is relative """

if os.path.isabs(console_args.cluster_config_path):
return console_args.cluster_config_path
else:
return os.path.join(root_dir, console_args.cluster_config_path)
return os.path.join(root_dir, console_args.cluster_config_path)


def get_root_dir(args):
Expand Down

0 comments on commit 03abc80

Please sign in to comment.