#!/bin/bash
# traefik v2.2 service installimine
# Juhend: https://dockerswarm.rocks/traefik/
# Installimiseks kasutatakse konfi faili traefik.yml

docker network create -d overlay traefik-network
docker network create -d overlay --attachable traefik-external

docker stack deploy -c traefik.yml traefik

# Kontrolli stack'i
docker stack ps traefik

# -------------- käsitis serifikaatide lisamine -------------
# Genereeri oma arvutis
mkcert mustikas.maastikuarhitekt.ee
# Lisa faili
/var/lib/docker/volumes/traefik_certs/_data/dynamic_conf.yml
# ----
# Dynamic configuration
tls:
  certificates:
    - certFile: /letsencrypt/owngenerated/mustikas.maastikuarhitekt.ee/mustikas.maastikuarhitekt.ee.pem
      keyFile: /letsencrypt/owngenerated/mustikas.maastikuarhitekt.ee/mustikas.maastikuarhitekt.ee-key.pem

# ------------------- meiliserveri sertifikaatidest --------------
# Loe kataloogist programming. Seal on pythoni skript, kuidas acme.json failist
# vastav sertifikaat eraldada ja kuhu paigaldada.


## -------------- base64-encode to base64 url -----------------
echo 'my-secret-key' | base64 | sed -e 's/+/-/g' -e 's///_/g' -e 's/=//g'


## ----------------  Juhul kui on API viga  -------------------
## What worked for me as a workaround (without downgrading docker) was to add 
## the variable DOCKER_MIN_API_VERSION=1.24 to the docker service configuration. 
## I did it in this way (on a debian machine):

systemctl edit docker.service
# Add this part above the line ### Lines below this comment will be discarded:
[Service]
Environment=DOCKER_MIN_API_VERSION=1.24
# Save the file and exit
systemctl restart docker


## ------------------------------------------------------------





