Commit 03f88c63 authored by Vesa Vertainen's avatar Vesa Vertainen

added setup-private-registry.sh login

parent fd53ec0f
......@@ -377,11 +377,11 @@ EOF
## Create certs and credentials for private registry
echo -e "$CHECK Added certificate for Docker private registry" ${BUILD_PATH}/certs/${COMMON_NAME_REGISTRY}.crt /etc/docker/certs.d/${EXTERNAL_URL_REGISTRY}:5000/ca.crt
if [ ! -d /etc/docker/certs.d/${EXTERNAL_URL_REGISTRY}:5000 ]; then mkdir /etc/docker/certs.d/${EXTERNAL_URL_REGISTRY}:5000; fi
if [ ! -d /etc/docker/certs.d/${EXTERNAL_URL_REGISTRY}:5000 ]; then mkdir -p /etc/docker/certs.d/${EXTERNAL_URL_REGISTRY}:5000; fi
cp ${BUILD_PATH}/certs/${COMMON_NAME_REGISTRY}.crt /etc/docker/certs.d/${EXTERNAL_URL_REGISTRY}:5000/ca.crt
if [ ! -d /opt/cincan/auth ]; then mkdir /opt/cincan/auth; fi
htpasswd -Bbc /opt/cincan/auth/htpasswd ${USER_NAME} ${PASSWORD} > /dev/null 2>&1
htpasswd -Bbc /opt/cincan/auth/htpasswd ${USER_NAME} ${PASSWORD}
# Create README and inform user
......
......@@ -27,8 +27,32 @@ if [ ! "${UID}" -eq 0 ] ;then
fi
# FUNCTIONS
# Ask for Docker username (because root is not necessarily the Docker user), cincan credentials, and login to private registry
function getCredentials() {
echo -e "Type in your CinCan username [default: cincan]:"
read -r CINCAN_USERNAME
if [ "$CINCAN_USERNAME" = "" ]; then CINCAN_USERNAME="cincan";fi
echo -e "Type in your CinCan password:"
read -r PASSWORD
if [ "$PASSWORD" = "" ]; then
echo -e "Password not given"
exit 0
fi
echo -e "Type in your DOCKER username if not root [root]:"
read -r DOCKER_USERNAME
if [ "$DOCKER_USERNAME" = "" ]; then DOCKER_USERNAME="root";fi
sudo su "$DOCKER_USERNAME" -c "docker login -u $CINCAN_USERNAME -p $PASSWORD 172.20.0.6:5000" || { echo "Login failed"; exit 1; }
}
# Clone tools repository (or update if exists)
function cloneRepo() {
echo -e "$CHECK Cloning the tools repository"
if [ ! -d "tools" ];then
git clone https://gitlab.com/CinCan/tools.git
......@@ -37,34 +61,38 @@ else
cd tools || exit
git pull
fi
}
# User menu
# Create image selection options
TOOLS_LIST=$(ls -d -- */)
TOOLS_COUNT=$(echo "$TOOLS_LIST" |grep -o "/" |wc -l)
i=1; while
OPTION="$(echo $TOOLS_LIST |cut -d " " -f $i)"
OPTION="${OPTION::-1}"
function createMenuOptions() {
TOOLS_LIST=$(ls -d -- */)
TOOLS_COUNT=$(echo "$TOOLS_LIST" |grep -o "/" |wc -l)
if [ -f "$OPTION/Dockerfile" ]; then
if cat "$OPTION/Dockerfile" |head -n 10 |grep -q microsoft; then
OS="Windows"
else
OS="Linux"
fi
i=1; while
OPTION="$(echo $TOOLS_LIST |cut -d " " -f $i)"
OPTION="${OPTION::-1}"
OPTIONS="${OPTIONS} ${OPTION} ${OS} OFF"
fi
if [ -f "$OPTION/Dockerfile" ]; then
if cat "$OPTION/Dockerfile" |head -n 10 |grep -q microsoft; then
OS="Windows"
else
OS="Linux"
fi
i=$((i+1))
[ $i -lt $((TOOLS_COUNT+1)) ]
do :;done
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 --fb --backtitle \
" CinCan - Continuous Integration for the Collaborative Analysis of Incidents " \
......@@ -79,6 +107,7 @@ function selectImages() {
# Menu for selecting installation method
function installationMethod() {
INSTALLATION_METHOD=$(whiptail --title "Cincan private registry setup" --fb --menu "Choose installation method" --backtitle \
" CinCan - Continuous Integration for the Collaborative Analysis of Incidents " \
......@@ -114,27 +143,10 @@ 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 / pull images
TOOLS_COUNT_TO_INSTALL=$(echo "$TOOLS_TO_INSTALL" |wc -w)
if [ "$INSTALLATION_METHOD" = 1 ]; then
# Build the images
# Build the images
function buildImages(){
echo -e "$CHECK Building Dockerfiles..."
i=1
......@@ -150,9 +162,12 @@ if [ "$INSTALLATION_METHOD" = 1 ]; then
i=$((i+1))
[ $i -lt $((TOOLS_COUNT_TO_INSTALL+1)) ]
do :;done
else
# Pull images
}
# Pull images
function pullImages(){
echo -e "$CHECK Downloading images from Docker hub"
i=1
......@@ -168,26 +183,51 @@ else
i=$((i+1))
[ $i -lt $((TOOLS_COUNT_TO_INSTALL+1)) ]
do :;done
fi
}
# Push images to the private registry
echo -e "$CHECK Pushing images to Docker private registry"
function pushImages() {
echo -e "$CHECK Pushing images to Docker private registry"
i=1
while
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"
echo -e "$CHECK pushed $CURRENT_TOOL to $PRIVATE_REGISTRY:5000/"
docker rmi -f cincan/"$CURRENT_TOOL"
docker rmi -f "$PRIVATE_REGISTRY:5000/$CURRENT_TOOL"
echo -e "$CHECK removed $CURRENT_TOOL from host"
i=$((i+1))
[ $i -lt $((TOOLS_COUNT_TO_INSTALL+1)) ]
do :;done
}
i=1
while
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"
# Execute functions
getCredentials
cloneRepo
createMenuOptions
selectImages
installationMethod
TOOLS_COUNT_TO_INSTALL=$(echo "$TOOLS_TO_INSTALL" |wc -w)
if [ "$INSTALLATION_METHOD" = 1 ]; then
buildImages
else
pullImages
fi
sudo su "$DOCKER_USERNAME" -c "docker push $PRIVATE_REGISTRY:5000/$CURRENT_TOOL"
echo -e "$CHECK pushed $CURRENT_TOOL to $PRIVATE_REGISTRY:5000/"
pushImages
docker rmi -f cincan/"$CURRENT_TOOL"
docker rmi -f "$PRIVATE_REGISTRY:5000/$CURRENT_TOOL"
echo -e "$CHECK removed $CURRENT_TOOL from host"
echo -e "\n\nTo fetch the list of images in the private registry, type:"
echo "curl -X GET https://<USERNAME>:<PASSWORD>@172.20.0.6:5000/v2/_catalog --insecure"
i=$((i+1))
[ $i -lt $((TOOLS_COUNT_TO_INSTALL+1)) ]
do :;done
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