Class: Cluster

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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

Constant Summary collapse

AUTHOR =
'Simon de Boer'
EMAIL =
'[email protected]'
NAME =
'cluster'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(infrastructure) ⇒ Cluster

Returns a new instance of Cluster.



37
38
39
40
41
# File 'lib/cluster.rb', line 37

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

Class Method Details

.set_credentials_fileObject



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

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

.versionObject



7
8
9
# File 'lib/cluster/version.rb', line 7

def version
  '0.8.2'
end

Instance Method Details

#authorize(*ips) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cluster.rb', line 25

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

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cluster.rb', line 107

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)/ # name / dns
    current.dns
  when /^i/ # ip
    current.ip
  when /^a/ # add
    @sub.alter_instances!(current) {|i| i.services = Array(params) }
  when /^e/ # enable
   @sub.alter_instances!(current) {|i| i.enable *params }
  when /^di/ # disable
   @sub.alter_instances!(current) {|i| i.disable *params }
  when /^s/ # state
   @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



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

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



221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/cluster.rb', line 221

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

#instance_state(state) ⇒ Object



146
147
148
# File 'lib/cluster.rb', line 146

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

#labeled(name) ⇒ Object



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

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

#machine(groups = []) ⇒ Object



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

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

#release(*params) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cluster.rb', line 57

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



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

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



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cluster.rb', line 13

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



181
182
183
184
185
186
187
188
189
# File 'lib/cluster.rb', line 181

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



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/cluster.rb', line 191

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



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

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

#service(roles) ⇒ Object



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

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

#start(machine_size, *services) ⇒ Object



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

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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/cluster.rb', line 150

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



69
70
71
# File 'lib/cluster.rb', line 69

def to_credentials
  {'credentials' => nil}
end

#update_image_file(filename) ⇒ Object



102
103
104
105
# File 'lib/cluster.rb', line 102

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