Пример #1
0
func main() {
	kingpin.Version(version.VERSION)
	kingpin.Parse()
	log.Println("Starting bootstrap")
	agentManifest, err := pods.ManifestFromPath(*agentManifestPath)
	if err != nil {
		log.Fatalln("Could not get agent manifest: %s", err)
	}
	log.Println("Installing and launching consul")

	var consulPod *pods.Pod
	var consulManifest *pods.Manifest
	if *existingConsul == "" {
		consulManifest, err = pods.ManifestFromPath(*consulManifestPath)
		if err != nil {
			log.Fatalf("Could not get consul manifest: %s", err)
		}
		consulPod = pods.NewPod(consulManifest.ID(), pods.PodPath(*podRoot, consulManifest.ID()))
		err = InstallConsul(consulPod, consulManifest)
		if err != nil {
			log.Fatalf("Could not install consul: %s", err)
		}
	} else {
		log.Printf("Using existing Consul at %s\n", *existingConsul)

		consulPod, err = pods.ExistingPod(*existingConsul)
		if err != nil {
			log.Fatalf("The existing consul pod is invalid: %s", err)
		}
		consulManifest, err = consulPod.CurrentManifest()
		if err != nil {
			log.Fatalf("Cannot get the current consul manifest: %s", err)
		}
	}

	if err = VerifyConsulUp(*timeout); err != nil {
		log.Fatalln(err)
	}
	time.Sleep(500 * time.Millisecond)
	// schedule consul in the reality store as well, to ensure the preparers do
	// not all restart their consul agents simultaneously after bootstrapping
	err = ScheduleForThisHost(consulManifest, true)
	if err != nil {
		log.Fatalf("Could not register consul in the intent store: %s", err)
	}

	log.Println("Registering base agent in consul")
	err = ScheduleForThisHost(agentManifest, false)
	if err != nil {
		log.Fatalf("Could not register base agent with consul: %s", err)
	}
	log.Println("Installing and launching base agent")
	err = InstallBaseAgent(agentManifest)
	if err != nil {
		log.Fatalf("Could not install base agent: %s", err)
	}
	if err := VerifyReality(30*time.Second, consulManifest.ID(), agentManifest.ID()); err != nil {
		log.Fatalln(err)
	}
	log.Println("Bootstrapping complete")
}
Пример #2
0
func main() {
	restart.Version(version.VERSION)
	restart.Parse(os.Args[1:])

	pods.Log.Logger.Formatter = &logrus.TextFormatter{
		DisableTimestamp: false,
		FullTimestamp:    true,
		TimestampFormat:  "15:04:05.000",
	}
	logger := pods.Log

	if *podName == "" && *podDir == "" {
		logger.NoFields().Fatalln("Must pass a pod name or pod home directory")
	}

	var path string
	if *podName != "" {
		path = filepath.Join(pods.DEFAULT_PATH, *podName)
	} else {
		path = *podDir
	}

	pod, err := pods.ExistingPod(path)
	if err != nil {
		logger.NoFields().Fatalln(err)
	}

	manifest, err := pod.CurrentManifest()
	if err != nil {
		logger.NoFields().Fatalln(err)
	}

	logger = logger.SubLogger(logrus.Fields{"pod": pod.Id})
	logger.NoFields().Infoln("Finding services to restart")

	services, err := pod.Services(manifest)
	if err != nil {
		logger.WithError(err).Fatalln("Could not determine services to restart")
	}

	ls, err := pod.Launchables(manifest)
	if err != nil {
		logger.WithError(err).Fatalln("Could not determine launchables in pod")
	}

	if len(ls) == 0 {
		logger.NoFields().Fatalln("No launchables in pod")
	}

	for _, service := range services {
		res, err := runit.DefaultSV.Stat(&service)
		if err != nil {
			logger.WithErrorAndFields(err, logrus.Fields{"service": service.Name}).Fatalln("Could not stat service")
		}
		logger.WithFields(logrus.Fields{
			"service": service.Name,
			"time":    res.ChildTime,
			"status":  res.ChildStatus,
			"PID":     res.ChildPID,
		}).Infoln("Will restart")
	}

	logger.NoFields().Infoln("Halting pod")

	ok, err := pod.Halt(manifest)
	if err != nil {
		logger.WithError(err).Fatalln("Could not halt pod")
	} else if !ok {
		logger.NoFields().Warningln("Failed to cleanly halt services. Some may have been sent a SIGKILL.")
	}

	logger.NoFields().Infoln("Starting pod")

	ok, err = pod.Launch(manifest)
	if err != nil {
		logger.WithError(err).Fatalln("Could not start pod")
	} else if !ok {
		logger.NoFields().Warningln("Some services did not come up - check output for details")
	}

	logger.NoFields().Infoln("Restart successful.")
}