summaryrefslogtreecommitdiff
path: root/puppet/modules/try/manifests/file.pp
blob: d21925c0590527e988a08bfaeecf64c620dcb7f1 (plain)
1
2
3
4
5
6
7
8
9
10
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#
# Works like the built-in type "file", but gets gracefully ignored if the target/source does not exist or is undefined.
#
# Also, if the source or target doesn't exist, and the destination is a git repo, then the file is restored from git.
#
# /bin/true and /usr/bin/test are hardcoded to their paths in debian.
#
# known limitations:
# * this is far too noisy
# * $restore does not work for directories
# * only file:// $source is supported
# * $content is not supported, only $target or $source.
#
define try::file (
  $ensure = undef,
  $target = undef,
  $source = undef,
  $owner = undef,
  $group = undef,
  $recurse = undef,
  $purge = undef,
  $force = undef,
  $mode = undef,
  $restore = true) {

  # dummy exec to propagate requires:
  # metaparameter 'require' will get triggered by this dummy exec
  # so then we just need to depend on this to capture all requires.
  # exec { $name: command => "/bin/true" }

  exec {
    "chmod_${name}":
      command => "chmod -R ${mode} '${name}'",
      onlyif => "/usr/bin/test $mode",
      loglevel => debug;
    "chown_${name}":
      command => "chown -R ${owner} '${name}'",
      onlyif => "/usr/bin/test $owner",
      loglevel => debug;
    "chgrp_${name}":
      command => "chgrp -R ${group} '${name}'",
      onlyif => "/usr/bin/test $group",
      loglevel => debug;
  }

  if $target {
    exec { "symlink_${name}":
      command => "ln -s ${target} ${name}",
      onlyif => "/usr/bin/test -d '${target}'",
    }
  } elsif $source {
    if $ensure == "directory" {
      if $purge {
        exec { "rsync_${name}":
          command => "rsync -r --delete '${source}/' '${name}'",
          onlyif => "/usr/bin/test -d '${source}'",
          unless => "/usr/bin/diff -q '${source}' '${name}'",
          notify => [Exec["chmod_${name}"], Exec["chown_${name}"], Exec["chgrp_${name}"]]
        }
      } else {
        exec { "cp_r_${name}":
          command => "cp -r '${source}' '${name}'",
          onlyif => "/usr/bin/test -d '${source}'",
          unless => "/usr/bin/diff -q '${source}' '${name}'",
          notify => [Exec["chmod_${name}"], Exec["chown_${name}"], Exec["chgrp_${name}"]]
        }
      }
    } else {
      exec { "cp_${name}":
        command => "cp '${source}' '${name}'",
        onlyif => "/usr/bin/test -e '${source}'",
        unless => "/usr/bin/diff -q '${source}' '${name}'",
        notify => [Exec["chmod_${name}"], Exec["chown_${name}"], Exec["chgrp_${name}"]]
      }
    }
  }

  #
  # if the target/source does not exist (or is undef), and the file happens to be in a git repo,
  # then restore the file to its original state.
  #

  if $target {
    $target_or_source = $target
  } else {
    $target_or_source = $source
  }

  if ($target_or_source == undef) or $restore {
    $file_basename = basename($name)
    $file_dirname  = dirname($name)
    $command = "git rev-parse && unlink '${name}'; git checkout -- '${file_basename}' && chown --reference='${file_dirname}' '${name}'; true"
    debug($command)

    if $target_or_source == undef {
      exec { "restore_${name}":
        command => $command,
        cwd => $file_dirname,
        loglevel => info;
      }
    } else {
      exec { "restore_${name}":
        unless => "/usr/bin/test -e '${target_or_source}'",
        command => $command,
        cwd => $file_dirname,
        loglevel => info;
      }
    }
  }
}