Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions ch10/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
all:
cat Makefile

start:
CUBE_WORKER_HOST=localhost \
CUBE_WORKER_PORT=5555 \
CUBE_MANAGER_HOST=localhost \
CUBE_MANAGER_PORT=5556 \
go run main.go

list:
@curl -s localhost:5556/tasks | jq

send/%:
@curl -s --request POST \
--header 'Content-Type: application/json' \
--data @task$*.json \
localhost:5556/tasks | jq

delete:
@echo 'curl -s --request DELETE "localhost:5556/tasks/xxxxx"'

10 changes: 5 additions & 5 deletions ch10/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ func main() {
mhost := os.Getenv("CUBE_MANAGER_HOST")
mport, _ := strconv.Atoi(os.Getenv("CUBE_MANAGER_PORT"))

fmt.Println("Starting Cube worker")

fmt.Println("Starting Cube worker 1")
w1 := worker.Worker{
Queue: *queue.New(),
Db: make(map[uuid.UUID]*task.Task),
}
wapi1 := worker.Api{Address: whost, Port: wport, Worker: &w1}

fmt.Println("Starting Cube worker 2")
w2 := worker.Worker{
Queue: *queue.New(),
Db: make(map[uuid.UUID]*task.Task),
}
wapi2 := worker.Api{Address: whost, Port: wport + 1, Worker: &w2}

fmt.Println("Starting Cube worker 3")
w3 := worker.Worker{
Queue: *queue.New(),
Db: make(map[uuid.UUID]*task.Task),
Expand All @@ -58,8 +59,8 @@ func main() {
fmt.Sprintf("%s:%d", whost, wport+1),
fmt.Sprintf("%s:%d", whost, wport+2),
}
//m := manager.New(workers, "roundrobin")
m := manager.New(workers, "epvm")
m := manager.New(workers, "roundrobin")
//m := manager.New(workers, "epvm")
mapi := manager.Api{Address: mhost, Port: mport, Manager: m}

go m.ProcessTasks()
Expand All @@ -68,5 +69,4 @@ func main() {
//go m.UpdateNodeStats()

mapi.Start()

}
6 changes: 5 additions & 1 deletion ch10/manager/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package manager

import (
"fmt"
"log"
"net/http"

"github.com/go-chi/chi/v5"
Expand Down Expand Up @@ -32,5 +33,8 @@ func (a *Api) initRouter() {

func (a *Api) Start() {
a.initRouter()
http.ListenAndServe(fmt.Sprintf("%s:%d", a.Address, a.Port), a.Router)
err := http.ListenAndServe(fmt.Sprintf("%s:%d", a.Address, a.Port), a.Router)
if err != nil {
log.Fatal(err)
}
}
34 changes: 34 additions & 0 deletions ch10/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type Task struct {
RestartPolicy string
StartTime time.Time
FinishTime time.Time
HealthCheck string
HostPorts nat.PortMap
RestartCount int
}

type TaskEvent struct {
Expand Down Expand Up @@ -151,6 +154,11 @@ func (d *Docker) Run() DockerResult {
return DockerResult{ContainerId: resp.ID, Action: "start", Result: "success"}
}

type DockerInspectResponse struct {
Error error
Container *types.ContainerJSON
}

func (d *Docker) Stop(id string) DockerResult {
log.Printf("Attempting to stop container %v", id)
ctx := context.Background()
Expand All @@ -172,3 +180,29 @@ func (d *Docker) Stop(id string) DockerResult {

return DockerResult{Action: "stop", Result: "success", Error: nil}
}

func (d *Docker) Inspect(containerID string) DockerInspectResponse {
dc, _ := client.NewClientWithOpts(client.FromEnv)
ctx := context.Background()
resp, err := dc.ContainerInspect(ctx, containerID)
if err != nil {
log.Printf("Error inspecting container: %s\n", err)
return DockerInspectResponse{Error: err}
}

return DockerInspectResponse{Container: &resp}
}

// FIXME:
func (d *Docker) Remove(containerID string) DockerResult {
dc, _ := client.NewClientWithOpts(client.FromEnv)
ctx := context.Background()
// resp
_, err := dc.ContainerInspect(ctx, containerID)
if err != nil {
log.Printf("Error inspecting container: %s\n", err)
return DockerResult{Error: err}
}

return DockerResult{Action: "stop", Result: "success", Error: nil}
}
4 changes: 4 additions & 0 deletions ch12/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

all:
@echo "go run main.go worker -n w01 -p 5556"
@echo "go run main.go manager -w localhost:5556,localhost:5557"
14 changes: 2 additions & 12 deletions ch12/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>

*/
package cmd

Expand All @@ -10,18 +9,11 @@ import (
"github.com/spf13/cobra"
)



// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "cube",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "a toy orchestrator",
Long: `cube is a minimal orchestrator system that was written as a didactical tool.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Expand All @@ -47,5 +39,3 @@ func init() {
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}


28 changes: 16 additions & 12 deletions ch12/go.mod
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
module cube

go 1.19
go 1.21

toolchain go1.22.4

require (
github.com/boltdb/bolt v1.3.1
github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8
github.com/docker/docker v20.10.6+incompatible
github.com/docker/go-connections v0.4.0
github.com/docker/go-units v0.4.0
github.com/go-chi/chi/v5 v5.0.4
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3
github.com/google/go-cmp v0.5.4
github.com/google/uuid v1.2.0
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
github.com/moby/moby v20.10.6+incompatible
github.com/spf13/cobra v1.7.0
)

require (
github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/containerd/containerd v1.5.1 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect
golang.org/x/sys v0.0.0-20210423082822-04245dca01da // indirect
google.golang.org/genproto v0.0.0-20201110150050-8816d57aaa9a // indirect
google.golang.org/grpc v1.37.1 // indirect
google.golang.org/protobuf v1.25.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
Loading