Matteo Scandolo | 3e765d5 | 2018-05-14 15:22:23 -0700 | [diff] [blame] | 1 | #!/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 | # |
| 26 | display_help () { |
Luca Prete | 284d764 | 2018-05-24 15:19:43 -0700 | [diff] [blame] | 27 | echo "Tags and pushes to a remote registry all the images present in the local docker registry." >&2 |
| 28 | echo " " |
Matteo Scandolo | 3e765d5 | 2018-05-14 15:22:23 -0700 | [diff] [blame] | 29 | echo "Usage: $0 {--push|--help} [docker-registry] [tag=candidate] " >&2 |
| 30 | echo " " |
Luca Prete | 284d764 | 2018-05-24 15:19:43 -0700 | [diff] [blame] | 31 | 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 Scandolo | 3e765d5 | 2018-05-14 15:22:23 -0700 | [diff] [blame] | 33 | 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 | # |
| 45 | tag_and_push () { |
| 46 | echo "Pushing images to $DOCKER_REGISTRY with tag $DOCKER_TAG" |
| 47 | echo " " |
| 48 | |
| 49 | # reading docker images |
Luca Prete | 284d764 | 2018-05-24 15:19:43 -0700 | [diff] [blame] | 50 | DOCKER_IMAGES=$(docker images --format="{{.Repository}}:{{.Tag}}" --filter "dangling=false" | grep -v none) |
Matteo Scandolo | 3e765d5 | 2018-05-14 15:22:23 -0700 | [diff] [blame] | 51 | |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 52 | # split string to list only on newlines |
| 53 | IFS=$'\n' |
| 54 | for image in $DOCKER_IMAGES; |
Matteo Scandolo | 3e765d5 | 2018-05-14 15:22:23 -0700 | [diff] [blame] | 55 | do |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 56 | docker tag "$image $DOCKER_REGISTRY/$image" |
| 57 | docker push "$DOCKER_REGISTRY/$image" |
Matteo Scandolo | 3e765d5 | 2018-05-14 15:22:23 -0700 | [diff] [blame] | 58 | done |
| 59 | } |
| 60 | |
| 61 | # |
| 62 | # Init |
| 63 | # |
| 64 | CLI_OPT=$1 |
| 65 | DOCKER_REGISTRY=$2 |
| 66 | DOCKER_TAG=${3:-"candidate"} |
| 67 | |
| 68 | while : |
| 69 | do |
| 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 Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 84 | echo Error: Unknown option: "$CLI_OPT" >&2 |
Matteo Scandolo | 3e765d5 | 2018-05-14 15:22:23 -0700 | [diff] [blame] | 85 | echo " " |
| 86 | display_help |
| 87 | exit -1 |
| 88 | ;; |
| 89 | esac |
Zack Williams | 249ac7f | 2018-05-18 13:34:14 -0700 | [diff] [blame] | 90 | done |