blob: a76670c32f3a995fc568cd87e9fcec776843d0ac [file] [log] [blame]
Matteo Scandolo3e765d52018-05-14 15:22:23 -07001#!/usr/bin/env bash
2#
3# Copyright 2017-present Open Networking Foundation
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# #
19# Tags and push all the local images to a docker-registry #
20# #
21###########################################################
22
23#
24# Displays the help menu.
25#
26display_help () {
27 echo "Usage: $0 {--push|--help} [docker-registry] [tag=candidate] " >&2
28 echo " "
29 echo " -h, --help Display this help message."
30 echo " -r, --registry Tags and push all the local docker images to <docker-registry>"
31 echo " "
32 echo " docker-registry The address of the registry"
33 echo " tag The tag to be used"
34 echo " "
35 echo "Example usages:"
36 echo " ./tag_and_push.sh -r 192.168.10.100:30500"
37 echo " ./tag_and_push.sh -r 192.168.10.100:30500 devel"
38}
39
40#
41# Tag and push all the locally available docker images
42#
43tag_and_push () {
44 echo "Pushing images to $DOCKER_REGISTRY with tag $DOCKER_TAG"
45 echo " "
46
47 # reading docker images
48 DOCKER_IMAGES_STR=$(docker images --format="{{.Repository}} {{.Tag}}" --filter "dangling=false" | grep -v none | grep "^xosproject")
49 DOCKER_IMAGES=($DOCKER_IMAGES_STR)
50
51 # looping over docker images
52 IMAGELIST_LENGTH=${#DOCKER_IMAGES[@]}
53 while [ $IMAGELIST_LENGTH -gt 0 ]
54 do
55 IMAGE=${DOCKER_IMAGES[0]}:${DOCKER_IMAGES[1]}
56
57 docker tag $IMAGE $DOCKER_REGISTRY/$IMAGE
58 docker push $DOCKER_REGISTRY/$IMAGE
59
60 # removing the already tagged and pushed image
61 DOCKER_IMAGES=("${DOCKER_IMAGES[@]:2}")
62 IMAGELIST_LENGTH=${#DOCKER_IMAGES[@]}
63 done
64}
65
66#
67# Init
68#
69CLI_OPT=$1
70DOCKER_REGISTRY=$2
71DOCKER_TAG=${3:-"candidate"}
72
73while :
74do
75 case $CLI_OPT in
76 -r | --registry)
77 tag_and_push
78 exit 0
79 ;;
80 -h | --help)
81 display_help
82 exit 0
83 ;;
84 --) # End of all options
85 shift
86 break
87 ;;
88 *)
89 echo Error: Unknown option: $CLI_OPT >&2
90 echo " "
91 display_help
92 exit -1
93 ;;
94 esac
95done