Class: Iravat::IrEC2
- Inherits:
-
Object
- Object
- Iravat::IrEC2
- Defined in:
- lib/ec2/ir_ec2.rb
Overview
require ‘./ir_module’
Instance Method Summary collapse
- #connectToAWS ⇒ Object
- #createNewInstance(imageID, instanceType, availablilityZone, userCookieData) ⇒ Object
-
#error_ec2(err) ⇒ Object
common error handler for RightScale Lib.
- #getKey ⇒ Object
- #getSecurityGroup ⇒ Object
-
#initialize ⇒ IrEC2
constructor
A new instance of IrEC2.
- #printAllActiveImages ⇒ Object
- #rebootInstance(instanceID) ⇒ Object
- #terminateInstance(instanceID) ⇒ Object
Constructor Details
#initialize ⇒ IrEC2
Returns a new instance of IrEC2.
7 8 9 10 11 12 13 14 15 |
# File 'lib/ec2/ir_ec2.rb', line 7 def initialize() @logger = IrLogger.new(self.class.name).getLog4rLogger() awsConfig=IrAwsConfigManager.new(IRAVAT_AWS_CONFIG_XML_PATH) awsConfig.loadConfigurationFile() @awsKeyID=awsConfig.getPathFromXML("/ChipMonkAWSConfigs/AWSAccessKey/AWSAccessKey") @awsSecretKey=awsConfig.getPathFromXML("/ChipMonkAWSConfigs/AWSAccessKey/AWSAccessSecretKey") @awsKeyToUse=awsConfig.getPathFromXML("/ChipMonkAWSConfigs/EC2/KeyNameToUse") @awsSecurityGroupToUse=awsConfig.getPathFromXML("/ChipMonkAWSConfigs/EC2/SecurityGroupToUse") end |
Instance Method Details
#connectToAWS ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ec2/ir_ec2.rb', line 26 def connectToAWS() begin @ec2 = RightAws::Ec2.new(@awsKeyID,@awsSecretKey) if(@ec2.nil?) @logger.error("Failed to create an access to EC2") return false end return true rescue RightAws::AwsError=>err self.error_ec2(err) return false end end |
#createNewInstance(imageID, instanceType, availablilityZone, userCookieData) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/ec2/ir_ec2.rb', line 110 def createNewInstance(imageID,instanceType, availablilityZone, userCookieData) #run_instances(image_id, min_count, max_count, group_ids, key_name, user_data='', addressing_type = nil, #instance_type = nil, kernel_id = nil, ramdisk_id = nil, availability_zone = nil, monitoring_enabled = nil, #subnet_id = nil, disable_api_termination = nil, instance_initiated_shutdown_behavior = nil, block_device_mappings = nil, #placement_group_name = nil, client_token = nil) begin responseDetails=@ec2.run_instances(imageID, 1, 1, self.getSecurityGroup(), self.getKey(), userCookieData, nil, instanceType, nil, nil, availablilityZone)[0] @logger.debug("============================================") @logger.debug("AMI instance launched") @logger.debug("Instance ID:" + responseDetails[:aws_instance_id]) @logger.debug("Instance State:" + responseDetails[:aws_state]) @logger.debug("Instance Type:" + responseDetails[:aws_instance_type]) @logger.debug("Availability Zone:" + responseDetails[:aws_availability_zone]) @logger.debug ("============================================") rescue RightAws::AwsError=>err self.error_ec2(err) return nil end return responseDetails[:aws_instance_id] end |
#error_ec2(err) ⇒ Object
common error handler for RightScale Lib
19 20 21 22 23 |
# File 'lib/ec2/ir_ec2.rb', line 19 def error_ec2 (err) err.errors.each do |e| @logger.error(err.errors) end end |
#getKey ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ec2/ir_ec2.rb', line 41 def getKey() begin keyPairs= @ec2.describe_key_pairs([@awsKeyToUse]) if(keyPairs.nil? or keyPairs.length <= 0) @logger.error("No Key pair existing for mentioned key name " + @awsKeyToUse) return nil end return @awsKeyToUse rescue RightAws::AwsError=>err self.error_ec2(err) return nil end end |
#getSecurityGroup ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ec2/ir_ec2.rb', line 57 def getSecurityGroup() begin securityGroups = @ec2.describe_security_groups([@awsSecurityGroupToUse]) if(securityGroups.nil? or securityGroups.length <=0 ) @logger.error("No security group created") return nil end return [@awsSecurityGroupToUse] rescue RightAws::AwsError=>err self.error_ec2(err) return nil end end |
#printAllActiveImages ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ec2/ir_ec2.rb', line 73 def printAllActiveImages() begin awsInstances=@ec2.describe_instances #Loop on all instance awsInstances.each do |awsInstance| @logger.info("============================================") @logger.info("AWS Instance ID:" + awsInstance[:aws_instance_id]) @logger.info("AWS Image ID:" + awsInstance[:aws_image_id]) @logger.info("AWS State:" + awsInstance[:aws_state]) if( awsInstance[:aws_state] == "running") @logger.info("IP Address:" + awsInstance[:ip_address]) @logger.info("DNS Name:" + awsInstance[:dns_name]) @logger.info("AWS Instance Type:" + awsInstance[:aws_instance_type]) @logger.info("AWS Private DNS Name:" + awsInstance[:private_dns_name]) @logger.info("AWS Instance Launch Time:" + awsInstance[:aws_launch_time]) @logger.info("AWS Availability Zone:" + awsInstance[:aws_availability_zone]) blockDevicesMapped=awsInstance[:block_device_mappings] blockDevicesMapped.each do |blockDevice| @logger.info(" -----------------------------------------------") @logger.info(" EBS Device Name:" + blockDevice[:device_name]) @logger.info(" EBS Device ID:" + blockDevice[:ebs_volume_id]) @logger.info(" EBS Device Attach Time:" + blockDevice[:ebs_attach_time]) @logger.info(" -----------------------------------------------") end end @logger.info("============================================") end rescue RightAws::AwsError=>err self.error_ec2(err) return nil end end |
#rebootInstance(instanceID) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/ec2/ir_ec2.rb', line 146 def rebootInstance(instanceID) begin response=@ec2.reboot_instances(instanceID) if(response == false) @logger.debug("AMI instance id " + instanceID + "failed to reboot") else @logger.debug("AMI instance id " + instanceID + "rebooted successfully") end rescue RightAws::AwsError=>err self.error_ec2(err) return nil end end |
#terminateInstance(instanceID) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/ec2/ir_ec2.rb', line 160 def terminateInstance(instanceID) begin instanceIDArr=[instanceID] responseDetails=@ec2.terminate_instances(instanceIDArr) @logger.debug("============================================") @logger.debug("AMI instance terminated") @logger.debug("Instance ID:" + responseDetails[0][:aws_instance_id]) @logger.debug("Current State Code:" + responseDetails[0][:aws_current_state_code].to_s()) @logger.debug("Current State Name:" + responseDetails[0][:aws_current_state_name]) @logger.debug("Previous State Code:" + responseDetails[0][:aws_prev_state_code].to_s()) @logger.debug("Previous State Name:" + responseDetails[0][:aws_prev_state_name]) @logger.debug("============================================") rescue RightAws::AwsError=>err self.error_ec2(err) return nil end end |