Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameter descriptions and data types #157

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 86 additions & 92 deletions manifests/get.pp
Original file line number Diff line number Diff line change
@@ -1,57 +1,62 @@
# 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:
# @summary
# get files via rsync
#
# Requires:
# $source must be set
#
# Sample Usage:
# @param source
# source to copy from
# @param path
# path to copy to, defaults to $name
# @param user
# username on remote system
# @param purge
# if set, rsync will use '--delete'
# @param exclude
# Path or paths to be exclude
# @param include
# Path or paths to include
# @param exclude_first
# if 'true' (default) then first exclude and then include; the other way around if 'false'
# @param keyfile
# path to ssh key used to connect to remote host, defaults to /home/${user}/.ssh/id_rsa
# @param timeout
# timeout in seconds, defaults to 900
# @param options
# default options to pass to rsync (-a)
# @param chown
# ownership to pass to rsync (requires rsync 3.1.0+)
# @param chmod
# permissions to pass to rsync
# @param logfile
# logname to pass to rsync
# @param onlyif
# Condition to run the rsync command
#
# @example Sync from a source
# 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,
String[1] $source,
String[1] $path = $name,
Optional[String[1]] $user = undef,
Boolean $purge = false,
Boolean $recursive = false,
Boolean $links = false,
Boolean $hardlinks = false,
Boolean $copylinks = false,
Boolean $times = false,
Variant[Array[String[1]], String[1]] $include = [],
Variant[Array[String[1]], String[1]] $exclude = [],
Boolean $exclude_first = true,
Optional[String[1]] $keyfile = undef,
Integer[0] $timeout = 900,
String[1] $execuser = 'root',
Array[String[1]] $options = ['-a'],
Optional[String[1]] $chown = undef,
Optional[String[1]] $chmod = undef,
Optional[String[1]] $logfile = undef,
Variant[Undef, String[1], Array[String[1]]] $onlyif = undef,
) {
if $keyfile {
$mykeyfile = $keyfile
Expand All @@ -60,100 +65,89 @@
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

if !$onlyif {
$onlyif_real = "test `rsync --dry-run --itemize-changes ${rsync_options} | wc -l` -gt 0"
} else {
if $onlyif {
$onlyif_real = $onlyif
} else {
# TODO: add dry run to $command?
$onlyif_real = "test `rsync --dry-run --itemize-changes ${rsync_options_str} | wc -l` -gt 0"
}


exec { "rsync ${name}":
command => "rsync -q ${rsync_options}",
command => $command,
path => ['/bin', '/usr/bin', '/usr/local/bin'],
user => $execuser,
# perform a dry-run to determine if anything needs to be updated
Expand Down
19 changes: 13 additions & 6 deletions manifests/init.pp
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
# Class: rsync
# @summary manage rsync
#
# This module manages rsync
# @param package_ensure
# ensure state of the rsync package
# @param manage_package
# if true, manage the rsync package
# @param puts
# create rsync::puts defined type resources
# @param gets
# create rsync::gets defined type resources
#
class rsync (
$package_ensure = 'installed',
$manage_package = true,
$puts = {},
$gets = {},
String $package_ensure = 'installed',
Boolean $manage_package = true,
Hash $puts = {},
Hash $gets = {},
) {
if $manage_package {
package { 'rsync':
Expand Down
Loading