Skip to content

Commit

Permalink
Make docker environment and configurations up to date.
Browse files Browse the repository at this point in the history
  • Loading branch information
mraron committed Apr 13, 2024
1 parent 7a1344f commit b300dea
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 53 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ build: build_base build_web build_glue build_judge ## Builds all services
build_base: ## builds njudge-base
docker build -t $(PROJECT_NAME)-base .
build_web: ## builds njudge-web
docker build --build-arg="PROJECT_NAME=$(PROJECT_NAME)" -t $(PROJECT_NAME)-web -f internal/web/Dockerfile .
docker build --build-arg="PROJECT_NAME=$(PROJECT_NAME)" -t $(PROJECT_NAME)-web -f web.Dockerfile .
build_glue: ## builds njudge-glue
docker build --build-arg="PROJECT_NAME=$(PROJECT_NAME)" -t $(PROJECT_NAME)-glue -f internal/glue/Dockerfile .
docker build --build-arg="PROJECT_NAME=$(PROJECT_NAME)" -t $(PROJECT_NAME)-glue -f glue.Dockerfile .
build_judge: ## builds njudge-judge
docker build --build-arg="PROJECT_NAME=$(PROJECT_NAME)" -t $(PROJECT_NAME)-judge -f internal/judge/Dockerfile .
docker build --build-arg="PROJECT_NAME=$(PROJECT_NAME)" -t $(PROJECT_NAME)-judge -f judge.Dockerfile .

up: build ## builds an runs docker-compose up
COMPOSE_PROJECT_NAME="$(PROJECT_NAME)" docker-compose up
Expand Down
23 changes: 17 additions & 6 deletions cmd/glue.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"golang.org/x/net/context"
"io/fs"
"log/slog"
"strings"
"time"
)

Expand All @@ -26,7 +25,7 @@ var DefaultGlueConfig = GlueConfig{
Host: "db",
Name: "postgres",
Port: 5432,
SSLMode: true,
SSLMode: false,
},
}

