Module: MuxTf::Cli::Mux

Extended by:
PiotrbCliUtils::ShellHelpers, PiotrbCliUtils::Util
Defined in:
lib/mux_tf/cli/mux.rb

Class Method Summary collapse

Class Method Details

.parse_control_line(line) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/mux_tf/cli/mux.rb', line 74

def parse_control_line(line)
  keyword, remainder = line.split(" ", 2)
  case keyword
  when "%begin"
    # n1, n2, n3, remainder = remainder.split(" ", 4)
    p [:begin, n1, n2, n3, remainder]
  when "%end"
    # n1, n2, n3, remainder = remainder.split(" ", 4)
    p [:end, n1, n2, n3, remainder]
  when "%session-changed"
    # n1, s1, remainder = remainder.split(" ", 3)
    # p [:session_changed, n1, s1, remainder]
  when "%window-pane-changed"
    # n1, n2, remainder = remainder.split(" ", 3)
    # p [:window_pane_changed, n1, n2, remainder]
  when "%layout-change"
    # ignore
    # p [:layout_change, remainder]
  when "%pane-mode-changed"
    # ignore
    p [:layout_change, remainder]
  when "%subscription-changed"
    sub_name, n1, n2, n3, n4, _, remainder = remainder.split(" ", 7)
    if sub_name == "pane-info"
      pane_id, pane_index, pane_title, pane_dead_status = remainder.strip.split(",", 4)
      if pane_dead_status != ""
        p [:pane_exited, pane_id, pane_index, pane_title, pane_dead_status]
        Tmux.kill_pane(pane_id)
        panes = Tmux.list_panes
        if panes.length == 1 && panes.first[:name] == "spawner"
          Tmux.kill_pane(panes.first[:id])
          # its the last pane, so the whole thing should exit
        end
      end
    else
      p [:subscription_changed, sub_name, n1, n2, n3, n4, remainder]
    end
  when "%output"
    pane, = remainder.split(" ", 2)
    if pane == "%1"
      # skip own output
      # else
      #   p [:output, pane, remainder]
    end
  else
    p [keyword, remainder]
  end
end

.run(args) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/mux_tf/cli/mux.rb', line 27

def run(args)
  if ENV["MUX_V2"]
    run_v2(args)
  else
    run_v1(args)
  end
end

.run_create_sessionObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mux_tf/cli/mux.rb', line 35

def run_create_session
  project = File.basename(Dir.getwd)

  if Tmux.session_running?(project)
    log "Killing existing session ..."
    Tmux.kill_session(project)
  end

  log "Starting new session ..."
  with_clean_env do
    Tmux.new_session project
  end
  Tmux.select_pane "initial"

  window_id = Tmux.list_windows.first[:id]

  Tmux.set "remain-on-exit", "on"

  Tmux.set_hook "pane-exited", "select-layout tiled"
  Tmux.set_hook "window-pane-changed", "select-layout tiled"
  Tmux.set_hook "pane-exited", "select-layout tiled"

  Tmux.set "mouse", "on"

  puts "\e]0;tmux: #{project}\007"

  Tmux.split_window :horizontal, "#{project}:#{window_id}", cwd: Dir.getwd,
                                                            cmd: File.expand_path(File.join(__dir__, "..", "..", "..", "exe", "tf_mux spawner"))
  Tmux.select_pane "spawner"

  initial_pane = Tmux.find_pane("initial")
  Tmux.kill_pane initial_pane[:id]
  Tmux.tile!

  log "Attaching ..."
  Tmux.attach(project)
  log "Done!"
end

.run_spawnerObject



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
148
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
174
175
176
177
178
179
# File 'lib/mux_tf/cli/mux.rb', line 123

