Esempio n. 1
0
func runServerReboot(c *cli.Context) {
	stack := mustStack(c)

	if len(c.Args()) != 0 {
		cli.ShowSubcommandHelp(c)
		os.Exit(2)
	}

	serverName := c.String("server")
	servers, err := client.Servers(stack.Uid)
	if err != nil {
		printFatal(err.Error())
	}

	server, err := findServer(servers, serverName)
	if err != nil {
		printFatal(err.Error())
	}

	if server == nil {
		printFatal("Server '" + serverName + "' not found")
	}

	fmt.Printf("Server: %s\n", server.Name)

	// confirmation is needed if the stack is production
	if !c.Bool("y") {
		mustConfirm("This operation will reboot your server during which time your server will not be available. Proceed with reboot? [yes/N]", "yes")
	}
	executeServerReboot(*stack, *server)
}
Esempio n. 2
0
func runContainerStop(c *cli.Context) {
	if len(c.Args()) == 0 {
		cli.ShowSubcommandHelp(c)
		os.Exit(2)
	}

	stack := mustStack(c)
	w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
	defer w.Flush()

	containerUid := c.Args()[0]
	container, err := client.GetContainer(stack.Uid, containerUid)
	must(err)

	if container == nil {
		printFatal("Container with Id '" + containerUid + "' not found")
	}

	asyncId, err := startContainerStop(stack.Uid, containerUid)
	if err != nil {
		printFatal(err.Error())
	}
	genericRes, err := endServerSet(*asyncId, stack.Uid)
	if err != nil {
		printFatal(err.Error())
	}
	printGenericResponse(*genericRes)
	return
}
Esempio n. 3
0
func runContainerExec(c *cli.Context) {
	if len(c.Args()) != 2 {
		cli.ShowSubcommandHelp(c)
		os.Exit(2)
	}

	fmt.Println("Running exec on container...")

	stack := mustStack(c)
	w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
	defer w.Flush()

	containerUid := c.Args()[0]
	command := c.Args()[1]
	container, err := client.GetContainer(stack.Uid, containerUid)
	must(err)

	if container == nil {
		printFatal("Container with Id '" + containerUid + "' not found")
	}

	server, err := client.GetServer(stack.Uid, container.ServerUid, 0)
	must(err)

	dockerFlags := c.String("docker-flags")
	userCommand := fmt.Sprintf("sudo docker exec %s %s %s", dockerFlags, container.Uid, command)
	err = SshToServerForCommand(*server, userCommand, true)
	if err != nil {
		printFatal(err.Error())
	}
}
Esempio n. 4
0
func runContainerAttach(c *cli.Context) {
	if len(c.Args()) != 1 {
		cli.ShowSubcommandHelp(c)
		os.Exit(2)
	}

	fmt.Println("Attaching to container...")

	stack := mustStack(c)
	w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
	defer w.Flush()

	containerUid := c.Args()[0]
	container, err := client.GetContainer(stack.Uid, containerUid)
	must(err)

	if container == nil {
		printFatal("Container with Id '" + containerUid + "' not found")
	}

	server, err := client.GetServer(stack.Uid, container.ServerUid, 0)
	must(err)

	userCommand := fmt.Sprintf("sudo docker attach --no-stdin=true --sig-proxy=false %s", container.Uid)

	err = SshToServerForCommand(*server, userCommand, false)
	if err != nil {
		printFatal(err.Error())
	}
}
Esempio n. 5
0
func runServerSettings(c *cli.Context) {
	stack := mustStack(c)

	// get the server
	serverName := c.String("server")
	if len(serverName) == 0 {
		cli.ShowSubcommandHelp(c)
		os.Exit(2)
	}

	servers, err := client.Servers(stack.Uid)
	if err != nil {
		printFatal(err.Error())
	}

	server, err := findServer(servers, serverName)
	if err != nil {
		printFatal(err.Error())
	}

	if server == nil {
		printFatal("Server '" + serverName + "' not found")
	}

	fmt.Printf("Server: %s\n", server.Name)

	getServerSettings(*stack, *server, c.Args())
}
Esempio n. 6
0
func runEnvVarsSet(c *cli.Context) {
	if len(c.Args()) != 1 {
		cli.ShowSubcommandHelp(c)
		os.Exit(2)
	}

	kv := c.Args()[0]
	kvs := strings.Split(kv, "=")

	if len(kvs) != 2 {
		cli.ShowSubcommandHelp(c)
		os.Exit(2)
	}

	key := strings.ToUpper(kvs[0])
	value := kvs[1]

	stack := mustStack(c)

	envVars, err := client.StackEnvVars(stack.Uid)
	must(err)

	existing := false
	for _, i := range envVars {
		if i.Key == key {
			if i.Readonly == true {
				printFatal("The selected environment variable is readonly")
			} else {
				existing = true
			}
		}
	}

	fmt.Println("Please wait while your environment variable setting is applied...")

	asyncId, err := startEnvVarSet(stack.Uid, key, value, existing)
	if err != nil {
		printFatal(err.Error())
	}
	genericRes, err := endEnvVarSet(*asyncId, stack.Uid)
	if err != nil {
		printFatal(err.Error())
	}
	printGenericResponse(*genericRes)

	return
}
Esempio n. 7
0
func runServerSet(c *cli.Context) {
	fmt.Println(c.Args())
	stack := mustStack(c)

	if len(c.Args()) != 1 {
		cli.ShowSubcommandHelp(c)
		os.Exit(2)
	}

	// get the server
	args := c.Args()

	serverName := c.String("server")

	// filter out the server name
	kvs := args[0]
	kva := strings.Split(kvs, "=")
	if len(kva) != 2 {
		cli.ShowSubcommandHelp(c)
		os.Exit(2)
	}
	key := kva[0]
	value := kva[1]

	servers, err := client.Servers(stack.Uid)
	if err != nil {
		printFatal(err.Error())
	}

	server, err := findServer(servers, serverName)
	if err != nil {
		printFatal(err.Error())
	}

	if server == nil {
		printFatal("Server '" + serverName + "' not found")
	}

	fmt.Printf("Server: %s\n", server.Name)

	executeServerSet(*stack, *server, c, key, value)
}
Esempio n. 8
0
func runJobRun(c *cli.Context) {

	stack := mustStack(c)

	// get the job
	if len(c.Args()) != 1 {
		cli.ShowSubcommandHelp(c)
		os.Exit(2)
	}
	jobName := c.Args()[0]

	jobs, err := client.GetJobs(stack.Uid, nil)
	if err != nil {
		printFatal(err.Error())
	}

	var jobNames []string
	for _, job := range jobs {
		jobNames = append(jobNames, job.GetBasicJob().Name)
	}

	idx, err := fuzzyFind(jobNames, jobName, false)
	if err != nil {
		printFatal(err.Error())
	}
	jobUid := string(jobs[idx].GetBasicJob().Uid)

	jobArgs := ""

	if len(c.StringSlice("arg")) > 0 {
		for i, arg := range c.StringSlice("arg") {
			if i > 0 {
				jobArgs = jobArgs + " "
			}
			jobArgs = jobArgs + "\"" + arg + "\""
		}
	}

	asyncId, err := startJobRun(stack.Uid, jobUid, &jobArgs)
	if err != nil {
		printFatal(err.Error())
	}
	genericRes, err := endJobRun(*asyncId, stack.Uid)
	if err != nil {
		printFatal(err.Error())
	}
	printGenericResponse(*genericRes)
	return
}
Esempio n. 9
0
func runServiceStop(c *cli.Context) {
	if len(c.Args()) != 1 {
		cli.ShowSubcommandHelp(c)
		os.Exit(2)
	}

	stack := mustStack(c)
	serviceName := c.Args()[0]

	flagServer := c.String("server")

	var serverUid *string
	if flagServer == "" {
		serverUid = nil
	} else {
		servers, err := client.Servers(stack.Uid)
		if err != nil {
			printFatal(err.Error())
		}
		server, err := findServer(servers, flagServer)
		if err != nil {
			printFatal(err.Error())
		}
		if server == nil {
			printFatal("Server '" + flagServer + "' not found")
		}
		if !server.HasRole("docker") {
			printFatal("Server '" + flagServer + "' can not host containers")
		}
		fmt.Printf("Server: %s\n", server.Name)
		serverUid = &server.Uid
	}

	asyncId, err := startServiceStop(stack.Uid, serviceName, serverUid)
	if err != nil {
		printFatal(err.Error())
	}
	genericRes, err := endServiceStop(*asyncId, stack.Uid)
	if err != nil {
		printFatal(err.Error())
	}
	printGenericResponse(*genericRes)
	return
}
Esempio n. 10
0
func runServiceInfo(c *cli.Context) {
	if len(c.Args()) != 1 {
		cli.ShowSubcommandHelp(c)
		os.Exit(2)
	}

	w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
	defer w.Flush()

	stack := mustStack(c)
	serviceName := c.Args()[0]

	flagServer := c.String("server")

	var serverUid *string
	if flagServer == "" {
		serverUid = nil
	} else {
		servers, err := client.Servers(stack.Uid)
		if err != nil {
			printFatal(err.Error())
		}
		server, err := findServer(servers, flagServer)
		if err != nil {
			printFatal(err.Error())
		}
		if server == nil {
			printFatal("Server '" + flagServer + "' not found")
		}
		if !server.HasRole("docker") {
			printFatal("Server '" + flagServer + "' is not a docker server")
		}
		fmt.Printf("Server: %s\n", server.Name)
		serverUid = &server.Uid
	}

	service, err := client.GetService(stack.Uid, serviceName, serverUid, nil)
	must(err)

	printServiceInfoList(w, service)
	return
}
Esempio n. 11
0
func runSlaveResync(c *cli.Context) {
	stack := mustStack(c)

	if len(c.Args()) < 1 {
		cli.ShowSubcommandHelp(c)
		os.Exit(2)
	}

	// get the server
	serverName := c.Args()[0]
	flagDbType := c.String("dbtype")

	servers, err := client.Servers(stack.Uid)
	if err != nil {
		printFatal(err.Error())
	}

	server, err := findServer(servers, serverName)
	if err != nil {
		printFatal(err.Error())
	}

	if server == nil {
		printFatal("Server '" + serverName + "' not found")
	}

	fmt.Printf("Server: %s\n", server.Name)
	currenttime := time.Now().Local()
	fmt.Printf("Started: %s\n", currenttime.Format("2006-01-02 15:04:05"))

	asyncId, err := startSlaveResync(stack.Uid, server.Uid, &flagDbType)
	if err != nil {
		printFatal(err.Error())
	}
	genericRes, err := endSlaveResync(*asyncId, stack.Uid)
	if err != nil {
		printFatal(err.Error())
	}
	printGenericResponse(*genericRes)
}
Esempio n. 12
0
func runServiceScale(c *cli.Context) {
	if len(c.Args()) != 2 {
		cli.ShowSubcommandHelp(c)
		os.Exit(2)
	}

	stack := mustStack(c)
	serviceName := c.Args()[0]
	count := c.Args()[1]

	flagServer := c.String("server")
	flagGroup := c.String("group")

	// fetch servers info
	servers, err := client.Servers(stack.Uid)
	must(err)

	if flagServer != "" {
		server, err := findServer(servers, flagServer)
		must(err)
		if server == nil {
			printFatal("Server '" + flagServer + "' not found")
		}
		if !server.HasRole("docker") {
			printFatal("Server '" + flagServer + "' can not host containers")
		}
		fmt.Printf("Server: %s\n", server.Name)
		// filter servers collection down
		servers = make([]cloud66.Server, 1)
		servers[0] = *server
	}

	if flagGroup != "" {
		if flagGroup != "web" {
			printFatal("Only web group is supported at the moment")
		}
	}

	// param for api call
	serverCountDesired := make(map[string]int)

	var absoluteCount int
	if strings.ContainsAny(count, "+ & -") {

		// fetch service information for existing counts
		service, err := client.GetService(stack.Uid, serviceName, nil, nil)
		must(err)

		serverCountCurrent := service.ServerContainerCountMap()
		relativeCount, _ := strconv.Atoi(count)

		for _, server := range servers {
			if _, present := serverCountCurrent[server.Name]; present {
				serverCountDesired[server.Uid] = relativeCount + serverCountCurrent[server.Name]
			} else {
				serverCountDesired[server.Uid] = relativeCount
			}
		}

	} else {
		absoluteCount, _ = strconv.Atoi(count)
		for _, server := range servers {
			serverCountDesired[server.Uid] = absoluteCount
		}
	}

	// validate non < 0
	for serverUid, count := range serverCountDesired {
		if count < 0 {
			serverCountDesired[serverUid] = 0
		}
	}

	fmt.Println("Scaling your '" + serviceName + "' service")

	var asyncId *int
	if flagGroup != "" {
		var groupMap = make(map[string]int)
		groupMap["web"] = absoluteCount
		asyncId, err = startServiceScaleByGroup(stack.Uid, serviceName, groupMap)
	} else {
		asyncId, err = startServiceScale(stack.Uid, serviceName, serverCountDesired)
	}

	must(err)
	genericRes, err := endServiceScale(*asyncId, stack.Uid)
	must(err)
	printGenericResponse(*genericRes)
	return
}