Пример #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("Taint cloudstack instance safely\n")
		fmt.Printf("Will taint nic\n")
		fmt.Printf("Usage: terraform-taint-cloudstack-instance-and-nic <instance resource id>\n")
		fmt.Printf("Example: terraform-taint-cloudstack-instance-and-nic cloudstack_instance.db.1\n")
		os.Exit(1)
	}

	cloudstackInstanceId := 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[cloudstackInstanceId]; ok {

			vmName := resourceState.Primary.Attributes["id"]
			taint(cloudstackInstanceId)

			for resourceId, resource := range state.Modules[0].Resources {
				if resource.Type == "cloudstack_nic" && resource.Primary.Attributes["virtual_machine_id"] == vmName {
					taint(resourceId)
				}
			}

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

}