ClickHouse/utils/clickhouse-docker

58 lines
1.9 KiB
Plaintext
Raw Permalink Normal View History

2020-04-09 13:16:56 +00:00
#!/bin/bash
if [ $# -lt 1 ]
then
cat << HELP
clickhouse-docker -- open clickhouse-client of desired version in docker container (automatically removed after you exit bash shell).
EXAMPLE:
- start latest version:
clickhouse-docker latest
- start version 20.1:
clickhouse-docker 20.1
2020-08-08 01:21:04 +00:00
- list available versions:
2020-04-09 13:16:56 +00:00
clickhouse-docker list
HELP
exit
fi
param="$1"
if [ "${param}" = "list" ]
then
# https://stackoverflow.com/a/39454426/1555175
2021-09-19 22:50:09 +00:00
wget -nv https://registry.hub.docker.com/v1/repositories/clickhouse/clickhouse-server/tags -O - | sed -e 's/[][]//g' -e 's/"//g' -e 's/ //g' | tr '}' '\n' | awk -F: '{print $3}'
2020-04-09 13:16:56 +00:00
else
2021-09-19 22:52:21 +00:00
docker pull clickhouse/clickhouse-server:${param}
2020-04-09 13:16:56 +00:00
tmp_dir=$(mktemp -d -t ci-XXXXXXXXXX) # older version require /nonexistent folder to exist to run clickhouse client :D
chmod 777 ${tmp_dir}
set -e
2021-09-19 22:52:21 +00:00
containerid=`docker run -v${tmp_dir}:/nonexistent -d clickhouse/clickhouse-server:${param}`
2020-04-09 13:16:56 +00:00
set +e
while :
do
# that trick with init-file allows to start clickhouse client inside bash shell (nice if you need exit to bash, check smth, and get back to clickhouse-client)
docker exec -it ${containerid} bash -c 'bash --init-file <(echo "clickhouse client -m")'
printf "\n\nYou exited the session. What next?\n"
echo " [Q]uit and remove container."
echo " [R]estart clickhouse and run clickhouse-client in shell again."
echo "You can also hit Ctrl+C to exit and keep container running."
while :
do
read -p "Quit or restart [Q/R]?" choice
case "$choice" in
q|Q|exit ) break 2;;
r|R|restart ) echo "Restarting container ..."; docker restart ${containerid} > /dev/null; break 1;;
* ) echo "I don't understand. Please type Q or R" ;;
esac
done
done
docker rm -f ${containerid} > /dev/null
rm -rf ${tmp_dir}
fi