summaryrefslogtreecommitdiffstats
path: root/build/docker-entrypoint.sh
blob: 81dcd84c81ba940cfb15c8d8c50e47a94b9c8b4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env bash

set -eo pipefail

# Set distribution family
distro_type=$(cat /etc/*-release | grep -w "ID" | awk -F'=' '{ print $2 }' | tr -d '"')
case "$distro_type" in
        ubuntu)
                distro_type="ubuntu"
        ;;
        rhel|centos)
                distro_type="rhel"
        ;;
        *)
                echo "Unknown type of linux distribution."
                exit 1
        ;;
esac

# Target path for created repository
OFFLINE_REPO_DIR=""

# Path to directory containing onap_rpm.list and onap_deb.list files
PCKG_LIST_DIR=""

# Path to additional packages lists
ADD_LIST_DIR=""

# Use cache by default
drop_cache=false

# Show help
help () {
cat <<EOF
Docker entrypoint script for creating RPM/DEB repository based on container platform type

usage: create-repo.sh [OPTION]...

  -d | --directory              target repository path
  -l | --list                   input rpm/deb list directory
  -a | --additional-list        additional packages list; can be used multiple times
  -p | --packages-lists-path    other additional packages lists
  -r | --drop-cache             remove cached packages (use package cache by default)
  -h | --help                   show this help

Both paths have to be set with shared volume between
container and the host. Default path in container is: /tmp/
Repository will be created at: /<path>/resources/pkg/rhel/
RMP/DEB list is stored at: ./data_list/
EOF
}

# Getting input parameters
if [[ $# -eq 0 ]] ; then
    help # show help
    exit 0
fi
while [[ $# -gt 0 ]]
do
    case "$1" in
        -h|--help)
            # Help parameter
            help # show help
            exit
            ;;
        -d|--directory)
            # Directory parameter
            # Set target reposity path
            OFFLINE_REPO_DIR="$2"
            shift
            ;;
        -l|--list)
            # List parameter
            # Set path containing onap_rpm.list or onap_deb.list file
            PCKG_LIST_DIR="$2"
            shift
            ;;
        -p|--packages-lists-path)
            # Path parameter
            # Set path for additional packages lists
            ADD_LIST_DIR="$2"
            shift
            ;;
        -a|--additional-list)
            # Array of additional packages lists
            ADDITIONAL_LISTS+=("$2")
            shift
            ;;
        -r|--drop-cache)
            # Set flag to clean cache
            drop_cache=true
            ;;
        *)
            # unknown option
            help # show help
            exit
            ;;
    esac
    shift
done

# Testing if directory parameter was used
# If not variable is set to /tmp/repo by default
if test -z "$OFFLINE_REPO_DIR"
then
    OFFLINE_REPO_DIR="/tmp/repo/"
fi

# Testing if list parameter was used
# If not variable is set to default value /tmp/offline/data-list
if test -z "$PCKG_LIST_DIR"
then
    PCKG_LIST_DIR="/tmp/offline/data_list"
fi

# Testing if additional packages list parameter was used
# If not variable is set to default value /tmp/additional-lists
if test -z "$PCKG_LIST_DIR"
then
    PCKG_LIST_DIR="/tmp/additional-lists"
fi

# Clean target repo dir if --drop-cache set
if ${drop_cache};
then
    rm -rf ${OFFLINE_REPO_DIR}/*
fi

case "$distro_type" in
    ubuntu)
        # Change current working dir
        pushd $OFFLINE_REPO_DIR

        # Install dpkg-deb package for create repository in folder
        # Install software-properties-common to get add-apt-repository command
        # Install apt-transport-https, ca-certificates, curl and gnupg-agent allowing apt to use a repository over HTTPS
        apt-get update -y
        apt-get install dpkg-dev apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y

        # Add Docker's official GPG key:
        curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
        apt-key fingerprint 0EBFCD88

        # Add docker repository
        add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

        # Temp fix of known bug
        # https://bugs.launchpad.net/ubuntu/+source/aptitude/+bug/1543280
        chown _apt $OFFLINE_REPO_DIR

        # Create tmp file for package list
        list_file=$(mktemp)

        # Enumerate packages that are already downloaded
        for package in $(cat ${PCKG_LIST_DIR}/onap_deb.list);
        do
            # If package name contains explicit version info cut the version string off for further processing
            p=$(echo $package |sed -r 's/=.*//')
            # Add package to download list only if it's not already there
            if [ $(ls ${p}_*.deb 2>/dev/null | wc -l) -eq 0 ];
            then
                echo ${package} >> ${list_file}
            fi
        done

        # Download all packages via apt-get to repository folder
        for i in $(cat ${list_file});do echo apt-get download $i -y; done
        for i in $(cat ${list_file});
            do
                for depends in $(apt-cache depends $i | grep -E 'Depends' | grep -v 'Depends:.*>$' | cut -d ':' -f 2,3 | sed -e s/'<'/''/ -e s/'>'/''/);
                do
echo                    apt-get download $depends -y;
                done;
            done

        # Download all packages with dependencies from all additional packages lists via apt-get to repository folder
        if ! [ ${#ADDITIONAL_LISTS[@]} -eq 0 ]; then
            for list in ${ADDITIONAL_LISTS[@]}
            do

                # Create tmp file for package list
                list_file=$(mktemp)

                # Enumerate packages that are already downloaded
                for package in $(cat ${ADD_LIST_DIR}/${list});
                do
                    # If package name contains explicit version info cut the version string off for further processing
                    p=$(echo $package |sed -r 's/=.*//')
                    # Add package to download list only if it's not already there
                    if [ $(ls ${p}_*.deb 2>/dev/null | wc -l) -eq 0 ];
                    then
                        echo ${package} >> ${list_file}
                    fi
                done

                for i in $(cat ${list_file});do apt-get download $i -y; done
                for i in $(cat ${list_file});
                do
                    for depends in $(apt-cache depends $i | grep -E 'Depends' | cut -d ':' -f 2,3 | sed -e s/'<'/''/ -e s/'>'/''/);
                        do apt-get download $depends -y;
                    done;
                done
            done
        fi

        # In repository folder create gz package with deb packages
        dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz
    ;;

    rhel)
        # Install createrepo package for create repository in folder,
        # yum-utils due to yum-config-manager for adding docker repository
        # and epel-release for additional packages (like jq etc.)
        yum install createrepo yum-utils epel-release -y

        # Add official docker repository
        yum-config-manager --add-repo=https://download.docker.com/linux/centos/7/x86_64/stable/

        # Create tmp file for package list
        list_file=$(mktemp)

        # Enumerate packages that are already downloaded
        for package in $(cat ${PCKG_LIST_DIR}/onap_rpm.list);
        do
            # Add package to download list only if it's not already there
            if [ ! -f ${OFFLINE_REPO_DIR}/${package}.rpm ];
            then
                echo ${package} >> ${list_file}
            fi
        done

        # Download all packages from onap_rpm.list via yumdownloader to repository folder
        for i in $(cat ${list_file});do yumdownloader --resolve --downloadonly --destdir=${OFFLINE_REPO_DIR} $i -y; done

        # Download all packages from all additional packages lists via yumdownloader to repository folder
        if ! [ ${#ADDITIONAL_LISTS[@]} -eq 0 ]; then
            for list in ${ADDITIONAL_LISTS[@]}
            do
                # Create tmp file for additional package list
                list_file=$(mktemp)
                # Enumerate packages that are already downloaded
                for package in $(cat ${ADD_LIST_DIR}/${list});
                do
                    # Add package to download list only if it's not already there
                    if [ ! -f ${OFFLINE_REPO_DIR}/${package}.rpm ];
                    then
                        echo ${package} >> ${list_file}
                    fi
                done

                for i in $(cat ${list_file});
                do
                    yumdownloader --resolve --downloadonly --destdir=${OFFLINE_REPO_DIR} $i -y
                done
            done
        fi

        # Create repository
        createrepo $OFFLINE_REPO_DIR
    ;;

    *)
        echo "Unknown type of linux distribution."
        exit 1
    ;;
esac