Class: CloudProviders::Ec2Instance

Inherits:
CloudProviderInstance show all
Defined in:
lib/cloud_providers/ec2/ec2_instance.rb

Instance Method Summary collapse

Methods inherited from CloudProviderInstance

#[], #[]=, #after_bootstrap, #after_configure, #before_bootstrap, #before_configure, #bootstrap!, #bootstrapped?, #determine_os, #each, #elapsed_runtime, #has_key?, #keypair, #keys, #loaded, #on_all_callbacks, #os, #pack_clouds_dot_rb_and_expected_directories, #pending?, #refresh!, #running?, #terminate!, #terminated?, #terminating?, #to_hash, #valid?, #values, #wait_for_port, #wait_for_public_ip

Methods included from Callbacks

included

Methods included from Connections

#host, #rsync, #run, #scp, #ssh, #ssh_options, #user

Constructor Details

#initialize(opts = {}, &block) ⇒ Ec2Instance

A new instance will be created from a hash. The parent clouds describe_instances list will be searched for the first one matching any of this instance’s provided unique identifiers. If an instance is found, this instance’s properties will be set to the properties provided If the found instance has properties of the same key as the provided options, the found instance’s values will override the passed in options



26
27
28
29
30
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 26

def initialize(opts={}, &block)
  opts.delete(:id)  # metavirt (in case your using it) passes an id that we do not want to set
  set_vars_from_options(opts)
  super
end

Instance Method Details

#cloud_provider(o = {}, &block) ⇒ Object

#TODO: test or remove def hosts_file_listing_for(cl)

string = (cl.name == cloud.name) ? "#{name}.#{my_cloud.name}\t#{name}" : "#{name}.#{my_cloud.name}"
"#{internal_ip}\t#{string}"

end



44
45
46
47
48
49
50
51
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 44

def cloud_provider(o={}, &block)
  @cloud_provider ||= if cloud
    cloud.cloud_provider
  else
    options_for_cloud_provider = o.choose{|k,v| Ec2.default_options.has_key?(k)}
    Ec2.new( options_for_cloud_provider, &block)
  end
end

#configure!(opts = {}) ⇒ Object

add ec2 specific configuration steps

Raises:

  • (StandardError)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 59

def configure!(opts={})
  cloud opts[:cloud] if opts[:cloud]
  raise StandardError.new("cloud is not defined.  It must be defined to run configure on the instance") unless cloud
  vputs "configuring ec2 instance #{instance_id}."
  ec2_dir = "/etc/poolparty/ec2"
  FileUtils.mkdir_p(cloud.tmp_path/ec2_dir) unless File.directory?(cloud.tmp_path/ec2_dir)
  run ["mkdir -p #{ec2_dir}"]
  # Save a yaml file of aws varibles and send to the instance
  File.open(cloud.tmp_path/"etc"/"poolparty"/'env.yml', 'w') do |f|
    f<<YAML::dump(cloud_provider.aws_hash(ec2_dir))  #TODO: don't save sensitive info in /tmp
  end
  # We scp these files directly to the instance so to reduce the risk of accidentally leaving them in an insecure location
  scp(:source=>cert,        :destination=>ec2_dir/File.basename(cert)) if cert
  scp(:source=>private_key, :destination=>ec2_dir/File.basename(private_key)) if private_key
  scp(:source=>cloud_cert,  :destination=>ec2_dir/File.basename(cloud_cert)) if cloud_cert
  # TODO: install_ec2_tools
  super opts
  vputs "completed configuring instance  #{instance_id}."
end

#make_image(opts = {}) ⇒ Object

create an image file and copy this instance to the image file.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 95

def make_image(opts={})
  opts = {:volume       => '/',
          :size         => 6000,
          :destination  => '/mnt/bundle',
          :prefix       => image_id,
          :cert         => cert,
          :exclude      => nil,
          :kernel       => kernel_id,
          :ramdisk      => ramdisk_id,
          :ec2cert      => cloud_cert
          }.merge(opts)
  image_file = File.join(opts[:destination], opts[:prefix] )
  cmds = ["mkdir -p #{opts[:destination]}"]
  cmds << "dd if=/dev/zero of=#{image_file} bs=1M count=#{opts[:size]}"
  cmds << "mkfs.ext3 -F -j  #{image_file}"
  cmds << "mkdir -p #{opts[:destination]}/loop"
  cmds << "mount -o loop #{image_file} #{opts[:destination]}/loop"
  cmds << "rsync -ax #{rsync_excludes(opts[:exclude])} #{opts[:volume]}/ #{opts[:destination]}/loop/"
  cmds << "umount #{opts[:destination]}/loop"
  self.run cmds
  image_file
end

#right_awsObject

Access the right_aws instance directly



54
55
56
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 54

def right_aws
  cloud_provider.ec2.describe_instances instance_id
end

#rsync_excludes(array_of_abs_paths_to_exclude = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 79

def rsync_excludes(array_of_abs_paths_to_exclude=nil)
  array_of_abs_paths_to_exclude ||= %w( /sys
                         /proc
                         /dev/pts
                         /dev
                         /media
                         /mnt
                         /proc
                         /sys
                         /etc/udev/rules.d/70-persistent-net.rules
                         /etc/udev/rules.d/z25_persistent-net.rules
                        )
  array_of_abs_paths_to_exclude.inject(''){|str, path| str<<"--exclude=#{path}"; str}
end

#to_sObject

Printing. This is how we extract the instances into the listing on the local side into the local listing file



34
35
36
# File 'lib/cloud_providers/ec2/ec2_instance.rb', line 34

def to_s
  "#{name}\t#{dns_name}\t#{instance_id}"
end