blob: a2bdec536dcfca24a9b3fbf9d125f5679ce0bd7c [file] [log] [blame]
Matteo Scandolo3e765d52018-05-14 15:22:23 -07001#!/usr/bin/env bash
Luca Prete8ae13c22018-11-06 16:06:58 -08002
3# Copyright 2018-present Open Networking Foundation
Matteo Scandolo3e765d52018-05-14 15:22:23 -07004#
Luca Prete8ae13c22018-11-06 16:06:58 -08005# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy
7# of the License at:
Matteo Scandolo3e765d52018-05-14 15:22:23 -07008#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
Luca Prete8ae13c22018-11-06 16:06:58 -080012# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
Matteo Scandolo3e765d52018-05-14 15:22:23 -070016
Luca Prete8ae13c22018-11-06 16:06:58 -080017# copy-ssh-keys.sh - Adds ssh keys to nodes given as parameters to the script,
18# after removing them from the ~/.ssh/known_hosts file on the local system.
19#
20# This script should be run interactively as it will prompt for input, and only
21# invoked once, so as not to add multiple copies of the SSH key to the remote
22# system.
23
24#############################################################
25# #
26# Tags and push a list of local images to a target registry #
27# #
28#############################################################
Matteo Scandolo3e765d52018-05-14 15:22:23 -070029
30#
31# Displays the help menu.
32#
33display_help () {
Luca Prete8ae13c22018-11-06 16:06:58 -080034 echo "Tags and pushes to a target Docker registry a list of images on the system or given in stdin."
Luca Prete284d7642018-05-24 15:19:43 -070035 echo " "
Luca Prete8ae13c22018-11-06 16:06:58 -080036 echo "Usage: $0 [-f|--filter filter] [-r|--registry docker-registry] [-s|--source image-file] [-t|--tag custom-tag] [-h --help]"
Matteo Scandolo3e765d52018-05-14 15:22:23 -070037 echo " "
Luca Prete8ae13c22018-11-06 16:06:58 -080038 echo " filter A string used to filter the image names (used as 'grep -E \"^$filter\"')"
39 echo " docker-registry The address of the target registry. DockerHub will be instead used by default"
40 echo " custom-tag An optional, custom tag to be used to tag images. The same tag of the original images will be used otherwise"
Matteo Scandolo3e765d52018-05-14 15:22:23 -070041 echo " "
42 echo "Example usages:"
Luca Prete8ae13c22018-11-06 16:06:58 -080043 echo " echo onosproject/onos:1.13.5 | ./tag_and_push.sh" # push the local onosproject/onos:1.13.5 image given in input and pushes it to DockerHub
44 echo " cat images | ./tag_and_push.sh -t my_tag" # tag with "my_tag" the images in the file images given in input and push them to DockerHub
45 echo " ./tag_and_push.sh -s images.txt -t my_tag" # tag with "my_tag" the images in the file images.txt and push them to DockerHub
Luca Preted5587ac2018-12-11 13:08:25 -080046 echo " ./tag_and_push.sh -f xosproject -r 192.168.10.100:30500" # tag all the xosproject images and push them to the registry 192.168.10.100:30500
Luca Prete8ae13c22018-11-06 16:06:58 -080047 echo " ./tag_and_push.sh -r 192.168.10.100:30500" # tag all local images and push them to the registry 192.168.10.100:30500
Luca Preted5587ac2018-12-11 13:08:25 -080048 echo " ./tag_and_push.sh -f xosproject" # push all local images containing xosproject in the name and pushes them to dockerhub
Matteo Scandolo3e765d52018-05-14 15:22:23 -070049}
50
Luca Prete8ae13c22018-11-06 16:06:58 -080051# Parse params
52while :; do
53 case $1 in
54 -s|--source)
55 shift
56 if [[ -z $1 ]] || [[ $1 = -f ]] || [[ $1 = -t ]] || [[ $1 = -r ]] || [[ $1 = -h ]]
57 then
58 display_help
59 exit 1
60 fi
61 custom_file="$1"
62 ;;
63 -r|--registry)
64 shift
65 if [[ -z $1 ]] || [[ $1 = -f ]] || [[ $1 = -t ]] || [[ $1 = -s ]] || [[ $1 = -h ]]
66 then
67 display_help
68 exit 1
69 fi
70 registry="$1"
71 ;;
72 -f|--filter)
73 shift
74 if [[ -z $1 ]] || [[ $1 = -r ]] || [[ $1 = -t ]] || [[ $1 = -s ]] || [[ $1 = -h ]]
75 then
76 display_help
77 exit 1
78 fi
79 filter="$1"
80 ;;
81 -t|--tag)
82 shift
83 if [[ -z $1 ]] || [[ $1 = -r ]] || [[ $1 = -f ]] || [[ $1 = -s ]] || [[ $1 = -h ]]
84 then
85 display_help
86 exit 1
87 fi
88 custom_tag="$1"
89 ;;
90 -h|--help)
91 display_help
92 exit 0
93 ;;
94 *) break
95 esac
96 shift
97done
Matteo Scandolo3e765d52018-05-14 15:22:23 -070098
Luca Prete8ae13c22018-11-06 16:06:58 -080099# Source images list
100if [ -t 0 ]; then
101 images=$(docker images | awk '{if (NR!=1) {print}}' | awk '{ a=$1":"$2; print a }')
Matteo Scandolo9ab700e2018-09-20 16:56:16 -0700102else
Luca Prete8ae13c22018-11-06 16:06:58 -0800103 images=""
104 while IFS= read -r line; do
105 # shellcheck disable=SC1117
106 images+="$line\n"
107 done < "${custom_file:-/dev/stdin}"
Matteo Scandolo9ab700e2018-09-20 16:56:16 -0700108fi
Luca Prete8ae13c22018-11-06 16:06:58 -0800109
110# Filter images
Zack Williamsf0b8e942019-07-23 16:21:28 -0700111if [[ -n "$filter" ]]
Luca Prete8ae13c22018-11-06 16:06:58 -0800112then
Matteo Scandolo4989ad92018-12-17 11:32:17 -0800113 images=$(echo -e "${images}" | grep -E "${filter}")
114fi
115
Zack Williamsf0b8e942019-07-23 16:21:28 -0700116if [[ -n "$registry" ]]
Matteo Scandolo4989ad92018-12-17 11:32:17 -0800117then
Matteo Scandolo0dd75372018-12-18 13:13:22 -0800118 images=$(echo "${images}" | grep -v "${registry}")
Luca Prete8ae13c22018-11-06 16:06:58 -0800119fi
120
121for image in $(echo -e "${images}"); do
122 new_image=""
123
124 # Set registry
Zack Williamsf0b8e942019-07-23 16:21:28 -0700125 if [[ -n "$registry" ]]
Matteo Scandolo4989ad92018-12-17 11:32:17 -0800126 then
127 new_image+="${registry}/"
128 else
129 new_image=""
130 fi
Luca Prete8ae13c22018-11-06 16:06:58 -0800131
132 IFS=':' read -r -a image_tag_splitted <<< "$image"
133
134 # Set image name
Matteo Scandolo4989ad92018-12-17 11:32:17 -0800135 new_image+="${image_tag_splitted[0]}:"
Luca Prete8ae13c22018-11-06 16:06:58 -0800136
137 # Set tag
138 splitted_tag="${image_tag_splitted[1]}"
139 new_image+="${custom_tag:-$splitted_tag}"
140
141 docker tag "${image}" "${new_image}" > /dev/null
142 docker push "${new_image}" > /dev/null
143
144 echo "${new_image}"
145done