Module: Bolt::Util

Defined in:
lib/bolt/util.rb,
lib/bolt/util/puppet_log_level.rb

Defined Under Namespace

Modules: PuppetLogLevel

Class Method Summary collapse

Class Method Details

.class_name_to_file_name(cls_name) ⇒ Object



207
208
209
210
211
# File 'lib/bolt/util.rb', line 207

def class_name_to_file_name(cls_name)
  # Note this turns Bolt::CLI -> 'bolt/cli' not 'bolt/c_l_i'
  # this won't handle Bolt::Inventory2Foo
  cls_name.gsub(/([a-z])([A-Z])/, '\1_\2').gsub('::', '/').downcase
end

.deep_clone(obj, cloned = {}) ⇒ Object

Performs a deep_clone, using an identical copy if the cloned structure contains multiple references to the same object and prevents endless recursion. Credit to Jan Molic via github.com/rubyworks/facets/blob/master/LICENSE.txt



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/bolt/util.rb', line 171

def deep_clone(obj, cloned = {})
  cloned[obj.object_id] if cloned.include?(obj.object_id)

  begin
    cl = obj.clone
  rescue TypeError
    # unclonable (TrueClass, Fixnum, ...)
    cloned[obj.object_id] = obj
    return obj
  else
    cloned[obj.object_id] = cl
    cloned[cl.object_id] = cl

    if cl.is_a? Hash
      obj.each { |k, v| cl[k] = deep_clone(v, cloned) }
    elsif cl.is_a? Array
      cl.collect! { |v| deep_clone(v, cloned) }
    elsif cl.is_a? Struct
      obj.each_pair { |k, v| cl[k] = deep_clone(v, cloned) }
    end

    cl.instance_variables.each do |var|
      v = cl.instance_variable_get(var)
      v_cl = deep_clone(v, cloned)
      cl.instance_variable_set(var, v_cl)
    end

    return cl
  end
end

.deep_merge(hash1, hash2) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/bolt/util.rb', line 104

def deep_merge(hash1, hash2)
  recursive_merge = proc do |_key, h1, h2|
    if h1.is_a?(Hash) && h2.is_a?(Hash)
      h1.merge(h2, &recursive_merge)
    else
      h2
    end
  end
  hash1.merge(hash2, &recursive_merge)
end

.file_stat(path) ⇒ Object

This is stubbed for testing validate_file



203
204
205
# File 'lib/bolt/util.rb', line 203

def file_stat(path)
  File.stat(File.expand_path(path))
end

.map_vals(hash) ⇒ Object



115
116
117
118
119
# File 'lib/bolt/util.rb', line 115

def map_vals(hash)
  hash.each_with_object({}) do |(k, v), acc|
    acc[k] = yield(v)
  end
end

.module_name(path) ⇒ Object

Accepts a path with either ‘plans’ or ‘tasks’ in it and determines the name of the module



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
# File 'lib/bolt/util.rb', line 42

def module_name(path)
  # Remove extra dots and slashes
  path = Pathname.new(path).cleanpath.to_s
  fs = File::SEPARATOR
  regex = Regexp.new("#{fs}plans#{fs}|#{fs}tasks#{fs}")

  # Only accept paths with '/plans/' or '/tasks/'
  unless path.match?(regex)
    msg = "Could not determine module from #{path}. "\
      "The path must include 'plans' or 'tasks' directory"
    raise Bolt::Error.new(msg, 'bolt/modulepath-error')
  end

  # Split the path on the first instance of /plans/ or /tasks/
  parts = path.split(regex, 2)
  # Module name is the last entry before 'plans' or 'tasks'
  modulename = parts[0].split(fs)[-1]
  filename = File.basename(path).split('.')[0]
  # Remove "/init.*" if filename is init or just remove the file
  # extension
  if filename == 'init'
    parts[1].chomp!(File.basename(path))
  else
    parts[1].chomp!(File.extname(path))
  end

  # The plan or task name is the rest of the path
  [modulename, parts[1].split(fs)].flatten.join('::')
end

.postwalk_vals(data, skip_top = false, &block) ⇒ Object

Accepts a Data object and returns a copy with all hash and array values modified by the given block. Descendants are modified before their parents.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/bolt/util.rb', line 153

def postwalk_vals(data, skip_top = false, &block)
  new_data = if data.is_a? Hash
               map_vals(data) { |v| postwalk_vals(v, &block) }
             elsif data.is_a? Array
               data.map { |v| postwalk_vals(v, &block) }
             else
               data
             end
  if skip_top
    new_data
  else
    yield(new_data)
  end
end

.read_config_file(path, default_paths = nil, file_name = 'file') ⇒ Object



6
7
8
9
10
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
# File 'lib/bolt/util.rb', line 6

