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

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

Instance Method Summary collapse

Instance Method Details

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



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
# File 'lib/runbook/runs/ssh_kit.rb', line 94

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__statements__assert(object, metadata) ⇒ Object



11
12
13
14
15
16
17
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
# File 'lib/runbook/runs/ssh_kit.rb', line 11

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
      giveup_msg = "after #{[timeout_msg, attempts_msg].compact.join(" or ")}, give up..."
      [:toolbox].output(giveup_msg)
      if object.timeout_statement
        object.timeout_statement.parent = object.parent
        object.timeout_statement.run(self, .dup)
      end
      [:toolbox].output("and exit")
    end
    return
  end

  gave_up = 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)
        gave_up = true
        break
      end

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

      sleep(object.interval)
    end
  end

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

#runbook__statements__capture(object, metadata) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/runbook/runs/ssh_kit.rb', line 66

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



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/runbook/runs/ssh_kit.rb', line 81

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/runbook/runs/ssh_kit.rb', line 121

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/runbook/runs/ssh_kit.rb', line 141

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



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/runbook/runs/ssh_kit.rb', line 162

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