def run_spawner
  project = File.basename(Dir.getwd)

  control_thread = Thread.new do
    puts "Control Thread Started"
    Tmux.attach_control(project, on_spawn: lambda { |stdin|
      stdin.write("refresh-client -B \"pane-info:%*:\#{pane_id},\#{pane_index},\#{pane_title},\#{pane_dead_status}\"\n")
      stdin.flush
    }, on_line: lambda { |stream, line|
      if stream == :stdout
        parse_control_line(line)
        # p info
      else
        p [stream, line]
      end
    })
    puts "Control Thread Exited"
  end

  begin
    log "Enumerating folders ..."
    dirs = enumerate_terraform_dirs

    fail_with "Error: - no subfolders detected! Aborting." if dirs.empty?

    tasks = dirs.map { |dir|
      {
        name: dir,
        cwd: dir,
        cmd: File.expand_path(File.join(__dir__, "..", "..", "..", "exe", "tf_current"))
      }
    }

    if ENV["MUX_TF_AUTH_WRAPPER"]
      log "Warming up AWS connection ..."
      words = Shellwords.shellsplit(ENV["MUX_TF_AUTH_WRAPPER"])
      result = capture_shell([*words, "aws", "sts", "get-caller-identity"], raise_on_error: true)
      p JSON.parse(result)
    end

    window_id = Tmux.list_windows.first[:id]

    return if tasks.empty?

    tasks.each do |task|
      log "launching task: #{task[:name]} ...", depth: 2
      Tmux.split_window :horizontal, "#{project}:#{window_id}", cmd: task[:cmd], cwd: task[:cwd]
      Tmux.select_pane task[:name]
      Tmux.tile!
      task[:commands]&.each do |cmd|
        Tmux.send_keys cmd, enter: true
      end
    end
  ensure
    control_thread.join
  end
end

.run_v1(_args) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/mux_tf/cli/mux.rb', line 191

def run_v1(_args)
  Dotenv.load(".env.mux")

  dirs = enumerate_terraform_dirs

  tasks = dirs.map { |dir|
    {
      name: dir,
      cwd: dir,
      cmd: File.expand_path(File.join(__dir__, "..", "..", "..", "exe", "tf_current"))
    }
  }

  project = File.basename(Dir.getwd)

  if ENV["MUX_TF_AUTH_WRAPPER"]
    log "Warming up AWS connection ..."
    words = Shellwords.shellsplit(ENV["MUX_TF_AUTH_WRAPPER"])
    result = capture_shell([*words, "aws", "sts", "get-caller-identity"], raise_on_error: true)
    p JSON.parse(result)
  end

  if Tmux.session_running?(project)
    log "Killing existing session ..."
    Tmux.kill_session(project)
  end

  log "Starting new session ..."
  with_clean_env do
    Tmux.new_session project
  end
  Tmux.select_pane "initial"

  # Tmux.set "remain-on-exit", "on"

  Tmux.set_hook "pane-exited", "select-layout tiled"
  Tmux.set_hook "window-pane-changed", "select-layout tiled"

  Tmux.set "mouse", "on"

  window_id = Tmux.list_windows.first[:id]

  unless tasks.empty?
    tasks.each do |task|
      log "launching task: #{task[:name]} ...", depth: 2
      Tmux.split_window :horizontal, "#{project}:#{window_id}", cmd: task[:cmd], cwd: task[:cwd]
      Tmux.select_pane task[:name]
      Tmux.tile!
      task[:commands]&.each do |cmd|
        Tmux.send_keys cmd, enter: true
      end
    end
  end

  log "Almost done ..."

  initial_pane = Tmux.find_pane("initial")
  Tmux.kill_pane initial_pane[:id]
  Tmux.tile!

  puts "\e]0;tmux: #{project}\007"

  sleep 1

  log "Attaching ..."
  Tmux.attach(project, cc: !!ENV["MUXP_CC_MODE"])
  log "Done!"
end

.run_v2(args) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/mux_tf/cli/mux.rb', line 181

def run_v2(args)
  Dotenv.load(".env.mux")

  if args[0] == "spawner"
    run_spawner
  else
    run_create_session
  end
end

.with_clean_envObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mux_tf/cli/mux.rb', line 12

def with_clean_env
  backup = {}
  Bundler.with_original_env do
    ENV.keys.grep(/^(RBENV_|RUBYLIB)/).each do |key|
      backup[key] = ENV.fetch(key)
      ENV.delete(key)
    end
    yield
  end
ensure
  backup.each do |k, v|
    ENV[k] = v
  end
end