forked from benjaminbollen/mintnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
126 lines (109 loc) · 3.58 KB
/
util.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
package main
import (
"errors"
"fmt"
"io/ioutil"
"strings"
. "github.com/tendermint/go-common"
pcm "github.com/tendermint/go-process"
"github.com/tendermint/go-wire"
)
// Copy a file (or dir recursively) from srcPath (local machine) to
// dstPath in the tmcore container.
func copyToMachine(mach string, app string, srcPath string, dstPath string, copyContents bool) error {
// First, copy the file to a temporary location
// in the machine.
tempFile := "temp_" + RandStr(12)
args := []string{"scp", "-r", srcPath, mach + ":" + tempFile}
if !runProcess("scp-file-"+mach, "docker-machine", args, true) {
return errors.New("Failed to copy file to machine " + mach)
}
// Next, docker cp the file into the container
if copyContents {
tempFile = tempFile + "/."
}
args = []string{"ssh", mach, Fmt("docker cp %v %v_tmcommon:%v", tempFile, app, dstPath)}
if !runProcess("docker-cp-file-"+mach, "docker-machine", args, true) {
return errors.New("Failed to docker-cp file to container in machine " + mach)
}
// Next, change the ownership of the file to tmuser
// TODO We don't really want to change all the permissions
args = []string{"ssh", mach, Fmt(`docker run --rm --volumes-from %v_tmcommon -u root tendermint/tmbase chown -R tmuser:tmuser %v`, app, dstPath)}
if !runProcess("docker-chmod-file-"+mach, "docker-machine", args, true) {
return errors.New("Failed to docker-run(chmod) file in machine " + mach)
}
// TODO: remove tempFile
return nil
}
// NOTE: returns false if any error
func checkFileExists(mach string, container string, path string) bool {
args := []string{"ssh", mach, Fmt(`docker exec %v ls %v`, container, path)}
_, ok := runProcessGetResult("check-file-exists-"+mach, "docker-machine", args, false)
return ok
}
//--------------------------------------------------------------------------------
func runProcess(label string, command string, args []string, verbose bool) bool {
_, res := runProcessGetResult(label, command, args, verbose)
return res
}
func runProcessGetResult(label string, command string, args []string, verbose bool) (string, bool) {
outFile := NewBufferCloser(nil)
proc, err := pcm.StartProcess(label, command, args, nil, outFile)
if err != nil {
if verbose {
fmt.Println(Red(err.Error()))
}
return "", false
}
<-proc.WaitCh
if verbose {
fmt.Println(Green(command), Green(args))
}
if proc.ExitState.Success() {
if verbose {
fmt.Println(Blue(string(outFile.Bytes())))
}
return string(outFile.Bytes()), true
} else {
// Error!
if verbose {
fmt.Println(Red(string(outFile.Bytes())))
}
return string(outFile.Bytes()), false
}
}
//--------------------------------------------------------------------------------
func eB(s string) string {
s = strings.Replace(s, `\`, `\\`, -1)
s = strings.Replace(s, `$`, `\$`, -1)
s = strings.Replace(s, `"`, `\"`, -1)
s = strings.Replace(s, `'`, `\'`, -1)
s = strings.Replace(s, `!`, `\!`, -1)
s = strings.Replace(s, `#`, `\#`, -1)
s = strings.Replace(s, `%`, `\%`, -1)
s = strings.Replace(s, "\t", `\t`, -1)
s = strings.Replace(s, "`", "\\`", -1)
return s
}
func condenseBash(cmd string) string {
cmd = strings.TrimSpace(cmd)
lines := strings.Split(cmd, "\n")
res := []string{}
for _, line := range lines {
line = strings.TrimSpace(line)
res = append(res, line)
}
return strings.Join(res, "; ")
}
//--------------------------------------------------------------------------------
func ReadJSONFile(o interface{}, filename string) error {
b, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
wire.ReadJSON(o, b, &err)
if err != nil {
return err
}
return nil
}