Skip to content

Commit 558bfb0

Browse files
committed
feat(deisctl) : add hooks and units dirs to constants
1 parent 63e7db9 commit 558bfb0

4 files changed

Lines changed: 40 additions & 16 deletions

File tree

constants/constants.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package constant
2+
3+
const (
4+
UnitsDir = "/var/lib/deis/units/"
5+
HooksDir = "/var/lib/deis/hooks/"
6+
Version = "/etc/deis-version"
7+
MachineId = "/etc/machine-id"
8+
)

deisctl.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"fmt"
55
"github.com/deis/deisctl/client"
66
"github.com/deis/deisctl/cmd"
7+
"github.com/deis/deisctl/constants"
78
"github.com/deis/deisctl/updatectl"
9+
"github.com/deis/deisctl/utils"
810
docopt "github.com/docopt/docopt-go"
911
"os"
1012
"strconv"
@@ -34,7 +36,13 @@ func Update(args []string) {
3436
"--min-sleep=5",
3537
"--max-sleep=10",
3638
}
39+
if err := utils.Execute(constant.HooksDir + "pre-update.sh"); err != nil {
40+
fmt.Println("pre-updatehook failed")
41+
}
3742
updatectl.Update(Args)
43+
if err := utils.Execute(constant.HooksDir + "post-update.sh"); err != nil {
44+
fmt.Println("post-updatehook failed")
45+
}
3846
}
3947

4048
func setGlobalFlags(args map[string]interface{}) {

updatectl/instance.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
update "github.com/coreos/updatectl/client/update/v1"
1010
"github.com/deis/deisctl/client"
1111
"github.com/deis/deisctl/cmd"
12+
"github.com/deis/deisctl/constants"
1213
"github.com/deis/deisctl/utils"
1314
"io"
1415
"log"
@@ -20,12 +21,6 @@ import (
2021
"time"
2122
)
2223

23-
const (
24-
initialInterval = time.Second * 10
25-
maxInterval = time.Minute * 7
26-
downloadDir = "/home/core/deis/systemd/"
27-
)
28-
2924
var (
3025
instanceFlags struct {
3126
groupId string
@@ -103,7 +98,7 @@ func (c *Client) getCodebaseUrl(uc *omaha.UpdateCheck) string {
10398

10499
func (c *Client) updateservice() {
105100
fmt.Println("starting systemd units")
106-
files, _ := utils.ListFiles(downloadDir + "*.service")
101+
files, _ := utils.ListFiles(constant.UnitsDir + "*.service")
107102
fmt.Println(files)
108103
deis, _ := client.NewClient()
109104
localServices := deis.GetLocaljobs()
@@ -125,9 +120,9 @@ func (c *Client) updateservice() {
125120
}
126121
}
127122
if count == 0 {
128-
go func() {
129-
_ = cmd.PullImage(service)
130-
}()
123+
if err := cmd.PullImage(service); err != nil {
124+
fmt.Println("pulling Image failed for " + service)
125+
}
131126
}
132127
}
133128

@@ -143,7 +138,7 @@ func (c *Client) downloadFromUrl(url, fileName string) (err error) {
143138
fmt.Printf("Downloading %s to %s", url, fileName)
144139

145140
// TODO: check file existence first with io.IsExist
146-
output, err := os.Create(downloadDir + fileName)
141+
output, err := os.Create(constant.UnitsDir + fileName)
147142
if err != nil {
148143
fmt.Println("Error while creating", fileName, "-", err)
149144
return
@@ -239,13 +234,15 @@ func (c *Client) SetVersion(resp *omaha.Response) {
239234
url := c.getCodebaseUrl(uc)
240235
c.MakeRequest("13", "1", false, false)
241236
c.downloadFromUrl(url, "deis.tar.gz")
242-
utils.Extract(downloadDir+"deis.tar.gz", downloadDir)
237+
utils.Extract(constant.UnitsDir+"deis.tar.gz", constant.UnitsDir)
243238
c.MakeRequest("14", "1", false, false)
244239
c.updateservice()
240+
fmt.Println("Installation done")
241+
c.MakeRequest("2", "1", false, false)
245242
fmt.Println("updated done")
246243
c.MakeRequest("3", "1", false, false)
247244
// installed
248-
fmt.Println("updated done")
245+
249246
// simulate reboot lock for a while
250247
for c.pingsRemaining > 0 {
251248
c.MakeRequest("", "", false, true)

utils/utils.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
_ "bufio"
77
"bytes"
88
"fmt"
9+
"github.com/deis/deisctl/constants"
910
"github.com/docker/docker/api/client"
1011
"github.com/satori/go.uuid"
1112
"io"
@@ -38,7 +39,7 @@ func GetNewClient() (
3839
}
3940

4041
func PullImage(cli *client.DockerCli, args ...string) error {
41-
fmt.Println("pulling image :" + args[0])
42+
fmt.Println("pulling image : " + args[0])
4243
err := cli.CmdPull(args...)
4344
return err
4445
}
@@ -61,7 +62,7 @@ func GetServices() []string {
6162
}
6263

6364
func GetMachineID(root string) string {
64-
fullPath := filepath.Join(root, "/etc/machine-id")
65+
fullPath := filepath.Join(root, constant.MachineId)
6566
id, err := ioutil.ReadFile(fullPath)
6667
if err != nil {
6768
return ""
@@ -70,7 +71,7 @@ func GetMachineID(root string) string {
7071
}
7172

7273
func GetVersion() string {
73-
id, err := ioutil.ReadFile("/home/core/deis/systemd/version")
74+
id, err := ioutil.ReadFile(constant.Version)
7475
if err != nil {
7576
return ""
7677
}
@@ -192,6 +193,7 @@ func getExitCode(err error) (int, error) {
192193
}
193194

194195
// RunCommandWithStdoutStderr execs a command and returns its output.
196+
195197
func RunCommandWithStdoutStderr(cmd *exec.Cmd) (bytes.Buffer, bytes.Buffer, error) {
196198
var stdout, stderr bytes.Buffer
197199
stderrPipe, err := cmd.StderrPipe()
@@ -223,6 +225,15 @@ func RunCommandWithStdoutStderr(cmd *exec.Cmd) (bytes.Buffer, bytes.Buffer, erro
223225
return stdout, stderr, err
224226
}
225227

228+
func Execute(script string) error {
229+
cmdl := exec.Command("sh", "-c", script)
230+
if _, _, err := RunCommandWithStdoutStderr(cmdl); err != nil {
231+
fmt.Println("(Error )")
232+
return err
233+
}
234+
return nil
235+
}
236+
226237
func runCommandWithOutput(cmd *exec.Cmd) (output string, exitCode int, err error) {
227238
exitCode = 0
228239
out, err := cmd.CombinedOutput()

0 commit comments

Comments
 (0)