Skip to content
This repository has been archived by the owner on May 18, 2021. It is now read-only.

Commit

Permalink
Merge 8280dad into 1fe3622
Browse files Browse the repository at this point in the history
  • Loading branch information
mlafeldt committed Dec 17, 2014
2 parents 1fe3622 + 8280dad commit c442124
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 206 deletions.
20 changes: 8 additions & 12 deletions chef/omnibus/omnibus.go
Expand Up @@ -16,26 +16,22 @@ var ScriptURL = "https://www.opscode.com/chef/install.sh"
// An Installer allows to install Chef.
type Installer struct {
ChefVersion string
ScriptPath string
SandboxPath string
RootPath string
}

func (i Installer) skip() bool {
return i.ChefVersion == "" || i.ChefVersion == "false"
}

func (i Installer) scriptPathTo(elem ...string) string {
slice := append([]string{i.ScriptPath}, elem...)
return path.Join(slice...)
}

func (i Installer) writeWrapperScript() error {
script := i.scriptPathTo("install-wrapper.sh")
script := path.Join(i.SandboxPath, "install-wrapper.sh")
log.Debugf("Writing install wrapper script to %s\n", script)
return ioutil.WriteFile(script, []byte(wrapperScript), 0644)
}

func (i Installer) downloadOmnibusScript() error {
script := i.scriptPathTo("install.sh")
script := path.Join(i.SandboxPath, "install.sh")
if util.FileExist(script) {
log.Debugf("Omnibus script already downloaded to %s\n", script)
return nil
Expand All @@ -44,8 +40,8 @@ func (i Installer) downloadOmnibusScript() error {
return util.DownloadFile(script, ScriptURL)
}

// PrepareScripts sets up the scripts required to install Chef.
func (i Installer) PrepareScripts() error {
// PrepareFiles sets up the scripts required to install Chef.
func (i Installer) PrepareFiles() error {
if i.skip() {
log.Debug("Skipping setup of install scripts")
return nil
Expand All @@ -66,8 +62,8 @@ func (i Installer) Command() []string {
return []string{
"sudo",
"sh",
i.scriptPathTo("install-wrapper.sh"),
i.scriptPathTo("install.sh"),
path.Join(i.RootPath, "install-wrapper.sh"),
path.Join(i.RootPath, "install.sh"),
i.ChefVersion,
}
}
8 changes: 4 additions & 4 deletions chef/omnibus/omnibus_test.go
Expand Up @@ -11,14 +11,14 @@ import (
"github.com/stretchr/testify/assert"
)

func TestPrepareScripts(t *testing.T) {
func TestPrepareFiles(t *testing.T) {
ts := httptest.NewServer(http.FileServer(http.Dir(".")))
defer ts.Close()
ScriptURL = ts.URL + "/omnibus_test.go"

wd, _ := os.Getwd()
i := Installer{ChefVersion: "1.2.3", ScriptPath: wd}
assert.NoError(t, i.PrepareScripts())
i := Installer{ChefVersion: "1.2.3", SandboxPath: wd}
assert.NoError(t, i.PrepareFiles())

defer os.Remove("install.sh")
defer os.Remove("install-wrapper.sh")
Expand All @@ -36,7 +36,7 @@ func TestCommand(t *testing.T) {
"1.2.3": []string{"sudo", "sh", "/some/path/install-wrapper.sh", "/some/path/install.sh", "1.2.3"},
}
for version, cmd := range tests {
i := Installer{ChefVersion: version, ScriptPath: "/some/path"}
i := Installer{ChefVersion: version, RootPath: "/some/path"}
assert.Equal(t, cmd, i.Command())
}
}
59 changes: 47 additions & 12 deletions main.go
Expand Up @@ -4,10 +4,12 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"time"

"github.com/mlafeldt/chef-runner/chef/cookbook"
"github.com/mlafeldt/chef-runner/chef/omnibus"
"github.com/mlafeldt/chef-runner/chef/runlist"
"github.com/mlafeldt/chef-runner/cli"
"github.com/mlafeldt/chef-runner/driver"
Expand All @@ -17,6 +19,17 @@ import (
"github.com/mlafeldt/chef-runner/log"
"github.com/mlafeldt/chef-runner/provisioner"
"github.com/mlafeldt/chef-runner/provisioner/chefsolo"
"github.com/mlafeldt/chef-runner/resolver"
)

const (
// SandboxPath is the path to the local sandbox directory where
// chef-runner stores files that will be uploaded to a machine.
SandboxPath = ".chef-runner/sandbox"

// RootPath is the path on the machine where files from SandboxPath
// will be uploaded to.
RootPath = "/tmp/chef-runner"
)

func abort(v ...interface{}) {
Expand All @@ -37,12 +50,12 @@ func findDriver(flags *cli.Flags) (driver.Driver, error) {
func uploadFiles(drv driver.Driver) error {
log.Info("Uploading local files to machine. This may take a while...")
log.Debugf("Uploading files from %s to %s on machine\n",
provisioner.SandboxPath, provisioner.RootPath)
return drv.Upload(provisioner.RootPath, provisioner.SandboxPath+"/")
SandboxPath, RootPath)
return drv.Upload(RootPath, SandboxPath+"/")
}

func installChef(drv driver.Driver, p provisioner.Provisioner) error {
installCmd := p.InstallCommand()
func installChef(drv driver.Driver, installer omnibus.Installer) error {
installCmd := installer.Command()
if len(installCmd) == 0 {
log.Info("Skipping installation of Chef")
return nil
Expand Down Expand Up @@ -114,17 +127,39 @@ func main() {

var p provisioner.Provisioner
p = chefsolo.Provisioner{
RunList: runList,
Attributes: attributes,
Format: flags.Format,
LogLevel: flags.LogLevel,
UseSudo: true,
ChefVersion: flags.ChefVersion,
RunList: runList,
Attributes: attributes,
Format: flags.Format,
LogLevel: flags.LogLevel,
UseSudo: true,

SandboxPath: SandboxPath,
RootPath: RootPath,
}

log.Debugf("Provisioner = %+v\n", p)

if err := p.CreateSandbox(); err != nil {
log.Info("Preparing local files")
log.Debug("Creating local sandbox in", SandboxPath)
if err := os.MkdirAll(SandboxPath, 0755); err != nil {
abort(err)
}

if err := p.PrepareFiles(); err != nil {
abort(err)
}

log.Debug("Preparing cookbooks")
if err := resolver.AutoResolve(path.Join(SandboxPath, "cookbooks")); err != nil {
abort(err)
}

installer := omnibus.Installer{
ChefVersion: flags.ChefVersion,
SandboxPath: SandboxPath,
RootPath: RootPath,
}
if err := installer.PrepareFiles(); err != nil {
abort(err)
}

Expand All @@ -137,7 +172,7 @@ func main() {
abort(err)
}

if err := installChef(drv, p); err != nil {
if err := installChef(drv, installer); err != nil {
abort(err)
}

Expand Down
79 changes: 19 additions & 60 deletions provisioner/chefsolo/provisioner.go
Expand Up @@ -4,12 +4,10 @@ package chefsolo
import (
"fmt"
"io/ioutil"
"path"
"strings"

"github.com/mlafeldt/chef-runner/chef/omnibus"
"github.com/mlafeldt/chef-runner/log"
base "github.com/mlafeldt/chef-runner/provisioner"
"github.com/mlafeldt/chef-runner/resolver"
)

const (
Expand All @@ -20,17 +18,16 @@ const (
DefaultLogLevel = "info"
)

// CookbookPath is the path to the sandbox directory where cookbooks are stored.
var CookbookPath = base.SandboxPathTo("cookbooks")

// Provisioner is a provisioner based on Chef Solo.
type Provisioner struct {
RunList []string
Attributes string
Format string
LogLevel string
UseSudo bool
ChefVersion string
RunList []string
Attributes string
Format string
LogLevel string
UseSudo bool

SandboxPath string
RootPath string
}

func (p Provisioner) prepareJSON() error {
Expand All @@ -39,60 +36,22 @@ func (p Provisioner) prepareJSON() error {
if p.Attributes != "" {
data = p.Attributes
}
return ioutil.WriteFile(base.SandboxPathTo("dna.json"), []byte(data), 0644)
return ioutil.WriteFile(path.Join(p.SandboxPath, "dna.json"), []byte(data), 0644)
}

func (p Provisioner) prepareSoloConfig() error {
log.Debug("Preparing Chef Solo config")
data := fmt.Sprintf("cookbook_path \"%s\"\n", base.RootPathTo("cookbooks"))
data := fmt.Sprintf("cookbook_path \"%s\"\n", path.Join(p.RootPath, "cookbooks"))
data += "ssl_verify_mode :verify_peer\n"
return ioutil.WriteFile(base.SandboxPathTo("solo.rb"), []byte(data), 0644)
}

func (p Provisioner) prepareCookbooks() error {
log.Debug("Preparing cookbooks")
return resolver.AutoResolve(CookbookPath)
}

func (p Provisioner) prepareInstallScripts() error {
i := omnibus.Installer{
ChefVersion: p.ChefVersion,
ScriptPath: base.SandboxPath,
}
return i.PrepareScripts()
}

// CreateSandbox creates the sandbox directory. This includes preparing Chef
// configuration data and cookbooks.
func (p Provisioner) CreateSandbox() error {
funcs := []func() error{
base.CreateSandbox,
p.prepareJSON,
p.prepareSoloConfig,
p.prepareCookbooks,
p.prepareInstallScripts,
}
for _, f := range funcs {
if err := f(); err != nil {
return err
}
}
return nil
}

// CleanupSandbox deletes the sandbox directory.
func (p Provisioner) CleanupSandbox() error {
return base.CleanupSandbox()
return ioutil.WriteFile(path.Join(p.SandboxPath, "solo.rb"), []byte(data), 0644)
}

// InstallCommand returns the command string to conditionally install Chef onto
// a machine.
func (p Provisioner) InstallCommand() []string {
i := omnibus.Installer{
ChefVersion: p.ChefVersion,
ScriptPath: base.RootPath,
// PrepareFiles prepares Chef configuration data.
func (p Provisioner) PrepareFiles() error {
if err := p.prepareJSON(); err != nil {
return err
}
return i.Command()
return p.prepareSoloConfig()
}

func (p Provisioner) sudo(args []string) []string {
Expand All @@ -117,8 +76,8 @@ func (p Provisioner) ProvisionCommand() []string {

cmd := []string{
"chef-solo",
"--config", base.RootPathTo("solo.rb"),
"--json-attributes", base.RootPathTo("dna.json"),
"--config", path.Join(p.RootPath, "solo.rb"),
"--json-attributes", path.Join(p.RootPath, "dna.json"),
"--format", format,
"--log_level", logLevel,
}
Expand Down

0 comments on commit c442124

Please sign in to comment.