Class: Bixby::Provision::Config

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

Constant Summary collapse

EXPORTS =
[]

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

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
# File 'lib/bixby/provision/dsl/config.rb', line 11

def file(dest, opts={})

  # By default, attempt to preserve ownership and mode if none are given in opts
  opts[:preserve] = true if !opts.include? :preserve

  dest_file = File.expand_path(dest)
  if File.exists? dest_file then
    stat = File.stat(dest_file)
    old_owner = "#{stat.uid}:#{stat.gid}"
    old_mode  = stat.mode
  else
    old_owner = old_mode = nil
    dir.create(File.dirname(dest_file))
  end

  source = resolve_file(opts.delete(:source), dest_file)
  if source.nil? then
    # TODO raise
  end

  template = get_template(source)
  if template.nil? then

    if sha256sum(dest_file) == sha256sum(source) then
      logger.info "[config] skipping #{dest_file}: sha256sum matches"
      return
    end

    # just copy the file over
    if File.writable?(dest_file) then
      logger.info "[config] copying #{source} to #{dest}"
      FileUtils.cp(source, dest_file)
    else
      logger.info "[config] copying #{source} to #{dest} (as root)"
      logged_sudo("cp #{source} #{dest_file}")
    end

  else
    # use template
    logger.info "[config] rendering template #{source}"
    str = template.render(self.proxy)

    if (File.exists? dest_file and File.writable? dest_file) or
        File.writable? File.dirname(dest_file) then

      # write directly
      File.open(dest_file, 'w') { |f| f.write str }

    else
      # write to temp and mv into place
      t = Tempfile.new("bixby-provision-")
      t.write str
      t.close
      logged_sudo("mv #{t.path} #{dest_file}")
    end

  end

  # set correct ownership/mode
  owner = opts[:chown]
  mode = opts[:chmod]
  if opts[:preserve] then
    owner ||= old_owner
    mode ||= old_mode
  end
  chown(dest_file, owner)
  chmod(dest_file, mode)

end