コード例 #1
0
ファイル: main.go プロジェクト: digipost/cloud-tools
func main() {

	if len(os.Args) != 2 {
		fmt.Printf("Restart VPC\n")
		fmt.Printf("Usage: cloudstack-vpc-restart <vpcname>\n")
		os.Exit(1)
	}

	vpcName := os.Args[1]

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

	vpcService := cloudstack.NewVPCService(client)

	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 {
		if _, err := vpcService.RestartVPC(vpcService.NewRestartVPCParams(vpcId)); err != nil {
			fmt.Printf("Failed to restart VPC \"%s\": %s\n", vpcName, err.Error())
		} else {
			fmt.Printf("Restarting VPC \"%s\"... ", vpcName)
		}

	}

}
コード例 #2
0
ファイル: main.go プロジェクト: digipost/cloud-tools
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)
			}
		}

	}

}
コード例 #3
0
ファイル: main.go プロジェクト: digipost/cloud-tools
// Utility to enable remote access VPN on an
// Apache Cloudstack VPC
//
// If 10.x.0.0/16 is the CIDR of your VPC,
// the vpn concentrator will distribute IPs in
// Will give you an IP in the 10.(x+100).0.0 CIDR
//
// To enable routing to this network:
//
// sudo route add 10.x.0.0/16 10.(x+100).0.1
//
func main() {

	if len(os.Args) < 2 {
		fmt.Printf("Enable remote VPN access on VPC\n")
		fmt.Printf("Usage: cloudstack-vpn <vpcname> [<PASSWORD_STORE_DIR>]\n")
		os.Exit(1)
	}

	vpcNameProvided := os.Args[1]

	apiurl, apikey, secret := config.CloudstackClientConfig()

	client := cloudstack.NewClient(apiurl, apikey, secret, true)
	asyncClient := cloudstack.NewAsyncClient(apiurl, apikey, secret, true)

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

		fmt.Printf("VPC id %s found for vpc name \"%s\"\n", vpcId, vpcName)

		if ipAddressId, err := findPublicIPAddressForVPC(client, vpcId); err != nil {
			fmt.Printf("Failed to find public IP address for VPC id %s: %s", vpcId, err.Error())
		} else {

			var vpn *cloudstack.RemoteAccessVpn

			if vpnExisting, err := findRemoteAccessVPN(client, ipAddressId); err != nil {
				fmt.Printf("Failed to find remote access VPN: %s", err.Error())
			} else if vpnExisting == nil {

				fmt.Printf("Remote Access VPN not enabled for VPC, creating new one\n")

				if vpnAddressRange, err := findVPNAddressRange(client, vpcId); err != nil {
					fmt.Printf("Failed to find cidr range: %s\n", err)
				} else {
					fmt.Printf("VPN address range %s \n", vpnAddressRange)

					if vpnCreated, err := createRemoteAccessVPN(asyncClient, ipAddressId, vpnAddressRange); err != nil {
						fmt.Printf("Failed to create new remote access VPN: %s", err.Error())
					} else {
						vpn = vpnCreated
					}

				}

			} else {
				vpn = vpnExisting
			}

			if vpn != nil {

				fmt.Printf("VPN connection details for VPC \"%s\":\n", vpcName)
				fmt.Printf("IP address: %s\n", vpn.Publicip)
				fmt.Printf("Preshared secret: %s\n", vpn.Presharedkey)

				if len(os.Args) == 3 {
					fmt.Printf("Saving preshared secret to password store in : %s\n", os.Args[2])
					config.InsertPasswordFor(os.Args[2], fmt.Sprintf("shared/%s", vpcName), vpn.Presharedkey)
				}
			}

		}

	}

}