Enable sudo on agent.connect() when MUTAGEN_USE_SUDO environment var set to 1
#505
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
While trying to fix Mutagen for my use-case, I specifically decided to do the least work possible so I did not consider any refactoring which I might have had a been a member of the Mutagen team, so please judge my PR accordingly.
I found one problem followed by another. First, I needed to make Mutagen run the agent as
sudo, and then I needed to usevisudoto make/var/lib/docker/volumes/*/_dataavailable to me (note the wildcard in the path). The specific text I used invisudowas:Of course
sudoshould ONLY be used when someone specifically requests it and this PR handles that.To use
sudoonly when specifically requested and not when others who use Mutagen do not need or wantsudoI considered adding a configuration item — and even did so and got it working — but then realized it was notsynchronizationvs.forwardingspecific. I also decided I could do it with fewer code changes by just using an environment variable —MUTAGEN_USE_SUDO— so that is what I did.I modified
./pkg/agent/dial.goand adding the following code just before the command, composed withfmt.Sprintf():The above code first tests to see if the agent exists and if it does not it prints the value of
notInstalledFlag— or"Agent not installed"— tostdErr. This allows a test for that value later in thefunc.If that is returned my code then causes
agent.connect()to immediately return to its caller while settingTryInstallandCmdExetotrueto cause the installer to try to install the agent. I had to add that last bit because there were numerous times during my testing where I found Mutagen would simply give up and never retry to install when the agent was not installed.Hopefully you will find this useful and that it will meet the needs of others with similar scenarios to the one I experienced. But if you do not, I will at least have a custom Mutagen that meets my needs.