Ondřej Šmalec | cb93bce | 2019-11-19 15:28:38 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | #Variables |
| 4 | SCRIPT_PATH=$0 |
| 5 | CMD=$(basename "$0") |
| 6 | |
| 7 | REPO_IMAGE=${1} |
| 8 | |
| 9 | #Server with connection to internet |
| 10 | SERVER=${2} |
| 11 | |
| 12 | #User and password for docker login on $SERVER |
| 13 | REMOTE_USER= |
| 14 | REMOTE_PASSWD= |
| 15 | |
| 16 | #User and password for docker login on localhost |
| 17 | LOCAL_USER= |
| 18 | LOCAL_PASSWD= |
Ondřej Šmalec | 2b2ba7f | 2020-07-14 10:25:53 +0200 | [diff] [blame] | 19 | IMAGE=`sed 's/^[^.]*\.[^/]*\///g' <<< ${1}` |
Ondřej Šmalec | cb93bce | 2019-11-19 15:28:38 +0100 | [diff] [blame] | 20 | |
| 21 | help() |
| 22 | { |
| 23 | cat <<EOF |
| 24 | ${CMD} - tool for downloading image and adding it to offline nexus repository |
| 25 | |
| 26 | Usage |
| 27 | $SCRIPT_PATH <repository>/<image>:<tag> <server> |
| 28 | |
| 29 | $SCRIPT_PATH <repository>/<image>:<tag> <server> --local-repo example.repository |
| 30 | |
| 31 | MANDATORY PARAMETERS: |
| 32 | <repository>/<image>:<tag> == image to be downloaded (e.g. nexus3.onap.org:10001/library/busybox:latest) |
| 33 | <server> == server with connection to internet and docker installed |
| 34 | |
| 35 | OPTIONAL PARAMETERS: |
| 36 | --remote-user == user for docker login on <server> |
| 37 | --remote-passwd == password for cicd-user |
| 38 | --local-user == user for docker login on localhost |
| 39 | --local-passwd == password for local-user |
| 40 | --local-repo == local repository where new image should be pushed |
| 41 | |
| 42 | EOF |
| 43 | } |
| 44 | |
| 45 | if [ $# -lt 2 ]; then |
| 46 | help |
| 47 | exit 1 |
| 48 | fi |
| 49 | |
| 50 | #Set up parameters |
| 51 | while [[ $# -gt 2 ]]; do |
| 52 | case $3 in |
| 53 | --remote-user) REMOTE_USER=$4 |
| 54 | ;; |
| 55 | --remote-passwd) REMOTE_PASSWD=$4 |
| 56 | ;; |
| 57 | --local-user) LOCAL_USER=$4 |
| 58 | ;; |
| 59 | --local-passwd) LOCAL_PASSWD=$4 |
| 60 | ;; |
| 61 | --local-repo) LOCAL_REPO=$4 |
| 62 | ;; |
| 63 | -h | --help) help |
| 64 | exit 0 |
| 65 | ;; |
| 66 | *) help |
| 67 | exit 1 |
| 68 | ;; |
| 69 | esac |
| 70 | shift 2 |
| 71 | done |
| 72 | |
| 73 | REMOTE_USER=${REMOTE_USER:-jenkins} |
| 74 | REMOTE_PASSWD=${REMOTE_PASSWD:-jenkins} |
| 75 | LOCAL_USER=${LOCAL_USER:-admin} |
| 76 | LOCAL_PASSWD=${LOCAL_PASSWD:-admin123} |
| 77 | LOCAL_REPO=${LOCAL_REPO:-nexus3.onap.org:10001} |
| 78 | |
| 79 | # Login to CICD server, pull image and push it into CICD nexus repo |
| 80 | /usr/bin/ssh -oStrictHostKeyChecking=no $SERVER << EOF |
| 81 | set -e |
| 82 | docker pull $REPO_IMAGE |
| 83 | docker tag $REPO_IMAGE $SERVER/$IMAGE |
| 84 | docker login -u $REMOTE_USER -p $REMOTE_PASSWD $SERVER |
| 85 | docker push $SERVER/$IMAGE |
| 86 | docker rmi $REPO_IMAGE |
| 87 | docker rmi $SERVER/$IMAGE |
| 88 | EOF |
| 89 | |
| 90 | if [ $? -eq 1 ] |
| 91 | then |
| 92 | exit 1 |
| 93 | fi |
| 94 | |
| 95 | # Download image from CICD nexus repo and push it into local repo |
| 96 | docker pull $SERVER/$IMAGE |
| 97 | docker tag $SERVER/$IMAGE $LOCAL_REPO/$IMAGE |
| 98 | docker login -u $LOCAL_USER -p $LOCAL_PASSWD $LOCAL_REPO |
| 99 | docker push $LOCAL_REPO/$IMAGE |
| 100 | docker rmi $SERVER/$IMAGE |
| 101 | docker rmi $LOCAL_REPO/$IMAGE |
| 102 | |
| 103 | echo 'Done Successfully' |