Class: Visor::Image::CLI

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

Constant Summary collapse

COMMANDS =

Available commands

%w[start stop restart status clean]
NO_CONF_LOAD =

Commands that wont load options from the config file

%w[stop status clean]
DEFAULT_DIR =

Default files directory

File.expand_path('~/.visor')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV) ⇒ CLI

Initialize a new CLI



23
24
25
26
27
28
29
30
31
# File 'lib/image/cli.rb', line 23

def initialize(argv=ARGV)
  @argv      = argv
  @options   = {}
  @conf_file = load_conf_file
  @options   = defaults
  @new_opts  = []
  @parser    = parser
  @command   = parse!
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



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

def argv
  @argv
end

#commandObject (readonly)

Returns the value of attribute command.



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

def command
  @command
end

#conf_fileObject (readonly)

Returns the value of attribute conf_file.



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

def conf_file
  @conf_file
end

#new_optsObject (readonly)

Returns the value of attribute new_opts.



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

def new_opts
  @new_opts
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#parserObject (readonly)

OptionParser parser



46
47
48
# File 'lib/image/cli.rb', line 46

def parser
  @parser
end

Instance Method Details

#cleanObject

Remove all files created by the daemon.



145
146
147
148
149
150
151
# File 'lib/image/cli.rb', line 145

def clean
  begin
    FileUtils.rm(pid_file) rescue Errno::ENOENT
    FileUtils.rm(url_file) rescue Errno::ENOENT
  end
  put_and_log :warn, "Removed all tracking files created at server start"
end

#defaultsObject

Generate the default options



34
35
36
37
38
39
40
41
42
43
# File 'lib/image/cli.rb', line 34

def defaults
  {:config     => ENV['GOLIATH_CONF'],
   :address    => conf_file[:bind_host],
   :port       => conf_file[:bind_port],
   :log_file   => File.join(File.expand_path(conf_file[:log_path]), conf_file[:log_file]),
   :pid_file   => File.join(DEFAULT_DIR, 'visor_api.pid'),
   :env        => :production,
   :daemonize  => true,
   :log_stdout => false}
end

#launch!Object

Launch the server



198
199
200
201
202
203
204
205
# File 'lib/image/cli.rb', line 198

def launch!
  put_and_log :info, "Starting visor-image at #{options[:address]}:#{options[:port]}"
  debug_settings

  runner     = Goliath::Runner.new(opts_to_goliath, Visor::Image::Server.new)
  runner.app = Goliath::Rack::Builder.build(Visor::Image::Server, runner.api)
  runner.run
end

#restartObject

Restart server



154
155
156
157
158
159
# File 'lib/image/cli.rb', line 154

def restart
  @restart = true
  stop
  sleep 0.1 while running?
  start
end

#run!Object

Parse the current shell arguments and run the command. Exits on error.



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
# File 'lib/image/cli.rb', line 117

def run!
  if command.nil?
    abort @parser.to_s

  elsif COMMANDS.include?(command)
    unless NO_CONF_LOAD.include?(command)
      @options.merge!({address: options[:address], port: options[:port]})
    end

    case command
    when 'start' then
      start
    when 'stop' then
      stop
    when 'restart' then
      restart
    when 'status' then
      status
    else
      clean
    end
    exit 0
  else
    abort "Unknown command: #{command}. Available commands: #{COMMANDS.join(', ')}"
  end
end

#startObject

Start the server



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/image/cli.rb', line 184

def start
  FileUtils.mkpath(DEFAULT_DIR) unless Dir.exists?(DEFAULT_DIR)
  begin
    is_it_running?
    can_use_port?
    write_url
    launch!
  rescue => e
    put_and_log :warn, "Error starting visor-image: #{e.message}\n#{e.backtrace.to_s}"
    exit! 1
  end
end

#statusObject

Display current server status



162
163
164
165
166
167
168
# File 'lib/image/cli.rb', line 162

def status
  if running?
    STDERR.puts "visor-image is running PID: #{fetch_pid} URL: #{fetch_url}"
  else
    STDERR.puts "visor-image is not running."
  end
end

#stopObject

Stop the server



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/image/cli.rb', line 171

def stop
  begin
    pid = File.read(pid_file)
    put_and_log :warn, "Stopping visor-image with PID: #{pid.to_i} Signal: INT"
    Process.kill(:INT, pid.to_i)
    File.delete(url_file)
  rescue
    put_and_log :warn, "Cannot stop visor-image, is it running?"
    exit! 1
  end
end