Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krainboltgreene/965b5659d4e95ac52ae5fb43221f58c4 to your computer and use it in GitHub Desktop.
Save krainboltgreene/965b5659d4e95ac52ae5fb43221f58c4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "json"
host = ARGV[0]
service = ARGV[1] || ARGV[0]
nodes = JSON.parse(`bin/terraform-nodes | jq '[.[] | [.hostname, .ip]]'`)
groups = nodes.
# [['a-b-c-d-e', '1.1.1.1']]
flat_map {|node| node.first.gsub(HOST_PREFIX, "").split("-").reduce([]) {|list, key| [*list, [[list.last&.first, key].compact.join("_"), node.last]]}}.
# [["a", "1.1.1.1"], ["a-b", "1.1.1.1"], ["a-b-c", "1.1.1.1"], ["a-b-c-d", "1.1.1.1"], ["a-b-c-d-e", "1.1.1.1"]]
group_by {|node| node.first.gsub(/\_\d+/, "")}.
# {"a"=>[["a", "1.1.1.1"]], "a-b"=>[["a-b", "1.1.1.1"]], "a-b-c"=>[["a-b-c", "1.1.1.1"]], "a-b-c-d"=>[["a-b-c-d", "1.1.1.1"]], "a-b-c-d-e"=>[["a-b-c-d-e", "1.1.1.1"]]}
transform_values {|nodes| nodes.map(&:last).uniq}
# {"a"=>["1.1.1.1"], "a-b"=>["1.1.1.1"], "a-b-c"=>["1.1.1.1"], "a-b-c-d"=>["1.1.1.1"], "a-b-c-d-e"=>["1.1.1.1"]}
groups[host].map { |node| Thread.new { IO.popen("ssh #{node} 'sudo journalctl -xfe -u #{service}'") { |io| puts(io.readline) } } }.each(&:join)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment