func()

in tools/ctpu/commands/up.go [444:532]


func (c *upCmd) Execute(ctx context.Context, flags *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
	err := c.cfg.Validate()
	if err != nil {
		log.Print(err)
		return subcommands.ExitFailure
	}

	if !c.cli.IsGcloudInstalled() {
		c.cli.PrintInstallInstructions()
		return subcommands.ExitFailure
	}

	// Determine if the APIs have been enabled and/or if there are instances running
	tpuInstance, tpuEnabled, err := c.tpu.OptionallyRetrieveInstance(false)
	gceInstance, _, err := c.gce.OptionallyRetrieveInstance(false)
	alreadyRunning := tpuInstance != nil && tpuInstance.IsRunning() && gceInstance != nil && gceInstance.IsRunning()

	if tpuEnabled {
		// TF Version check.
		err = c.ConfigureTFVersion()
		if err != nil {
			log.Print(err)
			return subcommands.ExitFailure
		}
	}

	if !alreadyRunning && !c.skipConfirmation {
		confirmed, err := c.confirmExecution(tpuEnabled)
		if err != nil {
			log.Print(err)
			return subcommands.ExitFailure
		}
		if !confirmed {
			color.Red("Canceling ctpu; no changes have been made.")
			fmt.Printf(`
Note: if there was a configuration issue, you can override the settings using
flags on the command line (e.g. --zone, --project, --name) for a given ctpu
execution. For the full list of flags, run: ctpu help up
`)
			return subcommands.ExitUsageError
		}
	}

	if !tpuEnabled {
		origTFVersion := c.tfVersion
		// Enable the API by attempting to retrieve the instance.
		_, err := c.tpu.Instance()
		if err != nil {
			log.Print(err)
			return subcommands.ExitFailure
		}
		// TF Version check.
		err = c.ConfigureTFVersion()
		if err != nil {
			log.Print(err)
			return subcommands.ExitFailure
		}
		if origTFVersion == "" {
			fmt.Printf("Selected TensorFlow version: %q. Launching instances...\n", c.tfVersion)
		}
	}

	tpu, err := c.launchInstances(ctx)
	if err != nil {
		fmt.Printf("%v\n", err)
		return subcommands.ExitFailure
	}

	// Do not attempt to ssh, as no instance was started.
	if c.tpuOnly {
		fmt.Println("Operation success; not ssh-ing to GCE VM due to --tpu-only flag.")
		return subcommands.ExitSuccess
	}

	if inOrg, err := c.rmg.IsProjectInGoogleOrg(); err == nil && inOrg && c.cfg.Environment == "devshell" {
		color.Red("WARNING: Attempting to ssh from Cloud Shell in Google org; this might not work!")
	}

	if c.forwardPorts {
		fmt.Println("About to ssh (with port forwarding enabled -- see docs for details)...")
	} else {
		fmt.Println("About to ssh...")
	}
	if err := c.cli.SSHToInstance(c.forwardPorts, c.forwardAgent, tpu); err != nil {
		log.Printf("Could not ssh to instance: %v\n", err)
		return subcommands.ExitFailure
	}
	return subcommands.ExitSuccess
}