Skip to content

Instantly share code, notes, and snippets.

@rkive
Created March 1, 2012 23:42
Show Gist options
  • Save rkive/1954056 to your computer and use it in GitHub Desktop.
Save rkive/1954056 to your computer and use it in GitHub Desktop.
Personal Fog experiments
#!/usr/bin/env ruby
require 'rubygems'
require 'mixlib/cli'
require 'fog'
require 'chef/search/query'
require 'chef/knife'
class Stacks
def initialize
# We assume you have a fog credentials file located in ~/.fog
puts "Creating connections to AWS"
@cf = Fog::AWS::CloudFormation.new
@as = Fog::AWS::AutoScaling.new
@ec2 = Fog::Compute::AWS.new
end
def initS3
puts "Creating connection to s3"
@s3 = Fog::Storage.new( :provider => 'AWS' )
end
def stackExists?(stack_name)
begin
@cf.describe_stacks(options = {'StackName' => stack_name}).body['Stacks']
rescue Excon::Errors::BadRequest => e
puts "Validation error: 400 Bad request"
puts "\n" + e.response.body
return false
else
return true
end
end
def listStacks
puts "Grabbing all the stacks."
begin
all_stacks = @cf.describe_stacks.body['Stacks']
all_stacks.each do |stack|
puts "StackStatus: #{stack['StackStatus']} :: CreationTime: #{stack['CreationTime']} :: StackName: #{stack['StackName']}"
end
rescue Excon::Errors::SocketError => e
puts "\n" + e.response.body
rescue Excon::Errors::SocketError => e
puts "\n" + e.response.body
end
end
def showStack(stack_name)
if !stackExists?(stack_name) then
puts("\nThere is no CloudFormation stack with name #{stack_name}")
exit 0
end
stack = @cf.describe_stack_resources(options = {'StackName' => stack_name}).body['StackResources']
# * 'StackId'<~String> -
# * 'StackName'<~String> -
# * 'LogicalResourceId'<~String> -
# * 'PhysicalResourceId'<~String> -
# * 'ResourceType'<~String> -
# * 'Timestamp'<~Time> -
# * 'ResourceStatus'<~String> -
stack.each do |s|
case s['ResourceType']
when 'AWS::EC2::Instance'
puts "We found an instance!"
puts "InstanceId: #{s['PhysicalResourceId']} :: RoleID: #{s['LogicalResourceId']}"
puts ""
when 'AWS::CloudFormation::Stack'
# puts "We found a sub_stack!"
puts "StackName: #{s['StackId']} :: RoleID: #{s['LogicalResourceId']}"
end
end
end
=begin
def checkNodeSSH(stack_name)
if !stackExists?(stack_name) then
puts("\nThere is no CloudFormation stack with name #{stack_name}")
exit 0
end
getStackNodes(stack_name)
end
=end
def getAutoScalerNodes
as_groups_body = @as.describe_auto_scaling_groups.body
#print "as_groups_body is class:"
#puts as_groups_body.class
all_autoscaler_groups = as_groups_body['DescribeAutoScalingGroupsResult']['AutoScalingGroups']
#print "all_autoscaler_groups is class:"
#puts all_autoscaler_groups.class
all_autoscaler_groups.each do |autoscaler_group|
puts "AutoScalingGroupName: #{autoscaler_group['AutoScalingGroupName']}"
autoscaler_group['Instances'].each do |instance|
showInstance(instance['InstanceId'])
puts ""
end #autoscaler_group.each
end
end
# def getAutoScalerGroups
# as_groups_body = @as.describe_auto_scaling_groups.body
# all_autoscaler_groups = as_groups_body['DescribeAutoScalingGroupsResult']['AutoScalingGroups']
#
# all_autoscaler_groups.each do |autoscaler_group|
# puts "AutoScalingGroupName: #{autoscaler_group['AutoScalingGroupName']}"
# end
# end
def getAutoScalerGroups
as_groups_body = @as.describe_auto_scaling_groups.body
all_autoscaler_groups = as_groups_body['DescribeAutoScalingGroupsResult']['AutoScalingGroups']
return all_autoscaler_groups
end
def showAutoScalerGroups(groups)
groups.each do |autoscaler_group|
puts autoscaler_group['AutoScalingGroupName']
end
end
def showAllAutoScalerGroupInstances(groups)
groups.each do |autoscaler_group|
autoscaler_group['Instances'].each do |instance|
showInstance(instance['InstanceId'])
puts ""
end
end
end
def showInstance1(instanceID)
di_body = @ec2.describe_instances('instance-id' => instanceID).body
#di_body = @ec2.describe_instances.body
#print "di_body is class:"
#puts di_body.class
## reservation_set is an Array
reservation_set = di_body['reservationSet']
#print "reservation_set is class:"
#puts reservation_set.class
#puts reservation_set
reservation_set.each do |reservation_item|
#print "reservation_item is class:"
#puts reservation_item.class
#puts reservation_item
## instance_set is an Array
instance_set = reservation_item['instancesSet']
#print "instance_set is class:"
#puts instance_set.class
instance_set.each do |instance_item|
#print "instance_item is class:"
#puts instance_item.class
#puts instance_item
puts " instance['instanceId']: #{instance_item['instanceId']} :: dnsName: #{instance_item['dnsName']} "
# instance_item.each do |key, value|
# puts "instance_item #{key} has #{value}"
# end
end
end
end
def showAllInstances1
di_body = @ec2.describe_instances.body
di_body['reservationSet'].each do |instances|
instances['instancesSet'].each do |instance|
puts "Id:: #{instance['instanceId']} size:: #{instance['instanceType']} ami:: #{instance['imageId']} DNS:: #{instance['dnsName']} Name:: #{instance['tagSet']['Name']}"
end
end
## The hash with the instance attributes is nested in a couple Hash's and Array's
## Addressing array [0] will only work if we've passed in a specific instanceID.
#instance = di_body['reservationSet'][0]['instancesSet'][0]
#puts " instanceId: #{instance['instanceId']} :: dnsName: #{instance['dnsName']} "
end
def getAllInstances(groups)
all_instances = Array.new
groups.each do |autoscaler_group|
autoscaler_group['Instances'].each do |instance|
all_instances << instance
end
end
return all_instances
end
def getInstanceDNS(instanceID)
di_body = @ec2.describe_instances('instance-id' => instanceID).body
## The hash with the instance attributes is nested in a couple Hash's and Array's
## Addressing array [0] will only work if we've passed in a specific instanceID.
instance = di_body['reservationSet'][0]['instancesSet'][0]
dnsNames = instance['dnsName']
return dnsNames
end
def gatherHostnames(instances)
all_dnsNames = Array.new
instances.each do |instance|
value = getInstanceDNS(instance['InstanceId'])
all_dnsNames << value
end
return all_dnsNames
end
def showInstance(instanceID)
di_body = @ec2.describe_instances('instance-id' => instanceID).body
## The hash with the instance attributes is nested in a couple Hash's and Array's
## Addressing array [0] will only work if we've passed in a specific instanceID.
instance = di_body['reservationSet'][0]['instancesSet'][0]
puts " instanceId: #{instance['instanceId']} :: dnsName: #{instance['dnsName']} "
end
def showAllInstances
di_body = @ec2.describe_instances.body
## The hash with the instance attributes is nested in a couple Hash's and Array's
## Addressing array [0] will only work if we've passed in a specific instanceID.
instance = di_body['reservationSet'][0]['instancesSet'][0]
puts " instanceId: #{instance['instanceId']} :: dnsName: #{instance['dnsName']} "
end
# def showStack(stack_name)
# if !stackExists?(stack_name) then
# puts("\nThere is no CloudFormation stack with name #{stack_name}")
# exit 0
# end
#
# stack = @cf.describe_stack_resources(options = {'StackName' => stack_name}).body['StackResources']
#
# # * 'StackId'<~String> -
# # * 'StackName'<~String> -
# # * 'LogicalResourceId'<~String> -
# # * 'PhysicalResourceId'<~String> -
# # * 'ResourceType'<~String> -
# # * 'Timestamp'<~Time> -
# # * 'ResourceStatus'<~String> -
#
# stack.each do |s|
# puts s['LogicalResourceId']
# case s['ResourceType']
# when 'AWS::EC2::Instance'
# puts "We found an instance!"
# puts "InstanceId: #{s['PhysicalResourceId']} :: RoleID: #{s['LogicalResourceId']}"
# puts ""
# when 'AWS::CloudFormation::Stack'
# puts "We found a sub_stack!"
# sub_stack = @cf.describe_stack_resources(options = {'StackName' => stack_name}).body['StackResources']
# sub_stack.each do |ss|
# puts "ss is:"
# puts ss
# puts ""
# if ss['ResourceType'] == 'AWS::AutoScaling::AutoScalingGroup'
# puts "We found an AutoScalingGroup with the sub_stack"
# @as = Fog::AWS::AutoScaling.new
# instances = @as.describe_auto_scaling_instances
# end
# end
# #showStack(sub_stack)
# puts ""
# end
# end
# end
def uploadTemplate(template_name, template_file, s3_bucket)
initS3
directory = @s3.directories.create(
:key => s3_bucket,
:public => false
)
file = directory.files.create(
:key => template_name,
:body => File.open("#{template_file}"),
:public => false
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment