Module: Runbook::Runs::SSHKit::ClassMethods

Includes:
Helpers::SSHKitHelper
Included in:
Runbook::Runs::SSHKit
Defined in:
lib/runbook/runs/ssh_kit.rb

Instance Method Summary collapse

Methods included from Helpers::SSHKitHelper

#_as_block, #_coordinator_options, #_servers, #_with_block, #_with_umask, #_within_block, #find_ssh_config, #render_ssh_config_output, #ssh_kit_command, #ssh_kit_command_options, #with_ssh_config

Instance Method Details

#_handle_capture(object, metadata, &block) ⇒ Object



101
102
103
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/runs/ssh_kit.rb', line 101

def _handle_capture(object, , &block)
  ssh_config = find_ssh_config(object)

  if [:noop]
    ssh_config_output = render_ssh_config_output(ssh_config)
    [:toolbox].output(ssh_config_output) unless ssh_config_output.empty?
    [:toolbox].output("[NOOP] Capture: `#{object.cmd}` into #{object.into}")
    return
  end

  [:toolbox].output("\n") # for formatting

  capture_args = ssh_kit_command(object.cmd, raw: object.raw)
  capture_options = ssh_kit_command_options(ssh_config)
  capture_options[:strip] = object.strip
  capture_options[:verbosity] = Logger::INFO

  capture_msg = "Capturing output of `#{object.cmd}`\n\n"
  [:toolbox].output(capture_msg)

  result = block.call(ssh_config, capture_args, capture_options)

  target = object.parent.dsl
  target.singleton_class.class_eval { attr_accessor object.into }
  target.send("#{object.into}=".to_sym, result)
end

#runbook__entities__step(object, metadata) ⇒ Object



12
13
14
15
16
# File 'lib/runbook/runs/ssh_kit.rb', line 12

def runbook__entities__step(object, )
  airbrussh_context = Runbook.configuration._airbrussh_context
  airbrussh_context.set_current_task_name(object.title)
  super
end

#runbook__statements__assert(object, metadata) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# File 'lib/runbook/runs/ssh_kit.rb', line 18

def runbook__statements__assert(object, )
  cmd_ssh_config = find_ssh_config(object, :cmd_ssh_config)

  if [:noop]
    ssh_config_output = render_ssh_config_output(cmd_ssh_config)
    [:toolbox].output(ssh_config_output) unless ssh_config_output.empty?
    interval_msg = "(running every #{object.interval} second(s))"
    [:toolbox].output("[NOOP] Assert: `#{object.cmd}` returns 0 #{interval_msg}")
    if object.timeout > 0 || object.attempts > 0
      timeout_msg = object.timeout > 0 ? "#{object.timeout} second(s)" : nil
      attempts_msg = object.attempts > 0 ? "#{object.attempts} attempts" : nil
      abort_msg = "after #{[timeout_msg, attempts_msg].compact.join(" or ")}, abort..."
      [:toolbox].output(abort_msg)
      if object.abort_statement
        object.abort_statement.parent = object.parent
        object.abort_statement.run(self, .dup)
      end
      [:toolbox].output("and exit")
    end
    return
  end

  should_abort = false
  test_args = ssh_kit_command(object.cmd, raw: object.cmd_raw)
  test_options = ssh_kit_command_options(cmd_ssh_config)

  with_ssh_config(cmd_ssh_config) do
    time = Time.now
    count = object.attempts
    while !(test(*test_args, test_options))
      if ((count -= 1) == 0)
        should_abort = true
        break
      end

      if (object.timeout > 0 && Time.now - time > object.timeout)
        should_abort = true
        break
      end

      sleep(object.interval)
    end
  end

  if should_abort
    error_msg = "Error! Assertion `#{object.cmd}` failed"
    [:toolbox].error(error_msg)
    if object.abort_statement
      object.abort_statement.parent = object.parent
      object.abort_statement.run(self, .dup)
    end
    raise Runbook::Runner::ExecutionError, error_msg
  end
end

#runbook__statements__capture(object, metadata) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/runbook/runs/ssh_kit.rb', line 73

def runbook__statements__capture(object, )
  _handle_capture(object, ) do |ssh_config, capture_args, capture_options|
    if (ssh_config[:servers].size > 1)
      warn_msg = "Warning: `capture` does not support multiple servers. Use `capture_all` instead.\n"
      [:toolbox].warn(warn_msg)
    end

    result = ""
    with_ssh_config(ssh_config) do
      result = capture(*capture_args, capture_options)
    end
    result
  end
end

#runbook__statements__capture_all(object, metadata) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/runbook/runs/ssh_kit.rb', line 88

def runbook__statements__capture_all(object, )
  _handle_capture(object, ) do |ssh_config, capture_args, capture_options|
    result = {}
    mutex = Mutex.new
    with_ssh_config(ssh_config) do
      hostname = self.host.hostname
      capture_result = capture(*capture_args, capture_options)
      mutex.synchronize { result[hostname] = capture_result }
    end
    result
  end
end

#runbook__statements__command(object, metadata) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/runbook/runs/ssh_kit.rb', line 128

def runbook__statements__command(object, )
  ssh_config = find_ssh_config(object)

  if [:noop]
    ssh_config_output = render_ssh_config_output(ssh_config)
    [:toolbox].output(ssh_config_output) unless ssh_config_output.empty?
    [:toolbox].output("[NOOP] Run: `#{object.cmd}`")
    return
  end

  [:toolbox].output("\n") # for formatting

  execute_args = ssh_kit_command(object.cmd, raw: object.raw)
  exec_options = ssh_kit_command_options(ssh_config)

  with_ssh_config(ssh_config) do
    execute(*execute_args, exec_options)
  end
end

#runbook__statements__download(object, metadata) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/runbook/runs/ssh_kit.rb', line 148

def runbook__statements__download(object, )
  ssh_config = find_ssh_config(object)

  if [:noop]
    ssh_config_output = render_ssh_config_output(ssh_config)
    [:toolbox].output(ssh_config_output) unless ssh_config_output.empty?
    options = object.options
    to = " to #{object.to}" if object.to
    opts = " with options #{options}" unless options == {}
    noop_msg = "[NOOP] Download: #{object.from}#{to}#{opts}"
    [:toolbox].output(noop_msg)
    return
  end

  [:toolbox].output("\n") # for formatting

  with_ssh_config(ssh_config) do
    download!(object.from, object.to, object.options)
  end
end

#runbook__statements__upload(object, metadata) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/runbook/runs/ssh_kit.rb', line 169

def runbook__statements__upload(object, )
  ssh_config = find_ssh_config(object)

  if [:noop]
    ssh_config_output = render_ssh_config_output(ssh_config)
    [:toolbox].output(ssh_config_output) unless ssh_config_output.empty?
    options = object.options
    to = " to #{object.to}" if object.to
    opts = " with options #{options}" unless options == {}
    noop_msg = "[NOOP] Upload: #{object.from}#{to}#{opts}"
    [:toolbox].output(noop_msg)
    return
  end

  [:toolbox].output("\n") # for formatting

  with_ssh_config(ssh_config) do
    upload!(object.from, object.to, object.options)
  end
end