Created
April 16, 2020 17:44
-
-
Save krainboltgreene/965b5659d4e95ac52ae5fb43221f58c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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