Skip to content

Commit

Permalink
osbuild-worker: add pulp configuration
Browse files Browse the repository at this point in the history
Add support for pulp client configuration in the worker config.
  • Loading branch information
achilleas-k committed Aug 21, 2023
1 parent 5f27fd1 commit 40d2754
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 23 deletions.
16 changes: 3 additions & 13 deletions cmd/osbuild-upload-pulp-ostree/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"io"
"os"

"github.com/osbuild/osbuild-composer/internal/upload/pulp"
Expand All @@ -17,16 +15,6 @@ func check(err error) {
}
}

func readCredentials(credPath string) *pulp.Credentials {
fp, err := os.Open(credPath)
check(err)
data, err := io.ReadAll(fp)
check(err)
var creds pulp.Credentials
check(json.Unmarshal(data, &creds))
return &creds
}

func main() {
var filename, apiURL, repository, basePath, credsFile string
flag.StringVar(&filename, "archive", "", "ostree archive to upload")
Expand All @@ -36,9 +24,11 @@ func main() {
flag.StringVar(&credsFile, "credentials", "", `file containing credentials (format: {"username": "...", "password": "..."})`)
flag.Parse()

client := pulp.NewClient(apiURL, readCredentials(credsFile))
client, err := pulp.NewClientFromFile(apiURL, credsFile)
check(err)

repoURL, err := client.UploadAndDistributeCommit(filename, repository, basePath)
check(err)

fmt.Printf("The commit will be available in the repository at %s\n", repoURL)
}
6 changes: 6 additions & 0 deletions cmd/osbuild-worker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ type containersConfig struct {
TLSVerify bool `toml:"tls_verify"`
}

type pulpConfig struct {
Credentials string `toml:"credentials"`
ServerURL string `toml:"server_address"`
}

type workerConfig struct {
Composer *composerConfig `toml:"composer"`
Koji map[string]kojiServerConfig `toml:"koji"`
Expand All @@ -71,6 +76,7 @@ type workerConfig struct {
GenericS3 *genericS3Config `toml:"generic_s3"`
Authentication *authenticationConfig `toml:"authentication"`
Containers *containersConfig `toml:"containers"`
Pulp *pulpConfig `toml:"pulp"`
// default value: /api/worker/v1
BasePath string `toml:"base_path"`
DNFJson string `toml:"dnf-json"`
Expand Down
25 changes: 17 additions & 8 deletions cmd/osbuild-worker/jobimpl-osbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type OSBuildJobImpl struct {
AWSBucket string
S3Config S3Configuration
ContainersConfig ContainersConfiguration
PulpConfig PulpConfiguration
}

// Returns an *awscloud.AWS object with the credentials of the request. If they
Expand Down Expand Up @@ -267,23 +268,31 @@ func (impl *OSBuildJobImpl) getContainerClient(destination string, targetOptions
}

func (impl *OSBuildJobImpl) getPulpClient(targetOptions *target.PulpOSTreeTargetOptions) (*pulp.Client, error) {
creds := &pulp.Credentials{}
var creds *pulp.Credentials
if targetOptions.Username != "" && targetOptions.Password != "" {
creds = &pulp.Credentials{
Username: targetOptions.Username,
Password: targetOptions.Password,
}
} else {
// TODO: read from worker configuration
return nil, fmt.Errorf("no credentials for pulp were set")
}

if targetOptions.ServerAddress == "" {
// TODO: read from worker configuration
address := targetOptions.ServerAddress
if address == "" {
address = impl.PulpConfig.ServerAddress
}
if address == "" {
return nil, fmt.Errorf("pulp server address not set")
}

return pulp.NewClient(targetOptions.ServerAddress, creds), nil
if creds != nil {
return pulp.NewClient(address, creds), nil
}

// read from worker configuration
if impl.PulpConfig.CredsFilePath == "" {
return nil, fmt.Errorf("pulp credentials not set")
}

return pulp.NewClientFromFile(impl.PulpConfig.ServerAddress, impl.PulpConfig.CredsFilePath)
}

func (impl *OSBuildJobImpl) Run(job worker.Job) error {
Expand Down
11 changes: 11 additions & 0 deletions cmd/osbuild-worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,13 @@ func main() {
containersTLSVerify = config.Containers.TLSVerify
}

var pulpCredsFilePath = ""
var pulpAddress = ""
if config.Pulp != nil {
pulpCredsFilePath = config.Pulp.Credentials
pulpAddress = config.Pulp.ServerURL
}

// depsolve jobs can be done during other jobs
depsolveCtx, depsolveCtxCancel := context.WithCancel(context.Background())
solver := dnfjson.NewBaseSolver(rpmmd_cache)
Expand Down Expand Up @@ -455,6 +462,10 @@ func main() {
CertPath: containersCertPath,
TLSVerify: &containersTLSVerify,
},
PulpConfig: PulpConfiguration{
CredsFilePath: pulpCredsFilePath,
ServerAddress: pulpAddress,
},
},
worker.JobTypeKojiInit: &KojiInitJobImpl{
KojiServers: kojiServers,
Expand Down
24 changes: 22 additions & 2 deletions internal/upload/pulp/pulp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pulp

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
Expand All @@ -17,8 +18,27 @@ type Client struct {
}

type Credentials struct {
Username string
Password string
Username string `json:"username"`
Password string `json:"password"`
}

func NewClientFromFile(url, path string) (*Client, error) {
fp, err := os.Open(path)
if err != nil {
return nil, err
}
defer fp.Close()

data, err := io.ReadAll(fp)
if err != nil {
return nil, err
}
var creds Credentials
if err := json.Unmarshal(data, &creds); err != nil {
return nil, err
}

return NewClient(url, &creds), nil
}

func NewClient(url string, creds *Credentials) *Client {
Expand Down

0 comments on commit 40d2754

Please sign in to comment.