Adding support for Ubuntu in create_repo and docker_entrypoint scripts
[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 help () {
25     echo -e "Docker entrypoint script for creating RPM/DEB repository based on linux distribution where script is running\n"
26     echo "usage: create-repo.sh [-d|--directory output directory] [-l|--list input rpm/deb list directory]"
27     echo "-h --help: Show this help"
28     echo "-d --directory: set path for repo directory in container"
29     echo -e "-l --list: set path where rpm or deb list is stored in container\n"
30     echo "Both paths have to be set with shared volume between"
31     echo "container and host computer. Default path in container is: /tmp/"
32     echo "Repository will be created at: /<path>/resources/pkg/rhel/"
33     echo "RMP/DEB list is stored at: ./data_list/"
34 }
35
36 # Getting input parametters
37 POSITIONAL=()
38 if [[ $# -eq 0 ]] ; then
39     help # show help
40     exit 0
41 fi
42 while [[ $# -gt 0 ]]
43 do
44     case "$1" in
45         -h|--help)
46             # Help parametter
47             help # show help
48             exit
49             ;;
50         -d|--directory)
51             # Directory parametter
52             # Sets path where will be created reposity
53             OFFLINE_REPO_DIR="$2"
54             ;;
55         -l|--list)
56             # List parametter
57             # Sets path where is stored onap_rpm.list or onap_deb.list file
58             PCKG_LIST_DIR="$2"
59             ;;
60         *)
61             # unknown option
62             help # show help
63             exit
64             ;;
65     esac
66     shift;shift
67 done
68
69 # Testing if directory parametter was used
70 # If not variable is sets to default value:
71 # /tmp/repo/resources/pkg/rpm
72 # or
73 # /tmp/repo/resources/pkg/deb
74 if test -z "$OFFLINE_REPO_DIR"
75 then
76     OFFLINE_REPO_DIR="/tmp/repo/"
77 fi
78
79 # Testing if list parametter was used
80 # If not variable is sets to default value /tmp/offline/data-list
81 if test -z "$PCKG_LIST_DIR"
82 then
83     PCKG_LIST_DIR="/tmp/offline/data_list/"
84 fi
85
86 case "$distro_type" in
87     ubuntu)
88         # Change current working dir
89         pushd $OFFLINE_REPO_DIR
90
91         # Install dpkg-deb package for create repository in folder
92         # Install software-properties-common to get add-apt-repository command
93         # Install apt-transport-https, ca-certificates, curl and gnupg-agent allowing apt to use a repository over HTTPS
94         apt-get update -y
95         apt-get install dpkg-dev apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y
96
97         # Add Docker's official GPG key:
98         curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
99         apt-key fingerprint 0EBFCD88
100
101         # Add docker repository
102         add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
103
104         # Temp fix of known bug
105         # https://bugs.launchpad.net/ubuntu/+source/aptitude/+bug/1543280
106         chown _apt $OFFLINE_REPO_DIR
107
108         # Download all packages from onap_deb.list via apt-get to repository folder
109         for i in $(cat ${PCKG_LIST_DIR}onap_deb.list | awk '{print $1}');do apt-get download $i -y; done
110         for i in $(cat ${PCKG_LIST_DIR}onap_deb.list | awk '{print $1}');
111         do
112             for depends in $(apt-cache depends $i | grep -E 'Depends' | cut -d ':' -f 2,3 | sed -e s/'<'/''/ -e s/'>'/''/);
113                 do apt-get download $depends -y;
114             done;
115         done
116
117         # In repository folder create gz package with deb packages
118         dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz
119     ;;
120
121     rhel)
122         # Install createrepo package for create repository in folder
123         # and yum-utils due to yum-config-manager for adding docker repository
124         yum install createrepo yum-utils -y
125
126         # Add official docker repository
127         yum-config-manager --add-repo=https://download.docker.com/linux/centos/7/x86_64/stable/
128
129         # Download all packages from onap_rpm.list via yumdownloader to repository folder
130         for i in $(cat ${PCKG_LIST_DIR}onap_rpm.list | awk '{print $1}');do yumdownloader --resolve --downloadonly --destdir=${OFFLINE_REPO_DIR} $i -y; done
131
132         # In repository folder create repositor
133         createrepo $OFFLINE_REPO_DIR
134     ;;
135
136     *)
137         echo "Unknown type of linux distribution."
138         exit 1
139     ;;
140 esac