-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathga_service.go
More file actions
181 lines (156 loc) · 4.94 KB
/
ga_service.go
File metadata and controls
181 lines (156 loc) · 4.94 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
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
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"os"
"strings"
"time"
"github.com/golang/glog"
)
var (
resyncPeriod = 30 * time.Second
resyncInProgress = false
)
// GA Server
type gaServer struct {
// Command line / environment supplied configuration values
cfg *config
repoURL string
stopCh chan struct{}
}
func newGAServer(cfg *config) *gaServer {
return &gaServer{
stopCh: make(chan struct{}),
cfg: cfg,
repoURL: GitProtocolHTTPS + *cfg.username + ":" + *cfg.password + "@" + *cfg.server + "/" + *cfg.account + "/" + *cfg.repository + ".git",
}
}
func (as *gaServer) setDirectory() bool {
err := os.Chdir(*as.cfg.directory)
if err != nil {
glog.Errorf("error: executing chdir %s, returned: %v", *as.cfg.directory, err)
return false
}
if glog.V(2) {
dir, _ := os.Getwd()
glog.Infof("current working directory: %s", dir)
}
return true
}
func (as *gaServer) clone() bool {
if *as.cfg.initialize == false {
glog.V(2).Info("no initial repository clone requested")
return true
}
glog.V(2).Infof("clone initial repository: %s", *as.cfg.repository)
var cloneArgs = []string{GitClone, GitArgDepth, "1", as.repoURL}
if *as.cfg.directory != "" {
cloneArgs = append(cloneArgs, *as.cfg.directory)
}
_, err := Execute(GitCmd, cloneArgs)
if err != nil {
glog.Errorf("error: executing %s %s, returned: %v", GitCmd, GitClone, err)
return false
}
return true
}
func (as *gaServer) pushUpdates() {
glog.V(2).Infof("push updates at: %v", time.Now())
_, err := Execute(GitCmd, []string{GitPush, as.repoURL, GitBranchMaster})
if err != nil {
glog.Warningf("error: executing %s %s %s %s, returned: %v",
GitCmd, GitPush, GitRemoteOrigin, GitBranchMaster, err)
}
}
func (as *gaServer) commitUpdates() {
glog.V(2).Infof("commit updates at: %v", time.Now())
configAuthor := GitConfigUserName + "=\"" + *as.cfg.username + "\""
configEmail := GitConfigUserEmail + "=\"" + *as.cfg.email + "\""
commitAuthor := "\"" + *as.cfg.username + " <" + *as.cfg.email + ">\""
commitMessage := "\"" + "git-archivist: auto update: " + time.Now().String() + "\""
_, err := Execute(GitCmd, []string{GitArgC, configAuthor, GitArgC, configEmail, GitCommit, GitArgAuthor, commitAuthor, GitArgAM, commitMessage})
if err != nil {
glog.Warningf("error: executing %s %s %s, returned: %v",
GitCmd, GitCommit, commitMessage, err)
return
}
as.pushUpdates()
}
func (as *gaServer) checkForUpdates() {
// resyncInProgress is not a mutext, but a trivial way to avoid obvious failure modes
if resyncInProgress {
glog.V(2).Infof("resync currently in progress, skipping check for local updates at: %v", time.Now())
return
}
glog.V(2).Infof("checking for local updates at: %v", time.Now())
cmdOutBytes, err := Execute(GitCmd, []string{GitStatus, GitArgShort, GitArgNoUntracked})
if err != nil {
glog.Warningf("error: executing %s %s %s %s, returned: %v",
GitCmd, GitStatus, GitArgShort, GitArgNoUntracked, err)
return
}
mods := false
outLines := strings.Split(string(cmdOutBytes), "\n")
if len(outLines) > 0 {
for _, line := range outLines {
// Check to see if the 2nd character of the output line a liternal `M`, e.g.
// $ git status --short --untracked-files=no
// M config.go
// ^
if len(line) > 2 && line[1] == 77 {
mods = true
break
}
}
}
if mods {
resyncInProgress = true
defer func() {
resyncInProgress = false
}()
as.commitUpdates()
}
}
func (as *gaServer) checkForRemoteUpdates() {
// resyncInProgress is not a mutext, but a trivial way to avoid obvious failure modes
if resyncInProgress {
glog.V(2).Infof("resync currently in progress, skipping check for remote updates at: %v", time.Now())
return
}
glog.V(2).Infof("checking for remote updates at: %v", time.Now())
resyncInProgress = true
defer func() {
resyncInProgress = false
}()
_, err := Execute(GitCmd, []string{GitPull, GitRemoteOrigin, GitBranchMaster})
if err != nil {
glog.Warningf("error: executing %s %s %s %s, returned: %v",
GitCmd, GitStatus, GitArgShort, GitArgNoUntracked, err)
glog.Info("info: are there uncommitted changes blocking the pull's merge?")
}
}
func (as *gaServer) run() {
glog.V(2).Infof("starting run at: %v", time.Now())
if as.clone() == false {
return
}
if *as.cfg.initonly == true {
glog.Infof("exiting after initialization only, at: %v", time.Now())
return
}
if as.setDirectory() == false {
return
}
if *as.cfg.direction == "remote" || *as.cfg.direction == "both" {
// sync (push) all changes locall to the remote origin master branch
go Until(as.checkForUpdates, time.Duration(*as.cfg.frequency)*time.Second, as.stopCh)
if *as.cfg.direction == "both" {
time.Sleep(time.Duration(*as.cfg.frequency) * time.Second / 2)
}
}
if *as.cfg.direction == "local" || *as.cfg.direction == "both" {
// sync (pull) all changes from the remote origin master to the local master branch
go Until(as.checkForRemoteUpdates, time.Duration(*as.cfg.frequency)*time.Second, as.stopCh)
}
for {
time.Sleep(3600 * time.Second)
}
}