Matteo Scandolo | 3e765d5 | 2018-05-14 15:22:23 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Luca Prete | 8ae13c2 | 2018-11-06 16:06:58 -0800 | [diff] [blame] | 2 | |
| 3 | # Copyright 2018-present Open Networking Foundation |
Matteo Scandolo | 3e765d5 | 2018-05-14 15:22:23 -0700 | [diff] [blame] | 4 | # |
Luca Prete | 8ae13c2 | 2018-11-06 16:06:58 -0800 | [diff] [blame] | 5 | # 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 Scandolo | 3e765d5 | 2018-05-14 15:22:23 -0700 | [diff] [blame] | 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
Luca Prete | 8ae13c2 | 2018-11-06 16:06:58 -0800 | [diff] [blame] | 12 | # 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 Scandolo | 3e765d5 | 2018-05-14 15:22:23 -0700 | [diff] [blame] | 16 | |
Luca Prete | 8ae13c2 | 2018-11-06 16:06:58 -0800 | [diff] [blame] | 17 | # 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 Scandolo | 3e765d5 | 2018-05-14 15:22:23 -0700 | [diff] [blame] | 29 | |
| 30 | # |
| 31 | # Displays the help menu. |
| 32 | # |
| 33 | display_help () { |
Luca Prete | 8ae13c2 | 2018-11-06 16:06:58 -0800 | [diff] [blame] | 34 | echo "Tags and pushes to a target Docker registry a list of images on the system or given in stdin." |
Luca Prete | 284d764 | 2018-05-24 15:19:43 -0700 | [diff] [blame] | 35 | echo " " |
Luca Prete | 8ae13c2 | 2018-11-06 16:06:58 -0800 | [diff] [blame] | 36 | echo "Usage: $0 [-f|--filter filter] [-r|--registry docker-registry] [-s|--source image-file] [-t|--tag custom-tag] [-h --help]" |
Matteo Scandolo | 3e765d5 | 2018-05-14 15:22:23 -0700 | [diff] [blame] | 37 | echo " " |
Luca Prete | 8ae13c2 | 2018-11-06 16:06:58 -0800 | [diff] [blame] | 38 | 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 Scandolo | 3e765d5 | 2018-05-14 15:22:23 -0700 | [diff] [blame] | 41 | echo " " |
| 42 | echo "Example usages:" |
Luca Prete | 8ae13c2 | 2018-11-06 16:06:58 -0800 | [diff] [blame] | 43 | 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 Prete | d5587ac | 2018-12-11 13:08:25 -0800 | [diff] [blame] | 46 | 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 Prete | 8ae13c2 | 2018-11-06 16:06:58 -0800 | [diff] [blame] | 47 | 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 Prete | d5587ac | 2018-12-11 13:08:25 -0800 | [diff] [blame] | 48 | echo " ./tag_and_push.sh -f xosproject" # push all local images containing xosproject in the name and pushes them to dockerhub |
Matteo Scandolo | 3e765d5 | 2018-05-14 15:22:23 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Luca Prete | 8ae13c2 | 2018-11-06 16:06:58 -0800 | [diff] [blame] | 51 | # Parse params |
| 52 | while :; 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 |
| 97 | done |
Matteo Scandolo | 3e765d5 | 2018-05-14 15:22:23 -0700 | [diff] [blame] | 98 | |
Luca Prete | 8ae13c2 | 2018-11-06 16:06:58 -0800 | [diff] [blame] | 99 | # Source images list |
| 100 | if [ -t 0 ]; then |
| 101 | images=$(docker images | awk '{if (NR!=1) {print}}' | awk '{ a=$1":"$2; print a }') |
Matteo Scandolo | 9ab700e | 2018-09-20 16:56:16 -0700 | [diff] [blame] | 102 | else |
Luca Prete | 8ae13c2 | 2018-11-06 16:06:58 -0800 | [diff] [blame] | 103 | images="" |
| 104 | while IFS= read -r line; do |
| 105 | # shellcheck disable=SC1117 |
| 106 | images+="$line\n" |
| 107 | done < "${custom_file:-/dev/stdin}" |
Matteo Scandolo | 9ab700e | 2018-09-20 16:56:16 -0700 | [diff] [blame] | 108 | fi |
Luca Prete | 8ae13c2 | 2018-11-06 16:06:58 -0800 | [diff] [blame] | 109 | |
| 110 | # Filter images |
| 111 | if [[ ! -z "$filter" ]] |
| 112 | then |
Matteo Scandolo | 4989ad9 | 2018-12-17 11:32:17 -0800 | [diff] [blame] | 113 | images=$(echo -e "${images}" | grep -E "${filter}") |
| 114 | fi |
| 115 | |
| 116 | if [[ ! -z "$registry" ]] |
| 117 | then |
Matteo Scandolo | 0dd7537 | 2018-12-18 13:13:22 -0800 | [diff] [blame] | 118 | images=$(echo "${images}" | grep -v "${registry}") |
Luca Prete | 8ae13c2 | 2018-11-06 16:06:58 -0800 | [diff] [blame] | 119 | fi |
| 120 | |
| 121 | for image in $(echo -e "${images}"); do |
| 122 | new_image="" |
| 123 | |
| 124 | # Set registry |
Matteo Scandolo | 4989ad9 | 2018-12-17 11:32:17 -0800 | [diff] [blame] | 125 | if [[ ! -z "$registry" ]] |
| 126 | then |
| 127 | new_image+="${registry}/" |
| 128 | else |
| 129 | new_image="" |
| 130 | fi |
Luca Prete | 8ae13c2 | 2018-11-06 16:06:58 -0800 | [diff] [blame] | 131 | |
| 132 | IFS=':' read -r -a image_tag_splitted <<< "$image" |
| 133 | |
| 134 | # Set image name |
Matteo Scandolo | 4989ad9 | 2018-12-17 11:32:17 -0800 | [diff] [blame] | 135 | new_image+="${image_tag_splitted[0]}:" |
Luca Prete | 8ae13c2 | 2018-11-06 16:06:58 -0800 | [diff] [blame] | 136 | |
| 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}" |
| 145 | done |