Esempio n. 1
0
func (c *AzureCluster) createKeyPair() error {
	keypairName := "flynn"
	if c.base.SSHKeyName != "" {
		keypairName = c.base.SSHKeyName
	}
	keypair, err := loadSSHKey(keypairName)
	if err == nil {
		c.base.SendLog(fmt.Sprintf("Using saved key pair (%s)", keypairName))
	} else {
		c.base.SendLog("Creating key pair")
		keypair, err = sshkeygen.Generate()
		if err != nil {
			return err
		}
	}
	c.base.SSHKey = keypair
	c.base.SSHKeyName = keypairName
	return nil
}
Esempio n. 2
0
func (c *DigitalOceanCluster) createKeyPair() error {
	keypairName := "flynn"
	if c.base.SSHKeyName != "" {
		keypairName = c.base.SSHKeyName
	}
	if err := c.loadKeyPair(keypairName); err == nil {
		c.base.SendLog(fmt.Sprintf("Using saved key pair (%s)", c.base.SSHKeyName))
		return nil
	}

	keypair, err := loadSSHKey(keypairName)
	if err == nil {
		c.base.SendLog("Importing key pair")
	} else {
		c.base.SendLog("Creating key pair")
		keypair, err = sshkeygen.Generate()
		if err != nil {
			return err
		}
	}

	key, _, err := c.client.Keys.Create(&godo.KeyCreateRequest{
		Name:      keypairName,
		PublicKey: string(keypair.PublicKey),
	})
	if err != nil {
		return err
	}

	c.base.SSHKey = keypair
	c.base.SSHKeyName = keypairName
	c.KeyFingerprint = key.Fingerprint
	if err := c.saveField("KeyFingerprint", c.KeyFingerprint); err != nil {
		return err
	}

	err = saveSSHKey(keypairName, keypair)
	if err != nil {
		return err
	}
	return nil
}
Esempio n. 3
0
func (c *AWSCluster) createKeyPair() error {
	keypairNames := listSSHKeyNames()
	if c.base.SSHKeyName != "" {
		newKeypairNames := make([]string, len(keypairNames)+1)
		newKeypairNames[0] = c.base.SSHKeyName
		for i, name := range keypairNames {
			newKeypairNames[i+1] = name
		}
		keypairNames = newKeypairNames
	}
	for _, name := range keypairNames {
		if err := c.loadKeyPair(name); err == nil {
			c.base.SendLog(fmt.Sprintf("Using saved key pair (%s)", c.base.SSHKeyName))
			return nil
		}
	}

	keypairName := "flynn"
	if c.base.SSHKeyName != "" {
		keypairName = c.base.SSHKeyName
	}

	keypair, err := loadSSHKey(keypairName)
	if err == nil {
		c.base.SendLog("Importing key pair")
	} else {
		c.base.SendLog("Creating key pair")
		keypair, err = sshkeygen.Generate()
		if err != nil {
			return err
		}
	}

	enc := base64.StdEncoding
	publicKeyBytes := make([]byte, enc.EncodedLen(len(keypair.PublicKey)))
	enc.Encode(publicKeyBytes, keypair.PublicKey)

	var res *ec2.ImportKeyPairResult
	err = c.wrapRequest(func() error {
		var err error
		res, err = c.ec2.ImportKeyPair(&ec2.ImportKeyPairRequest{
			KeyName:           aws.String(keypairName),
			PublicKeyMaterial: publicKeyBytes,
		})
		return err
	})
	if apiErr, ok := err.(aws.APIError); ok && apiErr.Code == "InvalidKeyPair.Duplicate" {
		if c.base.YesNoPrompt(fmt.Sprintf("Key pair %s already exists, would you like to delete it?", keypairName)) {
			c.base.SendLog("Deleting key pair")
			if err := c.wrapRequest(func() error {
				return c.ec2.DeleteKeyPair(&ec2.DeleteKeyPairRequest{
					KeyName: aws.String(keypairName),
				})
			}); err != nil {
				return err
			}
			return c.createKeyPair()
		}
		for {
			keypairName = c.base.PromptInput("Please enter a new key pair name")
			if keypairName != "" {
				c.base.SSHKeyName = keypairName
				return c.createKeyPair()
			}
		}
	}
	if err != nil {
		return err
	}

	c.base.SSHKey = keypair
	c.base.SSHKeyName = *res.KeyName

	err = saveSSHKey(keypairName, keypair)
	if err != nil {
		return err
	}
	return nil
}