forked from teamdigitale/confini-amministrativi-istat
-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.sh
executable file
·149 lines (130 loc) · 3.92 KB
/
run.sh
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
#!/bin/bash
ACTION=$1
SOURCE_NAME=$2
NGINX_PORT=${2:-8080}
SWAGGER_UI_PORT=8091
SWAGGER_ED_PORT=8092
DOCKER_IMAGE_NAME=ondata-conf-amm-istat
DOCKER_IMAGE_VERSION=latest
DOCKER_IMAGE=$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_VERSION
DOCKER_VOLUME=$PWD:/app
NGINX_VERSION=latest
SWAGGER_UI_VERSION=v5.4.2
SWAGGER_ED_VERSION=next-v5-unprivileged
AJV_VERSION=5.0.0
OAS_SPEC_VERSION=v2
build () {
docker build -t $DOCKER_IMAGE_NAME .
}
rebuild () {
docker build --no-cache -t $DOCKER_IMAGE_NAME .
}
generate () {
if [ -z "$SOURCE_NAME" ]; then
docker run --rm -v $DOCKER_VOLUME $DOCKER_IMAGE
else
docker run --rm -e SOURCE_NAME=$SOURCE_NAME -v $DOCKER_VOLUME $DOCKER_IMAGE
fi
}
dev () {
if [ ! -z "$SOURCE_NAME" ]; then
docker run --rm -e SOURCE_NAME=$SOURCE_NAME -v $PWD/sources.json:/app/sources.json -v $PWD/main.py:/app/main.py -v $DOCKER_VOLUME $DOCKER_IMAGE
else
error
fi
}
serve () {
echo "API served at http://localhost:$NGINX_PORT"
docker run --rm -p $NGINX_PORT:80 -v $PWD/nginx.conf:/etc/nginx/conf.d/default.conf -v $PWD/dist:/usr/share/nginx/html:ro nginx:$NGINX_VERSION
echo "Shutdown API"
}
swagger_ui () {
echo "Swagger UI running at http://localhost:$SWAGGER_UI_PORT"
docker run --rm -p $SWAGGER_UI_PORT:8080 -v $PWD/dist/api/$OAS_SPEC_VERSION/openapi.$OAS_SPEC_VERSION.yml:/tmp/openapi.$OAS_SPEC_VERSION.yml -e SWAGGER_JSON=/tmp/openapi.$OAS_SPEC_VERSION.yml swaggerapi/swagger-ui:$SWAGGER_UI_VERSION
echo "Shutdown Swagger UI"
}
swagger_ed () {
echo "Swagger Editor running at http://localhost:$SWAGGER_ED_PORT"
docker run --rm -p $SWAGGER_ED_PORT:8080 swaggerapi/swagger-editor:$SWAGGER_ED_VERSION
echo "Shutdown Swagger Editor"
}
documentation () {
(trap 'kill 0' SIGINT; swagger_ui & swagger_ed)
}
deploy () {
git push origin `git subtree split --prefix api main`:gh-pages --force
}
shell () {
docker run --rm -it -v $DOCKER_VOLUME --entrypoint /bin/bash $DOCKER_IMAGE
}
validate () {
echo "Validate sources.json file..."
docker run --rm \
-v $PWD/sources.json:/opt/sources.json \
-v $PWD/sources.schema.min.json:/opt/sources.schema.json \
weibeld/ajv-cli:$AJV_VERSION \
ajv --spec draft2020 -c ajv-formats -s /opt/sources.schema.json -d /opt/sources.json
echo "... done!"
}
lint () {
poetry run pre-commit run --files main.py
}
help () {
echo "Usage: $0 [build | generate | serve | documentation] [YYYYMMDD | PORT]"
echo "Examples:"
echo "- $0 build # Build the Docker image"
echo "- $0 rebuild # Build the Docker image with --no-cache"
echo "- $0 generate # Generate all resources listed in sources.json file"
echo "- $0 generate 20230101 # Generate a custom resource only"
echo "- $0 dev 20230101 # Generate a custom resource in dev mode (no build required)"
echo "- $0 serve # Serve API on port $NGINX_PORT (default)"
echo "- $0 serve 8081 # Serve API on custom port"
echo "- $0 documentation # Run Swagger UI on port $SWAGGER_UI_PORT and Swagger Editor on port $SWAGGER_ED_PORT"
echo "- $0 deploy # Deploy sample"
echo "- $0 shell # Open a shell inside the container"
echo "- $0 validate # Validate sources.json file"
echo "- $0 lint # Run linter on code"
echo "You can exit the running process with ctrl+c"
}
error () {
echo "Error, wrong syntax!"
help
}
case $ACTION in
build)
build
;;
rebuild)
rebuild
;;
generate)
generate
;;
dev)
dev
;;
serve)
serve
;;
documentation)
documentation
;;
deploy)
deploy
;;
shell)
shell
;;
validate)
validate
;;
lint)
lint
;;
help)
help
;;
*)
error
;;
esac