-
Notifications
You must be signed in to change notification settings - Fork 2
/
s3storage.cmake
executable file
·278 lines (256 loc) · 8.95 KB
/
s3storage.cmake
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#
# s3storage.cmake
#
# Copyright (c) 2019 - 2022 Marius Zwicker
# All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##################################################
#
# BUILD/S3STORAGE.CMAKE
#
# Provides macros to upload or download files from an s3 compatible storage
#
# PROVIDED FUNCTIONS
# -----------------------
# mz_s3_download BUCKET FILE <object path> DESTINATION <local file path> (QUIET)
# Download file from the bucket path and store it at destination
# Specify QUIET to supress errors about missing objects or configuration
#
# Requires the following variables to be set in the env or as
# CMake variables:
# MZ_S3_ALIAS Alias by which the S3 server was registered with mc
# MZ_S3_CONFIG_DIR Alternative configuration directory to use with mc
#
# mz_s3_download BUCKET DIRECTORY <object path> DESTINATION <local directory path> (QUIET)
# Download directory from the bucket path and store it at destination
# Specify QUIET to supress errors about missing objects or configuration
#
# Requires the following variables to be set in the env or as
# CMake variables:
# MZ_S3_ALIAS Alias by which the S3 server was registered with mc
# MZ_S3_CONFIG_DIR Alternative configuration directory to use with mc
#
# mz_s3_upload BUCKET FILE <local file path> DESTINATION <object path> (PUBLIC) (QUIET)
# Upload file from the source path to bucket path enabling public
# access when adding the PUBLIC option
# Specify QUIET to supress errors about missing objects or configuration
#
# Requires the following variables to be set in the env or as
# CMake variables:
# MZ_S3_ALIAS Alias by which the S3 server was registered with mc
# MZ_S3_CONFIG_DIR Alternative configuration directory to use with mc
#
# mz_s3_upload BUCKET DIRECTORY <local directory path> DESTINATION <object path> (QUIET)
# Upload directory from the source path to bucket path
# Specify QUIET to supress errors about missing objects or configuration
#
# Requires the following variables to be set in the env or as
# CMake variables:
# MZ_S3_ALIAS Alias by which the S3 server was registered with mc
# MZ_S3_CONFIG_DIR Alternative configuration directory to use with mc
#
# PROVIDED CMAKE VARIABLES
# -----------------------
# MZ_S3_INCLUDE_PATH Path to the s3storage.cmake
#
########################################################################
mz_include_guard(GLOBAL)
macro(mz_s3_message MSG)
message("-- S3: ${MSG}")
endmacro()
macro(mz_s3_warning MSG)
message(WARNING "!! S3: ${MSG}")
endmacro()
macro(mz_s3_error MSG)
message(FATAL_ERROR "!! S3: ${MSG}")
endmacro()
# BOF: s3storage.cmake
if(NOT HAS_MZ_S3)
set(HAS_MZ_S3 true)
if(NOT MZ_S3_MC)
find_host_program(MZ_S3_MC NAMES minio-mc minio-mc.exe mc)
endif()
set(MZ_S3_INCLUDE_PATH ${CMAKE_CURRENT_LIST_FILE})
# EOF: s3storage.cmake
endif()
macro(_mz_s3_env QUIET )
if(NOT MZ_S3_MC)
if(NOT ${QUIET})
mz_s3_error("Missing minio's 'mc', cannot continue")
endif()
unset(_mz_s3_url)
return()
endif()
if(NOT MZ_S3_ALIAS)
set(MZ_S3_ALIAS $ENV{MZ_S3_ALIAS})
endif()
if( NOT ${QUIET} AND NOT MZ_S3_ALIAS )
mz_s3_error("MZ_S3_ALIAS not defined, cannot continue")
endif()
set(_mz_s3_mc "${MZ_S3_MC}")
if(NOT MZ_S3_CONFIG_DIR)
set(MZ_S3_CONFIG_DIR $ENV{MZ_S3_CONFIG_DIR})
endif()
if(MZ_S3_CONFIG_DIR)
set(_mz_s3_mc ${_mz_s3_mc} --config-dir ${MZ_S3_CONFIG_DIR})
endif()
execute_process(
COMMAND ${_mz_s3_mc} stat ${MZ_S3_ALIAS} --debug
ERROR_VARIABLE _mz_s3_alias_output
OUTPUT_QUIET
RESULT_VARIABLE _mz_s3_result
)
if( _mz_s3_result GREATER "0")
if(NOT ${QUIET})
mz_s3_error("'${MZ_S3_ALIAS}' was not registered with mc.\nTo register use\n\t${MZ_S3_MC} alias set ${MZ_S3_ALIAS} <url> <access_key> <secret_key>\n")
endif()
unset(_mz_s3_url)
else()
string(REGEX MATCH "Host: ([^\n\r]+)" _mz_s3_url ${_mz_s3_alias_output})
set(_mz_s3_url ${CMAKE_MATCH_1})
set(_mz_s3_archive_ext .zip)
mz_s3_message("Using alias '${MZ_S3_ALIAS}' for '${_mz_s3_url}'")
endif()
endmacro()
function(mz_s3_download BUCKET)
# see https://cmake.org/cmake/help/v3.18/command/cmake_parse_arguments.html#command:cmake_parse_arguments
set(_mz_s3_options
QUIET
)
set(_mz_s3_oneValueArgs
FILE
DIRECTORY
DESTINATION
)
set(_mz_s3_multiValueArgs
)
cmake_parse_arguments( _mz_s3
"${_mz_s3_options}"
"${_mz_s3_oneValueArgs}"
"${_mz_s3_multiValueArgs}"
${ARGN}
)
if(_mz_s3_UNPARSED_ARGUMENTS)
mz_s3_error("No such option: ${_mz_s3_UNPARSED_ARGUMENTS}")
endif()
_mz_s3_env(_mz_s3_QUIET)
if( _mz_s3_QUIET AND NOT _mz_s3_url )
mz_s3_message("Skipping download, mc not configured")
return()
endif()
if(_mz_s3_DIRECTORY)
set(_mz_s3_source ${_mz_s3_DIRECTORY}${_mz_s3_archive_ext})
set(_mz_s3_extract_to ${_mz_s3_DESTINATION})
set(_mz_s3_DESTINATION ${_mz_s3_DESTINATION}${_mz_s3_archive_ext})
mz_s3_message("Downloading directory from ${MZ_S3_ALIAS}/${BUCKET}/${_mz_s3_source}")
else()
set(_mz_s3_source ${_mz_s3_FILE})
mz_s3_message("Downloading file from ${MZ_S3_ALIAS}/${BUCKET}/${_mz_s3_source}")
endif()
if(_mz_s3_QUIET)
set(_mz_s3_quiet OUTPUT_QUIET ERROR_QUIET)
else()
set(_mz_s3_quiet)
endif()
execute_process(
COMMAND ${_mz_s3_mc} cp ${MZ_S3_ALIAS}/${BUCKET}/${_mz_s3_source} ${_mz_s3_DESTINATION}
RESULT_VARIABLE _mz_s3_result
${_mz_s3_quiet}
)
if( _mz_s3_result GREATER "0")
if(NOT _mz_s3_QUIET)
mz_s3_error("Download failed")
endif()
return()
endif()
if( _mz_s3_extract_to )
file(ARCHIVE_EXTRACT INPUT ${_mz_s3_DESTINATION}
DESTINATION ${_mz_s3_extract_to}
)
file(REMOVE ${_mz_s3_DESTINATION})
endif()
endfunction()
function(mz_s3_upload BUCKET)
# see https://cmake.org/cmake/help/v3.18/command/cmake_parse_arguments.html#command:cmake_parse_arguments
set(_mz_s3_options
PUBLIC
QUIET
)
set(_mz_s3_oneValueArgs
FILE
DIRECTORY
DESTINATION
)
set(_mz_s3_multiValueArgs
)
cmake_parse_arguments( _mz_s3
"${_mz_s3_options}"
"${_mz_s3_oneValueArgs}"
"${_mz_s3_multiValueArgs}"
${ARGN}
)
if(_mz_s3_UNPARSED_ARGUMENTS)
mz_s3_error("No such option: ${_mz_s3_UNPARSED_ARGUMENTS}")
endif()
_mz_s3_env(_mz_s3_QUIET)
if( _mz_s3_QUIET AND NOT _mz_s3_url )
mz_s3_message("Skipping upload, mc not configured")
return()
endif()
if(_mz_s3_DIRECTORY)
set(_mz_s3_source ${_mz_s3_DIRECTORY}${_mz_s3_archive_ext})
set(_mz_s3_DESTINATION ${_mz_s3_DESTINATION}${_mz_s3_archive_ext})
mz_s3_message("Uploading directory to ${MZ_S3_ALIAS}/${BUCKET}/${_mz_s3_DESTINATION}")
file(GLOB _mz_s3_ARCHIVE_CONTENTS
LIST_DIRECTORIES true
RELATIVE ${_mz_s3_DIRECTORY}
${_mz_s3_DIRECTORY}/*
)
# ARCHIVE_CREATE does not allow us to specify a working directory
execute_process(
COMMAND ${CMAKE_COMMAND} -E tar cf ${_mz_s3_source} --format=zip -- ${_mz_s3_ARCHIVE_CONTENTS}
WORKING_DIRECTORY ${_mz_s3_DIRECTORY}
)
else()
set(_mz_s3_source ${_mz_s3_FILE})
mz_s3_message("Uploading file to ${MZ_S3_ALIAS}/${BUCKET}/${_mz_s3_DESTINATION}")
endif()
if(_mz_s3_QUIET)
set(_mz_s3_quiet OUTPUT_QUIET ERROR_QUIET)
else()
set(_mz_s3_quiet)
endif()
execute_process(
COMMAND ${_mz_s3_mc} cp ${_mz_s3_source} ${MZ_S3_ALIAS}/${BUCKET}/${_mz_s3_DESTINATION}
${_mz_s3_quiet}
)
if(_mz_s3_DIRECTORY)
file(REMOVE ${_mz_s3_source})
endif()
if( _mz_s3_result GREATER "0")
if(NOT _mz_s3_QUIET)
mz_s3_error("Upload failed")
endif()
return()
endif()
if(_mz_s3_PUBLIC)
mz_s3_message("Public access via ${_mz_s3_url}/${BUCKET}/${_mz_s3_DESTINATION}")
execute_process(
COMMAND ${_mz_s3_mc} policy set download ${MZ_S3_ALIAS}/${BUCKET}/${_mz_s3_DESTINATION}
)
endif()
endfunction()