From 87107582273d254e98a66a3036bc0fc487edfa68 Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Mon, 6 Feb 2017 04:27:09 -0500 Subject: [PATCH] add nomad --- services/boot/config.go | 6 +++ services/boot/controller.go | 5 +- services/boot/nomad.go | 99 +++++++++++++++++++++++++++++++++++++ services/boot/runit.go | 4 +- 4 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 services/boot/nomad.go diff --git a/services/boot/config.go b/services/boot/config.go index 3ee981d4ee..50e2850a17 100644 --- a/services/boot/config.go +++ b/services/boot/config.go @@ -34,6 +34,9 @@ type portsConfig struct { ConsulSerfLAN int ConsulSerfWAN int ConsulServer int + NomadHTTP int + NomadRPC int + NomadSerf int VaultServer int } @@ -91,6 +94,9 @@ func DefaultConfig() *Config { ConsulSerfLAN: 18301, ConsulSerfWAN: 18302, ConsulServer: 18300, + NomadHTTP: 14646, + NomadRPC: 14647, + NomadSerf: 14648, VaultServer: 18200, }, DataDir: "/var/lib/arvados", diff --git a/services/boot/controller.go b/services/boot/controller.go index 953507f820..784f09f3f0 100644 --- a/services/boot/controller.go +++ b/services/boot/controller.go @@ -24,7 +24,10 @@ func (c *controller) Boot(ctx context.Context) error { }, consul, }, - vault, + Concurrent{ + vault, + nomad, + }, Concurrent{ dispatchLocal, dispatchSLURM, diff --git a/services/boot/nomad.go b/services/boot/nomad.go new file mode 100644 index 0000000000..07c5de859f --- /dev/null +++ b/services/boot/nomad.go @@ -0,0 +1,99 @@ +package main + +import ( + "context" + "fmt" + "os" + "path" + "sync" + + "github.com/hashicorp/nomad/api" +) + +var nomad = &nomadBooter{} + +type nomadBooter struct { + sync.Mutex +} + +func (nb *nomadBooter) Boot(ctx context.Context) error { + nb.Lock() + defer nb.Unlock() + + if nb.check(ctx) == nil { + return nil + } + cfg := cfg(ctx) + bin := cfg.UsrDir + "/bin/nomad" + err := (&download{ + URL: "https://releases.hashicorp.com/nomad/0.5.4/nomad_0.5.4_linux_amd64.zip", + Dest: bin, + //Size: 29079005, + Mode: 0755, + }).Boot(ctx) + if err != nil { + return err + } + + dataDir := path.Join(cfg.DataDir, "nomad") + if err := os.MkdirAll(dataDir, 0700); err != nil { + return err + } + + cf := path.Join(cfg.DataDir, "nomad.json") + err = atomicWriteJSON(cf, map[string]interface{}{ + "client": map[string]interface{}{ + "enabled": true, + "options": map[string]interface{}{ + "driver.raw_exec.enable": true, + }, + }, + "consul": map[string]interface{}{ + "address": fmt.Sprintf("127.0.0.1:%d", cfg.Ports.ConsulHTTP), + }, + "data_dir": dataDir, + "datacenter": cfg.SiteID, + "ports": map[string]int{ + "http": cfg.Ports.NomadHTTP, + "rpc": cfg.Ports.NomadRPC, + "serf": cfg.Ports.NomadSerf, + }, + "server": map[string]interface{}{ + "enabled": true, + "bootstrap_expect": len(cfg.ControlHosts), + }, + }, 0644) + if err != nil { + return err + } + + supervisor := newSupervisor(ctx, "arvados-nomad", bin, "agent", "-config="+cf) + running, err := supervisor.Running(ctx) + if err != nil { + return err + } + if !running { + defer feedbackf(ctx, "starting nomad service")() + err = supervisor.Start(ctx) + if err != nil { + return fmt.Errorf("starting nomad: %s", err) + } + } + return nb.check(ctx) +} + +var nomadCfg = api.DefaultConfig() + +func (nb *nomadBooter) check(ctx context.Context) error { + cfg := cfg(ctx) + nomadCfg.Address = fmt.Sprintf("http://127.0.0.1:%d", cfg.Ports.NomadHTTP) + nomad, err := api.NewClient(nomadCfg) + if err != nil { + return err + } + _, err = nomad.Agent().Datacenter() + if err != nil { + return err + } + return nil +} diff --git a/services/boot/runit.go b/services/boot/runit.go index ec906adefe..a47db32a77 100644 --- a/services/boot/runit.go +++ b/services/boot/runit.go @@ -4,8 +4,6 @@ import ( "bytes" "context" "fmt" - "io/ioutil" - "os" "path" ) @@ -20,7 +18,7 @@ func (r *runitService) Start(ctx context.Context) error { return err } - var script bytes.Buffer + script := &bytes.Buffer{} fmt.Fprintf(script, "#!/bin/sh\n\nexec %q", r.cmd) for _, arg := range r.args { fmt.Fprintf(script, " %q", arg) -- 2.30.2