Ejemplo n.º 1
0
// Finishes the copy state of source system into destination and makes this system the active one
func deploymentMigrationFinalizeDeprecated(c *cli.Context) error {
	fmt.Printf("'%d'", len(c.Args()))
	err := checkArgNum(c.Args(), 1, "system migration finalize <old_management_endpoint>")
	if err != nil {
		return err
	}
	sourceAddress := c.Args().First()
	client.Esxclient, err = client.GetClient(c.GlobalIsSet("non-interactive"))
	if err != nil {
		return err
	}
	deployments, err := client.Esxclient.Deployments.GetAll()
	if err != nil {
		return err
	}
	finalizeMigrationSpec := photon.FinalizeMigrationOperation{}
	finalizeMigrationSpec.SourceLoadBalancerAddress = sourceAddress

	// Finalize deployment migration
	for _, deployment := range deployments.Items {
		finalizeMigrate, err := client.Esxclient.Deployments.FinalizeDeploymentMigration(&finalizeMigrationSpec, deployment.ID)
		if err != nil {
			return err
		}
		_, err = pollTask(finalizeMigrate.ID)
		if err != nil {
			return err
		}

		return nil
	}

	return nil
}
Ejemplo n.º 2
0
// Add most hosts in batch mode
func addHosts(c *cli.Context) error {
	err := checkArgNum(c.Args(), 1, "system addHosts <file>")
	if err != nil {
		return err
	}
	file := c.Args().First()
	dcMap, err := manifest.LoadInstallation(file)
	if err != nil {
		return err
	}

	client.Esxclient, err = client.GetClient(false)
	if err != nil {
		return err
	}

	deployments, err := client.Esxclient.Deployments.GetAll()
	deploymentID := deployments.Items[0].ID

	// Create Hosts
	err = createHostsInBatch(dcMap, deploymentID)
	if err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 3
0
// Starts the recurring copy state of source system into destination
func deploymentMigrationPrepareDeprecated(c *cli.Context) error {
	err := checkArgNum(c.Args(), 1, "system migration prepare <old_management_endpoint>")
	if err != nil {
		return err
	}
	sourceAddress := c.Args().First()
	client.Esxclient, err = client.GetClient(c.GlobalIsSet("non-interactive"))
	if err != nil {
		return err
	}
	deployments, err := client.Esxclient.Deployments.GetAll()
	if err != nil {
		return err
	}
	initializeMigrationSpec := photon.InitializeMigrationOperation{}
	initializeMigrationSpec.SourceLoadBalancerAddress = sourceAddress

	// Initialize deployment migration
	for _, deployment := range deployments.Items {
		initializeMigrate, err := client.Esxclient.Deployments.InitializeDeploymentMigration(&initializeMigrationSpec, deployment.ID)
		if err != nil {
			return err
		}
		_, err = pollTask(initializeMigrate.ID)
		if err != nil {
			return err
		}
		fmt.Printf("Deployment '%s' migration started [source management endpoint: '%s'].\n", deployment.ID, sourceAddress)

		return nil
	}

	return nil
}
Ejemplo n.º 4
0
// Retrieves a list of tasks, returns an error if one occurred
func listTasks(c *cli.Context) error {
	err := checkArgNum(c.Args(), 0, "task list <options>")
	if err != nil {
		return err
	}
	entityId := c.String("entityId")
	entityKind := c.String("entityKind")
	state := c.String("state")

	client.Esxclient, err = client.GetClient(c.GlobalIsSet("non-interactive"))
	if err != nil {
		return err
	}

	options := &photon.TaskGetOptions{
		State:      state,
		EntityID:   entityId,
		EntityKind: entityKind,
	}
	taskList, err := client.Esxclient.Tasks.GetAll(options)
	if err != nil {
		return err
	}

	err = printTaskList(taskList.Items, c)
	if err != nil {
		return err
	}
	return nil
}
Ejemplo n.º 5
0
// Deploy Photon Controller based on DC_map
func deploy(c *cli.Context) error {
	err := checkArgNum(c.Args(), 1, "system deploy <file>")
	if err != nil {
		return err
	}
	file := c.Args().First()
	dcMap, err := manifest.LoadInstallation(file)
	if err != nil {
		return err
	}

	client.Esxclient, err = client.GetClient(false)
	if err != nil {
		return err
	}

	deploymentID, err := createDeploymentFromDcMap(dcMap)
	if err != nil {
		return err
	}

	// Create Hosts
	err = createHostsFromDcMap(dcMap, deploymentID)
	if err != nil {
		return err
	}

	// Deploy
	err = doDeploy(dcMap, deploymentID)
	if err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 6
0
// Track the progress of the task, returns an error if one occurred
func monitorTask(c *cli.Context) error {
	err := checkArgNum(c.Args(), 1, "task monitor <task id>")
	if err != nil {
		return err
	}
	id := c.Args()[0]

	client.Esxclient, err = client.GetClient(c.GlobalIsSet("non-interactive"))
	if err != nil {
		return err
	}

	if c.GlobalIsSet("non-interactive") {
		task, err := client.Esxclient.Tasks.Wait(id)
		if err != nil {
			return err
		}
		fmt.Printf("%s\t%s\t%s\t%s\n", task.ID, task.State, task.Entity.ID, task.Entity.Kind)
	} else {
		task, err := pollTask(id)
		if err != nil {
			return err
		}
		w := new(tabwriter.Writer)
		w.Init(os.Stdout, 4, 4, 2, ' ', 0)
		fmt.Fprintf(w, "Task:\t%s\n", task.ID)
		fmt.Fprintf(w, "Entity:\t%s %s\n", task.Entity.Kind, task.Entity.ID)
		fmt.Fprintf(w, "State:\t%s\n", task.State)
		err = w.Flush()
		if err != nil {
			return err
		}
	}
	return nil
}
Ejemplo n.º 7
0
// Set host's availability zone with the specified host ID, returns an error if one occurred
func setHostAvailabilityZone(c *cli.Context, w io.Writer) error {
	err := checkArgNum(c.Args(), 2, "host set-availability-zone <id> <availability-zone-id>")
	if err != nil {
		return err
	}
	id := c.Args().First()
	availabilityZoneId := c.Args()[1]

	client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c))
	if err != nil {
		return err
	}

	setAvailabilityZoneSpec := photon.HostSetAvailabilityZoneOperation{}
	setAvailabilityZoneSpec.AvailabilityZoneId = availabilityZoneId
	setTask, err := client.Esxclient.Hosts.SetAvailabilityZone(id, &setAvailabilityZoneSpec)
	if err != nil {
		return err
	}
	id, err = waitOnTaskOperation(setTask.ID, c)
	if err != nil {
		return err
	}
	if utils.NeedsFormatting(c) {
		host, err := client.Esxclient.Hosts.Get(id)
		if err != nil {
			return err
		}
		utils.FormatObject(host, w, c)
	}

	return nil
}
Ejemplo n.º 8
0
// List all the hosts in the current deployment
// This uses the same back-end code as "deployments list-hosts", but we look up the
// deployment ID so that users don't have to specify it. In most or all installations,
// there will not be more than one deployment ID.
func listHosts(c *cli.Context, w io.Writer) error {
	err := checkArgNum(c.Args(), 0, "host list")
	if err != nil {
		return err
	}

	client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c))
	if err != nil {
		return err
	}

	// Find the current deployment
	deployments, err := client.Esxclient.Deployments.GetAll()
	if err != nil {
		return err
	}
	numDeployments := len(deployments.Items)
	if numDeployments == 0 {
		return fmt.Errorf("There are no deployments, so the hosts cannot be listed.")
	} else if numDeployments > 1 {
		return fmt.Errorf("There are multiple deployments, which normally should not happen. Use deployments list-hosts.")
	}
	id := deployments.Items[0].ID

	hosts, err := client.Esxclient.Deployments.GetHosts(id)
	if err != nil {
		return err
	}

	err = printHostList(hosts.Items, w, c)
	if err != nil {
		return err
	}
	return nil
}
Ejemplo n.º 9
0
// displays the migration status
func showMigrationStatusDeprecated(c *cli.Context) error {
	err := checkArgNum(c.Args(), 0, "migration status")
	if err != nil {
		return err
	}
	client.Esxclient, err = client.GetClient(c.GlobalIsSet("non-interactive"))
	if err != nil {
		return err
	}
	deployments, err := client.Esxclient.Deployments.GetAll()
	if err != nil {
		return err
	}

	for _, deployment := range deployments.Items {
		if deployment.Migration != nil {
			migration := deployment.Migration
			if c.GlobalIsSet("non-interactive") {
				fmt.Printf("%d\t%d\t%d\t%d\t%d\n", migration.CompletedDataMigrationCycles, migration.DataMigrationCycleProgress,
					migration.DataMigrationCycleSize, migration.VibsUploaded, migration.VibsUploading+migration.VibsUploaded)
			} else {
				fmt.Printf("  Migration status:\n")
				fmt.Printf("    Completed data migration cycles:          %d\n", migration.CompletedDataMigrationCycles)
				fmt.Printf("    Current data migration cycles progress:   %d / %d\n", migration.DataMigrationCycleProgress,
					migration.DataMigrationCycleSize)
				fmt.Printf("    VIB upload progress:                      %d / %d\n", migration.VibsUploaded, migration.VibsUploading+migration.VibsUploaded)
			}
		}
		return nil
	}
	return nil
}
Ejemplo n.º 10
0
func showNetwork(c *cli.Context, w io.Writer) error {
	err := checkArgNum(c.Args(), 1, "network show <id>")
	if err != nil {
		return err
	}
	id := c.Args().First()

	client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c))
	if err != nil {
		return err
	}

	network, err := client.Esxclient.Subnets.Get(id)
	if err != nil {
		return err
	}

	if c.GlobalIsSet("non-interactive") {
		portGroups := getCommaSeparatedStringFromStringArray(network.PortGroups)
		fmt.Printf("%s\t%s\t%s\t%s\t%s\t%t\n", network.ID, network.Name, network.State, portGroups,
			network.Description, network.IsDefault)
	} else if utils.NeedsFormatting(c) {
		utils.FormatObject(network, w, c)
	} else {
		fmt.Printf("Network ID: %s\n", network.ID)
		fmt.Printf("  Name:        %s\n", network.Name)
		fmt.Printf("  State:       %s\n", network.State)
		fmt.Printf("  Description: %s\n", network.Description)
		fmt.Printf("  Port Groups: %s\n", network.PortGroups)
		fmt.Printf("  Is Default: %t\n", network.IsDefault)
	}

	return nil
}
Ejemplo n.º 11
0
// Retrieves tasks from specified flavor
func getFlavorTasks(c *cli.Context, w io.Writer) error {
	err := checkArgNum(c.Args(), 1, "flavor tasks <id> [<options>]")
	if err != nil {
		return err
	}
	id := c.Args().First()

	state := c.String("state")
	options := &photon.TaskGetOptions{
		State: state,
	}

	client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c))
	if err != nil {
		return err
	}

	taskList, err := client.Esxclient.Flavors.GetTasks(id, options)
	if err != nil {
		return err
	}

	err = printTaskList(taskList.Items, c)
	if err != nil {
		return err
	}
	return nil
}
Ejemplo n.º 12
0
func detachDisk(c *cli.Context) error {
	err := checkArgNum(c.Args(), 1, "vm detach-disk --disk <id> <vm-id>")
	if err != nil {
		return err
	}

	id := c.Args().First()
	diskID := c.String("disk")

	client.Esxclient, err = client.GetClient(c.GlobalIsSet("non-interactive"))
	if err != nil {
		return err
	}

	operation := &photon.VmDiskOperation{
		DiskID: diskID,
	}

	task, err := client.Esxclient.VMs.DetachDisk(id, operation)
	if err != nil {
		return err
	}

	_, err = waitOnTaskOperation(task.ID, c)
	if err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 13
0
// displays the migration status
func showMigrationStatus(c *cli.Context) error {
	id, err := getDeploymentId(c)
	if err != nil {
		return err
	}

	client.Esxclient, err = client.GetClient(c.GlobalIsSet("non-interactive"))
	if err != nil {
		return err
	}

	deployment, err := client.Esxclient.Deployments.Get(id)
	if err != nil {
		return err
	}

	if deployment.Migration == nil {
		fmt.Print("No migration information available")
		return nil
	}

	migration := deployment.Migration
	if c.GlobalIsSet("non-interactive") {
		fmt.Printf("%d\t%d\t%d\t%d\t%d\n", migration.CompletedDataMigrationCycles, migration.DataMigrationCycleProgress,
			migration.DataMigrationCycleSize, migration.VibsUploaded, migration.VibsUploading+migration.VibsUploaded)
	} else {
		fmt.Printf("  Migration status:\n")
		fmt.Printf("    Completed data migration cycles:          %d\n", migration.CompletedDataMigrationCycles)
		fmt.Printf("    Current data migration cycles progress:   %d / %d\n", migration.DataMigrationCycleProgress,
			migration.DataMigrationCycleSize)
		fmt.Printf("    VIB upload progress:                      %d / %d\n", migration.VibsUploaded, migration.VibsUploading+migration.VibsUploaded)
	}

	return nil
}
Ejemplo n.º 14
0
// Retrieves tasks for VM
func getVMTasks(c *cli.Context) error {
	err := checkArgNum(c.Args(), 1, "vm tasks <id> [<options>]")
	if err != nil {
		return err
	}

	id := c.Args().First()
	state := c.String("state")

	client.Esxclient, err = client.GetClient(c.GlobalIsSet("non-interactive"))
	if err != nil {
		return err
	}

	options := &photon.TaskGetOptions{
		State: state,
	}

	taskList, err := client.Esxclient.VMs.GetTasks(id, options)
	if err != nil {
		return err
	}

	err = printTaskList(taskList.Items, c)
	if err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 15
0
func restartVM(c *cli.Context) error {
	err := checkArgNum(c.Args(), 1, "vm restart <id>")
	if err != nil {
		return err
	}

	id := c.Args().First()

	client.Esxclient, err = client.GetClient(c.GlobalIsSet("non-interactive"))
	if err != nil {
		return err
	}

	opTask, err := client.Esxclient.VMs.Restart(id)
	if err != nil {
		return err
	}

	_, err = waitOnTaskOperation(opTask.ID, c)
	if err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 16
0
// Sends a delete tenant task to client based on the cli.Context
// Returns an error if one occurred
func deleteTenant(c *cli.Context) error {
	err := checkArgNum(c.Args(), 1, "tenant delete <id>")
	if err != nil {
		return err
	}
	id := c.Args().First()

	client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c))
	if err != nil {
		return err
	}

	deleteTask, err := client.Esxclient.Tenants.Delete(id)
	if err != nil {
		return err
	}

	_, err = waitOnTaskOperation(deleteTask.ID, c)
	if err != nil {
		return err
	}

	err = clearConfigTenant(id)
	if err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 17
0
func getVMMksTicket(c *cli.Context) error {
	err := checkArgNum(c.Args(), 1, "vm mks-ticket <id>")
	if err != nil {
		return err
	}

	id := c.Args().First()

	client.Esxclient, err = client.GetClient(c.GlobalIsSet("non-interactive"))
	if err != nil {
		return err
	}

	task, err := client.Esxclient.VMs.GetMKSTicket(id)
	if err != nil {
		return err
	}

	if c.GlobalIsSet("non-interactive") {
		task, err := client.Esxclient.Tasks.Wait(task.ID)
		if err != nil {
			return err
		}
		mksTicket := task.ResourceProperties.(map[string]interface{})
		fmt.Printf("%s\t%v\n", task.Entity.ID, mksTicket["ticket"])
	} else {
		task, err = pollTask(task.ID)
		if err != nil {
			return err
		}
		mksTicket := task.ResourceProperties.(map[string]interface{})
		fmt.Printf("VM ID: %s \nMks ticket ID is %v\n", task.Entity.ID, mksTicket["ticket"])
	}
	return nil
}
Ejemplo n.º 18
0
// Set security groups for a tenant
func setSecurityGroups(c *cli.Context) error {
	err := checkArgNum(c.Args(), 2, "tenant set_security_groups <id> <comma-separated security group names>")
	if err != nil {
		return err
	}
	id := c.Args().First()
	items := []string{}
	if c.Args()[1] != "" {
		items = regexp.MustCompile(`\s*,\s*`).Split(c.Args()[1], -1)
	}
	securityGroups := &photon.SecurityGroupsSpec{
		Items: items,
	}

	client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c))
	if err != nil {
		return err
	}

	task, err := client.Esxclient.Tenants.SetSecurityGroups(id, securityGroups)
	if err != nil {
		return err
	}

	_, err = waitOnTaskOperation(task.ID, c)
	if err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 19
0
func setDefaultNetwork(c *cli.Context, w io.Writer) error {
	err := checkArgNum(c.Args(), 1, "network set-default <id>")
	if err != nil {
		return err
	}
	id := c.Args().First()

	client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c))
	if err != nil {
		return err
	}

	task, err := client.Esxclient.Subnets.SetDefault(id)
	if err != nil {
		return err
	}

	if confirmed(utils.IsNonInteractive(c)) {
		id, err := waitOnTaskOperation(task.ID, c)
		if err != nil {
			return err
		}

		if utils.NeedsFormatting(c) {
			network, err := client.Esxclient.Subnets.Get(id)
			if err != nil {
				return err
			}
			utils.FormatObject(network, w, c)
		}
	} else {
		fmt.Println("OK. Canceled")
	}
	return nil
}
// Retrieves availability zone against specified id.
func showAvailabilityZone(c *cli.Context, w io.Writer) error {
	err := checkArgNum(c.Args(), 1, "availability-zone show <id>")
	if err != nil {
		return err
	}
	id := c.Args().First()

	client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c))
	if err != nil {
		return err
	}

	zone, err := client.Esxclient.AvailabilityZones.Get(id)
	if err != nil {
		return err
	}

	if c.GlobalIsSet("non-interactive") {
		fmt.Printf("%s\t%s\t%s\t%s\n", zone.ID, zone.Name, zone.Kind, zone.State)
	} else if utils.NeedsFormatting(c) {
		utils.FormatObject(zone, w, c)
	} else {
		fmt.Printf("AvailabilityZone ID: %s\n", zone.ID)
		fmt.Printf("  Name:        %s\n", zone.Name)
		fmt.Printf("  Kind:        %s\n", zone.Kind)
		fmt.Printf("  State:       %s\n", zone.State)
	}

	return nil
}
Ejemplo n.º 21
0
func deleteNetwork(c *cli.Context) error {
	err := checkArgNum(c.Args(), 1, "network delete <id>")
	if err != nil {
		return err
	}
	id := c.Args().First()

	client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c))
	if err != nil {
		return err
	}

	task, err := client.Esxclient.Subnets.Delete(id)
	if err != nil {
		return err
	}

	if confirmed(c.GlobalIsSet("non-interactive")) {
		_, err = waitOnTaskOperation(task.ID, c)
		if err != nil {
			return err
		}
	} else {
		fmt.Println("OK. Canceled")
	}
	return nil
}
Ejemplo n.º 22
0
// Deletes an image by id
func deleteImage(c *cli.Context) error {
	err := checkArgNum(c.Args(), 1, "image delete <path>")
	if err != nil {
		return err
	}
	id := c.Args().First()

	if confirmed(utils.IsNonInteractive(c)) {
		client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c))
		if err != nil {
			return err
		}

		deleteTask, err := client.Esxclient.Images.Delete(id)
		if err != nil {
			return err
		}

		_, err = waitOnTaskOperation(deleteTask.ID, c)
		if err != nil {
			return err
		}
	} else {
		fmt.Println("OK, canceled")
	}

	return nil
}
Ejemplo n.º 23
0
func detachIso(c *cli.Context) error {
	err := checkArgNum(c.Args(), 1, "vm detach-iso <id>")
	if err != nil {
		return err
	}

	id := c.Args().First()

	client.Esxclient, err = client.GetClient(c.GlobalIsSet("non-interactive"))
	if err != nil {
		return err
	}

	task, err := client.Esxclient.VMs.DetachISO(id)
	if err != nil {
		return err
	}

	_, err = waitOnTaskOperation(task.ID, c)
	if err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 24
0
func setVMTag(c *cli.Context) error {
	err := checkArgNum(c.Args(), 1, "vm set-tag <id> [<options>]")
	if err != nil {
		return err
	}

	id := c.Args().First()

	tag := c.String("tag")
	vmTag := &photon.VmTag{}

	if len(tag) == 0 {
		return fmt.Errorf("Please input a tag")
	}
	vmTag.Tag = tag

	client.Esxclient, err = client.GetClient(c.GlobalIsSet("non-interactive"))
	if err != nil {
		return err
	}

	task, err := client.Esxclient.VMs.SetTag(id, vmTag)
	if err != nil {
		return err
	}

	_, err = waitOnTaskOperation(task.ID, c)
	if err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 25
0
// Sends a "resize cluster" request to the API client based on the cli.Context
// Returns an error if one occurred
func resizeCluster(c *cli.Context, w io.Writer) error {
	err := checkArgNum(c.Args(), 2, "cluster resize <id> <new worker count> [<options>]")
	if err != nil {
		return err
	}

	cluster_id := c.Args()[0]
	worker_count_string := c.Args()[1]
	worker_count, err := strconv.Atoi(worker_count_string)
	wait_for_ready := c.IsSet("wait-for-ready")

	if len(cluster_id) == 0 || err != nil || worker_count <= 0 {
		return fmt.Errorf("Provide a valid cluster ID and worker count")
	}

	client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c))
	if err != nil {
		return err
	}

	if !utils.IsNonInteractive(c) {
		fmt.Printf("\nResizing cluster %s to worker count %d\n", cluster_id, worker_count)
	}

	if confirmed(utils.IsNonInteractive(c)) {
		resizeSpec := photon.ClusterResizeOperation{}
		resizeSpec.NewWorkerCount = worker_count
		resizeTask, err := client.Esxclient.Clusters.Resize(cluster_id, &resizeSpec)
		if err != nil {
			return err
		}

		_, err = waitOnTaskOperation(resizeTask.ID, c)
		if err != nil {
			return err
		}

		if wait_for_ready {
			cluster, err := waitForCluster(cluster_id)
			if err != nil {
				return err
			}
			if utils.NeedsFormatting(c) {
				utils.FormatObject(cluster, w, c)
			} else {
				fmt.Printf("Cluster %s is ready\n", cluster.ID)
			}
		} else {
			fmt.Println("Note: A background task is running to gradually resize the cluster to its target capacity.")
			fmt.Printf("You may continue to use the cluster. You can run 'cluster show %s'\n", resizeTask.Entity.ID)
			fmt.Println("to see the state of the cluster. If the resize operation is still in progress, the cluster state")
			fmt.Println("will show as RESIZING. Once the cluster is resized, the cluster state will show as READY.")
		}
	} else {
		fmt.Println("Cancelled")
	}

	return nil
}
Ejemplo n.º 26
0
// Retrieves a list of projects, returns an error if one occurred
func listProjects(c *cli.Context, w io.Writer) error {
	err := checkArgNum(c.Args(), 0, "project list [<options>]")
	if err != nil {
		return err
	}
	tenantName := c.String("tenant")

	client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c))
	if err != nil {
		return err
	}

	tenant, err := verifyTenant(tenantName)
	if err != nil {
		return err
	}

	projects, err := client.Esxclient.Tenants.GetProjects(tenant.ID, nil)
	if err != nil {
		return err
	}

	if c.GlobalIsSet("non-interactive") {
		for _, t := range projects.Items {
			limits := quotaLineItemListToString(t.ResourceTicket.Limits)
			usage := quotaLineItemListToString(t.ResourceTicket.Usage)
			fmt.Printf("%s\t%s\t%s\t%s\n", t.ID, t.Name, limits, usage)
		}
	} else if utils.NeedsFormatting(c) {
		utils.FormatObjects(projects.Items, w, c)
	} else {
		w := new(tabwriter.Writer)
		w.Init(os.Stdout, 4, 4, 2, ' ', 0)
		fmt.Fprintf(w, "ID\tName\tLimit\tUsage\n")
		for _, t := range projects.Items {
			rt := t.ResourceTicket
			for i := 0; i < len(rt.Limits); i++ {
				if i == 0 {
					fmt.Fprintf(w, "%s\t%s\t%s %g %s\t%s %g %s\n", t.ID, t.Name,
						rt.Limits[i].Key, rt.Limits[i].Value, rt.Limits[i].Unit,
						rt.Usage[i].Key, rt.Usage[i].Value, rt.Usage[i].Unit)
				} else {
					fmt.Fprintf(w, "\t\t%s %g %s\t%s %g %s\n",
						rt.Limits[i].Key, rt.Limits[i].Value, rt.Limits[i].Unit,
						rt.Usage[i].Key, rt.Usage[i].Value, rt.Usage[i].Unit)
				}
			}
			for i := len(rt.Limits); i < len(rt.Usage); i++ {
				fmt.Fprintf(w, "\t\t\t%s %g %s\n", rt.Usage[i].Key, rt.Usage[i].Value, rt.Usage[i].Unit)
			}
		}
		err := w.Flush()
		if err != nil {
			return err
		}
		fmt.Printf("\nTotal projects: %d\n", len(projects.Items))
	}
	return nil
}
Ejemplo n.º 27
0
// Show project info with the specified project id, returns an error if one occurred
func showProject(c *cli.Context, w io.Writer) error {
	err := checkArgNum(c.Args(), 1, "project show <id>")
	if err != nil {
		return err
	}
	id := c.Args().First()

	client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c))
	if err != nil {
		return err
	}

	project, err := client.Esxclient.Projects.Get(id)
	if err != nil {
		return err
	}

	if c.GlobalIsSet("non-interactive") {
		securityGroups := []string{}
		for _, s := range project.SecurityGroups {
			securityGroups = append(securityGroups, fmt.Sprintf("%s:%t", s.Name, s.Inherited))
		}
		scriptSecurityGroups := strings.Join(securityGroups, ",")
		limits := quotaLineItemListToString(project.ResourceTicket.Limits)
		usages := quotaLineItemListToString(project.ResourceTicket.Usage)

		fmt.Printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n", project.ID, project.Name, project.ResourceTicket.TenantTicketID,
			project.ResourceTicket.TenantTicketName, limits, usages, scriptSecurityGroups)
	} else if utils.NeedsFormatting(c) {
		utils.FormatObject(project, w, c)
	} else {
		w := new(tabwriter.Writer)
		w.Init(os.Stdout, 4, 4, 2, ' ', 0)
		fmt.Fprintf(w, "Project ID: %s\n", project.ID)
		fmt.Fprintf(w, "  Name: %s\n", project.Name)
		fmt.Fprintf(w, "  TenantTicketID: %s\n", project.ResourceTicket.TenantTicketID)
		fmt.Fprintf(w, "    TenantTicketName: %s\n", project.ResourceTicket.TenantTicketName)
		fmt.Fprintf(w, "    Limits:\n")
		for _, l := range project.ResourceTicket.Limits {
			fmt.Fprintf(w, "      %s\t%g\t%s\n", l.Key, l.Value, l.Unit)
		}
		fmt.Fprintf(w, "    Usage:\n")
		for _, u := range project.ResourceTicket.Usage {
			fmt.Fprintf(w, "      %s\t%g\t%s\n", u.Key, u.Value, u.Unit)
		}
		if len(project.SecurityGroups) != 0 {
			fmt.Fprintf(w, "  SecurityGroups:\n")
			for _, s := range project.SecurityGroups {
				fmt.Fprintf(w, "    %s\t%t\n", s.Name, s.Inherited)
			}
		}
		err = w.Flush()
		if err != nil {
			return err
		}
	}
	return nil
}
Ejemplo n.º 28
0
//Enable cluster type for the specified deployment id
func enableClusterType(c *cli.Context) error {
	id, err := getDeploymentId(c)
	if err != nil {
		return err
	}

	clusterType := c.String("type")
	imageID := c.String("image-id")

	if !c.GlobalIsSet("non-interactive") {
		var err error
		clusterType, err = askForInput("Cluster Type: ", clusterType)
		if err != nil {
			return err
		}
		imageID, err = askForInput("Image ID: ", imageID)
		if err != nil {
			return err
		}
	}

	if len(id) == 0 {
		return fmt.Errorf("Please provide deployment id")
	}
	if len(clusterType) == 0 {
		return fmt.Errorf("Please provide cluster type using --type flag")
	}

	if len(imageID) == 0 {
		return fmt.Errorf("Please provide image ID using --image-id flag")
	}

	if confirmed(c.GlobalIsSet("non-interactive")) {
		client.Esxclient, err = client.GetClient(c.GlobalIsSet("non-interactive"))
		if err != nil {
			return err
		}
		clusterConfigSpec := &photon.ClusterConfigurationSpec{
			Type:    clusterType,
			ImageID: imageID,
		}

		task, err := client.Esxclient.Deployments.EnableClusterType(id, clusterConfigSpec)
		if err != nil {
			return err
		}

		_, err = waitOnTaskOperation(task.ID, c)
		if err != nil {
			return err
		}

	} else {
		fmt.Println("Cancelled")
	}
	return nil
}
Ejemplo n.º 29
0
// Sends a "show cluster" request to the API client based on the cli.Context
// Returns an error if one occurred
func showCluster(c *cli.Context, w io.Writer) error {
	err := checkArgNum(c.Args(), 1, "cluster show <id>")
	if err != nil {
		return err
	}
	id := c.Args().First()

	client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c))
	if err != nil {
		return err
	}

	cluster, err := client.Esxclient.Clusters.Get(id)
	if err != nil {
		return err
	}

	vms, err := client.Esxclient.Clusters.GetVMs(id)
	if err != nil {
		return err
	}

	var master_vms []photon.VM
	for _, vm := range vms.Items {
		for _, tag := range vm.Tags {
			if strings.Count(tag, ":") == 2 && !strings.Contains(strings.ToLower(tag), "worker") {
				master_vms = append(master_vms, vm)
				break
			}
		}
	}

	if c.GlobalIsSet("non-interactive") {
		extendedProperties := strings.Trim(strings.TrimLeft(fmt.Sprint(cluster.ExtendedProperties), "map"), "[]")
		fmt.Printf("%s\t%s\t%s\t%s\t%d\t%s\n", cluster.ID, cluster.Name, cluster.State, cluster.Type,
			cluster.WorkerCount, extendedProperties)
	} else if utils.NeedsFormatting(c) {
		utils.FormatObject(cluster, w, c)
	} else {
		fmt.Println("Cluster ID:            ", cluster.ID)
		fmt.Println("  Name:                ", cluster.Name)
		fmt.Println("  State:               ", cluster.State)
		fmt.Println("  Type:                ", cluster.Type)
		fmt.Println("  Worker count:        ", cluster.WorkerCount)
		fmt.Println("  Extended Properties: ", cluster.ExtendedProperties)
		fmt.Println()
	}

	err = printClusterVMs(master_vms, c.GlobalIsSet("non-interactive"))
	if err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 30
0
func attachIso(c *cli.Context) error {
	err := checkArgNum(c.Args(), 1, "vm attach-iso <id> [<options>]")
	if err != nil {
		return err
	}

	id := c.Args().First()
	path := c.String("path")
	name := c.String("name")

	if !c.GlobalIsSet("non-interactive") {
		path, err = askForInput("Iso path: ", path)
		if err != nil {
			return err
		}
		name, err = askForInput("ISO name (default: "+filepath.Base(path)+"): ", name)
		if err != nil {
			return err
		}
	}

	if len(path) == 0 {
		return fmt.Errorf("Please provide iso path")
	}
	if len(name) == 0 {
		name = filepath.Base(path)
	}

	path, err = filepath.Abs(path)
	if err != nil {
		return err
	}

	file, err := os.Open(path)
	if err != nil {
		return err
	}

	client.Esxclient, err = client.GetClient(c.GlobalIsSet("non-interactive"))
	if err != nil {
		return err
	}

	task, err := client.Esxclient.VMs.AttachISO(id, file, name)
	if err != nil {
		return err
	}

	_, err = waitOnTaskOperation(task.ID, c)
	if err != nil {
		return err
	}

	return nil
}