This repository was archived by the owner on Nov 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcd.go
More file actions
129 lines (111 loc) · 2.81 KB
/
cd.go
File metadata and controls
129 lines (111 loc) · 2.81 KB
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
/*
* Copyright (c) 2020-present unTill Pro, Ltd. and Contributors
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package main
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"sync"
"time"
"github.com/spf13/cobra"
gc "github.com/untillpro/gochips"
)
var (
ctx context.Context
cancel context.CancelFunc
repoURLs []string
// used in tests
afterIteration func() = func() {}
onError func(r interface{}) = func(r interface{}) {}
)
func runCmdRoot(cmd *cobra.Command, args []string) error {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
// *************************************************
gc.Doing("Starting seeding")
if ctx == nil {
ctx, cancel = context.WithCancel(context.Background())
}
var wg sync.WaitGroup
wg.Add(1)
go seed(ctx, &wg)
go func() {
signal := <-signals
fmt.Println("Got signal:", signal)
cancel()
}()
// *************************************************
wg.Wait()
gc.Info("Finished")
return nil
}
func seed(ctx context.Context, wg *sync.WaitGroup) {
gc.Info("Seeding started")
defer func() {
if r := recover(); r != nil {
gc.Error("Recovered: ", r)
onError(r)
}
defer wg.Done()
deployer.Stop()
gc.Info("Seeding ended")
}()
timeoutDur := time.Duration(timeoutSec) * time.Second
// *************************************************
gc.Info("timeout", timeoutDur)
gc.Info("replacements", replacements)
gc.Info("repos", repoURLs)
if len(workingDir) > 0 {
gc.Info("Creating working dir...")
gc.PanicIfError(os.MkdirAll(workingDir, 0755))
}
// *************************************************
for _, initCmdCombined := range initCmds {
initCmdSplitted := strings.Split(initCmdCombined, ";")
for _, initCmd := range initCmdSplitted {
gc.Info("Executing init command:", initCmd)
initArgs := strings.Split(initCmd, " ")
gc.ExitIfError(new(gc.PipedExec).
Command(initArgs[0], initArgs[1:]...).
WorkingDir(workingDir).
Run(os.Stdout, os.Stderr))
}
}
for {
iteration()
afterIteration()
// TODO: clean WD after iteration
select {
case <-time.After(timeoutDur):
case <-ctx.Done():
gc.Verbose("seeder", "Done")
return
}
}
}
func iteration() {
defer func() {
if r := recover(); r != nil {
gc.Error("iteration: Recovered: ", r)
onError(r)
}
}()
gc.Verbose("iteration", "Checking if repos changed")
changedRepos := watcher.Watch(repoURLs)
if len(changedRepos) > 0 {
watcher.Clean(changedRepos) // clean before build
for _, changedRepo := range changedRepos {
deployer.Deploy(changedRepo)
}
deployer.DeployAll(changedRepos)
watcher.Clean(changedRepos) // clean after build. May be not reached in case of panic on Deploy*()
} else {
gc.Verbose("*** Nothing changed")
}
}