Module: Doable::Helpers::OS

Includes:
Exceptions::OS
Included in:
Linux
Defined in:
lib/doable/helpers/os.rb

Instance Method Summary collapse

Instance Method Details

#die_if_dir_exists(directory) ⇒ Boolean

Die if a directory __does__ exist. Useful for ensuring software wasn’t previously installed, etc.

Parameters:

  • directory (String)

    Directory to verify does not exist

Returns:

  • (Boolean)

Raises:



39
40
41
42
43
44
45
46
# File 'lib/doable/helpers/os.rb', line 39

def die_if_dir_exists(directory)
  full_path = File.expand_path(directory)
  if File.directory?(full_path)
    raise DirectoryExists, directory
  else
    return true
  end
end

#find_directory(directory) ⇒ String

Die if a directory doesn’t exist

Parameters:

  • directory (String)

    Directory to verify

Returns:

  • (String)

Raises:



28
29
30
31
32
# File 'lib/doable/helpers/os.rb', line 28

def find_directory(directory)
  full_path = File.expand_path(directory)
  raise MissingDirectory, directory unless File.directory?(full_path)
  return full_path
end

#find_file(file, message = "") ⇒ String

Throws an exception if a file doesn’t exist

Parameters:

  • file (String)

    File or directory to verify

  • message (String) (defaults to: "")

    Option message to log if file does not exist

Returns:

  • (String)

Raises:



97
98
99
100
101
102
103
104
# File 'lib/doable/helpers/os.rb', line 97

def find_file(file, message = "")
  unless File.exists?(File.expand_path(file))
    message.empty? ? log("Filename #{file} is invalid", :error) : log(message, :error)
    raise MissingFile, file
  end

  return File.expand_path(file)
end

#find_or_create_directory(directory) ⇒ String

Ensure a directory exists

Parameters:

  • directory (String)

    Directory to create or verify

Returns:

  • (String)


18
19
20
21
22
# File 'lib/doable/helpers/os.rb', line 18

def find_or_create_directory(directory)
  full_path = File.expand_path(directory)
  FileUtils.mkdir_p(full_path) unless File.directory?(full_path)
  return full_path
end

#find_or_create_file(file) ⇒ Object

Used for backwards compatibility (just a wrapper for touch)

Parameters:

  • file (String)

    File to create or verify



84
85
86
87
88
89
90
# File 'lib/doable/helpers/os.rb', line 84

def find_or_create_file(file)
  unless File.exists?(File.expand_path(file))
    log "File '#{file}' does not seem to exist... creating it...", :warn
    touch File.expand_path(file)
  end
  return File.expand_path(file)
end

#must_run_as(user) ⇒ Object

Confirms the current script is running as a certain user

Parameters:

  • user (String)

    User to confirm we’re using

Raises:



10
11
12
13
# File 'lib/doable/helpers/os.rb', line 10

def must_run_as(user)
  raise WrongUser, user unless user == Etc.getpwuid.name
  log "Running as correct user"
end

#remove(file_or_directory) ⇒ Object

Remove a file or directory

Parameters:

  • file_or_directory (String)

    File or directory path to remove



50
51
52
# File 'lib/doable/helpers/os.rb', line 50

def remove(file_or_directory)
  FileUtils.rm_rf(File.expand_path(file_or_directory))
end

#sed(substitutions, input_file, output_file) ⇒ Object

A simple replacement for ‘sed`. This is far from a complete implementation and should be used sparingly. A much better approach is to use ERB files.

Parameters:

  • substitutions (Hash)

    Hash of Strings indexing Strings. Keys are replaced with their value.

  • input_file (IO, String)

    IO object or String to use as the source

  • output_file (IO, String)

    IO object or String to use as the destination

Raises:



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/doable/helpers/os.rb', line 112

def sed(substitutions, input_file, output_file)
  input_file  = input_file.kind_of?(IO) ? input_file : File.open(input_file, "r")
  output_file = output_file.kind_of?(IO) ? output_file : File.open(output_file, "w")
  raise InvalidInput unless substitutions.kind_of?(Hash)

  # Very fancy way to replace things in a file and write to a new file
  output_file.puts(
    input_file.read.gsub(Regexp.union(substitutions.keys)) do |match|
      # Hideous way to work with regular expressions *and* string substitutions in a single pass over a large file
      substitutions[substitutions.keys.keep_if {|s| substitutions[s].to_s if match == s or s.match(match) }.first].to_s
    end
  )
  output_file.close
end

#tee(command) ⇒ Fixnum, String

Ruby in-memory equivalent of ‘tee` Note that this command is dangerous for very long command output. Need to consider setting a buffer max size for this.

Parameters:

  • command (String)

    Command to run

Returns:

  • (Fixnum, String)

    Returns the exit code of the command, along with any output



65
66
67
68
69
70
71
72
73
74
# File 'lib/doable/helpers/os.rb', line 65

def tee(command)
  command_output = ""
  IO.popen(command) do |io|
    while line = io.gets
      command_output << line
      LOGGING_MUTEX.synchronize { puts line } 
    end
  end
  return [$?.exitstatus, command_output]
end

#touch(file_list, options = {}) ⇒ Object

Used like Unix touch

Parameters:

  • file_list (Array<String>, String)

    List of files to create or verify



78
79
80
# File 'lib/doable/helpers/os.rb', line 78

def touch(file_list, options = {})
  FileUtils.touch(file_list, options)
end

#validate_host(hostname) ⇒ Object

Check that the OS can resolve a hostname

Parameters:

  • hostname (String)

    Hostname to validate is resolveable

Raises:



57
58
59
# File 'lib/doable/helpers/os.rb', line 57

def validate_host(hostname)
  raise InvalidHostname, hostname unless Resolv.getaddress(hostname)
end