Class: Cluster

Inherits:
Object
  • Object
show all
Defined in:
lib/cluster.rb,
lib/cluster/cli.rb,
lib/cluster/version.rb,
lib/cluster/configuration.rb

Defined Under Namespace

Modules: Logging Classes: Cli, Configuration, Version

Constant Summary collapse

AUTHOR =
'Simon de Boer'
EMAIL =
'[email protected]'
NAME =
'cluster'
BUCKET =
'oven'
LOCATION =
"http://oven.s3.amazonaws.com/cluster.gem"
IMAGES =
'http://oven.s3.amazonaws.com/cluster_images.yml'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(infrastructure) ⇒ Cluster

Returns a new instance of Cluster.



41
42
43
44
45
# File 'lib/cluster.rb', line 41

def initialize(infrastructure)
  self.class.set_credentials_file
  infrastructure.configure
  @sub = infrastructure
end

Class Method Details

.set_credentials_fileObject



266
267
268
269
270
271
272
273
274
# File 'lib/cluster.rb', line 266

def set_credentials_file
  unless Cluster::Configuration.credentials?
    if ENV['CREDENTIALS'] and File.exist?(ENV['CREDENTIALS'])
      Cluster::Configuration[:credentials_file] = ENV['CREDENTIALS']
    elsif ENV['HOME'] and File.exist?(File.join(ENV['HOME'], '.cluster', 'credentials.yml'))
      Cluster::Configuration[:credentials_file] = File.join(ENV['HOME'], '.cluster', 'credentials.yml')
    end
  end
end

Instance Method Details

#authorize(*ips) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cluster.rb', line 29

def authorize(*ips)
  if ips.empty?
    current_ip = ''
    require 'open-uri'
    open('http://checkip.dyndns.com').each do |line|
            current_ip = $1 and break if line =~ /IP Address: ([\d\.]+)</
    end
    ips << "#{current_ip}/32"
  end
  @sub.authorize(ips)
end

#cost(*sizes) ⇒ Object



9
10
11
# File 'lib/cluster.rb', line 9

def cost(*sizes)
  @sub.cost(sizes)
end

#create_data_store(name) ⇒ Object



90
91
92
# File 'lib/cluster.rb', line 90

def create_data_store(name)
  @sub.create_data_store(name)
end

#create_file_store(name) ⇒ Object



86
87
88
# File 'lib/cluster.rb', line 86

def create_file_store(name)
  @sub.create_file_store(name)
end

#credentials_urlObject



228
229
230
# File 'lib/cluster.rb', line 228

def credentials_url
  @sub.credentials_url
end

#current(*params) ⇒ Object Also known as: instance



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cluster.rb', line 128

def current(*params)
  unless @sub.in_cluster?
    msg = "#{Cluster::NAME} says that we aren't in the cluster?"
    $stderr.puts  msg
    raise RuntimeError.new(msg)
  end

  unless params.length > 0
    msg = "#{Cluster::NAME} current needs to know what to do?\n\t* services\n\t* name (|| dns)\n\t* ip\n\t* enable\n\t* disable"
    $stderr.puts msg
    raise ArgumentError.new(msg)
  end

  current = @sub.current_instance
  cmd = params.shift

  case cmd.downcase
  when 'services'
    current.services - current.disabled_services
  when /^(n|dn)/
    current.dns
  when /^i/
    current.ip
  when /^e/
   @sub.alter_instances!(current) {|i| i.enable *params }
  when /^di/
   @sub.alter_instances!(current) {|i| i.disable *params }
  when /^s/
   @sub.alter_instances!(current) {|i| i.set_state *params }
  else
    msg = "#{Cluster::NAME} curent did not understand '#{params.join(' ')}'"
    $stderr.puts msg
    raise ArgumentError.new(msg)
  end
end

#fetch_credentials(url) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/cluster.rb', line 232

def fetch_credentials(url)
  unless Cluster::Configuration.credentials?
    msg = "Need to know where to save the incoming credentials."
    $stderr.puts msg
    raise RuntimeError.new(msg)
  end

  out = File.open(Cluster::Configuration[:credentials_file], 'w')
  open(url).each do |l| out.write(l); end
  out.close
end

#fetch_monitor(output) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/cluster.rb', line 244

def fetch_monitor(output)
  monitor = @sub.fetch_monitor
  unless monitor
    msg = "#{Cluster::NAME} cannot find any monitor information."
    $stderr.puts msg
    raise RuntimeError.new(msg)
  end

  File.open(output, 'w') {|f|
    f.write(monitor)
  }
end

#gemurlObject



257
258
259
# File 'lib/cluster.rb', line 257

def gemurl
  Cluster::LOCATION
end

#imageurlObject



261
262
263
# File 'lib/cluster.rb', line 261

def imageurl
  Cluster::IMAGES
end

#instance_state(state) ⇒ Object



165
166
167
# File 'lib/cluster.rb', line 165

def instance_state(state)
  @sub.current_instance.set_state(state)
end

#labeled(name) ⇒ Object



66
67
68
# File 'lib/cluster.rb', line 66

