Skip to content

Commit

Permalink
move Establish and Start functions to wireguard agent package
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldwan committed Aug 10, 2021
1 parent db0e75f commit 156f2ec
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
16 changes: 1 addition & 15 deletions cmd/fly_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func runFlyAgentStart(ctx *cmdctx.CmdContext) error {
c.Kill()
}

_, err = EstablishFlyAgent(ctx)
_, err = agent.Establish(api)
if err != nil {
fmt.Fprintf(os.Stderr, "can't start agent: %s", err)
}
Expand All @@ -81,17 +81,3 @@ func runFlyAgentStop(ctx *cmdctx.CmdContext) error {

return err
}

func EstablishFlyAgent(ctx *cmdctx.CmdContext) (*agent.Client, error) {
api := ctx.Client.API()

c, err := agent.DefaultClient(api)
if err == nil {
_, err := c.Ping()
if err == nil {
return c, nil
}
}

return StartAgent(api, os.Args[0])
}
2 changes: 1 addition & 1 deletion cmd/ssh_terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func runSSHConsole(ctx *cmdctx.CmdContext) error {
return fmt.Errorf("get app: %w", err)
}

agentclient, err := EstablishFlyAgent(ctx)
agentclient, err := agent.Establish(client)
if err != nil {
return fmt.Errorf("can't establish agent: %s\n", err)
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ func (s *Server) tunnelFor(slug string) (*wg.Tunnel, error) {
}

func resolve(tunnel *wg.Tunnel, addr string) (string, error) {
log.Printf("Resolving %v %s", tunnel, addr)
host, port, err := net.SplitHostPort(addr)
if err != nil {
return "", err
Expand Down Expand Up @@ -437,3 +438,18 @@ func captureWireguardConnErr(err error, org string) {
&wireGuardConnErr{Org: org, Err: err},
)
}

/// Establish starts the daemon if necessary and returns a client
func Establish(apiClient *api.Client) (*Client, error) {
c, err := DefaultClient(apiClient)
if err == nil {
_, err := c.Ping()
if err == nil {
return c, nil
}
}

fmt.Println("command", os.Args[0])

return StartDaemon(apiClient, os.Args[0])
}
9 changes: 4 additions & 5 deletions cmd/agent_start.go → pkg/agent/start.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build !windows

package cmd
package agent

import (
"fmt"
Expand All @@ -9,10 +9,9 @@ import (
"time"

"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/pkg/agent"
)

func StartAgent(api *api.Client, command string) (*agent.Client, error) {
func StartDaemon(api *api.Client, command string) (*Client, error) {
cmd := exec.Command(command, "agent", "daemon-start")
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
Expand All @@ -25,10 +24,10 @@ func StartAgent(api *api.Client, command string) (*agent.Client, error) {

// this is gross placeholder logic

for i := 0; i < 5; i++ {
for i := 0; i < 20; i++ {
time.Sleep(100 * time.Millisecond)

c, err := agent.DefaultClient(api)
c, err := DefaultClient(api)
if err == nil {
_, err := c.Ping()
if err == nil {
Expand Down
5 changes: 2 additions & 3 deletions cmd/agent_start_noagent.go → pkg/agent/start_noagent.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// +build windows

package cmd
package agent

import (
"fmt"

"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/pkg/agent"
)

func StartAgent(api *api.Client, cmd string) (*agent.Client, error) {
func StartDaemon(api *api.Client, cmd string) (*Client, error) {
return nil, fmt.Errorf("can't start agent on this platform (this is a bug, please report)")
}

0 comments on commit 156f2ec

Please sign in to comment.