Skip to content

Commit

Permalink
Introduce DEBUG logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmxs committed Jul 4, 2018
1 parent 7543d36 commit 2504ab5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
Binary file added .DS_Store
Binary file not shown.
15 changes: 15 additions & 0 deletions aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,16 @@ type AwsProvider struct {
}

func (p *AwsProvider) GetRunningInstances() ([]*types.Instance, error) {
if context.DryRun {
log.Debug("[AWS] Fetching instanes")
}
instChan := make(chan *types.Instance, 5)
wg := sync.WaitGroup{}
wg.Add(len(regions))
for _, region := range regions {
if context.DryRun {
log.Debugf("[AWS] Fetching instanes from: %s", region)
}
go func(region string) {
defer wg.Done()

Expand All @@ -68,6 +74,9 @@ func (p *AwsProvider) GetRunningInstances() ([]*types.Instance, error) {
log.Errorf("[AWS] Failed to fetch the running instances in region: %s, err: %s", region, e)
return
}
if context.DryRun {
log.Debugf("[AWS] Processing instances: [%s] in region: %s", instanceResult.Reservations, region)
}
for _, res := range instanceResult.Reservations {
for _, inst := range res.Instances {
instChan <- newInstance(inst)
Expand All @@ -91,6 +100,9 @@ func (a AwsProvider) TerminateInstances([]*types.Instance) error {
}

func (a AwsProvider) GetAccesses() ([]*types.Access, error) {
if context.DryRun {
log.Debug("[AWS] Fetching access keys")
}
client, err := newIamClient()
if err != nil {
return nil, err
Expand All @@ -100,6 +112,9 @@ func (a AwsProvider) GetAccesses() ([]*types.Access, error) {
if err != nil {
return nil, err
}
if context.DryRun {
log.Debugf("[AWS] Processing access keys: [%s]", resp.AccessKeyMetadata)
}
accesses := []*types.Access{}
for _, akm := range resp.AccessKeyMetadata {
if *akm.Status == "Active" {
Expand Down
9 changes: 8 additions & 1 deletion azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,21 @@ type AzureProvider struct {
}

func (p *AzureProvider) GetRunningInstances() ([]*types.Instance, error) {
if context.DryRun {
log.Debug("[AZURE] Fetching instances")
}
instances := make([]*types.Instance, 0)
result, err := vmClient.ListAll(ctx.Background())
if err != nil {
log.Errorf("[AZURE] Failed to fetch the running instances, err: %s", err.Error())
return nil, err
}
for _, inst := range result.Values() {
instances = append(instances, newInstance(inst, getCreationTimeFromTags, utils.ConvertTags))
newInstance := newInstance(inst, getCreationTimeFromTags, utils.ConvertTags)
if context.DryRun {
log.Debugf("[AZURE] Converted instance: [%s]", newInstance)
}
instances = append(instances, newInstance)
}
return instances, nil
}
Expand Down
18 changes: 18 additions & 0 deletions gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,18 @@ type GcpProvider struct {
}

func (p *GcpProvider) GetRunningInstances() ([]*types.Instance, error) {
if context.DryRun {
log.Debug("[GCP] Fetching instanes")
}
instances := make([]*types.Instance, 0)
instanceList, err := computeClient.Instances.AggregatedList(projectId).Filter("status eq RUNNING").Do()
if err != nil {
log.Errorf("[GCP] Failed to fetch the running instances, err: %s", err.Error())
return nil, err
}
if context.DryRun {
log.Debugf("[GCP] Processing instances: [%s]", instanceList.Items)
}
for _, items := range instanceList.Items {
for _, inst := range items.Instances {
instances = append(instances, newInstance(inst))
Expand All @@ -69,6 +75,9 @@ func (p *GcpProvider) GetRunningInstances() ([]*types.Instance, error) {
}

func (a GcpProvider) TerminateInstances(instances []*types.Instance) error {
if context.DryRun {
log.Debug("[GCP] Terminating instanes")
}
instanceGroups, err := computeClient.InstanceGroupManagers.AggregatedList(projectId).Do()
if err != nil {
log.Errorf("[GCP] Failed to fetch instance groups, err: %s", err.Error())
Expand All @@ -79,6 +88,9 @@ func (a GcpProvider) TerminateInstances(instances []*types.Instance) error {
instanceGroupsToDelete := map[*compute.InstanceGroupManager]bool{}

for _, inst := range instances {
if context.DryRun {
log.Debugf("[GCP] Terminating instane: %s", inst.GetName())
}
groupFound := false
for _, i := range instanceGroups.Items {
for _, group := range i.InstanceGroupManagers {
Expand All @@ -92,6 +104,9 @@ func (a GcpProvider) TerminateInstances(instances []*types.Instance) error {
}
}

if context.DryRun {
log.Debugf("[GCP] Instance groups to terminate: [%s]", instanceGroupsToDelete)
}
wg := sync.WaitGroup{}
wg.Add(len(instanceGroupsToDelete))
for g := range instanceGroupsToDelete {
Expand All @@ -108,6 +123,9 @@ func (a GcpProvider) TerminateInstances(instances []*types.Instance) error {
}
}(g)
}
if context.DryRun {
log.Debugf("[GCP] Instances to terminate: [%s]", instancesToDelete)
}
wg.Add(len(instancesToDelete))
for _, i := range instancesToDelete {
go func(inst *types.Instance) {
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func main() {
os.Exit(0)
}

if context.DryRun {
log.SetLevel(log.DebugLevel)
}

op := func() *types.OpType {
for i := range context.Operations {
if i.String() == *opType {
Expand Down

0 comments on commit 2504ab5

Please sign in to comment.