From bee4fc6e7ca1e6374e6a6bcb4120ff3a375598c5 Mon Sep 17 00:00:00 2001 From: Chris Bednarski Date: Sun, 20 Sep 2015 17:06:02 -0700 Subject: [PATCH] Added init command --- command/init.go | 98 +++++++++++++++++++++++++++++++++++++++++++++++++ commands.go | 6 +++ 2 files changed, 104 insertions(+) create mode 100644 command/init.go diff --git a/command/init.go b/command/init.go new file mode 100644 index 000000000..fd8a910aa --- /dev/null +++ b/command/init.go @@ -0,0 +1,98 @@ +package command + +import ( + "fmt" + "os" + "path/filepath" +) + +// InitCommand generates a new job template that you can customize to your +// liking, like vagrant init +type InitCommand struct { + Meta +} + +func (c *InitCommand) Help() string { + return initUsage +} + +func (c *InitCommand) Run(args []string) int { + dir, err := os.Getwd() + if err != nil { + c.Ui.Error("Unable to determine pwd; aborting") + return 1 + } + + // Derive the job name from the pwd folder name, which is our best guess at + // the project's name + jobname := filepath.Base(dir) + jobfile := fmt.Sprintf("%s.nomad", jobname) + jobpath := filepath.Join(dir, jobfile) + if _, err := os.Stat(jobpath); err == nil { + c.Ui.Error(fmt.Sprintf("%s file already exists", jobfile)) + return 1 + } + + file, err := os.Create(jobfile) + defer file.Close() + if err != nil { + c.Ui.Error(fmt.Sprintf("Unable to create file %s: %s", jobfile, err)) + return 1 + } + + _, err = file.WriteString(defaultJob) + if err != nil { + c.Ui.Error(fmt.Sprintf("Failed to write job template to %s", jobfile)) + return 1 + } + + c.Ui.Output(fmt.Sprintf("Initialized nomad job template in %s", jobfile)) + + return 0 +} + +func (c *InitCommand) Synopsis() string { + return "Create a new job template" +} + +const initUsage = `` + +const defaultJob = ` +job "my-app" { + region = "global" + type = "service" + priority = 50 + + // Each task in the group will be scheduled on the same machine(s). + group "app-group" { + // How many copies of this group should we run? + count = 5 + + task "python-webapp" { + driver = "docker" + config { + image = "org/container" + } + resources { + // For CPU 1024 = 1ghz + cpu = 500 + // Memory in megabytes + memory = 128 + + network { + dynamic_ports = 2 + } + } + } + + task "logshipper" { + driver = "exec" + } + + constraint { + attribute = "kernel.os" + value = "linux" + } + } +} +` diff --git a/commands.go b/commands.go index e60e37ff6..d7c74d008 100644 --- a/commands.go +++ b/commands.go @@ -46,6 +46,12 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory { }, nil }, + "init": func() (cli.Command, error) { + return &command.InitCommand{ + Meta: meta, + }, nil + }, + "node-drain": func() (cli.Command, error) { return &command.NodeDrainCommand{ Meta: meta,