|
| 1 | +// Copyright 2022 Jetpack Technologies Inc and contributors. All rights reserved. |
| 2 | +// Use of this source code is governed by the license in the LICENSE file. |
| 3 | + |
| 4 | +package cloud |
| 5 | + |
| 6 | +import ( |
| 7 | +"encoding/json" |
| 8 | +"fmt" |
| 9 | +"log" |
| 10 | +"os" |
| 11 | +"strings" |
| 12 | +"time" |
| 13 | + |
| 14 | +"github.com/AlecAivazis/survey/v2" |
| 15 | +"github.com/fatih/color" |
| 16 | +"go.jetpack.io/devbox/cloud/mutagen" |
| 17 | +"go.jetpack.io/devbox/cloud/sshclient" |
| 18 | +"go.jetpack.io/devbox/cloud/sshconfig" |
| 19 | +"go.jetpack.io/devbox/cloud/stepper" |
| 20 | +) |
| 21 | + |
| 22 | +func Shell() error { |
| 23 | +// TODO: check if `devbox.json` exists. |
| 24 | +// TODO: find project's "root" directory based on `devbox.json` location. |
| 25 | +setupSSHConfig() |
| 26 | + |
| 27 | +c := color.New(color.FgMagenta).Add(color.Bold) |
| 28 | +c.Println("Devbox Cloud") |
| 29 | +fmt.Println("Blazingly fast remote development that feels local") |
| 30 | +fmt.Print("\n") |
| 31 | + |
| 32 | +username := promptUsername() |
| 33 | +s1 := stepper.Start("Creating a virtual machine on the cloud...") |
| 34 | +vmHostname := getVirtualMachine(username) |
| 35 | +s1.Success("Created virtual machine") |
| 36 | + |
| 37 | +s2 := stepper.Start("Starting file syncing...") |
| 38 | +err := syncFiles(username, vmHostname) |
| 39 | +if err != nil { |
| 40 | +s2.Fail("Starting file syncing [FAILED]") |
| 41 | +log.Fatal(err) |
| 42 | +} |
| 43 | +s2.Success("File syncing started") |
| 44 | + |
| 45 | +s3 := stepper.Start("Connecting to virtual machine...") |
| 46 | +time.Sleep(1 * time.Second) |
| 47 | +s3.Stop("Connecting to virtual machine") |
| 48 | + |
| 49 | +fmt.Print("\n") |
| 50 | + |
| 51 | +return shell(username, vmHostname) |
| 52 | +} |
| 53 | + |
| 54 | +func setupSSHConfig() { |
| 55 | +if err := sshconfig.Setup(); err != nil { |
| 56 | +log.Fatal(err) |
| 57 | +} |
| 58 | +} |
| 59 | + |
| 60 | +func promptUsername() string { |
| 61 | +username := "" |
| 62 | +prompt := &survey.Input{ |
| 63 | +Message: "What is your github username?", |
| 64 | +Default: os.Getenv("USER"), |
| 65 | +} |
| 66 | +err := survey.AskOne(prompt, &username, survey.WithValidator(survey.Required)) |
| 67 | +if err != nil { |
| 68 | +log.Fatal(err) |
| 69 | +} |
| 70 | +return username |
| 71 | +} |
| 72 | + |
| 73 | +type authResponse struct { |
| 74 | +VMHostname string `json:"vm_host"` |
| 75 | +} |
| 76 | + |
| 77 | +func getVirtualMachine(username string) string { |
| 78 | +client := sshclient.Client{ |
| 79 | +Username: username, |
| 80 | +// TODO: change gateway to prod by default before relesing. |
| 81 | +Hostname: "gateway.dev.devbox.sh", |
| 82 | +} |
| 83 | +bytes, err := client.Exec("auth") |
| 84 | +if err != nil { |
| 85 | +log.Fatal(err) |
| 86 | +} |
| 87 | +resp := &authResponse{} |
| 88 | +err = json.Unmarshal(bytes, resp) |
| 89 | +if err != nil { |
| 90 | +log.Fatal(err) |
| 91 | +} |
| 92 | + |
| 93 | +return resp.VMHostname |
| 94 | +} |
| 95 | + |
| 96 | +func syncFiles(username string, hostname string) error { |
| 97 | +// TODO: instead of id, have the server return the machine's name and use that |
| 98 | +// here to. It'll make things easier to debug. |
| 99 | +id, _, _ := strings.Cut(hostname, ".") |
| 100 | +_, err := mutagen.Sync(&mutagen.SessionSpec{ |
| 101 | +// If multiple projects can sync to the same machine, we need the name to also include |
| 102 | +// the project's id. |
| 103 | +Name: fmt.Sprintf("devbox-%s", id), |
| 104 | +AlphaPath: ".", // We should use location of `devbox.json` |
| 105 | +BetaAddress: fmt.Sprintf("%s@%s", username, hostname), |
| 106 | +// It's important that the beta path is a "clean" directory that will contain *only* |
| 107 | +// the projects files. If we pick a pre-existing directories with other files, those |
| 108 | +// files will be synced back to the local directory (due to two-way-sync) and pollute |
| 109 | +// the user's local project |
| 110 | +BetaPath: "~/Code/", |
| 111 | +IgnoreVCS: true, |
| 112 | +SyncMode: "two-way-resolved", |
| 113 | +}) |
| 114 | +if err != nil { |
| 115 | +return err |
| 116 | +} |
| 117 | +time.Sleep(1 * time.Second) |
| 118 | +return nil |
| 119 | +} |
| 120 | + |
| 121 | +func shell(username string, hostname string) error { |
| 122 | +client := &sshclient.Client{ |
| 123 | +Username: username, |
| 124 | +Hostname: hostname, |
| 125 | +} |
| 126 | +return client.Shell() |
| 127 | +} |
0 commit comments