feat: openports
This commit is contained in:
77
internal/space_worker/openports.go
Normal file
77
internal/space_worker/openports.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package space_worker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"playbookctl/internal/types"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (app *SpaceWorker) Openports(targetHost string) error {
|
||||
app.log.Debug(fmt.Sprintf("workDir: %s", app.workDir))
|
||||
|
||||
var ansibleHost string
|
||||
if targetHost != "" {
|
||||
ansibleHost = targetHost
|
||||
} else {
|
||||
var err error
|
||||
ansibleHost, err = app.GetDefaultHost()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
app.log.Trace("try read hosts.yml")
|
||||
hosts, err := types.ReadHosts(app.workDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var sshTargetHost string
|
||||
var sshTargetPort uint16
|
||||
var sshTargetUser string
|
||||
for serverName, props := range *hosts {
|
||||
if serverName == ansibleHost {
|
||||
sshTargetHost = props.Host
|
||||
sshTargetPort = props.Port
|
||||
sshTargetUser = props.User
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
app.log.Trace("try read ports.yml")
|
||||
portsDef, err := types.ReadPortsDef(app.workDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
message := []string{"Open SSH connection to"}
|
||||
sshArgs := []string{"-N"}
|
||||
for _, portDef := range *portsDef {
|
||||
sshArgs = append(sshArgs, "-L", fmt.Sprintf("127.0.0.1:%d:127.0.0.1:%d", portDef.LocalPort, portDef.RemotePort))
|
||||
|
||||
if portDef.Url == "" {
|
||||
message = append(message, fmt.Sprintf(" - %s (%d -> %d)", portDef.Name, portDef.LocalPort, portDef.RemotePort))
|
||||
} else {
|
||||
message = append(message, fmt.Sprintf(" - %s (%d -> %d) :: %s", portDef.Name, portDef.LocalPort, portDef.RemotePort, portDef.Url))
|
||||
}
|
||||
}
|
||||
sshArgs = append(sshArgs, "-p", strconv.Itoa(int(sshTargetPort)), fmt.Sprintf("%s@%s", sshTargetUser, sshTargetHost))
|
||||
|
||||
app.log.Info(strings.Join(message, "\n"))
|
||||
|
||||
command := exec.Command("/usr/bin/ssh", sshArgs...)
|
||||
command.Dir = app.workDir
|
||||
command.Stdin = os.Stdin
|
||||
command.Stdout = os.Stdout
|
||||
command.Stderr = os.Stderr
|
||||
|
||||
app.log.Debug(fmt.Sprintf("ssh command line: %s", command.Args))
|
||||
err = command.Run()
|
||||
|
||||
return err
|
||||
}
|
||||
30
internal/types/ports.go
Normal file
30
internal/types/ports.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"gopkg.in/yaml.v3"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type PortDef struct {
|
||||
Name string `yaml:"name"`
|
||||
LocalPort uint16 `yaml:"local"`
|
||||
RemotePort uint16 `yaml:"remote"`
|
||||
Url string `yaml:"url"`
|
||||
}
|
||||
|
||||
func ReadPortsDef(workDir string) (*[]PortDef, error) {
|
||||
var portsDef []PortDef
|
||||
{
|
||||
bb, err := os.ReadFile(filepath.Join(workDir, "ports.yml"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := yaml.Unmarshal(bb, &portsDef); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &portsDef, nil
|
||||
}
|
||||
Reference in New Issue
Block a user