Skip to content

Commit b366679

Browse files
committed
feat(deisctl): get groupid and app id from etcd defaults to env variables
1 parent 985f003 commit b366679

3 files changed

Lines changed: 98 additions & 53 deletions

File tree

constant/constants.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package constant
22

33
const (
4-
UnitsDir = "/var/lib/deis/units/"
5-
HooksDir = "/var/lib/deis/hooks/"
6-
Version = "/etc/deis-version"
7-
MachineId = "/etc/machine-id"
4+
UnitsDir = "/var/lib/deis/units/"
5+
HooksDir = "/var/lib/deis/hooks/"
6+
Version = "/etc/deis-version"
7+
MachineId = "/etc/machine-id"
8+
UpdatekeyDir = "/deis/update/"
89
)

updatectl/instance.go

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
update "github.com/coreos/updatectl/client/update/v1"
1919
"github.com/deis/deisctl/client"
2020
"github.com/deis/deisctl/cmd"
21-
_ "github.com/deis/deisctl/constant"
21+
"github.com/deis/deisctl/constant"
2222
"github.com/deis/deisctl/utils"
2323
)
2424

@@ -64,9 +64,9 @@ func init() {
6464
cmdInstanceDeis.Flags.StringVar(&instanceFlags.OEM, "oem", "deisclient", "oem to report")
6565
// simulate reboot lock.
6666
cmdInstanceDeis.Flags.IntVar(&instanceFlags.pingOnly, "ping-only", 0, "halt update and just send ping requests this many times.")
67-
cmdInstanceDeis.Flags.StringVar(&instanceFlags.appId, "app-id", os.Getenv("DEISCTL_APP_ID"), "Application ID to update.")
67+
cmdInstanceDeis.Flags.StringVar(&instanceFlags.appId, "app-id", utils.GetKey(constant.UpdatekeyDir, "app-id", "DEISCTL_APP_ID"), "Application ID to update.")
6868
//instanceFlags.appId.required = true
69-
cmdInstanceDeis.Flags.StringVar(&instanceFlags.groupId, "group-id", os.Getenv("DEISCTL_GROUP_ID"), "Group ID to update.")
69+
cmdInstanceDeis.Flags.StringVar(&instanceFlags.groupId, "group-id", utils.GetKey(constant.UpdatekeyDir, "group-id", "DEISCTL_GROUP_ID"), "Group ID to update.")
7070
//instanceFlags.groupId.required = true
7171
cmdInstanceDeis.Flags.StringVar(&instanceFlags.version, "version", utils.GetVersion(), "Version to report.")
7272
}
@@ -93,40 +93,60 @@ func (c *Client) Log(format string, v ...interface{}) {
9393
fmt.Printf(format, v...)
9494
}
9595

96+
func (c *Client) failed(tag string, err error) {
97+
c.Log("%s %v\n", tag, err)
98+
c.MakeRequest("3", "0", false, false)
99+
}
100+
96101
func (c *Client) getCodebaseUrl(uc *omaha.UpdateCheck) string {
97102
return uc.Urls.Urls[0].CodeBase + uc.Manifest.Packages.Packages[0].Name
98103
}
99104

