Skip to content

Commit 79c2bac

Browse files
committed
test and bug fixes, refactors, and many other things.
1 parent c7ded3f commit 79c2bac

8 files changed

+222
-168
lines changed

harp.go

+51-138
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
// $HOME/harp/$APP/script
4545

4646
func init() {
47+
log.SetOutput(os.Stdout)
4748
if option.debug {
4849
log.SetFlags(log.Lshortfile)
4950
} else {
@@ -115,6 +116,14 @@ func (t *Tasks) Set(s string) error {
115116
return nil
116117
}
117118

119+
type FlagStrings []string
120+
121+
func (t FlagStrings) String() string { return "" }
122+
func (t *FlagStrings) Set(s string) error {
123+
*t = append(*t, s)
124+
return nil
125+
}
126+
118127
var (
119128
option = struct {
120129
configPath string
@@ -137,17 +146,21 @@ var (
137146
syncFileLimit int
138147

139148
// TODO: can specify a single server, instead of the whole server set
140-
server string
141-
serverSet string
142-
serverSets []string
149+
servers FlagStrings
150+
serverSets FlagStrings
143151
help bool
144152
version bool
145153

146154
buildArgs string
147155

148156
all bool
149157

158+
deploy string
159+
150160
tasks Tasks
161+
hand bool
162+
163+
cli bool
151164
}{}
152165

153166
migrations []Migration
@@ -195,10 +208,10 @@ func main() {
195208

196209
// flag.StringVar(&option.script, "scripts", "", "scripts to build and run on server")
197210

198-
flag.StringVar(&option.serverSet, "s", "", "specify server sets to deploy, multiple sets are split by comma")
199-
flag.StringVar(&option.serverSet, "server-set", "", "specify server sets to deploy, multiple sets are split by comma")
211+
flag.Var(&option.serverSets, "s", "specify server sets to deploy, multiple sets are split by comma")
212+
flag.Var(&option.serverSets, "server-set", "specify server sets to deploy, multiple sets are split by comma")
200213

201-
flag.StringVar(&option.server, "server", "", "specify servers to deploy, multiple servers are split by comma")
214+
flag.Var(&option.servers, "server", "specify servers to deploy, multiple servers are split by comma")
202215

203216
flag.BoolVar(&option.all, "all", false, "execute action on all server")
204217

@@ -207,7 +220,10 @@ func main() {
207220
// flag.StringVar(&option.migration, "m", "", "specify migrations to run on server, multiple migrations are split by comma")
208221
// flag.StringVar(&option.server, "server", "", "specify servers to deploy, multiple servers are split by comma")
209222

223+
flag.StringVar(&option.deploy, "deploy", "", "deploy app to servers/sets")
224+
210225
flag.Var(&option.tasks, "run", "run go scripts/packages on remote server.")
226+
flag.BoolVar(&option.hand, "hand", false, "pirnt out shell scripts could be executed by hand on remote servers")
211227

212228
flag.Parse()
213229

@@ -216,16 +232,19 @@ func main() {
216232
return
217233
}
218234

235+
var action string
219236
args := flag.Args()
220-
if len(migrations) > 0 {
221-
args = append(args, "run")
222-
}
223-
if len(args) == 0 || option.help {
237+
switch {
238+
case len(migrations) > 0:
239+
action = "run"
240+
case len(args) > 0:
241+
action = args[0]
242+
case len(args) == 0 || option.help:
224243
printUsage()
225244
return
226245
}
227246

228-
switch args[0] {
247+
switch action {
229248
case "init":
230249
initHarp()
231250
return
@@ -238,19 +257,22 @@ func main() {
238257
cfg = parseCfg(option.configPath)
239258

240259
var servers []*Server
241-
if args[0] != "cross-compile" && args[0] != "xc" {
260+
if action != "cross-compile" && action != "xc" {
242261
servers = retrieveServers()
243262
}
244263

245-
switch args[0] {
264+
switch action {
246265
case "kill":
247266
kill(servers)
248267
case "deploy":
249268
deploy(servers)
250269
case "migrate", "run":
251270
// TODO: could specify to run on all servers
252-
// migrations := retrieveMigrations(args[1:])
253-
// var server = cfg.Servers[serverSets[0]][0]
271+
if len(migrations) == 0 {
272+
log.Println("run command is deprecated. please use flag: -run.")
273+
log.Println("e.g. harp -s prod -run file.go -run file2.go")
274+
os.Exit(1)
275+
}
254276
migrate(servers, migrations)
255277
case "info":
256278
info(servers)
@@ -336,103 +358,6 @@ func deploy(servers []*Server) {
336358
wg.Wait()
337359
}
338360

339-
func syncFiles() {
340-
log.Println("syncing files")
341-
if err := os.MkdirAll(filepath.Join(tmpDir, "files"), 0755); err != nil {
342-
exitf("os.MkdirAll(.harp/files) error: %s", err)
343-
}
344-
345-
var wg sync.WaitGroup
346-
wg.Add(len(cfg.App.Files))
347-
for _, file := range cfg.App.Files {
348-
go func(f File) {
349-
defer func() { wg.Done() }()
350-
var src, gopath string
351-
for _, gopath = range GoPaths {
352-
src = filepath.Join(gopath, "src", f.Path)
353-
if _, err := os.Stat(src); err != nil {
354-
src = ""
355-
continue
356-
}
357-
358-
break
359-
}
360-
if src == "" {
361-
exitf("failed to find %s from %s", f.Path, GoPaths)
362-
}
363-
364-
dst := filepath.Join(tmpDir, "files", strings.Replace(f.Path, "/", "_", -1))
365-
if fi, err := os.Stat(src); err != nil {
366-
exitf("os.Stat(%s) error: %s", src, err)
367-
} else if fi.IsDir() {
368-
if option.debug {
369-
log.Println(dst, fi.Mode())
370-
}
371-
if err := os.Mkdir(dst, fi.Mode()); err != nil {
372-
exitf("os.Mkdir(%s) error: %s", dst, err)
373-
}
374-
} else {
375-
// a single file speicified in Files.
376-
copyFile(dst, src)
377-
}
378-
379-
// handle directory here
380-
base := filepath.Join(gopath, "src", f.Path)
381-
err := filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
382-
if err != nil {
383-
exitf("walk %s: %s", path, err)
384-
} else if path == base {
385-
return nil
386-
}
387-
388-
rel, err := filepath.Rel(base, path)
389-
if err != nil {
390-
exitf("fielpath.Rel(%s, %s) error: %s", base, path, err)
391-
}
392-
393-
for _, e := range append(cfg.App.DefaultExcludeds, f.Excludeds...) {
394-
matched, err := filepath.Match(e, rel)
395-
if err != nil {
396-
exitf("filepath.Match(%s, %s) error: %s", e, rel, err)
397-
}
398-
if !matched && !option.softExclude {
399-
matched = strings.Contains(rel, e)
400-
}
401-
if matched {
402-
if info.IsDir() {
403-
return filepath.SkipDir
404-
} else {
405-
return nil
406-
}
407-
}
408-
}
409-
410-
if info.IsDir() {
411-
if option.debug {
412-
log.Println(filepath.Join(dst, rel), info.Mode())
413-
}
414-
if err := os.Mkdir(filepath.Join(dst, rel), info.Mode()); err != nil {
415-
exitf("os.Mkdir(%s) error: %s", filepath.Join(dst, rel), err)
416-
}
417-
return nil
418-
}
419-
420-
wg.Add(1)
421-
go func() {
422-
defer func() { wg.Done() }()
423-
copyFile(filepath.Join(dst, rel), path)
424-
}()
425-
return nil
426-
})
427-
if err != nil && err != filepath.SkipDir {
428-
exitf("walking %s: %s", src, err)
429-
}
430-
}(file)
431-
}
432-
433-
wg.Wait()
434-
}
435-
436361
func info(servers []*Server) {
437362
var wg sync.WaitGroup
438363
for _, serv := range servers {
@@ -456,15 +381,11 @@ func parseCfg(configPath string) (cfg Config) {
456381
r, err := os.OpenFile(configPath, os.O_RDONLY, 0644)
457382
if err != nil {
458383
if os.IsNotExist(err) {
459-
fmt.Printf("Config %s doesn't exist or is unspecified.\nTo specify with flag -c (e.g. -c harp.json)\n", configPath)
460-
os.Exit(1)
461-
return
384+
exitf("Config %s doesn't exist or is unspecified.\nTo specify with flag -c (e.g. -c harp.json)", configPath)
462385
}
463386
exitf("failed to read config: %s", err)
464387
}
465-
r = JsonConfigReader.New(r)
466-
err = json.NewDecoder(r).Decode(&cfg)
467-
if err != nil {
388+
if err := json.NewDecoder(JsonConfigReader.New(r)).Decode(&cfg); err != nil {
468389
exitf("failed to parse config: %s", err)
469390
}
470391

@@ -650,31 +571,17 @@ examples:
650571
}
651572

652573
func retrieveServers() []*Server {
653-
var serverSets []string
654-
for _, set := range strings.Split(option.serverSet, ",") {
655-
set = strings.TrimSpace(set)
656-
if set == "" {
657-
continue
658-
}
659-
serverSets = append(serverSets, set)
660-
}
661-
662-
var servers []string
663-
for _, server := range strings.Split(option.server, ",") {
664-
server = strings.TrimSpace(server)
665-
if server == "" {
666-
continue
667-
}
668-
servers = append(servers, server)
669-
}
574+
serverSets := option.serverSets
575+
servers := option.servers
670576

671577
if option.all {
578+
serverSets = []string{}
672579
for set, _ := range cfg.Servers {
673580
serverSets = append(serverSets, set)
674581
}
675582
}
676583

677-
if option.server == "" && option.serverSet == "" {
584+
if len(servers) == 0 && len(serverSets) == 0 {
678585
println("please specify servers or server sets to deploy (-s or -server).")
679586
println("specify -all flag to execute the action on all servers.")
680587
os.Exit(1)
@@ -705,7 +612,13 @@ serversLoop:
705612
}
706613
}
707614
}
708-
println("unknown server:", server)
615+
616+
// one-shot servers
617+
if s := newOneShotServer(server); s != nil {
618+
targetServers = append(targetServers, newOneShotServer(server))
619+
} else {
620+
exitf("wrong url format (eg: name@host:port): %s", server)
621+
}
709622
os.Exit(1)
710623
}
711624

@@ -735,7 +648,7 @@ func initHarp() {
735648
"name": "%s",
736649
"importpath": "%s",
737650
"envs": {},
738-
"DefaultExcludeds": [".git/", "tmp/", ".DS_Store", "node_modules/", "*.swp"],
651+
"DefaultExcludeds": [".git/", "tmp/", ".DS_Store", "node_modules/", "*.swp", "*.go"],
739652
"files": [
740653
"%s"
741654
]

harp_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func TestRetrieveServers(t *testing.T) {
2929
},
3030
},
3131
}
32-
serverSet = "prod"
33-
server = "[email protected]:49156"
32+
option.serverSets = []string{"prod"}
33+
option.servers = []string{"[email protected]:49156"}
3434
servers := retrieveServers()
3535
if len(servers) != 3 {
3636
t.Error("failed to retrieve 3 correct servers")

harp_test.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ tmp/harp -c test/harp2.json -s prod -run github.com/bom-d-van/harp/test/migratio
6767
echo ====================
6868
echo tmp/harp -c test/harp.json -s prod rollback ls
6969
for version in `tmp/harp -c test/harp.json -s prod rollback ls | tail -2`; do
70-
echo rollback version: $version
70+
echo "tmp/harp -c test/harp.json -s prod rollback $version"
7171
tmp/harp -c test/harp.json -s prod rollback $version
72-
ssh app@$dmip -p 49153 -- cat /home/app/harp/app/log/app.log
72+
ssh app@$dmip -p 49153 -- tail /home/app/harp/app/log/app.log
7373
# ssh app@$dmip -p 49153 -- cat /home/app/src/github.com/bom-d-van/harp/test/files/file1
74-
ssh app@$dmip -p 49153 -- cat /home/app/src/github.com/bom-d-van/harp/test/files/file2
74+
# ssh app@$dmip -p 49153 -- cat /home/app/src/github.com/bom-d-van/harp/test/files/file2
7575
done
7676

7777
git checkout -- test/test_version.go test/files/file1 test/files/file2

0 commit comments

Comments
 (0)