Class: Judo::Base
- Inherits:
-
Object
- Object
- Judo::Base
- Defined in:
- lib/judo/base.rb
Instance Attribute Summary collapse
-
#group ⇒ Object
Returns the value of attribute group.
Class Method Summary collapse
Instance Method Summary collapse
- #access_id ⇒ Object
- #access_secret ⇒ Object
- #base_domain ⇒ Object
- #bucket ⇒ Object
- #bucket_name ⇒ Object
- #create_server(name, group, options) ⇒ Object
- #ec2 ⇒ Object
- #ec2_instances ⇒ Object
- #ec2_snapshots ⇒ Object
- #ec2_volumes ⇒ Object
- #ensure_db_version ⇒ Object
- #ensure_init ⇒ Object
- #ensure_setup ⇒ Object
- #fetch_servers_state ⇒ Object
- #fetch_snapshots_state ⇒ Object
- #find_groups(names) ⇒ Object
- #find_server(name) ⇒ Object
- #find_servers(names) ⇒ Object
- #find_servers_by_name_or_groups(*names) ⇒ Object
- #find_servers_by_name_or_groups_with_not(*names) ⇒ Object
- #get(key) ⇒ Object
- #get_db_version ⇒ Object
- #get_group(name) ⇒ Object
- #group_versions ⇒ Object
- #groups ⇒ Object
- #has_init? ⇒ Boolean
- #init_keypair ⇒ Object
- #init_sdb ⇒ Object
- #init_security_group ⇒ Object
-
#initialize(options) ⇒ Base
constructor
A new instance of Base.
- #instance_id_to_judo(instance_id) ⇒ Object
- #ip_to_judo(ip) ⇒ Object
- #ips ⇒ Object
- #keypair_file(&blk) ⇒ Object
- #mk_server_name(group) ⇒ Object
- #new_server_id ⇒ Object
- #new_snapshot(name, server) ⇒ Object
- #reload_ec2_instances ⇒ Object
- #s3 ⇒ Object
- #s3_get(k) ⇒ Object
- #s3_put(k, file) ⇒ Object
- #s3_url(k) ⇒ Object
- #sdb ⇒ Object
- #server_domain ⇒ Object
- #servers ⇒ Object
- #servers_state ⇒ Object
- #set_db_version(new_version) ⇒ Object
- #snapshot_domain ⇒ Object
- #snapshots ⇒ Object
- #snapshots_state ⇒ Object
- #state ⇒ Object
- #task(msg, &block) ⇒ Object
-
#update(attrs) ⇒ Object
i’m copy pasting code from server - this needs to be its own module.
- #volumes ⇒ Object
Constructor Details
#initialize(options) ⇒ Base
Returns a new instance of Base.
12 13 14 15 |
# File 'lib/judo/base.rb', line 12 def initialize() @access_id = [:access_id] @access_secret = [:access_secret] end |
Instance Attribute Details
#group ⇒ Object
Returns the value of attribute group.
3 4 5 |
# File 'lib/judo/base.rb', line 3 def group @group end |
Class Method Details
.defaults ⇒ Object
5 6 7 8 9 10 |
# File 'lib/judo/base.rb', line 5 def self.defaults { :access_id => ENV['AWS_ACCESS_KEY_ID'], :access_secret => ENV['AWS_SECRET_ACCESS_KEY'] } end |
Instance Method Details
#access_id ⇒ Object
17 18 19 |
# File 'lib/judo/base.rb', line 17 def access_id @access_id || (raise JudoError, "no AWS Access ID specified") end |
#access_secret ⇒ Object
21 22 23 |
# File 'lib/judo/base.rb', line 21 def access_secret @access_secret || (raise JudoError, "no AWS Secret Key specified") end |
#base_domain ⇒ Object
37 38 39 |
# File 'lib/judo/base.rb', line 37 def base_domain "judo_base" end |
#bucket ⇒ Object
217 218 219 |
# File 'lib/judo/base.rb', line 217 def bucket @bucket ||= s3.bucket(bucket_name, true) end |
#bucket_name ⇒ Object
25 26 27 |
# File 'lib/judo/base.rb', line 25 def bucket_name "judo_#{access_id}" end |
#create_server(name, group, options) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/judo/base.rb', line 146 def create_server(name, group, ) s = Judo::Server.new(self, new_server_id, group) servers << s s.create(name, ) s end |
#ec2 ⇒ Object
197 198 199 |
# File 'lib/judo/base.rb', line 197 def ec2 @ec2 ||= Aws::Ec2.new(access_id, access_secret, :logger => Logger.new(nil)) end |
#ec2_instances ⇒ Object
193 194 195 |
# File 'lib/judo/base.rb', line 193 def ec2_instances @ec2_instance ||= ec2.describe_instances end |
#ec2_snapshots ⇒ Object
189 190 191 |
# File 'lib/judo/base.rb', line 189 def ec2_snapshots @ec2_snapshots ||= ec2.describe_snapshots([], :owner => "self") end |
#ec2_volumes ⇒ Object
185 186 187 |
# File 'lib/judo/base.rb', line 185 def ec2_volumes @ec2_volumes ||= ec2.describe_volumes end |
#ensure_db_version ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/judo/base.rb', line 305 def ensure_db_version case get_db_version when 0 task("Upgrading Judo SDB from version 0 to 2") do sdb.create_domain(server_domain) sdb.create_domain(snapshot_domain) set_db_version(2) end when 1 task("Upgrading Judo SDB from version 1 to 2") do sdb.create_domain(snapshot_domain) set_db_version(2) end when 2 # current version else raise JudoError, "Judo SDB has higher version (#{get_db_version}) " + "than current gem (2) - please upgrade Judo" end end |
#ensure_init ⇒ Object
252 253 254 255 256 257 258 |
# File 'lib/judo/base.rb', line 252 def ensure_init if !has_init? init_sdb init_security_group init_keypair end end |
#ensure_setup ⇒ Object
247 248 249 250 |
# File 'lib/judo/base.rb', line 247 def ensure_setup ensure_init ensure_db_version end |
#fetch_servers_state ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/judo/base.rb', line 111 def fetch_servers_state s = {} sdb.select("select * from `#{server_domain}`")[:items].each do |group| group.each do |key,val| s[key] = val end end s end |
#fetch_snapshots_state ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/judo/base.rb', line 101 def fetch_snapshots_state s = {} sdb.select("select * from `#{snapshot_domain}`")[:items].each do |group| group.each do |key,val| s[key] = val end end s end |
#find_groups(names) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/judo/base.rb', line 41 def find_groups(names) return groups if names.include?(":all") names.map do |name| groups.detect { |g| g.displayname == name } || (raise JudoError, "No such group #{name}") end end |
#find_server(name) ⇒ Object
48 49 50 |
# File 'lib/judo/base.rb', line 48 def find_server(name) find_servers([name]).first end |
#find_servers(names) ⇒ Object
52 53 54 55 56 |
# File 'lib/judo/base.rb', line 52 def find_servers(names) names.map do |name| servers.detect { |s| s.name == name || s.displayname == name } || (raise JudoError, "No such server #{name}") end end |
#find_servers_by_name_or_groups(*names) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/judo/base.rb', line 65 def find_servers_by_name_or_groups(*names) just_servers = names.flatten.reject { |s| s =~ /^:/ } just_groups = names.flatten.select { |s| s =~ /^:/ } [find_groups(just_groups).map { |g| g.servers } + find_servers(just_servers)].flatten end |
#find_servers_by_name_or_groups_with_not(*names) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/judo/base.rb', line 58 def find_servers_by_name_or_groups_with_not(*names) ok_servers = names.flatten.reject { |s| s =~ /^\^/ } not_servers = names.flatten.select { |s| s =~ /^\^/ }.map { |s| s =~ /^\^(.*)$/ ; $1 } find_servers_by_name_or_groups(ok_servers) - find_servers_by_name_or_groups(not_servers) end |
#get(key) ⇒ Object
233 234 235 |
# File 'lib/judo/base.rb', line 233 def get(key) state[key] && [state[key]].flatten.first end |
#get_db_version ⇒ Object
301 302 303 |
# File 'lib/judo/base.rb', line 301 def get_db_version get("dbversion").to_i end |
#get_group(name) ⇒ Object
159 160 161 162 163 |
# File 'lib/judo/base.rb', line 159 def get_group(name) group = groups.detect { |g| g.name == name } group ||= Judo::Group.new(self, name) group end |
#group_versions ⇒ Object
201 202 203 |
# File 'lib/judo/base.rb', line 201 def group_versions @group_version ||= sdb.get_attributes(base_domain, "group_versions")[:attributes] end |
#groups ⇒ Object
177 178 179 |
# File 'lib/judo/base.rb', line 177 def groups @groups ||= group_versions.map { |name,ver| Judo::Group.new(self, name) } end |
#has_init? ⇒ Boolean
260 261 262 |
# File 'lib/judo/base.rb', line 260 def has_init? sdb.list_domains[:domains].include?(base_domain) end |
#init_keypair ⇒ Object
281 282 283 284 285 286 287 |
# File 'lib/judo/base.rb', line 281 def init_keypair task("Initializing Judo Keypair") do ec2.delete_key_pair("judo") material = ec2.create_key_pair("judo")[:aws_material] s3_put("judo.pem", material) end end |
#init_sdb ⇒ Object
264 265 266 267 268 |
# File 'lib/judo/base.rb', line 264 def init_sdb task("Initializing Judo SDB") do sdb.create_domain(base_domain) end end |
#init_security_group ⇒ Object
270 271 272 273 274 275 276 277 278 279 |
# File 'lib/judo/base.rb', line 270 def init_security_group task("Initializing Judo Security Group") do begin ec2.create_security_group('judo', 'Judo') ec2.("judo", 22, 22,'tcp','0.0.0.0/0') rescue Aws::AwsError => e raise unless e. =~ /InvalidGroup.Duplicate/ end end end |
#instance_id_to_judo(instance_id) ⇒ Object
209 210 211 |
# File 'lib/judo/base.rb', line 209 def instance_id_to_judo(instance_id) servers.detect { |s| s.instance_id and s.instance_id == instance_id } end |
#ip_to_judo(ip) ⇒ Object
205 206 207 |
# File 'lib/judo/base.rb', line 205 def ip_to_judo(ip) servers.detect { |s| s.elastic_ip == ip } end |
#ips ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/judo/base.rb', line 86 def ips @ips ||= ec2.describe_addresses.map do |ip| { :ip => ip[:public_ip], :instance_id => ip[:instance_id], :attached_to => instance_id_to_judo(ip[:instance_id]), :assigned_to => ip_to_judo(ip[:public_ip]) } end end |
#keypair_file(&blk) ⇒ Object
289 290 291 292 293 294 295 |
# File 'lib/judo/base.rb', line 289 def keypair_file(&blk) Tempfile.open("judo.pem") do |file| file.write(s3_get("judo.pem")) file.flush blk.call(file.path) end end |
#mk_server_name(group) ⇒ Object
141 142 143 144 |
# File 'lib/judo/base.rb', line 141 def mk_server_name(group) index = servers.map { |s| (s.name =~ /^#{s.group.name}.(\d*)$/); $1.to_i }.sort.last.to_i + 1 "#{group}#{index}" end |
#new_server_id ⇒ Object
137 138 139 |
# File 'lib/judo/base.rb', line 137 def new_server_id rand(2**32).to_s(36) end |
#new_snapshot(name, server) ⇒ Object
153 154 155 156 157 |
# File 'lib/judo/base.rb', line 153 def new_snapshot(name, server) s = Judo::Snapshot.new(self, name, server) snapshots << s s end |
#reload_ec2_instances ⇒ Object
181 182 183 |
# File 'lib/judo/base.rb', line 181 def reload_ec2_instances @ec2_instance = nil end |
#s3 ⇒ Object
213 214 215 |
# File 'lib/judo/base.rb', line 213 def s3 @s3 ||= Aws::S3.new(access_id, access_secret, :logger => Logger.new(nil)) end |
#s3_get(k) ⇒ Object
225 226 227 |
# File 'lib/judo/base.rb', line 225 def s3_get(k) bucket.get(k) end |
#s3_put(k, file) ⇒ Object
229 230 231 |
# File 'lib/judo/base.rb', line 229 def s3_put(k, file) bucket.put(k, file) end |
#s3_url(k) ⇒ Object
221 222 223 |
# File 'lib/judo/base.rb', line 221 def s3_url(k) Aws::S3Generator::Key.new(bucket, k).get end |
#sdb ⇒ Object
97 98 99 |
# File 'lib/judo/base.rb', line 97 def sdb @sdb ||= Aws::SdbInterface.new(access_id, access_secret, :logger => Logger.new(nil)) end |
#server_domain ⇒ Object
29 30 31 |
# File 'lib/judo/base.rb', line 29 def server_domain "judo_servers" end |
#servers ⇒ Object
133 134 135 |
# File 'lib/judo/base.rb', line 133 def servers @servers ||= servers_state.map { |id,data| Judo::Server.new(self, id, data["group"].first) } end |
#servers_state ⇒ Object
125 126 127 |
# File 'lib/judo/base.rb', line 125 def servers_state @servers_state ||= fetch_servers_state end |
#set_db_version(new_version) ⇒ Object
297 298 299 |
# File 'lib/judo/base.rb', line 297 def set_db_version(new_version) update "dbversion" => new_version end |
#snapshot_domain ⇒ Object
33 34 35 |
# File 'lib/judo/base.rb', line 33 def snapshot_domain "judo_snapshots" end |
#snapshots ⇒ Object
129 130 131 |
# File 'lib/judo/base.rb', line 129 def snapshots @snapshots ||= snapshots_state.map { |name,data| Judo::Snapshot.new(self, name, data["server"].first) } end |
#snapshots_state ⇒ Object
121 122 123 |
# File 'lib/judo/base.rb', line 121 def snapshots_state @snapshots_state ||= fetch_snapshots_state end |
#state ⇒ Object
243 244 245 |
# File 'lib/judo/base.rb', line 243 def state @state ||= sdb.get_attributes(base_domain, "judo")[:attributes] end |
#task(msg, &block) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/judo/base.rb', line 165 def task(msg, &block) printf "---> %-24s ", "#{msg}..." STDOUT.flush start = Time.now result = block.call result = "done" unless result.is_a? String finish = Time.now time = sprintf("%0.1f", finish - start) puts "#{result} (#{time}s)" result end |
#update(attrs) ⇒ Object
i’m copy pasting code from server - this needs to be its own module
238 239 240 241 |
# File 'lib/judo/base.rb', line 238 def update(attrs) sdb.put_attributes(base_domain, "judo", attrs, :replace) state.merge! attrs end |
#volumes ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/judo/base.rb', line 72 def volumes @volumes ||= ec2_volumes.map do |v| { :id => v[:aws_id], :size => v[:aws_size], :status => v[:aws_status], :device => v[:aws_device], :instance_id => v[:aws_instance_id], :attached_to => instance_id_to_judo(v[:aws_instance_id]), :assigned_to => servers.detect { |s| s.volumes.invert[v[:aws_id]] } } end end |