100-
func (c *Client) updateservice() {
105+
func (c *Client) updateservice() (err error) {
101106
fmt.Println("starting systemd units")
102107
// files, _ := utils.ListFiles(constant.UnitsDir + "*.service")
103108
deis, _ := client.NewClient()
104109
localServices := deis.GetLocaljobs()
105110
fmt.Printf("local services: %v\n", localServices)
106-
// Services := utils.GetServices()
111+
Services := utils.GetServices()
107112
if localServices.Len() == 0 {
108113
fmt.Println("no local services")
109114
return
110115
}
111116
for _, service := range localServices {
112-
if strings.HasSuffix(service,"-data.service") {
117+
if strings.HasSuffix(service, "-data.service") {
113118
continue
114119
}
115120
localService := strings.Split(strings.Split(service, "-")[1], ".service")[0]
116-
cmd.Uninstall(deis, []string{localService})
121+
err := cmd.Uninstall(deis, []string{localService})
122+
if err != nil {
123+
return err
124+
}
117125
time.Sleep(1 * time.Second)
118-
cmd.Install(deis, []string{localService})
126+
err = cmd.Install(deis, []string{localService})
127+
if err != nil {
128+
return err
129+
}
119130
}
120-
// var count int
121-
// for _, service := range Services {
122-
// count = 0
123-
// for _, lserv := range localServices {
124-
// if strings.Contains(lserv, strings.Split(strings.Split(service, "-")[1], ".")[0]) {
125-
// count = count + 1
126-
// }
127-
// }
128-
// }
129-
131+
var count int
132+
for _, service := range Services {
133+
count = 0
134+
if strings.HasSuffix(service, "-data.service") {
135+
continue
136+
}
137+
for _, lserv := range localServices {
138+
if strings.Contains(lserv, strings.Split(strings.Split(service, "-")[1], ".")[0]) {
139+
count = count + 1
140+
}
141+
}
142+
if count == 0 {
143+
if err = cmd.PullImage(service); err != nil {
144+
fmt.Println("failed pulling image", service)
145+
return err
146+
}
147+
}
148+
}
149+
return nil
130150
// pre-install hook (download all new docker images)
131151
// use systemd to list local deis-* units, ignore -data units
132152
// cmd.Unistall([]string{"router.3"})
@@ -137,7 +157,6 @@ func (c *Client) updateservice() {
137157
func (c *Client) downloadFromUrl(url, filePath string) (err error) {
138158
fmt.Printf("Downloading %s to %s", url, filePath)
139159

140-
// TODO: check file existence first with io.IsExist
141160
output, err := os.Create(filePath)
142161
if err != nil {
143162
fmt.Println("Error while creating", filePath, "-", err)
@@ -231,15 +250,15 @@ func (c *Client) SetVersion(resp *omaha.Response) {
231250
}
232251
}()
233252
uc := resp.Apps[0].UpdateCheck
234-
url := c.getCodebaseUrl(uc)
235-
c.MakeRequest("13", "1", false, false)
236-
c.downloadFromUrl(url, "/tmp/deis.tar.gz")
237-
utils.Extract("/tmp/deis.tar.gz", "/")
238-
c.MakeRequest("14", "1", false, false)
239-
c.updateservice()
240-
fmt.Println("Installation done")
253+
254+
err := c.updateservice()
255+
if err != nil {
256+
c.failed("update failed", err)
257+
return
258+
}
259+
c.Log("Installation done ")
241260
c.MakeRequest("2", "1", false, false)
242-
fmt.Println("updated done")
261+
c.Log("Update done ")
243262
c.MakeRequest("3", "1", false, false)
244263
// installed
245264

@@ -253,8 +272,9 @@ func (c *Client) SetVersion(resp *omaha.Response) {
253272
c.Log("updated from %s to %s\n", c.Version, uc.Manifest.Version)
254273

255274
c.Version = uc.Manifest.Version
275+
utils.PutVersion(c.Version)
256276

257-
_, err := c.MakeRequest("3", "2", false, false) // Send complete with new version.
277+
_, err = c.MakeRequest("3", "2", false, false) // Send complete with new version.
258278
if err != nil {
259279
log.Println(err)
260280
}
@@ -275,6 +295,23 @@ func (c *Client) Loop(n, m int) {
275295
if uc.Status != "ok" {
276296
c.Log("update check status: %s\n", uc.Status)
277297
} else {
298+
url := c.getCodebaseUrl(uc)
299+
if !strings.Contains(url, "deis") {
300+
c.failed("Wrong Url", err)
301+
continue
302+
}
303+
c.MakeRequest("13", "1", false, false)
304+
err = c.downloadFromUrl(url, "/tmp/deis.tar.gz")
305+
if err != nil {
306+
c.failed("Download failed", err)
307+
continue
308+
}
309+
err = utils.Extract("/tmp/deis.tar.gz", "/")
310+
if err != nil {
311+
c.failed("Extract failed", err)
312+
continue
313+
}
314+
c.MakeRequest("14", "1", false, false)
278315
c.SetVersion(resp)
279316
}
280317
}

utils/utils.go

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"syscall"
1717
"time"
1818

19+
"github.com/coreos/go-etcd/etcd"
1920
"github.com/deis/deisctl/constant"
2021
"github.com/docker/docker/api/client"
2122
uuid "github.com/satori/go.uuid"
@@ -28,6 +29,22 @@ func NewUuid() string {
2829
return strings.Split(s1, "-")[0]
2930
}
3031

32+
func getetcdClient() *etcd.Client {
33+
34+
machines := []string{"http://127.0.0.1:4001/"}
35+
c := etcd.NewClient(machines)
36+
return c
37+
}
38+
39+
func GetKey(dir, key, perm string) string {
40+
c := getetcdClient()
41+
result, err := c.Get(dir+key, false, false)
42+
if err != nil || result.Node.Value == "" {
43+
return os.Getenv(perm)
44+
}
45+
return result.Node.Value
46+
}
47+
3148
func GetNewClient() (
3249
cli *client.DockerCli, stdout *io.PipeReader, stdoutPipe *io.PipeWriter) {
3350
testDaemonAddr := "/var/run/docker.sock"
@@ -112,34 +129,16 @@ func CreateFile(path string) error {
112129
return nil
113130
}
114131

115-
// Chdir sets the current working directory to the relative path specified.
116-
func Chdir(app string) error {
117-
var wd, _ = os.Getwd()
118-
dir, _ := filepath.Abs(filepath.Join(wd, app))
119-
err := os.Chdir(dir)
120-
fmt.Println(dir)
121-
return err
122-
}
123-
124-
func Extract(file, dir string) {
132+
func Extract(file, dir string) (err error) {
125133
var wd, _ = os.Getwd()
126134
_ = os.Chdir(dir)
127135
cmdl := exec.Command("tar", "-C", "/", "-xvf", file)
128136
if _, _, err := RunCommandWithStdoutStderr(cmdl); err != nil {
129137
fmt.Printf("Failed:\n%v", err)
130-
} else {
131-
fmt.Println("ok")
138+
return err
132139
}
133140
_ = os.Chdir(wd)
134-
}
135-
136-
// Rmdir removes a directory and its contents.
137-
func Rmdir(app string) error {
138-
var wd, _ = os.Getwd()
139-
dir, _ := filepath.Abs(filepath.Join(wd, app))
140-
err := os.RemoveAll(dir)
141-
fmt.Println(dir)
142-
return err
141+
return nil
143142
}
144143

145144
// GetUserDetails returns sections of a UUID.
@@ -168,6 +167,14 @@ func GetHostIPAddress() string {
168167
return IP
169168
}
170169

170+
func PutVersion(version string) error {
171+
err := ioutil.WriteFile(constant.Version, []byte(version), 0644)
172+
if err != nil {
173+
return err
174+
}
175+
return nil
176+
}
177+
171178
// Append grows a string array by appending a new element.
172179
func Append(slice []string, data string) []string {
173180
m := len(slice)

0 commit comments

Comments
 (0)