-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for writing files to containers from cloud-init config
We are adding the support for "write_files" section in cloud-init config for containers. Also the definition of envs is moved to the section "runcmd" with lines like "- VAR=value", however the old syntax of "VAR=value" is still supported. The rest of cloud-init config is ignored. Signed-off-by: Paul Gaiduk <paulg@zededa.com>
- Loading branch information
Showing
5 changed files
with
209 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package cloudconfig | ||
|
||
import ( | ||
"encoding/base64" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/lf-edge/eve/pkg/pillar/base" | ||
fileutils "github.com/lf-edge/eve/pkg/pillar/utils/file" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type CloudConfig struct { | ||
RunCmd []string `yaml:"runcmd"` | ||
WriteFiles []WritableFile `yaml:"write_files"` | ||
} | ||
|
||
type WritableFile struct { | ||
Path string `yaml:"path"` | ||
Content string `yaml:"content"` | ||
Permissions string `yaml:"permissions"` | ||
Encoding string `yaml:"encoding"` | ||
Owner string `yaml:"owner"` | ||
} | ||
|
||
func IsCloudConfig(ci string) bool { | ||
// check if the first line is #cloud-config | ||
lines := strings.Split(ci, "\n") | ||
if len(lines) == 0 { | ||
return false | ||
} | ||
return strings.HasPrefix(lines[0], "#cloud-config") | ||
} | ||
|
||
func ParseCloudConfig(ci string) (*CloudConfig, error) { | ||
var cc CloudConfig | ||
err := yaml.Unmarshal([]byte(ci), &cc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &cc, nil | ||
} | ||
|
||
func WriteFile(log *base.LogObject, file WritableFile, rootPath string) error { | ||
// transform file.Permission to os.FileMode | ||
perm, err := strconv.ParseUint(file.Permissions, 8, 32) | ||
if err != nil { | ||
return err | ||
} | ||
mode := os.FileMode(perm) | ||
|
||
writePath := filepath.Join(rootPath, file.Path) | ||
// sanitize path | ||
if !strings.HasPrefix(filepath.Clean(writePath), rootPath) { | ||
return fmt.Errorf("invalid path %s", writePath) | ||
} | ||
|
||
var contentBytes []byte | ||
switch file.Encoding { | ||
case "b64": | ||
// decode base64 content | ||
contentBytes, err = base64.StdEncoding.DecodeString(file.Content) | ||
if err != nil { | ||
return err | ||
} | ||
default: | ||
return errors.New("unsupported encoding type. Only base64 is supported") | ||
} | ||
|
||
log.Tracef("Creating file %s with mode %s in %s\n", file.Path, mode, rootPath) | ||
err = fileutils.WriteRename(writePath, contentBytes) | ||
if err != nil { | ||
return err | ||
} | ||
err = os.Chmod(writePath, mode) | ||
if err != nil { | ||
return err | ||
} | ||
if file.Owner != "" { | ||
log.Warn("Changing owner of files written by cloud-init is not supported yet") | ||
} | ||
|
||
return nil | ||
} |