Skip to content

Commit

Permalink
add dynamic config support and rooted context option
Browse files Browse the repository at this point in the history
  • Loading branch information
gondor committed Feb 6, 2017
1 parent 262f2ab commit 9a7f212
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ script:
- go build

after_success:
- go build -ldflags "-X main.version=0.$TRAVIS_BUILD_NUMBER -X 'main.built=$(date -u '+%Y-%m-%d %H:%M:%S')'"
- go build -ldflags "-X main.version=1.$TRAVIS_BUILD_NUMBER -X 'main.built=$(date -u '+%Y-%m-%d %H:%M:%S')'"
- docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"
- docker build -t containx/beethoven .
- docker tag containx/beethoven containx/beethoven:0.$TRAVIS_BUILD_NUMBER
- docker push containx/beethoven:0.$TRAVIS_BUILD_NUMBER
- docker tag containx/beethoven containx/beethoven:1.$TRAVIS_BUILD_NUMBER
- docker push containx/beethoven:1.$TRAVIS_BUILD_NUMBER
- docker push containx/beethoven
# whitelist
branches:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM nginx:stable
FROM nginx:mainline

ENV DOCKER_ENV true

Expand Down
2 changes: 2 additions & 0 deletions beethoven.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ var (
Long: fmt.Sprintf(Usage, version, built),
Example: Example,
}

serveCmd = &cobra.Command{
Use: "serve",
Short: "Start serving traffic",
Run: serve,
Example: Example,
}

// Logger
log = logger.GetLogger("beethoven")
format = logging.MustStringFormatter("%{time:2006-01-02 15:04:05} %{level:.9s} [%{module}]: %{message}")
Expand Down
14 changes: 14 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ type Config struct {
// Location of the nginx.conf - default: /etc/nginx/nginx.conf
NginxConfig string `json:"nginx_config"`


// User defined configuration data that can be used as part of the template parsing
// if Beethoven is launched with --root-apps=false .
Data map[string]interface{}

/* Internal */
Version string `json:"-"`
}

var (
FileNotFound = errors.New("Cannot find the specified config file")
dryRun = false
rootedApps = true
)

// AddFlags is a hook to add additional CLI Flags
Expand All @@ -70,12 +76,14 @@ func AddFlags(cmd *cobra.Command) {
cmd.Flags().String("label", "master", "Remote: The branch to fetch the config from, env: CONFIG_LABEL")
cmd.Flags().String("profile", "default", "Remote: The profile to use, env: CONFIG_PROFILE")
cmd.Flags().Bool("dryrun", false, "Bypass NGINX validation/reload -- used for debugging logs")
cmd.Flags().Bool("root-apps", true, "True by defaults, template context is all apps from marathon. False, apps is a field in the template as well as config")
}

func LoadConfigFromCommand(cmd *cobra.Command) (*Config, error) {
remote, _ := cmd.Flags().GetBool("remote")
config, _ := cmd.Flags().GetString("config")
dryRun, _ = cmd.Flags().GetBool("dryrun")
rootedApps, _ = cmd.Flags().GetBool("root-apps")

if remote {
server := os.Getenv("CONFIG_SERVER")
Expand Down Expand Up @@ -203,3 +211,9 @@ func (c *Config) Filter() *regexp.Regexp {
func (c *Config) DryRun() bool {
return dryRun
}

// IsTemplatedAppRooted means that application from marathon are the actual object during
// template parsing. If false then applications are a sub-element.
func (c *Config) IsTemplatedAppRooted() bool {
return rootedApps
}
15 changes: 13 additions & 2 deletions generator/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,18 @@ func (g *Generator) writeConfiguration() (bool, error) {
if err != nil {
return false, fmt.Errorf("Error loading template: %s", err.Error())
}
result, err := tpl.Exec(g.templateData.Apps)

var ctx interface{} = g.templateData.Apps
if g.cfg.IsTemplatedAppRooted() == false {
if g.cfg.Data != nil {
g.templateData.Data = g.cfg.Data
} else {
g.templateData.Data = map[string]interface{}{}
}
ctx = g.templateData
}

result, err := tpl.Exec(ctx)
if err != nil {
return false, err
}
Expand All @@ -39,7 +50,7 @@ func (g *Generator) writeConfiguration() (bool, error) {
}

g.tracker.SetLastConfigRendered(time.Now())
// log.Info("wrote config: %s, contents: \n\n%s", tplFilename, result)
log.Info("wrote config: %s, contents: \n\n%s", tplFilename, result)

if g.cfg.DryRun() {
log.Debug("Has Changed from Config : %v", g.templateAndConfMatch(tplFilename))
Expand Down
1 change: 1 addition & 0 deletions generator/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type App struct {

type TemplateData struct {
Apps map[string]*App
Data map[string]interface{}
}

func marathonTaskToTask(mt *marathon.Task) Task {
Expand Down

0 comments on commit 9a7f212

Please sign in to comment.