Class: Puma::ControlCLI
- Inherits:
-
Object
- Object
- Puma::ControlCLI
- Defined in:
- lib/puma/control_cli.rb
Constant Summary collapse
- COMMANDS =
%w{halt restart phased-restart start stats status stop}
Instance Method Summary collapse
-
#initialize(argv, stdout = STDOUT, stderr = STDERR) ⇒ ControlCLI
constructor
A new instance of ControlCLI.
- #is_windows? ⇒ Boolean
- #message(msg) ⇒ Object
- #prepare_configuration ⇒ Object
- #run ⇒ Object
- #send_request ⇒ Object
- #send_signal ⇒ Object
Constructor Details
#initialize(argv, stdout = STDOUT, stderr = STDERR) ⇒ ControlCLI
Returns a new instance of ControlCLI.
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 |
# File 'lib/puma/control_cli.rb', line 16 def initialize(argv, stdout=STDOUT, stderr=STDERR) @argv = argv @stdout = stdout @stderr = stderr @options = {} opts = OptionParser.new do |o| o. = "Usage: pumactl (-p PID | -P pidfile | -S status_file | -C url -T token | -F config.rb) (#{COMMANDS.join("|")})" o.on "-S", "--state PATH", "Where the state file to use is" do |arg| @options[:state] = arg end o.on "-Q", "--quiet", "Not display messages" do |arg| @options[:quiet_flag] = true end o.on "-P", "--pidfile PATH", "Pid file" do |arg| @options[:pidfile] = arg end o.on "-p", "--pid PID", "Pid" do |arg| @options[:pid] = arg.to_i end o.on "-C", "--control-url URL", "The bind url to use for the control server" do |arg| @options[:control_url] = arg end o.on "-T", "--control-token TOKEN", "The token to use as authentication for the control server" do |arg| @options[:control_auth_token] = arg end o.on "-F", "--config-file PATH", "Puma config script" do |arg| @options[:config_file] = arg end o.on_tail("-H", "--help", "Show this message") do @stdout.puts o exit end o.on_tail("-V", "--version", "Show version") do puts Const::PUMA_VERSION exit end end opts.order!(argv) { |a| opts.terminate a } command = argv.shift @options[:command] = command if command Puma::Configuration.new(@options).load if @options[:config_file] # check present of command unless @options[:command] raise "Available commands: #{COMMANDS.join(", ")}" end unless COMMANDS.include? @options[:command] raise "Invalid command: #{@options[:command]}" end rescue => e @stdout.puts e. exit 1 end |
Instance Method Details
#is_windows? ⇒ Boolean
12 13 14 |
# File 'lib/puma/control_cli.rb', line 12 def is_windows? RUBY_PLATFORM =~ /(win|w)32$/ ? true : false end |
#message(msg) ⇒ Object
85 86 87 |
# File 'lib/puma/control_cli.rb', line 85 def (msg) @stdout.puts msg unless @options[:quiet_flag] end |
#prepare_configuration ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/puma/control_cli.rb', line 89 def prepare_configuration if @options.has_key? :state unless File.exist? @options[:state] raise "Status file not found: #{@options[:state]}" end status = YAML.load File.read(@options[:state]) if status.kind_of?(Hash) && status.has_key?("config") conf = status["config"] # get control_url if url = conf.[:control_url] @options[:control_url] = url end # get control_auth_token if token = conf.[:control_auth_token] @options[:control_auth_token] = token end # get pid @options[:pid] = status["pid"].to_i else raise "Invalid status file: #{@options[:state]}" end elsif @options.has_key? :pidfile # get pid from pid_file @options[:pid] = File.open(@options[:pidfile]).gets.to_i end end |
#run ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/puma/control_cli.rb', line 215 def run start if @options[:command] == "start" prepare_configuration if is_windows? send_request else @options.has_key?(:control_url) ? send_request : send_signal end rescue => e e. exit 1 end |
#send_request ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/puma/control_cli.rb', line 123 def send_request uri = URI.parse @options[:control_url] # create server object by scheme @server = case uri.scheme when "tcp" TCPSocket.new uri.host, uri.port when "unix" UNIXSocket.new "#{uri.host}#{uri.path}" else raise "Invalid scheme: #{uri.scheme}" end if @options[:command] == "status" "Puma is started" else url = "/#{@options[:command]}" if @options.has_key?(:control_auth_token) url = url + "?token=#{@options[:control_auth_token]}" end @server << "GET #{url} HTTP/1.0\r\n\r\n" unless data = @server.read raise "Server closed connection before responding" end response = data.split("\r\n") if response.empty? raise "Server sent empty response" end (@http,@code,@message) = response.first.split(" ",3) if @code == "403" raise "Unauthorized access to server (wrong auth token)" elsif @code == "404" raise "Command error: #{response.last}" elsif @code != "200" raise "Bad response from server: #{@code}" end "Command #{@options[:command]} sent success" response.last if @options[:command] == "stats" end @server.close end |
#send_signal ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/puma/control_cli.rb', line 174 def send_signal unless pid = @options[:pid] raise "Neither pid nor control url available" end begin Process.getpgid pid rescue SystemCallError if @options[:command] == "restart" @options.delete(:command) start else raise "No pid '#{pid}' found" end end case @options[:command] when "restart" Process.kill "SIGUSR2", pid when "halt" Process.kill "QUIT", pid when "stop" Process.kill "SIGTERM", pid when "stats" puts "Stats not available via pid only" return when "phased-restart" Process.kill "SIGUSR1", pid else "Puma is started" return end "Command #{@options[:command]} sent success" end |