Last active
December 11, 2015 04:28
-
-
Save hryk/4544988 to your computer and use it in GitHub Desktop.
~/.csshclustersを読んでtmuxのwindowを開く。
This file contains 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' | |
def expand_clusters clusters | |
expanded = false | |
clusters.each do |label, hosts| | |
tmp_hosts = [] | |
hosts.each do |host| | |
if clusters.key?(host) | |
tmp_hosts.concat(clusters[host]) | |
expanded = true | |
else | |
tmp_hosts << host | |
end | |
end | |
clusters[label] = tmp_hosts | |
end | |
if expanded | |
expand_clusters clusters | |
else | |
return clusters | |
end | |
end | |
def parse_config | |
labels, hosts = [], [] | |
clusters = Hash.new | |
open("#{ENV['HOME']}/.csshclusters", 'r') do |f| | |
f.each do |line| | |
next if line.start_with?("\n", '#') | |
line_ary = line.split(/\s/) | |
label = line_ary.shift | |
labels << label | |
hosts.concat line_ary | |
clusters[label] = line_ary | |
end | |
end | |
expand_clusters clusters | |
end | |
def open_ssh hosts | |
`tmux new-window` | |
hosts.each_with_index do |hostname, index| | |
`tmux split-window -d "ssh #{hostname}"` | |
`tmux select-layout tiled` | |
end | |
`tmux set-window-option synchronize-panes on` | |
end | |
def main | |
clusters = parse_config | |
targets = [] | |
ARGV.each do |label| | |
targets.concat(clusters[label]) | |
end | |
open_ssh(targets) if targets.size > 0 | |
end | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment