Class: Puma::ControlCLI

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/control_cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdout = STDOUT) ⇒ ControlCLI

Returns a new instance of ControlCLI.



14
15
16
17
# File 'lib/puma/control_cli.rb', line 14

def initialize(argv, stdout=STDOUT)
  @argv = argv
  @stdout = stdout
end

Instance Method Details

#command_haltObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/puma/control_cli.rb', line 98

def command_halt
  sock = connect
  body = request sock, "/halt"

  if body != '{ "status": "ok" }'
    raise "Invalid response: '#{body}'"
  else
    @stdout.puts "Requested halt from server"
  end
end

#command_pidObject



83
84
85
# File 'lib/puma/control_cli.rb', line 83

def command_pid
  @stdout.puts "#{@state['pid']}"
end

#command_restartObject



109
110
111
112
113
114
115
116
117
118
# File 'lib/puma/control_cli.rb', line 109

def command_restart
  sock = connect
  body = request sock, "/restart"

  if body != '{ "status": "ok" }'
    raise "Invalid response: '#{body}'"
  else
    @stdout.puts "Requested restart from server"
  end
end

#command_statsObject



120
121
122
123
124
125
# File 'lib/puma/control_cli.rb', line 120

def command_stats
  sock = connect
  body = request sock, "/stats"

  @stdout.puts body
end

#command_stopObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/puma/control_cli.rb', line 87

def command_stop
  sock = connect
  body = request sock, "/stop"

  if body != '{ "status": "ok" }'
    raise "Invalid response: '#{body}'"
  else
    @stdout.puts "Requested stop from server"
  end
end

#connectObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/puma/control_cli.rb', line 27

def connect
  if str = @config.options[:control_url]
    uri = URI.parse str
    case uri.scheme
    when "tcp"
      return TCPSocket.new uri.host, uri.port
    when "unix"
      path = "#{uri.host}#{uri.path}"
      return UNIXSocket.new path
    else
      raise "Invalid URI: #{str}"
    end
  end

  raise "No status address configured"
end

#request(sock, url) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/puma/control_cli.rb', line 63

def request(sock, url)
  token = @config.options[:control_auth_token]
  if token
    url = "#{url}?token=#{token}"
  end

  sock << "GET #{url} HTTP/1.0\r\n\r\n"

  rep = sock.read.split("\r\n")

  m = %r!HTTP/1.\d (\d+)!.match(rep.first)
  if m[1] == "403"
    raise "Unauthorized access to server (wrong auth token)"
  elsif m[1] != "200"
    raise "Bad response code from server: #{m[1]}"
  end

  return rep.last
end

#runObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/puma/control_cli.rb', line 44

def run
  setup_options

  @parser.parse! @argv

  @state = YAML.load File.read(@path)
  @config = @state['config']

  cmd = @argv.shift

  meth = "command_#{cmd}"

  if respond_to?(meth)
    __send__(meth)
  else
    raise "Unknown command: #{cmd}"
  end
end

#setup_optionsObject



19
20
21
22
23
24
25
# File 'lib/puma/control_cli.rb', line 19

def setup_options
  @parser = OptionParser.new do |o|
    o.on "-S", "--state PATH", "Where the state file to use is" do |arg|
      @path = arg
    end
  end
end