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.
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#install_updates_on_boot ⇒ Object
Returns the value of attribute install_updates_on_boot.
-
#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.
-
#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_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
- #clone(options) ⇒ Object
- #create_new_instance(new_instance_hostname, instance_type, ami_id, agent_version) ⇒ Object
- #get_new_agent_version ⇒ Object
- #hostname_unique?(hostname, all_sibling_hostnames) ⇒ Boolean
- #increment_hostname(hostname, all_sibling_hostnames) ⇒ Object
-
#initialize(instance, layer, opsworks, cli) ⇒ CloneableInstance
constructor
A new instance of CloneableInstance.
- #make_new_hostname(old_hostname) ⇒ Object
- #verify_agent_version ⇒ Object
- #verify_ami_id ⇒ Object
- #verify_instance_type ⇒ Object
Constructor Details
#initialize(instance, layer, opsworks, cli) ⇒ CloneableInstance
Returns a new instance of CloneableInstance.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/opsicle/cloneable_instance.rb', line 7 def initialize(instance, layer, opsworks, cli) self.hostname = instance.hostname self.status = instance.status self.layer = layer 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.cli = cli 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 |
#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_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 |
#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_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
#clone(options) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/opsicle/cloneable_instance.rb', line 31 def clone() puts "\nCloning an instance..." new_instance_hostname = make_new_hostname(self.hostname) ami_id = verify_ami_id agent_version = verify_agent_version instance_type = verify_instance_type create_new_instance(new_instance_hostname, instance_type, ami_id, agent_version) end |
#create_new_instance(new_instance_hostname, instance_type, ami_id, agent_version) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/opsicle/cloneable_instance.rb', line 115 def create_new_instance(new_instance_hostname, instance_type, ami_id, agent_version) 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, virtualization_type: self.virtualization_type, subnet_id: self.subnet_id, 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, }) puts "\nNew instance has been created: #{new_instance.instance_id}" end |
#get_new_agent_version ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/opsicle/cloneable_instance.rb', line 95 def get_new_agent_version agents = @opsworks.describe_agent_versions(stack_id: self.stack_id).agent_versions version_ids = [] agents.each do |agent| version_ids << agent.version end version_ids.each_with_index { |id, index| puts "#{index.to_i + 1}) #{id}"} id_index = @cli.ask("Which agent version ID?\n", Integer) { |q| q.in = 1..version_ids.length.to_i } - 1 version_ids[id_index] end |
#hostname_unique?(hostname, all_sibling_hostnames) ⇒ Boolean
65 66 67 |
# File 'lib/opsicle/cloneable_instance.rb', line 65 def hostname_unique?(hostname, all_sibling_hostnames) !all_sibling_hostnames.include?(hostname) end |
#increment_hostname(hostname, all_sibling_hostnames) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/opsicle/cloneable_instance.rb', line 58 def increment_hostname(hostname, all_sibling_hostnames) until hostname_unique?(hostname, all_sibling_hostnames) do hostname = hostname.gsub(/(\d\d\z)/) { "#{($1.to_i + 1).to_s.rjust(2, '0')}" } end hostname end |
#make_new_hostname(old_hostname) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/opsicle/cloneable_instance.rb', line 42 def make_new_hostname(old_hostname) all_sibling_hostnames = self.layer.instances.collect { |instance| instance.hostname } if old_hostname =~ /\d\d\z/ new_instance_hostname = increment_hostname(old_hostname, all_sibling_hostnames) else new_instance_hostname = old_hostname << "_clone" end puts "\nAutomatically generated hostname: #{new_instance_hostname}\n" rewriting = @cli.ask("Do you wish to rewrite this hostname?\n1) Yes\n2) No", Integer) new_instance_hostname = @cli.ask("Please write in the new instance's hostname and press ENTER:") if rewriting == 1 new_instance_hostname end |
#verify_agent_version ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/opsicle/cloneable_instance.rb', line 82 def verify_agent_version if self.layer.agent_version agent_version = self.layer.agent_version else puts "\nCurrent agent version is #{self.agent_version}" rewriting = @cli.ask("Do you wish to override this version? By overriding, you are choosing to override the current agent version for all instances you are cloning.\n1) Yes\n2) No", Integer) agent_version = rewriting == 1 ? get_new_agent_version : self.agent_version end self.layer.agent_version = agent_version agent_version end |
#verify_ami_id ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/opsicle/cloneable_instance.rb', line 69 def verify_ami_id if self.layer.ami_id ami_id = self.layer.ami_id else puts "\nCurrent AMI id is #{self.ami_id}" rewriting = @cli.ask("Do you wish to override this AMI? By overriding, you are choosing to override the current AMI for all instances you are cloning.\n1) Yes\n2) No", Integer) ami_id = rewriting == 1 ? @cli.ask("Please write in the new AMI id press ENTER:") : self.ami_id end self.layer.ami_id = ami_id ami_id end |
#verify_instance_type ⇒ Object
108 109 110 111 112 113 |
# File 'lib/opsicle/cloneable_instance.rb', line 108 def verify_instance_type puts "\nCurrent instance type is #{self.instance_type}" rewriting = @cli.ask("Do you wish to override this instance type?\n1) Yes\n2) No", Integer) instance_type = rewriting == 1 ? @cli.ask("Please write in the new instance type press ENTER:") : self.instance_type instance_type end |