Skip to content

Commit eb57e1e

Browse files
authored
Replace COMPLEMENT_VERSION_CHECK_ITERATIONS with COMPLEMENT_SPAWN_HS_TIMEOUT_SECS (#234)
The new name is clearer and easier to understand. Fixes #232. Currently, Complement will still accept the old env var but will log a Deprecated warning. Homerunner will just look for the new env var HOMERUNNER_SPAWN_HS_TIMEOUT_SECS and will ignore the old env var.
1 parent 7d427c3 commit eb57e1e

File tree

6 files changed

+42
-29
lines changed

6 files changed

+42
-29
lines changed

cmd/homerunner/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ An HTTP server which can spin up homeservers and execute blueprints on them. Pow
44

55
Environment variables (all are optional):
66
```
7-
HOMERUNNER_LIFETIME_MINS=30 # how long networks can exist for before being destroyed
7+
HOMERUNNER_LIFETIME_MINS=30 # how long networks can exist for before being destroyed
88
HOMERUNNER_PORT=54321 # port to listen on
9-
HOMERUNNER_VER_CHECK_ITERATIONS=100 # how long to wait for the base image to spin up
9+
HOMERUNNER_SPAWN_HS_TIMEOUT_SECS=5 # how long to wait for the base image to spin up
1010
HOMERUNNER_KEEP_BLUEPRINTS='clean_hs federation_one_to_one_room' # space delimited blueprint names to keep images for
1111
HOMERUNNER_SNAPSHOT_BLUEPRINT=/some/file.json # single shot execute this blueprint then commit the image, does not run the server
1212
```

cmd/homerunner/main.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const Pkg = "homerunner"
2020
type Config struct {
2121
HomeserverLifetimeMins int
2222
Port int
23-
VersionCheckIterations int
23+
SpawnHSTimeout time.Duration
2424
KeepBlueprints []string
2525
Snapshot string
2626
}
@@ -29,7 +29,7 @@ func NewConfig() *Config {
2929
cfg := &Config{
3030
HomeserverLifetimeMins: 30,
3131
Port: 54321,
32-
VersionCheckIterations: 100,
32+
SpawnHSTimeout: 5 * time.Second,
3333
KeepBlueprints: strings.Split(os.Getenv("HOMERUNNER_KEEP_BLUEPRINTS"), " "),
3434
Snapshot: os.Getenv("HOMERUNNER_SNAPSHOT_BLUEPRINT"),
3535
}
@@ -39,20 +39,20 @@ func NewConfig() *Config {
3939
if val, _ := strconv.Atoi(os.Getenv("HOMERUNNER_PORT")); val != 0 {
4040
cfg.Port = val
4141
}
42-
if val, _ := strconv.Atoi(os.Getenv("HOMERUNNER_VER_CHECK_ITERATIONS")); val != 0 {
43-
cfg.VersionCheckIterations = val
42+
if val, _ := strconv.Atoi(os.Getenv("HOMERUNNER_SPAWN_HS_TIMEOUT_SECS")); val != 0 {
43+
cfg.SpawnHSTimeout = time.Duration(val) * time.Second
4444
}
4545
return cfg
4646
}
4747

4848
func cleanup(c *Config) {
4949
cfg := &config.Complement{
50-
PackageNamespace: Pkg,
51-
BaseImageURI: "nothing",
52-
DebugLoggingEnabled: true,
53-
VersionCheckIterations: c.VersionCheckIterations,
54-
KeepBlueprints: c.KeepBlueprints,
55-
BestEffort: true,
50+
PackageNamespace: Pkg,
51+
BaseImageURI: "nothing",
52+
DebugLoggingEnabled: true,
53+
SpawnHSTimeout: c.SpawnHSTimeout,
54+
KeepBlueprints: c.KeepBlueprints,
55+
BestEffort: true,
5656
}
5757
builder, err := docker.NewBuilder(cfg)
5858
if err != nil {

cmd/homerunner/setup.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ func (r *Runtime) CreateDeployment(imageURI string, blueprint *b.Blueprint) (*do
3737
}
3838
namespace := "homerunner_" + blueprint.Name
3939
cfg := &config.Complement{
40-
BaseImageURI: imageURI,
41-
DebugLoggingEnabled: true,
42-
VersionCheckIterations: r.Config.VersionCheckIterations,
43-
BestEffort: true,
44-
PackageNamespace: Pkg,
40+
BaseImageURI: imageURI,
41+
DebugLoggingEnabled: true,
42+
SpawnHSTimeout: r.Config.SpawnHSTimeout,
43+
BestEffort: true,
44+
PackageNamespace: Pkg,
4545
}
4646
builder, err := docker.NewBuilder(cfg)
4747
if err != nil {

internal/config/config.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package config
22

33
import (
4+
"fmt"
45
"os"
56
"strconv"
67
"strings"
8+
"time"
79
)
810

911
type Complement struct {
10-
BaseImageURI string
11-
BaseImageArgs []string
12-
DebugLoggingEnabled bool
13-
AlwaysPrintServerLogs bool
14-
BestEffort bool
15-
VersionCheckIterations int
16-
KeepBlueprints []string
12+
BaseImageURI string
13+
BaseImageArgs []string
14+
DebugLoggingEnabled bool
15+
AlwaysPrintServerLogs bool
16+
BestEffort bool
17+
SpawnHSTimeout time.Duration
18+
KeepBlueprints []string
1719
// The namespace for all complement created blueprints and deployments
1820
PackageNamespace string
1921
}
@@ -24,7 +26,12 @@ func NewConfigFromEnvVars() *Complement {
2426
cfg.BaseImageArgs = strings.Split(os.Getenv("COMPLEMENT_BASE_IMAGE_ARGS"), " ")
2527
cfg.DebugLoggingEnabled = os.Getenv("COMPLEMENT_DEBUG") == "1"
2628
cfg.AlwaysPrintServerLogs = os.Getenv("COMPLEMENT_ALWAYS_PRINT_SERVER_LOGS") == "1"
27-
cfg.VersionCheckIterations = parseEnvWithDefault("COMPLEMENT_VERSION_CHECK_ITERATIONS", 100)
29+
cfg.SpawnHSTimeout = time.Duration(parseEnvWithDefault("COMPLEMENT_SPAWN_HS_TIMEOUT_SECS", 30)) * time.Second
30+
if os.Getenv("COMPLEMENT_VERSION_CHECK_ITERATIONS") != "" {
31+
fmt.Fprintln(os.Stderr, "Deprecated: COMPLEMENT_VERSION_CHECK_ITERATIONS will be removed in a later version. Use COMPLEMENT_SPAWN_HS_TIMEOUT_SECS instead which does the same thing and is clearer.")
32+
// each iteration had a 50ms sleep between tries so the timeout is 50 * iteration ms
33+
cfg.SpawnHSTimeout = time.Duration(50*parseEnvWithDefault("COMPLEMENT_VERSION_CHECK_ITERATIONS", 100)) * time.Millisecond
34+
}
2835
cfg.KeepBlueprints = strings.Split(os.Getenv("COMPLEMENT_KEEP_BLUEPRINTS"), " ")
2936
if cfg.BaseImageURI == "" {
3037
panic("COMPLEMENT_BASE_IMAGE must be set")

internal/docker/builder.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ func (d *Builder) deployBaseImage(blueprintName string, hs b.Homeserver, context
383383
return deployImage(
384384
d.Docker, d.Config.BaseImageURI, d.CSAPIPort, fmt.Sprintf("complement_%s", contextStr),
385385
d.Config.PackageNamespace, blueprintName, hs.Name, asIDToRegistrationMap, contextStr,
386-
networkID, d.Config.VersionCheckIterations,
386+
networkID, d.Config.SpawnHSTimeout,
387387
)
388388
}
389389

@@ -491,7 +491,8 @@ func generateASRegistrationYaml(as b.ApplicationService) string {
491491
}
492492

493493
func deployImage(
494-
docker *client.Client, imageID string, csPort int, containerName, pkgNamespace, blueprintName, hsName string, asIDToRegistrationMap map[string]string, contextStr, networkID string, versionCheckIterations int,
494+
docker *client.Client, imageID string, csPort int, containerName, pkgNamespace, blueprintName, hsName string,
495+
asIDToRegistrationMap map[string]string, contextStr, networkID string, spawnHSTimeout time.Duration,
495496
) (*HomeserverDeployment, error) {
496497
ctx := context.Background()
497498
var extraHosts []string
@@ -595,7 +596,12 @@ func deployImage(
595596
versionsURL := fmt.Sprintf("%s/_matrix/client/versions", baseURL)
596597
// hit /versions to check it is up
597598
var lastErr error
598-
for i := 0; i < versionCheckIterations; i++ {
599+
stopTime := time.Now().Add(spawnHSTimeout)
600+
for {
601+
if time.Now().After(stopTime) {
602+
lastErr = fmt.Errorf("timed out checking for homeserver to be up: %s", lastErr)
603+
break
604+
}
599605
res, err := http.Get(versionsURL)
600606
if err != nil {
601607
lastErr = fmt.Errorf("GET %s => error: %s", versionsURL, err)

internal/docker/deployer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (d *Deployer) Deploy(ctx context.Context, blueprintName string) (*Deploymen
8989
// TODO: Make CSAPI port configurable
9090
deployment, err := deployImage(
9191
d.Docker, img.ID, 8008, fmt.Sprintf("complement_%s_%s_%s_%d", d.config.PackageNamespace, d.DeployNamespace, contextStr, d.Counter),
92-
d.config.PackageNamespace, blueprintName, hsName, asIDToRegistrationMap, contextStr, networkID, d.config.VersionCheckIterations)
92+
d.config.PackageNamespace, blueprintName, hsName, asIDToRegistrationMap, contextStr, networkID, d.config.SpawnHSTimeout)
9393
if err != nil {
9494
if deployment != nil && deployment.ContainerID != "" {
9595
// print logs to help debug

0 commit comments

Comments
 (0)