Tuesday, 19 July 2016

Vagrant SetUp



Open command Prompt

Check installation

C:\Users\psharm>vagrant -v
Vagrant 1.8.1

Choose the VM to install at :


Go for hashicorp/precise32

Install using

C:\Users\psharm\machine1>vagrant box add hashicorp/precise32
==> box: Loading metadata for box 'hashicorp/precise32'
==> box: Adding box 'hashicorp/precise32' (v1.0.0) for provider: virtualbox

 Alternate 

C:\Users\psharm>cd Angular2Test

C:\Users\psharm\Angular2Test> vagrant init
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.


C:\Users\psharm\Angular2Test> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'base' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Box file was not detected as metadata. Adding it directly...
==> default: Adding box 'base' (v0) for provider: virtualbox
    default: Downloading: base
    default:
An error occurred while downloading the remote file. The error
message, if any, is reproduced below. Please fix this error and try
again.

Couldn't open file /C:/Users/psharm/Angular2Test/base

C:\Users\psharm\Angular2Test> vagrant init precise64 http://files.vagrantup.com/precise64.box
`Vagrantfile` already exists in this directory. Remove it before
running `vagrant init`.

delete the previously created vagrant file

C:\Users\psharm\Angular2Test> vagrant init precise64 http://files.vagrantup.com/precise64.box
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

C:\Users\psharm\Angular2Test>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'precise64' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Box file was not detected as metadata. Adding it directly...
==> default: Adding box 'precise64' (v0) for provider: virtualbox
    default: Downloading: http://files.vagrantup.com/precise64.box
    default: Progress: 100% (Rate: 2388k/s, Estimated time remaining: --:--:--)
==> default: Successfully added box 'precise64' (v0) for 'virtualbox'!
==> default: Importing base box 'precise64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: Angular2Test_default_1468906683296_20165
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.2.0
    default: VirtualBox Version: 5.0
==> default: Mounting shared folders...
    default: /vagrant => C:/Users/psharm/Angular2Test

At this point you can see the folder ".vagrant" is created in  the Angular2Test folder 

C:\Users\psharm\Angular2Test>vagrant ssh
`ssh` executable not found in any directories in the %PATH% variable. Is an
SSH client installed? Try installing Cygwin, MinGW or Git, all of which
contain an SSH client. Or use your favorite SSH client with the following
authentication information shown below:

Host: 127.0.0.1
Port: 2222
Username: vagrant
Private key: C:/Users/psharm/Angular2Test/.vagrant/machines/default/virtualbox/private_key

You can use putty to log into vagrant machine, with password vagrant. 

When you’re done playing around with the SSH session, log out by typing exit and pressing enter. This will put you back into your terminal on the host machine. Run vagrant destroy, and the virtual machine will be deleted and any resources it consumed such as RAM or disk space will be reclaimed:


C:\Users\psharm\Angular2Test>vagrant destroy
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...

C:\Users\psharm\Angular2Test> vagrant up

Because Vagrant runs the virtual machines without a user interface, it is easy to forget
the current state of the environment (i.e., whether the virtual machine was created, if
it’s running, etc.). For this, vagrant status comes in handy:
$ vagrant status

C:\Users\psharm\Angular2Test>vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

Now we can edit the vagrant file to install all the dependencies 

Vagrant.configure(2) do |config|

 config.vm.box = "precise64"
  config.vm.box_url = "http://files.vagrantup.com/precise64.box"

  config.vm.network :forwarded_port, guest: 9000, host: 9000
  config.vm.network :forwarded_port, guest: 35729, host: 35729

  config.vm.provision "shell", :path => "provision.sh"
   

  end

This file points to provision.sh . In this file we write the unix commands and , when we run  vagrant up . This will start the machine and install all the dependencies for us.



vagrant halt

C:\Users\psharm>vagrant --help
Usage: vagrant [options] []

    -v, --version                    Print the version and exit.
    -h, --help                       Print this help.

Common commands:
     box             manages boxes: installation, removal, etc.
     connect         connect to a remotely shared Vagrant environment
     destroy         stops and deletes all traces of the vagrant machine
     global-status   outputs status Vagrant environments for this user
     halt            stops the vagrant machine
     help            shows the help for a subcommand
     init            initializes a new Vagrant environment by creating a Vagrantfile
     login           log in to HashiCorp's Atlas
     package         packages a running vagrant environment into a box
     plugin          manages plugins: install, uninstall, update, etc.
     port            displays information about guest port mappings
     powershell      connects to machine via powershell remoting
     provision       provisions the vagrant machine
     push            deploys code in this environment to a configured destination
     rdp             connects to machine via RDP
     reload          restarts vagrant machine, loads new Vagrantfile configuration
     resume          resume a suspended vagrant machine
     share           share your Vagrant environment with anyone in the world
     snapshot        manages snapshots: saving, restoring, etc.
     ssh             connects to machine via SSH
     ssh-config      outputs OpenSSH valid configuration to connect to the machine
     status          outputs status of the vagrant machine
     suspend         suspends the machine
     up              starts and provisions the vagrant environment
     version         prints current and latest Vagrant version

For help on any individual command run `vagrant COMMAND -h`

Additional subcommands are available, but are either more advanced
or not commonly used. To see all subcommands, run the command
`vagrant list-commands`.

provision.sh should be placed in the same folder 
The contents are :

#!/usr/bin/env bash

# Some provisioning variables
node_version="5.7.1"
n_directory="/opt/n/xyz"
app_directory="/home/vagrant/xyz"

echo "--- Running vagrant-provision.sh ---"

echo "--- Updating apt ---"
sudo apt-get update

echo "--- Installing git ---"
sudo apt-get install -y git

echo "--- Installing n (Node version management) ---"
sudo git clone $n_directory
cd $n_directory
git checkout

sudo make install

echo "--- Installing node.js and npm using n ---"

echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
mkdir ~/local
mkdir ~/node-latest-install
cd ~/node-latest-install
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=~/local
make install # ok, fine, this step probably takes more than 30 seconds...
curl https://www.npmjs.org/install.sh | sh

echo "--- Installing curl ---"
sudo apt-get install -y curl

curl https://raw.githubusercontent.com/creationix/nvm/v0.11.1/install.sh | bash

cd $app_directory

source ~/.profile
echo "--- Installing nvm ---"

sudo nvm install 0.12.15

nvm use 0.12.15

echo "--- Installing app dependencies (npm install) ---"
echo "This could take a while..."

echo "--- Installing npm ---"
sudo apt-get install -y npm 
echo "--- Installing typings ---"

sudo npm install typings --global
echo "--- npm install ---"

sudo npm install


echo "--- done ---"