コード例 #1
0
ファイル: azure_cluster.go プロジェクト: joshteng/flynn
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
}
コード例 #2
0
ファイル: digital_ocean_cluster.go プロジェクト: devick/flynn
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
}
コード例 #3
0
ファイル: aws_cluster.go プロジェクト: yanghongkjxy/flynn
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
}