forked from coreos/updateservicectl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
watch.go
202 lines (169 loc) · 4.91 KB
/
watch.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"text/tabwriter"
"time"
"github.com/coreos/updatectl/Godeps/_workspace/src/code.google.com/p/go-uuid/uuid"
"github.com/coreos/updatectl/Godeps/_workspace/src/github.com/coreos/go-omaha/omaha"
"github.com/coreos/updatectl/client/update/v1"
)
var (
watchFlags struct {
interval int
version string
appId StringFlag
groupId StringFlag
clientId string
}
cmdWatch = &Command{
Name: "watch",
Usage: "[OPTION]... <cmd> <args>",
Summary: `Watch for app versions and exec a given command.`,
Run: watch,
}
)
func init() {
cmdWatch.Flags.IntVar(&watchFlags.interval, "interval", 1, "Update polling interval")
cmdWatch.Flags.StringVar(&watchFlags.version, "version", "0.0.0", "Starting version number")
cmdWatch.Flags.Var(&watchFlags.appId, "app-id", "Application to watch.")
cmdWatch.Flags.Var(&watchFlags.groupId, "group-id", "Group of application to subscribe to.")
cmdWatch.Flags.StringVar(&watchFlags.clientId, "client-id", "", "Client id to report ad. If not provided a random UUID will be generated.")
}
func fetchUpdateCheck(server string, appID string, groupID string, clientID string, version string, debug bool) (*omaha.UpdateCheck, error) {
client := &http.Client{}
// TODO: Fill out the OS field correctly based on /etc/os-release
request := omaha.NewRequest("lsb", "CoreOS", "", "")
app := request.AddApp(fmt.Sprintf("{%s}", appID), version)
app.AddUpdateCheck()
app.MachineID = clientID
app.BootId = uuid.New()
app.Track = groupID
event := app.AddEvent()
event.Type = "1"
event.Result = "0"
raw, err := xml.MarshalIndent(request, "", " ")
if err != nil {
fmt.Fprintln(os.Stderr, err)
return nil, err
}
if debug {
fmt.Fprintf(os.Stderr, "Request: %s%s\n", xml.Header, raw)
}
resp, err := client.Post(server+"/v1/update/", "text/xml", bytes.NewReader(raw))
if err != nil {
fmt.Fprintln(os.Stderr, err)
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return nil, err
}
if debug {
fmt.Fprintf(os.Stderr, "Response: %s%s\n", xml.Header, string(body))
}
oresp := &omaha.Response{}
err = xml.Unmarshal(body, oresp)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return nil, err
}
return oresp.Apps[0].UpdateCheck, nil
}
func prepareEnvironment(appID string, version string, oldVersion string, updateCheck *omaha.UpdateCheck) []string {
env := os.Environ()
env = append(env, "UPDATE_SERVICE_VERSION="+version)
if oldVersion != "" {
env = append(env, "UPDATE_SERVICE_OLD_VERSION="+oldVersion)
}
env = append(env, "UPDATE_SERVICE_APP_ID="+appID)
if updateCheck.Status == "ok" {
url, err := url.Parse(updateCheck.Urls.Urls[0].CodeBase)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
url.Path = path.Join(url.Path, updateCheck.Manifest.Packages.Packages[0].Name)
env = append(env, "UPDATE_SERVICE_URL="+url.String())
}
return env
}
func runCmd(cmdName string, args []string, appID string, version string, oldVersion string, updateCheck *omaha.UpdateCheck) {
cmd := exec.Command(cmdName, args...)
cmd.Env = prepareEnvironment(appID, version, oldVersion, updateCheck)
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
stderr, err := cmd.StderrPipe()
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
err = cmd.Start()
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
go io.Copy(os.Stdout, stdout)
go io.Copy(os.Stderr, stderr)
cmd.Wait()
}
func watch(args []string, service *update.Service, out *tabwriter.Writer) int {
tick := time.NewTicker(time.Second * time.Duration(watchFlags.interval))
server := globalFlags.Server
debug := globalFlags.Debug
version := watchFlags.version
if watchFlags.appId.Get() == nil || watchFlags.groupId.Get() == nil {
return ERROR_USAGE
}
if len(args) == 0 {
return ERROR_USAGE
}
appId := watchFlags.appId.String()
groupId := watchFlags.groupId.String()
clientId := watchFlags.clientId
if clientId == "" {
clientId = uuid.New()
}
// initial check
updateCheck, err := fetchUpdateCheck(server, appId, groupId, clientId, version, debug)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
runCmd(args[0], args[1:], appId, version, "", updateCheck)
for {
select {
case <-tick.C:
updateCheck, err := fetchUpdateCheck(server, appId, groupId, clientId, version, debug)
if err != nil {
log.Printf("warning: update check failed (%v)\n", err)
continue
}
if updateCheck.Status == "noupdate" {
continue
} else if updateCheck.Status == "error-version" {
continue
}
newVersion := updateCheck.Manifest.Version
if newVersion != version {
runCmd(args[0], args[1:], appId, newVersion, version, updateCheck)
}
version = newVersion
}
}
tick.Stop()
return OK
}