forked from v3io/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-demos.sh
190 lines (165 loc) · 5.78 KB
/
update-demos.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
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
#!/usr/bin/env bash
set -o errexit
set -o pipefail
SCRIPT="$(basename $0)"
product="Iguazio Data Science Platform"
git_owner=mlrun
git_repo=demos
git_base_url="https://github.com/${git_owner}/${git_repo}"
git_url="${git_base_url}.git"
user=${V3IO_USERNAME}
USAGE="\
$SCRIPT:
Retrieves updated demos from the mlrun/demos GitHub repository.
USAGE: ${SCRIPT} [OPTIONS]
OPTIONS:
-h|--help - Display this message and exit.
-b|--branch - Git branch name. Default: The latest release branch that
matches the version of the installed 'mlrun' package.
-u|--user - Username, which determines the directory to which to copy the
retrieved demo files (/v3io/users/<username>).
Default: \$V3IO_USERNAME, if set to a non-empty string.
--mlrun-ver - The MLRun version for which to get demos; determines the Git
branch from which to get the demos, unless -b|--branch is set.
Default: The version of the installed 'mlrun' package.
--dry-run - Show files to update but don't execute the update.
--no-backup - Don't back up the existing demos directory before the update.
Default: Back up the existing demos directory to a
/v3io/users/<username>/demos.old/<timestamp>/ directory."
error_exit()
{
# ----------------------------------------------------------------
# Function for exit due to fatal program error
# Accepts 1 argument:
# string containing descriptive error message
# ----------------------------------------------------------------
echo "${SCRIPT}: ${1:-"Unknown Error"}" 1>&2
exit 1
}
error_usage()
{
echo "${SCRIPT}: ${1:-"Unknown Error"}" 1>&2
echo -e "$USAGE"
exit 1
}
while :
do
case $1 in
-h | --help) echo -e "$USAGE"; exit 0 ;;
-b|--branch)
if [ "$2" ]; then
branch=$2
shift
else
error_usage "$1: Missing branch name."
fi
;;
--branch=?*)
branch=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--branch=) # Handle the case of an empty --branch=
error_usage "$1: Missing branch name."
;;
-u|--user)
if [ "$2" ]; then
user=$2
shift
else
error_usage "$1: Missing username."
fi
;;
--user=?*)
user=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--user=) # Handle the case of an empty --user=
error_usage "$1: Missing username."
;;
--mlrun-ver)
if [ "$2" ]; then
mlrun_version=$2
shift
else
error_usage "$1: Missing MLRun version."
fi
;;
--mlrun-ver=?*)
mlrun_version=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--mlrun-ver=) # Handle the case of an empty --mlrun-ver=
error_usage "$1: Missing MLRun version."
;;
--dry-run)
dry_run=1
;;
--no-backup)
no_backup=1
;;
-*) error_usage "$1: Unknown option."
;;
*) break;
esac
shift
done
if [ -z "${user}" ]; then
error_usage "Missing username."
fi
if [ ! -z "${dry_run}" ]; then
echo "Dry run; no files will be copied."
fi
if [ ! -z "${no_backup}" ]; then
echo "The existing demos directory won't be backed up before the update."
fi
if [ -z "${branch}" ]; then
if [ -z "${mlrun_version}" ]; then
pip_mlrun=$(pip show mlrun | grep Version) || :
if [ -z "${pip_mlrun}" ]; then
error_exit "MLRun version not found. Aborting..."
else
echo "Detected MLRun version: ${pip_mlrun}"
mlrun_version="${pip_mlrun##Version: }"
fi
else
echo "Looking for demos for the specified MLRun version - ${mlrun_version}."
fi
tag_prefix=`echo ${mlrun_version} | cut -d . -f1-2`
latest_tag=`git ls-remote --tags --refs --sort='v:refname' "${git_url}" "refs/tags/v${tag_prefix}.*" | tail -n1 | awk '{ print $2}'`
if [ -z "${latest_tag}" ]; then
error_exit "Couldn't locate a Git tag with prefix 'v${tag_prefix}.*'. Aborting..."
else
# Remove the prefix from the Git tag
branch=${latest_tag#refs/tags/}
echo "Detected ${git_url} tag: ${branch}"
fi
fi
dest_dir="/v3io/users/${user}"
demos_dir="${dest_dir}/demos"
echo "Updating demos from ${git_url} branch ${branch} to '${demos_dir}'..."
temp_dir=$(mktemp -d /tmp/temp-get-demos.XXXXXXXXXX)
trap "{ rm -rf $temp_dir; }" EXIT
echo "Copying files to a temporary directory '${temp_dir}'..."
tar_url="${git_base_url}/archive/${branch}.tar.gz"
wget -qO- "${tar_url}" | tar xz -C "${temp_dir}" --strip-components 1
if [ -z "${dry_run}" ]; then
if [ -d "${demos_dir}" ]; then
if [ -z "${no_backup}" ]; then
# Back up the existing demos directory
dt=$(date '+%Y%m%d%H%M%S');
old_demos_dir="${dest_dir}/demos.old/${dt}"
echo "Moving existing '${demos_dir}' to ${old_demos_dir}'..."
mkdir -p "${old_demos_dir}"
cp -r "${demos_dir}/." "${old_demos_dir}" && rm -rf "${demos_dir}"
else
rm -rf "${demos_dir}"
fi
fi
echo "Copying files to '${demos_dir}'..."
mkdir -p "${demos_dir}"
cp -RT "${temp_dir}" "${demos_dir}"
else
# Dry run
echo "Identified the following files to copy to '${dest_dir}':"
find "${temp_dir}/" -not -path '*/\.*' -type f -printf "%p\n" | sed -e "s|^${temp_dir}/|./demos/|"
fi
echo "Deleting temporary directory '${temp_dir}..."
rm -rf "${temp_dir}"
echo "DONE"