Defined Type: tp::copy_file

Defined in:
manifests/copy_file.pp

Summary

Copies a file from source to destination

Overview

This defines copied a file from a source to a destination using an exec resource. It does not overwrite the destination file if it exists (and overwrite is set to true) and can be used as alternative to a file resource to avoid duplicated  resources.

Parameters:

  • source (Stdlib::AbsolutePath)

    The source of the file to copy (must be a valid path)

  • ensure (Enum['present','absent']) (defaults to: 'present')

    If to create (present) or remove (absent)the target file

  • path (Stdlib::AbsolutePath) (defaults to: $title)

    The path of the file to create. Default $title

  • owner (Optional[String]) (defaults to: undef)

    The owner of the created file

  • group (Optional[String]) (defaults to: undef)

    The group of the created file

  • mode (Optional[String]) (defaults to: undef)

    The mode of the created file

  • overwrite (Boolean) (defaults to: false)

    If to overwrite the target file when already existing



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
# File 'manifests/copy_file.pp', line 16

define tp::copy_file (
  Stdlib::AbsolutePath $source,
  Enum['present','absent'] $ensure = 'present',
  Stdlib::AbsolutePath $path       = $title,
  Optional[String] $owner          = undef,
  Optional[String] $group          = undef,
  Optional[String] $mode           = undef,
  Boolean $overwrite               = false,
) {
  if $overwrite {
    $exec_creates = undef
    $exec_unless  = "diff ${source} ${path} >/dev/null"
  } else {
    $exec_creates = $path
    $exec_unless  = undef
  }
  if $ensure == 'present' {
    exec { "tp::copy_file ${title}":
      command => "cp ${source} ${path}",
      path    => $facts['path'],
      creates => $exec_creates,
      unless  => $exec_unless,
    }
    if $owner {
      exec { "chown ${owner} ${title}":
        command => "chown ${owner} ${path}",
        path    => $facts['path'],
        onlyif  => "[ $(ls -ld ${path} | awk '{ print \$3 }') != ${owner} ]",
      }
    }
    if $group {
      exec { "chgrp ${group} ${title}":
        command => "chgrp ${group} ${path}",
        path    => $facts['path'],
        onlyif  => "[ $(ls -ld ${path} | awk '{ print \$4 }') != ${group} ]",
      }
    }
    if $mode {
      exec { "chmod ${mode} ${title}":
        command     => "chmod ${mode} ${path}",
        path        => $facts['path'],
        subscribe   => Exec["tp::copy_file ${title}"],
        refreshonly => true,
      }
    }
  } else {
    exec { "tp::copy_file ${title}":
      command => "rm -f ${path}",
      path    => $facts['path'],
      onlyif  => "test -f ${path}",
    }
  }
}