Ejemplo n.º 1
0
func (this *Zones) Run(args []string) (exitCode int) {
	// header
	this.Ui.Output(fmt.Sprintf("%10s %-70s", "zone", "zookeeper ensemble"))
	this.Ui.Output(fmt.Sprintf("%s %s",
		strings.Repeat("-", 10),
		strings.Repeat("-", 70)))

	if len(args) > 0 {
		// user specified the zones to print
		for _, zone := range args {
			if zk, present := ctx.Zones()[zone]; present {
				this.Ui.Output(fmt.Sprintf("%10s %s", zone, zk))
			} else {
				this.Ui.Output(fmt.Sprintf("%10s not defined", zone))
			}
		}

		return
	}

	// print all by default
	defaultZone := ctx.ZkDefaultZone()
	for _, zone := range ctx.SortedZones() {
		if defaultZone == zone {
			this.Ui.Output(fmt.Sprintf("%10s %s", zone+"*", ctx.ZoneZkAddrs(zone)))
			continue
		}

		this.Ui.Output(fmt.Sprintf("%10s %s", zone, ctx.ZoneZkAddrs(zone)))
	}

	return
}
Ejemplo n.º 2
0
func forAllSortedZones(fn func(zkzone *zk.ZkZone)) {
	for _, zone := range ctx.SortedZones() {
		zkAddrs := ctx.ZoneZkAddrs(zone)
		if strings.TrimSpace(zkAddrs) == "" {
			continue
		}

		zkzone := zk.NewZkZone(zk.DefaultConfig(zone, zkAddrs))
		fn(zkzone)
	}
}
Ejemplo n.º 3
0
func (this *Zones) Run(args []string) (exitCode int) {
	cmdFlags := flag.NewFlagSet("zones", flag.ContinueOnError)
	cmdFlags.Usage = func() { this.Ui.Output(this.Help()) }
	cmdFlags.BoolVar(&this.ipInNumber, "n", false, "")
	cmdFlags.BoolVar(&this.plain, "plain", false, "")
	if err := cmdFlags.Parse(args); err != nil {
		return 2
	}

	// print all by default
	zones := make([][]string, 0)
	defaultZone := ctx.ZkDefaultZone()
	for _, zone := range ctx.SortedZones() {
		if defaultZone == zone {
			if this.ipInNumber {
				zones = append(zones, []string{zone + "*", ctx.ZoneZkAddrs(zone)})
			} else {
				zones = append(zones, []string{zone + "*", ctx.NamedZoneZkAddrs(zone)})
			}

			continue
		}

		if this.ipInNumber {
			zones = append(zones, []string{zone, ctx.ZoneZkAddrs(zone)})
		} else {
			zones = append(zones, []string{zone, ctx.NamedZoneZkAddrs(zone)})
		}

	}

	if this.plain {
		for _, z := range zones {
			this.Ui.Output(fmt.Sprintf("%s:", z[0]))
			this.Ui.Output(fmt.Sprintf("%s\n", z[1]))
		}
		return
	}

	table := tablewriter.NewWriter(os.Stdout)
	for _, z := range zones {
		table.Append(z)
	}
	table.SetHeader([]string{"Zone", "ZK ensemble"})
	table.SetFooter([]string{"Total", fmt.Sprintf("%d", len(zones))})
	table.Render() // Send output

	return

}
Ejemplo n.º 4
0
func forSortedZones(fn func(zkzone *zk.ZkZone)) {
	for _, zone := range ctx.SortedZones() {
		if strings.HasPrefix(zone, "z_") {
			// zk only
			continue
		}

		zkAddrs := ctx.ZoneZkAddrs(zone)
		if strings.TrimSpace(zkAddrs) == "" {
			continue
		}

		zkzone := zk.NewZkZone(zk.DefaultConfig(zone, zkAddrs))
		fn(zkzone)
	}
}
Ejemplo n.º 5
0
func main() {
	ctx.LoadFromHome()
	log.SetOutput(ioutil.Discard)
	log4go.AddFilter("stdout", log4go.INFO, log4go.NewConsoleLogWriter())

	app := os.Args[0]
	args := os.Args[1:]
	for _, arg := range args {
		if arg == "-v" || arg == "--version" {
			newArgs := make([]string, len(args)+1)
			newArgs[0] = "version"
			copy(newArgs[1:], args)
			args = newArgs
			break
		}

		if arg == "--generate-bash-completion" {
			if len(args) > 1 {
				// contextual auto complete
				lastArg := args[len(args)-2]
				switch lastArg {
				case "-z": // zone
					for _, zone := range ctx.SortedZones() {
						fmt.Println(zone)
					}
					return
				}
			}

			for name, _ := range commands {
				fmt.Println(name)
			}
			return
		}
	}
	c := cli.NewCLI(app, gafka.Version+"-"+gafka.BuildId)
	c.Args = os.Args[1:]
	if len(os.Args) > 1 {
		// command given, convert alias
		if alias, present := ctx.Alias(os.Args[1]); present {
			var cargs []string
			cargs = append(cargs, strings.Split(alias, " ")...)
			if len(os.Args) > 2 {
				cargs = append(cargs, os.Args[2:]...)
			}
			c.Args = cargs
		}
	}
	c.Commands = commands
	c.HelpFunc = func(m map[string]cli.CommandFactory) string {
		var buf bytes.Buffer
		buf.WriteString(fmt.Sprintf("A CLI tool for Zookeeper\n\n"))
		buf.WriteString(cli.BasicHelpFunc(app)(m))
		return buf.String()
	}

	exitCode, err := c.Run()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(1)
	} else if c.IsVersion() {
		os.Exit(0)
	}

	os.Exit(exitCode)
}
Ejemplo n.º 6
0
func main() {
	ctx.LoadFromHome()
	setupLogging()

	app := os.Args[0]
	args := os.Args[1:]
	for _, arg := range args {
		if arg == "-v" || arg == "--version" {
			newArgs := make([]string, len(args)+1)
			newArgs[0] = "version"
			copy(newArgs[1:], args)
			args = newArgs
			break
		}

		if arg == "--generate-bash-completion" {
			if len(args) > 1 {
				// contextual auto complete
				lastArg := args[len(args)-2]
				switch lastArg {
				case "-z": // zone
					for _, zone := range ctx.SortedZones() {
						fmt.Println(zone)
					}
					return

				case "-c": // cluster
					zone := ctx.ZkDefaultZone()
					for i := 0; i < len(args)-1; i++ {
						if args[i] == "-z" {
							zone = args[i+1]
						}
					}
					zkzone := zk.NewZkZone(zk.DefaultConfig(zone, ctx.ZoneZkAddrs(zone)))
					zkzone.ForSortedClusters(func(zkcluster *zk.ZkCluster) {
						fmt.Println(zkcluster.Name())
					})
					return

				case "-t": // topic
					zone := ctx.ZkDefaultZone()
					var cluster string
					for i := 0; i < len(args)-1; i++ {
						if args[i] == "-z" {
							zone = args[i+1]
						}

						if args[i] == "-c" {
							cluster = args[i+1]
						}
					}
					if cluster != "" {
						zkzone := zk.NewZkZone(zk.DefaultConfig(zone, ctx.ZoneZkAddrs(zone)))
						zkcluster := zkzone.NewCluster(cluster)
						topics, _ := zkcluster.Topics()
						for _, t := range topics {
							fmt.Println(t)
						}
					}
					return
				}

			}

			for name := range commands {
				fmt.Println(name)
			}
			for _, cmd := range ctx.Aliases() {
				fmt.Println(cmd)
			}
			return
		}
	}

	c := cli.NewCLI(app, gafka.Version+"-"+gafka.BuildId+"-"+gafka.BuiltAt)
	c.Args = os.Args[1:]
	if len(os.Args) > 1 {
		// command given, convert alias
		if alias, present := ctx.Alias(os.Args[1]); present {
			var cargs []string
			cargs = append(cargs, strings.Split(alias, " ")...)
			if len(os.Args) > 2 {
				cargs = append(cargs, os.Args[2:]...)
			}
			c.Args = cargs
		}
	}
	c.Commands = commands
	c.HelpFunc = func(m map[string]cli.CommandFactory) string {
		var buf bytes.Buffer
		buf.WriteString(fmt.Sprintf("Unified multi-datacenter multi-cluster kafka swiss-knife management console\n\n"))
		buf.WriteString(cli.BasicHelpFunc(app)(m))
		return buf.String()
	}

	exitCode, err := c.Run()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(1)
	} else if c.IsVersion() {
		os.Exit(0)
	}

	os.Exit(exitCode)
}
Ejemplo n.º 7
0
func (this *Zones) Run(args []string) (exitCode int) {
	cmdFlags := flag.NewFlagSet("zones", flag.ContinueOnError)
	cmdFlags.Usage = func() { this.Ui.Output(this.Help()) }
	cmdFlags.BoolVar(&this.ipInNumber, "n", false, "")
	cmdFlags.BoolVar(&this.plain, "plain", false, "")
	cmdFlags.BoolVar(&this.longFmt, "l", true, "")
	cmdFlags.BoolVar(&this.nameOnly, "s", false, "")
	cmdFlags.StringVar(&this.zone, "z", "", "")
	cmdFlags.BoolVar(&this.influxOnly, "i", false, "")
	if err := cmdFlags.Parse(args); err != nil {
		return 2
	}

	// print all by default
	lines := make([]string, 0)
	var header string
	if this.longFmt {
		header = "Zone|ZkAddr|InfluxDB"
	} else {
		header = "Zone|ZkAddr"
	}
	lines = append(lines, header)

	zones := make([][]string, 0)
	defaultZone := ctx.ZkDefaultZone()
	for _, zone := range ctx.SortedZones() {
		if !patternMatched(zone, this.zone) {
			continue
		}

		influxDbAddr := ctx.Zone(zone).InfluxAddr
		if this.influxOnly && influxDbAddr == "" {
			continue
		}

		if defaultZone == zone {
			if this.ipInNumber {
				if this.longFmt {
					lines = append(lines, fmt.Sprintf("%s*|%s|%s", zone, ctx.ZoneZkAddrs(zone), influxDbAddr))
				} else {
					lines = append(lines, fmt.Sprintf("%s*|%s", zone, ctx.ZoneZkAddrs(zone)))
				}
				zones = append(zones, []string{zone + "*", ctx.ZoneZkAddrs(zone), influxDbAddr})
			} else {
				if this.longFmt {
					lines = append(lines, fmt.Sprintf("%s*|%s|%s", zone, ctx.NamedZoneZkAddrs(zone), influxDbAddr))
				} else {
					lines = append(lines, fmt.Sprintf("%s*|%s", zone, ctx.NamedZoneZkAddrs(zone)))
				}
				zones = append(zones, []string{zone + "*", ctx.NamedZoneZkAddrs(zone), influxDbAddr})
			}

			continue
		}

		if this.ipInNumber {
			if this.longFmt {
				lines = append(lines, fmt.Sprintf("%s|%s|%s", zone, ctx.ZoneZkAddrs(zone), influxDbAddr))
			} else {
				lines = append(lines, fmt.Sprintf("%s|%s", zone, ctx.ZoneZkAddrs(zone)))
			}
			zones = append(zones, []string{zone, ctx.ZoneZkAddrs(zone), influxDbAddr})
		} else {
			if this.longFmt {
				lines = append(lines, fmt.Sprintf("%s|%s|%s", zone, ctx.NamedZoneZkAddrs(zone), influxDbAddr))
			} else {
				lines = append(lines, fmt.Sprintf("%s|%s", zone, ctx.NamedZoneZkAddrs(zone)))
			}
			zones = append(zones, []string{zone, ctx.NamedZoneZkAddrs(zone), influxDbAddr})
		}

	}

	if this.nameOnly {
		for _, z := range zones {
			// default zone name ends with '*'
			fmt.Println(strings.TrimRight(z[0], "*"))
		}
		return
	}

	if this.plain {
		for _, z := range zones {
			this.Ui.Output(fmt.Sprintf("%s:", z[0]))
			this.Ui.Output(fmt.Sprintf("%s", z[1]))
			if len(z) > 2 && len(z[2]) > 0 {
				this.Ui.Output(fmt.Sprintf("influxdb: %s", z[2]))
			}
			this.Ui.Output("")
		}
		return
	}

	if len(lines) > 1 {
		this.Ui.Output(columnize.SimpleFormat(lines))
	}

	return
}
Ejemplo n.º 8
0
func main() {
	ctx.LoadFromHome()
	setupLogging()

	app := os.Args[0]
	args := os.Args[1:]
	for _, arg := range args {
		if arg == "-v" || arg == "--version" {
			newArgs := make([]string, len(args)+1)
			newArgs[0] = "version"
			copy(newArgs[1:], args)
			args = newArgs
			break
		}

		if arg == "--generate-bash-completion" {
			if len(args) > 1 {
				// contextual auto complete
				lastArg := args[len(args)-2]
				switch lastArg {
				case "-z": // zone
					for _, zone := range ctx.SortedZones() {
						if ctx.Zone(zone).ZkHelix != "" {
							fmt.Println(zone)
						}
					}
					return

				case "-c": // cluster
					zone := ctx.ZkDefaultZone()
					for i := 0; i < len(args)-1; i++ {
						if args[i] == "-z" {
							zone = args[i+1]
						}
					}
					adm := zk.NewZkHelixAdmin(ctx.Zone(zone).ZkHelix)
					if err := adm.Connect(); err != nil {
						panic(err)
					}
					clusters, err := adm.Clusters()
					if err != nil {
						panic(err)
					}
					for _, cluster := range clusters {
						fmt.Println(cluster)
					}

					return
				}
			}

			for name := range commands {
				fmt.Println(name)
			}
			return
		}
	}

	c := cli.NewCLI(app, gafka.Version+"-"+gafka.BuildId+"-"+gafka.BuiltAt)
	c.Args = os.Args[1:]
	if len(os.Args) > 1 {
		// command given, convert alias
		if alias, present := ctx.Alias(os.Args[1]); present {
			var cargs []string
			cargs = append(cargs, strings.Split(alias, " ")...)
			if len(os.Args) > 2 {
				cargs = append(cargs, os.Args[2:]...)
			}
			c.Args = cargs
		}
	}
	c.Commands = commands
	c.HelpFunc = func(m map[string]cli.CommandFactory) string {
		var buf bytes.Buffer
		buf.WriteString(fmt.Sprintf("Helix administration console\n\n"))
		buf.WriteString(cli.BasicHelpFunc(app)(m))
		return buf.String()
	}

	exitCode, err := c.Run()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(1)
	} else if c.IsVersion() {
		os.Exit(0)
	}

	os.Exit(exitCode)
}