blob: 1532c6144c4abfd2b214d1ca0dffc2b9e6ad4c41 [file] [log] [blame]
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +01001#! /usr/bin/env bash
Petr Ospalý03e61242019-01-03 16:54:50 +01002
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +01003# COPYRIGHT NOTICE STARTS HERE
4#
5# Copyright 2018 © Samsung Electronics Co., Ltd.
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19# COPYRIGHT NOTICE ENDS HERE
Petr Ospalý03e61242019-01-03 16:54:50 +010020
21
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +010022# fail fast
23set -e
Petr Ospalý03e61242019-01-03 16:54:50 +010024
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +010025# OS check
26. /etc/os-release
27OS_ID="${ID}"
Petr Ospalý03e61242019-01-03 16:54:50 +010028
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +010029case "$OS_ID" in
30 centos)
31 ;;
32 rhel)
33 ;;
34 ubuntu)
35 ;;
36 *)
37 echo This OS is not supported: $OS_ID
38 exit 1
39 ;;
40esac
Petr Ospalý03e61242019-01-03 16:54:50 +010041
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +010042# boilerplate
43RELATIVE_PATH=./ # relative path from this script to 'common-functions.sh'
44if [ "$IS_COMMON_FUNCTIONS_SOURCED" != YES ] ; then
45 SCRIPT_DIR=$(dirname "${0}")
46 LOCAL_PATH=$(readlink -f "$SCRIPT_DIR")
47 . "${LOCAL_PATH}"/"${RELATIVE_PATH}"/common-functions.sh
48fi
Petr Ospalý03e61242019-01-03 16:54:50 +010049
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +010050#
51# local functions
52#
Petr Ospalý03e61242019-01-03 16:54:50 +010053
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +010054start_nexus() {
55 echo "** Starting nexus **"
56 if [[ -z "$NEXUS_DATA" ]]; then
57 echo "Nexus data env is not set"
58 exit -3
59 fi
Petr Ospalý03e61242019-01-03 16:54:50 +010060
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +010061 # valid for case of fresh nexus deployment
62 # data are inserted in later phases
63 mkdir -p $NEXUS_DATA
64 # hardening
65 chmod a+wrX $NEXUS_DATA
66 chown -R 200:200 $NEXUS_DATA
Petr Ospalý03e61242019-01-03 16:54:50 +010067
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +010068 docker rm -f nexus 1> /dev/null 2>&1 || true
Petr Ospalý03e61242019-01-03 16:54:50 +010069
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +010070 docker run -d --name nexus\
71 --restart unless-stopped \
72 -v $NEXUS_DATA:/nexus-data:rw \
73 sonatype/nexus3
Petr Ospalý03e61242019-01-03 16:54:50 +010074
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +010075 echo "** Creating docker network **"
76 docker network create nexus_network
77 docker network connect nexus_network nexus
78}
Petr Ospalý03e61242019-01-03 16:54:50 +010079
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +010080start_nginx() {
81 echo "** Starting reverse proxy - nginx **"
Petr Ospalý03e61242019-01-03 16:54:50 +010082
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +010083 docker rm -f nginx 1> /dev/null 2>&1 || true
84 mkdir -p $NGINX_HTTP_DIR/repo.install-server
Petr Ospalý03e61242019-01-03 16:54:50 +010085
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +010086 mkdir -p "$NGINX_HTTP_DIR/repo.install-server"
Petr Ospalý03e61242019-01-03 16:54:50 +010087
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +010088 docker run -d -p 80:80 -p 443:443 -p 10001:443 \
89 --name nginx \
90 --network nexus_network \
91 -v $GEN_CFG_PATH/nginx.conf:/etc/nginx/nginx.conf:ro \
92 -v $CERTS_TARGET_PATH:/etc/nginx/certs:ro \
93 -v $GIT_REPOS:/srv/git:rw \
94 -v $NGINX_LOG_DIR:/var/log/nginx:rw \
95 -v $NGINX_HTTP_DIR:/srv/http:ro \
96 -v $RHEL_REPO:/srv/http/repo.install-server:ro \
97 --restart unless-stopped \
98 own_nginx
99}
Petr Ospalý03e61242019-01-03 16:54:50 +0100100
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100101patch_cert() {
102 file=$1
103 cp "$APROJECT_DIR/cfg/$file" "$GEN_CFG_PATH/$file"
Petr Ospalý03e61242019-01-03 16:54:50 +0100104# sed "s#countryName =.*#countryName = $CERT_COUNTRY#" "$APROJECT_DIR/cfg/$file" > $GEN_CFG_PATH/$file
105# sed "s#localityName =.*#localityName = $CERT_LOCALITY#" "$APROJECT_DIR/cfg/$file" > $GEN_CFG_PATH/$file
106# sed "s#organizationName =.*#organizationName = $CERT_ORGANIZATION#" "$APROJECT_DIR/cfg/$file" > $GEN_CFG_PATH/$file
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100107}
Petr Ospalý03e61242019-01-03 16:54:50 +0100108
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100109patch_conf_files() {
110 # patch nexus and root cert
111 patch_cert nexus_cert.cnf
112 patch_cert cacert.cnf
Petr Ospalý03e61242019-01-03 16:54:50 +0100113
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100114 # patch nexus v3 ext cert
115 sed "s#nexus.student12#$NEXUS_FQDN#" "$APROJECT_DIR/cfg/v3.ext" > $GEN_CFG_PATH/v3.ext
Petr Ospalý03e61242019-01-03 16:54:50 +0100116
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100117 #patch nginx.conf
118 sed "s#nexus.student12#$NEXUS_FQDN#" "$APROJECT_DIR/cfg/nginx.conf" > $GEN_CFG_PATH/nginx.conf
119}
Petr Ospalý03e61242019-01-03 16:54:50 +0100120
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100121#
122# body
123#
Petr Ospalý03e61242019-01-03 16:54:50 +0100124
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100125message info "Nexus will be installed into this directory: $(pwd)"
Petr Ospalý03e61242019-01-03 16:54:50 +0100126
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100127if ! [ -f ./local_repo.conf ]; then
128 printf "[?] > Do you want continue? (if no, hit CTRL+C): "
129 read x
130fi
Petr Ospalý03e61242019-01-03 16:54:50 +0100131
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100132message info "Reading configuration"
133get_configuration
Petr Ospalý03e61242019-01-03 16:54:50 +0100134
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100135mkdir -p "$CERTS_TARGET_PATH"
136mkdir -p "$NGINX_LOG_DIR"
137mkdir -p "$GEN_CFG_PATH"
138if [ "$IS_SELF_EXTRACT" = YES ] ; then
139 message info "Now I will untar the resources"
140 message info "This may take a long time..."
141 sleep 3s
142 may_self_extract
143fi
Petr Ospalý03e61242019-01-03 16:54:50 +0100144
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100145#
146echo "Cleanup docker (if installed)"
147docker rm -f nginx 1> /dev/null 2>&1 || true
148docker rm -f nexus 1> /dev/null 2>&1 || true
Petr Ospalý03e61242019-01-03 16:54:50 +0100149
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100150install_files
151install_packages "$OS_ID"
152setup_vnc_server
Petr Ospalý03e61242019-01-03 16:54:50 +0100153
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100154update_hosts
Petr Ospalý03e61242019-01-03 16:54:50 +0100155
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100156# TODO
157#check_dependencies
Petr Ospalý03e61242019-01-03 16:54:50 +0100158
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100159echo "Restarting dnsmasq"
Petr Ospalý03e61242019-01-03 16:54:50 +0100160# TODO dnsmasq config?
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100161systemctl enable dnsmasq
162systemctl restart dnsmasq
Petr Ospalý03e61242019-01-03 16:54:50 +0100163
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100164echo "** Generating config files to $GEN_CFG_PATH **"
165echo "Configure ssl certificates"
Petr Ospalý03e61242019-01-03 16:54:50 +0100166
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100167patch_conf_files
168create_root_CA
Petr Ospalý03e61242019-01-03 16:54:50 +0100169
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100170# create selfinstall CA cert
171$BASH_SCRIPTS_DIR/tools/create_si_cacert_pkg.sh
172# run generated file
173./install_cacert.sh
Petr Ospalý03e61242019-01-03 16:54:50 +0100174
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100175create_cert "nexus"
Petr Ospalý03e61242019-01-03 16:54:50 +0100176
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100177echo "** Certificates finished **"
Petr Ospalý03e61242019-01-03 16:54:50 +0100178
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100179update_docker_cfg
Petr Ospalý03e61242019-01-03 16:54:50 +0100180
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100181echo "Restarting docker"
182systemctl enable docker
183systemctl restart docker
Petr Ospalý03e61242019-01-03 16:54:50 +0100184
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100185update_firewall
Petr Ospalý03e61242019-01-03 16:54:50 +0100186
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100187set +e
Petr Ospalý03e61242019-01-03 16:54:50 +0100188
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100189echo "** Loading images **"
190docker load -i $RESOURCES_DIR/offline_data/docker_images_infra/sonatype_nexus3_latest.tar
191docker load -i $RESOURCES_DIR/offline_data/docker_images_infra/own_nginx_latest.tar
Petr Ospalý03e61242019-01-03 16:54:50 +0100192
Piotr Perzanowski4e3b2282018-12-18 15:51:39 +0100193start_nexus
194start_nginx