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 =
'sdeboer@InGamerSports.com'
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


257
258
259
260
261
262
263
264
265
# File 'lib/cluster.rb', line 257

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


221
222
223
# File 'lib/cluster.rb', line 221

def credentials_url
  @sub.credentials_url
end

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


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

def current(*params)
  unless @sub.in_cluster?
    puts "#{Cluster::NAME} says that we aren't in the cluster?"
    exit 2
  end

  unless params.length > 0
    puts "#{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"
    exit 2
  end

  cmd = params.shift

  case cmd.downcase
  when 'services'
    @sub.current_instance.services
  when /^(n|dn)/
    @sub.current_instance.dns
  when /^i/
    @sub.current_instance.ip
  when /^e/
   @sub.alter_instances!(@sub.current_instance) {|i| i.enable *params }
  when /^di/
   @sub.alter_instances!(@sub.current_instance) {|i| i.disable *params }
  when /^s/
   @sub.alter_instances!(@sub.current_instance) {|i| i.set_state *params }
  else
    puts "${Cluster::NAME} current did not understand '#{params.join(' ')}'"
    exit 1
  end
end

#fetch_credentials(url) ⇒ Object


225
226
227
228
229
230
231
232
233
234
# File 'lib/cluster.rb', line 225

def fetch_credentials(url)
  unless Cluster::Configuration.credentials?
    puts "Need to know where to save the incoming credentials."
    exit 2
  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


236
237
238
239
240
241
242
243
244
245
246
# File 'lib/cluster.rb', line 236

def fetch_monitor(output)
  monitor = @sub.fetch_monitor
  unless monitor
    puts "#{Cluster::NAME} cannot find any monitor information."
    exit 1
  end

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

#gemurlObject


248
249
250
# File 'lib/cluster.rb', line 248

def gemurl
  Cluster::LOCATION
end

#imageurlObject


252
253
254
# File 'lib/cluster.rb', line 252

def imageurl
  Cluster::IMAGES
end

#instance_state(state) ⇒ Object


160
161
162
# File 'lib/cluster.rb', line 160

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


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/cluster.rb', line 179

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
      puts "#{Cluster::NAME} cannot open #{output} for writing."
      exit 2
    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


195
196
197
198
199
200
201
202
203
# File 'lib/cluster.rb', line 195

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


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/cluster.rb', line 205

def save_monitor(filename, key = nil)
  file = open(filename)
  unless file
    puts "#{Cluster::NAME} cannot open file '#{filename}' for reading."
    exit 2
  end

  key ||= File.basename(filename)
  begin
    @sub.save_monitor file, key
  rescue => err
    puts "#{Cluster::NAME} could not save monitor configuration: #{err.message}\n\t#{err.backtrace.join("\n\t")}"
    exit 2
  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
# 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
    puts "#{Cluster::NAME} cannot start new instance: #{err.message}\n\t#{err.backtrace.join("\n\t")}"
    exit 2
  end
end

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


164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/cluster.rb', line 164

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
           puts "#{Cluster::NAME} cannot open a file for storage with key: #{full_key}"
           exit 2
         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


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

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