Class: Visor::Meta::CLI
- Inherits:
-
Object
- Object
- Visor::Meta::CLI
- 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.('~/.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
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#argv ⇒ Object
readonly
Returns the value of attribute argv.
-
#cli_name ⇒ Object
readonly
Returns the value of attribute cli_name.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parser ⇒ Object
readonly
OptionParser parser.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#clean ⇒ Object
Remove all files created by the daemon.
- #default_opts ⇒ Object
-
#initialize(app, cli_name, argv = ARGV) ⇒ CLI
constructor
Initialize a CLI.
-
#launch! ⇒ Object
Launch the server.
-
#restart ⇒ Object
Restart server.
-
#run! ⇒ Object
Parse the current shell arguments and run the command.
-
#run_command ⇒ Object
Execute the command.
-
#start ⇒ Object
Start the server.
-
#status ⇒ Object
Display current server status.
-
#stop ⇒ Object
Stop the server.
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
#app ⇒ Object (readonly)
Returns the value of attribute app.
11 12 13 |
# File 'lib/meta/cli.rb', line 11 def app @app end |
#argv ⇒ Object (readonly)
Returns the value of attribute argv.
11 12 13 |
# File 'lib/meta/cli.rb', line 11 def argv @argv end |
#cli_name ⇒ Object (readonly)
Returns the value of attribute cli_name.
11 12 13 |
# File 'lib/meta/cli.rb', line 11 def cli_name @cli_name end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
11 12 13 |
# File 'lib/meta/cli.rb', line 11 def command @command end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
11 12 13 |
# File 'lib/meta/cli.rb', line 11 def env @env end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
11 12 13 |
# File 'lib/meta/cli.rb', line 11 def host @host end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/meta/cli.rb', line 11 def @options end |
#parser ⇒ Object (readonly)
OptionParser parser
47 48 49 |
# File 'lib/meta/cli.rb', line 47 def parser @parser end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
11 12 13 |
# File 'lib/meta/cli.rb', line 11 def port @port end |
Instance Method Details
#clean ⇒ Object
Remove all files created by the daemon.
129 130 131 132 133 134 135 136 137 |
# File 'lib/meta/cli.rb', line 129 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_opts ⇒ Object
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
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/meta/cli.rb', line 189 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 |
#restart ⇒ Object
Restart server
141 142 143 144 145 146 |
# File 'lib/meta/cli.rb', line 141 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.
97 98 99 100 101 102 103 104 105 |
# File 'lib/meta/cli.rb', line 97 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_command ⇒ Object
Execute the command
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/meta/cli.rb', line 109 def run_command unless NO_CONF_COMMANDS.include?(command) @conf = load_conf_file @host = [:host] || @conf[:bind_host] || DEFAULT_HOST @port = [:port] || @conf[:bind_port] || DEFAULT_PORT @env = [: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 |
#start ⇒ Object
Start the server
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/meta/cli.rb', line 174 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 |
#status ⇒ Object
Display current server status
150 151 152 153 154 155 156 |
# File 'lib/meta/cli.rb', line 150 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 |
#stop ⇒ Object
Stop the server
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/meta/cli.rb', line 160 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 |