Skip to content

Commit

Permalink
support docker compose
Browse files Browse the repository at this point in the history
  • Loading branch information
zc2638 committed Nov 29, 2023
1 parent 4a17357 commit 5d40fcb
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ go.work
*.db
testdata
*.local.yaml
_output
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ tag ?= latest
packages = `go list ./... | grep -v github.com/zc2638/ink/test`

build-%:
@CGO_ENABLED=0 go build -ldflags="-s -w" -installsuffix cgo -o output/$* ./cmd/$*
@CGO_ENABLED=0 go build -ldflags="-s -w" -installsuffix cgo -o _output/$* ./cmd/$*

docker-build-%: build-%
@docker build --platform $(GOOS)/$(GOARCH) -t zc2638/$* -f build/Dockerfile .
@docker build -t zc2638/$* -f docker/$*.dockerfile .

docker-tag-%:
@docker tag zc2638/$* zc2638/$*:$(tag)
Expand All @@ -20,11 +20,17 @@ docker-tag: docker-tag-inkd docker-tag-inker docker-tag-inkctl
docker-push: docker-push-inkd docker-push-inker docker-push-inkctl

clean:
@rm -rf output
@rm -rf _output
@echo "clean complete"

tests:
@go test $(packages)

e2e:
@ginkgo -v test/e2e/suite

up:
@docker compose -f docker/compose.yml up

down:
@docker compose -f docker/compose.yml down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ Controllable CICD workflow service

## Setup

### 一、Source
### 一、Docker Compose

```shell
docker compose -f docker/compose.yml up
```

### 二、Source

#### 1. Run inkd

Expand Down
9 changes: 9 additions & 0 deletions config/inkd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
server:
port: 2678
database:
driver: sqlite
dsn: /tmp/ink.db
debug: true
livelog:
file:
dir: /tmp/ink_cache
7 changes: 7 additions & 0 deletions config/inker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
logger:
level: debug
workers:
- count: 1
addr: http://localhost:2678
worker:
kind: docker
22 changes: 20 additions & 2 deletions core/command/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package command
import (
"context"
"fmt"
"os"
"path/filepath"
"time"

"github.com/99nil/gopkg/server"
Expand Down Expand Up @@ -46,7 +48,10 @@ func NewDaemon() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
var cfg DaemonConfig
if _, err := ParseConfig(opt.ConfigPath, &cfg, constant.DaemonName, opt.ConfigSubKey); err != nil {
return err
if _, ok := err.(*os.PathError); !ok {
return err
}
wslog.Warn("Config file not found, use default.")
}
if err := cfg.Validate(); err != nil {
return fmt.Errorf("validate config failed: %v", err)
Expand Down Expand Up @@ -102,7 +107,20 @@ type DaemonConfig struct {

func (c *DaemonConfig) Validate() error {
if c.Server.Port <= 0 {
return fmt.Errorf("server port (%d) is not available", c.Server.Port)
c.Server.Port = 2638
}
if c.Database.Driver == "" {
c.Database.Driver = "sqlite3"
cacheDir := filepath.Join(os.TempDir(), constant.Name)
c.Database.DSN = filepath.Join(cacheDir, "ink.db")
if err := os.MkdirAll(cacheDir, os.ModePerm); err != nil {
return err
}
}
if c.Livelog.File == nil {
c.Livelog.File = &livelog.ConfigFile{
Dir: filepath.Join(os.TempDir(), constant.Name, "cache"),
}
}
return nil
}
Expand Down
65 changes: 65 additions & 0 deletions docker/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
version: '3.8'
name: ink

networks:
default:
name: ink

services:
mysql57:
image: mysql:5.7.31
restart: always
container_name: ink_mysql57
hostname: ink_mysql57
volumes:
- "~/.ink/data/mysql57:/var/lib/mysql"
environment:
TZ: "Asia/Shanghai"
MYSQL_ROOT_PASSWORD: "123456"
ports:
- "3308:3306"
healthcheck:
test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost", "-u", "root", "-p123456" ]
interval: 10s
timeout: 20s
retries: 10
inkd:
image: zc2638/inkd:latest
restart: always
container_name: inkd
hostname: inkd
environment:
INKD_DATABASE_DRIVER: mysql
INKD_DATABASE_DSN: root:123456@tcp(ink_mysql57:3306)/ink
ports:
- "2678:2678"
depends_on:
mysql57:
condition: service_healthy
healthcheck:
test: [ "CMD", "wget", "http://localhost:2678"]
interval: 5s
timeout: 20s
retries: 10
inker:
image: zc2638/inker:latest
restart: always
container_name: inker
hostname: inker
command:
- sh
- -c
- |
cat > /work/config/inker.yaml << EOF
logger:
level: debug
workers:
- count: 1
addr: http://inkd:2678
worker:
kind: docker
EOF
inker
depends_on:
inkd:
condition: service_healthy
15 changes: 13 additions & 2 deletions build/Dockerfile → docker/inkd.dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
FROM golang:1.21 as builder

ENV GOPROXY=https://goproxy.cn,https://goproxy.io,direct
ENV CGO_ENABLED=0

WORKDIR /work
ADD . .
RUN make build-inkd

FROM alpine:3.18 as alpine

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories && \
Expand All @@ -6,10 +15,12 @@ RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositorie

FROM alpine:3.18
ENV TZ=Asia/Shanghai
ENV INKD_CONFIG=/work/config/inkd.yaml

COPY --from=alpine /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=alpine /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY output/inkd /bin/inkd
COPY --from=builder /work/config/inkd.yaml /work/config/inkd.yaml
COPY --from=builder /work/_output/inkd /bin/inkd

WORKDIR /work
CMD ["app"]
CMD ["inkd"]
26 changes: 26 additions & 0 deletions docker/inker.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM golang:1.21 as builder

ENV GOPROXY=https://goproxy.cn,https://goproxy.io,direct
ENV CGO_ENABLED=0

WORKDIR /work
ADD . .
RUN make build-inker

FROM alpine:3.18 as alpine

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories && \
apk update && \
apk add -U --no-cache ca-certificates tzdata

FROM alpine:3.18
ENV TZ=Asia/Shanghai
ENV INKD_CONFIG=/work/config/inker.yaml

COPY --from=alpine /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=alpine /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /work/config/inker.yaml /work/config/inker.yaml
COPY --from=builder /work/_output/inker /bin/inker

WORKDIR /work
CMD ["inkd"]
6 changes: 6 additions & 0 deletions pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ func AutoDatabase(cfg Config) error {
if err != nil {
return err
}
sdb, err := db.DB()
if err != nil {
return err
}
defer sdb.Close()

if err := db.Exec(initSQL).Error; err != nil {
return fmt.Errorf("create database failed: %v", err)
}
Expand Down

0 comments on commit 5d40fcb

Please sign in to comment.