-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-luarocks.sh
152 lines (133 loc) · 4.47 KB
/
create-luarocks.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
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
#!/usr/bin/env bash
set -e
CMDNAME=${0##*/}
echoerr() {
echo "ERROR: $@" 1>&2
}
usage() {
cat <<USAGE >&2
Usage:
$CMDNAME ARTIFACTORY_URL REPO USERNAME:PASSWORD [--install-any pkg1 pgk2] [--install-all pkg1 pgk2]
ARTIFACTORY_URL URL to Artifactory instance, e.g: https://repo.example.com/artifactory.
REPOSITORY Repository to store rocks and maintain index and manifest.
USERNAME:PASSWORD Credentials for user with read, write and remove permissions to this repository (split by colon).
--install-all pkg1 pgk2 (optional) Try to install all specified packages.
--install-any pkg1 pgk2 (optional) Try to install any of specified packages.
Examples:
$CMDNAME https://repo.example.com/artifactory myluarocks.snapshot deploy_user:password
$CMDNAME https://repo.example.com/artifactory myluarocks.snapshot deploy_user:password --install-any penlight rapidjson
USAGE
exit 1
}
create_luarocks() {
# Use Authorization header to authenticate against Artifactory
# https://www.jfrog.com/confluence/display/RTF/Using+WebDAV#UsingWebDAV-Authenticationfordavfs2Clients
echo "INFO: Connecting with user $LOGIN"
# Authentication setup
# Note: use $(echo -n) to remove newline character. You can also use `base64` command instead of `openssl`
BASE64_AUTH="$(echo -n $AUTH | openssl enc -base64)"
echo add_header Authorization \"Basic $BASE64_AUTH\" > ~/.davfs2/davfs2.conf
echo $ARTIFACTORY_URL/$REPOSITORY $LOGIN $PASSWORD > /etc/davfs2/secrets
# Mounting DAV storage
mkdir -p /mnt/$REPOSITORY
mount -t davfs $ARTIFACTORY_URL/$REPOSITORY /mnt/$REPOSITORY
sleep 3
cd /mnt
echo "INFO: Wait for the files to become available..."
# If the storage was successfully mounted, lost+found directory is available. In this case, we need to wait until the
# filesystems are synchronized by interacting with it. If the sync isn't happening in 30 seconds, exit with error
if [ -d "$REPOSITORY/lost+found" ]; then
TMP_FILE="$REPOSITORY/$(date +%Y%m%d%H%M%S)"
COUNTER=0
touch "$TMP_FILE"
until [ -f "$TMP_FILE.sha256" ]; do
if [ $COUNTER -gt 10 ]; then
echoerr "Something went wrong. Storage wasn't mounted properly"
exit 1
fi
sleep 3
COUNTER=$((COUNTER + 1))
echo "DEBUG: Tried $COUNTER time(s)"
done
rm -f "$TMP_FILE" "$TMP_FILE.sha256"
else
echoerr "Storage wasn't mounted - can't find lost+found directory"
exit 1
fi
luarocks-admin make-manifest $REPOSITORY
echo "DEBUG: Checking created manifests:"
ls -la /mnt/$REPOSITORY | grep -E 'manifest|index'
umount /mnt/$REPOSITORY
echo "INFO: Waiting for the repository to update..."
sleep 15
}
test_install_all() {
local packages="$1"
for package in $packages; do
echo "INFO: Trying to install '$package' package"
luarocks install --only-server=$ARTIFACTORY_URL/$REPOSITORY --deps-mode none $package
done
}
test_install_any() {
local packages="$1"
for package in $packages; do
echo "INFO: Trying to install '$package' package"
luarocks install --only-server=$ARTIFACTORY_URL/$REPOSITORY --deps-mode none $package &&
return 0 ||
continue
done
echoerr "Couldn't install any of the packages: $packages"
return 11
}
# Parse args
ARTIFACTORY_URL="$1"
REPOSITORY="$2"
AUTH="$3"
# Check for colon
if [[ -z ${AUTH##*:*} ]]; then
LOGIN="${AUTH%%:*}"
PASSWORD="${AUTH#*:}"
else
echoerr "Use username:password as authentication string"
exit 1
fi
for PARAM in "$ARTIFACTORY_URL" "$REPOSITORY" "$LOGIN" "$PASSWORD"; do
if [[ "$PARAM" == "" ]] && [[ "$1" != "--help" ]]; then
echoerr "You need to provide ARTIFACTORY_URL, REPOSITORY, and USERNAME:PASSWORD parameters"
usage
elif [[ "$1" == "--help" ]]; then
usage
fi
done
shift 3
while [[ $# -gt 0 ]]; do
case "$1" in
--install-all)
shift 1
INSTALL_ALL_PACKAGES=$@
break
;;
--install-any)
shift 1
INSTALL_ANY_PACKAGES=$@
break
;;
*)
echoerr "Unknown argument: $1"
usage
;;
esac
done
echo "INFO: Creating luarocks in $REPOSITORY..."
create_luarocks
echo "INFO: DONE"
if [[ ! -z "$INSTALL_ALL_PACKAGES" ]]; then
echo "INFO: Trying to install ALL packages '$INSTALL_ALL_PACKAGES' from $REPOSITORY..."
test_install_all "$INSTALL_ALL_PACKAGES"
echo "INFO: DONE"
fi
if [[ ! -z "$INSTALL_ANY_PACKAGES" ]]; then
echo "INFO: Trying to install ANY OF '$INSTALL_ANY_PACKAGES' packages from $REPOSITORY..."
test_install_any "$INSTALL_ANY_PACKAGES"
echo "INFO: DONE"
fi