Skip to content

Commit 72ff698

Browse files
authored
Merge pull request #604 from nanobox-io/bugfix/603
Refactor local engine configuration
2 parents be3b907 + 34a34d5 commit 72ff698

File tree

9 files changed

+131
-31
lines changed

9 files changed

+131
-31
lines changed

CHANGELOG.md

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
## 2.3.1 (unreleased)
2+
3+
FEATURES:
4+
- Add dns alias' to run and dry-run containers [#592](https://github.com/nanobox-io/nanobox/pull/592)
5+
- Support service configuration enhancements [#586](https://github.com/nanobox-io/nanobox/pull/586)
6+
- 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)
7+
- Untrack vendored files [#567](https://github.com/nanobox-io/nanobox/pull/567)
8+
9+
BUG FIXES:
10+
- Refactor local engine configuration [#604](https://github.com/nanobox-io/nanobox/pull/604)
11+
- Fix panic on linux if nfs isn't running [#601](https://github.com/nanobox-io/nanobox/pull/601)
12+
- Handle `nanobox-update` not being in global path [#596](https://github.com/nanobox-io/nanobox/pull/596)
13+
- Fix fast notify watcher warning typos [#594](https://github.com/nanobox-io/nanobox/pull/594)
14+
- Update `--help` docs for cli [#593](https://github.com/nanobox-io/nanobox/pull/593)[#598](https://github.com/nanobox-io/nanobox/pull/598)
15+
- Only pull `nanobox/*` images on `update-images` [#591](https://github.com/nanobox-io/nanobox/pull/591)
16+
- Only add valid, un-password protected ssh keys [#572](https://github.com/nanobox-io/nanobox/pull/572)
17+
- Fix regression and allow multiple apps on osx [#571](https://github.com/nanobox-io/nanobox/pull/571)
18+
- Fix broken route for email login [hotfix](d9469af105be39d402e5e6ad6312f3a9ecf95f6f)
19+
20+
121
## 2.3.0 (Sept 1, 2017)
222

323
FEATURES:

generators/containers/build.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ import (
1414
func BuildConfig(image string) docker.ContainerConfig {
1515
env := config.EnvID()
1616
code := fmt.Sprintf("%s%s/code:/app", provider.HostShareDir(), env)
17+
// mounting from b2d "global zone" to container will be the same whether local engine specified or not
1718
engine := fmt.Sprintf("%s%s/engine:/share/engine", provider.HostShareDir(), env)
1819

1920
if !provider.RequiresMount() {
2021
code = fmt.Sprintf("%s:/app", config.LocalDir())
21-
if config.EngineDir() != "" {
22-
engine = fmt.Sprintf("%s:/share/engine", config.EngineDir())
22+
23+
// todo: test this (likely docker-native linux)
24+
engineDir, _ := config.EngineDir()
25+
if engineDir != "" {
26+
engine = fmt.Sprintf("%s:/share/engine", engineDir)
2327
}
2428
}
2529

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

54-
if config.EngineDir() != "" {
55-
conf.Binds = append(conf.Binds, engine)
56-
}
57-
5858
return conf
5959
}
6060

generators/containers/compile.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ func CompileConfig(image string) docker.ContainerConfig {
1818

1919
if !provider.RequiresMount() {
2020
code = fmt.Sprintf("%s:/share/code", config.LocalDir())
21-
if config.EngineDir() != "" {
22-
engine = fmt.Sprintf("%s:/share/engine", config.EngineDir())
21+
engineDir, _ := config.EngineDir()
22+
if engineDir != "" {
23+
engine = fmt.Sprintf("%s:/share/engine", engineDir)
2324
}
2425
}
2526

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

54-
if config.EngineDir() != "" {
55-
conf.Binds = append(conf.Binds, engine)
56-
}
57-
5855
return conf
5956
}
6057

processors/env/mount.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package env
22

33
import (
44
"fmt"
5+
"path/filepath"
56

67
"github.com/nanobox-io/nanobox/models"
78
"github.com/nanobox-io/nanobox/util"
@@ -12,18 +13,21 @@ import (
1213

1314
// Mount sets up the env mounts
1415
func Mount(env *models.Env) error {
15-
1616
if !provider.RequiresMount() {
1717
return nil
1818
}
1919

2020
display.StartTask("Mounting codebase")
2121
defer display.StopTask()
2222

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

2832
// first, export the env on the workstation
2933
if err := provider.AddMount(src, dst); err != nil {

processors/env/setup.go

+37-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package env
22

33
import (
44
"fmt"
5+
"path/filepath"
56
"time"
67

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

4748
// todo: validate boxfile nodes
4849

50+
// ensure local engine exists
51+
engineDir, err := config.EngineDir()
52+
if err != nil {
53+
display.LocalEngineNotFound()
54+
err = util.ErrorfQuiet("[USER] custom local engine not found - %s", err.Error())
55+
if err2, ok := err.(util.Err); ok {
56+
err2.Suggest = "Ensure the engine defined in your boxfile.yml exists at the location specified"
57+
return err2
58+
}
59+
return err
60+
}
61+
4962
// init docker client
5063
if err := provider.Init(); err != nil {
5164
return util.ErrorAppend(err, "failed to init docker client")
@@ -57,8 +70,31 @@ func Setup(envModel *models.Env) error {
5770
return util.ErrorAppend(err, "failed to initialize the env data")
5871
}
5972

73+
// if switch from local engine, ensure old local engine gets unmounted
74+
oldBox := boxfile.New([]byte(envModel.UserBoxfile))
75+
newBox, _ := boxfile.NewFromFile(config.Boxfile()) // can ignore error, we made sure it exists before this point
76+
oldEngineName := oldBox.Node("run.config").StringValue("engine")
77+
newEngineName := newBox.Node("run.config").StringValue("engine")
78+
if (oldEngineName != newEngineName) && newEngineName != "" {
79+
oldEnginePath, err := filepath.Abs(oldEngineName)
80+
if err != nil {
81+
// todo: ignore here so if they delete their engine it won't break until they restore it
82+
return fmt.Errorf("Failed to resolve old engine location - %s", err.Error())
83+
}
84+
85+
err = UnmountEngine(envModel, oldEnginePath)
86+
if err != nil {
87+
return fmt.Errorf("Failed to unmount engine - %s", err.Error())
88+
}
89+
}
90+
6091
if util_provider.HasMount(fmt.Sprintf("%s%s/code", util_provider.HostShareDir(), envModel.ID)) {
61-
return nil
92+
if engineDir == "" {
93+
return nil
94+
}
95+
if util_provider.HasMount(fmt.Sprintf("%s%s/engine", util_provider.HostShareDir(), envModel.ID)) {
96+
return nil
97+
}
6298
}
6399

64100
display.OpenContext("Preparing environment")

processors/env/unmount.go

+21-5
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ func Unmount(env *models.Env) error {
2222
defer display.StopTask()
2323

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

2930
// unmount the env on the provider
3031
if err := provider.RemoveMount(src, dst); err != nil {
3132
display.ErrorTask()
3233
return util.ErrorAppend(err, "failed to remove engine mount")
3334
}
34-
3535
}
3636

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

50+
func UnmountEngine(env *models.Env, engineDir string) error {
51+
// unmount the engine if it's a local directory
52+
if engineDir != "" {
53+
src := engineDir // local directory
54+
dst := filepath.Join(provider.HostShareDir(), env.ID, "engine") // b2d "global zone"
55+
56+
// unmount the env on the provider
57+
if err := provider.RemoveMount(src, dst); err != nil {
58+
display.ErrorTask()
59+
return util.ErrorAppend(err, "failed to cleanup old engine mount")
60+
}
61+
}
62+
return nil
63+
}
64+
5065
// mountsInUse returns true if any of the env's apps are running
5166
func mountsInUse(env *models.Env) bool {
5267
devApp, _ := models.FindAppBySlug(env.ID, "dev")
@@ -58,7 +73,8 @@ func mountsInUse(env *models.Env) bool {
5873
func mounted(env *models.Env) bool {
5974

6075
// if the engine is mounted, check that
61-
if config.EngineDir() != "" {
76+
engineDir, _ := config.EngineDir()
77+
if engineDir != "" {
6278
dst := filepath.Join(provider.HostShareDir(), env.ID, "engine")
6379

6480
if provider.HasMount(dst) {

util/config/dirs.go

+25-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"os/exec"
77
"path/filepath"
8+
"regexp"
89
"runtime"
910
"strings"
1011
"syscall"
@@ -107,22 +108,38 @@ func SSHDir() string {
107108
return filepath.ToSlash(filepath.Join(p, ".ssh"))
108109
}
109110

111+
var engineDir string
112+
110113
// EngineDir gets the directory of the engine if it is a directory and on the
111114
// local file system
112-
func EngineDir() string {
113-
114-
box := boxfile.NewFromPath(Boxfile())
115-
engineName := box.Node("env").StringValue("engine")
115+
func EngineDir() (string, error) {
116+
if engineDir != "" {
117+
return engineDir, nil
118+
}
116119

117-
//
120+
engineName := boxfile.NewFromPath(Boxfile()).Node("run.config").StringValue("engine")
118121
if engineName != "" {
122+
// if engine specified is not a filepath, set engine path to blank
123+
var validLocal = regexp.MustCompile(`^[~|\.|\/|\\]`)
124+
if !validLocal.MatchString(engineName) {
125+
return "", nil
126+
}
127+
119128
fi, err := os.Stat(engineName)
120-
if err == nil && fi.IsDir() {
121-
return engineName
129+
if err != nil {
130+
return "", fmt.Errorf("Failed to find engine - %s", err.Error())
131+
}
132+
if fi.IsDir() {
133+
path, err := filepath.Abs(engineName)
134+
if err != nil {
135+
return "", fmt.Errorf("Failed to resolve engine location - %s", err.Error())
136+
}
137+
engineDir = path
138+
return path, nil
122139
}
123140
}
124141

125-
return ""
142+
return "", nil
126143
}
127144

128145
// BinDir creates a directory where nanobox specific binaries can be downloaded

util/display/messages.go

+11
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,14 @@ local web/worker, please use 'nanobox run'.
357357
--------------------------------------------------------------------------------
358358
`))
359359
}
360+
361+
func LocalEngineNotFound() {
362+
os.Stderr.WriteString(fmt.Sprintf(`
363+
--------------------------------------------------------------------------------
364+
LOCAL ENGINE NOT FOUND
365+
It appears the local engine sepcified does not exist at the location defined.
366+
Please double check your boxfile.yml and the path to the engine. If the path
367+
you specified exists, please contact support.
368+
--------------------------------------------------------------------------------
369+
`))
370+
}

util/provider/share/share_linux.go

-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ func (sh *ShareRPC) Add(req Request, resp *Response) error {
109109

110110
// Remove will remove an nfs share
111111
func Remove(path string) error {
112-
113112
// generate the entry
114113
entry, err := entry(path)
115114
if err != nil {

0 commit comments

Comments
 (0)