Automating tasks after vagrant ssh - bash

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

Related

cygwin: vagrant ssh, empty command prompt

If I vagrant ssh with windows cmd, I get a nice command prompt, like that:
vagrant#homestead:~$ echo foo
vagrant#homestead:~$ foo
But with cygwin and mintty, I have no prompt at all:
echo foo
foo
I see it has to do with "pseudo-tty allocation".
With cygwin and mintty, I can have my prompt with this :
vagrant ssh -- -t -t
How can I change cygwin and mintty so that I don't have to tell the -t ?
About the ssh -t option :
"Force pseudo-tty allocation. This can be used to execute arbi-
trary screen-based programs on a remote machine, which can be
very useful, e.g., when implementing menu services. Multiple -t
options force tty allocation, even if ssh has no local tty."
I had the same problem with and the solution was to set the VAGRANT_PREFER_SYSTEM_BIN environment variable to get vagrant to use your normal ssh executable.
You can do:
VAGRANT_PREFER_SYSTEM_BIN=1 vagrant ssh
or put this into your .bash_profile:
export VAGRANT_PREFER_SYSTEM_BIN=1
Reference: https://github.com/hashicorp/vagrant/issues/9143#issuecomment-343311263
I run in the same problem described above. But only on one of three PCs. But as a workaround I am doing:
# save the config to a file
vagrant ssh-config > vagrant-ssh
# run ssh with the file.
ssh -F vagrant-ssh default
From an answer of How to ssh to vagrant without actually running "vagrant ssh"?
In this case I am getting the prompt and what's more important also history cycling and ctrl-c etc. are working properly.
Vagrant is a windows program managing Virtual machine
https://www.vagrantup.com/intro/index.html
as such it does not well interface with the pseudo tty
structure used by cygwin programs.
Read for reference on similar issues with a lot of other windows program
https://github.com/mintty/mintty/issues/56
Mintty is a Cygwin program. It expect interactive program running inside it to use the cygwin tty functionality for interactive behaviour.
Running Vagrant from Bash in Windows CMD, make CMD the terminal control so Vagrant has no problem in the interactive behaviour.
I do not see the need to run Vagrant inside Cygwin
Since vagrant is windows-based, I use ConEmu instead of cygwin's shell (mintty)
choco install conemu via chocolatey and it works
General solution is to teach vagrant to use ssh, compatible with preferred terminal. Like Cygwin ssh+mintty.
Modern Vagrant (v2.1.2) has VAGRANT_PREFER_SYSTEM_BIN=1 by default on Windows.
To troubleshoot issue:
VAGRANT_LOG=info vagrant ssh
In v2.1.2 they broke Cygwin support. See my bug report with hack to lib/vagrant/util/ssh.rb to make it work.

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?
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.

Can I get vagrant to execute a series of commands where I get shell access and the webserver launches?

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.

Vagrant - Chef provisioning

I need help regarding chef...
-> I packaged ubuntu w/ gui
-> on vagrantfile Im running chef.. but because it's gui it doesn't run the installations (vagrant up)
help anyone?
additional infos:: gui = true
when running the vagrantile on a non gui box, you will see the installations on your terminal being performed automatically...
when vagrant up it stops here
Vagrant::Errors::VagrantError: The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!
mkdir -p /vagrant
Stdout from the command:
Stderr from the command:
sudo: no tty present and no askpass program specified
Make sure that vagrant user is allowed to execute sudo commands without password prompt.
Usually this can be achieved by adding this line to /etc/sudoers
vagrant ALL=(ALL) NOPASSWD:ALL
Or just edit line with admins group (this is how it's done in official precise64 vagrant box)
%admin ALL=(ALL) NOPASSWD:ALL
Note that you should edit /etc/sudoers only with sudo visudo command.
P.S. This method is quick and dirty way, it's better to manage /etc/sudoers.d/ folder and never touch /etc/sudoers file manually.
Sounds like you vagrant user is not in the sudoers list.
Compare /etc/sudoers.d on gui and non-gui box.

How do you automatically go to a folder whenever you use vagrant with "vagrant ssh" with Laravel Homestead?

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

Resources