1
+ #! /bin/bash
2
+
3
+ # This library holds utility functions for archiving
4
+ # built binaries and releases.
5
+
6
+ function os::build::archive::name() {
7
+ echo " ${OS_RELEASE_ARCHIVE} -${OS_GIT_VERSION} -$1 " | tr ' +' ' -'
8
+ }
9
+ readonly -f os::build::archive::name
10
+
11
+ function os::build::archive::zip() {
12
+ local default_name
13
+ default_name=" $( os::build::archive::name " ${platform} " ) .zip"
14
+ local archive_name=" ${archive_name:- $default_name } "
15
+ echo " ++ Creating ${archive_name} "
16
+ for file in " $@ " ; do
17
+ pushd " ${release_binpath} " & > /dev/null
18
+ sha256sum " ${file} "
19
+ popd & > /dev/null
20
+ zip " ${OS_OUTPUT_RELEASEPATH} /${archive_name} " -qj " ${release_binpath} /${file} "
21
+ done
22
+ }
23
+ readonly -f os::build::archive::zip
24
+
25
+ function os::build::archive::tar() {
26
+ local base_name
27
+ base_name=" $( os::build::archive::name " ${platform} " ) "
28
+ local default_name=" ${base_name} .tar.gz"
29
+ local archive_name=" ${archive_name:- $default_name } "
30
+ echo " ++ Creating ${archive_name} "
31
+ pushd " ${release_binpath} " & > /dev/null
32
+ find . -type f -exec sha256sum {} \;
33
+ if [[ -n " $( which bsdtar) " ]]; then
34
+ bsdtar -czf " ${OS_OUTPUT_RELEASEPATH} /${archive_name} " -s " ,^\.,${base_name} ," $@
35
+ else
36
+ tar -czf --xattrs-exclude=' LIBARCHIVE.xattr.security.selinux' " ${OS_OUTPUT_RELEASEPATH} /${archive_name} " --transform=" s,^\.,${base_name} ," $@
37
+ fi
38
+ popd & > /dev/null
39
+ }
40
+ readonly -f os::build::archive::tar
41
+
42
+ # Checks if the filesystem on a partition that the provided path points to is
43
+ # supporting hard links.
44
+ #
45
+ # Input:
46
+ # $1 - the path where the hardlinks support test will be done.
47
+ # Returns:
48
+ # 0 - if hardlinks are supported
49
+ # non-zero - if hardlinks aren't supported
50
+ function os::build::archive::internal::is_hardlink_supported() {
51
+ local path=" $1 "
52
+ # Determine if FS supports hard links
53
+ local temp_file=$( TMPDIR=" ${path} " mktemp)
54
+ ln " ${temp_file} " " ${temp_file} .link" & > /dev/null && unlink " ${temp_file} .link" || local supported=$?
55
+ rm -f " ${temp_file} "
56
+ return ${supported:- 0}
57
+ }
58
+ readonly -f os::build::archive::internal::is_hardlink_supported
59
+
60
+ # Extract a tar.gz compressed archive in a given directory. If the
61
+ # archive contains hardlinks and the underlying filesystem is not
62
+ # supporting hardlinks then the a hard dereference will be done.
63
+ #
64
+ # Input:
65
+ # $1 - path to archive file
66
+ # $2 - directory where the archive will be extracted
67
+ function os::build::archive::extract_tar() {
68
+ local archive_file=" $1 "
69
+ local change_dir=" $2 "
70
+
71
+ if [[ -z " ${archive_file} " ]]; then
72
+ return 0
73
+ fi
74
+
75
+ local tar_flags=" --strip-components=1"
76
+
77
+ # Unpack archive
78
+ echo " ++ Extracting $( basename ${archive_file} ) "
79
+ if [[ " ${archive_file} " == * .zip ]]; then
80
+ unzip -o " ${archive_file} " -d " ${change_dir} "
81
+ return 0
82
+ fi
83
+ if os::build::archive::internal::is_hardlink_supported " ${change_dir} " ; then
84
+ # Ensure that tar won't try to set an owner when extracting to an
85
+ # nfs mount. Setting ownership on an nfs mount is likely to fail
86
+ # even for root.
87
+ local mount_type=$( df -P -T " ${change_dir} " | tail -n +2 | awk ' {print $2}' )
88
+ if [[ " ${mount_type} " = " nfs" ]]; then
89
+ tar_flags=" ${tar_flags} --no-same-owner"
90
+ fi
91
+ tar mxzf " ${archive_file} " ${tar_flags} -C " ${change_dir} "
92
+ else
93
+ local temp_dir=$( TMPDIR=/dev/shm/ mktemp -d)
94
+ tar mxzf " ${archive_file} " ${tar_flags} -C " ${temp_dir} "
95
+ pushd " ${temp_dir} " & > /dev/null
96
+ tar cO --hard-dereference * | tar xf - -C " ${change_dir} "
97
+ popd & > /dev/null
98
+ rm -rf " ${temp_dir} "
99
+ fi
100
+ }
101
+ readonly -f os::build::archive::extract_tar
102
+
103
+ # os::build::archive::detect_local_release_tars verifies there is only one primary and one
104
+ # image binaries release tar in OS_OUTPUT_RELEASEPATH for the given platform specified by
105
+ # argument 1, exiting if more than one of either is found.
106
+ #
107
+ # If the tars are discovered, their full paths are exported to the following env vars:
108
+ #
109
+ # OS_PRIMARY_RELEASE_TAR
110
+ # OS_IMAGE_RELEASE_TAR
111
+ function os::build::archive::detect_local_release_tars() {
112
+ local platform=" $1 "
113
+
114
+ if [[ ! -d " ${OS_OUTPUT_RELEASEPATH} " ]]; then
115
+ echo " There are no release artifacts in ${OS_OUTPUT_RELEASEPATH} "
116
+ return 2
117
+ fi
118
+ if [[ ! -f " ${OS_OUTPUT_RELEASEPATH} /.commit" ]]; then
119
+ echo " There is no release .commit identifier ${OS_OUTPUT_RELEASEPATH} "
120
+ return 2
121
+ fi
122
+ local primary=$( find ${OS_OUTPUT_RELEASEPATH} -maxdepth 1 -type f -name openshift-origin-server-* -${platform} * \( -name * .tar.gz -or -name * .zip \) )
123
+ if [[ $( echo " ${primary} " | wc -l) -ne 1 || -z " ${primary} " ]]; then
124
+ echo " There should be exactly one ${platform} server tar in $OS_OUTPUT_RELEASEPATH "
125
+ [[ -z " ${WARN-} " ]] && return 2
126
+ fi
127
+
128
+ local client=$( find ${OS_OUTPUT_RELEASEPATH} -maxdepth 1 -type f -name openshift-origin-client-tools-* -${platform} * \( -name * .tar.gz -or -name * .zip \) )
129
+ if [[ $( echo " ${client} " | wc -l) -ne 1 || -z " ${client} " ]]; then
130
+ echo " There should be exactly one ${platform} client tar in $OS_OUTPUT_RELEASEPATH "
131
+ [[ -n " ${WARN-} " ]] || return 2
132
+ fi
133
+
134
+ local image=$( find ${OS_OUTPUT_RELEASEPATH} -maxdepth 1 -type f -name openshift-origin-image* -${platform} * \( -name * .tar.gz -or -name * .zip \) )
135
+ if [[ $( echo " ${image} " | wc -l) -ne 1 || -z " ${image} " ]]; then
136
+ echo " There should be exactly one ${platform} image tar in $OS_OUTPUT_RELEASEPATH "
137
+ [[ -n " ${WARN-} " ]] || return 2
138
+ fi
139
+
140
+ export OS_PRIMARY_RELEASE_TAR=" ${primary} "
141
+ export OS_IMAGE_RELEASE_TAR=" ${image} "
142
+ export OS_CLIENT_RELEASE_TAR=" ${client} "
143
+ export OS_RELEASE_COMMIT=" $( cat ${OS_OUTPUT_RELEASEPATH} /.commit) "
144
+ }
145
+ readonly -f os::build::archive::detect_local_release_tars
0 commit comments