Skip to content

Argument --vmware-password should be optional, if not supplied, migratekit should ask for password interactively #133

@joelmclean

Description

@joelmclean

This is just an issue to track something we patched in a fork of migratekit, and I felt I should share it here.

Two changes to main.go:

  • add some imports
#~/vexxhost/migratekit$ git diff origin/main micron21/main
diff --git a/main.go b/main.go
index e0244ec..4f06ab4 100644
--- a/main.go
+++ b/main.go
@@ -6,7 +6,9 @@ import (
        "net/url"
        "os"
        "time"
-
+       "fmt"
+       "golang.org/x/term"
+        "strings"
        "github.com/erikgeiser/promptkit/confirmation"
        "github.com/gophercloud/gophercloud/v2/openstack/compute/v2/flavors"
        log "github.com/sirupsen/logrus"
  • update the function to handle no password, and prompt for password, but only if run as interactive terminal
@@ -83,6 +85,22 @@ var rootCmd = &cobra.Command{
                        log.SetLevel(log.DebugLevel)
                }

+               if password == "" {
+                   if !term.IsTerminal(int(os.Stdin.Fd())) {
+                       return fmt.Errorf("password is required but terminal is not interactive; please provide --vmware-password flag")
+                   }
+                   fmt.Print("Enter VMware Password: ")
+
+                   // ReadPassword hides the user's typing for security
+                   bytePassword, err := term.ReadPassword(int(os.Stdin.Fd()))
+                   if err != nil {
+                       fmt.Fprintf(os.Stderr, "Error reading password: %v\n", err)
+                       return err
+                   }
+                   password = strings.TrimSpace(string(bytePassword))
+                   fmt.Println() // Add a newline since ReadPassword doesn't print one
+               }
+
                endpointUrl := &url.URL{
                        Scheme: "https",
                        Host:   endpoint,
  • remove vmware-password as required flag
@@ -335,7 +353,6 @@ func init() {
        rootCmd.MarkPersistentFlagRequired("vmware-username")

        rootCmd.PersistentFlags().StringVar(&password, "vmware-password", "", "VMware password")
-       rootCmd.MarkPersistentFlagRequired("vmware-password")

        rootCmd.PersistentFlags().StringVar(&path, "vmware-path", "", "VMware VM path (e.g. '/Datacenter/vm/VM')")
        rootCmd.MarkPersistentFlagRequired("vmware-path")

We were able to compile this (docker build .) and are now using a local copy on our registry with this patch applied.

I will create a pull request to close this issue; note that I'm an average-to-sub-par programmer at best, so this can probably be done much nicer, feel free to improve on the idea.


Usage then becomes:

docker run -it --rm --privileged --network host -v /dev:/dev \
 -v /usr/lib64/vmware-vix-disklib/:/usr/lib64/vmware-vix-disklib:ro \
 --env-file <(env | grep OS_) our.local.registry:5000/migratekit:v1 migrate \
 --vmware-endpoint vcenter.our.vcenter.url --vmware-username 'my-migration-username' \
 --vmware-path '/VDC/vm/vm-group/vm-name' --availability-zone zone_1

and the output prompts for password, see below output:

root@migration-xxxxxx:~# docker run -it --rm --privileged --network host -v /dev:/dev \
 -v /usr/lib64/vmware-vix-disklib/:/usr/lib64/vmware-vix-disklib:ro \
 --env-file <(env | grep OS_) our.local.registry:5000/migratekit:v1 migrate \
 --vmware-endpoint vcenter.our.vcenter.url --vmware-username 'my-migration-username' \
 --vmware-path '/VDC/vm/vm-group/vm-name' --availability-zone zone_1
Enter VMware Password:
INFO[0004] Setting Disk Bus: virtio
Creating snapshot 100% [============================================================================================================================] (100/100) [0s:0s]
WARN[0006] Change ID mismatch, full copy needed          currentChangeId= snapshotChangeId="redacted"

Happy Migrating!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions