Class: Opsicle::CloneableInstance
- Inherits:
-
Object
- Object
- Opsicle::CloneableInstance
- Defined in:
- lib/opsicle/cloneable_instance.rb
Instance Attribute Summary collapse
-
#agent_version ⇒ Object
Returns the value of attribute agent_version.
-
#ami_id ⇒ Object
Returns the value of attribute ami_id.
-
#architecture ⇒ Object
Returns the value of attribute architecture.
-
#auto_scaling_type ⇒ Object
Returns the value of attribute auto_scaling_type.
-
#availability_zone ⇒ Object
Returns the value of attribute availability_zone.
-
#cli ⇒ Object
Returns the value of attribute cli.
-
#ebs_optimized ⇒ Object
Returns the value of attribute ebs_optimized.
-
#ec2 ⇒ Object
Returns the value of attribute ec2.
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#install_updates_on_boot ⇒ Object
Returns the value of attribute install_updates_on_boot.
-
#instance_id ⇒ Object
Returns the value of attribute instance_id.
-
#instance_type ⇒ Object
Returns the value of attribute instance_type.
-
#layer ⇒ Object
Returns the value of attribute layer.
-
#layer_ids ⇒ Object
Returns the value of attribute layer_ids.
-
#new_instance_id ⇒ Object
Returns the value of attribute new_instance_id.
-
#opsworks ⇒ Object
Returns the value of attribute opsworks.
-
#os ⇒ Object
Returns the value of attribute os.
-
#root_device_type ⇒ Object
Returns the value of attribute root_device_type.
-
#ssh_key_name ⇒ Object
Returns the value of attribute ssh_key_name.
-
#stack ⇒ Object
Returns the value of attribute stack.
-
#stack_id ⇒ Object
Returns the value of attribute stack_id.
-
#status ⇒ Object
Returns the value of attribute status.
-
#subnet_id ⇒ Object
Returns the value of attribute subnet_id.
-
#tenancy ⇒ Object
Returns the value of attribute tenancy.
-
#virtualization_type ⇒ Object
Returns the value of attribute virtualization_type.
Instance Method Summary collapse
- #ask_for_new_option(description) ⇒ Object
- #ask_for_overriding_permission(description, overriding_all) ⇒ Object
- #ask_for_possible_options(arr, description) ⇒ Object
- #ask_to_start_instance ⇒ Object
- #auto_generated_hostname ⇒ Object
- #clone(options) ⇒ Object
- #clone_with_defaults(options) ⇒ Object
- #create_new_instance(new_instance_hostname, instance_type, ami_id, agent_version, subnet_id) ⇒ Object
- #find_subnet_name(subnet) ⇒ Object
- #hostname_unique?(name) ⇒ Boolean
- #increment_hostname ⇒ Object
-
#initialize(instance, layer, stack, opsworks, ec2, cli) ⇒ CloneableInstance
constructor
A new instance of CloneableInstance.
- #make_new_hostname ⇒ Object
- #sibling_hostnames ⇒ Object
- #start_new_instance ⇒ Object
- #verify_agent_version ⇒ Object
- #verify_ami_id ⇒ Object
- #verify_instance_type ⇒ Object
- #verify_subnet_id ⇒ Object
Constructor Details
#initialize(instance, layer, stack, opsworks, ec2, cli) ⇒ CloneableInstance
Returns a new instance of CloneableInstance.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/opsicle/cloneable_instance.rb', line 31 def initialize(instance, layer, stack, opsworks, ec2, cli) self.hostname = instance.hostname self.status = instance.status self.layer = layer self.stack = stack self.ami_id = instance.ami_id self.instance_type = instance.instance_type self.agent_version = instance.agent_version self.stack_id = instance.stack_id self.layer_ids = instance.layer_ids self.auto_scaling_type = instance.auto_scaling_type self.os = instance.os self.ssh_key_name = instance.ssh_key_name self.availability_zone = instance.availability_zone self.virtualization_type = instance.virtualization_type self.subnet_id = instance.subnet_id self.architecture = instance.architecture self.root_device_type = instance.root_device_type self.install_updates_on_boot = instance.install_updates_on_boot self.ebs_optimized = instance.ebs_optimized self.tenancy = instance.tenancy self.opsworks = opsworks self.ec2 = ec2 self.cli = cli self.instance_id = instance.instance_id self.new_instance_id = nil end |
Instance Attribute Details
#agent_version ⇒ Object
Returns the value of attribute agent_version.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def agent_version @agent_version end |
#ami_id ⇒ Object
Returns the value of attribute ami_id.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def ami_id @ami_id end |
#architecture ⇒ Object
Returns the value of attribute architecture.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def architecture @architecture end |
#auto_scaling_type ⇒ Object
Returns the value of attribute auto_scaling_type.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def auto_scaling_type @auto_scaling_type end |
#availability_zone ⇒ Object
Returns the value of attribute availability_zone.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def availability_zone @availability_zone end |
#cli ⇒ Object
Returns the value of attribute cli.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def cli @cli end |
#ebs_optimized ⇒ Object
Returns the value of attribute ebs_optimized.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def ebs_optimized @ebs_optimized end |
#ec2 ⇒ Object
Returns the value of attribute ec2.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def ec2 @ec2 end |
#hostname ⇒ Object
Returns the value of attribute hostname.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def hostname @hostname end |
#install_updates_on_boot ⇒ Object
Returns the value of attribute install_updates_on_boot.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def install_updates_on_boot @install_updates_on_boot end |
#instance_id ⇒ Object
Returns the value of attribute instance_id.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def instance_id @instance_id end |
#instance_type ⇒ Object
Returns the value of attribute instance_type.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def instance_type @instance_type end |
#layer ⇒ Object
Returns the value of attribute layer.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def layer @layer end |
#layer_ids ⇒ Object
Returns the value of attribute layer_ids.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def layer_ids @layer_ids end |
#new_instance_id ⇒ Object
Returns the value of attribute new_instance_id.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def new_instance_id @new_instance_id end |
#opsworks ⇒ Object
Returns the value of attribute opsworks.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def opsworks @opsworks end |
#os ⇒ Object
Returns the value of attribute os.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def os @os end |
#root_device_type ⇒ Object
Returns the value of attribute root_device_type.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def root_device_type @root_device_type end |
#ssh_key_name ⇒ Object
Returns the value of attribute ssh_key_name.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def ssh_key_name @ssh_key_name end |
#stack ⇒ Object
Returns the value of attribute stack.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def stack @stack end |
#stack_id ⇒ Object
Returns the value of attribute stack_id.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def stack_id @stack_id end |
#status ⇒ Object
Returns the value of attribute status.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def status @status end |
#subnet_id ⇒ Object
Returns the value of attribute subnet_id.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def subnet_id @subnet_id end |
#tenancy ⇒ Object
Returns the value of attribute tenancy.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def tenancy @tenancy end |
#virtualization_type ⇒ Object
Returns the value of attribute virtualization_type.
3 4 5 |
# File 'lib/opsicle/cloneable_instance.rb', line 3 def virtualization_type @virtualization_type end |
Instance Method Details
#ask_for_new_option(description) ⇒ Object
212 213 214 |
# File 'lib/opsicle/cloneable_instance.rb', line 212 def ask_for_new_option(description) @cli.ask("Please write in the new #{description} and press ENTER:") end |
#ask_for_overriding_permission(description, overriding_all) ⇒ Object
216 217 218 219 220 221 222 223 |
# File 'lib/opsicle/cloneable_instance.rb', line 216 def (description, overriding_all) if overriding_all ans = @cli.ask("Do you wish to override this #{description}? By overriding, you are choosing to override the current #{description} for all instances you are cloning.\n1) Yes\n2) No", Integer) else ans = @cli.ask("Do you wish to override this #{description}?\n1) Yes\n2) No", Integer) end ans == 1 end |
#ask_for_possible_options(arr, description) ⇒ Object
206 207 208 209 210 |
# File 'lib/opsicle/cloneable_instance.rb', line 206 def (arr, description) arr.each_with_index { |id, index| puts "#{index.to_i + 1}) #{id}"} id_index = @cli.ask("Which #{description}?\n", Integer) { |q| q.in = 1..arr.length.to_i } - 1 arr[id_index] end |
#ask_to_start_instance ⇒ Object
257 258 259 260 |
# File 'lib/opsicle/cloneable_instance.rb', line 257 def ask_to_start_instance ans = @cli.ask("Do you wish to start this new instance?\n1) Yes\n2) No", Integer) ans == 1 end |
#auto_generated_hostname ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/opsicle/cloneable_instance.rb', line 88 def auto_generated_hostname if hostname =~ /\d\d\z/ increment_hostname else hostname << "-clone" end end |
#clone(options) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/opsicle/cloneable_instance.rb', line 59 def clone() puts "\nCloning an instance..." new_instance_hostname = make_new_hostname ami_id = verify_ami_id agent_version = verify_agent_version subnet_id = verify_subnet_id instance_type = verify_instance_type create_new_instance(new_instance_hostname, instance_type, ami_id, agent_version, subnet_id) start_new_instance end |
#clone_with_defaults(options) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/opsicle/cloneable_instance.rb', line 73 def clone_with_defaults() puts "\nCloning an instance..." new_hostname = auto_generated_hostname create_new_instance(new_hostname, instance_type, ami_id, agent_version, subnet_id) opsworks.start_instance(instance_id: new_instance_id) puts "\nNew instance is starting…" end |
#create_new_instance(new_instance_hostname, instance_type, ami_id, agent_version, subnet_id) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/opsicle/cloneable_instance.rb', line 225 def create_new_instance(new_instance_hostname, instance_type, ami_id, agent_version, subnet_id) new_instance = @opsworks.create_instance({ stack_id: self.stack_id, # required layer_ids: self.layer_ids, # required instance_type: instance_type, # required auto_scaling_type: self.auto_scaling_type, # accepts load, timer hostname: new_instance_hostname, os: self.os, ami_id: ami_id, ssh_key_name: self.ssh_key_name, # availability_zone: self.availability_zone, subnet_id: subnet_id, virtualization_type: self.virtualization_type, architecture: self.architecture, # accepts x86_64, i386 root_device_type: self.root_device_type, # accepts ebs, instance-store install_updates_on_boot: self.install_updates_on_boot, #ebs_optimized: self.ebs_optimized, agent_version: agent_version, tenancy: self.tenancy, }) self.new_instance_id = new_instance.instance_id self.layer.add_new_instance(new_instance_id) puts "\nNew instance #{new_instance_hostname} has been created: #{new_instance_id}" end |
#find_subnet_name(subnet) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/opsicle/cloneable_instance.rb', line 116 def find_subnet_name(subnet) = subnet. tag = nil .each { |t| tag = t if t.key == 'Name' } tag.value if tag end |
#hostname_unique?(name) ⇒ Boolean
112 113 114 |
# File 'lib/opsicle/cloneable_instance.rb', line 112 def hostname_unique?(name) !sibling_hostnames.include?(name) end |
#increment_hostname ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/opsicle/cloneable_instance.rb', line 100 def increment_hostname name = hostname until hostname_unique?(name) do name = name.gsub(/(\d\d\z)/) { "#{($1.to_i + 1).to_s.rjust(2, '0')}" } end name end |
#make_new_hostname ⇒ Object
81 82 83 84 85 86 |
# File 'lib/opsicle/cloneable_instance.rb', line 81 def make_new_hostname new_instance_hostname = auto_generated_hostname puts "\nAutomatically generated hostname: #{new_instance_hostname}\n" new_instance_hostname = ask_for_new_option("instance's hostname") if ("hostname", false) new_instance_hostname end |
#sibling_hostnames ⇒ Object
96 97 98 |
# File 'lib/opsicle/cloneable_instance.rb', line 96 def sibling_hostnames self.layer.instances.collect { |instance| instance.hostname } end |
#start_new_instance ⇒ Object
250 251 252 253 254 255 |
# File 'lib/opsicle/cloneable_instance.rb', line 250 def start_new_instance if ask_to_start_instance @opsworks.start_instance(instance_id: self.new_instance_id) puts "\nNew instance is starting…" end end |
#verify_agent_version ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/opsicle/cloneable_instance.rb', line 147 def verify_agent_version if self.layer.agent_version agent_version = self.layer.agent_version else puts "\nCurrent agent version is #{self.agent_version}" if ("agent version", true) agents = @opsworks.describe_agent_versions(stack_id: self.stack_id).agent_versions version_ids = agents.collect { |i| i.version }.uniq agent_version = (version_ids, "agent version") else agent_version = self.agent_version end end self.layer.agent_version = agent_version agent_version end |
#verify_ami_id ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/opsicle/cloneable_instance.rb', line 123 def verify_ami_id if self.layer.ami_id ami_id = self.layer.ami_id else puts "\nCurrent AMI id is #{self.ami_id}" if ("AMI ID", true) instances = @opsworks.describe_instances(stack_id: self.stack_id).instances ami_ids = instances.collect { |i| i.ami_id }.uniq ami_ids << "Provide a different AMI ID." ami_id = (ami_ids, "AMI ID") if ami_id == "Provide a different AMI ID." ami_id = ask_for_new_option('AMI ID') end else ami_id = self.ami_id end end self.layer.ami_id = ami_id ami_id end |
#verify_instance_type ⇒ Object
199 200 201 202 203 204 |
# File 'lib/opsicle/cloneable_instance.rb', line 199 def verify_instance_type puts "\nCurrent instance type is #{self.instance_type}" rewriting = ("instance type", false) instance_type = rewriting ? ask_for_new_option('instance type') : self.instance_type instance_type end |
#verify_subnet_id ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/opsicle/cloneable_instance.rb', line 166 def verify_subnet_id if self.layer.subnet_id subnet_id = self.layer.subnet_id else current_subnet = Aws::EC2::Subnet.new(id: self.subnet_id) subnet_name = find_subnet_name(current_subnet) puts "\nCurrent subnet ID is \"#{subnet_name}\" #{current_subnet.availability_zone} (#{self.subnet_id})" if ("subnet ID", true) ec2_subnets = ec2.describe_subnets.subnets subnets = [] ec2_subnets.each do |subnet| if subnet.vpc_id == stack.vpc_id subnet_name = find_subnet_name(subnet) zone_name = subnet.availability_zone subnet_id = subnet.subnet_id subnets << "\"#{subnet_name}\" #{zone_name} (#{subnet_id})" end end subnets = subnets.sort subnet_id = (subnets, "subnet ID") subnet_id = subnet_id.scan(/(subnet-[a-z0-9]*)/).first.first if subnet_id else subnet_id = self.subnet_id end end self.layer.subnet_id = subnet_id subnet_id end |