entrypoint.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package control
  2. import (
  3. "os"
  4. "os/exec"
  5. "syscall"
  6. "github.com/codegangsta/cli"
  7. "github.com/rancher/os/log"
  8. "golang.org/x/net/context"
  9. "github.com/rancher/os/cmd/cloudinitexecute"
  10. "github.com/rancher/os/config"
  11. "github.com/rancher/os/docker"
  12. "github.com/rancher/os/util"
  13. )
  14. const (
  15. ca = "/etc/ssl/certs/ca-certificates.crt"
  16. caBase = "/etc/ssl/certs/ca-certificates.crt.rancher"
  17. )
  18. func entrypointAction(c *cli.Context) error {
  19. if _, err := os.Stat("/host/dev"); err == nil {
  20. cmd := exec.Command("mount", "--rbind", "/host/dev", "/dev")
  21. if err := cmd.Run(); err != nil {
  22. log.Errorf("Failed to mount /dev: %v", err)
  23. }
  24. }
  25. if err := util.FileCopy(caBase, ca); err != nil && !os.IsNotExist(err) {
  26. log.Error(err)
  27. }
  28. cfg := config.LoadConfig()
  29. shouldWriteFiles := false
  30. for _, file := range cfg.WriteFiles {
  31. if file.Container != "" {
  32. shouldWriteFiles = true
  33. }
  34. }
  35. if shouldWriteFiles {
  36. writeFiles(cfg)
  37. }
  38. setupCommandSymlinks()
  39. if len(os.Args) < 3 {
  40. return nil
  41. }
  42. binary, err := exec.LookPath(os.Args[2])
  43. if err != nil {
  44. return err
  45. }
  46. return syscall.Exec(binary, os.Args[2:], os.Environ())
  47. }
  48. func writeFiles(cfg *config.CloudConfig) error {
  49. id, err := util.GetCurrentContainerID()
  50. if err != nil {
  51. return err
  52. }
  53. client, err := docker.NewSystemClient()
  54. if err != nil {
  55. return err
  56. }
  57. info, err := client.ContainerInspect(context.Background(), id)
  58. if err != nil {
  59. return err
  60. }
  61. cloudinitexecute.WriteFiles(cfg, info.Name[1:])
  62. return nil
  63. }
  64. func setupCommandSymlinks() {
  65. for _, link := range []symlink{
  66. {config.RosBin, "/usr/bin/cloud-init-execute"},
  67. {config.RosBin, "/usr/bin/cloud-init-save"},
  68. {config.RosBin, "/usr/bin/dockerlaunch"},
  69. {config.RosBin, "/usr/bin/respawn"},
  70. {config.RosBin, "/usr/bin/system-docker"},
  71. {config.RosBin, "/usr/sbin/netconf"},
  72. {config.RosBin, "/usr/sbin/wait-for-docker"},
  73. {config.RosBin, "/usr/sbin/poweroff"},
  74. {config.RosBin, "/usr/sbin/reboot"},
  75. {config.RosBin, "/usr/sbin/halt"},
  76. {config.RosBin, "/usr/sbin/shutdown"},
  77. {config.RosBin, "/sbin/poweroff"},
  78. {config.RosBin, "/sbin/reboot"},
  79. {config.RosBin, "/sbin/halt"},
  80. {config.RosBin, "/sbin/shutdown"},
  81. } {
  82. os.Remove(link.newname)
  83. if err := os.Symlink(link.oldname, link.newname); err != nil {
  84. log.Error(err)
  85. }
  86. }
  87. }