Class: Haas::Aws
- Inherits:
-
Object
- Object
- Haas::Aws
- Defined in:
- lib/haas/aws.rb
Constant Summary collapse
- CENTOS_IMAGES =
{ "6.5" => { "us-east-1"=>"ami-8997afe0", "us-west-2"=>"ami-b6bdde86", "us-west-1"=>"ami-1a013c5f", "eu-west-1"=>"ami-42718735", "ap-southeast-1"=>"ami-a08fd9f2", "ap-southeast-2"=>"ami-e7138ddd", "ap-northeast-1"=>"ami-81294380", "sa-east-1"=>"ami-7d02a260" }, "7" => { "us-east-1"=>"ami-96a818fe", "us-west-2"=>"ami-c7d092f7", "us-west-1"=>"ami-6bcfc42e", "eu-west-1"=>"ami-e4ff5c93", "ap-southeast-1"=>"ami-aea582fc", "ap-southeast-2"=>"ami-bd523087", "ap-northeast-1"=>"ami-89634988", "sa-east-1"=>"ami-bf9520a2" } }
Class Method Summary collapse
- .connect ⇒ Object
- .create_key_pair ⇒ Object
- .ec2 ⇒ Object
- .is_cluster_ssh_open?(instances) ⇒ Boolean
- .launch_instances ⇒ Object
- .nb_instance_available ⇒ Object
- .nb_running_instances ⇒ Object
- .region ⇒ Object
- .terminate_cluster(cluster) ⇒ Object
Class Method Details
.connect ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/haas/aws.rb', line 28 def self.connect @region = Haas::Config.[:aws_region] || 'us-east-1' AWS.config( access_key_id: ENV['AWS_KEY'], secret_access_key: ENV['AWS_SECRET'], region: region ) @ec2 = AWS::EC2.new end |
.create_key_pair ⇒ Object
61 62 63 64 65 |
# File 'lib/haas/aws.rb', line 61 def self.create_key_pair key_pair = Haas::KeyPair.create(name: Haas.cluster.name) File.write(Haas.cluster.identity_file_path, key_pair.private_key) File.chmod(0600, Haas.cluster.identity_file_path) end |
.ec2 ⇒ Object
38 39 40 |
# File 'lib/haas/aws.rb', line 38 def self.ec2 @ec2 end |
.is_cluster_ssh_open?(instances) ⇒ Boolean
135 136 137 138 139 140 |
# File 'lib/haas/aws.rb', line 135 def self.is_cluster_ssh_open?(instances) instances.each do |instance| return false unless Haas::Utils.is_port_open?(instance.public_dns_name,22) end return true end |
.launch_instances ⇒ Object
67 68 69 70 71 72 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/haas/aws.rb', line 67 def self.launch_instances image_id = CENTOS_IMAGES["6.5"][region] if !ec2.security_groups.filter('group-name', 'haas-security-group').first security_group = ec2.security_groups.create('haas-security-group') security_group.(:tcp, 22) security_group.(:tcp, 80) security_group.(:tcp, 443) security_group.(:tcp, 8080) security_group.(:tcp, 0..65535, security_group) security_group.(:udp, 0..65535, security_group) security_group.(:icmp, -1, security_group) end instances = ec2.instances.create({ :image_id => image_id, :instance_type => Haas::Config.[:instance_type], :key_name => Haas.cluster.name, :security_groups => ['haas-security-group'], :block_device_mappings => [ { :device_name => "/dev/sda", :ebs => { :volume_size => 8, # 8 GiB :delete_on_termination => true } }, { :device_name => "/dev/sdf", :virtual_name => "ephemeral0" } ], :count => Haas::Config.[:nb_instances].to_i }) print "Waiting for the instances to start " while instances.any? {|i| i.status == :pending; } do print '.' sleep 1 end puts " done" print "Waiting for the instances to be initialized and accessible " while !is_cluster_ssh_open?(instances) do print '.' sleep 1 end puts " done" instances.each do |instance| Haas::Node.create( cluster_id: Haas.cluster.id, instance_id: instance.id, public_ip_address: instance.ip_address, public_dns_name: instance.public_dns_name, private_ip_address: instance.private_ip_address, private_dns_name: instance.private_dns_name ) end end |
.nb_instance_available ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/haas/aws.rb', line 46 def self.nb_instance_available account_attributes = ec2.client.describe_account_attributes\ .data[:account_attribute_set]\ .inject({}) do |m, i| m[i[:attribute_name]] = i[:attribute_value_set].first[:attribute_value]; m end max_instances = account_attributes["max-instances"].to_i return max_instances - nb_running_instances end |
.nb_running_instances ⇒ Object
57 58 59 |
# File 'lib/haas/aws.rb', line 57 def self.nb_running_instances ec2.instances.inject({}) { |m, i| i.status == :running ? m[i.id] = i.status : nil; m }.length end |
.region ⇒ Object
42 43 44 |
# File 'lib/haas/aws.rb', line 42 def self.region @region end |
.terminate_cluster(cluster) ⇒ Object
128 129 130 131 132 133 |
# File 'lib/haas/aws.rb', line 128 def self.terminate_cluster cluster ec2.client.terminate_instances({ instance_ids: cluster.nodes.map(&:instance_id) }) cluster.destroy end |