etc_hosts.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package system
  15. import (
  16. "errors"
  17. "fmt"
  18. "os"
  19. "path"
  20. "github.com/rancher/os/config/cloudinit/config"
  21. )
  22. const DefaultIpv4Address = "127.0.0.1"
  23. type EtcHosts struct {
  24. config.EtcHosts
  25. }
  26. func (eh EtcHosts) generateEtcHosts() (out string, err error) {
  27. if eh.EtcHosts != "localhost" {
  28. return "", errors.New("Invalid option to manage_etc_hosts")
  29. }
  30. // use the operating system hostname
  31. hostname, err := os.Hostname()
  32. if err != nil {
  33. return "", err
  34. }
  35. return fmt.Sprintf("%s %s\n", DefaultIpv4Address, hostname), nil
  36. }
  37. func (eh EtcHosts) File() (*File, error) {
  38. if eh.EtcHosts == "" {
  39. return nil, nil
  40. }
  41. etcHosts, err := eh.generateEtcHosts()
  42. if err != nil {
  43. return nil, err
  44. }
  45. return &File{config.File{
  46. Path: path.Join("etc", "hosts"),
  47. RawFilePermissions: "0644",
  48. Content: etcHosts,
  49. }}, nil
  50. }