entrypoint.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package control
  2. import (
  3. "os"
  4. "os/exec"
  5. "syscall"
  6. log "github.com/Sirupsen/logrus"
  7. "github.com/codegangsta/cli"
  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. if len(os.Args) < 3 {
  39. return nil
  40. }
  41. binary, err := exec.LookPath(os.Args[2])
  42. if err != nil {
  43. return err
  44. }
  45. return syscall.Exec(binary, os.Args[2:], os.Environ())
  46. }
  47. func writeFiles(cfg *config.CloudConfig) error {
  48. id, err := util.GetCurrentContainerId()
  49. if err != nil {
  50. return err
  51. }
  52. client, err := docker.NewSystemClient()
  53. if err != nil {
  54. return err
  55. }
  56. info, err := client.ContainerInspect(context.Background(), id)
  57. if err != nil {
  58. return err
  59. }
  60. cloudinitexecute.WriteFiles(cfg, info.Name[1:])
  61. return nil
  62. }