Skip to content
Permalink
Browse files

add deploy service #130

  • Loading branch information...
umputun committed Nov 20, 2016
1 parent 666de7a commit c625a3e9bcf37192134ffb4f78fb19152b00f158
Showing with 115 additions and 2 deletions.
  1. +17 −0 deploy/Dockerfile
  2. +10 −0 deploy/deploy.sh
  3. +58 −0 deploy/main.go
  4. +27 −0 docker-compose-deploy.yml
  5. +3 −2 docker-compose.yml
@@ -0,0 +1,17 @@
FROM golang:alpine

ENV DOCKER_HOST unix://var/run/docker.sock
RUN apk add --update docker py-pip && pip install docker-compose && docker -v

ADD . /go/src/github.com/umputun/rt-bot/deploy
RUN \
cd /go/src/github.com/umputun/rt-bot/deploy && \
go get -v && \
go build -o /srv/deploy && \
rm -rf /go/src/*

ADD deploy.sh /srv/deploy.sh

EXPOSE 8080
WORKDIR /srv
CMD ["/srv/deploy"]
@@ -0,0 +1,10 @@
#/bin/sh
cd /srv/rt-bot
echo "deploy bots"
/usr/bin/docker-compose ps

/usr/bin/docker-compose pull
/usr/bin/docker-compose up -d

/usr/bin/docker-compose ps
echo "all done"
@@ -0,0 +1,58 @@
package main

import (
"bytes"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"time"
)

func main() {
log.Printf("deployer")

ch := make(chan struct{})
go deploy(ch)

http.HandleFunc("/deploy", auth(func(w http.ResponseWriter, r *http.Request) {
st := time.Now()
ch <- struct{}{}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{"status": "ok", "duration": %v}`, time.Since(st))
}))

if err := http.ListenAndServeTLS(":443", "/srv/rt-bot/etc/ssl/nginx.crt", "/srv/rt-bot/etc/ssl/nginx.key", nil); err != nil {
log.Fatalf("failed to start server, %v", err)
}
}

func auth(fn http.HandlerFunc) http.HandlerFunc {

return func(w http.ResponseWriter, r *http.Request) {
user, pass, _ := r.BasicAuth()
if user != os.Getenv("DEPLOY_USER") || pass != os.Getenv("DEPLOY_PASSWD") {
http.Error(w, "Unauthorized.", 401)
return
}
fn(w, r)
}
}

func deploy(ch <-chan struct{}) {
for range ch {
log.Print("deploy request")
cmd := exec.Command("sh", "-c", "/srv/deploy.sh")
var out, stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
log.Printf("error %s", stderr.String())
return
}
log.Printf("out: %s", out.String())
log.Printf("err: %s", stderr.String())
}
}
@@ -0,0 +1,27 @@
version: '2'

services:

deploy:
build: deploy
container_name: deploy
hostname: deploy
restart: always

logging:
driver: json-file
options:
max-size: "10m"
max-file: "5"

ports:
- 1443:443

environment:
- DEPLOY_USER
- DEPLOY_PASSWD
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- .:/srv/rt-bot
- ./docker-compose.yml:/srv/docker-compose.yml
- ./.env:/srv/.env
@@ -51,6 +51,7 @@ services:
- tweet-bot
- rest-voter


hello:
build: hello
image: "${DOCKER_REGISTRY_NAMESPACE}/hello-bot"
@@ -307,7 +308,7 @@ services:
driver: json-file
options:
max-size: "10m"
max-file: "5"
max-file: "5"

excerpt-bot:
build: excerpt-bot
@@ -332,7 +333,7 @@ services:
options:
max-size: "10m"
max-file: "5"

rest-voter:
build: rest-voter
image: "${DOCKER_REGISTRY_NAMESPACE}/rest-voter"

0 comments on commit c625a3e

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.