Class: Judo::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/judo/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Base

Returns a new instance of Base.



12
13
14
15
# File 'lib/judo/base.rb', line 12

def initialize(options)
  @access_id     = options[:access_id]
  @access_secret = options[:access_secret]
end

Instance Attribute Details

#groupObject

Returns the value of attribute group.



3
4
5
# File 'lib/judo/base.rb', line 3

def group
  @group
end

Class Method Details

.defaultsObject



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_idObject



17
18
19
# File 'lib/judo/base.rb', line 17

def access_id
  @access_id || (raise JudoError, "no AWS Access ID specified")
end

#access_secretObject



21
22
23
# File 'lib/judo/base.rb', line 21

def access_secret
  @access_secret || (raise JudoError, "no AWS Secret Key specified")
end

#base_domainObject



37
38
39
# File 'lib/judo/base.rb', line 37

def base_domain
  "judo_base"
end

#bucketObject



217
218
219
# File 'lib/judo/base.rb', line 217

def bucket
  @bucket ||= s3.bucket(bucket_name, true)
end

#bucket_nameObject



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, options)
  s = Judo::Server.new(self, new_server_id, group)
  servers << s
  s.create(name, options)
  s
end

#ec2Object



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_instancesObject



193
194
195
# File 'lib/judo/base.rb', line 193

def ec2_instances
  @ec2_instance ||= ec2.describe_instances
end

#ec2_snapshotsObject



189
190
191
# File 'lib/judo/base.rb', line 189

def ec2_snapshots
  @ec2_snapshots ||= ec2.describe_snapshots([], :owner => "self")
end

#ec2_volumesObject



185
186
187
# File 'lib/judo/base.rb', line 185

def ec2_volumes
  @ec2_volumes ||= ec2.describe_volumes
end

#ensure_db_versionObject



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_initObject



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_setupObject



247
248
249
250
# File 'lib/judo/base.rb', line 247

def ensure_setup
  ensure_init
  ensure_db_version
end

#fetch_servers_stateObject



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_stateObject



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_versionObject



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_versionsObject



201
202
203
# File 'lib/judo/base.rb', line 201

def group_versions
  @group_version ||= sdb.get_attributes(base_domain, "group_versions")[:attributes]
end

#groupsObject



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

Returns:

  • (Boolean)


260
261
262
# File 'lib/judo/base.rb', line 260

def has_init?
  sdb.list_domains[:domains].include?(base_domain)
end

#init_keypairObject



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_sdbObject



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_groupObject



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.authorize_security_group_IP_ingress("judo", 22, 22,'tcp','0.0.0.0/0')
    rescue Aws::AwsError => e
      raise unless e.message =~ /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

#ipsObject



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_idObject



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_instancesObject



181
182
183
# File 'lib/judo/base.rb', line 181

def reload_ec2_instances
  @ec2_instance = nil
end

#s3Object



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

#sdbObject



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_domainObject



29
30
31
# File 'lib/judo/base.rb', line 29

def server_domain
  "judo_servers"
end

#serversObject



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_stateObject



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_domainObject



33
34
35
# File 'lib/judo/base.rb', line 33

def snapshot_domain
  "judo_snapshots"
end

#snapshotsObject



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_stateObject



121
122
123
# File 'lib/judo/base.rb', line 121

def snapshots_state
  @snapshots_state ||= fetch_snapshots_state
end

#stateObject



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

#volumesObject



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