ea1e4c38fa
This reverts commitcdfc94f4e9
, reversing changes made to2051cd3e1c
. Conflicts: config/config.go config/config_test.go config/etc_hosts.go config/etcd.go config/file.go config/fleet.go config/oem.go config/unit.go config/update.go config/user.go initialize/config.go initialize/config_test.go initialize/env.go initialize/manage_etc_hosts.go initialize/workspace.go system/env.go system/etc_hosts_test.go system/etcd.go system/etcd_test.go system/fleet.go system/fleet_test.go system/oem.go system/oem_test.go system/systemd.go system/update.go system/update_test.go test
115 lines
2.6 KiB
Go
115 lines
2.6 KiB
Go
/*
|
|
Copyright 2014 CoreOS, Inc.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package system
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
"strconv"
|
|
)
|
|
|
|
type File struct {
|
|
Encoding string
|
|
Content string
|
|
Owner string
|
|
Path string
|
|
RawFilePermissions string `yaml:"permissions"`
|
|
}
|
|
|
|
func (f *File) Permissions() (os.FileMode, error) {
|
|
if f.RawFilePermissions == "" {
|
|
return os.FileMode(0644), nil
|
|
}
|
|
|
|
// Parse string representation of file mode as octal
|
|
perm, err := strconv.ParseInt(f.RawFilePermissions, 8, 32)
|
|
if err != nil {
|
|
return 0, errors.New("Unable to parse file permissions as octal integer")
|
|
}
|
|
return os.FileMode(perm), nil
|
|
}
|
|
|
|
func WriteFile(f *File, root string) (string, error) {
|
|
if f.Encoding != "" {
|
|
return "", fmt.Errorf("Unable to write file with encoding %s", f.Encoding)
|
|
}
|
|
|
|
fullpath := path.Join(root, f.Path)
|
|
dir := path.Dir(fullpath)
|
|
|
|
if err := EnsureDirectoryExists(dir); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
perm, err := f.Permissions()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
var tmp *os.File
|
|
// Create a temporary file in the same directory to ensure it's on the same filesystem
|
|
if tmp, err = ioutil.TempFile(dir, "cloudinit-temp"); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if err := ioutil.WriteFile(tmp.Name(), []byte(f.Content), perm); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if err := tmp.Close(); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Ensure the permissions are as requested (since WriteFile can be affected by sticky bit)
|
|
if err := os.Chmod(tmp.Name(), perm); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if f.Owner != "" {
|
|
// We shell out since we don't have a way to look up unix groups natively
|
|
cmd := exec.Command("chown", f.Owner, tmp.Name())
|
|
if err := cmd.Run(); err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
if err := os.Rename(tmp.Name(), fullpath); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return fullpath, nil
|
|
}
|
|
|
|
func EnsureDirectoryExists(dir string) error {
|
|
info, err := os.Stat(dir)
|
|
if err == nil {
|
|
if !info.IsDir() {
|
|
return fmt.Errorf("%s is not a directory", dir)
|
|
}
|
|
} else {
|
|
err = os.MkdirAll(dir, 0755)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|