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 string `toml:"type"`
IAMProfile string `toml:"iam_profile"`
KeyName string `toml:"key_name"`
}
type workerConfig struct {

View file

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

View file

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

View file

@ -26,7 +26,7 @@ write_files:
// Runs an instance with a security group that only allows traffic to
// 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()
if err != nil {
logrus.Errorf("Error getting the identity document, %s", err)
@ -67,7 +67,7 @@ func (a *AWS) RunSecureInstance(iamProfile string) (*SecureInstance, error) {
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 != "" {
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)
descrLTOutput, err := a.ec2.DescribeLaunchTemplates(&ec2.DescribeLaunchTemplatesInput{
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)
if err != nil {
return "", err

View file

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