-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync.go
261 lines (224 loc) · 6.62 KB
/
sync.go
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package cmd
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type SyncOptions struct {
CommonOptions
Daemon bool
NoKsyncInit bool
SingleMode bool
Container string
Namespace string
Pod string
Dir string
RemoteDir string
Reload bool
WatchOnly bool
stopCh chan struct{}
}
var (
sync_long = templates.LongDesc(`
Synchronises your local files to a DevPod so you an build and test your code easily on the cloud
For more documentation see: [https://jenkins-x.io/developing/devpods/](https://jenkins-x.io/developing/devpods/)
`)
sync_example = templates.Examples(`
# Starts synchonizing the current directory files to the users DevPod
jx sync
`)
defaultStignoreFile = `.git
.idea
.settings
.vscode
bin
build
target
node_modules
`
)
func NewCmdSync(f Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &SyncOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "sync",
Short: "Synchronises your local files to a devpod",
Long: sync_long,
Example: sync_example,
Aliases: []string{"log"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
/* cmd.Flags().StringVarP(&options.Container, "container", "c", "", "The name of the container to log")
cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "", "the namespace to look for the Deployment. Defaults to the current namespace")
cmd.Flags().StringVarP(&options.Pod, "pod", "p", "", "the pod name to use")
cmd.Flags().StringVarP(&options.Dir, "dir", "d", "", "The directory to watch. Defaults to the current directory")
cmd.Flags().StringVarP(&options.RemoteDir, "remote-dir", "r", "", "The remote directory in the DevPod to sync")
cmd.Flags().BoolVarP(&options.Reload, "reload", "", false, "Should we reload the remote container on file changes?")
*/
cmd.Flags().BoolVarP(&options.Daemon, "daemon", "", false, "Runs ksync in a background daemon")
cmd.Flags().BoolVarP(&options.NoKsyncInit, "no-init", "", false, "Disables the use of 'ksync init' to ensure we have initialised ksync")
cmd.Flags().BoolVarP(&options.SingleMode, "single-mode", "", false, "Terminates eagerly if `ksync watch` fails")
// deprecated
cmd.Flags().BoolVarP(&options.WatchOnly, "watch-only", "", false, "Deprecated this flag is now ignored!")
return cmd
}
func (o *SyncOptions) Run() error {
// ksync is installed to the jx/bin dir, so we can add it for the user
os.Setenv("PATH", util.PathWithBinary())
client, _, err := o.KubeClient()
if err != nil {
return err
}
_, err = o.installKSync()
if err != nil {
return err
}
if !o.NoKsyncInit {
flag, err := kube.IsDaemonSetExists(client, "ksync", "kube-system")
if !flag || err != nil {
log.Infof("Initialising ksync\n")
err = o.runCommandInteractive(true, "ksync", "init", "--upgrade")
if err != nil {
return err
}
}
}
if o.SingleMode {
return o.KsyncWatch()
}
for {
err = o.KsyncWatch()
if err != nil {
log.Warnf("Failed on ksync watch: %s\n", err)
}
}
}
func (o *SyncOptions) waitForKsyncWatchToFail() {
logged := false
for {
_, err := o.getCommandOutput("", "ksync", "get")
if err != nil {
// lets assume watch is no longer running
log.Infof("Looks like 'ksync watch' is not running: %s\n", err)
return
}
if !logged {
logged = true
log.Infof("It looks like 'ksync watch' is already running so we don't need to run it yet...\n")
}
time.Sleep(time.Second * 5)
}
}
func (o *SyncOptions) KsyncWatch() error {
o.waitForKsyncWatchToFail()
args := []string{"watch"}
if o.Daemon {
args = append(args, "--daemon")
}
cmd := exec.Command("ksync", args...)
cmd.Stdout = o.Out
cmd.Stderr = o.Out
err := cmd.Start()
if err != nil {
return err
}
log.Infof("Started the ksync watch\n")
time.Sleep(1 * time.Second)
state := cmd.ProcessState
if state != nil && state.Exited() {
return fmt.Errorf("ksync watch terminated")
}
return cmd.Wait()
}
// CreateKsync removes the exiting ksync if it already exists then create a new ksync of the given name
func (o *SyncOptions) CreateKsync(client kubernetes.Interface, ns string, name string, dir string, remoteDir string, username string) error {
// ksync is installed to the jx/bin dir, so we can add it for the user
os.Setenv("PATH", util.PathWithBinary())
info := util.ColorInfo
log.Infof("synchronizing directory %s to DevPod %s path %s\n", info(dir), info(name), info(remoteDir))
ignoreFile := filepath.Join(dir, ".stignore")
exists, err := util.FileExists(ignoreFile)
if err != nil {
return err
}
if !exists {
err = ioutil.WriteFile(ignoreFile, []byte(defaultStignoreFile), DefaultWritePermissions)
if err != nil {
return err
}
}
matchLabels := map[string]string{
kube.LabelDevPodUsername: username,
}
selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{MatchLabels: matchLabels})
if err != nil {
return err
}
_, pods, err := kube.GetPodsWithLabels(client, ns, selector.String())
if err != nil {
return err
}
// ignore the bad lines that come
ignoreNames := []string{"starting", "watching"}
deleteNames := []string{}
err = o.retry(5, time.Second, func() error {
text, err := o.getCommandOutput(dir, "ksync", "get")
if err == nil {
for i, line := range strings.Split(text, "\n") {
if i > 1 {
cols := strings.Split(strings.TrimSpace(line), " ")
if len(cols) > 2 {
n := strings.TrimSpace(cols[0])
if n == name || pods[n] == nil {
if util.StringArrayIndex(deleteNames, n) < 0 && util.StringArrayIndex(ignoreNames, n) < 0 {
deleteNames = append(deleteNames, n)
}
}
}
}
}
}
return err
})
if err != nil {
log.Warnf("Failed to get from ksync daemon: %s\n", err)
}
reload := "--reload=false"
if o.Reload {
reload = "--reload=true"
}
for _, n := range deleteNames {
// ignore results as we may not have a spec yet for this name
log.Infof("Removing old ksync %s\n", n)
o.runCommand("ksync", "delete", n)
}
time.Sleep(1 * time.Second)
return o.runCommand("ksync", "create", "--name", name, "-l", "jenkins.io/devpod="+name, reload, "-n", ns, dir, remoteDir)
}
func (o *SyncOptions) killWatchProcess(cmd *exec.Cmd) {
if err := cmd.Process.Kill(); err != nil {
log.Warnf("failed to kill 'ksync watch' process: %s\n", err)
}
}