Fail the entrypoint script on any command failure
[oom/offline-installer.git] / build / docker-entrypoint.sh
1 #!/usr/bin/env bash
2
3 set -eo pipefail
4
5 # Set distribution family
6 distro_type=$(cat /etc/*-release | grep -w "ID" | awk -F'=' '{ print $2 }' | tr -d '"')
7 case "$distro_type" in
8         ubuntu)
9                 distro_type="ubuntu"
10         ;;
11         rhel|centos)
12                 distro_type="rhel"
13         ;;
14         *)
15                 echo "Unknown type of linux distribution."
16                 exit 1
17         ;;
18 esac
19
20 # Target path for created repository
21 OFFLINE_REPO_DIR=""
22
23 # Path to directory containing onap_rpm.list and onap_deb.list files
24 PCKG_LIST_DIR=""
25
26 # Path to additional packages lists
27 ADD_LIST_DIR=""
28
29 # Show help
30 help () {
31 cat <<EOF
32 Docker entrypoint script for creating RPM/DEB repository based on container platform type
33
34 usage: create-repo.sh [OPTION]...
35
36   -d | --directory              target repository path
37   -l | --list                   input rpm/deb list directory
38   -a | --additional-list        additional packages list; can be used multiple times
39   -p | --packages-lists-path    other additional packages lists
40   -h | --help                   show this help
41
42 Both paths have to be set with shared volume between
43 container and the host. Default path in container is: /tmp/
44 Repository will be created at: /<path>/resources/pkg/rhel/
45 RMP/DEB list is stored at: ./data_list/
46 EOF
47 }
48
49 # Getting input parameters
50 if [[ $# -eq 0 ]] ; then
51     help # show help
52     exit 0
53 fi
54 while [[ $# -gt 0 ]]
55 do
56     case "$1" in
57         -h|--help)
58             # Help parameter
59             help # show help
60             exit
61             ;;
62         -d|--directory)
63             # Directory parameter
64             # Set target reposity path
65             OFFLINE_REPO_DIR="$2"
66             ;;
67         -l|--list)
68             # List parameter
69             # Set path containing onap_rpm.list or onap_deb.list file
70             PCKG_LIST_DIR="$2"
71             ;;
72         -p|--packages-lists-path)
73             # Path parameter
74             # Set path for additional packages lists
75             ADD_LIST_DIR="$2"
76             ;;
77         -a|--additional-list)
78             # Array of additional packages lists
79             ADDITIONAL_LISTS+=("$2")
80             ;;
81         *)
82             # unknown option
83             help # show help
84             exit
85             ;;
86     esac
87     shift;shift
88 done
89
90 # Testing if directory parameter was used
91 # If not variable is set to /tmp/repo by default
92 if test -z "$OFFLINE_REPO_DIR"
93 then
94     OFFLINE_REPO_DIR="/tmp/repo/"
95 fi
96
97 # Testing if list parameter was used
98 # If not variable is set to default value /tmp/offline/data-list
99 if test -z "$PCKG_LIST_DIR"
100 then
101     PCKG_LIST_DIR="/tmp/offline/data_list/"
102 fi
103
104 # Testing if additional packages list parameter was used
105 # If not variable is set to default value /tmp/additional-lists
106 if test -z "$PCKG_LIST_DIR"
107 then
108     PCKG_LIST_DIR="/tmp/additional-lists/"
109 fi
110
111 # Clean target repo dir for idempotency
112 rm -rf ${OFFLINE_REPO_DIR}/*
113
114 case "$distro_type" in
115     ubuntu)
116         # Change current working dir
117         pushd $OFFLINE_REPO_DIR
118
119         # Install dpkg-deb package for create repository in folder
120         # Install software-properties-common to get add-apt-repository command
121         # Install apt-transport-https, ca-certificates, curl and gnupg-agent allowing apt to use a repository over HTTPS
122         apt-get update -y
123         apt-get install dpkg-dev apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y
124
125         # Add Docker's official GPG key:
126         curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
127         apt-key fingerprint 0EBFCD88
128
129         # Add docker repository
130         add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
131
132         # Temp fix of known bug
133         # https://bugs.launchpad.net/ubuntu/+source/aptitude/+bug/1543280
134         chown _apt $OFFLINE_REPO_DIR
135
136         # Download all packages from onap_deb.list via apt-get to repository folder
137         for i in $(cat ${PCKG_LIST_DIR}onap_deb.list | awk '{print $1}');do apt-get download $i -y; done
138         for i in $(cat ${PCKG_LIST_DIR}onap_deb.list | awk '{print $1}');
139                     do
140                     for depends in $(apt-cache depends $i | grep -E 'Depends' | cut -d ':' -f 2,3 | sed -e s/'<'/''/ -e s/'>'/''/);
141                         do apt-get download $depends -y;
142                     done;
143                 done
144
145         # Download all packages with dependencies from all additional packages lists via apt-get to repository folder
146         if ! [ ${#ADDITIONAL_LISTS[@]} -eq 0 ]; then
147             for list in ${ADDITIONAL_LISTS[@]}
148             do
149                 for i in $(cat ${ADD_LIST_DIR}$list | awk '{print $1}');do apt-get download $i -y; done
150                 for i in $(cat ${ADD_LIST_DIR}$list | awk '{print $1}');
151                     do
152                     for depends in $(apt-cache depends $i | grep -E 'Depends' | cut -d ':' -f 2,3 | sed -e s/'<'/''/ -e s/'>'/''/);
153                         do apt-get download $depends -y;
154                     done;
155                 done
156             done
157         fi
158
159         # In repository folder create gz package with deb packages
160         dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz
161     ;;
162
163     rhel)
164         # Install createrepo package for create repository in folder,
165         # yum-utils due to yum-config-manager for adding docker repository
166         # and epel-release for additional packages (like jq etc.)
167         yum install createrepo yum-utils epel-release -y
168
169         # Add official docker repository
170         yum-config-manager --add-repo=https://download.docker.com/linux/centos/7/x86_64/stable/
171
172         # Download all packages from onap_rpm.list via yumdownloader to repository folder
173         for i in $(cat ${PCKG_LIST_DIR}onap_rpm.list | awk '{print $1}');do yumdownloader --resolve --downloadonly --destdir=${OFFLINE_REPO_DIR} $i -y; done
174
175         # Download all packages from all additional packages lists via apt-get to repository folder
176         if ! [ ${#ADDITIONAL_LISTS[@]} -eq 0 ]; then
177             for list in ${ADDITIONAL_LISTS[@]}
178             do
179                 for i in $(cat ${ADD_LIST_DIR}$list | awk '{print $1}');do yumdownloader --resolve --downloadonly --destdir=${OFFLINE_REPO_DIR} $i -y; done
180             done
181         fi
182
183         # Create repository
184         createrepo $OFFLINE_REPO_DIR
185     ;;
186
187     *)
188         echo "Unknown type of linux distribution."
189         exit 1
190     ;;
191 esac