-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexander Neishkasha
committed
Jan 15, 2020
1 parent
6dde730
commit 370afbe
Showing
3 changed files
with
70 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ import ( | |
"log" | ||
) | ||
|
||
const RunningCode = 16 | ||
|
||
type AwsFacade struct { | ||
region string | ||
env string | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"github.com/creack/pty" | ||
"golang.org/x/crypto/ssh/terminal" | ||
"io" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"os/signal" | ||
"syscall" | ||
) | ||
|
||
type SshFacade struct { | ||
ip string | ||
env string | ||
} | ||
|
||
func (ssh *SshFacade) Connect() { | ||
pathToKey, err := ssh.pathToKey() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
cmd := exec.Command("ssh", "-i"+pathToKey, "ubuntu@"+ssh.ip) | ||
// Start the command with a pty. | ||
ptmx, err := pty.Start(cmd) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
// Make sure to close the pty at the end. | ||
defer func() { _ = ptmx.Close() }() // Best effort. | ||
|
||
// Handle pty size. | ||
ch := make(chan os.Signal, 1) | ||
signal.Notify(ch, syscall.SIGWINCH) | ||
go func() { | ||
for range ch { | ||
if err := pty.InheritSize(os.Stdin, ptmx); err != nil { | ||
log.Printf("error resizing pty: %s", err) | ||
} | ||
} | ||
}() | ||
ch <- syscall.SIGWINCH // Initial resize. | ||
|
||
// Set stdin in raw mode. | ||
oldState, err := terminal.MakeRaw(int(os.Stdin.Fd())) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer func() { _ = terminal.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort. | ||
|
||
// Copy stdin to the pty and the pty to stdout. | ||
go func() { _, _ = io.Copy(ptmx, os.Stdin) }() | ||
_, _ = io.Copy(os.Stdout, ptmx) | ||
} | ||
|
||
func (ssh *SshFacade) pathToKey() (key string, err error) { | ||
if ssh.env == "stg" { | ||
return os.Getenv("STAGE_KEY"), nil | ||
} else if ssh.env == "prod" { | ||
return os.Getenv("PROD_KEY"), nil | ||
} else { | ||
return "", errors.New("Key for env '" + ssh.env + "' not found") | ||
} | ||
} |