Module: Prepper::Tools::File

Included in:
Package
Defined in:
lib/prepper/tools/file.rb

Overview

Helper methods for file and directory management

Instance Method Summary collapse

Instance Method Details

#chown(path, owner, flags = "") ⇒ Object

Changes ownership of a path

Parameters:

  • path (String)

    the path which we want to change the ownership of

  • owner (String)

    name of the owner, ie: ‘root:root’

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

    flags to pass to chown, ie: ‘-R’ to do it recursively



12
13
14
# File 'lib/prepper/tools/file.rb', line 12

def chown(path, owner, flags = "")
  @commands << Command.new("chown #{flags} #{owner} #{path}", sudo: true)
end

#directory(path, opts = {}) ⇒ Object

Create a directory unless it already exists

Parameters:

  • path (String)

    path of the directory

  • opts (Hash) (defaults to: {})

    options hash

Options Hash (opts):

  • :owner (String)

    Owner of the directory, ie: ‘root:root’

  • :mode (String)

    mode bits, ie: ‘0777’



21
22
23
24
25
# File 'lib/prepper/tools/file.rb', line 21

def directory(path, opts = {})
  @commands <<  Command.new("mkdir -p #{path}", opts.merge(sudo: true, verifier: has_directory?(path)))
  @commands <<  Command.new("chown #{opts[:owner]} #{path}", sudo: true) if opts[:owner]
  @commands <<  Command.new("chmod #{opts[:mode]} #{path}", sudo: true) if opts[:mode]
end

#file(path, opts = {}) ⇒ Object

Create a file unless it already exists. The contents can be set to a string or a template can be rendered with the provided locals

Parameters:

  • path (String)

    path of the file

  • opts (Hash) (defaults to: {})

    options hash

Options Hash (opts):

  • :content (String)

    string content of the file

  • :template (String)

    name of the template for the file

  • :locals (String)

    hash of variables to pass to the template

  • :verify_content (String)

    set to true to verify the file content is the same in case the file already exists

  • :owner (String)

    Owner of the directory, ie: ‘root:root’

  • :mode (String)

    mode bits, ie: ‘0777’



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/prepper/tools/file.rb', line 44

def file(path, opts = {})
  opts[:locals] ||= {}
  opts[:verify_content] ||= true
  content = opts[:content] || render_template(opts[:template], opts[:locals])
  verifier = if opts[:verify_content]
    matches_content?(path, content)
  else
    has_file?(path)
  end
  io = StringIO.new(content)
  @commands << Command.new("put!", {params: [io, path, {owner: opts[:owner], mode: opts[:mode]}], verifier: verifier})
end

#has_directory?(path) ⇒ Boolean

returns a verifier command to test if a directory exists

Parameters:

  • path (String)

    path to test

Returns:

  • (Boolean)


29
30
31
# File 'lib/prepper/tools/file.rb', line 29

def has_directory?(path)
  Command.new("test -d #{path}", sudo: true)
end

#has_file?(path) ⇒ Boolean

returns a verifier command to test if a file exists

Parameters:

  • path (String)

    path to test

Returns:

  • (Boolean)


59
60
61
# File 'lib/prepper/tools/file.rb', line 59

def has_file?(path)
  Command.new("test -f #{path}", sudo: true)
end

#has_symlink?(link, file = nil) ⇒ Boolean

returns a verifier command to test if symlink exists

Parameters:

  • path (String)

    path to test

  • file (String) (defaults to: nil)

    optionally check if it points to the correct file

Returns:

  • (Boolean)


106
107
108
109
110
111
112
# File 'lib/prepper/tools/file.rb', line 106

def has_symlink?(link, file = nil)
  if file
    Command.new("'#{file}' = `readlink #{link}`")
  else
    Command.new("test -L #{link}", sudo: true)
  end
end

#matches_content?(path, content) ⇒ Boolean

returns a verifier command to test if as a matching content

Parameters:

  • path (String)

    path to test

  • content (String)

    expected content

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/prepper/tools/file.rb', line 66

def matches_content?(path, content)
  md5 = Digest::MD5.hexdigest(content)
  Command.new("md5sum #{path} | cut -f1 -d' '`\" = \"#{md5}\"", sudo: true, verifier: has_file?(path))
end

#render_template(template, locals) ⇒ Object

render an ERB template

Parameters:

  • template (String)

    name of the template

  • locals (Hash)

    hash of variables to pass to the template



117
118
119
# File 'lib/prepper/tools/file.rb', line 117

def render_template(template, locals)
  ERB.new(::File.read("./templates/#{template}")).result_with_hash(locals)
end

creates a symlink

Parameters:

  • link (String)

    link

  • target (String)

    target

  • opts (Hash) (defaults to: {})

    options hash



75
76
77
78
79
80
81
# File 'lib/prepper/tools/file.rb', line 75

def symlink(link, target, opts = {})
  opts.merge!(
    sudo: true,
    verifier: has_symlink?(link)
  )
  @commands << Command.new("ln -s #{target} #{link}", opts)
end