2014-04-02 02:02:12 +04:00
|
|
|
package initialize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/coreos/coreos-cloudinit/system"
|
|
|
|
)
|
|
|
|
|
|
|
|
const DefaultIpv4Address = "127.0.0.1"
|
|
|
|
|
|
|
|
func generateEtcHosts(option string) (out string, err error) {
|
|
|
|
if option != "localhost" {
|
|
|
|
return "", errors.New("Invalid option to manage_etc_hosts")
|
|
|
|
}
|
|
|
|
|
|
|
|
// use the operating system hostname
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-04-07 22:01:17 +04:00
|
|
|
return fmt.Sprintf("%s %s\n", DefaultIpv4Address, hostname), nil
|
2014-04-02 02:02:12 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write an /etc/hosts file
|
|
|
|
func WriteEtcHosts(option string, root string) error {
|
|
|
|
|
|
|
|
etcHosts, err := generateEtcHosts(option)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
file := system.File{
|
|
|
|
Path: path.Join(root, "etc", "hosts"),
|
|
|
|
RawFilePermissions: "0644",
|
|
|
|
Content: etcHosts,
|
|
|
|
}
|
|
|
|
|
|
|
|
return system.WriteFile(&file)
|
|
|
|
}
|