This commit is contained in:
2022-01-24 20:21:59 +02:00
commit 651f3ff412
12 changed files with 772 additions and 0 deletions

35
.gitignore vendored Normal file
View File

@@ -0,0 +1,35 @@
# ---- IntelliJ IDEA
*.iws
*.iml
*.ipr
.idea/
out/
# ---- VS Code
.vscode/
*.code-workspace
# ---- Eclipse
.classpath
.project
.settings
.externalToolBuilders
# ---- Mac OS X
.DS_Store
# Thumbnails
._*
# Files that might appear on external disk
.Spotlight-V100
.Trashes
# ---- Windows
# Windows image file caches
Thumbs.db
# Folder config file
Desktop.ini
# ---- Linux
.directory
*.zip

61
9-comm/Dockerfile Normal file
View File

@@ -0,0 +1,61 @@
FROM alpine:3.14
ENV LANG='en_US.UTF-8' \
LANGUAGE='en_US:en' \
LC_ALL='en_US.UTF-8'
#
# SonarQube setup
#
ARG SONARQUBE_VERSION=9.2.4.50792
ARG SONARQUBE_ZIP_URL=https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-${SONARQUBE_VERSION}.zip
ENV JAVA_HOME='/usr/lib/jvm/java-11-openjdk' \
PATH="/opt/java/openjdk/bin:$PATH" \
SONARQUBE_HOME=/opt/sonarqube \
SONAR_VERSION="${SONARQUBE_VERSION}" \
SQ_DATA_DIR="/opt/sonarqube/data" \
SQ_EXTENSIONS_DIR="/opt/sonarqube/extensions" \
SQ_LOGS_DIR="/opt/sonarqube/logs" \
SQ_TEMP_DIR="/opt/sonarqube/temp"
RUN set -eux; \
addgroup -S -g 1000 sonarqube; \
adduser -S -D -u 1000 -G sonarqube sonarqube; \
apk add --no-cache --virtual build-dependencies gnupg unzip curl; \
apk add --no-cache bash su-exec ttf-dejavu openjdk11-jre; \
# pub 2048R/D26468DE 2015-05-25
# Key fingerprint = F118 2E81 C792 9289 21DB CAB4 CFCA 4A29 D264 68DE
# uid sonarsource_deployer (Sonarsource Deployer) <infra@sonarsource.com>
# sub 2048R/06855C1D 2015-05-25
echo "networkaddress.cache.ttl=5" >> "${JAVA_HOME}/conf/security/java.security"; \
sed --in-place --expression="s?securerandom.source=file:/dev/random?securerandom.source=file:/dev/urandom?g" "${JAVA_HOME}/conf/security/java.security"; \
for server in $(shuf -e ha.pool.sks-keyservers.net \
hkp://p80.pool.sks-keyservers.net:80 \
keyserver.ubuntu.com \
hkp://keyserver.ubuntu.com:80 \
pgp.mit.edu) ; do \
gpg --batch --keyserver "${server}" --recv-keys 679F1EE92B19609DE816FDE81DB198F93525EC1A && break || : ; \
done; \
mkdir --parents /opt; \
cd /opt; \
curl --fail --location --output sonarqube.zip --silent --show-error "${SONARQUBE_ZIP_URL}"; \
curl --fail --location --output sonarqube.zip.asc --silent --show-error "${SONARQUBE_ZIP_URL}.asc"; \
gpg --batch --verify sonarqube.zip.asc sonarqube.zip; \
unzip -q sonarqube.zip; \
mv "sonarqube-${SONARQUBE_VERSION}" sonarqube; \
rm sonarqube.zip*; \
rm -rf ${SONARQUBE_HOME}/bin/*; \
chown -R sonarqube:sonarqube ${SONARQUBE_HOME}; \
# this 777 will be replaced by 700 at runtime (allows semi-arbitrary "--user" values)
chmod -R 777 "${SQ_DATA_DIR}" "${SQ_EXTENSIONS_DIR}" "${SQ_LOGS_DIR}" "${SQ_TEMP_DIR}"; \
apk del --purge build-dependencies;
COPY --chown=sonarqube:sonarqube run.sh sonar.sh ${SONARQUBE_HOME}/bin/
WORKDIR ${SONARQUBE_HOME}
EXPOSE 9000
STOPSIGNAL SIGINT
ENTRYPOINT ["/opt/sonarqube/bin/run.sh"]
CMD ["/opt/sonarqube/bin/sonar.sh"]

59
9-comm/run.sh Normal file
View File

@@ -0,0 +1,59 @@
#!/usr/bin/env bash
set -euo pipefail
declare -a sq_opts=()
set_prop_from_deprecated_env_var() {
if [ "$2" ]; then
sq_opts+=("-D$1=$2")
fi
}
# if nothing is passed, assume we want to run sonarqube server
if [ "$#" == 0 ]; then
set -- /opt/sonarqube/bin/sonar.sh
fi
# if first arg looks like a flag, assume we want to run sonarqube server with flags
if [ "${1:0:1}" = '-' ]; then
set -- /opt/sonarqube/bin/sonar.sh "$@"
fi
if [[ "$1" = '/opt/sonarqube/bin/sonar.sh' ]]; then
chown -R "$(id -u):$(id -g)" "${SQ_DATA_DIR}" "${SQ_EXTENSIONS_DIR}" "${SQ_LOGS_DIR}" "${SQ_TEMP_DIR}" 2>/dev/null || :
chmod -R 700 "${SQ_DATA_DIR}" "${SQ_EXTENSIONS_DIR}" "${SQ_LOGS_DIR}" "${SQ_TEMP_DIR}" 2>/dev/null || :
# Allow the container to be started with `--user`
if [[ "$(id -u)" = '0' ]]; then
chown -R sonarqube:sonarqube "${SQ_DATA_DIR}" "${SQ_EXTENSIONS_DIR}" "${SQ_LOGS_DIR}" "${SQ_TEMP_DIR}"
echo "Dropping Privileges"
exec su-exec sonarqube "$0" "$@"
fi
#
# Deprecated way to pass settings to SonarQube that will be removed in future versions.
# Please use environment variables (https://docs.sonarqube.org/latest/setup/environment-variables/)
# instead to customize SonarQube.
#
while IFS='=' read -r envvar_key envvar_value
do
if [[ "$envvar_key" =~ sonar.* ]] || [[ "$envvar_key" =~ ldap.* ]]; then
sq_opts+=("-D${envvar_key}=${envvar_value}")
fi
done < <(env)
#
# Deprecated environment variable mapping that will be removed in future versions.
# Please use environment variables from https://docs.sonarqube.org/latest/setup/environment-variables/
# instead of using these 4 environment variables below.
#
set_prop_from_deprecated_env_var "sonar.jdbc.username" "${SONARQUBE_JDBC_USERNAME:-}"
set_prop_from_deprecated_env_var "sonar.jdbc.password" "${SONARQUBE_JDBC_PASSWORD:-}"
set_prop_from_deprecated_env_var "sonar.jdbc.url" "${SONARQUBE_JDBC_URL:-}"
set_prop_from_deprecated_env_var "sonar.web.javaAdditionalOpts" "${SONARQUBE_WEB_JVM_OPTS:-}"
if [ ${#sq_opts[@]} -ne 0 ]; then
set -- "$@" "${sq_opts[@]}"
fi
fi
exec "$@"

2
9-comm/sonar.sh Normal file
View File

@@ -0,0 +1,2 @@
#!/usr/bin/env bash
exec java -jar lib/sonar-application-"${SONAR_VERSION}".jar -Dsonar.log.console=true "$@"

165
LICENSE Normal file
View File

@@ -0,0 +1,165 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.

6
NOTICE.txt Normal file
View File

@@ -0,0 +1,6 @@
docker-sonarqube
Copyright (C) 2015-2019 SonarSource SA
mailto:info AT sonarsource DOT com
This product includes software developed at
SonarSource (http://www.sonarsource.com/).

0
README.md Normal file
View File

62
build-and-run.sh Normal file
View File

@@ -0,0 +1,62 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
port=9000
print_usage() {
cat << EOF
usage: $0 [IMAGE...]
examples:
$0
$0 7.4-community
$0 7.4-community-alpine
EOF
}
warn() {
echo "[warn] $@" >&2
}
fatal() {
echo "[error] $@" >&2
exit 1
}
require() {
local prog missing=()
for prog; do
if ! type "$prog" &>/dev/null; then
missing+=("$prog")
fi
done
[[ ${#missing[@]} = 0 ]] || fatal "could not find required programs on the path: ${missing[@]}"
}
require docker
for arg; do
if [[ $arg == "-h" ]] || [[ $arg == "--help" ]]; then
print_usage
exit
fi
done
if [[ $# = 0 ]]; then
print_usage
exit 1
fi
image=$1
image=${image%/}
if ! [[ -d "$image" ]]; then
warn "not a valid image, directory does not exist: $image"
exit 1
fi
name=sqtest:$image
docker build -t "$name" -f "$image/Dockerfile" "$PWD/$image"
docker run -p $port:9000 "$name"

59
examples.md Normal file
View File

@@ -0,0 +1,59 @@
# Examples
This section provides examples on how to run SonarQube server in a container:
- using [docker commands](#run-sonarqube-using-docker-commands)
- using [docker-compose](#run-sonarqube-using-docker-compose)
To analyze a project check our [scanner docs](https://docs.sonarqube.org/latest/analysis/overview/).
## Run SonarQube using docker commands
Before you start SonarQube, we recommend creating volumes to store SonarQube data, logs, temporary data and extensions. If you don't do that, you can loose them when you decide to update to newer version of SonarQube or upgrade to a higher SonarQube edition. Commands to create the volumes:
```bash
$> docker volume create --name sonarqube_data
$> docker volume create --name sonarqube_extensions
$> docker volume create --name sonarqube_logs
$> docker volume create --name sonarqube_temp
```
After that you can start the SonarQube server (this example uses the Community Edition):
```bash
$> docker run \
-v sonarqube_data:/opt/sonarqube/data \
-v sonarqube_extensions:/opt/sonarqube/extensions \
-v sonarqube_logs:/opt/sonarqube/logs \
-v sonarqube_temp:/opt/sonarqube/temp \
--name="sonarqube" -p 9000:9000 sonarqube:8.2
```
The above command starts SonarQube with an embedded database. We recommend starting the instance with a separate database
by providing `SONAR_JDBC_URL`, `SONAR_JDBC_USERNAME` and `SONAR_JDBC_PASSWORD` like this:
```bash
$> docker run \
-v sonarqube_data:/opt/sonarqube/data \
-v sonarqube_extensions:/opt/sonarqube/extensions \
-v sonarqube_logs:/opt/sonarqube/logs \
-v sonarqube_temp:/opt/sonarqube/temp \
-e SONAR_JDBC_URL="..." \
-e SONAR_JDBC_USERNAME="..." \
-e SONAR_JDBC_PASSWORD="..." \
--name="sonarqube" -p 9000:9000 sonarqube:8.2
```
## Run SonarQube using Docker Compose
### Requirements
* Docker Engine 1.10.1+
* Docker Compose 1.6.0+
### SonarQube with Postgres:
Go to [this directory](example-compose-files/sq-with-h2) to run SonarQube in development mode or [this directory](example-compose-files/sq-with-postgres) to run both SonarQube and PostgreSQL. Then run [docker-compose](https://github.com/docker/compose):
```bash
$ docker-compose up
```
To restart SonarQube container (for example after upgrading or installing a plugin):
```bash
$ docker-compose restart sonarqube
```

103
run-public-image-tests.sh Normal file
View File

@@ -0,0 +1,103 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
port=9000
info() {
echo "[info] $@"
}
warn() {
echo "[warn] $@" >&2
}
fatal() {
echo "[error] $@" >&2
exit 1
}
require() {
local prog missing=()
for prog; do
if ! type "$prog" &>/dev/null; then
missing+=("$prog")
fi
done
[[ ${#missing[@]} = 0 ]] || fatal "could not find reqired programs on the path: ${missing[@]}"
}
wait_for_sonarqube() {
local image=$1 i web_up=no sonarqube_up=no
for ((i = 0; i < 10; i++)); do
info "$image: waiting for web server to start ..."
if curl -sI localhost:$port | grep '^HTTP/.* 200'; then
web_up=yes
break
fi
sleep 5
done
[[ $web_up = yes ]] || return 1
for ((i = 0; i < 10; i++)); do
info "$image: waiting for sonarqube to be ready ..."
if curl -s localhost:$port/api/system/status | grep '"status":"UP"'; then
sonarqube_up=yes
break
fi
sleep 10
done
[[ "$sonarqube_up" = yes ]]
}
sanity_check_image() {
local image=$1 id result
docker system prune -fa
docker pull ${image}
id=$(docker run -d -p ${port}:9000 "$image")
info "$image: container started: $id"
if wait_for_sonarqube "$image"; then
info "$image: OK !"
result=ok
else
warn "$image: could not confirm service started"
result=failure
fi
info "$image: stopping container: $id"
docker container stop "$id"
[[ $result == ok ]]
}
require curl docker
results=()
if sanity_check_image "sonarqube"; then
results+=("success")
else
results+=("failure")
fi
echo
failures=0
echo "sonarqube => ${results[0]}"
if [[ ${results[0]} != success ]]; then
((failures++)) || :
fi
[[ ${failures} = 0 ]]

171
run-tests.sh Normal file
View File

@@ -0,0 +1,171 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
port=9000
print_usage() {
cat << EOF
usage: $0 [IMAGE...]
examples:
$0 7.6-community
EOF
}
info() {
echo "[info] $@"
}
warn() {
echo "[warn] $@" >&2
}
fatal() {
echo "[error] $@" >&2
exit 1
}
require() {
local prog missing=()
for prog; do
if ! type "$prog" &>/dev/null; then
missing+=("$prog")
fi
done
[[ ${#missing[@]} = 0 ]] || fatal "could not find reqired programs on the path: ${missing[@]}"
}
wait_for_sonarqube() {
local image=$1 i web_up=no sonarqube_up=no
for ((i = 0; i < 10; i++)); do
info "$image: waiting for web server to start ..."
if curl -sI localhost:$port | grep '^HTTP/.* 200'; then
web_up=yes
break
fi
sleep 5
done
[[ $web_up = yes ]] || return 1
for ((i = 0; i < 20; i++)); do
info "$image: waiting for sonarqube to be ready ..."
if curl -s localhost:$port/api/system/status | grep '"status":"UP"'; then
sonarqube_up=yes
break
fi
sleep 10
done
[[ "$sonarqube_up" = yes ]]
}
wait_for_sonarqube_dce() {
local image=$1 i web_up=no sonarqube_up=no
for ((i = 0; i < 80; i++)); do
info "$image: waiting for web server to start ..."
if curl -sI localhost:$port | grep '^HTTP/.* 200'; then
web_up=yes
break
fi
sleep 5
done
[[ $web_up = yes ]] || return 1
for ((i = 0; i < 80; i++)); do
info "$image: waiting for sonarqube to be ready ..."
if curl -s localhost:$port/api/system/status | grep '"status":"UP"'; then
sonarqube_up=yes
break
fi
sleep 10
done
[[ "$sonarqube_up" = yes ]]
}
sanity_check_image() {
local image=$1 id result
local test_case=$2
if [[ $2 == docker ]]; then
id=$(docker run -d -p $port:9000 "$image")
info "$image: container started: $id"
if wait_for_sonarqube "$image"; then
info "$image: OK !"
result=ok
else
warn "$image: could not confirm service started"
result=failure
fi
info "$image: stopping container: $id"
docker container stop "$id"
[[ $result == ok ]]
elif [ $2 == docker-compose ]; then
if [[ $1 =~ "8" ]]; then
_test_compose_path="tests/8/dce-compose-test"
elif [[ $1 =~ "9" ]]; then
_test_compose_path="tests/9/dce-compose-test"
fi
cd $_test_compose_path
export PORT=$port
docker-compose up -d --scale sonarqube=0
sleep 60
docker-compose up -d --scale sonarqube=1
if wait_for_sonarqube_dce "$image"; then
info "$image: OK !"
result=ok
else
warn "$image: could not confirm service started"
result=failure
fi
info "$image: stopping container stack"
docker-compose stop
[[ $result == ok ]]
fi
}
require curl docker
for arg; do
if [[ $arg == "-h" ]] || [[ $arg == "--help" ]]; then
print_usage
exit
fi
done
if [[ $# = 0 ]]; then
warn "at least one image as parameter is required"
exit
fi
image=($1)
test_case=($2)
results=()
if sanity_check_image "$image" "$test_case"; then
results+=("success")
else
results+=("failure")
fi
failures=0
echo "${image} => ${results}"
if [[ ${results} != success ]]; then
((failures++))
fi
[[ $failures = 0 ]]

49
update.sh Normal file
View File

@@ -0,0 +1,49 @@
#!/bin/bash
# Enable globstar for Searching recursively
shopt -s globstar
# Reset the Option Index in case getopts has been used previously in the same shell.
OPTIND=1
function show_help() {
echo "update.sh help"
echo ""
echo "This Script will update a given Version in all Dockerfiles present under the current directory"
echo "If the old version can not be found, it does nothing"
echo ""
echo "usage:"
echo "update.sh <old version> <new version>"
echo ""
echo "example:"
echo "update.sh -o 8.9.1.44547 -n 9.0.0.12345"
exit 0
}
##########
## Main ##
##########
OLD_VERSION=""
NEW_VERSION=""
while getopts ":h:o:n:" o; do
case "${o}" in
o)
OLD_VERSION=${OPTARG}
;;
n)
NEW_VERSION=${OPTARG}
;;
h)
show_help
;;
*)
show_help
;;
esac
done
shift $((OPTIND-1))
for i in ./**/Dockerfile; do
sed -i "s/${OLD_VERSION}/${NEW_VERSION}/g" $i
done