Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Refactor local engine configuration #604

Merged
merged 2 commits into from
Oct 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
## 2.3.1 (unreleased)

FEATURES:
- Add dns alias' to run and dry-run containers [#592](https://github.com/nanobox-io/nanobox/pull/592)
- Support service configuration enhancements [#586](https://github.com/nanobox-io/nanobox/pull/586)
- Support password protected ssh keys [#576](https://github.com/nanobox-io/nanobox/pull/576)[#580](https://github.com/nanobox-io/nanobox/pull/580)[#582](https://github.com/nanobox-io/nanobox/pull/582)
- Untrack vendored files [#567](https://github.com/nanobox-io/nanobox/pull/567)

BUG FIXES:
- Refactor local engine configuration [#604](https://github.com/nanobox-io/nanobox/pull/604)
- Fix panic on linux if nfs isn't running [#601](https://github.com/nanobox-io/nanobox/pull/601)
- Handle `nanobox-update` not being in global path [#596](https://github.com/nanobox-io/nanobox/pull/596)
- Fix fast notify watcher warning typos [#594](https://github.com/nanobox-io/nanobox/pull/594)
- Update `--help` docs for cli [#593](https://github.com/nanobox-io/nanobox/pull/593)[#598](https://github.com/nanobox-io/nanobox/pull/598)
- Only pull `nanobox/*` images on `update-images` [#591](https://github.com/nanobox-io/nanobox/pull/591)
- Only add valid, un-password protected ssh keys [#572](https://github.com/nanobox-io/nanobox/pull/572)
- Fix regression and allow multiple apps on osx [#571](https://github.com/nanobox-io/nanobox/pull/571)
- Fix broken route for email login [hotfix](d9469af105be39d402e5e6ad6312f3a9ecf95f6f)


## 2.3.0 (Sept 1, 2017)

FEATURES:
Expand Down
12 changes: 6 additions & 6 deletions generators/containers/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ import (
func BuildConfig(image string) docker.ContainerConfig {
env := config.EnvID()
code := fmt.Sprintf("%s%s/code:/app", provider.HostShareDir(), env)
// mounting from b2d "global zone" to container will be the same whether local engine specified or not
engine := fmt.Sprintf("%s%s/engine:/share/engine", provider.HostShareDir(), env)

if !provider.RequiresMount() {
code = fmt.Sprintf("%s:/app", config.LocalDir())
if config.EngineDir() != "" {
engine = fmt.Sprintf("%s:/share/engine", config.EngineDir())

// todo: test this (likely docker-native linux)
engineDir, _ := config.EngineDir()
if engineDir != "" {
engine = fmt.Sprintf("%s:/share/engine", engineDir)
}
}

Expand Down Expand Up @@ -51,10 +55,6 @@ func BuildConfig(image string) docker.ContainerConfig {
// set http[s]_proxy and no_proxy vars
setProxyVars(&conf)

if config.EngineDir() != "" {
conf.Binds = append(conf.Binds, engine)
}

return conf
}

Expand Down
9 changes: 3 additions & 6 deletions generators/containers/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ func CompileConfig(image string) docker.ContainerConfig {

if !provider.RequiresMount() {
code = fmt.Sprintf("%s:/share/code", config.LocalDir())
if config.EngineDir() != "" {
engine = fmt.Sprintf("%s:/share/engine", config.EngineDir())
engineDir, _ := config.EngineDir()
if engineDir != "" {
engine = fmt.Sprintf("%s:/share/engine", engineDir)
}
}

Expand Down Expand Up @@ -51,10 +52,6 @@ func CompileConfig(image string) docker.ContainerConfig {
// set http[s]_proxy and no_proxy vars
setProxyVars(&conf)

if config.EngineDir() != "" {
conf.Binds = append(conf.Binds, engine)
}

return conf
}

Expand Down
12 changes: 8 additions & 4 deletions processors/env/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package env

import (
"fmt"
"path/filepath"

"github.com/nanobox-io/nanobox/models"
"github.com/nanobox-io/nanobox/util"
Expand All @@ -12,18 +13,21 @@ import (

// Mount sets up the env mounts
func Mount(env *models.Env) error {

if !provider.RequiresMount() {
return nil
}

display.StartTask("Mounting codebase")
defer display.StopTask()

// BUG(glinton) if the `build` isn't successful and the engine changes in the boxfile,
// this mount sticks around and must be cleaned up manually.
//
// mount the engine if it's a local directory
if config.EngineDir() != "" {
src := config.EngineDir()
dst := fmt.Sprintf("%s%s/engine", provider.HostShareDir(), env.ID)
engineDir, _ := config.EngineDir()
if engineDir != "" {
src := engineDir // local directory
dst := filepath.Join(provider.HostShareDir(), env.ID, "engine") // b2d "global zone"

// first, export the env on the workstation
if err := provider.AddMount(src, dst); err != nil {
Expand Down
38 changes: 37 additions & 1 deletion processors/env/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package env

import (
"fmt"
"path/filepath"
"time"

"github.com/jcelliott/lumber"
Expand Down Expand Up @@ -46,6 +47,18 @@ func Setup(envModel *models.Env) error {

// todo: validate boxfile nodes

// ensure local engine exists
engineDir, err := config.EngineDir()
if err != nil {
display.LocalEngineNotFound()
err = util.ErrorfQuiet("[USER] custom local engine not found - %s", err.Error())
if err2, ok := err.(util.Err); ok {
err2.Suggest = "Ensure the engine defined in your boxfile.yml exists at the location specified"
return err2
}
return err
}

// init docker client
if err := provider.Init(); err != nil {
return util.ErrorAppend(err, "failed to init docker client")
Expand All @@ -57,8 +70,31 @@ func Setup(envModel *models.Env) error {
return util.ErrorAppend(err, "failed to initialize the env data")
}

// if switch from local engine, ensure old local engine gets unmounted
oldBox := boxfile.New([]byte(envModel.UserBoxfile))
newBox, _ := boxfile.NewFromFile(config.Boxfile()) // can ignore error, we made sure it exists before this point
oldEngineName := oldBox.Node("run.config").StringValue("engine")
newEngineName := newBox.Node("run.config").StringValue("engine")
if (oldEngineName != newEngineName) && newEngineName != "" {
oldEnginePath, err := filepath.Abs(oldEngineName)
if err != nil {
// todo: ignore here so if they delete their engine it won't break until they restore it
return fmt.Errorf("Failed to resolve old engine location - %s", err.Error())
}

err = UnmountEngine(envModel, oldEnginePath)
if err != nil {
return fmt.Errorf("Failed to unmount engine - %s", err.Error())
}
}

if util_provider.HasMount(fmt.Sprintf("%s%s/code", util_provider.HostShareDir(), envModel.ID)) {
return nil
if engineDir == "" {
return nil
}
if util_provider.HasMount(fmt.Sprintf("%s%s/engine", util_provider.HostShareDir(), envModel.ID)) {
return nil
}
}

display.OpenContext("Preparing environment")
Expand Down
26 changes: 21 additions & 5 deletions processors/env/unmount.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ func Unmount(env *models.Env) error {
defer display.StopTask()

// unmount the engine if it's a local directory
if config.EngineDir() != "" {
src := config.EngineDir()
dst := filepath.Join(provider.HostShareDir(), env.ID, "engine")
engineDir, _ := config.EngineDir()
if engineDir != "" {
src := engineDir // local directory
dst := filepath.Join(provider.HostShareDir(), env.ID, "engine") // b2d "global zone"

// unmount the env on the provider
if err := provider.RemoveMount(src, dst); err != nil {
display.ErrorTask()
return util.ErrorAppend(err, "failed to remove engine mount")
}

}

// unmount the app src
Expand All @@ -47,6 +47,21 @@ func Unmount(env *models.Env) error {
return nil
}

func UnmountEngine(env *models.Env, engineDir string) error {
// unmount the engine if it's a local directory
if engineDir != "" {
src := engineDir // local directory
dst := filepath.Join(provider.HostShareDir(), env.ID, "engine") // b2d "global zone"

// unmount the env on the provider
if err := provider.RemoveMount(src, dst); err != nil {
display.ErrorTask()
return util.ErrorAppend(err, "failed to cleanup old engine mount")
}
}
return nil
}

// mountsInUse returns true if any of the env's apps are running
func mountsInUse(env *models.Env) bool {
devApp, _ := models.FindAppBySlug(env.ID, "dev")
Expand All @@ -58,7 +73,8 @@ func mountsInUse(env *models.Env) bool {
func mounted(env *models.Env) bool {

// if the engine is mounted, check that
if config.EngineDir() != "" {
engineDir, _ := config.EngineDir()
if engineDir != "" {
dst := filepath.Join(provider.HostShareDir(), env.ID, "engine")

if provider.HasMount(dst) {
Expand Down
33 changes: 25 additions & 8 deletions util/config/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"syscall"
Expand Down Expand Up @@ -107,22 +108,38 @@ func SSHDir() string {
return filepath.ToSlash(filepath.Join(p, ".ssh"))
}

var engineDir string

// EngineDir gets the directory of the engine if it is a directory and on the
// local file system
func EngineDir() string {

box := boxfile.NewFromPath(Boxfile())
engineName := box.Node("env").StringValue("engine")
func EngineDir() (string, error) {
if engineDir != "" {
return engineDir, nil
}

//
engineName := boxfile.NewFromPath(Boxfile()).Node("run.config").StringValue("engine")
if engineName != "" {
// if engine specified is not a filepath, set engine path to blank
var validLocal = regexp.MustCompile(`^[~|\.|\/|\\]`)
if !validLocal.MatchString(engineName) {
return "", nil
}

fi, err := os.Stat(engineName)
if err == nil && fi.IsDir() {
return engineName
if err != nil {
return "", fmt.Errorf("Failed to find engine - %s", err.Error())
}
if fi.IsDir() {
path, err := filepath.Abs(engineName)
if err != nil {
return "", fmt.Errorf("Failed to resolve engine location - %s", err.Error())
}
engineDir = path
return path, nil
}
}

return ""
return "", nil
}

// BinDir creates a directory where nanobox specific binaries can be downloaded
Expand Down
11 changes: 11 additions & 0 deletions util/display/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,14 @@ local web/worker, please use 'nanobox run'.
--------------------------------------------------------------------------------
`))
}

func LocalEngineNotFound() {
os.Stderr.WriteString(fmt.Sprintf(`
--------------------------------------------------------------------------------
LOCAL ENGINE NOT FOUND
It appears the local engine sepcified does not exist at the location defined.
Please double check your boxfile.yml and the path to the engine. If the path
you specified exists, please contact support.
--------------------------------------------------------------------------------
`))
}
1 change: 0 additions & 1 deletion util/provider/share/share_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ func (sh *ShareRPC) Add(req Request, resp *Response) error {

// Remove will remove an nfs share
func Remove(path string) error {

// generate the entry
entry, err := entry(path)
if err != nil {
Expand Down