Class: Visor::Meta::CLI

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

Constant Summary collapse

COMMANDS =

Available commands

%w[start stop restart status clean]
NO_CONF_COMMANDS =

Commands that wont load options from the config file

%w[stop status]
DEFAULT_DIR =

Default config files directories to look at

File.expand_path('~/.visor')
DEFAULT_HOST =

Default host address

'0.0.0.0'
DEFAULT_PORT =

Default port

4567
DEFAULT_ENV =

Default application environment

:production

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, cli_name, argv = ARGV) ⇒ CLI

Initialize a CLI



29
30
31
32
33
34
35
36
# File 'lib/meta/cli.rb', line 29

def initialize(app, cli_name, argv=ARGV)
  @app = app
  @cli_name = cli_name
  @argv = argv
  @options = default_opts
  @parser = parser
  @command = parse!
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



11
12
13
# File 'lib/meta/cli.rb', line 11

def app
  @app
end

#argvObject (readonly)

Returns the value of attribute argv.



11
12
13
# File 'lib/meta/cli.rb', line 11

def argv
  @argv
end

#cli_nameObject (readonly)

Returns the value of attribute cli_name.



11
12
13
# File 'lib/meta/cli.rb', line 11

def cli_name
  @cli_name
end

#commandObject (readonly)

Returns the value of attribute command.



11
12
13
# File 'lib/meta/cli.rb', line 11

def command
  @command
end

#envObject (readonly)

Returns the value of attribute env.



11
12
13
# File 'lib/meta/cli.rb', line 11

def env
  @env
end

#hostObject (readonly)

Returns the value of attribute host.



11
12
13
# File 'lib/meta/cli.rb', line 11

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/meta/cli.rb', line 11

def options
  @options
end

#parserObject (readonly)

OptionParser parser



47
48
49
# File 'lib/meta/cli.rb', line 47

def parser
  @parser
end

#portObject (readonly)

Returns the value of attribute port.



11
12
13
# File 'lib/meta/cli.rb', line 11

def port
  @port
end

Instance Method Details

#cleanObject

Remove all files created by the daemon.



132
133
134
135
136
137
138
139
140
# File 'lib/meta/cli.rb', line 132

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

#default_optsObject



38
39
40
41
42
43
# File 'lib/meta/cli.rb', line 38

def default_opts
  {debug: false,
   foreground: false,
   no_proxy: false,
   environment: DEFAULT_ENV}
end

#launch!Object

Launch the server



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/meta/cli.rb', line 192

def launch!
  put_and_log :info, "Starting #{cli_name} at #{host}:#{port}"
  debug_settings

  Rack::Server.start(app: app,
                     Host: host,
                     Port: port,
                     environment: get_env,
                     daemonize: daemonize?,
                     pid: pid_file)
end

#restartObject

Restart server



144
145
146
147
148
149
# File 'lib/meta/cli.rb', line 144

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.



100
101
102
103
104
105
106
107
108
# File 'lib/meta/cli.rb', line 100

def run!
  if command.nil?
    abort @parser.to_s
  elsif COMMANDS.include?(command)
    run_command
  else
    abort "Unknown command: #{command}. Available commands: #{COMMANDS.join(', ')}"
  end
end

#run_commandObject

Execute the command



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

def run_command
  unless NO_CONF_COMMANDS.include?(command)
    @conf = load_conf_file
    @host = options[:host] || @conf[:bind_host] || DEFAULT_HOST
    @port = options[:port] || @conf[:bind_port] || DEFAULT_PORT
    @env = options[:environment]
  end

  case command
    when 'start' then start
    when 'stop' then stop
    when 'restart' then restart
    when 'status' then status
    else clean
  end
  exit 0
end

#startObject

Start the server



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/meta/cli.rb', line 177

def start
  FileUtils.mkpath(DEFAULT_DIR)
  begin
    is_it_running?
    can_use_port?
    write_url
    launch!
  rescue => e
    put_and_log :warn, "ERROR starting #{cli_name}: #{e}"
    exit! 1
  end
end

#statusObject

Display current server status



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

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

#stopObject

Stop the server



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/meta/cli.rb', line 163

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