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