Galaxiset Galaxiset - 4 months ago 20
Bash Question

Run rest of batch commands after vagrant starts

1) Hello I want to make a script to run a script to start vagrant from different directory and when i log in to it i want to start few commands inside vagrant, is it possible?

script.bat

call ng build
call node copy.js
cd \b\
vagrant up
vagrant ssh
cd /
sudo /etc/init.d/httpd start
sudo /etc/init.d/postgresql start
java -jar /opt/app/app-0.0.1-SNAPSHOT.jar


For now it stops after I log in to vagrant using vagrant ssh.
Additionally:
2) Also do you know a command that I could use to stay in my directory after I exit from vagrant. I originally run this script from directory C:/a and I want to run these commands inside C:/b folder and want to stay inside C:/a after exiting.
I tried pushd and popd but with no effect.

3) Also do is it possible to not run vagrant up when I do exit from vagrant but it is still up?

Answer

You can either

  1. do vagrant shell provisioning that can be run always so add something like in your Vagrantfile

      config.vm.provision "shell", privileged: false, run: "always", inline: <<-SHELL
        sudo /etc/init.d/httpd start
        sudo /etc/init.d/postgresql start
        java -jar /opt/app/app-0.0.1-SNAPSHOT.jar
      SHELL
    

so that will run those commands each time you run vagrant up

  1. run the commands from vagrant ssh from the script.bat file, something like

    call ng build
    call node copy.js
    cd \b\
    vagrant up
    vagrant ssh -c "sudo /etc/init.d/httpd start"
    vagrant ssh -c "sudo /etc/init.d/postgresql start"
    vagrant ssh -c "java -jar /opt/app/app-0.0.1-SNAPSHOT.jar"
    

To asnwer 2nd point

Also do you know a command that I could use to stay in my directory after I exit from vagrant. I originally run this script from directory C:/a and I want to run these commands inside C:/b folder and want to stay inside C:/a after exiting

vagrant will return to the current directory it was running.

Lets say you run from c:/a your script, inside your script you're running to c:/b, when you exit from vagrant, it will return from c:/b. If you want to exit and return to c:/a, make sure to initiate a cd c:/a before executing vagrant command

3) Also do is it possible to not run vagrant up when I do exit from vagrant but it is still up?

Its not clear - vagrant up means you boot a VM and it is running - after this you can ssh to get inside the VM but you exit the VM at this point, the VM is still up and running until you run vagrant halt.

You might have some confusion between how vagrant is running and how it runs within your script. Make sure you get familiar to the way vagrant is running and then after you can script it.

Comments