osbuild-worker: allow adding key to aws.ec2 executor

This is useful during testing to set up the executor machine.
This commit is contained in:
Sanne Raymaekers 2024-02-28 11:03:03 +01:00
parent c480d79e95
commit 040eec4089
5 changed files with 15 additions and 6 deletions

View file

@ -74,6 +74,7 @@ type pulpConfig struct {
type executorConfig struct { type executorConfig struct {
Type string `toml:"type"` Type string `toml:"type"`
IAMProfile string `toml:"iam_profile"` IAMProfile string `toml:"iam_profile"`
KeyName string `toml:"key_name"`
} }
type workerConfig struct { type workerConfig struct {

View file

@ -79,6 +79,7 @@ type PulpConfiguration struct {
type ExecutorConfiguration struct { type ExecutorConfiguration struct {
Type string Type string
IAMProfile string IAMProfile string
KeyName string
} }
type OSBuildJobImpl struct { type OSBuildJobImpl struct {
@ -488,7 +489,7 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error {
case "host": case "host":
executor = osbuildexecutor.NewHostExecutor() executor = osbuildexecutor.NewHostExecutor()
case "aws.ec2": case "aws.ec2":
executor = osbuildexecutor.NewAWSEC2Executor(impl.OSBuildExecutor.IAMProfile) executor = osbuildexecutor.NewAWSEC2Executor(impl.OSBuildExecutor.IAMProfile, impl.OSBuildExecutor.KeyName)
default: default:
osbuildJobResult.JobError = clienterrors.WorkerClientError(clienterrors.ErrorInvalidConfig, "No osbuild executor defined", nil) osbuildJobResult.JobError = clienterrors.WorkerClientError(clienterrors.ErrorInvalidConfig, "No osbuild executor defined", nil)
return err return err

View file

@ -476,6 +476,7 @@ func main() {
OSBuildExecutor: ExecutorConfiguration{ OSBuildExecutor: ExecutorConfiguration{
Type: config.OSBuildExecutor.Type, Type: config.OSBuildExecutor.Type,
IAMProfile: config.OSBuildExecutor.IAMProfile, IAMProfile: config.OSBuildExecutor.IAMProfile,
KeyName: config.OSBuildExecutor.KeyName,
}, },
KojiServers: kojiServers, KojiServers: kojiServers,
GCPConfig: gcpConfig, GCPConfig: gcpConfig,

View file

@ -26,7 +26,7 @@ write_files:
// Runs an instance with a security group that only allows traffic to // Runs an instance with a security group that only allows traffic to
// the host. Will replace resources if they already exists. // the host. Will replace resources if they already exists.
func (a *AWS) RunSecureInstance(iamProfile string) (*SecureInstance, error) { func (a *AWS) RunSecureInstance(iamProfile, keyName string) (*SecureInstance, error) {
identity, err := a.ec2metadata.GetInstanceIdentityDocument() identity, err := a.ec2metadata.GetInstanceIdentityDocument()
if err != nil { if err != nil {
logrus.Errorf("Error getting the identity document, %s", err) logrus.Errorf("Error getting the identity document, %s", err)
@ -67,7 +67,7 @@ func (a *AWS) RunSecureInstance(iamProfile string) (*SecureInstance, error) {
return nil, err return nil, err
} }
ltID, err := a.createOrReplaceLT(identity.InstanceID, imageID, sgID, instanceType, iamProfile) ltID, err := a.createOrReplaceLT(identity.InstanceID, imageID, sgID, instanceType, iamProfile, keyName)
if ltID != "" { if ltID != "" {
secureInstance.LTID = ltID secureInstance.LTID = ltID
} }
@ -284,7 +284,7 @@ func isLaunchTemplateNotFoundError(err error) bool {
} }
func (a *AWS) createOrReplaceLT(hostInstanceID, imageID, sgID, instanceType, iamProfile string) (string, error) { func (a *AWS) createOrReplaceLT(hostInstanceID, imageID, sgID, instanceType, iamProfile, keyName string) (string, error) {
ltName := fmt.Sprintf("launch-template-for-%s-runner-instance", hostInstanceID) ltName := fmt.Sprintf("launch-template-for-%s-runner-instance", hostInstanceID)
descrLTOutput, err := a.ec2.DescribeLaunchTemplates(&ec2.DescribeLaunchTemplatesInput{ descrLTOutput, err := a.ec2.DescribeLaunchTemplates(&ec2.DescribeLaunchTemplatesInput{
LaunchTemplateNames: []*string{ LaunchTemplateNames: []*string{
@ -344,6 +344,10 @@ func (a *AWS) createOrReplaceLT(hostInstanceID, imageID, sgID, instanceType, iam
} }
} }
if keyName != "" {
input.LaunchTemplateData.KeyName = aws.String(keyName)
}
createLaunchTemplateOutput, err := a.ec2.CreateLaunchTemplate(input) createLaunchTemplateOutput, err := a.ec2.CreateLaunchTemplate(input)
if err != nil { if err != nil {
return "", err return "", err

View file

@ -14,6 +14,7 @@ import (
type awsEC2Executor struct { type awsEC2Executor struct {
iamProfile string iamProfile string
keyName string
} }
func (ec2e *awsEC2Executor) RunOSBuild(manifest []byte, store, outputDirectory string, exports, exportPaths, checkpoints, func (ec2e *awsEC2Executor) RunOSBuild(manifest []byte, store, outputDirectory string, exports, exportPaths, checkpoints,
@ -28,7 +29,7 @@ func (ec2e *awsEC2Executor) RunOSBuild(manifest []byte, store, outputDirectory s
return nil, err return nil, err
} }
si, err := aws.RunSecureInstance(ec2e.iamProfile) si, err := aws.RunSecureInstance(ec2e.iamProfile, ec2e.keyName)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -91,8 +92,9 @@ func (ec2e *awsEC2Executor) RunOSBuild(manifest []byte, store, outputDirectory s
return &osbuildResult, nil return &osbuildResult, nil
} }
func NewAWSEC2Executor(iamProfile string) Executor { func NewAWSEC2Executor(iamProfile, keyName string) Executor {
return &awsEC2Executor{ return &awsEC2Executor{
iamProfile, iamProfile,
keyName,
} }
} }