Search code examples
djangobatch-filevagrant

How can I use a batch file to run Vagrant commands, but leave me connected to Vagrant once finished?


Here's what I have in the batch file so far:

set root="C:\Users\esohlberg\lwebsite"
cd %root%
vagrant up
vagrant ssh -- -t "source lw/bin/activate && cd /vagrant/; ./manage.py runserver 0.0.0.0:8000"
cmd /k

Once Vagrant is up, I activate a virtualenv, cd into the right place, and run a server. Executing this takes me all the way to the server running, where I can see

System check identified no issues (0 silenced).
August 24, 2018 - 12:33:12
Django version 2.0.3, using settings 'lwebsite.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

However, as soon as I quit with CONTROL-C, I see

Connection to 127.0.0.1 closed.

and I'm no longer in Vagrant. Is it possible to set up the commands in such a way that once the server is quit, I stay in the /vagrant/ directory with the connection still up and Vagrant's virtualenv still active? This would allow me to manage the site or run the server again with less hassle.

I've already looked at https://www.vagrantup.com/docs/provisioning/shell.html, but the examples seem to show commands executed only during provisioning, which I don't want to do every single time I execute this file.


Solution

  • I created three .bat files to start, stop vagrant and connect over ssh.

    For starting vagrant, also added this to auto run when Windows starting:

    cd /d "C:\Homestead"
    start vagrant up
    

    For stoping vagrant:

    @echo off
    cd /d "C:\Homestead"
    start vagrant halt
    

    And for connecting to local server over ssh

    cd /d "C:\Homestead"
    vagrant ssh