Skip to content

Commit

Permalink
installer: enables override of tsuru api configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
andrestc committed Jun 30, 2017
1 parent 31171aa commit 63c4029
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
5 changes: 5 additions & 0 deletions tsuru/installer/components.go
Expand Up @@ -25,6 +25,11 @@ type ComponentsConfig struct {
RootUserEmail string `yaml:"-"`
RootUserPassword string `yaml:"-"`
IaaSConfig iaasConfig `yaml:"-"`
Tsuru tsuruComponent
}

type tsuruComponent struct {
Config map[string]interface{}
}

type TsuruAPI struct{}
Expand Down
15 changes: 15 additions & 0 deletions tsuru/installer/defaultconfig/config.go
@@ -1,3 +1,18 @@
package defaultconfig

import (
"fmt"

yaml "gopkg.in/yaml.v1"
)

//go:generate bash -c "rm -f configs.go && go run ./generator/main.go -o configs.go"

func DefaultTsuruConfig() map[string]interface{} {
conf := make(map[string]interface{})
err := yaml.Unmarshal([]byte(Tsuru), conf)
if err != nil {
panic(fmt.Sprintf("invalid default config for tsuru api: %s", err))
}
return conf
}
4 changes: 4 additions & 0 deletions tsuru/installer/install_test.go
Expand Up @@ -37,6 +37,9 @@ func (s *S) TestParseConfigFileNotExists(c *check.C) {
}

func (s *S) TestParseConfigFile(c *check.C) {
expectedTsuruConf := defaultconfig.DefaultTsuruConfig()
expectedTsuruConf["debug"] = true
expectedTsuruConf["repo-manager"] = "gandalf"
expected := &InstallOpts{
Name: "tsuru-test",
DockerMachineConfig: dm.DockerMachineConfig{
Expand Down Expand Up @@ -65,6 +68,7 @@ func (s *S) TestParseConfigFile(c *check.C) {
},
},
},
Tsuru: tsuruComponent{Config: expectedTsuruConf},
},
Hosts: hostGroups{
Apps: hostGroupConfig{
Expand Down
35 changes: 24 additions & 11 deletions tsuru/installer/installer.go
Expand Up @@ -14,6 +14,8 @@ import (
"strings"
"time"

yaml "gopkg.in/yaml.v1"

"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/provision"
"github.com/docker/machine/libmachine/provision/serviceaction"
Expand All @@ -36,6 +38,7 @@ var (
TargetName: "tsuru",
RootUserEmail: "admin@example.com",
RootUserPassword: "admin123",
Tsuru: tsuruComponent{Config: defaultconfig.DefaultTsuruConfig()},
},
Hosts: hostGroups{
Apps: hostGroupConfig{Size: 1},
Expand Down Expand Up @@ -81,7 +84,11 @@ func (i *Installer) Install(opts *InstallOpts) (*Installation, error) {
return nil, fmt.Errorf("pre-install checks failed: %s", errChecks)
}
setCoreDriverDefaultOpts(opts)
coreMachines, err := i.ProvisionMachines(opts.Hosts.Core.Size, opts.Hosts.Core.Driver.Options, deployTsuruConfig)
deployFunc, err := deployTsuruConfig(opts)
if err != nil {
return nil, err
}
coreMachines, err := i.ProvisionMachines(opts.Hosts.Core.Size, opts.Hosts.Core.Driver.Options, deployFunc)
if err != nil {
return nil, fmt.Errorf("failed to provision components machines: %s", err)
}
Expand Down Expand Up @@ -113,18 +120,24 @@ func (i *Installer) Install(opts *InstallOpts) (*Installation, error) {
}, nil
}

func deployTsuruConfig(m *dockermachine.Machine) error {
fmt.Println("Deploying tsuru config...")
_, err := m.Host.RunSSHCommand("sudo mkdir -p /etc/tsuru/")
func deployTsuruConfig(installOpts *InstallOpts) (func(*dockermachine.Machine) error, error) {
conf, err := yaml.Marshal(installOpts.Tsuru.Config)
if err != nil {
return fmt.Errorf("failed to create tsuru config directory: %s", err)
return nil, fmt.Errorf("failed to marshal tsuru api config: %s", err)
}
remoteWriteCmdFmt := "printf '%%s' '%s' | sudo tee %s"
_, err = m.Host.RunSSHCommand(fmt.Sprintf(remoteWriteCmdFmt, string(defaultconfig.Tsuru), "/etc/tsuru/tsuru.conf"))
if err != nil {
return fmt.Errorf("failed to write remote file: %s", err)
}
return nil
return func(m *dockermachine.Machine) error {
fmt.Println("Deploying tsuru config...")
_, err := m.Host.RunSSHCommand("sudo mkdir -p /etc/tsuru/")
if err != nil {
return fmt.Errorf("failed to create tsuru config directory: %s", err)
}
remoteWriteCmdFmt := "printf '%%s' '%s' | sudo tee %s"
_, err = m.Host.RunSSHCommand(fmt.Sprintf(remoteWriteCmdFmt, string(conf), "/etc/tsuru/tsuru.conf"))
if err != nil {
return fmt.Errorf("failed to write remote file: %s", err)
}
return nil
}, nil
}

// HACK: Sometimes docker will simply freeze while pulling the images for each
Expand Down
5 changes: 5 additions & 0 deletions tsuru/installer/testdata/hosts.yml
Expand Up @@ -16,3 +16,8 @@ driver:
name: amazonec2
options:
opt1: option1-value
components:
tsuru:
config:
debug: true
repo-manager: gandalf

0 comments on commit 63c4029

Please sign in to comment.