worker: introduce JobImplementation interface

Introduce JobImplementation and turn the current RunJob() into
OSBuildJobImpl. Make main() select a job impl based on job type.

This is in preparation to add additional impls.
This commit is contained in:
Lars Karlitski 2020-11-01 20:25:06 +01:00
parent 3bc642e4aa
commit 6b6cd7ca9f
2 changed files with 33 additions and 5 deletions

View file

@ -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

View file

@ -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)