Пример #1
0
func main() {

	if len(os.Args) < 2 {
		fmt.Printf("Stop VM\n")
		fmt.Printf("Usage:cloudstack-stop-vm <vm resource id> [-f] \n")
		fmt.Printf("Example: cloudstack-stop-vm cloudstack_instance.db.1\n")
		os.Exit(1)
	}

	resourceId := os.Args[1]

	// Should we force stop
	forced := false
	if len(os.Args) == 3 {

		if os.Args[2] == "-f" {
			fmt.Printf("Will try to forcefully stop vm\n")
			forced = true
		} else {
			fmt.Printf("Unrecognized second parameter. Should be -f\n")
			os.Exit(1)
		}

	}

	// Read Terraform state
	if state, err := terraform.ReadState("terraform.tfstate"); err != nil {
		fmt.Printf("Unable to read terraform.tfstate: %s", err.Error())
		os.Exit(1)
	} else {
		if resourceState, ok := state.Modules[0].Resources[resourceId]; ok {

			vmId := resourceState.Primary.Attributes["id"]
			apiurl, apikey, secret := config.CloudstackClientConfig()
			client := cloudstack.NewAsyncClient(apiurl, apikey, secret, true)

			vmService := cloudstack.NewVirtualMachineService(client)
			params := vmService.NewStopVirtualMachineParams(vmId)
			params.SetForced(forced)

			fmt.Printf("Stopping vm with id %s\n", vmId)
			if res, err := vmService.StopVirtualMachine(params); err != nil {
				fmt.Printf("Unable to stop vm: %s", err.Error())
				os.Exit(1)
			} else {
				fmt.Printf("Stopped vm with id %s\n", vmId)
				fmt.Printf("State is %s\n", res.State)
				os.Exit(0)
			}
		} else {
			fmt.Println("Instance id not found")
			os.Exit(1)
		}

	}

}
Пример #2
0
func main() {

	if len(os.Args) != 2 {
		fmt.Printf("Start VM disk is attached to\n")
		fmt.Printf("Usage:cloudstack-start-vm-for-disk <disk resource id>\n")
		fmt.Printf("Example: cloudstack-start-vm-for-disk cloudstack_disk.db.1\n")
		os.Exit(1)
	}

	diskResourceId := os.Args[1]

	// Read Terraform state
	if state, err := terraform.ReadState("terraform.tfstate"); err != nil {
		fmt.Printf("Unable to read terraform.tfstate: %s", err.Error())
		os.Exit(1)
	} else {

		if resourceState, ok := state.Modules[0].Resources[diskResourceId]; ok {

			vmId := resourceState.Primary.Attributes["virtual_machine_id"]

			apiurl, apikey, secret := config.CloudstackClientConfig()
			client := cloudstack.NewAsyncClient(apiurl, apikey, secret, true)

			vmService := cloudstack.NewVirtualMachineService(client)
			params := vmService.NewStartVirtualMachineParams(vmId)

			fmt.Printf("Starting vm with id %s\n", vmId)
			if res, err := vmService.StartVirtualMachine(params); err != nil {
				fmt.Printf("Unable to start vm: %s", err.Error())
				os.Exit(1)
			} else {
				fmt.Printf("Started vm with id %s\n", vmId)
				fmt.Printf("State is %s\n", res.State)
				os.Exit(0)
			}

		} else {
			fmt.Println("Disk id not found")
			os.Exit(1)
		}

	}

}
Пример #3
0
func main() {

	if len(os.Args) != 2 {
		fmt.Printf("Stop VMs in VPC\n")
		fmt.Printf("Usage: cloudstack-vm-stop <vpcname>\n")
		os.Exit(1)
	}

	vpcName := os.Args[1]

	apiurl, apikey, secret := config.CloudstackClientConfig()
	client := cloudstack.NewClient(apiurl, apikey, secret, true)

	if vpcId, vpcName, err := cloudstackutils.FindVpcId(client, vpcName); err != nil {
		fmt.Printf("Failed to find id for VPC \"%s\": %s\n", vpcName, err.Error())
	} else {

		vmService := cloudstack.NewVirtualMachineService(client)
		virtualMachinesParams := vmService.NewListVirtualMachinesParams()
		virtualMachinesParams.SetVpcid(vpcId)
		virtualMachinesParams.SetState("Running")

		if vms, err := vmService.ListVirtualMachines(virtualMachinesParams); err != nil {
			fmt.Printf("Failed to list VMs in VPC \"%s\": %s\n", vpcName, err.Error())
		} else {

			if vms.Count > 0 {

				for _, vm := range vms.VirtualMachines {
					fmt.Printf("Stopping VM %s / %s \n", vm.Displayname, vm.Name)
					if _, err := vmService.StopVirtualMachine(vmService.NewStopVirtualMachineParams(vm.Id)); err != nil {
						fmt.Printf("Failed to stop VM with id %s: %s\n", vm.Id, err.Error())
					} else {
						fmt.Printf("VM with id %s stopped\n", vm.Id)
					}
				}
			} else {
				fmt.Printf("No running VMs in %s\n", vpcName)
			}
		}

	}

}