blob: 17e5915048d33ba9897ad6ca010e062dedb748a3 [file] [log] [blame]
Ondřej Šmaleccb93bce2019-11-19 15:28:38 +01001#!/usr/bin/env bash
2
3#Variables
4SCRIPT_PATH=$0
5CMD=$(basename "$0")
6
7REPO_IMAGE=${1}
8
9#Server with connection to internet
10SERVER=${2}
11
12#User and password for docker login on $SERVER
13REMOTE_USER=
14REMOTE_PASSWD=
15
16#User and password for docker login on localhost
17LOCAL_USER=
18LOCAL_PASSWD=
Ondřej Šmalec2b2ba7f2020-07-14 10:25:53 +020019IMAGE=`sed 's/^[^.]*\.[^/]*\///g' <<< ${1}`
Ondřej Šmaleccb93bce2019-11-19 15:28:38 +010020
21help()
22{
23cat <<EOF
24${CMD} - tool for downloading image and adding it to offline nexus repository
25
26Usage
27 $SCRIPT_PATH <repository>/<image>:<tag> <server>
28
29 $SCRIPT_PATH <repository>/<image>:<tag> <server> --local-repo example.repository
30
31MANDATORY 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
35OPTIONAL 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
42EOF
43}
44
45if [ $# -lt 2 ]; then
46 help
47 exit 1
48fi
49
50#Set up parameters
51while [[ $# -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
71done
72
73REMOTE_USER=${REMOTE_USER:-jenkins}
74REMOTE_PASSWD=${REMOTE_PASSWD:-jenkins}
75LOCAL_USER=${LOCAL_USER:-admin}
76LOCAL_PASSWD=${LOCAL_PASSWD:-admin123}
77LOCAL_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
88EOF
89
90if [ $? -eq 1 ]
91then
92 exit 1
93fi
94
95# Download image from CICD nexus repo and push it into local repo
96docker pull $SERVER/$IMAGE
97docker tag $SERVER/$IMAGE $LOCAL_REPO/$IMAGE
98docker login -u $LOCAL_USER -p $LOCAL_PASSWD $LOCAL_REPO
99docker push $LOCAL_REPO/$IMAGE
100docker rmi $SERVER/$IMAGE
101docker rmi $LOCAL_REPO/$IMAGE
102
103echo 'Done Successfully'