Repository: nanovms/ops
Pull request #1029 (2021-05-11 12:34:13)

fix paths specified in configuration file
closes #1028 


Patch:
diff --git a/cmd/flags_config.go b/cmd/flags_config.go
index 3bc4291..6b3aacf 100644
--- a/cmd/flags_config.go
+++ b/cmd/flags_config.go
@@ -6,6 +6,7 @@ import (
 	"fmt"
 	"io/ioutil"
 	"os"
+	"path"
 	"strings"
 
 	"github.com/nanovms/ops/lepton"
@@ -36,6 +37,8 @@ func (flags *ConfigCommandFlags) MergeToConfig(c *types.Config) (err error) {
 
 		err = unWarpConfig(flags.Config, c)
 
+		c.LocalFilesParentDirectory = path.Dir(flags.Config)
+
 		return
 	} else if c == nil {
 		*c = *lepton.NewConfig()
diff --git a/fs/manifest.go b/fs/manifest.go
index 5faca13..ba68257 100644
--- a/fs/manifest.go
+++ b/fs/manifest.go
@@ -51,17 +51,21 @@ func (m *Manifest) AddNetworkConfig(networkConfig *ManifestNetworkConfig) {
 }
 
 // AddUserProgram adds user program
-func (m *Manifest) AddUserProgram(imgpath string) {
+func (m *Manifest) AddUserProgram(imgpath string) (err error) {
 	parts := strings.Split(imgpath, "/")
 	if parts[0] == "." {
 		parts = parts[1:]
 	}
 	program := path.Join("/", path.Join(parts...))
-	err := m.AddFile(program, imgpath)
+
+	err = m.AddFile(program, imgpath)
 	if err != nil {
-		panic(err)
+		return
 	}
+
 	m.SetProgram(program)
+
+	return
 }
 
 // SetProgram sets user program
@@ -142,7 +146,11 @@ func (m *Manifest) AddKernel(path string) {
 }
 
 // AddDirectory adds all files in dir to image
-func (m *Manifest) AddDirectory(dir string) error {
+func (m *Manifest) AddDirectory(dir string, workDir string) error {
+	if err := os.Chdir(workDir); err != nil {
+		return err
+	}
+
 	err := filepath.Walk(dir, func(hostpath string, info os.FileInfo, err error) error {
 		if err != nil {
 			return err
diff --git a/lepton/config.go b/lepton/config.go
index 14f8ad3..f26e8bb 100644
--- a/lepton/config.go
+++ b/lepton/config.go
@@ -50,6 +50,7 @@ func NewConfig() *types.Config {
 	c.RunConfig.Accel = true
 	c.RunConfig.Memory = "2G"
 	c.VolumesDir = LocalVolumeDir
+	c.LocalFilesParentDirectory = "."
 
 	return c
 }
diff --git a/lepton/image.go b/lepton/image.go
index 13f0300..aa4051f 100644
--- a/lepton/image.go
+++ b/lepton/image.go
@@ -233,7 +233,9 @@ func setManifestFromConfig(m *fs.Manifest, c *types.Config) error {
 	m.AddKlibs(c.RunConfig.Klibs)
 
 	for _, f := range c.Files {
-		err := m.AddFile(f, f)
+		hostPath := path.Join(c.LocalFilesParentDirectory, f)
+		filePath := f
+		err := m.AddFile(filePath, hostPath)
 		if err != nil {
 			return err
 		}
@@ -247,7 +249,7 @@ func setManifestFromConfig(m *fs.Manifest, c *types.Config) error {
 	}
 
 	for _, d := range c.Dirs {
-		err := m.AddDirectory(d)
+		err := m.AddDirectory(d, c.LocalFilesParentDirectory)
 		if err != nil {
 			return err
 		}
@@ -311,8 +313,12 @@ func BuildManifest(c *types.Config) (*fs.Manifest, error) {
 
 	addCommonFilesToManifest(m)
 
-	m.AddUserProgram(c.Program)
-	err := setManifestFromConfig(m, c)
+	err := m.AddUserProgram(c.Program)
+	if err != nil {
+		return nil, err
+	}
+
+	err = setManifestFromConfig(m, c)
 	if err != nil {
 		return nil, errors.Wrap(err, 1)
 	}
diff --git a/types/config.go b/types/config.go
index a132fc2..2cef5bd 100644
--- a/types/config.go
+++ b/types/config.go
@@ -81,6 +81,10 @@ type Config struct {
 	// RunConfig
 	RunConfig RunConfig
 
+	// LocalFilesParentDirectory is the parent directory of the files/directories specified in Files and Dirs
+	// The default value is the directory from where the ops command is running
+	LocalFilesParentDirectory string
+
 	// TargetRoot
 	TargetRoot string
 


Test patch:
diff --git a/cmd/flags_config_test.go b/cmd/flags_config_test.go
index b8592d2..6fe04b1 100644
--- a/cmd/flags_config_test.go
+++ b/cmd/flags_config_test.go
@@ -39,7 +39,8 @@ func TestConfigFlagsMergeToConfig(t *testing.T) {
 				NetMask:   "255.255.255.0",
 				Gateway:   "192.168.1.254",
 			},
-			VolumesDir: lepton.LocalVolumeDir,
+			VolumesDir:                lepton.LocalVolumeDir,
+			LocalFilesParentDirectory: ".",
 		}
 
 		writeConfigToFile(expected, configFileName)
@@ -67,7 +68,8 @@ func TestConfigFlagsMergeToConfig(t *testing.T) {
 			RunConfig: types.RunConfig{
 				Memory: "2G",
 			},
-			VolumesDir: lepton.LocalVolumeDir,
+			VolumesDir:                lepton.LocalVolumeDir,
+			LocalFilesParentDirectory: ".",
 		}
 		config := &types.Config{
 			CloudConfig: types.ProviderConfig{