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