Skip to content

Commit

Permalink
Adding AWS CLI to ai.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aslan committed May 8, 2016
1 parent 9840bfe commit f5c5840
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 14 deletions.
4 changes: 3 additions & 1 deletion lib/zaws/awscli/commands/ec2/DescribeInstances.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def initialize(shellout, awscli)
end

def execute(region, view, filters={}, textout=nil, verbose=nil,profile=nil)
comline="aws --output #{view} --region #{region} ec2 describe-instances"
comline = "aws"
comline = comline + " --output #{view}"
comline = comline + " --region #{region} ec2 describe-instances"
comline = comline + " --profile #{profile}" if profile
comline = comline + " --filter" if filters.length > 0
filters.each do |key, item|
Expand Down
6 changes: 2 additions & 4 deletions lib/zaws/awscli/credentials.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
module ZAWS
class AWSCLI
class Credentials
def initialize(shellout, aws, filename)
@shellout=shellout
@aws=aws
@awsconfig=Inifile.new(filename)
def initialize(filename)
@awsconfig=ZAWS::Helper::IniFile.new(:filename=>filename)
end

def profiles
Expand Down
10 changes: 8 additions & 2 deletions lib/zaws/awscli/data/ec2/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ def load(command, data, verbose,cache=true)
end
end

def view()
return @instance_raw_data
def view(view=nil)
if view=="yaml"
return @instance_hash.to_yaml
elsif view=="hash"
return @instance_hash
else
return @instance_raw_data
end
end

def hash_identification(single_instance_hash)
Expand Down
23 changes: 23 additions & 0 deletions lib/zaws/awscli/regions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module ZAWS
class AWSCLI

def main_regions
return ['us-east-1',
'us-west-2',
'us-west-1',
'ap-southeast-1']
end

def extended_Regions
return ['us-east-1',
'eu-central-1',
'ap-southeast-1',
'ap-northeast-1',
'ap-southeast-2',
'ap-northeast-2',
'sa-east-1']
end


end
end
13 changes: 11 additions & 2 deletions lib/zaws/command/ai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,26 @@ def initialize(*args)
nessusapi = ZAWS::Nessusapi.new(shellout)
sumoapi = ZAWS::Sumoapi.new(shellout)
newrelicapi = ZAWS::Newrelicapi.new(shellout)
@ai = ZAWS::Controllers::AI.new(shellout, nessusapi,sumoapi,newrelicapi)
awscli = ZAWS::AWSCLI.new(shellout)
@ai = ZAWS::Controllers::AI.new(shellout, nessusapi,sumoapi,newrelicapi,awscli)
@out = $stdout
@print_exit_code = false
end

desc "query", "query"
option :home, :type => :string, :default => ENV['HOME'], :desc => 'Home directory location for credentials file'
def query(value)
@ai.query.all(options[:home], @out, (options[:verbose] ? @out : nil),value)
@ai.awscli.home=options[:home]
@ai.query.all(@out, (options[:verbose] ? @out : nil),value)
end

desc "query_aws", "query_aws"
option :home, :type => :string, :default => ENV['HOME'], :desc => 'Home directory location for credentials file'
def query_aws(value)
@ai.awscli.home=options[:home]
@ai.query.all_aws(@out, (options[:verbose] ? @out : nil),value)
end

end
end
end
7 changes: 6 additions & 1 deletion lib/zaws/controllers/ai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ module ZAWS
module Controllers
class AI

def initialize(shellout, nessusapi,sumoapi,newrelicapi)
def initialize(shellout, nessusapi,sumoapi,newrelicapi,awscli)
@shellout=shellout
@_nessusapi= nessusapi ? nessusapi : ZAWS::Nessusapi.new(@shellout)
@_sumoapi= sumoapi ? sumoapi : ZAWS::Sumoapi.new(@shellout)
@_newrelicapi= newrelicapi ? newrelicapi : ZAWS::Newrelicapi.new(@shellout)
@_awscli= awscli ? awscli : ZAWS::AWSCLI.new(@shellout)
end

def nessusapi
Expand All @@ -21,6 +22,10 @@ def newrelicapi
return @_newrelicapi
end

def awscli
return @_awscli
end

def query
@_query ||= (ZAWS::Services::AI::Query.new(@shellout, self))
end
Expand Down
7 changes: 6 additions & 1 deletion lib/zaws/helper/inifile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

# This class represents the INI file and can be used to parse, modify,
# and write INI files.
class IniFile

module ZAWS
module Helper
class IniFile
include Enumerable

class Error < StandardError; end
Expand Down Expand Up @@ -625,3 +628,5 @@ def unescape_value( value )
end

end # IniFile
end
end
33 changes: 32 additions & 1 deletion lib/zaws/services/ai/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,38 @@ def all(home,out,verbose=nil,value)
end
out.puts(results.to_yaml)
end


def all_aws(out,verbose=nil,value)
profile_creds=ZAWS::AWSCLI::Credentials.new("#{@ai.awscli.home}/.aws/credentials")
item = []
verbose.puts("DEBUG: regions: " + @ai.awscli.main_regions.join(",")) if verbose
verbose.puts("DEBUG: profiles: " +profile_creds.profiles.join(",")) if verbose
profile_creds.profiles.each do |profile|
verbose.puts("DEBUG: Iterating over profile: "+ profile) if verbose
@ai.awscli.main_regions.each do |region|
filters= {}
verbose.puts("DRBUG: Calling describe instances") if verbose
@ai.awscli.command_ec2.describeInstances.execute(region,'json' ,filters, nil, verbose,profile)
res = @ai.awscli.data_ec2.instance.view('hash')
res['profile']=profile
item << res
end
end
results = {}
results['awscli']= []
item.each do |reservations|
reservations['Reservations'].each do |reservation|
reservation['Instances'].each do |instance|
if instance['PrivateIpAddress'] and instance['PrivateIpAddress'].include?(value)
instance['progile']=reservations['profile']
results['awscli'] << instance
end
end
end
end
out.puts(results.to_yaml)
end

end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/zaws/services/ec2/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ def view(region, viewtype, textout=nil, verbose=nil, vpcid=nil, externalid=nil,p
filters= {}
filters['vpc-id']=vpcid if vpcid
filters['tag:externalid']=externalid if externalid
view=viewtype=='yaml'? 'json':viewtype
@aws.awscli.home=home
@aws.awscli.command_ec2.describeInstances.execute(region, 'json',filters, textout, verbose,profile)
instances = @aws.awscli.data_ec2.instance.view
@aws.awscli.command_ec2.describeInstances.execute(region,view ,filters, textout, verbose,profile)
instances = @aws.awscli.data_ec2.instance.view(viewtype)
textout.puts(instances) if textout
return instances
end
Expand Down

0 comments on commit f5c5840

Please sign in to comment.