-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
169 lines (157 loc) · 3.37 KB
/
main.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
package main
import (
"io/ioutil"
"log"
"os"
"fmt"
"path/filepath"
"os/exec"
"strings"
"github.com/urfave/cli"
"gopkg.in/ini.v1"
)
func main() {
a := cli.NewApp()
a.Version = "0.1.4"
a.Name = "boxter"
a.Usage = "manages syncing of playbook releases"
a.Commands = []cli.Command{
{
Name: "sync",
Usage: "syncs new or specified version of playbook",
Action: sync,
Flags: []cli.Flag{
cli.StringFlag{
Name: "config",
Usage: "path to the configuration file",
},
cli.StringFlag{
Name: "host",
Usage: "the name of the host machine",
},
cli.StringFlag{
Name: "boxid",
Usage: "unique id of the box",
},
cli.StringFlag{
Name: "remote-playbook-dir",
Usage: "the directory to sync playbooks to in a remote host",
},
cli.StringFlag{
Name: "ssh",
Usage: "ssh connection url",
},
cli.StringFlag{
Name: "rsh",
Usage: "passed to rsync",
},
},
},
}
err := a.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func sync(ctx *cli.Context) error {
host := ctx.String("host")
serial := ctx.String("boxid")
ssh := ctx.String("ssh")
rsh := ctx.String("rsh")
cFile := ctx.String("config")
rDir := ctx.String("remote-playbook-dir")
ver := ctx.Args().First()
b, err := ioutil.ReadFile(cFile)
if err != nil {
return err
}
cfg, err := newConfig(b)
if err != nil {
return err
}
if rDir != "" {
cfg.RemotePlaybookDir = rDir
}
v, ok := cfg.Get(host)
if !ok || strings.TrimSpace(v.Serial) != strings.TrimSpace(serial) {
return uauthorized(cfg, host, serial)
}
if ver != "" {
v.version = ver
}
if cfg.hasPlay(v.version) {
// if v.version == "latest" {
// v.version = cfg.latestPlay()
// }
return rsync(cfg, v, rsh, ssh)
}
return fmt.Errorf("no play for %s found", v)
}
func uauthorized(cfg *config, host, id string) error {
f := filepath.Join(cfg.SerialDir, "unauthorized.ini")
u := "unauthorized"
fi, err := os.OpenFile(f, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer fi.Close()
b, err := ioutil.ReadAll(fi)
if err != nil {
return err
}
var o *ini.File
if len(b) > 0 {
o, err = ini.Load(f)
if err != nil {
return err
}
s, err := o.GetSection(u)
if err != nil {
return err
}
k, err := s.GetKey(host)
if err != nil {
return err
}
k.SetValue(id)
} else {
o = ini.Empty()
s, err := o.NewSection(u)
if err != nil {
return err
}
s.NewKey(host, id)
}
_, err = o.WriteTo(fi)
return err
}
func rsync(cfg *config, ver hostProp, rsh, ssh string) error {
src := filepath.Join(cfg.LocalPlaybookDir, ver.version) + "/"
dest := filepath.Join(cfg.RemotePlaybookDir, "playbook")
fmt.Printf("syncing %s\n", ver)
cmd := exec.Command(
"rsync", "-rLptz", "--delete", "--rsh", rsh, src, fmt.Sprintf("%s:%s", ssh, dest),
)
b, err := cmd.CombinedOutput()
fmt.Println(string(b))
if err != nil {
return err
}
fmt.Println("OK")
fmt.Println("syncing mainfest")
o := filepath.Join(cfg.SerialDir, ver.Serial)
manifestFile := "voxbox-manifest.json"
os.MkdirAll(o, 0755)
hm := fmt.Sprintf("%s:%s", ssh, filepath.Join(cfg.RemotePlaybookDir, manifestFile))
sm := filepath.Join(o, manifestFile)
cmd = exec.Command(
"rsync", "-z", "--ignore-missing-args", "--rsh", rsh, hm, sm,
)
b, err = cmd.CombinedOutput()
fmt.Println(string(b))
if err != nil {
return err
}
fmt.Println("OK")
return nil
}