def labeled(name)
  @sub.instances.select {|i| i.identified_by? name}
end

#machine(groups = []) ⇒ Object



47
48
49
50
# File 'lib/cluster.rb', line 47

def machine(groups = [])
  res = machines(groups)
  res.empty? ? nil : res.first
end

#machines(groups = []) ⇒ Object Also known as: instances



52
53
54
# File 'lib/cluster.rb', line 52

def machines(groups = [])
  @sub.machines(groups)
end

#period(*args) ⇒ Object



13
14
15
# File 'lib/cluster.rb', line 13

def period(*args)
  @sub.period(args)
end

#release(*params) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cluster.rb', line 70

def release(*params)
  klass = @sub.release_class
  env, tag = params
  if tag
    klass.find(env, tag) or klass.create(:environment => env, :tag => tag)
  elsif env
    klass.current(env)
  else
    nil
  end
end

#retrieve(service, key, output = nil) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/cluster.rb', line 185

def retrieve(service, key, output = nil)
  res = @sub.retrieve File.join(service, key)
  if output
    begin
      File.open(output, 'w') {|f| f.write res }
    rescue => err
      msg = "#{Cluster::NAME} cannot open #{output} for writing."
      $stderr.puts msg
      raise RuntimeError.new(msg)
    end
  else
    res
  end
end

#revoke(*ips) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cluster.rb', line 17

def revoke(*ips)
  if ips.empty?
    current_ip = ''
    require 'open-uri'
    open('http://checkip.dyndns.com').each do |line|
            current_ip = $1 and break if line =~ /IP Address: ([\d\.]+)</
    end
    ips << "#{current_ip}/32"
  end
  @sub.revoke(ips)
end

#save_credentials(location = nil) ⇒ Object



200
201
202
203
204
205
206
207
208
# File 'lib/cluster.rb', line 200

def save_credentials(location = nil)
  creds = if Cluster::Configuration.credentials?
            File.open(Cluster::Configuration[:credentials_file])
          else
            cx = @sub.to_credentials.merge to_credentials
            StringIO.new(cx.to_yaml)
          end
  @sub.save_credentials(creds, location)
end

#save_monitor(filename, key = nil) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/cluster.rb', line 210

def save_monitor(filename, key = nil)
  file = open(filename)
  unless file
    msg = "#{Cluster::NAME} cannot open file '#{filename}' for reading."
    $stderr.puts msg
    raise RuntimeError.new(msg)
  end

  key ||= File.basename(filename)
  begin
    @sub.save_monitor file, key
  rescue => err
    msg = "#{Cluster::NAME} could not save monitor configuration: #{err.message}\n\t#{err.backtrace.join("\n\t")}"
    $stderr.puts msg
    raise RuntimeError.new(msg)
  end
end

#security(*groups) ⇒ Object



4
5
6
7
# File 'lib/cluster.rb', line 4

def security(*groups)
  groups << 'access' if groups.empty?
  @sub.security(groups)
end

#service(roles) ⇒ Object



57
58
59
60
# File 'lib/cluster.rb', line 57

def service(roles)
  res = services(roles)
  res.empty? ? nil : res.first
end

#services(roles = []) ⇒ Object



62
63
64
# File 'lib/cluster.rb', line 62

def services(roles = [])
  @sub.services(roles)
end

#start(machine_size, *services) ⇒ Object



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
# File 'lib/cluster.rb', line 94

def start(machine_size, *services)
  size = machine_size.to_s.strip.downcase
  unless @sub.class.machine_sizes.include? size
    msg = "#{Cluster::NAME} needs to have a size from: #{@sub.class.machine_sizes.join(', ')}"
    puts msg
    raise msg
  end
  # FIXME need a way to pass through a number argument
  number ||= 1

  services = services.map {|s|
    s.split(',')
  }.flatten

  # This may not be necessary, but might as well make sure they are
  # up to date any time that we are starting a new instance.
  self.save_credentials

  begin
    number.times.map do |n|
      @sub.new_instance(size, services)
    end
  rescue => err
    msg ="#{Cluster::NAME} cannot start new instance: #{err.message}\n\t#{err.backtrace.join("\n\t")}"
    $stderr.puts msg
    raise RuntimeError.new(msg)
  end
end

#store(service, key, filename = nil) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/cluster.rb', line 169

def store(service, key, filename = nil)
  full_key = File.join service, key
  file = if filename and File.readable? filename
           File.open(filename, 'r')
         elsif File.readable? full_key
           File.open(full_key, 'r')
         elsif File.readable? File.basename(full_key)
           File.open(File.basename(full_key))
         else
           msg = "#{Cluster::NAME} cannot open a file for storage with key: #{full_key}"
           $stderr.puts msg
           raise RuntimeError.new(msg)
         end
  @sub.store full_key, file
end

#to_credentialsObject



82
83
84
# File 'lib/cluster.rb', line 82

def to_credentials
  {'credentials' => nil}
end

#update_image_file(filename) ⇒ Object



123
124
125
126
# File 'lib/cluster.rb', line 123

def update_image_file(filename)
  input = File.open(filename)
  @sub.save_images(input)
end