Module: Teleport::Mirror

Includes:
Constants
Included in:
Install
Defined in:
lib/teleport/mirror.rb

Overview

Helper module for recursively mirroring files from a src to a dst.

Constant Summary collapse

IGNORE =

Don’t install these files.

[
 ".",
 "..",
 "Gemfile",
 "Gemfile.lock",              
 /^.#/,
]

Constants included from Constants

Constants::DATA, Constants::DIR, Constants::FILES, Constants::GEM, Constants::PUBKEY, Constants::RUBYGEMS

Instance Method Summary collapse

Instance Method Details

#install_dir(path) ⇒ Object

Install directory from the teleport data directory into the normal filesystem. Path can use a few different formats:

Returns true if the dir was installed, false if the dir had previously been installed and no change was required.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/teleport/mirror.rb', line 61

def install_dir(path)
  dirty = false

  path, dst = path_to_src(path), path_to_dst(path)
  mkdir_if_necessary(dst, user_for_file(dst)) if !dst.empty?      
  
  files = Dir.new(path).to_a.sort
  files.delete_if { |file| IGNORE.any? { |i| i === file } }
  files.each do |i|
    i = "#{path}/#{i}"
    if File.directory?(i)
      dirty = install_dir(i) || dirty
    else
      dirty = install_file(i) || dirty
    end
  end
  
  dirty
end

#install_file(path) ⇒ Object

Install file from the teleport data directory into the normal filesystem. Path can use a few different formats:

Note that the path can be an erb file. For example, “etc/hosts.erb” will be installed as “etc/hosts”.

Returns true if the file was installed, false if the file had previously been installed and no change was required.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/teleport/mirror.rb', line 30

def install_file(path)
  path, dst = path_to_src(path), path_to_dst(path)

  # run erb if necessary
  if path =~ /#{DATA}\/(.*)\.erb$/
    tmp = "#{DATA}/#{$1.gsub('/', '_')}"
    dst = dst.gsub(/\.erb$/, "")
    File.open(tmp, "w") do |f|
      f.write ERB.new(File.read(path)).result(binding)
    end
    (path, tmp)
    path = tmp
  end

  if !File.symlink?(path)
    cp_if_necessary(path, dst, user_for_file(dst), mode_for_file(dst))
  else
    ln_if_necessary(File.readlink(path), dst)
  end
end