Example #1
0
func doFirewallCreateModify(c *cli.Context) {
	method := "PUT"
	if c.Command.Name == "fwcreate" {
		method = "POST"
	}

	if len(c.Args()) == 0 {
		displayWrongNumOfArgsAndExit(c)
	}
	vename := c.Args().Get(0)

	var fw lib.Firewall
	if len(c.String("setting-file")) > 0 {
		assert(lib.LoadConfig(c.String("setting-file"), &fw))
	} else {
		if s, ok := conf.Servers[vename]; ok && len(s.Firewall.Rule) > 0 {
			fw = s.Firewall
		} else {
			displayErrorAndExit("Couldn't find Firewall rules for '" + vename + "'")
		}
	}

	var b bytes.Buffer
	assert(xml.NewEncoder(&b).Encode(fw))

	resp, err := client.SendRequest(method, "/ve/"+vename+"/firewall", &b)
	assert(err)

	switch resp.StatusCode {
	case 200:
		fmt.Println(string(resp.Body))
	default:
		displayErrorAndExit(string(resp.Body))
	}
}
Example #2
0
func action(c *cli.Context, fn func(c *cli.Context)) {
	if len(c.String("config")) > 0 {
		err := lib.LoadConfig(c.String("config"), &conf)
		assert(err)
		if len(conf.BaseURL) == 0 || len(conf.Username) == 0 || len(conf.Password) == 0 {
			displayErrorAndExit("Invalid config data. BaseURL, Username and Password must be correctly specified in a config file")
		}
		client = lib.NewClient(conf.BaseURL, conf.Username, conf.Password)
		prettytable.Separator = columnSeparator
		fn(c)
	} else {
		displayErrorAndExit("Config path is empty. It must be specified to use this command.\nPlease see '" + c.App.Name + " help' result")
	}
}
Example #3
0
func doCreate(c *cli.Context) {
	if len(c.Args()) == 0 {
		displayWrongNumOfArgsAndExit(c)
	}
	vename := c.Args().Get(0)

	var ve lib.CreateVe
	if len(c.String("setting-file")) > 0 {
		assert(lib.LoadConfig(c.String("setting-file"), &ve))
		ve.Name = vename
	} else {
		if s, ok := conf.Servers[vename]; ok && s.Spec != nil {
			ve = *s.Spec
		} else {
			cli.ShowCommandHelp(c, c.Command.Name)
			os.Exit(1)
		}
	}

	if len(ve.Hostname) == 0 {
		ve.Hostname = ve.Name
	}

	var b bytes.Buffer
	assert(xml.NewEncoder(&b).Encode(ve))

	resp, err := client.SendRequest("POST", "/ve/", &b)
	assert(err)

	if resp.StatusCode >= 400 {
		displayErrorAndExit(string(resp.Body))
	}

	pwd := lib.PasswordResponse{}
	assert(xml.Unmarshal(resp.Body, &pwd))

	outputResult(c, pwd, func(format string) {
		lib.PrintXMLStruct(pwd)
	})
}
Example #4
0
func doAutoscaleCreateUpdate(c *cli.Context) {
	method := "PUT"
	if c.Command.Name == "autoscale-create" {
		method = "POST"
	}

	if len(c.Args()) == 0 {
		displayWrongNumOfArgsAndExit(c)
	}
	vename := c.Args().Get(0)

	var data lib.AutoscaleData
	if len(c.String("setting-file")) > 0 {
		assert(lib.LoadConfig(c.String("setting-file"), &data))
	} else {
		if s, ok := conf.Servers[vename]; ok && len(s.AutoscaleRule) > 0 {
			data = lib.AutoscaleData{AutoscaleRule: s.AutoscaleRule}
		} else {
			displayErrorAndExit("Couldn't find Autoscale rules for '" + vename + "'")
		}
	}
	var b bytes.Buffer
	assert(xml.NewEncoder(&b).Encode(data))

	resp, err := client.SendRequest(method, "/ve/"+vename+"/autoscale", &b)
	assert(err)

	if resp.StatusCode != 200 {
		displayErrorAndExit(string(resp.Body))
	}

	autoscale := lib.Autoscale{}
	assert(xml.Unmarshal(resp.Body, &autoscale))

	outputResult(c, autoscale, func(format string) {
		lib.PrintXMLStruct(autoscale)
	})
}
Example #5
0
func doModify(c *cli.Context) {
	if len(c.Args()) == 0 {
		displayWrongNumOfArgsAndExit(c)
	}
	vename := c.Args().Get(0)

	var ve lib.ReconfigureVe
	if len(c.String("setting-file")) > 0 {
		assert(lib.LoadConfig(c.String("setting-file"), &ve))
	}
	if len(c.String("description")) > 0 {
		ve.Description = c.String("description")
	}
	if c.Int("cpus") > 0 {
		if ve.ChangeCPU == nil {
			ve.ChangeCPU = new(lib.ChangeCPU)
		}
		ve.ChangeCPU.Number = c.Int("cpus")
	}
	if c.Int("cpu-power") > 0 {
		if ve.ChangeCPU == nil {
			ve.ChangeCPU = new(lib.ChangeCPU)
		}
		ve.ChangeCPU.Power = c.Int("cpu-power")
	}
	if c.Int("ram-size") > 0 {
		ve.RAMSize = c.Int("ram-size")
	}
	if c.Int("bandwidth") > 0 {
		ve.Bandwidth = c.Int("bandwidth")
	}
	if c.Int("add-ipv4") > 0 {
		if ve.ReconfigureIPv4 == nil {
			ve.ReconfigureIPv4 = new(lib.ReconfigureIP)
		}
		if ve.ReconfigureIPv4.AddIP == nil {
			ve.ReconfigureIPv4.AddIP = new(lib.AddIP)
		}
		ve.ReconfigureIPv4.AddIP.Number = c.Int("add-ipv4")
	}
	if len(c.StringSlice("drop-ipv4")) > 0 {
		if ve.ReconfigureIPv4 == nil {
			ve.ReconfigureIPv4 = new(lib.ReconfigureIP)
		}
		if ve.ReconfigureIPv4.DropIP == nil {
			ve.ReconfigureIPv4.DropIP = new(lib.DropIP)
		}
		for _, addr := range c.StringSlice("drop-ipv4") {
			a, err := lib.NewIPAddr(addr)
			assert(err)
			ve.ReconfigureIPv4.DropIP.IP = append(ve.ReconfigureIPv4.DropIP.IP, *a)
		}
	}
	if c.Int("add-ipv6") > 0 {
		if ve.ReconfigureIPv4 == nil {
			ve.ReconfigureIPv6 = new(lib.ReconfigureIP)
		}
		if ve.ReconfigureIPv6.AddIP == nil {
			ve.ReconfigureIPv6.AddIP = new(lib.AddIP)
		}
		ve.ReconfigureIPv6.AddIP.Number = c.Int("add-ipv6")
	}
	if len(c.StringSlice("drop-ipv6")) > 0 {
		if ve.ReconfigureIPv6 == nil {
			ve.ReconfigureIPv6 = new(lib.ReconfigureIP)
		}
		if ve.ReconfigureIPv6.DropIP == nil {
			ve.ReconfigureIPv6.DropIP = new(lib.DropIP)
		}
		for _, addr := range c.StringSlice("drop-ipv6") {
			a, err := lib.NewIPAddr(addr)
			assert(err)
			ve.ReconfigureIPv6.DropIP.IP = append(ve.ReconfigureIPv6.DropIP.IP, *a)
		}
	}
	if c.Int("disk-size") > 0 {
		ve.PrimaryDiskSize = c.Int("disk-size")
	}
	if c.Bool("custom-ns") {
		if ve.CustomNs == nil {
			ve.CustomNs = new(int)
		}
		*(ve.CustomNs) = 1
	}
	if c.Bool("no-custom-ns") {
		if ve.CustomNs == nil {
			ve.CustomNs = new(int)
		}
		*(ve.CustomNs) = 0
	}

	if ve == (lib.ReconfigureVe{}) {
		displayErrorAndExit("There is no modification parameter. Please see '" + c.App.Name + " help " + c.Command.Name)
	}

	if ve.ReconfigureIPv4 != nil && ve.ReconfigureIPv4.AddIP != nil && ve.ReconfigureIPv4.DropIP != nil {
		displayErrorAndExit("Invalid modification setting. Both ReconfigureIPv4.AddIP and DropIP can't be specified at same time")
	}
	if ve.ReconfigureIPv6 != nil && ve.ReconfigureIPv6.AddIP != nil && ve.ReconfigureIPv6.DropIP != nil {
		displayErrorAndExit("Invalid modification setting. Both ReconfigureIPv6.AddIP and DropIP can't be specified at same time")
	}

	var b bytes.Buffer
	assert(xml.NewEncoder(&b).Encode(ve))

	resp, err := client.SendRequest("PUT", "/ve/"+vename, &b)
	assert(err)
	switch resp.StatusCode {
	case 202:
		fmt.Println(vename, string(resp.Body))
	default:
		displayErrorAndExit(string(resp.Body))
	}
}