Class: Gretl::Client
- Inherits:
-
Object
- Object
- Gretl::Client
- Defined in:
- lib/gretl.rb,
lib/gretl.rb
Instance Attribute Summary collapse
-
#k8s ⇒ Object
readonly
Returns the value of attribute k8s.
Instance Method Summary collapse
- #active ⇒ Object
- #add(port, name: nil, cmd: nil, group: nil, cwd: nil) ⇒ Object
-
#agent_list ⇒ Object
── Agent gateway ─────────────────────────────────────────────────────────.
- #agent_logs(name, lines: 50) ⇒ Object
- #agent_spindown(name) ⇒ Object
- #agent_spinup(name, cmd: nil, port: nil) ⇒ Object
- #inactive ⇒ Object
-
#initialize(token: nil) ⇒ Client
constructor
A new instance of Client.
- #port(query) ⇒ Object
- #ports ⇒ Object
- #remove(query) ⇒ Object
- #start(query) ⇒ Object
- #start_group(group) ⇒ Object
- #status_group(group) ⇒ Object
- #stop(query) ⇒ Object
- #stop_group(group) ⇒ Object
- #wait_for_active(query, timeout: 30, interval: 0.5) ⇒ Object
- #wait_for_inactive(query, timeout: 30, interval: 0.5) ⇒ Object
Constructor Details
Instance Attribute Details
#k8s ⇒ Object (readonly)
Returns the value of attribute k8s.
250 251 252 |
# File 'lib/gretl.rb', line 250 def k8s @k8s end |
Instance Method Details
#active ⇒ Object
75 |
# File 'lib/gretl.rb', line 75 def active = Gretl._fetch_ports.select(&:active) |
#add(port, name: nil, cmd: nil, group: nil, cwd: nil) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/gretl.rb', line 132 def add(port, name: nil, cmd: nil, group: nil, cwd: nil) Gretl._ensure_daemon uri = URI("#{Gretl::BASE}/ports") body = { port: port } body[:name] = name if name body[:cmd] = cmd if cmd body[:group] = group if group body[:cwd] = cwd if cwd http = Net::HTTP.new(uri.host, uri.port) req = Net::HTTP::Post.new(uri, "Content-Type" => "application/json") req.body = body.to_json res = http.request(req) data = JSON.parse(res.body, symbolize_names: true) Gretl::PortInfo.new(**data.slice(*Gretl::PortInfo.members)) end |
#agent_list ⇒ Object
── Agent gateway ─────────────────────────────────────────────────────────
338 339 340 |
# File 'lib/gretl.rb', line 338 def agent_list Gretl._fetch_ports.select { |p| p.group&.downcase == "agents" } end |
#agent_logs(name, lines: 50) ⇒ Object
362 363 364 365 366 367 |
# File 'lib/gretl.rb', line 362 def agent_logs(name, lines: 50) match = agent_list.find { |p| p.name.downcase == name.downcase } raise ArgumentError, "No agent found with name #{name.inspect}" unless match raw = Gretl._request(:get, "/ports/#{match.port}/logs?lines=#{lines}") raw[:lines] || [] end |
#agent_spindown(name) ⇒ Object
355 356 357 358 359 360 |
# File 'lib/gretl.rb', line 355 def agent_spindown(name) match = agent_list.find { |p| p.name.downcase == name.downcase } raise ArgumentError, "No agent found with name #{name.inspect}" unless match Gretl._request(:post, "/ports/#{match.port}/stop") if match.active Gretl._request(:delete, "/ports/#{match.port}") end |
#agent_spinup(name, cmd: nil, port: nil) ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/gretl.rb', line 342 def agent_spinup(name, cmd: nil, port: nil) unless port used = Gretl._fetch_ports.map(&:port).to_set port = 8100 port += 1 while used.include?(port) && port <= 8200 raise "No available ports in the agent range (8100–8200)" if port > 8200 end add(port, name: name, cmd: cmd || "", group: "Agents") Gretl._request(:post, "/ports/#{port}/start") raw = Gretl._request(:get, "/ports/#{port}") Gretl::PortInfo.new(**raw.slice(*Gretl::PortInfo.members)) end |
#inactive ⇒ Object
76 |
# File 'lib/gretl.rb', line 76 def inactive = Gretl._fetch_ports.reject(&:active) |
#ports ⇒ Object
73 |
# File 'lib/gretl.rb', line 73 def ports = Gretl._fetch_ports |
#remove(query) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/gretl.rb', line 116 def remove(query) matches = Gretl._resolve(query) raise ArgumentError, "No port found matching #{query.inspect}" if matches.empty? matches.each { |p| Gretl._request(:delete, "/ports/#{p.port}") } matches end |
#start(query) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/gretl.rb', line 78 def start(query) matches = Gretl._resolve(query) raise ArgumentError, "No port found matching #{query.inspect}" if matches.empty? matches.each do |p| Gretl._request(:post, "/ports/#{p.port}/start") if !p.active && p.has_command end matches end |
#start_group(group) ⇒ Object
96 97 98 99 100 101 |
# File 'lib/gretl.rb', line 96 def start_group(group) matches = Gretl._fetch_ports.select { |p| p.group&.downcase == group.downcase } raise ArgumentError, "No ports found in group #{group.inspect}" if matches.empty? matches.each { |p| Gretl._request(:post, "/ports/#{p.port}/start") if !p.active && p.has_command } matches end |
#status_group(group) ⇒ Object
110 111 112 113 114 |
# File 'lib/gretl.rb', line 110 def status_group(group) matches = Gretl._fetch_ports.select { |p| p.group&.downcase == group.downcase } raise ArgumentError, "No ports found in group #{group.inspect}" if matches.empty? matches end |
#stop(query) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/gretl.rb', line 87 def stop(query) matches = Gretl._resolve(query) raise ArgumentError, "No port found matching #{query.inspect}" if matches.empty? matches.each do |p| Gretl._request(:post, "/ports/#{p.port}/stop") if p.active end matches end |
#stop_group(group) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/gretl.rb', line 103 def stop_group(group) matches = Gretl._fetch_ports.select { |p| p.group&.downcase == group.downcase } raise ArgumentError, "No ports found in group #{group.inspect}" if matches.empty? matches.each { |p| Gretl._request(:post, "/ports/#{p.port}/stop") if p.active } matches end |
#wait_for_active(query, timeout: 30, interval: 0.5) ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/gretl.rb', line 123 def wait_for_active(query, timeout: 30, interval: 0.5) deadline = Time.now + timeout while Time.now < deadline Gretl._resolve(query).each { |p| return p if p.active } sleep interval end raise "Port matching #{query.inspect} did not become active within #{timeout}s" end |
#wait_for_inactive(query, timeout: 30, interval: 0.5) ⇒ Object
148 149 150 151 152 153 154 155 |
# File 'lib/gretl.rb', line 148 def wait_for_inactive(query, timeout: 30, interval: 0.5) deadline = Time.now + timeout while Time.now < deadline Gretl._resolve(query).each { |p| return p if p.stopped? } sleep interval end raise "Port matching #{query.inspect} did not become inactive within #{timeout}s" end |