Skip to content

Commit

Permalink
Add RenderConfig option to ContainerPilot.
Browse files Browse the repository at this point in the history
This is useful in AutoPilot applications like MySQL that need to
rewrite the containerpilot.conf file and force a SIGHUP.
  • Loading branch information
sodre committed Jan 24, 2017
1 parent 2601882 commit 5ca3213
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
73 changes: 73 additions & 0 deletions config/render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package config

import (
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
)

// A Render Application
type RenderApp struct {
ConfigFlag string
RenderFlag string
RenderedConfig []byte
}

// EmptyApp creates an empty application
func EmptyApp() *RenderApp {
app := &RenderApp{}
return app
}

// NewRenderApp creates a new Config Rendering application
func NewRenderApp(configFlag string, renderFlag string) (*RenderApp, error) {

a := EmptyApp()

if configFlag == "" {
return nil, errors.New("-config flag is required")
}
if renderFlag != "-" && !strings.HasPrefix(renderFlag, "file://") {
return nil, errors.New("-render flag is invalid")
}

a.ConfigFlag = configFlag
a.RenderFlag = renderFlag

var data []byte
if strings.HasPrefix(configFlag, "file://") {
var err error
fName := strings.SplitAfter(configFlag, "file://")[1]
if data, err = ioutil.ReadFile(fName); err != nil {
return nil, fmt.Errorf("Could not read config file: %s", err)
}
} else {
data = []byte(configFlag)
}

template, err := ApplyTemplate(data)
if err != nil {
return nil, fmt.Errorf(
"Could not apply template to config: %v", err)
}

a.RenderedConfig = template

return a, nil
}

func (a *RenderApp) Run() {
if a.RenderFlag == "-" {
fmt.Printf("%s", a.RenderedConfig)
os.Exit(0)
}
if strings.HasPrefix(a.RenderFlag, "file://") {
var err error
fName := strings.SplitAfter(a.RenderFlag, "file://")[1]
if err = ioutil.WriteFile(fName, a.RenderedConfig, 0644); err != nil {
panic(fmt.Errorf("Could not write config file: %s", err))
}
}
}
18 changes: 16 additions & 2 deletions core/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ var (
GitHash string
)

type Runnable interface {
Run()
}

// App encapsulates the state of ContainerPilot after the initial setup.
// after it is run, it can be reloaded and paused with signals.
type App struct {
Expand Down Expand Up @@ -59,14 +63,17 @@ func EmptyApp() *App {
}

// LoadApp parses the commandline arguments and loads the config
func LoadApp() (*App, error) {
func LoadApp() (Runnable, error) {

var configFlag string
var renderFlag string
var versionFlag bool

if !flag.Parsed() {
flag.StringVar(&configFlag, "config", "",
"JSON config or file:// path to JSON config file.")
flag.StringVar(&renderFlag, "render", "",
"- for stdout or file:// path where to save redenred JSON config file.")
flag.BoolVar(&versionFlag, "version", false, "Show version identifier and quit.")
flag.Parse()
}
Expand All @@ -79,7 +86,14 @@ func LoadApp() (*App, error) {
}

os.Setenv("CONTAINERPILOT_PID", fmt.Sprintf("%v", os.Getpid()))
app, err := NewApp(configFlag)
var app Runnable
var err error
if renderFlag == "" {
app, err = NewApp(configFlag)
} else {
app, err = config.NewRenderApp(configFlag, renderFlag)
}

if err != nil {
return nil, err
}
Expand Down

0 comments on commit 5ca3213

Please sign in to comment.