-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpublish.sh
executable file
·121 lines (103 loc) · 3.85 KB
/
publish.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
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
#!/usr/bin/env bash
shopt -s extglob
set -o errtrace
set +o noclobber
export NOOP=
ASSUMEYES=0
VERBOSE=1
DEST=$1
BASE_URL=$2
case $OSTYPE in
linux-gnu)
MD5=md5sum
;;
darwin*)
MD5=md5
;;
esac
# Pre-requisites {{{
function install-prereq() # {{{2
{
case $OSTYPE in
linux-gnu)
if [[ -n $(dpkg -s bar | grep '^Status: install ok installed') ]]; then
$NOOP sudo apt-get install bar
fi
if [[ -n $(dpkg -s jq | grep '^Status: install ok installed') ]]; then
$NOOP sudo apt-get install jq
fi
;;
darwin*)
if [[ -n $(brew info bar | grep '^Not installed$') ]]; then
$NOOP brew install bar
fi
if [[ -n $(brew info jq | grep '^Not installed$') ]]; then
$NOOP brew install jq
fi
;;
esac
} # }}}2
# }}}
function main() # {{{2
{
install-prereq
for box_path in boxes/* ; do
box=${box_path##*/}
echo "Processing box ${box}"
if [[ -f "$DEST/$box/metadata.json" && "$DEST/$box/metadata.json" -nt "$box_path/metadata.json" ]]; then
echo " Downloading Metadata"
cp -f "$DEST/$box/metadata.json" "$box_path"
chmod 644 "$box_path/metadata.json"
fi
metadata=$(cat "$box_path/metadata.json")
for provider_path in $box_path/* ; do
[[ ! -d "$provider_path" ]] && continue
provider=${provider_path##*/}
echo " Processing provider ${provider}"
for box_filepath in $provider_path/*.box ; do
box_file=${box_filepath##*/}
echo " Box file: ${box_file}"
if [[ ! -f "${box_filepath}.md5" || "${box_filepath}.md5" -ot "${box_filepath}" ]]; then
printf " Calculating checksum..."
box_checksum=$(bar -n "${box_filepath}" | $MD5 | awk '{print $1}')
echo $box_checksum > "${box_filepath}.md5"
echo '.'
else
box_checksum=$(cat "${box_filepath}.md5")
fi
echo " Checksum: ${box_checksum}"
metadata_version=$(echo "$metadata" | jq '.versions[] | select(.version=="0.1.0")')
if [[ -z "$metadata_version" ]]; then
echo " ERROR: Cannot find the box version in the metadata"
continue
fi
metadata_provider=$(echo "$metadata_version" | jq ".providers[] | select(.name==\"$provider\")")
if [[ -z "$metadata_provider" ]]; then
echo " Adding provider"
echo " Copying box file"
mkdir -p "$DEST/$box/$provider"
bar -o "$DEST/$box/$provider/$box_file" "$box_filepath"
#cp "$box_filepath" "$DEST/$box/$provider/$box_file"
echo "$metadata" | jq "(.versions[] | select(.version==\"0.1.0\") | .providers) |= . + [{name: \"$provider\", url: \"$BASE_URL/$box/$provider/$box_file\", checksum_type: \"md5\", checksum: \"$box_checksum\"}]" > "$box_path/metadata.$$.json" && mv "$box_path/metadata.$$.json" "$box_path/metadata.json"
cp "$box_path/metadata.json" "$DEST/$box/metadata.json"
continue
fi
metadata_checksum=$(echo "$metadata_provider" | jq --raw-output '.checksum')
echo " Destination Checksum: ${metadata_checksum}"
if [[ $box_checksum == $metadata_checksum ]]; then
echo " Destination box is already uploaded"
continue
else
echo " Copying box file"
bar -o "$DEST/$box/$provider/$box_file" "$box_filepath"
#cp "$box_filepath" "$DEST/$box/$provider/$box_file"
chmod 644 "$DEST/$box/$provider/$box_file"
echo "$metadata" | jq "(.versions[] | select(.version==\"0.1.0\") | .providers[] | select(.name==\"$provider\") | .checksum) |= \"$box_checksum\"" > "$box_path/metadata.$$.json" && mv "$box_path/metadata.$$.json" "$box_path/metadata.json"
cp "$box_path/metadata.json" "$DEST/$box/metadata.json"
chmod 644 "$box_path/metadata.json"
fi
done
done
done
} # }}}2
main $@