-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbundle.sh
executable file
·94 lines (75 loc) · 2.39 KB
/
bundle.sh
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
#!/usr/bin/env bash
set -euo pipefail
usage="Usage: $0 <path_to_resource_bundle_directory> <output_archive.tar.gz>"
function err() { cat <<< "$@" 1>&2; }
function fatal() { cat <<< "$@" 1>&2; err "$usage"; exit 1; }
function abspath() { readlink -f "$1"; }
function bundle() { tar -hczvf "$1" "$2"; }
function check() {
# Checks command-line usage for required positional arguments.
# @INPUT $1 = Path on filesytem to create a tarball
# @INPUT $2 = Name of of output tarballl to create
# @CALLS fatal() with incorrect usage
die=false
# Path to archive
if [ -z "${1:-}" ]; then
die=true
err "Error: Failed to provide directory to archive."
fi
# Output tarball name
if [ -z "${2:-}" ]; then
die=true
err "Error: Failed to output file name for archive."
fi
if $die; then
fatal "Fatal: Please try again after providing the required arguments!"
fi
}
function chunk() {
# Splits resulting tarball into N 15MB chunks
# @INPUT $1 = Name of of output tarballl
# @CALLS fatal() if provided a non-supported archive
# Strip archive file extension,
# common tarball extensions: .tar.gz or .tgz
prefix=''
if [ -f $1 ] ; then
case $1 in
*.tar.gz) prefix="${1%.tar.gz}" ;;
*.tgz) prefix="${1%.tgz}" ;;
esac
else
fatal "'$1' is not supported file type"
fi
# Spilt file into N 15MB chunk files
split --numeric-suffixes=1 -b 15M "$1" "${prefix}_chunk-"
# Calculate MD5 of all the chunks
md5sum "${prefix}_chunk-"* > "${prefix}_chunks.md5"
}
function main() {
# Checks for required positional
# command line arguments
check "${1:-}" "${2:-}"
# Converts any relative paths to
# absolute paths, creates uninit
# output directories as needed,
# runs tar command in parent dir
# of the provided resource bundle
# path.
archive_dir=$(abspath "$1")
archive=$(basename "${archive_dir%/}")
parent_dir=$(dirname "$archive_dir")
output_dir=$(dirname "$2")
mkdir -p "$output_dir"
output_dir=$(abspath "$output_dir")
cd "$parent_dir"
# Create archive as a tarball
echo "Creating tarball... $2"
bundle "$2" "$archive"
# Splitting tarball into N
# chunks for fast parallel
# downloading of large
# resource bundles
echo "Chunking tarball... $2"
chunk "$2"
}
main "$@"