def read_config_file(path, default_paths = nil, file_name = 'file')
  require 'yaml'

  logger = Logging.logger[self]
  path_passed = path
  if path.nil? && default_paths
    found_default = default_paths.select { |p| File.exist?(p) }
    if found_default.size > 1
      logger.warn "Found #{file_name}s at #{found_default.join(', ')}, using the first"
    end
    # Use first found, fall back to first default and try to load even if it didn't exist
    path = found_default.first || default_paths.first
  end

  path = File.expand_path(path)
  content = File.open(path, "r:UTF-8") { |f| YAML.safe_load(f.read) }
  logger.debug("Loaded #{file_name} from #{path}")
  content
rescue Errno::ENOENT
  msg = "Could not read #{file_name} file: #{path}"
  if path_passed
    raise Bolt::FileError.new(msg, path)
  else
    logger.debug(msg)
    nil
  end
rescue Psych::Exception => e
  raise Bolt::FileError.new("Could not parse #{file_name} file: #{path}\n"\
                            "Error at line #{e.line} column #{e.column}", path)
rescue IOError, SystemCallError => e
  raise Bolt::FileError.new("Could not read #{file_name} file: #{path}\n"\
                            "error: #{e}", path)
end

.symbolize_top_level_keys(hsh) ⇒ Object

Accept hash and return hash with top level keys of type “String” converted to symbols.



237
238
239
# File 'lib/bolt/util.rb', line 237

def symbolize_top_level_keys(hsh)
  hsh.each_with_object({}) { |(k, v), h| k.is_a?(String) ? h[k.to_sym] = v : h[k] = v }
end

.to_code(string) ⇒ Object



72
73
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
# File 'lib/bolt/util.rb', line 72

def to_code(string)
  case string
  when Bolt::PAL::YamlPlan::DoubleQuotedString
    string.value.inspect
  when Bolt::PAL::YamlPlan::BareString
    if string.value.start_with?('$')
      string.value.to_s
    else
      "'#{string.value}'"
    end
  when Bolt::PAL::YamlPlan::EvaluableString, Bolt::PAL::YamlPlan::CodeLiteral
    string.value.to_s
  when String
    "'#{string}'"
  when Hash
    formatted = String.new("{")
    string.each do |k, v|
      formatted << "#{to_code(k)} => #{to_code(v)}, "
    end
    formatted.chomp!(", ")
    formatted << "}"
    formatted
  when Array
    formatted = String.new("[")
    formatted << string.map { |str| to_code(str) }.join(', ')
    formatted << "]"
    formatted
  else
    string
  end
end

.validate_file(type, path, allow_dir = false) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/bolt/util.rb', line 213

def validate_file(type, path, allow_dir = false)
  stat = file_stat(path)

  if !stat.readable?
    raise Bolt::FileError.new("The #{type} '#{path}' is unreadable", path)
  elsif !stat.file? && (!allow_dir || !stat.directory?)
    expected = allow_dir ? 'file or directory' : 'file'
    raise Bolt::FileError.new("The #{type} '#{path}' is not a #{expected}", path)
  elsif stat.directory?
    Dir.foreach(path) do |file|
      next if %w[. ..].include?(file)
      validate_file(type, File.join(path, file), allow_dir)
    end
  end
rescue Errno::ENOENT
  raise Bolt::FileError.new("The #{type} '#{path}' does not exist", path)
end

.walk_keys(data, &block) ⇒ Object

Accepts a Data object and returns a copy with all hash keys modified by block. use &:to_s to stringify keys or &:to_sym to symbolize them



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/bolt/util.rb', line 123

def walk_keys(data, &block)
  if data.is_a? Hash
    data.each_with_object({}) do |(k, v), acc|
      v = walk_keys(v, &block)
      acc[yield(k)] = v
    end
  elsif data.is_a? Array
    data.map { |v| walk_keys(v, &block) }
  else
    data
  end
end

.walk_vals(data, skip_top = false, &block) ⇒ Object

Accepts a Data object and returns a copy with all hash and array values Arrays and hashes including the initial object are modified before their descendants are.



139
140
141
142
143
144
145
146
147
148
# File 'lib/bolt/util.rb', line 139

def walk_vals(data, skip_top = false, &block)
  data = yield(data) unless skip_top
  if data.is_a? Hash
    map_vals(data) { |v| walk_vals(v, &block) }
  elsif data.is_a? Array
    data.map { |v| walk_vals(v, &block) }
  else
    data
  end
end

.windows?Boolean

Returns true if windows false if not.

Returns:

  • (Boolean)


232
233
234
# File 'lib/bolt/util.rb', line 232

def windows?
  !!File::ALT_SEPARATOR
end