Skip to content

Commit

Permalink
pass ssh private key to the instance creation
Browse files Browse the repository at this point in the history
  • Loading branch information
arturrez committed Jun 27, 2024
1 parent f8222c9 commit b6631cb
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions node/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type NodeParams struct {
Count int
Roles []SupportedRole
Network avalanche.Network
SSHPrivateKey string
AvalancheGoVersion string
AvalancheCliVersion string
UseStaticIP bool
Expand All @@ -30,8 +31,9 @@ type NodeParams struct {
func CreateNodes(
ctx context.Context,
nodeParams *NodeParams,

) ([]Node, error) {
nodes, err := createCloudInstances(ctx, *nodeParams.CloudParams, nodeParams.Count, nodeParams.UseStaticIP)
nodes, err := createCloudInstances(ctx, *nodeParams.CloudParams, nodeParams.Count, nodeParams.UseStaticIP, nodeParams.SSHPrivateKey)
if err != nil {
return nil, err
}
Expand All @@ -58,19 +60,22 @@ func CreateNodes(
}

// preCreateCheck checks if the cloud parameters are valid.
func preCreateCheck(cp CloudParams, count int) error {
func preCreateCheck(cp CloudParams, count int, sshPrivateKeyPath string) error {
if count < 1 {
return fmt.Errorf("count must be at least 1")
}
if err := cp.Validate(); err != nil {
return err
}
if sshPrivateKeyPath != "" && !utils.FileExists(sshPrivateKeyPath) {
return fmt.Errorf("ssh private key path %s does not exist", sshPrivateKeyPath)
}
return nil
}

// createCloudInstances launches the specified number of instances on the selected cloud platform.
func createCloudInstances(ctx context.Context, cp CloudParams, count int, useStaticIP bool) ([]Node, error) {
if err := preCreateCheck(cp, count); err != nil {
func createCloudInstances(ctx context.Context, cp CloudParams, count int, useStaticIP bool, sshPrivateKeyPath string) ([]Node, error) {
if err := preCreateCheck(cp, count, sshPrivateKeyPath); err != nil {
return nil, err
}
nodes := make([]Node, 0, count)
Expand Down Expand Up @@ -131,7 +136,8 @@ func createCloudInstances(ctx context.Context, cp CloudParams, count int, useSta
Cloud: cp.Cloud(),
CloudConfig: cp,
SSHConfig: SSHConfig{
User: constants.AnsibleSSHUser,
User: constants.AnsibleSSHUser,
PrivateKeyPath: sshPrivateKeyPath,
},
Roles: nil,
})
Expand Down Expand Up @@ -176,7 +182,8 @@ func createCloudInstances(ctx context.Context, cp CloudParams, count int, useSta
Cloud: cp.Cloud(),
CloudConfig: cp,
SSHConfig: SSHConfig{
User: constants.AnsibleSSHUser,
User: constants.AnsibleSSHUser,
PrivateKeyPath: sshPrivateKeyPath,
},
Roles: nil,
})
Expand Down

0 comments on commit b6631cb

Please sign in to comment.