Ejemplo n.º 1
0
// iam injects info into the environment about a host's self.
//
// Sets the following environment variables. (Values represent the data format.
// Instances will get its own values.)
//
//	MY_NODEIP=10.245.1.3
//	MY_SERVICE_IP=10.22.1.4
// 	MY_PORT_PEER=2380
// 	MY_PORT_CLIENT=2379
//	MY_NAMESPACE=default
//	MY_SELFLINK=/api/v1/namespaces/default/pods/deis-etcd-1-336jp
//	MY_UID=62a3b54a-6956-11e5-b8ab-0800279dd272
//	MY_APISERVER=https://10.247.0.1:443
//	MY_NAME=deis-etcd-1-336jp
//	MY_IP=10.246.44.7
//	MY_LABEL_NAME=deis-etcd-1   # One entry per label in the JSON
// 	MY_ANNOTATION_NAME=deis-etcd-1  # One entry per annitation in the JSON
//	MY_PORT_CLIENT=4100
//	MY_PORT_PEER=2380
func iam(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
	me, err := aboutme.FromEnv()
	if err != nil {
		log.Errf(c, "Failed aboutme.FromEnv: %s", err)
	} else {

		if strings.TrimSpace(me.IP) == "" {
			log.Warn(c, "No IP found by API query.")
			ip, err := aboutme.MyIP()
			if err != nil || ip == "" {
				// Force pod death.
				log.Errf(c, "Failed to get an IP address: %s", err)
				os.Exit(5)
			}
		}

		me.ShuntEnv()
		os.Setenv("ETCD_NAME", me.Name)
		c.Put("ETCD_NAME", me.Name)
	}

	passEnv("MY_PORT_CLIENT", "$DEIS_ETCD_1_SERVICE_PORT_CLIENT")
	passEnv("MY_PORT_PEER", "$DEIS_ETCD_1_SERVICE_PORT_PEER")
	return nil, nil
}
Ejemplo n.º 2
0
func main() {
	log.Printf("Starting etcd-discovery boot version %s", version)

	ip, err := aboutme.MyIP()
	if err != nil {
		log.Printf("Failed to start because could not get IP: %s", err)
		os.Exit(321)
	}

	port := os.Getenv("DEIS_ETCD_CLIENT_PORT")
	if port == "" {
		port = "2381"
	}

	aurl := fmt.Sprintf("http://%s:%s", ip, port)
	curl := fmt.Sprintf("http://%s:%s,http://localhost:%s", ip, port, port)

	cmd := exec.Command("etcd", "-advertise-client-urls", aurl, "-listen-client-urls", curl)
	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout
	go func() {
		err := cmd.Start()
		if err != nil {
			log.Printf("Failed to start etcd: %s", err)
			os.Exit(2)
		}
	}()

	// Give etcd time to start up.
	log.Print("Etcd needs to start. Sleeping for 5 seconds...")
	time.Sleep(5 * time.Second)
	log.Print("Woke up.")

	uuid, err := discovery.Token()
	if err != nil {
		log.Printf("Failed to read %s", discovery.TokenFile)
		os.Exit(404)
	}
	size := os.Getenv("DEIS_ETCD_CLUSTER_SIZE")
	if size == "" {
		size = "3"
	}

	key := fmt.Sprintf(discovery.ClusterSizeKey, uuid)
	cli := etcd.NewClient([]string{"http://localhost:2381"})
	if _, err := cli.Create(key, size, 0); err != nil {
		log.Printf("Failed to add key: %s", err)
	}

	log.Printf("etcd-discovery service secret is %s.", key)
	log.Printf("etcd-discovery service is running on %s:%s.", ip, port)
	if err := cmd.Wait(); err != nil {
		log.Printf("Etcd stopped running: %s", err)
	}
}
Ejemplo n.º 3
0
// iam injects info into the environment about a host's self.
//
// Sets the following environment variables. (Values represent the data format.
// Instances will get its own values.)
//
//	MY_NODEIP=10.245.1.3
//	MY_SERVICE_IP=10.22.1.4
// 	MY_PORT_PEER=2380
// 	MY_PORT_CLIENT=2379
//	MY_NAMESPACE=default
//	MY_SELFLINK=/api/v1/namespaces/default/pods/deis-etcd-1-336jp
//	MY_UID=62a3b54a-6956-11e5-b8ab-0800279dd272
//	MY_APISERVER=https://10.247.0.1:443
//	MY_NAME=deis-etcd-1-336jp
//	MY_IP=10.246.44.7
//	MY_LABEL_NAME=deis-etcd-1   # One entry per label in the JSON
// 	MY_ANNOTATION_NAME=deis-etcd-1  # One entry per annitation in the JSON
//	MY_PORT_CLIENT=4100
//	MY_PORT_PEER=2380
func iam(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
	me, err := aboutme.FromEnv()
	if err != nil {
		log.Errf(c, "Failed aboutme.FromEnv: %s", err)
		log.Warn(c, "Attempting to recover.")
	}

	// This will try to recover whenever IP is not set. Only some fields
	// can be recovered. But all we really need is IP and Name.
	if strings.TrimSpace(me.IP) == "" {
		log.Warn(c, "No IP found by API query.")
		ip, err := aboutme.MyIP()
		if err != nil || ip == "" {
			// Force pod death.
			log.Errf(c, "Failed to get an IP address: %s", err)
			os.Exit(5)
		}
		me.IP = ip
	}
	if strings.TrimSpace(me.Name) == "" {
		// Try to set name from DAPI.
		me.Name = os.Getenv("POD_NAME")
		log.Warnf(c, "Setting name to %q", me.Name)
	}
	if strings.TrimSpace(me.Namespace) == "" {
		// Try to set namespace from DAPI.
		me.Namespace = os.Getenv("POD_NAMESPACE")
		log.Warnf(c, "Setting name to %q", me.Namespace)
	}

	me.ShuntEnv()
	os.Setenv("ETCD_NAME", me.Name)
	c.Put("ETCD_NAME", me.Name)

	passEnv("MY_PORT_CLIENT", "$DEIS_ETCD_1_SERVICE_PORT_CLIENT")
	passEnv("MY_PORT_PEER", "$DEIS_ETCD_1_SERVICE_PORT_PEER")
	return nil, nil
}