Add support for kubernetes deployment in rapid scripts.
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / rapid / dockerimage.sh
1 #!/usr/bin/env bash
2 ##
3 ## Copyright (c) 2010-2019 Intel Corporation
4 ##
5 ## Licensed under the Apache License, Version 2.0 (the "License");
6 ## you may not use this file except in compliance with the License.
7 ## You may obtain a copy of the License at
8 ##
9 ##     http://www.apache.org/licenses/LICENSE-2.0
10 ##
11 ## Unless required by applicable law or agreed to in writing, software
12 ## distributed under the License is distributed on an "AS IS" BASIS,
13 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ## See the License for the specific language governing permissions and
15 ## limitations under the License.
16 ##
17
18 PROX_DEPLOY_DIR="."
19 PROX_IMAGE_NAME="prox_slim"
20 RSA_KEY_FILE_NAME="rapid_rsa_key"
21
22 DOCKERFILE="Dockerfile"
23 DOCKER_REGISTRY="localhost:5000"
24
25 USE_DOCKER_CACHE="n"
26
27 function create_ssh_key()
28 {
29         if [ -f ./${RSA_KEY_FILE_NAME} ]; then
30                 read -p "RSA key already exist! Do you want to remove it (yYnN)?" -n 1 -r
31
32                 if [ "$REPLY" == "y" ] || [ "$REPLY" == "Y" ]; then
33                         echo "Removing existing key..."
34                         sleep 3
35
36                         [ -f "./${RSA_KEY_FILE_NAME}" ] && rm -rf ./${RSA_KEY_FILE_NAME}
37                         [ -f "./${RSA_KEY_FILE_NAME}.pub" ] && rm -rf ./${RSA_KEY_FILE_NAME}.pub
38                 else
39                         echo "Using existing key..."
40                         return
41                 fi
42         fi
43
44         echo "Generating new RSA key..."
45         ssh-keygen -t rsa -b 4096 -N "" -f ./${RSA_KEY_FILE_NAME}
46 }
47
48 function build_prox_image()
49 {
50         if [ "${USE_DOCKER_CACHE}" == "y" ]; then
51                 echo "Building image using cache..."
52                 docker build --rm -t ${PROX_IMAGE_NAME}:latest -f ${DOCKERFILE} ${PROX_DEPLOY_DIR}
53         else
54                 echo "Building image without cache..."
55                 docker build --no-cache --rm -t ${PROX_IMAGE_NAME}:latest -f ${DOCKERFILE} ${PROX_DEPLOY_DIR}
56         fi
57 }
58
59 function save_prox_image()
60 {
61         echo "Saving image ${PROX_IMAGE_NAME}:latest to ./${PROX_IMAGE_NAME}.tar"
62         docker save -o ./${PROX_IMAGE_NAME}.tar ${PROX_IMAGE_NAME}:latest
63 }
64
65 function load_prox_image()
66 {
67         echo "Loading image ./${PROX_IMAGE_NAME}.tar"
68         docker load -i ./${PROX_IMAGE_NAME}.tar
69 }
70
71 function push_prox_image()
72 {
73         docker tag ${PROX_IMAGE_NAME}:latest ${DOCKER_REGISTRY}/${PROX_IMAGE_NAME}
74         docker push ${DOCKER_REGISTRY}/${PROX_IMAGE_NAME}
75 }
76
77 function print_help()
78 {
79         echo "${0}: [build|load|push]"
80         echo "    build: build and save image ${PROX_IMAGE_NAME}:latest using ${DOCKERFILE}"
81         echo "    load:  load saved image from ${PROX_IMAGE_NAME}.tar file in the local registry"
82         echo "    push:  tag and push local ${PROX_IMAGE_NAME}:latest image in the ${DOCKER_REGISTRY}/${PROX_IMAGE_NAME} registry"
83 }
84
85 if [ "$1" == "build" ]; then
86         create_ssh_key
87         build_prox_image
88         save_prox_image
89 elif [ "$1" == "load" ]; then
90         load_prox_image
91 elif [ "$1" == "push" ]; then
92         push_prox_image
93 else
94         print_help
95 fi