Skip to content

Conversation

savil
Copy link
Collaborator

@savil savil commented Dec 9, 2022

Summary

This PR adds logic for the sshshim to check for a live VM. If the VM is not reachable,
then it should terminate the sync session. We label each sync with the machineID
so that cloud-shells to the same vm get the same label for their mutagen sync.

To test for liveness, I currently execute a echo "alive" command in the VM. There
are likely better ways to do this. Suggestions welcome!

NOTE:
For now, I pass-thru the ssh/scp command if the arguments indicate that the server
is not a devbox-vm. In the next PR, I'll see if we can have a dedicated mutagen daemon
via setting MUTAGEN_DATA_DIRECTORY, in which case we can more strongly impose
that the ssh/scp command can be executed only with a liveVM. UPDATE: this works!

NOTE:
I also discovered that I need to manually empty-out the MUTAGEN_PROMPTER env-var.
This is done, because mutagen otherwise will expect a certain (interactive?) prompt
in the arguments to the mutagen CLI. Mutagen does set this env-var on all ssh/scp executions, but I'm a bit unclear on why this is hooked up.

How was it tested?

  1. ensure no active mutagen daemon (next PR should take away this step)

  2. manually start a vm (command omitted due to closed source, but happy to share)

  3. DEVBOX_FEATURE_SSH_SHIM=1 DEVBOX_VM=username@vmaddress devbox cloud shell

  4. cd ~/.config/devbox/ssh/shims and tail -f logs.txt to monitor the shim.

  5. exit the cloud shell

  6. fly m stop <id> to stop the vm

  7. touch foo.txt in the devbox project folder to trigger a sync action

  8. wait for the logs.txt to show the mutagen sync terminate command.

  9. ~/.cache/mutagen/bin/mutagen sync list to confirm the session is terminated.

@savil savil force-pushed the savil/cloud-ssh-4 branch from a0daaf8 to 1c84d2e Compare December 9, 2022 20:49
@savil savil requested review from loreto and gcurtis and removed request for loreto December 9, 2022 20:50
@savil savil marked this pull request as ready for review December 9, 2022 20:50
@savil savil force-pushed the savil/cloud-ssh-3 branch from 94078a7 to 5f8aff3 Compare December 12, 2022 16:43
@savil savil force-pushed the savil/cloud-ssh-4 branch from 78d54e9 to 9d0aa27 Compare December 12, 2022 16:43
@savil savil force-pushed the savil/cloud-ssh-3 branch from 5f8aff3 to 04fe82f Compare December 12, 2022 16:52
@savil savil force-pushed the savil/cloud-ssh-4 branch from 9d0aa27 to 7162db4 Compare December 12, 2022 16:52
@savil savil force-pushed the savil/cloud-ssh-3 branch from 04fe82f to b78cf72 Compare December 12, 2022 16:59
@savil savil force-pushed the savil/cloud-ssh-4 branch from 7162db4 to f7f504b Compare December 12, 2022 16:59
Base automatically changed from savil/cloud-ssh-3 to main December 12, 2022 17:11
@savil savil force-pushed the savil/cloud-ssh-4 branch 2 times, most recently from 84b95fd to daa80bf Compare December 12, 2022 18:42
newEnv = append(newEnv, fmt.Sprintf("%s=%s", k, v))
// envAsKeyValueStrings prepares the env-vars in key=value format to add to the command to be run
//
// panics if os.Environ() returns an array with any element not in key=value format
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this happen?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it really cannot, hence I thought it was acceptable to have the panic.

for _, cmdEnv := range cmd.Env {
if strings.HasPrefix(cmdEnv, "MUTAGEN_SSH") {
if strings.HasPrefix(cmdEnv, "MUTAGEN") {
envPrint = fmt.Sprintf("%s\n", cmdEnv)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this will always print the last MUTAGEN_* env var instead of all of them. Is that intentional?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha, yup, it was for this PR. In one of the subsequent PRs, I fixed it to print all of them.


err := cmd.Run()
if err != nil {
if err.Error() == "exit status 255" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you unwrap this to an exec.ExitError you can check the exit code directly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes, I had meant to do this, but forgot to update the code. Updated!


func checkActiveVM(vmAddr string) (bool, error) {

cmd := exec.Command("ssh", vmAddr, "echo 'alive'")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to use exec.CommandContext so you can enforce a timeout on the health check.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call! Added

@savil savil force-pushed the savil/cloud-ssh-4 branch from 4613c2a to 6524f8e Compare December 13, 2022 20:35
@savil savil merged commit ba4a4ac into main Dec 13, 2022
@savil savil deleted the savil/cloud-ssh-4 branch December 13, 2022 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants