Exemple #1
0
func (v vmware) FetchUserdata() ([]byte, error) {
	encoding, err := v.readConfig("coreos.config.data.encoding")
	if err != nil {
		return nil, err
	}

	data, err := v.readConfig("coreos.config.data")
	if err != nil {
		return nil, err
	}

	// Try to fallback to url if no explicit data
	if data == "" {
		url, err := v.readConfig("coreos.config.url")
		if err != nil {
			return nil, err
		}

		if url != "" {
			rawData, err := v.urlDownload(url)
			if err != nil {
				return nil, err
			}
			data = string(rawData)
		}
	}

	if encoding != "" {
		return config.DecodeContent(data, encoding)
	}
	return []byte(data), nil
}
func WriteFile(f *File, root string) (string, error) {
	fullpath := path.Join(root, f.Path)
	dir := path.Dir(fullpath)
	log.Printf("Writing file to %q", fullpath)

	content, err := config.DecodeContent(f.Content, f.Encoding)

	if err != nil {
		return "", fmt.Errorf("Unable to decode %s (%v)", f.Path, err)
	}

	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(), 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
	}

	log.Printf("Wrote file to %q", fullpath)
	return fullpath, nil
}
Exemple #3
0
// checkEncoding validates that, for each file under 'write_files', the
// content can be decoded given the specified encoding.
func checkEncoding(cfg node, report *Report) {
	for _, f := range cfg.Child("write_files").children {
		e := f.Child("encoding")
		if !e.IsValid() {
			continue
		}

		c := f.Child("content")
		if _, err := config.DecodeContent(c.String(), e.String()); err != nil {
			report.Error(c.line, fmt.Sprintf("content cannot be decoded as %q", e.String()))
		}
	}
}