-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sync.ts
90 lines (79 loc) · 2.3 KB
/
Sync.ts
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
import {ChildProcess, spawn} from 'child_process'
import {join} from 'path'
import Config from './Config'
import {NodeTunnel, SyncType, TargetPod} from './types'
class Sync {
public type: SyncType
public paths: Set<string> = new Set()
public targetPod: TargetPod
public error?: Error
constructor(type: SyncType, targetPod: TargetPod) {
this.type = type
this.targetPod = targetPod
}
public execute(config: Config, tunnel: NodeTunnel) {
const {targetPod} = this
if (!targetPod.containerFsPath) {
return Promise.reject(
new Error('Tried to sync pod with unresolved containerFsPath')
)
}
const rsyncSource = targetPod.syncSpec.localPath
? targetPod.syncSpec.localPath + '/'
: '.'
const relativeRsyncTargetPath = join(
targetPod.containerFsPath.split(`${config.nodeOverlay2Path}/`)[1],
targetPod.syncSpec.containerPath
)
const rsyncTarget = `rsync://localhost:${
tunnel.rsyncPort
}/overlay2/${relativeRsyncTargetPath}/`
const rsyncArgs = [
'--recursive',
'--links',
'--executability',
'--times',
'--compress',
'--delete-after',
]
for (const dir of targetPod.syncSpec.excludeDirs) {
rsyncArgs.push('--exclude')
rsyncArgs.push(dir)
}
let rsyncProcess: ChildProcess
let stderr = ''
if (this.type === SyncType.Full) {
rsyncProcess = spawn(
config.rsyncPath,
[...rsyncArgs, rsyncSource, rsyncTarget],
{cwd: config.rootPath}
)
rsyncProcess.stderr.on('data', data => (stderr += data.toString()))
} else if (this.type === SyncType.Partial) {
rsyncProcess = spawn(
config.rsyncPath,
[
...rsyncArgs,
'--delete-missing-args',
'--files-from=-',
rsyncSource,
rsyncTarget,
],
{cwd: config.rootPath}
)
rsyncProcess.stderr.on('data', data => (stderr += data.toString()))
rsyncProcess.stdin.write(Array.from(this.paths).join('\n'))
rsyncProcess.stdin.end()
}
return new Promise((resolve, reject) => {
rsyncProcess.on('close', code => {
if (code) {
const error = new Error(`(${code}): ${stderr}`)
this.error = error
}
resolve()
})
})
}
}
export default Sync