Skip to content

Instantly share code, notes, and snippets.

@ademidoff
Created April 1, 2021 12:38
Show Gist options
  • Save ademidoff/9736ecbb26286212c78bf49be6eeea5d to your computer and use it in GitHub Desktop.
Save ademidoff/9736ecbb26286212c78bf49be6eeea5d to your computer and use it in GitHub Desktop.
PMM2 install script
#!/bin/sh
# ###############################
# Script to run PMM2
# curl -fsSL https://gist.githubusercontent.com/rnovikovP/91be10dec6bae1111cf104f85fffd5cf/raw/get-pmm2.sh -o get-pmm2.sh ; chmod +x get-pmm2.sh ; ./get-pmm2.sh
#
#################################
set -o errexit
set -o xtrace
root_is_needed='no'
check_command() {
command -v "$@" > /dev/null 2>&1
}
run_root() {
sh='sh -c'
if [ "$(id -un)" != 'root' ]; then
if check_command sudo; then
sh='sudo -E sh -c'
elif check_command su; then
sh='su -c'
else
echo ERROR: root rights needed to run "$*" command
exit 1
fi
fi
${sh} "$@"
}
install_docker() {
if ! check_command docker; then
echo Installing docker
curl -fsSL get.docker.com -o /tmp/get-docker.sh \
|| wget -qO /tmp/get-docker.sh get.docker.com
sh /tmp/get-docker.sh
run_root 'service docker start' || :
fi
if ! docker ps; then
root_is_needed='yes'
if ! run_root 'docker ps'; then
echo ERROR: cannot run "docker ps" command
exit 1
fi
fi
}
run_docker() {
if [ "${root_is_needed}" = 'yes' ]; then
run_root "docker $*"
else
sh -c "docker $*"
fi
}
start_pmm() {
run_docker pull percona/pmm-server:2
if ! run_docker inspect pmm-data >/dev/null; then
run_docker create \
-v /srv/\
--name pmm-data \
percona/pmm-server:2 /bin/true
fi
if run_docker inspect pmm-server >/dev/null; then
run_docker stop pmm-server || :
run_docker rename pmm-server "pmm-server-$(date "+%F-%H%M%S")"
fi
run_docker run -d \
-p 443:443 \
--volumes-from pmm-data \
--name pmm-server \
--restart always \
percona/pmm-server:2
}
main() {
install_docker
start_pmm
}
main
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment