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
75
76
77
78
79
80
81
82
83
84
85
86
# 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::STRING}"
    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

  if command.downcase == 'gemurl'
    # We don't need to do any infrastructure for this one...and 
    # we may not have it yet...
    puts Cluster::LOCATION
    exit 0
  elsif command.downcase == 'imageurl'
    # We don't need to do any infrastructure for this one...and 
    # we may not have it yet...
    puts Cluster::IMAGES
    exit 0
  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

#alter_instances(ids) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/cluster/cli.rb', line 125

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



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

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



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

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) ⇒ Object



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

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

#labeled(*params) ⇒ Object



99
100
101
# File 'lib/cluster/cli.rb', line 99

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

#list(*params) ⇒ Object



103
104
105
106
107
# File 'lib/cluster/cli.rb', line 103

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



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

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


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/cluster/cli.rb', line 109

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



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

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

#security(*args) ⇒ Object



200
201
202
203
204
# File 'lib/cluster/cli.rb', line 200

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

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



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

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

#set_state(state, *ids) ⇒ Object



174
175
176
# File 'lib/cluster/cli.rb', line 174

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

#stop(*ids) ⇒ Object



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

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

#unlabel(id) ⇒ Object



170
171
172
# File 'lib/cluster/cli.rb', line 170

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