|  | 
| 1 |  | -# Definition: rsync::get | 
| 2 |  | -# | 
| 3 |  | -# get files via rsync | 
| 4 |  | -# | 
| 5 |  | -# Parameters: | 
| 6 |  | -#   $source  - source to copy from | 
| 7 |  | -#   $path    - path to copy to, defaults to $name | 
| 8 |  | -#   $user    - username on remote system | 
| 9 |  | -#   $purge   - if set, rsync will use '--delete' | 
| 10 |  | -#   $exlude  - string (or array) to be excluded | 
| 11 |  | -#   $include - string (or array) to be included | 
| 12 |  | -#   $exclude_first - if 'true' (default) then first exclude and then include; the other way around if 'false' | 
| 13 |  | -#   $keyfile - path to ssh key used to connect to remote host, defaults to /home/${user}/.ssh/id_rsa | 
| 14 |  | -#   $timeout - timeout in seconds, defaults to 900 | 
| 15 |  | -#   $options - default options to pass to rsync (-a) | 
| 16 |  | -#   $chown   - ownership to pass to rsync (optional; requires rsync 3.1.0+) | 
| 17 |  | -#   $chmod   - permissions to pass to rsync (optional) | 
| 18 |  | -#   $logfile - logname to pass to rsync (optional) | 
| 19 |  | -#   $onlyif  - Condition to run the rsync command | 
| 20 |  | -# | 
| 21 |  | -# Actions: | 
|  | 1 | +# @summary | 
| 22 | 2 | #   get files via rsync | 
| 23 | 3 | # | 
| 24 |  | -# Requires: | 
| 25 |  | -#   $source must be set | 
| 26 |  | -# | 
| 27 |  | -# Sample Usage: | 
|  | 4 | +# @param source | 
|  | 5 | +#   source to copy from | 
|  | 6 | +# @param path | 
|  | 7 | +#   path to copy to, defaults to $name | 
|  | 8 | +# @param user | 
|  | 9 | +#   username on remote system | 
|  | 10 | +# @param purge | 
|  | 11 | +#   if set, rsync will use '--delete' | 
|  | 12 | +# @param exclude | 
|  | 13 | +#   Path or paths to be exclude | 
|  | 14 | +# @param include | 
|  | 15 | +#   Path or paths to include | 
|  | 16 | +# @param exclude_first | 
|  | 17 | +#   if 'true' (default) then first exclude and then include; the other way around if 'false' | 
|  | 18 | +# @param keyfile | 
|  | 19 | +#   path to ssh key used to connect to remote host, defaults to /home/${user}/.ssh/id_rsa | 
|  | 20 | +# @param timeout | 
|  | 21 | +#   timeout in seconds, defaults to 900 | 
|  | 22 | +# @param options | 
|  | 23 | +#   default options to pass to rsync (-a) | 
|  | 24 | +# @param chown | 
|  | 25 | +#   ownership to pass to rsync (requires rsync 3.1.0+) | 
|  | 26 | +# @param chmod | 
|  | 27 | +#   permissions to pass to rsync | 
|  | 28 | +# @param logfile | 
|  | 29 | +#   logname to pass to rsync | 
|  | 30 | +# @param onlyif | 
|  | 31 | +#   Condition to run the rsync command | 
| 28 | 32 | # | 
|  | 33 | +# @example Sync from a source | 
| 29 | 34 | #  rsync::get { '/foo': | 
| 30 | 35 | #    source  => "rsync://${rsyncServer}/repo/foo/", | 
| 31 | 36 | #    require => File['/foo'], | 
| 32 |  | -#  } # rsync | 
|  | 37 | +#  } | 
| 33 | 38 | # | 
| 34 | 39 | define rsync::get ( | 
| 35 |  | -  $source, | 
| 36 |  | -  $path          = $name, | 
| 37 |  | -  $user          = undef, | 
| 38 |  | -  $purge         = undef, | 
| 39 |  | -  $recursive     = undef, | 
| 40 |  | -  $links         = undef, | 
| 41 |  | -  $hardlinks     = undef, | 
| 42 |  | -  $copylinks     = undef, | 
| 43 |  | -  $times         = undef, | 
| 44 |  | -  $include       = undef, | 
| 45 |  | -  $exclude       = undef, | 
| 46 |  | -  $exclude_first = true, | 
| 47 |  | -  $keyfile       = undef, | 
| 48 |  | -  $timeout       = '900', | 
| 49 |  | -  $execuser      = 'root', | 
| 50 |  | -  $options       = '-a', | 
| 51 |  | -  $chown         = undef, | 
| 52 |  | -  $chmod         = undef, | 
| 53 |  | -  $logfile       = undef, | 
| 54 |  | -  $onlyif        = undef, | 
|  | 40 | +  String[1] $source, | 
|  | 41 | +  String[1] $path = $name, | 
|  | 42 | +  Optional[String[1]] $user = undef, | 
|  | 43 | +  Boolean $purge = false, | 
|  | 44 | +  Boolean $recursive = false, | 
|  | 45 | +  Boolean $links = false, | 
|  | 46 | +  Boolean $hardlinks = false, | 
|  | 47 | +  Boolean $copylinks = false, | 
|  | 48 | +  Boolean $times = false, | 
|  | 49 | +  Variant[Array[String[1]], String[1]] $include = [], | 
|  | 50 | +  Variant[Array[String[1]], String[1]] $exclude = [], | 
|  | 51 | +  Boolean $exclude_first = true, | 
|  | 52 | +  Optional[String[1]] $keyfile = undef, | 
|  | 53 | +  Integer[0] $timeout = 900, | 
|  | 54 | +  String[1] $execuser = 'root', | 
|  | 55 | +  Array[String[1]] $options = ['-a'], | 
|  | 56 | +  Optional[String[1]] $chown = undef, | 
|  | 57 | +  Optional[String[1]] $chmod = undef, | 
|  | 58 | +  Optional[String[1]] $logfile       = undef, | 
|  | 59 | +  Variant[Undef, String[1], Array[String[1]]] $onlyif = undef, | 
| 55 | 60 | ) { | 
| 56 | 61 |   if $keyfile { | 
| 57 | 62 |     $mykeyfile = $keyfile | 
|  | 
| 60 | 65 |   } | 
| 61 | 66 | 
 | 
| 62 | 67 |   if $user { | 
| 63 |  | -    $myuser = "-e 'ssh -i ${mykeyfile} -l ${user}' ${user}@" | 
|  | 68 | +    $myuseropt = ['-e', "'ssh -i ${mykeyfile} -l ${user}'"] | 
|  | 69 | +    $myuser = "${user}@" | 
| 64 | 70 |   } else { | 
| 65 |  | -    $myuser = undef | 
|  | 71 | +    $myuseropt = [] | 
|  | 72 | +    $myuser = '' | 
| 66 | 73 |   } | 
| 67 | 74 | 
 | 
| 68 | 75 |   if $purge { | 
| 69 |  | -    $mypurge = '--delete' | 
| 70 |  | -  } else { | 
| 71 |  | -    $mypurge = undef | 
| 72 |  | -  } | 
| 73 |  | - | 
| 74 |  | -  if $exclude { | 
| 75 |  | -    $myexclude = join(prefix(flatten([$exclude]), '--exclude='), ' ') | 
|  | 76 | +    $mypurge = ['--delete'] | 
| 76 | 77 |   } else { | 
| 77 |  | -    $myexclude = undef | 
|  | 78 | +    $mypurge = [] | 
| 78 | 79 |   } | 
| 79 | 80 | 
 | 
| 80 |  | -  if $include { | 
| 81 |  | -    $myinclude = join(prefix(flatten([$include]), '--include='), ' ') | 
| 82 |  | -  } else { | 
| 83 |  | -    $myinclude = undef | 
| 84 |  | -  } | 
|  | 81 | +  $myexclude = prefix(any2array($exclude), '--exclude=') | 
|  | 82 | +  $myinclude = prefix(any2array($include), '--include=') | 
| 85 | 83 | 
 | 
| 86 | 84 |   if $recursive { | 
| 87 |  | -    $myrecursive = '-r' | 
|  | 85 | +    $myrecursive = ['-r'] | 
| 88 | 86 |   } else { | 
| 89 |  | -    $myrecursive = undef | 
|  | 87 | +    $myrecursive = [] | 
| 90 | 88 |   } | 
| 91 | 89 | 
 | 
| 92 | 90 |   if $links { | 
| 93 |  | -    $mylinks = '--links' | 
|  | 91 | +    $mylinks = ['--links'] | 
| 94 | 92 |   } else { | 
| 95 |  | -    $mylinks = undef | 
|  | 93 | +    $mylinks = [] | 
| 96 | 94 |   } | 
| 97 | 95 | 
 | 
| 98 | 96 |   if $hardlinks { | 
| 99 |  | -    $myhardlinks = '--hard-links' | 
|  | 97 | +    $myhardlinks = ['--hard-links'] | 
| 100 | 98 |   } else { | 
| 101 |  | -    $myhardlinks = undef | 
|  | 99 | +    $myhardlinks = [] | 
| 102 | 100 |   } | 
| 103 | 101 | 
 | 
| 104 | 102 |   if $copylinks { | 
| 105 |  | -    $mycopylinks = '--copy-links' | 
|  | 103 | +    $mycopylinks = ['--copy-links'] | 
| 106 | 104 |   } else { | 
| 107 |  | -    $mycopylinks = undef | 
|  | 105 | +    $mycopylinks = [] | 
| 108 | 106 |   } | 
| 109 | 107 | 
 | 
| 110 | 108 |   if $times { | 
| 111 |  | -    $mytimes = '--times' | 
|  | 109 | +    $mytimes = ['--times'] | 
| 112 | 110 |   } else { | 
| 113 |  | -    $mytimes = undef | 
|  | 111 | +    $mytimes = [] | 
| 114 | 112 |   } | 
| 115 | 113 | 
 | 
| 116 | 114 |   if $chown { | 
| 117 |  | -    $mychown = "--chown=${chown}" | 
|  | 115 | +    $mychown = ["--chown=${chown}"] | 
| 118 | 116 |   } else { | 
| 119 |  | -    $mychown = undef | 
|  | 117 | +    $mychown = [] | 
| 120 | 118 |   } | 
| 121 | 119 | 
 | 
| 122 | 120 |   if $chmod { | 
| 123 |  | -    $mychmod = "--chmod=${chmod}" | 
|  | 121 | +    $mychmod = ["--chmod=${chmod}"] | 
| 124 | 122 |   } else { | 
| 125 |  | -    $mychmod = undef | 
|  | 123 | +    $mychmod = [] | 
| 126 | 124 |   } | 
| 127 | 125 | 
 | 
| 128 | 126 |   if $logfile { | 
| 129 |  | -    $mylogfile = "--log-file=${logfile}" | 
|  | 127 | +    $mylogfile = ["--log-file=${logfile}"] | 
| 130 | 128 |   } else { | 
| 131 |  | -    $mylogfile = undef | 
|  | 129 | +    $mylogfile = [] | 
| 132 | 130 |   } | 
| 133 | 131 | 
 | 
| 134 |  | -  if $include or $exclude { | 
| 135 |  | -    if $exclude_first { | 
| 136 |  | -      $excludeandinclude = join(delete_undef_values([$myexclude, $myinclude]), ' ') | 
| 137 |  | -    } else { | 
| 138 |  | -      $excludeandinclude = join(delete_undef_values([$myinclude, $myexclude]), ' ') | 
| 139 |  | -    } | 
|  | 132 | +  if $exclude_first { | 
|  | 133 | +    $excludeandinclude = $myexclude + $myinclude | 
| 140 | 134 |   } else { | 
| 141 |  | -    $excludeandinclude = undef | 
|  | 135 | +    $excludeandinclude = $myinclude + $myexclude | 
| 142 | 136 |   } | 
| 143 | 137 | 
 | 
| 144 |  | -  $rsync_options = join( | 
| 145 |  | -    delete_undef_values([$options, $mypurge, $excludeandinclude, $mylinks, $myhardlinks, $mycopylinks, $mytimes, | 
| 146 |  | -      $myrecursive, $mychown, $mychmod, $mylogfile, "${myuser}${source}", $path]), ' ') | 
|  | 138 | +  $rsync_options = $options + $mypurge + $excludeandinclude + $mylinks + $myhardlinks + $mycopylinks + $mytimes + $myrecursive + $mychown + $mychmod + $mylogfile + $myuseropt + ["${myuser}${source}", $path] | 
|  | 139 | +  $command = ['rsync' + '-q'] + $rsync_options | 
|  | 140 | +  $rsync_options_str = join($rsync_options, ' ') | 
| 147 | 141 | 
 | 
| 148 |  | -  if !$onlyif { | 
| 149 |  | -    $onlyif_real = "test `rsync --dry-run --itemize-changes ${rsync_options} | wc -l` -gt 0" | 
| 150 |  | -  } else { | 
|  | 142 | +  if $onlyif { | 
| 151 | 143 |     $onlyif_real = $onlyif | 
|  | 144 | +  } else { | 
|  | 145 | +    # TODO: add dry run to $command? | 
|  | 146 | +    $onlyif_real = "test `rsync --dry-run --itemize-changes ${rsync_options_str} | wc -l` -gt 0" | 
| 152 | 147 |   } | 
| 153 | 148 | 
 | 
| 154 |  | - | 
| 155 | 149 |   exec { "rsync ${name}": | 
| 156 |  | -    command => "rsync -q ${rsync_options}", | 
|  | 150 | +    command => $command, | 
| 157 | 151 |     path    => ['/bin', '/usr/bin', '/usr/local/bin'], | 
| 158 | 152 |     user    => $execuser, | 
| 159 | 153 |     # perform a dry-run to determine if anything needs to be updated | 
|  | 
0 commit comments