Class: Bixby::Provision::FileDSL

Inherits:
Base
  • Object
show all
Defined in:
lib/bixby/provision/dsl/file.rb

Constant Summary collapse

EXPORTS =
[:symlink, :copy, :cp]

Constants inherited from Base

Base::PATH

Instance Attribute Summary

Attributes inherited from Base

#manifest, #proxy

Instance Method Summary collapse

Methods inherited from Base

#get_gid, #get_group, #get_uid, #get_user, #initialize, #tap

Methods included from Util::File

#chmod, #chown, #sha256sum, #which

Constructor Details

This class inherits a constructor from Bixby::Provision::Base

Instance Method Details

#copy(*args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
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
71
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
# File 'lib/bixby/provision/dsl/file.rb', line 31

def copy(*args)
  if args.last.kind_of? Hash then
    opts = args.pop
  else
    opts = {}
  end

  dest = File.expand_path(args.pop)
  if args.length > 1 || args.first.include?("*") then
    dest_dir = dest
    self.dir.mkdir(dest_dir)
  elsif File.directory? dest then
    dest_dir = dest
  else
    dest_dir = File.dirname(dest)
    self.dir.mkdir(dest_dir)
  end

  args = args.map{ |s| File.expand_path(s) }
  files = args.join(' ')

  flags = ""
  flags += "-r" if (opts[:recurse] || opts[:recursively])
  flags += " -f" if opts[:force]

  if File.writable? dest_dir then
    dest = args.size > 1 ? dest_dir : dest
    logger.info "[file] copying #{files} -> #{dest}"
    logged_systemu("cp #{flags} #{files} #{dest}")

  else
    # as root
    dest = args.size > 1 ? dest_dir : dest
    logger.info "[file] copying #{files} -> #{dest}"
    logged_sudo("cp #{flags} #{files} #{dest}")
  end


  return if !(opts[:chmod] or opts[:chown])

  # need to take care to translate src args to dest path
  # can't simply pass args into chmod/chown for this reason,
  # since we want to affect the newly copied files

  if args.length > 1 || args.first.include?("*") then
    # work on all source files
    args.each do |s|
      if s.include? "*" then
        Dir.glob(s).each{ |e|
          f = File.join(dest_dir, File.basename(e))
          chmod(f, opts[:chmod]) if opts[:chmod]
          chown(f, opts[:chown]) if opts[:chown]
        }
      else
        f = File.join(dest_dir, File.basename(e))
        chmod(f, opts[:chmod]) if opts[:chmod]
        chown(f, opts[:chown]) if opts[:chown]
      end
    end

  else
    # work on a single file
    if File.directory? dest then
      dest = File.join(dest, File.basename(args.first))
    end

    chmod(dest, opts[:chmod]) if opts[:chmod]
    chown(dest, opts[:chown]) if opts[:chown]
  end

end

#create(opts = {}) ⇒ Object



9
10
# File 'lib/bixby/provision/dsl/file.rb', line 9

def create(opts={})
end


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bixby/provision/dsl/file.rb', line 12

def symlink(source, dest)
  source = File.expand_path(source)
  dest   = File.expand_path(dest)

  if File.symlink?(dest) && File.realpath(dest) == source then
    logger.info "[file] #{dest} already points to #{source}"
    return
  end

  logger.info "[file] creating symlink: #{dest} -> #{source}"

  if File.writable? File.dirname(dest) then
    FileUtils.symlink(source, dest)
  else
    # as root
    logged_sudo("ln -sf #{source} #{dest}")
  end
end