Skip to content

Commit

Permalink
Merge pull request #26 from lyz-code/feat/add_asg_support
Browse files Browse the repository at this point in the history
Add ASG support
  • Loading branch information
lyz-code authored Jun 23, 2020
2 parents 76c99cc + 6fc8b77 commit f09e0fd
Show file tree
Hide file tree
Showing 30 changed files with 816 additions and 131 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Python package

on: [push]
on: [push, pull_request]

jobs:
build:
Expand Down
13 changes: 13 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@

0.8.0 / 2020-06-23
==================

New features:

* Added ASG support

Fixes:

* Upgrade requirements
* Improve installation method
* Improve logging definition

0.7.0 / 2020-04-29
==================

Expand Down
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@
# Install

```bash
git clone https://github.com/lyz-code/clinv
cd clinv
virtualenv -p python3 clinv
source clinv/bin/activate
pip3 install -r requirements.txt
python3 setup.py install
mkdir ~/.local/share/clinv
pip3 install git+git://github.com/lyz-code/clinv
```

`clinv` will use your AWS cli credentials, therefore you must have them
Expand Down
5 changes: 4 additions & 1 deletion clinv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def load_parser():
help='String used to search',
choices=[
'all',
'asg',
'ec2',
'iam_groups',
'iam_users',
Expand All @@ -58,6 +59,7 @@ def load_parser():
type=str,
help='String used to search',
choices=[
'asg',
'ec2',
'rds',
'services',
Expand All @@ -82,6 +84,7 @@ def load_parser():
help='String used to search',
default=None,
choices=[
'asg',
'ec2',
'rds',
'services',
Expand Down Expand Up @@ -141,6 +144,6 @@ def load_logger():
logging.addLevelName(logging.DEBUG, "[\033[32mDEBUG\033[0m]")
logging.addLevelName(logging.WARNING, "[\033[33mWARNING\033[0m]")
logging.basicConfig(
level=logging.INFO,
level=logging.WARNING,
format=" %(levelname)s %(message)s"
)
43 changes: 18 additions & 25 deletions clinv/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,27 @@
Inventory: Class to gather and manipulate the inventory data.
"""

from clinv.sources.aws import \
EC2src, \
IAMGroupsrc, \
IAMUsersrc, \
RDSsrc, \
Route53src, \
S3src, \
SecurityGroupsrc, \
VPCsrc

from clinv.sources.risk_management import \
Informationsrc, Projectsrc, Servicesrc, Peoplesrc
from clinv.sources import aws, risk_management
from yaml import YAMLError

import logging
import os
import yaml

active_source_plugins = [
EC2src,
IAMGroupsrc,
IAMUsersrc,
Informationsrc,
Peoplesrc,
Projectsrc,
RDSsrc,
Route53src,
S3src,
SecurityGroupsrc,
Servicesrc,
VPCsrc,
aws.ASGsrc,
aws.EC2src,
aws.IAMGroupsrc,
aws.IAMUsersrc,
risk_management.Informationsrc,
risk_management.Peoplesrc,
risk_management.Projectsrc,
aws.RDSsrc,
aws.Route53src,
aws.S3src,
aws.SecurityGroupsrc,
risk_management.Servicesrc,
aws.VPCsrc,
]


Expand Down Expand Up @@ -97,8 +88,10 @@ class Inventory():
"""

def __init__(self, inventory_dir, source_plugins=active_source_plugins):
self.log = logging.getLogger('main')
self.inventory_dir = inventory_dir

self.log = logging.getLogger(__name__)
self.log.setLevel(logging.INFO)
self._source_plugins = source_plugins
self.source_data_path = os.path.join(
self.inventory_dir,
Expand Down
3 changes: 2 additions & 1 deletion clinv/reports/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class ClinvReport():

def __init__(self, inventory):
self.inv = inventory.inv
self.log = logging.getLogger('main')
self.log = logging.getLogger(__name__)
self.log.setLevel(logging.INFO)

def short_print_resources(self, resource_list):
"""
Expand Down
28 changes: 26 additions & 2 deletions clinv/reports/unassigned.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class UnassignedReport(ClinvReport):
def __init__(self, inventory):
super().__init__(inventory)

def _unassigned_aws_resource(self, resource_type):
def _unassigned_aws_resource(self, resource_type, exclude_ids=[]):
"""
Do aggregation of data to print the resource_type resources that are
not associated to any service.
Expand All @@ -59,6 +59,7 @@ def _unassigned_aws_resource(self, resource_type):
'iam_groups',
'vpc',
]
exclude_ids (list): List of IDs to exclude from the report
Returns:
stdout: Prints the list of unassigned items.
Expand All @@ -75,10 +76,23 @@ def _unassigned_aws_resource(self, resource_type):
pass

for instance_id, instance in sorted(self.inv[resource_type].items()):
if instance_id in exclude_ids:
continue
if instance_id not in all_assigned_instances:
if instance.state != 'terminated':
instance.short_print()

def _unassigned_asg(self):
"""
Do aggregation of data to print the ASG resources that are not
associated to any service.
Returns:
stdout: Prints the list of unassigned items.
"""

self._unassigned_aws_resource('asg')

def _unassigned_ec2(self):
"""
Do aggregation of data to print the EC2 resources that are not
Expand All @@ -87,8 +101,16 @@ def _unassigned_ec2(self):
Returns:
stdout: Prints the list of unassigned items.
"""
for asg_id, asg in self.inv['asg'].items():
for instance_id in asg.instances:
instance_id

self._unassigned_aws_resource('ec2')
asg_instances = [
instance_id
for asg_id, asg in self.inv['asg'].items()
for instance_id in asg.instances
]
self._unassigned_aws_resource('ec2', asg_instances)

def _unassigned_rds(self):
"""
Expand Down Expand Up @@ -298,6 +320,8 @@ def output(self, resource_type):
"""

if resource_type == 'all':
self.log.info('Unassigned ASG')
self._unassigned_asg()
self.log.info('Unassigned EC2')
self._unassigned_ec2()
self.log.info('Unassigned RDS')
Expand Down
3 changes: 2 additions & 1 deletion clinv/sources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class ClinvSourcesrc():
def __init__(self, source_data={}, user_data={}):
self.source_data = source_data
self.user_data = user_data
self.log = logging.getLogger('main')
self.log = logging.getLogger(__name__)
self.log.setLevel(logging.INFO)

def prune_dictionary(self, dictionary, prune_keys):
"""
Expand Down
Loading

0 comments on commit f09e0fd

Please sign in to comment.