worker: add some type safety to port handling

The "port" used for build jobs is an actual TCP port (used e.g. for SSH
access). It is already enforced to be in a specific port range when
initially selected. Enshrine this fact in the API by making it a uint16.
This commit is contained in:
Conrad Hoffmann 2023-12-20 16:47:42 +01:00 committed by Simon Ser
parent dfa05f3399
commit 29f79f49c3
3 changed files with 9 additions and 10 deletions

View File

@ -54,7 +54,7 @@ type JobContext struct {
LogDir string
LogFile *os.File
Manifest *Manifest
Port int
Port uint16
Settled bool
NTasks int
@ -261,11 +261,10 @@ func (ctx *JobContext) Control(
}
func (ctx *JobContext) SSH(args ...string) *exec.Cmd {
sport := strconv.Itoa(ctx.Port)
switch ctx.ImageConfig.LoginCmd {
case "drawterm":
return exec.CommandContext(ctx.Context,
"env", fmt.Sprintf("DIALSRV=%s", sport),
"env", fmt.Sprintf("DIALSRV=%d", ctx.Port),
"PASS=password", "drawterm",
"-a", "none",
"-u", "glenda",
@ -274,7 +273,7 @@ func (ctx *JobContext) SSH(args ...string) *exec.Cmd {
case "ssh":
return exec.CommandContext(ctx.Context, "ssh",
append([]string{"-q",
"-p", sport,
"-p", fmt.Sprintf("%d", ctx.Port),
"-o", "UserKnownHostsFile=/dev/null",
"-o", "StrictHostKeyChecking=no",
"-o", "LogLevel=quiet",

View File

@ -37,7 +37,7 @@ func HttpServer() {
Manifest string `json:"manifest"`
Note *string `json:"note"`
OwnerId int `json:"owner_id"`
Port int `json:"port"`
Port uint16 `json:"port"`
Status string `json:"status"`
Task int `json:"task"`
Tasks int `json:"tasks"`

View File

@ -65,12 +65,12 @@ func (ctx *JobContext) Boot(r *goredis.Client) func() {
if ctx.Manifest.Arch != nil {
arch = *ctx.Manifest.Arch
}
ctx.Port = int(port)
ctx.Port = uint16(port)
ctx.Log.Printf("Booting image %s (%s) on port %d",
ctx.Manifest.Image, arch, port)
boot := ctx.Control(ctx.Context,
ctx.Manifest.Image, "boot", arch, strconv.Itoa(ctx.Port))
ctx.Manifest.Image, "boot", arch, fmt.Sprintf("%d", ctx.Port))
boot.Env = append(os.Environ(), fmt.Sprintf("BUILD_JOB_ID=%d", ctx.Job.Id))
boot.Stdout = ctx.LogFile
boot.Stderr = ctx.LogFile
@ -83,7 +83,7 @@ func (ctx *JobContext) Boot(r *goredis.Client) func() {
return func() {
ctx.Log.Printf("Tearing down build VM")
cleanup := ctx.Control(context.TODO(), ctx.Manifest.Image, "cleanup",
strconv.Itoa(ctx.Port))
fmt.Sprintf("%d", ctx.Port))
if err := cleanup.Run(); err != nil {
fmt.Printf("Failed to destroy build VM: %v\n", err)
}
@ -388,7 +388,7 @@ func (ctx *JobContext) ConfigureRepos() error {
for name, source := range ctx.Manifest.Repositories {
ctx.Log.Printf("Adding repository %s\n", name)
ctrl := ctx.Control(ctx.Context, ctx.Manifest.Image, "add-repo",
strconv.Itoa(ctx.Port), name, source)
fmt.Sprintf("%d", ctx.Port), name, source)
ctrl.Stdout = ctx.LogFile
ctrl.Stderr = ctx.LogFile
if err := ctrl.Run(); err != nil {
@ -540,7 +540,7 @@ func (ctx *JobContext) InstallPackages() error {
}
ctx.Log.Println("Installing packages")
ctrl := ctx.Control(ctx.Context, ctx.Manifest.Image, "install",
strconv.Itoa(ctx.Port), strings.Join(ctx.Manifest.Packages, " "))
fmt.Sprintf("%d", ctx.Port), strings.Join(ctx.Manifest.Packages, " "))
ctrl.Stdout = ctx.LogFile
ctrl.Stderr = ctx.LogFile
if err := ctrl.Run(); err != nil {