Repository navigation
Expand file tree
/
Copy pathsync_command.go
More file actions
128 lines (121 loc) · 3.34 KB
/
Copy pathsync_command.go
File metadata and controls
128 lines (121 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"code.cloudfoundry.org/cli/plugin"
"encoding/json"
"strings"
"time"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/cf/ssh/options"
"os"
"gopkg.in/urfave/cli.v1"
"errors"
)
const (
DEFAULT_SYNC_FOLDER = "sync"
DEFAULT_ROOT_TARGET_FOLDER = "~/app"
)
type SyncCommand struct {
cliConnection plugin.CliConnection
}
type SshInfo struct {
AppSSHEndpoint string `json:"app_ssh_endpoint"`
AppSSHHostKeyFingerprint string `json:"app_ssh_host_key_fingerprint"`
}
func (s SyncCommand) getSourceDir(c *cli.Context, appName string) string {
sourceDir := c.String("source")
if sourceDir == "" {
sourceDir = "./" + DEFAULT_SYNC_FOLDER + "-" + appName
}
return sourceDir
}
func (s SyncCommand) getTargetDir(c *cli.Context) string {
targetDir := c.String("target")
if targetDir == "" {
return DEFAULT_ROOT_TARGET_FOLDER
}
if !strings.HasPrefix(targetDir, "/") {
targetDir = "/" + targetDir
}
return DEFAULT_ROOT_TARGET_FOLDER + targetDir
}
func (s *SyncCommand) Sync(c *cli.Context) error {
forceSync := c.Bool("force-sync")
appName := c.Args().First()
if appName == "" {
return errors.New("You must pass an app name.")
}
sourceDir := s.getSourceDir(c, appName)
targetDir := s.getTargetDir(c)
logger.Info("Retrieving information about your app ...")
data, err := s.cliConnection.CliCommandWithoutTerminalOutput("curl", "/v2/info")
if err != nil {
return err
}
var sshInfo SshInfo
err = json.Unmarshal([]byte(strings.Join(data, "")), &sshInfo)
if err != nil {
return err
}
app, err := s.cliConnection.GetApp(appName)
if err != nil {
return err
}
sslDisabled, err := s.cliConnection.IsSSLDisabled()
if err != nil {
return err
}
logger.Info("Finished retrieving information about your app.\n")
logger.Info("Authenticating for ssh ...")
data, err = s.cliConnection.CliCommandWithoutTerminalOutput("ssh-code")
if err != nil {
return err
}
keepAliveInterval := 30 * time.Second
token := data[0]
secureShell := NewSecureShell(
DefaultSecureDialer(),
DefaultListenerFactory(),
keepAliveInterval,
models.Application{
ApplicationFields: models.ApplicationFields{
//must be diego to have ssh works
Diego: app.Diego,
// guid can be found by doing cf app myapp --guid
GUID: app.Guid,
// app must be start
State: app.State,
},
},
// this is a signature for ssh, it can be found when doing cf curl /v2/info
sshInfo.AppSSHHostKeyFingerprint,
// endpoint to connect in ssh, it can be found when doing cf curl /v2/info
sshInfo.AppSSHEndpoint,
// token retrieve when doing cf ssh-code
token,
)
err = secureShell.Connect(&options.SSHOptions{
AppName: appName,
SkipHostValidation: sslDisabled,
SkipRemoteExecution: true,
Command: []string{},
Index: uint(0),
ForwardSpecs: []options.ForwardSpec{},
TerminalRequest: options.RequestTTYAuto,
})
if err != nil {
return err
}
defer secureShell.Close()
logger.Info("Finished authenticating for ssh.")
keepaliveStopCh := make(chan struct{})
defer close(keepaliveStopCh)
go keepalive(secureShell.secureClient.Conn(), time.NewTicker(keepAliveInterval), keepaliveStopCh)
containerFiler := NewContainerFiler(secureShell.secureClient)
containerFiler.OutWriter = os.Stdout
sync, err := NewSync(containerFiler, sourceDir, targetDir)
if err != nil {
return err
}
sync.SetForceSync(forceSync)
return sync.Run()
}