more conf and add deployment commands
This commit is contained in:
parent
105f03bf67
commit
02b3ebf87b
4
Makefile
4
Makefile
|
@ -18,7 +18,9 @@ clean:
|
||||||
install:
|
install:
|
||||||
cp cmd/warren/warren /usr/bin
|
cp cmd/warren/warren /usr/bin
|
||||||
cp cmd/warrend/warrend /usr/bin
|
cp cmd/warrend/warrend /usr/bin
|
||||||
mkdir /var/run/warrend
|
mkdir -p /var/run/warrend
|
||||||
|
cp -n cmd/warrend/etc/warrend.json /etc
|
||||||
|
cp cmd/warrend/etc/init/warrend.conf /etc/init
|
||||||
|
|
||||||
uninstall:
|
uninstall:
|
||||||
rm /usr/bin/warren
|
rm /usr/bin/warren
|
||||||
|
|
|
@ -19,7 +19,7 @@ warren build - Runs go build and populates the following variables:
|
||||||
|
|
||||||
var buildCmd = &warren.Command{
|
var buildCmd = &warren.Command{
|
||||||
Name: "build",
|
Name: "build",
|
||||||
Usage: "",
|
Usage: buildUsage,
|
||||||
Summary: "build the go server in the current directory",
|
Summary: "build the go server in the current directory",
|
||||||
Help: buildUsage,
|
Help: buildUsage,
|
||||||
Run: buildRun,
|
Run: buildRun,
|
||||||
|
|
|
@ -13,7 +13,7 @@ warren deploy - Deploys the go server and support files
|
||||||
|
|
||||||
var deployCmd = &warren.Command{
|
var deployCmd = &warren.Command{
|
||||||
Name: "deploy",
|
Name: "deploy",
|
||||||
Usage: "",
|
Usage: deployUsage,
|
||||||
Summary: "deploy the go server and support files",
|
Summary: "deploy the go server and support files",
|
||||||
Help: deployUsage,
|
Help: deployUsage,
|
||||||
Run: deployRun,
|
Run: deployRun,
|
||||||
|
|
|
@ -17,7 +17,7 @@ warren run - Interfaces with warrend to manage running instances
|
||||||
|
|
||||||
var runCmd = &warren.Command{
|
var runCmd = &warren.Command{
|
||||||
Name: "run",
|
Name: "run",
|
||||||
Usage: "",
|
Usage: runUsage,
|
||||||
Summary: "Interface to warrend to manage running instances",
|
Summary: "Interface to warrend to manage running instances",
|
||||||
Help: runUsage,
|
Help: runUsage,
|
||||||
Run: runRun,
|
Run: runRun,
|
||||||
|
|
|
@ -4,25 +4,18 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/dballard/warren/lib/warren"
|
"github.com/dballard/warren/lib/warren"
|
||||||
"os"
|
"os"
|
||||||
"text/template"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var BuildGitHash string
|
var BuildGitHash string
|
||||||
var BuildGitBranch string
|
var BuildGitBranch string
|
||||||
var BuildDate string
|
var BuildDate string
|
||||||
|
|
||||||
var usagePrefix = `
|
var usagePrefix = `warren is a tool for building, deploying and running multiple versions of a Go server
|
||||||
warren is a tool for building, deploying and running multiple versions of a Go server
|
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
warren command [args]
|
warren command [args]
|
||||||
`
|
`
|
||||||
|
|
||||||
var usageTmpl = template.Must(template.New("usage").Parse(`
|
|
||||||
Commands:{{range .}}
|
|
||||||
{{.Name | printf "%-10s"}} {{.Summary}}{{end}}
|
|
||||||
`))
|
|
||||||
|
|
||||||
func usage() {
|
func usage() {
|
||||||
fmt.Println(BuildGitBranch, " ", BuildGitHash, " ", BuildDate)
|
fmt.Println(BuildGitBranch, " ", BuildGitHash, " ", BuildDate)
|
||||||
fmt.Print(usagePrefix)
|
fmt.Print(usagePrefix)
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/dballard/warren/lib/warren"
|
||||||
|
"net"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var regUsage = `
|
||||||
|
status -
|
||||||
|
`
|
||||||
|
|
||||||
|
var regCmd = &SockCommand{
|
||||||
|
Command: warren.Command{
|
||||||
|
Name: "reg",
|
||||||
|
Usage: regUsage,
|
||||||
|
Summary: "Register a deployment directory for running",
|
||||||
|
Help: regUsage,
|
||||||
|
},
|
||||||
|
Run: regRun,
|
||||||
|
}
|
||||||
|
|
||||||
|
// reg path [nickname]
|
||||||
|
func regRun(cmd *SockCommand, c net.Conn, args ...string) {
|
||||||
|
if (len(args) > 0 && args[0] == "help") || len(args) < 1 {
|
||||||
|
warren.WriteStringz(c, cmd.Help)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// get either supplied nick or parse from dir
|
||||||
|
d := new(Deployment)
|
||||||
|
d.Path = args[0]
|
||||||
|
if len(args) >= 2 && strings.TrimSpace(args[1]) != "" {
|
||||||
|
d.Name = args[1]
|
||||||
|
} else {
|
||||||
|
parts := strings.Split(strings.Trim(d.Path, " /"), "/")
|
||||||
|
if parts[len(parts)-1] == "deploy" {
|
||||||
|
d.Name = parts[len(parts)-2]
|
||||||
|
} else {
|
||||||
|
d.Name = parts[len(parts)-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err := addDeployment(d)
|
||||||
|
if err != nil {
|
||||||
|
warren.WriteStringz(c, "ERROR: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
warren.WriteStringz(c, "Registered deployment "+d.Name+" at "+d.Path)
|
||||||
|
}
|
|
@ -12,7 +12,7 @@ status -
|
||||||
var statusCmd = &SockCommand{
|
var statusCmd = &SockCommand{
|
||||||
Command: warren.Command{
|
Command: warren.Command{
|
||||||
Name: "status",
|
Name: "status",
|
||||||
Usage: "",
|
Usage: statusUsage,
|
||||||
Summary: "Print status of warrend",
|
Summary: "Print status of warrend",
|
||||||
Help: statusUsage,
|
Help: statusUsage,
|
||||||
},
|
},
|
||||||
|
@ -25,5 +25,10 @@ func statusRun(cmd *SockCommand, c net.Conn, args ...string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
warren.WriteStringz(c, "status")
|
warren.WriteStringz(c, "Status\n")
|
||||||
|
|
||||||
|
for _, d := range deployments {
|
||||||
|
warren.WriteStringz(c, d.Name+": "+d.Path)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"github.com/dballard/warren/lib/warren"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Conf struct {
|
||||||
|
deployments []string
|
||||||
|
}
|
||||||
|
|
||||||
|
var conf *Conf = nil
|
||||||
|
|
||||||
|
func LoadConf() {
|
||||||
|
file, err := os.Open(warren.WarrendConf)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error loading ", warren.WarrendConf, ": ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
decoder := json.NewDecoder(file)
|
||||||
|
conf = new(Conf)
|
||||||
|
err = decoder.Decode(conf)
|
||||||
|
if err != nil {
|
||||||
|
conf = nil
|
||||||
|
fmt.Println("Error parsing ", warren.WarrendConf, ": ", err)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Deployment struct {
|
||||||
|
Name string
|
||||||
|
Path string
|
||||||
|
}
|
||||||
|
|
||||||
|
var deployments map[string]*Deployment = make(map[string]*Deployment)
|
||||||
|
|
||||||
|
func addDeployment(d *Deployment) error {
|
||||||
|
// check if exists
|
||||||
|
if _, ok := deployments[d.Name]; ok {
|
||||||
|
return errors.New("Deployment with that name already exists")
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if dir exists
|
||||||
|
if _, err := os.Stat(d.Path); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// add to conf / save
|
||||||
|
deployments[d.Name] = d
|
||||||
|
|
||||||
|
// TODO: register with runner, like conf would
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -22,6 +22,10 @@ func (cmd *SockCommand) GetName() string {
|
||||||
return cmd.Name
|
return cmd.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cmd *SockCommand) GetSummary() string {
|
||||||
|
return cmd.Summary
|
||||||
|
}
|
||||||
|
|
||||||
func (cmd *SockCommand) Exec(c net.Conn, args []string) {
|
func (cmd *SockCommand) Exec(c net.Conn, args []string) {
|
||||||
cmd.Flag.Usage = func() {
|
cmd.Flag.Usage = func() {
|
||||||
// helpFunc(c, c.Name)
|
// helpFunc(c, c.Name)
|
||||||
|
@ -82,7 +86,7 @@ func initRunning() {
|
||||||
// drop down pid
|
// drop down pid
|
||||||
pidfile, err := os.Create(warren.WarrendPidFile)
|
pidfile, err := os.Create(warren.WarrendPidFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Could not create pidfile: ", warren.WarrendPidFile(), " : ", err)
|
log.Fatal("Could not create pidfile: ", warren.WarrendPidFile, " : ", err)
|
||||||
}
|
}
|
||||||
pid := os.Getpid()
|
pid := os.Getpid()
|
||||||
fmt.Fprintf(pidfile, "%d", pid)
|
fmt.Fprintf(pidfile, "%d", pid)
|
||||||
|
@ -109,9 +113,15 @@ func main() {
|
||||||
log.Fatal("warrend instance already running")
|
log.Fatal("warrend instance already running")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LoadConf()
|
||||||
|
if conf == nil {
|
||||||
|
log.Fatal("failed to load conf")
|
||||||
|
}
|
||||||
|
|
||||||
initRunning()
|
initRunning()
|
||||||
|
|
||||||
warren.RegisterCommand(statusCmd)
|
warren.RegisterCommand(statusCmd)
|
||||||
|
warren.RegisterCommand(regCmd)
|
||||||
|
|
||||||
// Handle signals
|
// Handle signals
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
|
|
||||||
type ICommand interface {
|
type ICommand interface {
|
||||||
GetName() string
|
GetName() string
|
||||||
|
GetSummary() string
|
||||||
}
|
}
|
||||||
|
|
||||||
var commands map[string]ICommand = make(map[string]ICommand)
|
var commands map[string]ICommand = make(map[string]ICommand)
|
||||||
|
@ -30,6 +31,10 @@ func (c *Command) GetName() string {
|
||||||
return c.Name
|
return c.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Command) GetSummary() string {
|
||||||
|
return c.Summary
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Command) Exec(args []string) {
|
func (c *Command) Exec(args []string) {
|
||||||
c.Flag.Usage = func() {
|
c.Flag.Usage = func() {
|
||||||
// helpFunc(c, c.Name)
|
// helpFunc(c, c.Name)
|
||||||
|
@ -47,8 +52,8 @@ func GetCommand(name string) ICommand {
|
||||||
}
|
}
|
||||||
|
|
||||||
var usageTmpl = template.Must(template.New("usage").Parse(`
|
var usageTmpl = template.Must(template.New("usage").Parse(`
|
||||||
Commands:{{range .}}
|
Commands:{{range $k, $v := .}}
|
||||||
{{.Name | printf "%-10s"}} {{.Summary}}{{end}}
|
{{$v.GetName| printf "%-10s"}} {{$v.GetSummary}}{{end}}
|
||||||
`))
|
`))
|
||||||
|
|
||||||
func PrintCommandUsage() {
|
func PrintCommandUsage() {
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
const CmdSockType string = "unix"
|
const CmdSockType string = "unix"
|
||||||
const CmdSockAddr string = "/var/run/warrend/warrend.sock"
|
const CmdSockAddr string = "/var/run/warrend/warrend.sock"
|
||||||
const WarrendPidFile string = "/var/run/warrend/warrend.pid"
|
const WarrendPidFile string = "/var/run/warrend/warrend.pid"
|
||||||
const WarrendConf string = "/etc/warrend.conf"
|
const WarrendConf string = "/etc/warrend.json"
|
||||||
|
|
||||||
func GitHash() string {
|
func GitHash() string {
|
||||||
hash, err := exec.Command("git", "rev-parse", "HEAD").Output()
|
hash, err := exec.Command("git", "rev-parse", "HEAD").Output()
|
||||||
|
|
Loading…
Reference in New Issue