Vagrant: Allow "vagrant box prune" to be automated (e.g. via cron)

Created on 13 Jun 2019  ยท  4Comments  ยท  Source: hashicorp/vagrant

Vagrant version: 2.2.4
Host operating system: ubuntu 16.04
Guest operating system: ubuntu 16.04
Vagrantfile: Not relevant
Debug output: Not relevant

Actual behavior / steps to reproduce

  • vagrant box prune removes all outdated / non-latest boxes.
  • If one of those boxes is being used by a VM, prune will display a prompt
  • A standard way to automatically respond to this kind of prompt is using yes N | command
  • That results in the following error:
โฏ yes N | vagrant box prune  
Checking for older boxes...
Vagrant is attempting to interface with the UI in a way that requires
a TTY. Most actions in Vagrant that require a TTY have configuration
switches to disable this requirement. Please do that or run Vagrant
with TTY.

It does not appear that vagrant box prune has a "configuration switch" to disable the TTY requirement.

I also understand that vagrant box prune has a --force option. This works if you don't want to keep the used VM box.

Expected behavior

Couple of options:

  • Allow vagrant prune box to be run without a TTY such that yes N | command will work
  • Add a new option to vagrant prune box that will skip the prompt but not delete any used VMs (this would be the opposite of --force)
  • Unknown third option?
core enhancement has-pr

Most helpful comment

Hi @kylegibson - I think it makes sense to add another "force" type option that will prune boxes, not in use, without confirmation. :+1:

All 4 comments

DIRTY HACK INCOMING
What follows is _NOT_ the right way to solve your problem, just a workaround to get going. Someone should really add an option to skip the TTY test. Unfortunately, I don't know Ruby.

I've looked for ways to simulate a TTY on stdin, and while there are solutions, they add lots of complexity. So I tried something simpler: since Vagrant checks if stdin is a TTY, we can trick it into believing it is by preloading a different version of the isatty function into the process.

Compile this small piece of C into a shared object:

/* isatty.c */
int isatty(int fd) {
    return 1;
}

gcc -o isatty.so -fPIC -shared isatty.c

Then launch Vagrant with your synthetic input, but preload this SO:

yes N | LD_PRELOAD=/path/to/isatty.so vagrant box prune

This should yield the desired result: I've tested it with vagrant destroy which gives the same error if called after a pipe. It is clearly a hack since this will make every single file descriptor passed to isatty look like a TTY and we are messing with standard function.

Hi @kylegibson - I think it makes sense to add another "force" type option that will prune boxes, not in use, without confirmation. :+1:

Would be nice, currently have vagrant box update automatically happening but can't get the prune done via cicd as I don't want to force. What about;
-silent
-skip-in-use-boxes
-skip-used-boxes
-leave-used-boxes

A command line argument using the word force, i would assume deleted or overrides and does not leave or ignore boxes that are in use.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jsirex picture jsirex  ยท  3Comments

StefanScherer picture StefanScherer  ยท  3Comments

mpontillo picture mpontillo  ยท  3Comments

hesco picture hesco  ยท  3Comments

bbaassssiiee picture bbaassssiiee  ยท  3Comments