Class: Opsicle::CloneableInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/opsicle/cloneable_instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_versionObject

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_idObject

Returns the value of attribute ami_id.



3
4
5
# File 'lib/opsicle/cloneable_instance.rb', line 3

def ami_id
  @ami_id
end

#architectureObject

Returns the value of attribute architecture.



3
4
5
# File 'lib/opsicle/cloneable_instance.rb', line 3

def architecture
  @architecture
end

#auto_scaling_typeObject

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_zoneObject

Returns the value of attribute availability_zone.



3
4
5
# File 'lib/opsicle/cloneable_instance.rb', line 3

def availability_zone
  @availability_zone
end

#cliObject

Returns the value of attribute cli.



3
4
5
# File 'lib/opsicle/cloneable_instance.rb', line 3

def cli
  @cli
end

#ebs_optimizedObject

Returns the value of attribute ebs_optimized.



3
4
5
# File 'lib/opsicle/cloneable_instance.rb', line 3

def ebs_optimized
  @ebs_optimized
end

#hostnameObject

Returns the value of attribute hostname.



3
4
5
# File 'lib/opsicle/cloneable_instance.rb', line 3

def hostname
  @hostname
end

#install_updates_on_bootObject

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_typeObject

Returns the value of attribute instance_type.



3
4
5
# File 'lib/opsicle/cloneable_instance.rb', line 3

def instance_type
  @instance_type
end

#layerObject

Returns the value of attribute layer.



3
4
5
# File 'lib/opsicle/cloneable_instance.rb', line 3

def layer
  @layer
end

#layer_idsObject

Returns the value of attribute layer_ids.



3
4
5
# File 'lib/opsicle/cloneable_instance.rb', line 3

def layer_ids
  @layer_ids
end

#opsworksObject

Returns the value of attribute opsworks.



3
4
5
# File 'lib/opsicle/cloneable_instance.rb', line 3

def opsworks
  @opsworks
end

#osObject

Returns the value of attribute os.



3
4
5
# File 'lib/opsicle/cloneable_instance.rb', line 3

def os
  @os
end

#root_device_typeObject

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_nameObject

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_idObject

Returns the value of attribute stack_id.



3
4
5
# File 'lib/opsicle/cloneable_instance.rb', line 3

def stack_id
  @stack_id
end

#statusObject

Returns the value of attribute status.



3
4
5
# File 'lib/opsicle/cloneable_instance.rb', line 3

def status
  @status
end

#subnet_idObject

Returns the value of attribute subnet_id.



3
4
5
# File 'lib/opsicle/cloneable_instance.rb', line 3

def subnet_id
  @subnet_id
end

#tenancyObject

Returns the value of attribute tenancy.



3
4
5
# File 'lib/opsicle/cloneable_instance.rb', line 3

def tenancy
  @tenancy
end

#virtualization_typeObject

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
41
# File 'lib/opsicle/cloneable_instance.rb', line 31

def clone(options)
  all_sibling_hostnames = self.layer.instances.collect { |instance| instance.hostname }
  new_instance_hostname = make_new_hostname(self.hostname, all_sibling_hostnames)
  puts "\nWe will make a new instance with hostname: #{new_instance_hostname}"

  options[:ami] ? ami_id = options[:ami] : ami_id = self.ami_id
  options[:instance_type] ? instance_type = options[:instance_type] : instance_type = self.instance_type
  options[:agent_version] ? agent_version = options[:agent_version] : agent_version = self.agent_version

  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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/opsicle/cloneable_instance.rb', line 71

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 "New instance is created: #{new_instance.instance_id}"
end

#hostname_unique?(hostname, all_hostnames) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/opsicle/cloneable_instance.rb', line 67

def hostname_unique?(hostname, all_hostnames)
  !all_hostnames.include?(hostname)
end

#increment_hostname(hostname, all_hostnames) ⇒ Object



60
61
62
63
64
65
# File 'lib/opsicle/cloneable_instance.rb', line 60

def increment_hostname(hostname, all_hostnames)
  until hostname_unique?(hostname, all_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, all_hostnames) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/opsicle/cloneable_instance.rb', line 43

def make_new_hostname(old_hostname, all_hostnames)
  if old_hostname =~ /\d\d\z/
    new_instance_hostname = increment_hostname(old_hostname, all_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)

  if rewriting == 1
    new_instance_hostname = @cli.ask("Please write in the new instance's hostname and press ENTER:")
  end

  new_instance_hostname
end