Commit fd53ec0f authored by Vesa Vertainen's avatar Vesa Vertainen

setup-private-registry.sh now lists tools with OS information, and asks for docker username

parent 2a49a521
#!/bin/bash
# TODO: list only linux images?
# Variable definitions
CHECK="\e[32m[+]\e[0m"
WARNING="\e[33m[!]\e[0m"
PRIVATE_REGISTRY=172.20.0.6
# Whiptail colors
......@@ -35,40 +32,52 @@ fi
echo -e "$CHECK Cloning the tools repository"
if [ ! -d "tools" ];then
git clone https://gitlab.com/CinCan/tools.git
cd tools
cd tools || exit
else
cd tools
# git pull
cd tools || exit
git pull
fi
# User menu
# Create options
# Create image selection options
TOOLS_LIST=$(ls -d -- */)
TOOLS_COUNT=$(echo $TOOLS_LIST |grep -o "/" |wc -l)
TOOLS_COUNT=$(echo "$TOOLS_LIST" |grep -o "/" |wc -l)
i=1
while
i=1; while
OPTION="$(echo $TOOLS_LIST |cut -d " " -f $i)"
OPTION="${OPTION::-1}"
OPTIONS="${OPTIONS} ${OPTION} ${OPTION} OFF"
if [ -f "$OPTION/Dockerfile" ]; then
if cat "$OPTION/Dockerfile" |head -n 10 |grep -q microsoft; then
OS="Windows"
else
OS="Linux"
fi
OPTIONS="${OPTIONS} ${OPTION} ${OS} OFF"
fi
i=$((i+1))
[ $i -lt $((TOOLS_COUNT+1)) ]
do :;done
# Menu to select images
function selectImages() {
TOOLS_TO_INSTALL=$(whiptail --title "Cincan private registry setup" --checklist --notags --fb --backtitle \
TOOLS_TO_INSTALL=$(whiptail --title "Cincan private registry setup" --checklist --fb --backtitle \
" CinCan - Continuous Integration for the Collaborative Analysis of Incidents " \
"Choose the images to install. Make sure you have the private registry \
set up, and that you are logged in. There are $TOOLS_COUNT images available." \
30 80 20 $OPTIONS 3>&1 1>&2 2>&3)
if [ ! "$TOOLS_TO_INSTALL" ]; then echo "Cancelled"; exit; fi
TOOLS_TO_INSTALL="${TOOLS_TO_INSTALL//\"}"
}
# Menu for selecting installation method
function installationMethod() {
INSTALLATION_METHOD=$(whiptail --title "Cincan private registry setup" --fb --menu "Choose installation method" --backtitle \
......@@ -90,12 +99,11 @@ function installationMethod() {
;;
3)
# Set previous selections to be selected also when returning to menu
i=1; while
TOOL_NUMBER="\$"$i
TOOL_ON=$(echo "${TOOLS_TO_INSTALL//\"}" |awk "{print $TOOL_NUMBER}")
OPTIONS=$(echo $OPTIONS | sed "s|$TOOL_ON $TOOL_ON OFF|$TOOL_ON $TOOL_ON ON|")
TOOL_ON=$(echo "$TOOLS_TO_INSTALL" |awk "{print $TOOL_NUMBER}")
TOOL_OS=$(echo "$OPTIONS" |grep -Po -- "$TOOL_ON \K\w*")
OPTIONS="${OPTIONS//$TOOL_ON $TOOL_OS OFF/$TOOL_ON $TOOL_OS ON}"
i=$((i+1))
[ $i -lt $((TOOLS_COUNT+2)) ]
do :;done
......@@ -106,19 +114,27 @@ function installationMethod() {
esac
}
# Ask for Docker username (because root is not necessarily the Docker user)
function getUsername() {
echo -e "Type in your Docker username if not root [root]:"
read -r DOCKER_USERNAME
if [ "$DOCKER_USERNAME" = "" ]; then DOCKER_USERNAME="root";fi
}
selectImages
installationMethod
getUsername
# Build or pull images
TOOLS_TO_INSTALL=$(echo "$TOOLS_TO_INSTALL" |sed s/\"//g)
TOOLS_COUNT_TO_INSTALL=$(echo "$TOOLS_TO_INSTALL" |wc -w)
# Build / pull images
# Build the images
TOOLS_COUNT_TO_INSTALL=$(echo "$TOOLS_TO_INSTALL" |wc -w)
if [ "$INSTALLATION_METHOD" = 1 ]; then
# Build the images
echo -e "$CHECK Building Dockerfiles..."
i=1
......@@ -128,19 +144,20 @@ if [ "$INSTALLATION_METHOD" = 1 ]; then
cd "$CURRENT_TOOL" || exit
echo "$CURRENT_TOOL" >> times
docker build . -t "$CURRENT_TOOL"
sudo su "$DOCKER_USERNAME" -c "docker build . -t cincan/$CURRENT_TOOL"
cd ..
i=$((i+1))
[ $i -lt $((TOOLS_COUNT_TO_INSTALL+1)) ]
do :;done
else
# Pull images
echo -e "$CHECK Downloading images from Docker hub"
i=1
while
CURRENT_TOOL="$(echo $TOOLS_TO_INSTALL |cut -d " " -f $i)"
CURRENT_TOOL=$(echo "$TOOLS_TO_INSTALL" |cut -d " " -f $i)
echo -e "\n$CHECK Downloading tool $i/$TOOLS_COUNT_TO_INSTALL: $CURRENT_TOOL\n"
cd "$CURRENT_TOOL" || exit
......@@ -158,19 +175,13 @@ fi
echo -e "$CHECK Pushing images to Docker private registry"
# Ask for Docker username (because root is not necessarily the Docker user)
echo -e "Type in your Docker username if not root [root]:"
read DOCKER_USERNAME
if [ "$DOCKER_USERNAME" = "" ]; then DOCKER_USERNAME="root";fi
i=1
while
CURRENT_TOOL="$(echo $TOOLS_TO_INSTALL |cut -d " " -f $i)"
CURRENT_TOOL=$(echo "$TOOLS_TO_INSTALL" |cut -d " " -f $i)
docker tag cincan/"$CURRENT_TOOL" "$PRIVATE_REGISTRY:5000/$CURRENT_TOOL"
echo -e "$CHECK tagged cincan/$CURRENT_TOOL as $PRIVATE_REGISTRY:5000/$CURRENT_TOOL"
sudo su $DOCKER_USERNAME -c "docker push $PRIVATE_REGISTRY:5000/$CURRENT_TOOL"
sudo su "$DOCKER_USERNAME" -c "docker push $PRIVATE_REGISTRY:5000/$CURRENT_TOOL"
echo -e "$CHECK pushed $CURRENT_TOOL to $PRIVATE_REGISTRY:5000/"
docker rmi -f cincan/"$CURRENT_TOOL"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment