Module: Runbook::Run::ClassMethods
Constant Summary
Helpers::TmuxHelper::FILE_PERMISSIONS
Instance Method Summary
collapse
-
#_handle_continue_result(result, object, metadata) ⇒ Object
-
#_method_name(object) ⇒ Object
-
#_step_choices(object, metadata) ⇒ Object
-
#execute(object, metadata) ⇒ Object
-
#past_position?(current_position, position) ⇒ Boolean
-
#runbook__entities__book(object, metadata) ⇒ Object
-
#runbook__entities__section(object, metadata) ⇒ Object
-
#runbook__entities__setup(object, metadata) ⇒ Object
-
#runbook__entities__step(object, metadata) ⇒ Object
-
#runbook__statements__ask(object, metadata) ⇒ Object
-
#runbook__statements__confirm(object, metadata) ⇒ Object
-
#runbook__statements__description(object, metadata) ⇒ Object
-
#runbook__statements__layout(object, metadata) ⇒ Object
-
#runbook__statements__note(object, metadata) ⇒ Object
-
#runbook__statements__notice(object, metadata) ⇒ Object
-
#runbook__statements__ruby_command(object, metadata) ⇒ Object
-
#runbook__statements__tmux_command(object, metadata) ⇒ Object
-
#runbook__statements__wait(object, metadata) ⇒ Object
-
#should_skip?(metadata) ⇒ Boolean
-
#start_at_is_substep?(object, metadata) ⇒ Boolean
#_all_panes_exist?, #_initialize_panes, #_kill_pane, #_layout_file, #_new_window, #_pager_escape_sequence, #_remove_stale_layouts, #_rename_window, #_runbook_pane, #_session_layout_files, #_session_panes, #_set_directory, #_setup_layout, #_setup_panes, #_slug, #_split, #_swap_panes, #_swap_runbook_pane, #kill_all_panes, #send_keys, #setup_layout
#deindent
Methods included from Hooks
#_hook_index, #hooks, #hooks_for, #register_hook
Instance Method Details
#_handle_continue_result(result, object, metadata) ⇒ Object
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/runbook/run.rb', line 229
def _handle_continue_result(result, object, metadata)
toolbox = metadata[:toolbox]
case result
when :continue
return
when :skip
position = metadata[:position]
split_position = position.split(".")
new_step = (split_position.pop.to_i + 1).to_s
start_at = (split_position << new_step).join(".")
metadata[:start_at] = start_at
when :jump
result = toolbox.ask("What position would you like to jump to?")
if past_position?(metadata[:position], result)
metadata[:reverse] = true
metadata[:reversed] = true
end
metadata[:start_at] = result
when :no_paranoid
metadata[:paranoid] = false
when :exit
toolbox.exit(0)
end
end
|
#_method_name(object) ⇒ Object
215
216
217
|
# File 'lib/runbook/run.rb', line 215
def _method_name(object)
object.class.to_s.underscore.gsub("/", "__")
end
|
#_step_choices(object, metadata) ⇒ Object
219
220
221
222
223
224
225
226
227
|
# File 'lib/runbook/run.rb', line 219
def _step_choices(object, metadata)
[
{key: "c", name: "Continue to execute this step", value: :continue},
{key: "s", name: "Skip this step", value: :skip},
{key: "j", name: "Jump to the specified position", value: :jump},
{key: "P", name: "Disable paranoid mode", value: :no_paranoid},
{key: "e", name: "Exit the runbook", value: :exit},
]
end
|
#execute(object, metadata) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/runbook/run.rb', line 18
def execute(object, metadata)
return if should_skip?(metadata)
method = _method_name(object)
if respond_to?(method)
send(method, object, metadata)
else
msg = "ERROR! No execution rule for #{object.class} (#{_method_name(object)}) in #{to_s}"
metadata[:toolbox].error(msg)
return
end
end
|
#past_position?(current_position, position) ⇒ Boolean
209
210
211
212
213
|
# File 'lib/runbook/run.rb', line 209
def past_position?(current_position, position)
current_pose = Gem::Version.new(current_position)
pose = Gem::Version.new(position)
return pose <= current_pose
end
|
#runbook__entities__book(object, metadata) ⇒ Object
31
32
33
|
# File 'lib/runbook/run.rb', line 31
def runbook__entities__book(object, metadata)
metadata[:toolbox].output("Executing #{object.title}...\n\n")
end
|
#runbook__entities__section(object, metadata) ⇒ Object
35
36
37
|
# File 'lib/runbook/run.rb', line 35
def runbook__entities__section(object, metadata)
metadata[:toolbox].output("Section #{metadata[:position]}: #{object.title}\n\n")
end
|
#runbook__entities__setup(object, metadata) ⇒ Object
39
40
41
|
# File 'lib/runbook/run.rb', line 39
def runbook__entities__setup(object, metadata)
metadata[:toolbox].output("Setup:\n\n")
end
|
#runbook__entities__step(object, metadata) ⇒ Object
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/runbook/run.rb', line 43
def runbook__entities__step(object, metadata)
toolbox = metadata[:toolbox]
title = " #{object.title}".rstrip
toolbox.output("Step #{metadata[:position]}:#{title}\n\n")
return if metadata[:auto] || metadata[:noop] ||
!metadata[:paranoid] || object.title.nil?
step_choices = _step_choices(object, metadata)
continue_result = toolbox.expand("Continue?", step_choices)
_handle_continue_result(continue_result, object, metadata)
end
|
#runbook__statements__ask(object, metadata) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/runbook/run.rb', line 54
def runbook__statements__ask(object, metadata)
target = object.parent.dsl
existing_val = target.instance_variable_get(:"@#{object.into}")
default = existing_val || object.default
if metadata[:auto]
if default
target.singleton_class.class_eval { attr_accessor object.into }
target.send("#{object.into}=".to_sym, default)
return
end
error_msg = "ERROR! Can't execute ask statement without default in automatic mode!"
metadata[:toolbox].error(error_msg)
raise Runbook::Runner::ExecutionError, error_msg
end
if metadata[:noop]
default_msg = default ? " (default: #{default})" : ""
echo_msg = object.echo ? "" : " (echo: false)"
metadata[:toolbox].output("[NOOP] Ask: #{object.prompt} (store in: #{object.into})#{default_msg}#{echo_msg}")
return
end
result = metadata[:toolbox].ask(object.prompt, default: default, echo: object.echo)
target = object.parent.dsl
target.singleton_class.class_eval { attr_accessor object.into }
target.send("#{object.into}=".to_sym, result)
end
|
#runbook__statements__confirm(object, metadata) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/runbook/run.rb', line 85
def runbook__statements__confirm(object, metadata)
if metadata[:auto]
metadata[:toolbox].output("Skipping confirmation (auto): #{object.prompt}")
else
if metadata[:noop]
metadata[:toolbox].output("[NOOP] Prompt: #{object.prompt}")
return
end
result = metadata[:toolbox].yes?(object.prompt)
metadata[:toolbox].exit(1) unless result
end
end
|
#runbook__statements__description(object, metadata) ⇒ Object
99
100
101
102
|
# File 'lib/runbook/run.rb', line 99
def runbook__statements__description(object, metadata)
metadata[:toolbox].output("Description:")
metadata[:toolbox].output("#{object.msg}\n")
end
|
#runbook__statements__layout(object, metadata) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/runbook/run.rb', line 104
def runbook__statements__layout(object, metadata)
if metadata[:noop]
metadata[:toolbox].output(
"[NOOP] Layout: #{object.structure.inspect}"
)
unless ENV["TMUX"]
msg = "Warning: layout statement called outside a tmux pane."
metadata[:toolbox].warn(msg)
end
return
end
unless ENV["TMUX"]
error_msg = "Error: layout statement called outside a tmux pane. Exiting..."
metadata[:toolbox].error(error_msg)
metadata[:toolbox].exit(1)
end
structure = object.structure
title = object.parent.title
layout_panes = setup_layout(structure, runbook_title: title)
metadata[:layout_panes].merge!(layout_panes)
end
|
#runbook__statements__note(object, metadata) ⇒ Object
128
129
130
|
# File 'lib/runbook/run.rb', line 128
def runbook__statements__note(object, metadata)
metadata[:toolbox].output("Note: #{object.msg}")
end
|
#runbook__statements__notice(object, metadata) ⇒ Object
132
133
134
|
# File 'lib/runbook/run.rb', line 132
def runbook__statements__notice(object, metadata)
metadata[:toolbox].warn("Notice: #{object.msg}")
end
|
#runbook__statements__ruby_command(object, metadata) ⇒ Object
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/runbook/run.rb', line 145
def runbook__statements__ruby_command(object, metadata)
if metadata[:noop]
metadata[:toolbox].output("[NOOP] Run the following Ruby block:\n")
begin
source = deindent(object.block.source)
metadata[:toolbox].output("```ruby\n#{source}\n```\n")
rescue ::MethodSource::SourceNotFoundError => e
metadata[:toolbox].output("Unable to retrieve source code")
end
return
end
next_index = metadata[:index] + 1
parent_items = object.parent.items
remaining_items = parent_items.slice!(next_index..-1)
object.parent.dsl.instance_exec(object, metadata, self, &object.block)
parent_items[next_index..-1].each { |item| item.dynamic! }
parent_items.push(*remaining_items)
end
|
#runbook__statements__tmux_command(object, metadata) ⇒ Object
136
137
138
139
140
141
142
143
|
# File 'lib/runbook/run.rb', line 136
def runbook__statements__tmux_command(object, metadata)
if metadata[:noop]
metadata[:toolbox].output("[NOOP] Run: `#{object.cmd}` in pane #{object.pane}")
return
end
send_keys(object.cmd, metadata[:layout_panes][object.pane])
end
|
#runbook__statements__wait(object, metadata) ⇒ Object
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/runbook/run.rb', line 165
def runbook__statements__wait(object, metadata)
if metadata[:noop]
metadata[:toolbox].output("[NOOP] Sleep #{object.time} seconds")
return
end
time = object.time
message = "Sleeping #{time} seconds [:bar] :current/:total"
pastel = Pastel.new
yellow = pastel.on_yellow(" ")
green = pastel.on_green(" ")
progress_bar = TTY::ProgressBar.new(
message,
total: time,
width: 60,
head: ">",
incomplete: yellow,
complete: green,
)
progress_bar.start
time.times do
sleep(1)
progress_bar.advance(1)
end
end
|
#should_skip?(metadata) ⇒ Boolean
191
192
193
194
195
196
197
198
199
200
201
|
# File 'lib/runbook/run.rb', line 191
def should_skip?(metadata)
if metadata[:reversed] && metadata[:position].empty?
current_pose = "0"
else
current_pose = metadata[:position]
end
return false if current_pose.empty?
position = Gem::Version.new(current_pose)
start_at = Gem::Version.new(metadata[:start_at])
return position < start_at
end
|
#start_at_is_substep?(object, metadata) ⇒ Boolean
203
204
205
206
207
|
# File 'lib/runbook/run.rb', line 203
def start_at_is_substep?(object, metadata)
return false unless object.is_a?(Entity)
return true if metadata[:position].empty?
metadata[:start_at].start_with?(metadata[:position])
end
|