// RemoveInstanceFromDiscovery removes an instance from etcd discovery.
func RemoveInstanceFromDiscovery(i swarmtypes.Instance) error {
	etcdMemberName, err := ssh.GetEtcd2MemberName(i.PublicIPAddress)
	if err != nil {
		return errgo.Mask(err)
	}
	// Not a quorum member
	if etcdMemberName == "" {
		return nil
	}

	discoveryUrl, err := ssh.GetEtcdDiscoveryUrl(i.PublicIPAddress)
	if err != nil {
		return errgo.Mask(err)
	}

	machineUrl := discoveryUrl + "/" + etcdMemberName
	req, err := http.NewRequest("DELETE", machineUrl, nil)
	if err != nil {
		return errgo.Mask(err)
	}

	_, err = http.DefaultClient.Do(req)
	if err != nil {
		return errgo.Mask(err)
	}
	return nil
}
Exemple #2
0
func runEtcd(args []string) (exit int) {
	if len(args) != 2 {
		return exitError("usage: kocho etcd discovery|peers <swarm>")
	}

	subCommand := args[0]
	swarmName := args[1]

	s, err := swarmService.Get(swarmName, swarm.AWS)
	if err != nil {
		return exitError(fmt.Sprintf("couldn't get instances of swarm: %s", swarmName), err)
	}

	instances, err := s.GetInstances()
	if err != nil {
		exitError(err)
	}

	if len(instances) == 0 {
		return exitError(errgo.Newf("could not find any running instances in swarm %s", swarmName))
	}

	switch subCommand {
	case "discovery":
		url, err := ssh.GetEtcdDiscoveryUrl(instances[0].PublicIPAddress)
		if err != nil {
			return exitError(err)
		}

		fmt.Println(url)
	case "peers":
		etcdPeers := []string{}
		for _, instance := range instances {
			etcdPeers = append(etcdPeers, fmt.Sprintf("http://%v:2379", instance.PrivateIPAddress))
		}
		peers := strings.Join(etcdPeers, ",")

		fmt.Println(peers)
	}

	return 0
}