// CreateKeyPair creates a KeyPair of a specified class in the specified region func CreateKeyPair(class, region string, dryRun bool) error { if dryRun { terminal.Information("--dry-run flag is set, not making any actual changes!") } // KeyPair Class Config keypairCfg, err := config.LoadKeyPairClass(class) if err != nil { return err } terminal.Information("Found KeyPair class configuration for [" + class + "]!") // Validate the region if !regions.ValidRegion(region) { return errors.New("Region [" + region + "] is Invalid!") } // Import the KeyPair to the requested region err = importKeyPair(region, class, []byte(keypairCfg.PublicKey), dryRun) if err != nil { return err } return nil }
// InstallKeyPair installs a keypair locally func InstallKeyPair(class string, dryRun bool) error { // KeyPair Class Config keypairCfg, err := config.LoadKeyPairClass(class) if err != nil { return err } terminal.Information("Found KeyPair class configuration for [" + class + "]!") if !dryRun { currentUser, _ := user.Current() sshLocation := currentUser.HomeDir + "/.ssh/" privateKeyPath := sshLocation + class + ".pem" publicKeyPath := sshLocation + class + ".pub" // Private Key if _, err := os.Stat(privateKeyPath); !os.IsNotExist(err) { terminal.Information("Local private key named [" + class + "] already exists!") } else { privateKey := []byte(keypairCfg.PrivateKey1 + keypairCfg.PrivateKey2 + keypairCfg.PrivateKey3 + keypairCfg.PrivateKey4) err = ioutil.WriteFile(privateKeyPath, privateKey, 0655) if err != nil { return err } terminal.Information("Created private key at [" + privateKeyPath + "]") } // Public Key if _, err := os.Stat(publicKeyPath); !os.IsNotExist(err) { terminal.Information("Local public key named [" + class + "] already exists!") } else { err = ioutil.WriteFile(publicKeyPath, []byte(keypairCfg.PublicKey), 0655) if err != nil { return err } terminal.Information("Created public key at [" + publicKeyPath + "]") } } return nil }