| tierno | 7d782ef | 2019-10-04 12:56:31 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | ## |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | # |
| 16 | # For those usages not covered by the Apache License, Version 2.0 please |
| 17 | # contact with: nfvlabs@tid.es |
| 18 | ## |
| 19 | |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 20 | [[ "$*" == *--help* ]] && echo \ |
| 21 | "This script tests docker build based on debian packages. It generates a docker image bases on Dockerfile-devops, " \ |
| 22 | "prints package information and if desired updates OSM RO docker with the generated image. |
| 23 | Generated packages are stored at './temp' folder. |
| 24 | Options: |
| 25 | --help show this help |
| 26 | --no-cache Use if found problems looking for packages |
| 27 | --update Use to update OSM, RO docker with this image" && exit 0 |
| 28 | |
| tierno | 366c260 | 2020-07-10 10:52:56 +0000 | [diff] [blame] | 29 | [[ "$*" == *--no-cache* ]] && no_cache="--no-cache" || no_cache="" |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 30 | [[ "$*" == *--update* ]] && update_osm="k8s" || update_osm="" |
| tierno | 7d782ef | 2019-10-04 12:56:31 +0000 | [diff] [blame] | 31 | |
| 32 | HERE=$(dirname $(readlink -f ${BASH_SOURCE[0]})) |
| 33 | export RO_BASE=$(dirname $HERE) |
| 34 | |
| 35 | # clean |
| 36 | docker rm -f ro_pkg 2>/dev/null && echo docker ro_pkg removed |
| 37 | rm -rf $HERE/temp/* |
| tierno | 57dadcf | 2019-11-23 15:11:15 +0000 | [diff] [blame] | 38 | find $RO_BASE -name "*.pyc" -exec rm {} ";" |
| tierno | 7d782ef | 2019-10-04 12:56:31 +0000 | [diff] [blame] | 39 | mkdir -p $HERE/temp |
| 40 | |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 41 | echo -e "\n\n[STAGE 1] Building dockerfile used for the package generation" |
| tierno | 7d782ef | 2019-10-04 12:56:31 +0000 | [diff] [blame] | 42 | docker build $RO_BASE -f $RO_BASE/Dockerfile -t opensourcemano/ro_pkg |
| 43 | sleep 2 |
| 44 | |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 45 | echo "[STAGE 1.1] Generating packages inside docker ro_pkg" |
| tierno | 7d782ef | 2019-10-04 12:56:31 +0000 | [diff] [blame] | 46 | docker run -d --name ro_pkg opensourcemano/ro_pkg bash -c 'sleep 3600' |
| 47 | docker cp $RO_BASE ro_pkg:/RO |
| 48 | docker exec ro_pkg bash -c 'cd /RO; ./devops-stages/stage-build.sh' |
| 49 | deb_files=`docker exec ro_pkg bash -c 'ls /RO/deb_dist/'` |
| 50 | [ -z "$deb_files" ] && echo "No packages generated" >&2 && exit 1 |
| 51 | echo $deb_files |
| 52 | |
| 53 | echo -e "\n\n[STAGE 1.2] Print package information and copy to '$HERE/temp/'" |
| 54 | # print package information and copy to "$HERE/temp/" |
| 55 | for deb_file in $deb_files ; do |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 56 | echo; echo; echo |
| 57 | echo $deb_file info: |
| 58 | echo "===========================" |
| 59 | docker cp ro_pkg:/RO/deb_dist/$deb_file $HERE/temp/ |
| 60 | dpkg -I $HERE/temp/$(basename $deb_file) |
| tierno | 7d782ef | 2019-10-04 12:56:31 +0000 | [diff] [blame] | 61 | done |
| 62 | |
| 63 | # docker rm -f ro_pkg |
| 64 | echo -e "\n\n[STAGE 2] Building docker image opensourcemano/ro:py3_devops based on debian packages" |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 65 | docker build $HERE -f $HERE/Dockerfile-devops -t opensourcemano/ro:py3_devops $no_cache || |
| tierno | 7d782ef | 2019-10-04 12:56:31 +0000 | [diff] [blame] | 66 | ! echo "error generating devops dockerfile" >&2 || exit 1 |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 67 | |
| 68 | [[ -z "$update_osm" ]] && exit 0 |
| tierno | 7d782ef | 2019-10-04 12:56:31 +0000 | [diff] [blame] | 69 | sleep 2 |
| tierno | 7d782ef | 2019-10-04 12:56:31 +0000 | [diff] [blame] | 70 | |
| 71 | echo -e "\n\n[STAGE 3] Update service osm_ro with generated docker image" |
| tierno | 7277486 | 2020-05-04 11:44:15 +0000 | [diff] [blame] | 72 | # try docker swarm. If fails try kebernetes |
| 73 | if docker service update osm_ro --force --image opensourcemano/ro:py3_devops 2>/dev/null |
| 74 | then |
| 75 | sleep 2 |
| 76 | docker container prune -f |
| 77 | elif kubectl -n osm patch deployment ro --patch \ |
| 78 | '{"spec": {"template": {"spec": {"containers": [{"name": "ro", "image": "opensourcemano/ro:py3_devops"}]}}}}' |
| 79 | then |
| 80 | kubectl -n osm scale deployment ro --replicas=0 |
| 81 | kubectl -n osm scale deployment ro --replicas=1 |
| 82 | else |
| 83 | echo "Cannot update OSM" && exit 1 |
| 84 | fi |
| tierno | 7d782ef | 2019-10-04 12:56:31 +0000 | [diff] [blame] | 85 | docker service logs osm_ro |