Expand All @@ -36,6 +35,7 @@ func NewGlueCmd(v *viper.Viper) *cobra.Command {
Use: "glue",
Short: "start glue service",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
v.SetConfigType("yaml")
v.SetConfigFile("glue.yaml")
v.AddConfigPath(".")

Expand All @@ -44,7 +44,7 @@ func NewGlueCmd(v *viper.Viper) *cobra.Command {
v.SetDefault("db.host", DefaultGlueConfig.Database.Host)
v.SetDefault("db.name", DefaultGlueConfig.Database.Name)
v.SetDefault("db.port", DefaultGlueConfig.Database.Port)
v.SetDefault("db.sslmode", DefaultGlueConfig.Database.SSLMode)
v.SetDefault("db.ssl_mode", DefaultGlueConfig.Database.SSLMode)

v.AutomaticEnv()
v.SetEnvPrefix("njudge")
Expand All @@ -57,7 +57,7 @@ func NewGlueCmd(v *viper.Viper) *cobra.Command {
}

cmd.Flags().VisitAll(func(flag *pflag.Flag) {
configName := strings.ReplaceAll(flag.Name, "-", "")
configName := flag.Name
if !flag.Changed && v.IsSet(configName) {
val := v.Get(configName)
_ = cmd.Flags().Set(flag.Name, fmt.Sprintf("%v", val))
Expand All @@ -68,10 +68,21 @@ func NewGlueCmd(v *viper.Viper) *cobra.Command {
},

RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println(cfg)
conn, err := cfg.Database.Connect()
if err != nil {
return err
}
for {
slog.Info("Trying to ping database...")
if err := conn.Ping(); err == nil {
slog.Info("OK, connected to database")
break
} else {
slog.Error("Failed to connect to database", "error", err)
}
time.Sleep(5 * time.Second)
}
judges := glue.NewJudges(conn, slog.Default())
go func() {
for {
Expand All @@ -94,10 +105,10 @@ func NewGlueCmd(v *viper.Viper) *cobra.Command {

cmd.Flags().StringVar(&cfg.Database.User, "db.user", DefaultGlueConfig.Database.User, "database user")
cmd.Flags().StringVar(&cfg.Database.Password, "db.password", DefaultGlueConfig.Database.Password, "database password")
cmd.Flags().StringVar(&cfg.Database.Host, "db.host", DefaultGlueConfig.Database.Password, "database host")
cmd.Flags().StringVar(&cfg.Database.Host, "db.host", DefaultGlueConfig.Database.Host, "database host")
cmd.Flags().StringVar(&cfg.Database.Name, "db.name", DefaultGlueConfig.Database.Name, "database name")
cmd.Flags().IntVar(&cfg.Database.Port, "db.port", DefaultGlueConfig.Database.Port, "database port")
cmd.Flags().BoolVar(&cfg.Database.SSLMode, "db.sslmode", DefaultGlueConfig.Database.SSLMode, "database sslmode")
cmd.Flags().BoolVar(&cfg.Database.SSLMode, "db.ssl_mode", DefaultGlueConfig.Database.SSLMode, "database sslmode")

return cmd
}
Expand Down
27 changes: 13 additions & 14 deletions cmd/judge.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@ import (
"io/fs"
"log/slog"
"runtime"
"strings"
"time"
)

type JudgeConfig struct {
Port int
ProblemsDir string
Port int `mapstructure:"port" yaml:"port"`
ProblemsDir string `mapstructure:"problems_dir" yaml:"problems_dir"`

Isolate bool
IsolateSandboxRange []int
Isolate bool `mapstructure:"isolate" yaml:"isolate"`
IsolateSandboxRange []int `mapstructure:"isolate_sandbox_range" yaml:"isolate_sandbox_range"`

UpdateStatusLimitEvery time.Duration
UpdateStatusLimitEvery time.Duration `mapstructure:"update_status_limit_every" yaml:"update_status_limit_every"`

Concurrency int
Concurrency int `mapstructure:"concurrency" yaml:"concurrency"`
}

var DefaultJudgeConfig = JudgeConfig{
Expand All @@ -48,10 +47,10 @@ func NewJudgeCmd(v *viper.Viper) *cobra.Command {
v.AddConfigPath(".")

v.SetDefault("port", DefaultJudgeConfig.Port)
v.SetDefault("problemsDir", DefaultJudgeConfig.ProblemsDir)
v.SetDefault("problems_dir", DefaultJudgeConfig.ProblemsDir)
v.SetDefault("isolate", DefaultJudgeConfig.Isolate)
v.SetDefault("isolateSandboxRange", DefaultJudgeConfig.IsolateSandboxRange)
v.SetDefault("updateStatusLimitEvery", DefaultJudgeConfig.UpdateStatusLimitEvery)
v.SetDefault("isolate_sandbox_range", DefaultJudgeConfig.IsolateSandboxRange)
v.SetDefault("update_status_limit_every", DefaultJudgeConfig.UpdateStatusLimitEvery)

v.AutomaticEnv()
v.SetEnvPrefix("njudge")
Expand All @@ -64,7 +63,7 @@ func NewJudgeCmd(v *viper.Viper) *cobra.Command {
}

cmd.Flags().VisitAll(func(flag *pflag.Flag) {
configName := strings.ReplaceAll(flag.Name, "-", "")
configName := flag.Name
if !flag.Changed && v.IsSet(configName) {
val := v.Get(configName)
_ = cmd.Flags().Set(flag.Name, fmt.Sprintf("%v", val))
Expand Down Expand Up @@ -131,10 +130,10 @@ func NewJudgeCmd(v *viper.Viper) *cobra.Command {
}

cmd.Flags().IntVar(&cfg.Port, "port", DefaultJudgeConfig.Port, "port to listen on")
cmd.Flags().StringVar(&cfg.ProblemsDir, "problems-dir", DefaultJudgeConfig.ProblemsDir, "directory of the problems")
cmd.Flags().StringVar(&cfg.ProblemsDir, "problems_dir", DefaultJudgeConfig.ProblemsDir, "directory of the problems")
cmd.Flags().BoolVar(&cfg.Isolate, "isolate", DefaultJudgeConfig.Isolate, "use isolate (otherwise dummy sandboxes are used which are NOT secure)")
cmd.Flags().IntSliceVar(&cfg.IsolateSandboxRange, "isolate-sandbox-range", DefaultJudgeConfig.IsolateSandboxRange, "inclusive interval of isolate sandbox IDs")
cmd.Flags().DurationVar(&cfg.UpdateStatusLimitEvery, "updateStatus-limit-every", DefaultJudgeConfig.UpdateStatusLimitEvery, "the rate of status updates for the clients")
cmd.Flags().IntSliceVar(&cfg.IsolateSandboxRange, "isolate_sandbox_range", DefaultJudgeConfig.IsolateSandboxRange, "inclusive interval of isolate sandbox IDs")
cmd.Flags().DurationVar(&cfg.UpdateStatusLimitEvery, "updateStatus_limit_every", DefaultJudgeConfig.UpdateStatusLimitEvery, "the rate of status updates for the clients")
cmd.Flags().IntVar(&cfg.Concurrency, "concurrency", DefaultJudgeConfig.Concurrency, "the maximum number of concurrently executed testcase")

return cmd
Expand Down
Empty file added configs/docker/glue.yaml
Empty file.
7 changes: 0 additions & 7 deletions configs/docker/glue_docker.json

This file was deleted.

1 change: 1 addition & 0 deletions configs/docker/judge.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
problems_dir: /njudge_problems
11 changes: 0 additions & 11 deletions configs/docker/judge_docker.json

This file was deleted.

12 changes: 8 additions & 4 deletions configs/docker/web_docker.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
"url": "http://localhost:5555",
"problemsDir": "/njudge_problems",
"templatesDir": "./internal/web/templates",
"DBName": "postgres",
"DBAccount": "postgres",
"DBPassword": "postgres",
"DBHost": "db",
"database": {
"user": "postgres",
"password": "postgres",
"host": "db",
"name": "postgres",
"port": 5432,
"ssl_mode": false
},
"cookieSecret": "secret_for_c00kies",
"customHead": "",

Expand Down
2 changes: 1 addition & 1 deletion internal/glue/Dockerfile → glue.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ARG PROJECT_NAME
FROM ${PROJECT_NAME}-base

COPY configs/docker/glue_docker.json ./glue.json
COPY configs/docker/glue.yaml ./glue.yaml

CMD ["./njudge", "glue"]
12 changes: 6 additions & 6 deletions internal/web/helpers/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
)

type Database struct {
User string `json:"user,omitempty"`
Password string `json:"password,omitempty"`
Host string `json:"host,omitempty"`
Name string `json:"name,omitempty"`
Port int `json:"port,omitempty"`
SSLMode bool `json:"ssl_mode,omitempty"`
User string `mapstructure:"user" yaml:"user" json:"user"`
Password string `mapstructure:"password" yaml:"password" json:"password"`
Host string `mapstructure:"host" yaml:"host" json:"host"`
Name string `mapstructure:"name" yaml:"name" json:"name"`
Port int `mapstructure:"port" yaml:"port" json:"port"`
SSLMode bool `mapstructure:"ssl_mode" yaml:"ssl_mode" json:"ssl_mode"`
}

func (db Database) Connect() (*sql.DB, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/judge/Dockerfile → judge.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ COPY --from=nimlang/nim:1.6.18 /usr/bin/nim /usr/bin/nim
RUN ln -s /usr/local/julia/bin/julia /usr/local/bin/julia

WORKDIR /app
COPY configs/docker/judge_docker.json ./judge.json
COPY configs/docker/judge.yaml ./judge.yaml

CMD ["./njudge", "judge"]
File renamed without changes.

0 comments on commit b300dea

Please sign in to comment.