Class: Cluster::Cli

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

Instance Method Summary collapse

Constructor Details

#initialize(arguments = nil) ⇒ Cli

Returns a new instance of Cli.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cluster/cli.rb', line 5

def initialize(arguments = nil)
  arguments ||= ARGV

  unless arguments.length >= 1
    puts "#{Cluster::NAME} usage: #{Cluster::NAME} [infa args] command [command args]"
    exit 1
  end

  if arguments.include?('--version')
    puts "#{Cluster::NAME} version #{Cluster.version}"
    exit 0
  end

  infra = []
  command = nil
  params = []
  for arg in arguments
    if command
      params << arg
    elsif arg =~ /^-/
      infra << arg
    else
      command = arg
    end
  end

  if arg = infra.detect {|a| a =~ /^(-c|--credentials=)(.+)/ }
    file = $2
    file = (file[0, 1] == File::SEPARATOR) ? file : File.join(ENV['PWD'], file)
    
    Cluster::Configuration[:credentials_file] = file
    infra.delete arg
  else
    Cluster.set_credentials_file
  end

  logger_file = if arg = infra.detect {|a| a =~ /^--logger=(.+)$/ }
                  infra.delete arg
                  $1
                elsif Cluster::Configuration[:credentials_file]
                  File.join(File.dirname(Cluster::Configuration[:credentials_file]), 'cluster.log')
                else
                  '/tmp/cluster.log'
                end

  unless command
    puts "#{Cluster::NAME} usage: #{Cluster::NAME} [infa args] command [command args]"
    exit 1
  end

  file = File.open(logger_file, File::WRONLY | File::APPEND | File::CREAT)

  $stderr = file
  Cluster::Configuration['logger'] = Logger.new(file, 5, 512000)

  @sub = Infrastructure.connect(infra)

  @named_output = (!params.empty? and params[0] == '-d' and params.shift)
  @ip_output = (!params.empty? and params[0] == '-i' and params.shift)
  @id_output = (!params.empty? and params[0] == '-I' and params.shift)

  @cluster = Cluster.new @sub
  if respond_to? command
    self.send command, *params
  elsif @cluster.respond_to? command
    puts @cluster.send(command, *params)
  else
    STDERR.puts "#{Cluster::NAME} does not understand #{command}."
  end
end

Instance Method Details

#add_self(name, services) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cluster/cli.rb', line 140

def add_self(name, services)
  @sub.update_instances
  ins = @sub.current_instance
  ins.state ||= 'starting'
  ins.friendly_name = name
  servs = services.split ','
  unless servs.empty?
    ins.enable servs
  end
  @sub.alter_instances! ins
  puts ins.aws_id
end

#alter_instances(ids) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cluster/cli.rb', line 113

def alter_instances(ids)
  altered = @sub.instances.map {|ins|
    if ids.any? {|p| ins.identified_by? p}
      yield ins
      ins
    else
      nil
    end
  }.compact

  @sub.alter_instances! altered
  puts "Instances altered :"
  for ins in altered 
    puts ins.to_s(:long)
  end
end

#disable(*params) ⇒ Object



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

def disable(*params)
  service_list = params.shift

  if !service_list or params.empty?
    STDERR.puts "Usage: #{Cluster::NAME} [infra] disable service[,services] instance [instances]"
    exit 2
  end

  servs = service_list.split(',')
  alter_instances(params) {|ins| ins.disable servs }
end

#enable(*params) ⇒ Object



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

def enable(*params)
  service_list = params.shift

  if !service_list or params.empty?
    STDERR.puts "Usage: #{Cluster::NAME} [infra] enable service[,services] instance [instances]"
    exit 2
  end

  servs = service_list.split(',')
  alter_instances(params) {|ins| ins.enable servs }
end

#label(name, id = nil) ⇒ Object



177
178
179
180
181
# File 'lib/cluster/cli.rb', line 177

def label(name, id = nil)
  id ||= @sub.current_instance.id

  alter_instances([id]) {|i| i.friendly_name = name.downcase}
end

#labeled(*params) ⇒ Object



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

def labeled(*params)
  print_instances @cluster.labeled(params.first)
end

#list(*params) ⇒ Object



91
92
93
94
95
# File 'lib/cluster/cli.rb', line 91

def list(*params)
  print_instances(@sub.instances) if params.empty?

  print_instances @sub.instances.select {|i| params.any? {|p| i.identified_by? p } }
end

#machines(*params) ⇒ Object



76
77
78
79
# File 'lib/cluster/cli.rb', line 76

def machines(*params)
  res = @cluster.machines(*params)
  print_instances(res)
end


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cluster/cli.rb', line 97

def print_instances(instances)
  return if instances.empty?

  for instance in instances
    if @named_output
      puts instance.dns
    elsif @ip_output
      puts instance.ip
    elsif @id_output
      puts instance.id
    else
      puts instance.to_s(:long)
    end
  end
end

#release(*params) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/cluster/cli.rb', line 200

def release(*params)
  if params.empty?
    puts "Need an environment to work on for releases."
    exit 1
  end

  tag_output = (!params.empty? and params.delete('-t'))
  rel = @cluster.release(*params)
  if !rel
    puts "No release found for environment #{params.first}"
    exit 1
  elsif tag_output
    puts rel.tag
  else
    puts "#{rel.environment} was released at #{rel.created_at} with '#{rel.tag}'"
  end
end

#remove(name) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/cluster/cli.rb', line 130

def remove(name)
  ins = @sub.instances.detect {|i| i.identified_by? name }
  if ins
    ins.remove
    puts "Instance removed #{ins.id}"
  else
    puts "No instance removed for #{name}"
  end
end

#security(*args) ⇒ Object



218
219
220
221
222
# File 'lib/cluster/cli.rb', line 218

def security(*args)
  @cluster.security(*args).each {|k, v|
    puts "#{k}: #{v.join(' ')}"
  }
end

#services(*params) ⇒ Object Also known as: service



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

def services(*params)
  res = @cluster.services(*params)
  print_instances(res)
end

#set_state(state, *ids) ⇒ Object



192
193
194
# File 'lib/cluster/cli.rb', line 192

def set_state(state, *ids)
  alter_instances(ids) {|i| i.set_state state }
end

#stop(*ids) ⇒ Object



196
197
198
# File 'lib/cluster/cli.rb', line 196

def stop(*ids)
  alter_instances(ids) {|i| i.stop! }
end

#unlabel(id = nil) ⇒ Object



187
188
189
190
# File 'lib/cluster/cli.rb', line 187

def unlabel(id = nil)
  id ||= @sub.current_instance.id
  alter_instances([id]) {|i| i.friendly_name = ''}
end

#update_machinesObject



183
184
185
# File 'lib/cluster/cli.rb', line 183

def update_machines
  @sub.update_machines
end