Add concurrency and id flag for daemon

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2015-12-07 15:19:56 -08:00
parent 9415a4210c
commit d9e8fe62cb
5 changed files with 40 additions and 10 deletions

View File

@ -198,6 +198,7 @@ func (s *server) writeState(w http.ResponseWriter, e *containerd.Event) error {
state := State{
Containers: []Container{},
Machine: Machine{
ID: m.ID,
Cpus: m.Cpus,
Memory: m.Memory,
},

View File

@ -15,8 +15,9 @@ const (
)
type Machine struct {
Cpus int `json:"cpus"`
Memory int64 `json:"memory"`
ID string `json:"id"`
Cpus int `json:"cpus"`
Memory int64 `json:"memory"`
}
type ContainerState struct {

View File

@ -29,6 +29,11 @@ func main() {
},
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "id",
Value: getDefaultID(),
Usage: "unique containerd id to identify the instance",
},
cli.BoolFlag{
Name: "debug",
Usage: "enable debug output in the logs",
@ -43,6 +48,11 @@ func main() {
Value: 2048,
Usage: "set the channel buffer size for events and signals",
},
cli.IntFlag{
Name: "c,concurrency",
Value: 10,
Usage: "set the concurrency level for tasks",
},
}
app.Before = func(context *cli.Context) error {
if context.GlobalBool("debug") {
@ -65,7 +75,12 @@ func main() {
return nil
}
app.Action = func(context *cli.Context) {
if err := daemon(context.String("state-dir"), 10, context.Int("buffer-size")); err != nil {
if err := daemon(
context.String("id"),
context.String("state-dir"),
context.Int("concurrency"),
context.Int("buffer-size"),
); err != nil {
logrus.Fatal(err)
}
}
@ -74,9 +89,9 @@ func main() {
}
}
func daemon(stateDir string, concurrency, bufferSize int) error {
func daemon(id, stateDir string, concurrency, bufferSize int) error {
tasks := make(chan *containerd.StartTask, concurrency*100)
supervisor, err := containerd.NewSupervisor(stateDir, tasks)
supervisor, err := containerd.NewSupervisor(id, stateDir, tasks)
if err != nil {
return err
}
@ -100,3 +115,12 @@ func daemon(stateDir string, concurrency, bufferSize int) error {
server := v1.NewServer(supervisor)
return http.ListenAndServe("localhost:8888", server)
}
// getDefaultID returns the hostname for the instance host
func getDefaultID() string {
hostname, err := os.Hostname()
if err != nil {
panic(err)
}
return hostname
}

View File

@ -3,12 +3,15 @@ package containerd
import "github.com/cloudfoundry/gosigar"
type Machine struct {
ID string
Cpus int
Memory int64
}
func CollectMachineInformation() (Machine, error) {
m := Machine{}
func CollectMachineInformation(id string) (Machine, error) {
m := Machine{
ID: id,
}
cpu := sigar.CpuList{}
if err := cpu.Get(); err != nil {
return m, err

View File

@ -3,6 +3,7 @@ package containerd
import (
"os"
"os/signal"
"path/filepath"
goruntime "runtime"
"sync"
"syscall"
@ -13,16 +14,16 @@ import (
)
// NewSupervisor returns an initialized Process supervisor.
func NewSupervisor(stateDir string, tasks chan *StartTask) (*Supervisor, error) {
func NewSupervisor(id, stateDir string, tasks chan *StartTask) (*Supervisor, error) {
if err := os.MkdirAll(stateDir, 0755); err != nil {
return nil, err
}
// register counters
r, err := newRuntime(stateDir)
r, err := newRuntime(filepath.Join(stateDir, id))
if err != nil {
return nil, err
}
machine, err := CollectMachineInformation()
machine, err := CollectMachineInformation(id)
if err != nil {
return nil, err
}