Module: Omnibus::Util

Included in:
BuildVersion, Builder, Config, Fetcher, Fetcher, GitCache, GitRepository, HealthCheck, Packager::Base, Project
Defined in:
lib/omnibus/util.rb

Constant Summary collapse

SHELLOUT_OPTIONS =

The default shellout options.

{
  log_level: :internal,
  timeout: 7200, # 2 hours
  environment: {},
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


21
22
23
24
# File 'lib/omnibus/util.rb', line 21

def self.included(base)
  # This module requires logging is also available
  base.send(:include, Logging)
end

Instance Method Details

#copy_file(source, destination) ⇒ String

Copy the source file to the destination.


165
166
167
168
169
# File 'lib/omnibus/util.rb', line 165

def copy_file(source, destination)
  log.debug(log_key) { "Copying `#{source}' to `#{destination}'" }
  FileUtils.cp(source, destination)
  destination
end

#create_directory(*paths) ⇒ String

Create a directory at the given path.


133
134
135
136
137
138
# File 'lib/omnibus/util.rb', line 133

def create_directory(*paths)
  path = File.join(*paths)
  log.debug(log_key) { "Creating directory `#{path}'" }
  FileUtils.mkdir_p(path)
  path
end

#create_file(*paths, &block) ⇒ String

Create a file at the given path. If a block is given, the contents of the block are written to the file. If the block is not given, the file is simply “touched”.


198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/omnibus/util.rb', line 198

def create_file(*paths, &block)
  path = File.join(*paths)
  log.debug(log_key) { "Creating file `#{path}'" }

  FileUtils.mkdir_p(File.dirname(path))

  if block
    File.open(path, 'wb') { |f| f.write(block.call) }
  else
    FileUtils.touch(path)
  end

  path
end

Create a symlink from a to b


219
220
221
222
# File 'lib/omnibus/util.rb', line 219

def create_link(a, b)
  log.debug(log_key) { "Linking `#{a}' to `#{b}'" }
  FileUtils.ln_s(a, b)
end

#remove_directory(*paths) ⇒ String

Remove the directory at the given path.


149
150
151
152
153
154
# File 'lib/omnibus/util.rb', line 149

def remove_directory(*paths)
  path = File.join(*paths)
  log.debug(log_key) { "Remove directory `#{path}'" }
  FileUtils.rm_rf(path)
  path
end

#remove_file(*paths) ⇒ String

Remove the file at the given path.


180
181
182
183
184
185
# File 'lib/omnibus/util.rb', line 180

def remove_file(*paths)
  path = File.join(*paths)
  log.debug(log_key) { "Removing file `#{path}'" }
  FileUtils.rm_f(path)
  path
end

#shellout(command, options = {}) ⇒ Mixlib::ShellOut #shellout(command_fragments, options = {}) ⇒ Mixlib::ShellOut

Shells out and runs command.


52
53
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/omnibus/util.rb', line 52

def shellout(*args)
  options = args.last.kind_of?(Hash) ? args.pop : {}
  options = SHELLOUT_OPTIONS.merge(options)

  # Grab the log_level
  log_level = options.delete(:log_level)

  # Set the live stream if one was not given
  options[:live_stream] ||= log.live_stream(:internal)

  # Since Mixlib::ShellOut supports :environment and :env, we want to
  # standardize here
  if options[:env]
    options[:environment] = options.fetch(:environment, {}).merge(options[:env])
  end

  # Log any environment options given
  unless options[:environment].empty?
    log.public_send(log_level, log_key)  { 'Environment:' }
    options[:environment].sort.each do |key, value|
      log.public_send(log_level, log_key)  { "  #{key}=#{value.inspect}" }
    end
  end

  # Log the actual command
  log.public_send(log_level, log_key) { "$ #{args.join(' ')}" }

  cmd = Mixlib::ShellOut.new(*args, options)
  cmd.environment['HOME'] = '/tmp' unless ENV['HOME']
  cmd.run_command
  cmd
end

#shellout!(*args) ⇒ Object

Similar to shellout method except it raises an exception if the command fails.

Raises:

See Also:


96
97
98
99
100
101
102
103
104
# File 'lib/omnibus/util.rb', line 96

def shellout!(*args)
  cmd = shellout(*args)
  cmd.error!
  cmd
rescue Mixlib::ShellOut::ShellCommandFailed
  raise CommandFailed.new(cmd)
rescue Mixlib::ShellOut::CommandTimeout
  raise CommandTimeout.new(cmd)
end

#windows_safe_path(*pieces) ⇒ String

Convert the given path to be appropiate for shelling out on Windows.


114
115
116
117
118
119
120
121
122
# File 'lib/omnibus/util.rb', line 114

def windows_safe_path(*pieces)
  path = File.join(*pieces)

  if File::ALT_SEPARATOR
    path.gsub(File::SEPARATOR, File::ALT_SEPARATOR)
  else
    path
  end
end