~qeef/damn-deploy

ee526ee4a5ef39bd1b4401ff8773fa59b447e45c — Jiri Vlasak 2 months ago 9b017f7 + 02e2280
Merge branch 'finished-areas'
7 files changed, 90 insertions(+), 4 deletions(-)

M .env
A finished/Dockerfile
M gen.yml
M http.yml
M upkeep/Dockerfile
A upkeep/finish_areas_with_priority_-10.sh
M upkeep/run_scripts.sh -rw-r--r-- => -rwxr-xr-x
M .env => .env +1 -0
@@ 67,3 67,4 @@ MAPPY_SUB=mappy
CHAT_SUB=chat
OSMSM_SUB=osmsm
RSS_SUB=rss
FINISHED_SUB=finished

A finished/Dockerfile => finished/Dockerfile +3 -0
@@ 0,0 1,3 @@
FROM nginx

RUN sed -i '/location \/ {$/ a autoindex on; autoindex_format json;' /etc/nginx/conf.d/default.conf

M gen.yml => gen.yml +3 -0
@@ 6,6 6,8 @@ services:
        env_file: .env
        networks:
            default:
        volumes:
            - finished-areas:/finished
    osmsm_gen:
        build:
            context: osmsm_gen


@@ 25,3 27,4 @@ services:
volumes:
    osmsm-volume:
    damn-rss-volume:
    finished-areas:

M http.yml => http.yml +14 -0
@@ 78,6 78,19 @@ services:
            - "traefik.http.routers.rss.tls.certresolver=le"
        volumes:
            - damn-rss-volume:/usr/share/nginx/html
    finished_www:
        build:
            context: finished
        env_file: .env
        restart: always
        environment:
            - NGINX_HOST=${FINISHED_SUB}.${DOMAIN_NAME}
        labels:
            - "traefik.http.routers.finished.rule=Host(`${FINISHED_SUB}.${DOMAIN_NAME}`)"
            - "traefik.http.routers.finished.tls=true"
            - "traefik.http.routers.finished.tls.certresolver=le"
        volumes:
            - finished-areas:/usr/share/nginx/html
    www:
        build:
            context: ${WWW_REPO}


@@ 167,3 180,4 @@ volumes:
    damndb-volume:
    osmsm-volume:
    damn-rss-volume:
    finished-areas:

M upkeep/Dockerfile => upkeep/Dockerfile +1 -1
@@ 7,7 7,7 @@ ENV POSTGRES_PASSWORD ${POSTGRES_PASSWORD:-pass}
ENV POSTGRES_USER ${POSTGRES_USER:-damnuser}
ENV POSTGRES_DB ${POSTGRES_DB:-damndb}

COPY ./finish_areas_with_priority_-10.sh /finish_areas_with_priority_-10.sh
COPY ./run_scripts.sh /run_scripts.sh
RUN chmod +x /run_scripts.sh
COPY ./scripts /scripts
ENTRYPOINT ["/run_scripts.sh"]

A upkeep/finish_areas_with_priority_-10.sh => upkeep/finish_areas_with_priority_-10.sh +64 -0
@@ 0,0 1,64 @@
#!/bin/sh
OD="/finished"
if [ ! -d "$OD" ]
then
	echo "Directory $OD does not exists."
	exit 1
fi
run_psql()
# Run psql query that returns JSON formatted rows.
{
	db="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DB_HOST}/${POSTGRES_DB}"
	psql -1 -c "$1" "$db"
	if [ x"$?" != "x0" ]
	then
		exit 1
	fi
}
run_psql_json()
{
	run_psql "$1" | grep '^ *{'
}
get_aid()
{
	echo "$1" | grep -o '"aid":[0-9]\{4,4\}' | grep -o '[0-9]\{4,4\}'
}
get_fn()
{
	echo "$(date --iso-8601).$(get_aid "$1").json"
}
CA="current_areas ca"
CS="current_squares cs"
CC="current_commits cc"
SQL1="with lasts as (select distinct on (aid) aid, date
	from current_commits order by aid, cid desc)
select row_to_json(ca) from $CA, lasts
where ca.aid=lasts.aid and ca.priority=-10
	and lasts.date + interval '1 month' < now()"
SQL2="select row_to_json(cs) from $CS where aid="
SQL3="select row_to_json(cc) from $CC where aid="
run_psql_json "$SQL1" | while read -r area
do
	aid=$(get_aid "$area")
	fn="$OD/$(get_fn "$area")"
	echo "{" > "$fn"
	echo "\"area\":$area," >> "$fn"
	echo "\"squares\":[" >> "$fn"
	run_psql_json "$SQL2$aid order by sid" | while read -r square
	do
		echo "$square," >> "$fn"
	done
	sed -i '$ s/.$//' "$fn"
	echo "]," >> "$fn"
	echo "\"commits\":[" >> "$fn"
	run_psql_json "$SQL3$aid order by cid desc" | while read -r commit
	do
		echo "$commit," >> "$fn"
	done
	sed -i '$ s/.$//' "$fn"
	echo "]}" >> "$fn"
	DC="delete from $CC where cc.aid=$aid;"
	DS="delete from $CS where cs.aid=$aid;"
	DA="delete from $CA where ca.aid=$aid;"
	run_psql "$DC$DS$DA"
done

M upkeep/run_scripts.sh => upkeep/run_scripts.sh +4 -3
@@ 1,6 1,7 @@
#!/bin/bash
#!/bin/sh
for s in `ls /scripts`
do
        psql -1 -f /scripts/$s \
        postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DB_HOST}/${POSTGRES_DB}
	psql -1 -f /scripts/$s \
	postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DB_HOST}/${POSTGRES_DB}
done
/finish_areas_with_priority_-10.sh