diff --git a/cmd/osbuild-worker/jobimpl-osbuild.go b/cmd/osbuild-worker/jobimpl-osbuild.go index fb5fb10a8..c2b85746f 100644 --- a/cmd/osbuild-worker/jobimpl-osbuild.go +++ b/cmd/osbuild-worker/jobimpl-osbuild.go @@ -23,6 +23,11 @@ import ( "github.com/osbuild/osbuild-composer/internal/worker" ) +type OSBuildJobImpl struct { + Store string + KojiServers map[string]koji.GSSAPICredentials +} + func packageMetadataToSignature(pkg osbuild.RPMPackageMetadata) *string { if pkg.SigGPG != "" { return &pkg.SigGPG @@ -56,7 +61,7 @@ func osbuildStagesToRPMs(stages []osbuild.StageResult) []koji.RPM { return rpms } -func RunJob(job worker.Job, store string, kojiServers map[string]koji.GSSAPICredentials) error { +func (impl *OSBuildJobImpl) Run(job worker.Job) error { outputDirectory, err := ioutil.TempDir("/var/tmp", "osbuild-worker-*") if err != nil { return fmt.Errorf("error creating temporary output directory: %v", err) @@ -76,7 +81,7 @@ func RunJob(job worker.Job, store string, kojiServers map[string]koji.GSSAPICred start_time := time.Now() - osbuildOutput, err := RunOSBuild(args.Manifest, store, outputDirectory, os.Stderr) + osbuildOutput, err := RunOSBuild(args.Manifest, impl.Store, outputDirectory, os.Stderr) if err != nil { return err } @@ -194,7 +199,7 @@ func RunJob(job worker.Job, store string, kojiServers map[string]koji.GSSAPICred } kojiServer, _ := url.Parse(options.Server) - creds, exists := kojiServers[kojiServer.Hostname()] + creds, exists := impl.KojiServers[kojiServer.Hostname()] if !exists { r = append(r, fmt.Errorf("Koji server has not been configured: %s", kojiServer.Hostname())) continue diff --git a/cmd/osbuild-worker/main.go b/cmd/osbuild-worker/main.go index b38ad70f3..c06ee8ccf 100644 --- a/cmd/osbuild-worker/main.go +++ b/cmd/osbuild-worker/main.go @@ -27,6 +27,11 @@ type connectionConfig struct { ClientCertFile string } +// Represents the implementation of a job type as defined by the worker API. +type JobImplementation interface { + Run(job worker.Job) error +} + func createTLSConfig(config *connectionConfig) (*tls.Config, error) { caCertPEM, err := ioutil.ReadFile(config.CACertFile) if err != nil { @@ -144,19 +149,37 @@ func main() { } } + jobImpls := map[string]JobImplementation{ + "osbuild": &OSBuildJobImpl{ + Store: store, + KojiServers: kojiServers, + }, + } + + acceptedJobTypes := []string{} + for jt := range jobImpls { + acceptedJobTypes = append(acceptedJobTypes, jt) + } + for { fmt.Println("Waiting for a new job...") - job, err := client.RequestJob([]string{"osbuild"}) + job, err := client.RequestJob(acceptedJobTypes) if err != nil { log.Fatal(err) } + impl, exists := jobImpls[job.Type()] + if !exists { + log.Printf("Ignoring job with unknown type %s", job.Type()) + continue + } + fmt.Printf("Running '%s' job %v\n", job.Type(), job.Id()) ctx, cancelWatcher := context.WithCancel(context.Background()) go WatchJob(ctx, job) - err = RunJob(job, store, kojiServers) + err = impl.Run(job) cancelWatcher() if err != nil { log.Printf("Job %s failed: %v", job.Id(), err)