File: get.pp

package info (click to toggle)
puppet-module-puppetlabs-rsync 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 228 kB
  • sloc: ruby: 634; sh: 15; makefile: 4
file content (169 lines) | stat: -rw-r--r-- 4,116 bytes parent folder | download | duplicates (2)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Definition: rsync::get
#
# get files via rsync
#
# Parameters:
#   $source  - source to copy from
#   $path    - path to copy to, defaults to $name
#   $user    - username on remote system
#   $purge   - if set, rsync will use '--delete'
#   $exlude  - string (or array) to be excluded
#   $include - string (or array) to be included
#   $exclude_first - if 'true' (default) then first exclude and then include; the other way around if 'false'
#   $keyfile - path to ssh key used to connect to remote host, defaults to /home/${user}/.ssh/id_rsa
#   $timeout - timeout in seconds, defaults to 900
#   $options - default options to pass to rsync (-a)
#   $chown   - ownership to pass to rsync (optional; requires rsync 3.1.0+)
#   $chmod   - permissions to pass to rsync (optional)
#   $logfile - logname to pass to rsync (optional)
#   $onlyif  - Condition to run the rsync command
#
# Actions:
#   get files via rsync
#
# Requires:
#   $source must be set
#
# Sample Usage:
#
#  rsync::get { '/foo':
#    source  => "rsync://${rsyncServer}/repo/foo/",
#    require => File['/foo'],
#  } # rsync
#
define rsync::get (
  $source,
  $path          = $name,
  $user          = undef,
  $purge         = undef,
  $recursive     = undef,
  $links         = undef,
  $hardlinks     = undef,
  $copylinks     = undef,
  $times         = undef,
  $include       = undef,
  $exclude       = undef,
  $exclude_first = true,
  $keyfile       = undef,
  $timeout       = '900',
  $execuser      = 'root',
  $options       = '-a',
  $chown         = undef,
  $chmod         = undef,
  $logfile       = undef,
  $onlyif        = undef,
) {

  if $keyfile {
    $mykeyfile = $keyfile
  } else {
    $mykeyfile = "/home/${user}/.ssh/id_rsa"
  }

  if $user {
    $myuser = "-e 'ssh -i ${mykeyfile} -l ${user}' ${user}@"
  } else {
    $myuser = undef
  }

  if $purge {
    $mypurge = '--delete'
  } else {
    $mypurge = undef
  }

  if $exclude {
    $myexclude = join(prefix(flatten([$exclude]), '--exclude='), ' ')
  } else {
    $myexclude = undef
  }

  if $include {
    $myinclude = join(prefix(flatten([$include]), '--include='), ' ')
  } else {
    $myinclude = undef
  }

  if $recursive {
    $myrecursive = '-r'
  } else {
    $myrecursive = undef
  }

  if $links {
    $mylinks = '--links'
  } else {
    $mylinks = undef
  }

  if $hardlinks {
    $myhardlinks = '--hard-links'
  } else {
    $myhardlinks = undef
  }

  if $copylinks {
    $mycopylinks = '--copy-links'
  } else {
    $mycopylinks = undef
  }

  if $times {
    $mytimes = '--times'
  } else {
    $mytimes = undef
  }

  if $chown {
    $mychown = "--chown=${chown}"
  } else {
    $mychown = undef
  }

  if $chmod {
    $mychmod = "--chmod=${chmod}"
  } else {
    $mychmod = undef
  }

  if $logfile {
    $mylogfile = "--log-file=${logfile}"
  } else {
    $mylogfile = undef
  }

  if $include or $exclude {
    if $exclude_first {
      $excludeandinclude = join(delete_undef_values([$myexclude, $myinclude]), ' ')
    } else {
      $excludeandinclude = join(delete_undef_values([$myinclude, $myexclude]), ' ')
    }
  } else {
    $excludeandinclude = undef
  }

  $rsync_options = join(
    delete_undef_values([$options, $mypurge, $excludeandinclude, $mylinks, $myhardlinks, $mycopylinks, $mytimes,
      $myrecursive, $mychown, $mychmod, $mylogfile, "${myuser}${source}", $path]), ' ')

  if !$onlyif {
    $onlyif_real = "test `rsync --dry-run --itemize-changes ${rsync_options} | wc -l` -gt 0"
  } else {
    $onlyif_real = $onlyif
  }


  exec { "rsync ${name}":
    command => "rsync -q ${rsync_options}",
    path    => [ '/bin', '/usr/bin', '/usr/local/bin' ],
    user    => $execuser,
    # perform a dry-run to determine if anything needs to be updated
    # this ensures that we only actually create a Puppet event if something needs to
    # be updated
    # TODO - it may make senes to do an actual run here (instead of a dry run)
    #        and relace the command with an echo statement or something to ensure
    #        that we only actually run rsync once
    onlyif  => $onlyif_real,
    timeout => $timeout,
  }
}