Class: Console

Inherits:
Object
  • Object
show all
Defined in:
lib/opsconsole/console.rb

Overview

Tmux integration

Class Method Summary collapse

Class Method Details

.attach(session:, window: nil, pane: nil) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/opsconsole/console.rb', line 69

def self.attach(session:, window: nil, pane: nil)
  select_pane(session: session,
              window: window,
              pane: pane)
  attach_session(session: session,
                 window: window,
                 pane: pane)
end

.attach_session(session:, window: nil, pane: nil) ⇒ Object



4
5
6
# File 'lib/opsconsole/console.rb', line 4

def self.attach_session(session:, window: nil, pane: nil)
  sh(%W[attach -t =#{session}:#{window}.#{pane}])
end

.create(session:, window:, user:, hosts:, commands:, synchronize:) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/opsconsole/console.rb', line 93

def self.create(session:, window:, user:, hosts:, commands:, synchronize:)
  return if present?(target: "=#{session}:#{window}.1")

  if present?(target: "=#{session}:")
    create_window(session: session,
                  window: window)
  else
    create_session(session: session,
                   window: window)
  end

  create_panes(session: session,
               window: window,
               user: user,
               hosts: hosts,
               commands: commands)

  set_window_option(session: session,
                    window: window,
                    option: 'synchronize-panes',
                    value: synchronize ? 'on' : 'off')
end

.create_panes(session:, window:, user:, hosts:, commands:) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/opsconsole/console.rb', line 116

def self.create_panes(session:, window:, user:, hosts:, commands:)
  hosts.size.downto(2) do |h|
    width = 100 / h
    split_window(session: session,
                 window: window,
                 pane: 1,
                 direction: 'h',
                 size: width)
  end

  hosts.size.downto(1) do |h|
    host = hosts[h - 1]
    ssh = "ssh -tt -o ConnectTimeout=2 #{user}@#{host}"
    commands = [ssh] if commands.empty?
    commands.size.downto(1) do |c|
      command = "#{ssh} '#{commands[c - 1]}'"
      split_window(session: session,
                   window: window,
                   pane: h,
                   direction: 'v',
                   size: 100 / c,
                   command: command)
    end
    kill_pane(session: session,
              window: window,
              pane: h)
  end

  select_pane(session: session,
              window: window,
              pane: 1)
end

.create_session(session:, window:) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/opsconsole/console.rb', line 149

def self.create_session(session:, window:)
  new_session(session: session,
              window: window)
  set_option(session: session,
             option: 'base-index',
             value: 1)
  set_option(session: session,
             option: 'pane-border-status',
             value: 'top')
  set_option(session: session,
             option: 'pane-border-format',
             value: '#{pane_index}')
  set_option(session: session,
             option: 'status-left-length',
             value: session.size + 4)
  set_option(session: session,
             option: 'status-right',
             value: ['#{session_name}',
                     '#{window_name}',
                     '#{pane_index}'].join(':'))
  set_window_option(session: session,
                    window: window,
                    option: 'pane-base-index',
                    value: 1)
end

.create_window(session:, window:) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/opsconsole/console.rb', line 175

def self.create_window(session:, window:)
  new_window(session: session,
             window: window)
  set_window_option(session: session,
                    window: window,
                    option: 'pane-base-index',
                    value: 1)
end

.destroy(session:, window:) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/opsconsole/console.rb', line 78

def self.destroy(session:, window:)
  if session.nil?
    STDERR.puts 'can not find session'
  elsif window.nil?
    kill_session(session: session)
  else
    kill_window(session: session,
                window: window)
  end
end

.kill_pane(session:, window:, pane:) ⇒ Object



16
17
18
# File 'lib/opsconsole/console.rb', line 16

def self.kill_pane(session:, window:, pane:)
  sh(%W[killp -t =#{session}:#{window}.#{pane}])
end

.kill_session(session:) ⇒ Object



12
13
14
# File 'lib/opsconsole/console.rb', line 12

def self.kill_session(session:)
  sh(%W[kill-session -t =#{session}:])
end

.kill_window(session:, window:) ⇒ Object



20
21
22
# File 'lib/opsconsole/console.rb', line 20

def self.kill_window(session:, window:)
  sh(%W[killw -t =#{session}:#{window}.])
end

.listObject



89
90
91
# File 'lib/opsconsole/console.rb', line 89

def self.list
  list_panes
end

.list_panesObject



24
25
26
27
28
29
30
31
# File 'lib/opsconsole/console.rb', line 24

def self.list_panes
  fmt = ['#{session_name}',
         '#{window_name}',
         '#{window_active}',
         '#{pane_index}',
         '#{pane_active}'].join(' ')
  sh(%W[lsp -a -F #{fmt}])
end

.new_session(session:, window:) ⇒ Object



33
34
35
# File 'lib/opsconsole/console.rb', line 33

def self.new_session(session:, window:)
  sh(%W[new -d -s #{session} -n #{window}])
end

.new_window(session:, window:) ⇒ Object



37
38
39
# File 'lib/opsconsole/console.rb', line 37

def self.new_window(session:, window:)
  sh(%W[neww -t #{session} -n #{window}])
end

.present?(target:) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/opsconsole/console.rb', line 41

def self.present?(target:)
  sh(%W[has -t #{target}].push(err: File::NULL))
end

.select_pane(session:, window:, pane:) ⇒ Object



61
62
63
# File 'lib/opsconsole/console.rb', line 61

def self.select_pane(session:, window:, pane:)
  sh(%W[selectp -t =#{session}:#{window}.#{pane}])
end

.select_window(session:, window:) ⇒ Object



65
66
67
# File 'lib/opsconsole/console.rb', line 65

def self.select_window(session:, window:)
  sh(%W[selectw -t =#{session}:#{window}.])
end

.set_option(session:, option:, value:) ⇒ Object



45
46
47
# File 'lib/opsconsole/console.rb', line 45

def self.set_option(session:, option:, value:)
  sh(%W[set -t =#{session}: #{option} #{value}])
end

.set_window_option(session:, window:, option:, value:) ⇒ Object



49
50
51
# File 'lib/opsconsole/console.rb', line 49

def self.set_window_option(session:, window:, option:, value:)
  sh(%W[setw -t =#{session}:#{window}. #{option} #{value}])
end

.sh(args) ⇒ Object



8
9
10
# File 'lib/opsconsole/console.rb', line 8

def self.sh(args)
  system('tmux', '-L', 'opsconsole', *args)
end

.split_window(session:, window:, pane:, direction:, size:, command: nil) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/opsconsole/console.rb', line 53

def self.split_window(session:, window:, pane:, direction:, size:, command: nil)
  if command.nil?
    sh(%W[splitw -t =#{session}:#{window}.#{pane} -#{direction} -p #{size}])
  else
    sh(%W[splitw -t =#{session}:#{window}.#{pane} -#{direction} -p #{size} #{command}])
  end
end