Skip to content

Commit 060273c

Browse files
committed
Added scripts.
Added readme. Added ci/cd.
0 parents  commit 060273c

18 files changed

+1348
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
public
2+
binaries

.gitlab-ci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
include:
2+
- project: 'singletonsd/pipelines/common'
3+
file: '/src/.gitlab-ci-scripts.yml'
4+
5+
stages:
6+
- test
7+
- build
8+
- deploy

README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# SINGLETON SD - SCRIPTS - DATABASES
2+
3+
This project contains Linux bash scripts to use locally or in gitlab-ci templates to interact with liquibase database projects.
4+
5+
> The **main repository** is hosted in [gitlab.com/singletonsd/scripts/databases](https://gitlab.com/singletonsd/scripts/databases.git) but it is automaticaly mirrored to [github.com/singletonsd](https://github.com/singletonsd/scripts-databases.git), [github.com/patoperpetua](https://github.com/patoperpetua/scripts-databases.git) and to [gitlab.com/patoperpetua](https://gitlab.com/patoperpetua/scripts-databases.git). If you are in the Github page it may occur that is not updated to the last version.
6+
7+
## AVAILABLE SCRIPTS
8+
9+
<!-- TODO: -->
10+
### GITLAB-CI LINT TEST
11+
12+
You can test your .gitlab-ci.yml files by executing the following:
13+
14+
```bash
15+
curl -s https://singletonsd.gitlab.io/scripts/gitlab-ci/latest/gitlab-ci_lint_test_standalone.sh | bash /dev/stdin
16+
```
17+
18+
That script contains the following options:
19+
20+
```bash
21+
-h | --help: display help.
22+
-o | --only: the name of the file or folder to test.
23+
```
24+
25+
It can be downloaded by:
26+
27+
```bash
28+
curl -o gitlab-ci_lint_test_standalone.sh -L https://singletonsd.gitlab.io/scripts/gitlab-ci/latest/gitlab-ci_lint_test_standalone.sh
29+
```
30+
31+
## DOWNLOAD
32+
33+
All scripts are available also inside a zip file under [this url](https://singletonsd.gitlab.io/scripts/databases/latest/scripts.zip). Or you can execute the following to download:
34+
35+
```bash
36+
mkdir -p binaries && \
37+
curl -o binaries/scripts.zip -L https://singletonsd.gitlab.io/scripts/databases/latest/scripts.zip && \
38+
cd binaries && unzip scripts.zip && mv src/* . && rm -r src && rm -r scripts.zip && cd ..
39+
```
40+
41+
## GIT HOOK
42+
43+
You can setup gitlab lint tester to be run before a commit. To do that just execute the following script under your git repository:
44+
45+
```bash
46+
curl -s https://singletonsd.gitlab.io/scripts/gitlab-ci/latest/gitlab-ci_lint_hook_installer.sh | bash /dev/stdin
47+
```
48+
49+
## STRUCTURE
50+
51+
Master branch is setup as latest folder. To use an specific version, put the version name before the file name like:
52+
53+
```url
54+
https://singletonsd.gitlab.io/scripts/databases/latest/gitlab-ci_lint_test_standalone.sh
55+
https://singletonsd.gitlab.io/scripts/databases/develop/gitlab-ci_lint_test_standalone.sh
56+
https://singletonsd.gitlab.io/scripts/databases/v0.0.2/gitlab-ci_lint_test_standalone.sh
57+
```
58+
59+
## DOCUMENTATION
60+
61+
- [Shellcheck](https://github.com/koalaman/shellcheck)
62+
63+
## TODO
64+
65+
- [ ] Fix documentation.
66+
- [X] Use gitlab-ci template.
67+
- [ ] Create a git hook installer.
68+
- [ ] Create reusable scripts.
69+
- [ ] Verify scripts functions.
70+
71+
----------------------
72+
73+
© [Singleton SD](http://www.singletonsd.com), Italy, 2019.

assets/drop_schema.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP DATABASE IF EXISTS SAV;

src/docker_push_all.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
3+
# Web Page of BASH best practices https://kvz.io/blog/2013/11/21/bash-best-practices/
4+
#Exit when a command fails.
5+
set -o errexit
6+
#Exit when script tries to use undeclared variables.
7+
set -o nounset
8+
#The exit status of the last command that threw a non-zero exit code is returned.
9+
set -o pipefail
10+
11+
#Trace what gets executed. Useful for debugging.
12+
#set -o xtrace
13+
14+
# Set magic variables for current file & dir
15+
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
16+
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
17+
__base="$(basename "${__file}" .sh)"
18+
__root="$(cd "$(dirname "${__dir}")" && pwd)"
19+
20+
echo "Script name: ${__base}"
21+
echo "Executing at ${__root}"
22+
23+
if ! type "docker" &> /dev/null; then
24+
echo "Docker is not installed. Install it and then re launch"
25+
exit 1
26+
fi
27+
28+
function usage(){
29+
echo -e "First Argument: basename image."
30+
}
31+
32+
if [ -z "${BASENAME+x}" ]; then
33+
BASENAME=registry.gitlab.com/ravimosharksas/databases/global
34+
fi
35+
36+
if [ $# -eq 1 ]; then
37+
BASENAME=${1}
38+
fi
39+
40+
for filename in dist/mysql/*.sql; do
41+
filename=$(basename -- "$filename")
42+
filename="${filename%.*}"
43+
tag=${filename#"mysql-"}
44+
docker push "${BASENAME}:${tag}"
45+
done

src/docker_wait_for_container.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
3+
# Web Page of BASH best practices https://kvz.io/blog/2013/11/21/bash-best-practices/
4+
#Exit when a command fails.
5+
set -o errexit
6+
#Exit when script tries to use undeclared variables.
7+
set -o nounset
8+
#The exit status of the last command that threw a non-zero exit code is returned.
9+
set -o pipefail
10+
11+
#Trace what gets executed. Useful for debugging.
12+
#set -o xtrace
13+
14+
# Set magic variables for current file & dir
15+
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
16+
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
17+
__base="$(basename "${__file}" .sh)"
18+
__root="$(cd "$(dirname "${__dir}")" && pwd)"
19+
20+
echo "Script name: ${__base}"
21+
echo "Executing at ${__root}"
22+
23+
MYSQL_USER=${MYSQL_USER}
24+
if [ "${MYSQL_USER}" == "" ]; then
25+
MYSQL_USER=root
26+
fi
27+
28+
maxcounter=
29+
counter=1
30+
while ! mysql --protocol TCP -u"${MYSQL_USER}" -p"${MYSQL_ROOT_PASSWORD}" -e "show databases;" > /dev/null 2>&1; do
31+
sleep 1
32+
counter=$(("${counter}" + 1))
33+
if [ "${counter}" -gt "${maxcounter}" ]; then
34+
>&2 echo "We have been waiting for MySQL too long already; failing."
35+
exit 1
36+
fi;
37+
done
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env bash
2+
3+
# Web Page of BASH best practices https://kvz.io/blog/2013/11/21/bash-best-practices/
4+
#Exit when a command fails.
5+
set -o errexit
6+
#Exit when script tries to use undeclared variables.
7+
set -o nounset
8+
#The exit status of the last command that threw a non-zero exit code is returned.
9+
set -o pipefail
10+
11+
#Trace what gets executed. Useful for debugging.
12+
#set -o xtrace
13+
14+
# Set magic variables for current file & dir
15+
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
16+
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
17+
__base="$(basename "${__file}" .sh)"
18+
__root="$(cd "$(dirname "${__dir}")" && pwd)"
19+
20+
echo "Script name: ${__base}"
21+
echo "Executing at ${__root}"
22+
23+
if ! type "docker" &> /dev/null; then
24+
echo "Docker is not installed. Install it and then re launch"
25+
exit 1
26+
fi
27+
28+
29+
function usage(){
30+
echo -e "First Argument: git commit sha"
31+
echo -e "Second Argument: basename image."
32+
}
33+
34+
FOLDER="dist/mysql"
35+
if [ -z "${BASENAME+x}" ]; then
36+
BASENAME=registry.gitlab.com/ravimosharksas/databases/global
37+
fi
38+
39+
CI_COMMIT_SHA=$(git rev-parse HEAD | cut -c 1-8)
40+
41+
if [ $# -lt 1 ]; then
42+
echo -e "Illegal number of parameters"
43+
echo -e "$(usage)"
44+
# read -r -p "Do you want to run script with IMAGE_NAME=${BASENAME}? [y/N] " response
45+
# if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
46+
# then
47+
# echo "Running with IMAGE_NAME=${BASENAME}"
48+
# else
49+
# exit 1;
50+
# fi
51+
else
52+
CI_COMMIT_SHA=${1}
53+
if [ $# -eq 2 ]; then
54+
BASENAME=${2}
55+
fi
56+
fi
57+
58+
for filename in ${FOLDER}/*.sql; do
59+
filename=$(basename -- "$filename")
60+
filename="${filename%.*}"
61+
tag=${filename#"mysql-"}
62+
./scripts/generate_docker_image_mysql.sh "mysql/${filename}" "${BASENAME}/mysql" "${tag}" "${CI_COMMIT_SHA}"
63+
done
64+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env bash
2+
3+
# Web Page of BASH best practices https://kvz.io/blog/2013/11/21/bash-best-practices/
4+
#Exit when a command fails.
5+
set -o errexit
6+
#Exit when script tries to use undeclared variables.
7+
set -o nounset
8+
#The exit status of the last command that threw a non-zero exit code is returned.
9+
set -o pipefail
10+
11+
#Trace what gets executed. Useful for debugging.
12+
#set -o xtrace
13+
14+
# Set magic variables for current file & dir
15+
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
16+
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
17+
__base="$(basename "${__file}" .sh)"
18+
__root="$(cd "$(dirname "${__dir}")" && pwd)"
19+
20+
echo "Script name: ${__base}"
21+
echo "Executing at ${__root}"
22+
23+
if ! type "docker" &> /dev/null; then
24+
echo "Docker is not installed. Install it and then re launch"
25+
exit 1
26+
fi
27+
28+
function usage(){
29+
echo -e "First Argument: sql script file path."
30+
echo -e "Secound Argument: tag"
31+
echo -e "Third Argument: name"
32+
echo -e "Forth Argument: git commit sha"
33+
}
34+
35+
FILE=
36+
TAG=
37+
BASENAME=registry.gitlab.com/ravimosharksas/databases/global/mysql
38+
39+
if [ -z "${DOCKER_IMAGE_BASE_NAME_MYSQL+x}" ]; then
40+
BASENAME=${DOCKER_IMAGE_BASE_NAME_MYSQL}
41+
fi
42+
43+
CI_COMMIT_SHA=$(git rev-parse HEAD | cut -c 1-8)
44+
if [ $# -lt 3 ]; then
45+
echo -e "Illegal number of parameters"
46+
echo -e "$(usage)"
47+
else
48+
FILE="dist/${1}"
49+
BASENAME=${2}
50+
TAG=${3}
51+
if [ $# -ge 4 ]; then
52+
CI_COMMIT_SHA=${4}
53+
fi
54+
fi
55+
56+
if [[ "$FILE" != *".sql" ]]; then
57+
FILE="${FILE}.sql"
58+
fi
59+
60+
if [ ! -f "${FILE}" ]; then
61+
echo "ERROR: No such file ${FILE}"
62+
fi
63+
DATE="$(date --rfc-2822 | sed 's/ /T/; s/\(\....\).*-/\1-/g')"
64+
65+
docker build --rm -f docker/mysql/Dockerfile -t \
66+
"${BASENAME}:${TAG}" \
67+
--label "version=${TAG}" \
68+
--label "vcs-ref=${CI_COMMIT_SHA}" \
69+
--label "build-date=${DATE}" \
70+
--build-arg SQL_FILE_SCRIPT="./${FILE}" .

0 commit comments

Comments
 (0)