blob: 7ec07304881517d18bc4a4a837eb72c761b1ded1 [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 () {
Luca Prete284d7642018-05-24 15:19:43 -070027 echo "Tags and pushes to a remote registry all the images present in the local docker registry." >&2
28 echo " "
Matteo Scandolo3e765d52018-05-14 15:22:23 -070029 echo "Usage: $0 {--push|--help} [docker-registry] [tag=candidate] " >&2
30 echo " "
Luca Prete284d7642018-05-24 15:19:43 -070031 echo " -h, --help Displays this help message."
32 echo " -r, --registry Tags and pushes all the local docker images to the remote <docker-registry>"
Matteo Scandolo3e765d52018-05-14 15:22:23 -070033 echo " "
34 echo " docker-registry The address of the registry"
35 echo " tag The tag to be used"
36 echo " "
37 echo "Example usages:"
38 echo " ./tag_and_push.sh -r 192.168.10.100:30500"
39 echo " ./tag_and_push.sh -r 192.168.10.100:30500 devel"
40}
41
42#
43# Tag and push all the locally available docker images
44#
45tag_and_push () {
46 echo "Pushing images to $DOCKER_REGISTRY with tag $DOCKER_TAG"
47 echo " "
48
49 # reading docker images
Luca Prete284d7642018-05-24 15:19:43 -070050 DOCKER_IMAGES=$(docker images --format="{{.Repository}}:{{.Tag}}" --filter "dangling=false" | grep -v none)
Matteo Scandolo3e765d52018-05-14 15:22:23 -070051
Zack Williams249ac7f2018-05-18 13:34:14 -070052 # split string to list only on newlines
53 IFS=$'\n'
54 for image in $DOCKER_IMAGES;
Matteo Scandolo3e765d52018-05-14 15:22:23 -070055 do
Zack Williams249ac7f2018-05-18 13:34:14 -070056 docker tag "$image $DOCKER_REGISTRY/$image"
57 docker push "$DOCKER_REGISTRY/$image"
Matteo Scandolo3e765d52018-05-14 15:22:23 -070058 done
59}
60
61#
62# Init
63#
64CLI_OPT=$1
65DOCKER_REGISTRY=$2
66DOCKER_TAG=${3:-"candidate"}
67
68while :
69do
70 case $CLI_OPT in
71 -r | --registry)
72 tag_and_push
73 exit 0
74 ;;
75 -h | --help)
76 display_help
77 exit 0
78 ;;
79 --) # End of all options
80 shift
81 break
82 ;;
83 *)
Zack Williams249ac7f2018-05-18 13:34:14 -070084 echo Error: Unknown option: "$CLI_OPT" >&2
Matteo Scandolo3e765d52018-05-14 15:22:23 -070085 echo " "
86 display_help
87 exit -1
88 ;;
89 esac
Zack Williams249ac7f2018-05-18 13:34:14 -070090done