Skip to content

Commit d96d9bb

Browse files
committed
feat(deisctl) : basic working version of updater
1 parent 292ad2b commit d96d9bb

6 files changed

Lines changed: 120 additions & 33 deletions

File tree

client/list.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package client
22

33
import (
44
"fmt"
5+
"github.com/coreos/fleet/job"
6+
"github.com/coreos/fleet/machine"
7+
"github.com/deis/deisctl/utils"
58
"os"
69
"sort"
710
"strings"
811
"text/tabwriter"
9-
10-
"github.com/coreos/fleet/job"
11-
"github.com/coreos/fleet/machine"
1212
)
1313

1414
// initialize tabwriter on stdout
@@ -114,15 +114,32 @@ func (c *FleetClient) List() (err error) {
114114
}
115115
for _, j := range jj {
116116
if strings.HasPrefix(j.Name, "deis-") {
117-
jobs[j.Name] = j
118-
sortable = append(sortable, j.Name)
117+
j.
118+
sortable = append(sortable, j.Name)
119119
}
120120
}
121121
sortable.Sort()
122122
printList(jobs, sortable)
123123
return
124124
}
125125

126+
func (c *FleetClient) GetLocaljobs() sort.StringSlice {
127+
cols := strings.Split(defaultListUnitFields, ",")
128+
var jobs map[string]job.Job
129+
var sortable sort.StringSlice
130+
jobs = make(map[string]job.Job, 0)
131+
jj, err := c.Fleet.Jobs()
132+
if err != nil {
133+
return err
134+
}
135+
for _, j := range jj {
136+
if strings.HasPrefix(j.Name, "deis-") && j.UnitState.MachineID == utils.GetMachineID() {
137+
sortable = append(sortable, j.Name)
138+
}
139+
}
140+
return sortable
141+
}
142+
126143
// printList writes units to stdout using a tabwriter
127144
func printList(jobs map[string]job.Job, sortable sort.StringSlice) {
128145
cols := strings.Split(defaultListUnitFields, ",")

cmd/cmd.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,28 @@ package cmd
22

33
import (
44
"fmt"
5+
"github.com/deis/deis/tests/dockercli"
6+
"github.com/deis/deisctl/client"
7+
"github.com/deis/deisctl/updatectl"
58
"regexp"
69
"strconv"
7-
8-
"github.com/deis/deisctl/client"
10+
"strings"
911
)
1012

1113
func List(c client.Client) error {
1214
err := c.List()
1315
return err
1416
}
1517

18+
func PullImage(service string) error {
19+
Dockercli, _, _ := dockercli.GetNewClient()
20+
fmt.Println("pulling image :" + strings.Split(service, ".")[0])
21+
err := cli.CmdPull(strings.Split(service, ".")[0])
22+
if err != nil {
23+
return err
24+
}
25+
}
26+
1627
func Scale(c client.Client, targets []string) error {
1728
for _, target := range targets {
1829
component, num, err := splitScaleTarget(target)
@@ -84,6 +95,31 @@ func Install(c client.Client, targets []string) error {
8495
return nil
8596
}
8697

98+
func Update(args []string) {
99+
100+
if len(args) != 4 {
101+
fmt.Println("unsufficient args")
102+
fmt.Println("usage: updatectl update instance deis")
103+
return
104+
}
105+
if args[2] != "instance" && args[3] != "deis" {
106+
fmt.Println("wrong args ")
107+
fmt.Println("usage: updatectl update instance deis")
108+
return
109+
}
110+
Args := []string{
111+
"instance",
112+
"deis",
113+
"--clients-per-app=1",
114+
"--min-sleep=5",
115+
"--max-sleep=10",
116+
"--app-id=329cd607-06fe-4bde-8ecd-613b58c6945f",
117+
"--group-id=bee2027e-29a4-4135-bffb-b2864234dd15",
118+
"--version=1.1.0",
119+
}
120+
updatectl.Update(Args)
121+
}
122+
87123
func installDataContainers(c client.Client) error {
88124
// data containers
89125
dataContainers := []string{

deisctl.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/deis/deisctl/client"
99
"github.com/deis/deisctl/cmd"
10-
"github.com/deis/deisctl/updatectl"
1110
docopt "github.com/docopt/docopt-go"
1211
)
1312

@@ -87,7 +86,7 @@ Options:
8786
case "uninstall":
8887
err = cmd.Uninstall(c, targets)
8988
case "update":
90-
updatectl.Update(os.Args)
89+
cmd.Update(os.Args)
9190
default:
9291
fmt.Printf(usage)
9392
os.Exit(2)

updatectl/instance.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"fmt"
88
"github.com/coreos/go-omaha/omaha"
99
update "github.com/coreos/updatectl/client/update/v1"
10+
"github.com/deis/deisctl/client"
11+
"github.com/deis/deisctl/cmd"
1012
"github.com/deis/deisctl/utils"
1113
"io"
1214
"log"
@@ -102,7 +104,37 @@ func (c *Client) updateservice() {
102104
fmt.Println("starting systemd units")
103105
files, _ := utils.ListFiles(downloadDir + "*.service")
104106
fmt.Println(files)
107+
deis, err := client.NewClient()
108+
localServices := deis.GetLocaljobs()
109+
Services := utils.GetServices()
110+
if len(localService) == 0 {
111+
fmt.Println("no local services")
112+
return
113+
}
114+
for _, service := range localServices {
115+
cmd.Unisntall(deis, strings.Split(strings.Split(service, "-")[1], ".")[0])
116+
cmd.Install(deis, strings.Split(strings.Split(service, "-")[1], ".")[0])
117+
}
118+
var count int
119+
for _, service := range Services {
120+
count = 0
121+
for _, lserv := range localServices {
122+
if strings.Contains(lserv, strings.Split(strings.Split(service, "-")[1], ".")[0]) {
123+
count = count + 1
124+
}
125+
}
126+
if count == 0 {
127+
go func() {
128+
cmd.PullImage(service)
129+
}()
130+
}
131+
}
105132

133+
// pre-install hook (download all new docker images)
134+
// use systemd to list local deis-* units, ignore -data units
135+
// cmd.Unistall([]string{"router.3"})
136+
// cmd.Install([]string{"router.3"})
137+
// post-install hook (make sure upgrade was successful)
106138
}
107139

108140
func (c *Client) downloadFromUrl(url, fileName string) (err error) {

updatectl/update.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,7 @@ func init() {
8787
globalFlagSet.StringVar(&globalFlags.Key, "key", os.Getenv("UPDATECTL_KEY"), "API Key")
8888

8989
commands = []*Command{
90-
/*// admin.go
91-
cmdAdminUser,
92-
// app.go
93-
cmdApp,
94-
// channel.go
95-
cmdChannel,
96-
// database.go
97-
cmdDatabase,
98-
// group.go
99-
cmdGroup,
100-
// help.go
101-
cmdHelp,
102-
// instance.go*/
10390
cmdInstance,
104-
// pkg.go
105-
/*cmdPackage,
106-
// watch.go
107-
cmdWatch,
108-
// upstream.go
109-
cmdUpstream,*/
11091
}
11192
}
11293

@@ -154,7 +135,6 @@ func findCommand(search string, args []string, commands []*Command) (cmd *Comman
154135
if len(args) < 1 {
155136
return
156137
}
157-
//fmt.Println(args)
158138
if search == "" {
159139
search = args[0]
160140
} else {
@@ -170,7 +150,6 @@ func findCommand(search string, args []string, commands []*Command) (cmd *Comman
170150
}
171151
if len(cmd.Subcommands) != 0 {
172152
subArgs := cmd.Flags.Args()
173-
fmt.Println(subArgs)
174153
var subCmd *Command
175154
subCmd, name = findCommand(search, subArgs, cmd.Subcommands)
176155
if subCmd != nil {
@@ -198,17 +177,14 @@ func Update(Args []string) {
198177
fmt.Printf("Run '%v help' for usage.\n", cliName)
199178
os.Exit(ERROR_NO_COMMAND)
200179
}
201-
fmt.Println(cmd)
202180
if cmd.Run == nil {
203181
// printCommandUsage(cmd)
204182
os.Exit(ERROR_USAGE)
205183
} else {
206-
fmt.Println("inside run")
207184
exit := handle(cmd.Run)(&cmd.Flags)
208185
if exit == ERROR_USAGE {
209186
// printCommandUsage(cmd)
210187
}
211-
fmt.Println("exiting")
212188
os.Exit(exit)
213189
}
214190
}

utils/utils.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bytes"
88
"fmt"
99
"io"
10+
"io/ioutil"
1011
"net"
1112
"os"
1213
"os/exec"
@@ -26,6 +27,32 @@ func NewUuid() string {
2627
return strings.Split(s1, "-")[0]
2728
}
2829

30+
func GetServices() []string {
31+
service := []string{
32+
"deis-builder.service",
33+
"deis-builder-data.service",
34+
"deis-cache.service",
35+
"deis-controller.service",
36+
"deis-database.service",
37+
"deis-database-data.service",
38+
"deis-logger.service",
39+
"deis-logger-data.service",
40+
"deis-registry.service",
41+
"deis-registry-data.service",
42+
"deis-router.service",
43+
}
44+
return service
45+
}
46+
47+
func GetMachineID(root string) string {
48+
fullPath := filepath.Join(root, "/etc/machine-id")
49+
id, err := ioutil.ReadFile(fullPath)
50+
if err != nil {
51+
return ""
52+
}
53+
return strings.TrimSpace(string(id))
54+
}
55+
2956
// GetFileBytes returns a byte array of the contents of a file.
3057
func GetFileBytes(filename string) []byte {
3158
file, _ := os.Open(filename)

0 commit comments

Comments
 (0)