Skip to content

Commit

Permalink
Feature - SSH Portal introduction (#112)
Browse files Browse the repository at this point in the history
* including machinery and beginning ssh portal integration

* Updates sshportal integration

* Further work on option wrappers

* Assert that we cannot remote to remote

---------

Co-authored-by: Blaize Kaye <[email protected]>
  • Loading branch information
bomoko and Blaize Kaye authored Apr 23, 2024
1 parent 10100a0 commit 9a45e79
Show file tree
Hide file tree
Showing 9 changed files with 1,173 additions and 41 deletions.
72 changes: 71 additions & 1 deletion cmd/sync.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -38,6 +39,10 @@ var skipTargetImport bool
var localTransferResourceName string
var namedTransferResource string

var apiEndpoint string
var useSshPortal bool // This is our feature flag for now. With the major version, we change the ssh config details for lagoon-sync files
const fallbackApi = "https://api.lagoon.amazeeio.cloud/graphql"

var syncCmd = &cobra.Command{
Use: "sync [mariadb|files|mongodb|postgres|etc.]",
Short: "Sync a resource type",
Expand Down Expand Up @@ -168,6 +173,33 @@ func syncCommandRun(cmd *cobra.Command, args []string) {

sshOptionWrapper := synchers.NewSshOptionWrapper(ProjectName, sshOptions)

if useSshPortal {

//Let's work out the api endpoint
if configRoot.Api != "" && apiEndpoint != "" {
apiEndpoint = configRoot.Api
}

apiConn := utils.ApiConn{}
err := apiConn.Init(apiEndpoint, sshKey, "ssh.main.lagoon-core.test6.amazee.io", "22")
if err != nil {
fmt.Println(err.Error())
return
}

defaultSshOption, sshopts, err := getEnvironmentSshDetails(apiConn, ProjectName, sshOptions)
if err != nil {
fmt.Println(err.Error())
return
}

sshOptionWrapper.SetDefaultSshOptions(defaultSshOption)
for envName, option := range sshopts {
sshOptionWrapper.AddSsshOptionForEnvironment(envName, option)
}

}

// let's update the named transfer resource if it is set
if namedTransferResource != "" {
err = lagoonSyncer.SetTransferResource(namedTransferResource)
Expand All @@ -178,6 +210,11 @@ func syncCommandRun(cmd *cobra.Command, args []string) {

utils.LogDebugInfo("Config that is used for SSH", sshOptions)

// Add assertion - we no longer support Remote to Remote syncs
if sourceEnvironment.EnvironmentName != synchers.LOCAL_ENVIRONMENT_NAME && targetEnvironment.EnvironmentName != synchers.LOCAL_ENVIRONMENT_NAME {
utils.LogFatalError("Remote to Remote transfers are not supported", nil)
}

err = runSyncProcess(synchers.RunSyncProcessFunctionTypeArguments{
SourceEnvironment: sourceEnvironment,
TargetEnvironment: targetEnvironment,
Expand Down Expand Up @@ -208,6 +245,38 @@ func getServiceName(SyncerType string) string {
return "cli"
}

func getEnvironmentSshDetails(conn utils.ApiConn, projectName string, defaultSshOptions synchers.SSHOptions) (synchers.SSHOptions, map[string]synchers.SSHOptions, error) {
environments, err := conn.GetProjectEnvironmentDeployTargets(projectName)
retMap := map[string]synchers.SSHOptions{}

if err != nil {
return synchers.SSHOptions{}, retMap, err
}

var defaultOptions synchers.SSHOptions
defaultSet := false

for _, environment := range *environments {
retMap[environment.Name] = synchers.SSHOptions{
Host: environment.DeployTarget.SSHHost,
Port: environment.DeployTarget.SSHPort,
Verbose: defaultSshOptions.Verbose,
PrivateKey: "",
SkipAgent: defaultSshOptions.SkipAgent,
RsyncArgs: defaultSshOptions.RsyncArgs,
}

if environment.EnvironmentType == "production" {
defaultOptions = retMap[environment.Name]
defaultSet = true
}
}
if defaultSet == false {
return synchers.SSHOptions{}, retMap, errors.New("COULD NOT FIND DEFAULT OPTION SET")
}
return defaultOptions, retMap, nil
}

func confirmPrompt(message string) (bool, error) {
prompt := promptui.Prompt{
Label: message,
Expand Down Expand Up @@ -240,7 +309,8 @@ func init() {
syncCmd.PersistentFlags().BoolVar(&skipTargetCleanup, "skip-target-cleanup", false, "Don't clean up any of the files generated on the target")
syncCmd.PersistentFlags().BoolVar(&skipTargetImport, "skip-target-import", false, "This will skip the import step on the target, in combination with 'no-target-cleanup' this essentially produces a resource dump")
syncCmd.PersistentFlags().StringVarP(&namedTransferResource, "transfer-resource-name", "", "", "The name of the temporary file to be used to transfer generated resources (db dumps, etc) - random /tmp file otherwise")

syncCmd.PersistentFlags().StringVarP(&apiEndpoint, "api", "A", "", "Specify your lagoon api endpoint - required for ssh-portal integration")
syncCmd.PersistentFlags().BoolVar(&useSshPortal, "use-ssh-portal", false, "This will use the SSH Portal rather than the (soon to be removed) SSH Service on Lagoon core. Will become default in a future release.")
// By default, we hook up the syncers.RunSyncProcess function to the runSyncProcess variable
// by doing this, it lets us easily override it for testing the command - but for most of the time
// this should be okay.
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ require (
github.com/mitchellh/mapstructure v1.4.3
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.9.0
github.com/uselagoon/machinery v0.0.18
github.com/withmandala/go-log v0.1.0
golang.org/x/crypto v0.13.0
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
golang.org/x/crypto v0.19.0
golang.org/x/net v0.21.0
gopkg.in/ini.v1 v1.64.0 // indirect
gopkg.in/yaml.v2 v2.4.0
)
Loading

0 comments on commit 9a45e79

Please sign in to comment.