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?
You can either
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
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.
use pushd and popd
Stores the current directory for use by the POPD command, then
changes to the specified directory.
PUSHD [path | ..]
path Specifies the directory to make the current directory.
If Command Extensions are enabled the PUSHD command accepts
network paths in addition to the normal drive letter and path.
If a network path is specified, PUSHD will create a temporary
drive letter that points to that specified network resource and
then change the current drive and directory, using the newly
defined drive letter. Temporary drive letters are allocated from
Z: on down, using the first unused drive letter found.
Related
Wondering if it is possible to automatically run a script or execute a command ONLY after vagrant ssh into the box? I understand that Ansible can provide beforehand installation and set up. But it failed to allow doing things automatically after entering the machine.
I am currently create a file script.sh. The file will be provided to the vagrant via Ansible. After I vagrant ssh into the box, I do bash script.sh to run the script. Is there better way?
Any suggestion would be more appreciated.
Two ways to achieve this,
Say assume your script is in vagrant home directory like,
:~$/home/vagrant/test-me.sh
1) Run command along with ssh
1a) vagrant ssh -- -t '/home/vagrant/test-me.sh; /bin/bash'
**-OR-**
1b) vagrant ssh -c '/home/vagrant/test-me.sh; /bin/bash'
2) Append complete script path in ~/.bashrc file (this should be in vagrant home directory if you are login as user vagrant)
:~$echo '. /home/vagrant/test-me.sh' >> ~/.bashrc
I'm on a Windows host using Git Bash to run the .sh files.
There are 4 components to my current project. To start up it on localhost, I have to:
webdriver-manager start since I'm the QA and need that running anyway
vagrant up in the project's parent folder, then close out that window (or just start the VM myself via VirtualBox UI)
vagrant ssh cd /vagrant cd "component's folder" docker-compose up x 4
grunt serve
Right now, I have a .sh file each for 1, 2, and 4, but I cannot find how to pass along multiple commands to vagrant ssh, especially since docker-compose up needs to be constantly running.
Is there a way to pass along those cds and the docker-compose?
I found the ssh documentation from vagrant which mentions something about needing to do fancy things to get it running background processes, but I have no idea what it's doing or how to implement that in a .sh file since the wording is so wishy-washy.
Also, I'm new to shell scripts in general, so if there's a smarter way to go about this to solve the issue, I'd appreciate it, too. These scripts aren't necessary, I just don't want to have to type it repeatedly every day when I'm running my tests locally.
From your Vagrantfile, have something like this
$script = <<SCRIPT
echo "running script in the VM"
cd /vagrant
cd "component's folder"
docker-compose up
cd "component's folder 2"
docker-compose up
# and add all other commands you would run from the VM
SCRIPT
Vagrant.configure(2) do |config|
....
config.vm.provision "shell", inline: $script
....
end
Note: this will run the commands as sudo (from your VM) if you want to run them as your vagrant user, just do
config.vm.provision "shell", inline: $script, privileged: "false"
If the commands needs to be invoked on vagrant up, you can provide provisioning script available on the host machine by:
config.vm.provision "shell", path: '/vagrant/scripts/provision.sh'
so Vagrant will then upload this script into the guest and execute it (using URL instead of path would also work),
Alternatively you may use inline shell syntax:
config.vm.provision "shell", inline: "echo Hello, World"
Or to run the script within VM, then try:
config.vm.provision "shell", inline: %Q(/usr/bin/env VAR=1 bash /vagrant/script.sh)
To run one-time off commands in VM, you may use vagrant ssh command for that, for example:
vagrant ssh -c "cd /vagrant && echo Hello, World"
Everytime I launch vagrant for one of our projects I go through the following incantation:
vagrant up
vagrant ssh
sudo su deploy
supervisorctl stop local
workon odoo-8.0
/home/deploy/odoo/build/8.0/openerp-server -c /home/deploy/odoo/local/odoo_serverrc
This runs the server in a way that lets me see the terminal output. Is there a way I could package this all up so I can do say; vagrant dev or some such?
You can use shell provisioner.
In your vagrantfile, you can do things like this:
$script = <<SCRIPT
echo I am provisioning...
date > /etc/vagrant_provisioned_at
SCRIPT
Vagrant.configure("2") do |config|
config.vm.provision "shell", inline: $script
end
You can replace
echo I am provisioning...
date > /etc/vagrant_provisioned_at
with your own commands.
On the first 'vagrant up' that creates the environment, provisioning is run. If the environment was already created and the up is just resuming a machine or booting it up, they won't run unless the --provision flag is explicitly provided.
There are many more good ways to provision, I would also recommend using Ansible. Here is the doc you can read:
https://docs.vagrantup.com/v2/provisioning/basic_usage.html
First, create a shell script with your commands in them:
#!/bin/bash
vagrant up
vagrant ssh
sudo su deploy
supervisorctl stop local
workon odoo-8.0
/home/deploy/odoo/build/8.0/openerp-server -c /home/deploy/odoo/local/odoo_serverrc
Put it somewhere in your guest with ansible. Next, copy the /home/vagrant/.bashrc file into yoour ansible files/ folder. Add the line
bash /path/to/shellfile.sh
to the .bashrc and make sure ansible copies it into your guest.
After that, the shell script should be executed every time you log into the guest.
I'm asking so I don't have to "cd" everytime I use Vagrant. Thanks.
You can add cd dir-name to your .bashrc file inside your vm. So once you ssh into your vagrant machine it'll automatically run and change the directory.
On ubuntu .bashrc file is located in home (/home/vagrant) directory.
Alternatively you can connect to your vagrant box through starndard ssh command. This will allow you to specify the directory name at the connect time and have more freedom.
For example
ssh -p 2222 vagrant#localhost -t "cd dir-name ; /bin/bash"
You can see vagrant ssh config using below command. So you can check your port, user.. etc.
vagrant ssh-config
This works:
vagrant ssh
cd /vagrant && grunt build
This doesn't:
vagrant ssh -c 'cd /vagrant && grunt build'
(exits with bash: grunt: command not found)
Why?
Reason I'm asking is that I have a shell script that deploys a site to GitHub Pages, and I wanted to add a build step at the beginning to get a fresh build right before deployment.
I also tried writing a shell script vagrant_build.sh like:
cd /vagrant
grunt build
and having vagrant run it with vagrant ssh -c 'bash /vagrant/vagrant_build.sh', but it still can't find grunt.
The docs say that vagrant ssh -c COMMAND runs a single command, do I need to take that 100% literally? I was interpreting it as 'anything you can fit in one line in a terminal'.
because grunt is not in your vagrant's path. so give it absolute path.
cd /vagrant && /path/to/grunt build