]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/staging/manifests/file.pp
add nanliu/staging to 3rdparty
[dsa-puppet.git] / 3rdparty / modules / staging / manifests / file.pp
1 # #### Overview:
2 #
3 # Define resource to retrieve files to staging directories. It is
4 # intententionally not replacing files, as these intend to be large binaries
5 # that are versioned.
6 #
7 # #### Notes:
8 #
9 #   If you specify a different staging location, please manage the file
10 #   resource as necessary.
11 #
12 define staging::file (
13   $source,              #: the source file location, supports local files, puppet://, http://, https://, ftp://, s3://
14   $target      = undef, #: the target file location, if unspecified ${staging::path}/${subdir}/${name}
15   $username    = undef, #: https or ftp username
16   $certificate = undef, #: https certificate file
17   $password    = undef, #: https or ftp user password or https certificate password
18   $environment = undef, #: environment variable for settings such as http_proxy, https_proxy, of ftp_proxy
19   $timeout     = undef, #: the the time to wait for the file transfer to complete
20   $curl_option = undef, #: options to pass to curl
21   $wget_option = undef, #: options to pass to wget
22   $tries       = undef, #: amount of retries for the file transfer when non transient connection errors exist
23   $try_sleep   = undef, #: time to wait between retries for the file transfer
24   $subdir      = $caller_module_name
25 ) {
26
27   include staging
28
29   $quoted_source = shellquote($source)
30
31   if $target {
32     $target_file = $target
33     $staging_dir = staging_parse($target, 'parent')
34   } else {
35     $staging_dir = "${staging::path}/${subdir}"
36     $target_file = "${staging_dir}/${name}"
37
38     if ! defined(File[$staging_dir]) {
39       file { $staging_dir:
40         ensure=>directory,
41       }
42     }
43   }
44
45   Exec {
46     path        => $staging::exec_path,
47     environment => $environment,
48     cwd         => $staging_dir,
49     creates     => $target_file,
50     timeout     => $timeout,
51     try_sleep   => $try_sleep,
52     tries       => $tries,
53     logoutput   => on_failure,
54   }
55
56   case $::staging_http_get {
57     'curl', default: {
58       $http_get        = "curl ${curl_option} -f -L -o ${target_file} ${quoted_source}"
59       $http_get_passwd = "curl ${curl_option} -f -L -o ${target_file} -u ${username}:${password} ${quoted_source}"
60       $http_get_cert   = "curl ${curl_option} -f -L -o ${target_file} -E ${certificate}:${password} ${quoted_source}"
61       $ftp_get         = "curl ${curl_option} -o ${target_file} ${quoted_source}"
62       $ftp_get_passwd  = "curl ${curl_option} -o ${target_file} -u ${username}:${password} ${quoted_source}"
63     }
64     'wget': {
65       $http_get        = "wget ${wget_option} -O ${target_file} ${quoted_source}"
66       $http_get_passwd = "wget ${wget_option} -O ${target_file} --user=${username} --password=${password} ${quoted_source}"
67       $http_get_cert   = "wget ${wget_option} -O ${target_file} --user=${username} --certificate=${certificate} ${quoted_source}"
68       $ftp_get         = $http_get
69       $ftp_get_passwd  = $http_get_passwd
70     }
71     'powershell':{
72       $http_get           = "powershell.exe -Command \"\$wc = New-Object System.Net.WebClient;\$wc.DownloadFile('${source}','${target_file}')\""
73       $ftp_get            = $http_get
74       $http_get_password  = "powershell.exe -Command \"\$wc = (New-Object System.Net.WebClient);\$wc.Credentials = New-Object System.Net.NetworkCredential('${username}','${password}');\$wc.DownloadFile(${source},${target_file})\""
75       $ftp_get_password   = $http_get_password
76     }
77   }
78
79   case $source {
80     /^\//: {
81       file { $target_file:
82         source  => $source,
83         replace => false,
84       }
85     }
86     /^puppet:\/\//: {
87       file { $target_file:
88         source  => $source,
89         replace => false,
90       }
91     }
92     /^http:\/\//: {
93       if $username { $command = $http_get_passwd }
94       else         { $command = $http_get        }
95       exec { $target_file:
96         command   => $command,
97       }
98     }
99     /^https:\/\//: {
100       if $username       { $command = $http_get_passwd }
101       elsif $certificate { $command = $http_get_cert   }
102       else               { $command = $http_get        }
103       exec { $target_file:
104         command => $command,
105       }
106     }
107     /^ftp:\/\//: {
108       if $username       { $command = $ftp_get_passwd }
109       else               { $command = $ftp_get        }
110       exec { $target_file:
111         command     => $command,
112       }
113     }
114     /^s3:\/\//: {
115       $command = "aws s3 cp ${source} ${target_file}"
116       exec { $target_file:
117         command   => $command,
118       }
119     }
120     default: {
121       fail("staging::file: do not recognize source ${source}.")
122     }
123   }
124
125 }