Skip to content

Commit 4a13582

Browse files
committed
Allow loading config from a file, load pools
1 parent fa2d130 commit 4a13582

File tree

4 files changed

+75
-23
lines changed

4 files changed

+75
-23
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ _testmain.go
2424
*.prof
2525

2626
bin/*
27-
languages.json
27+
languages.json
28+
config.json

config.go

+30-18
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,53 @@
11
package main
22

33
import (
4-
"fmt"
4+
"encoding/json"
5+
"io/ioutil"
56
"os"
67
"time"
78
)
89

10+
type PoolConfig struct {
11+
Image string `json:"image"`
12+
Capacity int `json:"capacity"`
13+
}
14+
915
type Config struct {
10-
DockerHost string
11-
SharedPath string
12-
RunDuration time.Duration
13-
ThrottleQuota int
14-
ThrottleConcurrency int
15-
NetworkDisabled bool
16-
MemoryLimit int64
16+
DockerHost string `json:"docker_host"`
17+
SharedPath string `json:"shared_path"`
18+
RunDuration time.Duration `json:"run_duration"`
19+
ThrottleQuota int `json:"throttle_quota"`
20+
ThrottleConcurrency int `json:"throttle_concurrency"`
21+
NetworkDisabled bool `json:"network_disabled"`
22+
MemoryLimit int64 `json:"memory_limit"`
23+
Pools []PoolConfig `json:"pools"`
1724
}
1825

19-
func NewConfig() (*Config, error) {
26+
func NewConfig() *Config {
2027
cfg := Config{
2128
DockerHost: os.Getenv("DOCKER_HOST"),
2229
SharedPath: os.Getenv("SHARED_PATH"),
2330
}
2431

25-
if cfg.DockerHost == "" {
26-
return nil, fmt.Errorf("Please set DOCKER_HOST environment variable")
27-
}
28-
29-
if cfg.SharedPath == "" {
30-
return nil, fmt.Errorf("Please set SHARED_PATH environment variable")
31-
}
32-
3332
cfg.SharedPath = expandPath(cfg.SharedPath)
3433
cfg.RunDuration = time.Second * 10
3534
cfg.ThrottleQuota = 5
3635
cfg.ThrottleConcurrency = 1
3736
cfg.NetworkDisabled = true
3837
cfg.MemoryLimit = 67108864
38+
cfg.Pools = []PoolConfig{}
39+
40+
return &cfg
41+
}
42+
43+
func NewConfigFromFile(path string) (*Config, error) {
44+
data, err := ioutil.ReadFile(path)
45+
if err != nil {
46+
return nil, err
47+
}
48+
49+
config := Config{}
3950

40-
return &cfg, nil
51+
err = json.Unmarshal(data, &config)
52+
return &config, err
4153
}

main.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
11
package main
22

33
import (
4+
"fmt"
45
"log"
6+
"os"
57

68
docker "github.com/fsouza/go-dockerclient"
79
)
810

911
const VERSION = "0.1.0"
1012

13+
func requireEnvVar(name string) {
14+
if os.Getenv(name) == "" {
15+
err := fmt.Errorf("Please set %s environment variable", name)
16+
log.Fatalln(err)
17+
}
18+
}
19+
20+
func getConfig() *Config {
21+
if os.Getenv("CONFIG") != "" {
22+
config, err := NewConfigFromFile(os.Getenv("CONFIG"))
23+
if err != nil {
24+
log.Fatalln(err)
25+
}
26+
27+
return config
28+
}
29+
30+
requireEnvVar("DOCKER_HOST")
31+
requireEnvVar("SHARED_PATH")
32+
return NewConfig()
33+
}
34+
1135
func main() {
1236
log.Printf("bitrun api v%s\n", VERSION)
1337

@@ -16,10 +40,7 @@ func main() {
1640
log.Fatalln(err)
1741
}
1842

19-
config, err := NewConfig()
20-
if err != nil {
21-
log.Fatalln(err)
22-
}
43+
config := getConfig()
2344

2445
client, err := docker.NewClient(config.DockerHost)
2546
if err != nil {

pool.go

+18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
docker "github.com/fsouza/go-dockerclient"
1010
)
1111

12+
var pools map[string]*Pool
13+
1214
type Pool struct {
1315
Config *Config
1416
Client *docker.Client
@@ -163,4 +165,20 @@ func (pool *Pool) Get() (string, error) {
163165
}
164166

165167
func RunPool(config *Config, client *docker.Client) {
168+
for _, cfg := range config.Pools {
169+
log.Println("initializing pool for:", cfg.Image)
170+
171+
pool, err := NewPool(config, client, cfg.Image, cfg.Capacity)
172+
if err != nil {
173+
log.Fatalln(err)
174+
}
175+
176+
err = pool.Load()
177+
if err != nil {
178+
log.Fatalln(err)
179+
}
180+
181+
go pool.Monitor()
182+
pools[cfg.Image] = pool
183+
}
166184
}

0 commit comments

Comments
 (0)