Final works on vagrant voltha-base box with BUILD.md and Makefile

Change-Id: Id4b1e1a5dbc07eb15a106a5bb86967dceb978f15
diff --git a/vagrant-base/BUILD.md b/vagrant-base/BUILD.md
index 248fc9f..c145c02 100644
--- a/vagrant-base/BUILD.md
+++ b/vagrant-base/BUILD.md
@@ -7,9 +7,9 @@
 
 ## What triggers the need for a new voltha-base
 
-```
-WHAT WE NEED TO CONSIDER AS A REASON TO CREATE A NEW IMAGE
-```
+* Any change in the Vagrantfile means that we need a new image, hence a new version.
+* Any changes in the libraries/utilities we use, like Ansible, we need to have a new image, hence a new version.
+* Any structural change in the project means we need to create a new image, hence a new version.
 
 ## The process of building and testing the new voltha base image
 
@@ -19,7 +19,6 @@
 * Working installation of Vagrant  -- see [https://www.vagrantup.com/downloads.html](https://www.vagrantup.com/downloads.html)
 * jq -- a useful command line too to work with JSON data. On the MAC, you can install jq with ```brew install jq```; on Ubuntu you can do it with ```sudo apt-get install jq```. You will not regret it.
 
-
 ### Build
 
 If you have not cloned Voltha, it's time to do it now. If you just want to check it out, you can do it anonymously:
@@ -62,7 +61,7 @@
 simply run `vagrant up`.
 ```
 
-### Test
+### Test (Optional)
 
 Once image is created and machine is running, as mentioned in previous steps, we can run the following script and test the sanity of the image before uploading it into Vagrant cloud.
 
@@ -75,32 +74,52 @@
 Once vagrant image is created, we can use this base image and package it and create a vagrant box of it.
 
 ```
+# If it exists, remove 'package.box' 
+rm -rf package.box
+
+# package the vagrant box
 vagrant package
 ```
 
 This should create a file named ```package.box```
 
-### Creating a new version on the vagrant cloud
+### Generate your version
 
-At this point we need to create a new version and POST it in the Vagrant Cloud. To do so, we need to create a version.
+To do so, we use versions using current data/time in this format <YYMMDD.HHMM.0>.
 
 ```
-curl https://atlas.hashicorp.com/api/v1/box/rrashidi/voltha-base/versions -X POST -H "X-Atlas-Token: <DEDICATED_VOLTHA_BASE_TOKEN>" -d version[version]='<YOUR_DESIRED_VERSION>' -d version[description]='This is your description' | jq
+DESIRED_VERSION=$(date +"%y%m%d.%H%M%S.0")
+```
+
+### Retrieve voltha-base token
+
+To do so, we use the previously generated token.
+
+```
+DEDICATED_VOLTHA_BASE_TOKEN=$(cat vagrant-token)
+```
+
+### Creating a new version on the vagrant cloud
+
+At this point we need to create a new version and POST it on the Vagrant Cloud. To do so, we need to create a version.
+
+```
+curl https://atlas.hashicorp.com/api/v1/box/voltha/voltha-base/versions -X POST -H "X-Atlas-Token: $DEDICATED_VOLTHA_BASE_TOKEN" -d version[version]=$DESIRED_VERSION -d version[description]='This is your description' | jq
 ```
 
 The result should look something like this:
 
 ```
 {
-  "version": "<YOUR_DESIRED_VERSION>",
+  "version": "<DESIRED_VERSION>",
   "status": "unreleased",
   "description_html": "<p>This is your description</p>\n",
   "description_markdown": "This is your description",
   "created_at": "2016-11-29T15:35:21.103Z",
   "updated_at": "2016-11-29T15:35:21.103Z",
-  "number": "<YOUR_DESIRED_VERSION>",
-  "release_url": "https://atlas.hashicorp.com/api/v1/box/rrashidi/voltha-base/version/<YOUR_DESIRED_VERSION>/release",
-  "revoke_url": "https://atlas.hashicorp.com/api/v1/box/rrashidi/voltha-base/version/<YOUR_DESIRED_VERSION>/revoke",
+  "number": "<DESIRED_VERSION>",
+  "release_url": "https://atlas.hashicorp.com/api/v1/box/voltha/voltha-base/version/<DESIRED_VERSION>/release",
+  "revoke_url": "https://atlas.hashicorp.com/api/v1/box/voltha/voltha-base/version/<DESIRED_VERSION>/revoke",
   "providers": []
 }
 ```
@@ -109,7 +128,7 @@
 Now, we need to create a provider for the newly-created version. We use VirtualBox as the provider.
 
 ```
-url https://atlas.hashicorp.com/api/v1/box/rrashidi/voltha-base/version/<YOUR_DESIRED_VERSION>/providers -X POST -H "X-Atlas-Token: <DEDICATED_VOLTHA_BASE_TOKEN>" -d provider[name]='virtualbox' | jq
+curl https://atlas.hashicorp.com/api/v1/box/voltha/voltha-base/version/$DESIRED_VERSION/providers -X POST -H "X-Atlas-Token: $DEDICATED_VOLTHA_BASE_TOKEN" -d provider[name]='virtualbox' | jq
 ```
 
 The result should look something like this:
@@ -122,7 +141,7 @@
   "original_url": null,
   "created_at": "2016-11-29T16:00:17.604Z",
   "updated_at": "2016-11-29T16:00:17.604Z",
-  "download_url": "https://atlas.hashicorp.com/rrashidi/boxes/voltha-base/versions/<YOUR_DESIRED_VERSION>/providers/virtualbox.box"
+  "download_url": "https://atlas.hashicorp.com/voltha/boxes/voltha-base/versions/<DESIRED_VERSION>/providers/virtualbox.box"
 }
 ```
 
@@ -131,7 +150,7 @@
 We need to upload the package.box file for the provider.
 
 ```
-curl 'https://atlas.hashicorp.com/api/v1/box/rrashidi/voltha-base/version/<YOUR_DESIRED_VERSION>/provider/virtualbox/upload?access_token=<DEDICATED_VOLTHA_BASE_TOKEN>' | jq
+curl https://atlas.hashicorp.com/api/v1/box/voltha/voltha-base/version/$DESIRED_VERSION/provider/virtualbox/upload?access_token=$DEDICATED_VOLTHA_BASE_TOKEN | jq
 ```
 
 The result should look something like this:
@@ -143,31 +162,33 @@
 }
 ```
 
-Then, we upload the .box file using the upload_path.
+Then, we upload the .box file using the upload_path. This action may take some minutes.
 
 ```
-curl -X PUT --upload-file package.box https://binstore-test.hashicorp.com/a5bfcdf9-609b-4e8f-b5b8-d9ebdea4d2c6
+curl -X PUT --upload-file package.box <upload_path from_previous_curl_command>
+
+# For example: curl -X PUT --upload-file package.box https://binstore-test.hashicorp.com/a5bfcdf9-609b-4e8f-b5b8-d9ebdea4d2c6
 ```
 
 Now that provider of the version and the vagrant image package is ready, we need to realese the version. To release a version to be accessible to all the users, we should use the provided release-url.
 
 ```
-curl https://atlas.hashicorp.com/api/v1/box/rrashidi/voltha-base/version/<YOUR_DESIRED_VERSION>/release -X PUT -H "X-Atlas-Token: <DEDICATED_VOLTHA_BASE_TOKEN>" | jq
+curl https://atlas.hashicorp.com/api/v1/box/voltha/voltha-base/version/$DESIRED_VERSION/release -X PUT -H "X-Atlas-Token: $DEDICATED_VOLTHA_BASE_TOKEN" | jq
 ```
 
 The result should look something like this:
 
 ```
 {
-  "version": "<YOUR_DESIRED_VERSION>",
+  "version": "<DESIRED_VERSION>",
   "status": "active",
   "description_html": "<p>This is your description</p>\n",
   "description_markdown": "This is your description",
   "created_at": "2016-11-29T15:48:50.809Z",
   "updated_at": "2016-11-29T16:15:14.471Z",
   "number": "0.1.3",
-  "release_url": "https://atlas.hashicorp.com/api/v1/box/rrashidi/voltha-base/version/<YOUR_DESIRED_VERSION>/release",
-  "revoke_url": "https://atlas.hashicorp.com/api/v1/box/rrashidi/voltha-base/version/<YOUR_DESIRED_VERSION>/revoke",
+  "release_url": "https://atlas.hashicorp.com/api/v1/box/voltha/voltha-base/version/<DESIRED_VERSION>/release",
+  "revoke_url": "https://atlas.hashicorp.com/api/v1/box/voltha/voltha-base/version/<DESIRED_VERSION>/revoke",
   "providers": [
     {
       "name": "virtualbox",
@@ -176,11 +197,35 @@
       "original_url": null,
       "created_at": "2016-11-29T16:00:17.604Z",
       "updated_at": "2016-11-29T16:00:17.604Z",
-      "download_url": "https://atlas.hashicorp.com/rrashidi/boxes/voltha-base/versions/<YOUR_DESIRED_VERSION>/providers/virtualbox.box"
+      "download_url": "https://atlas.hashicorp.com/voltha/boxes/voltha-base/versions/<DESIRED_VERSION>/providers/virtualbox.box"
     }
   ]
 }
 ```
 
-Following the execution of this command, the new version gets released. As the result, the previous version is not currently a released version. 
+Following the execution of this command, the new version is released. As the result, the previous version is not currently a released version. 
 
+## Add/Update voltha-base vagrant box to your local system (Optional)
+
+This part is optional and can be used to see if the newly created vagrant box (image) is available and usable. 
+
+* If voltha-base vagrant box is not available (ON YOUR LOCAL MACHINE), UPDATE operation produces as error message. To fix it, please ADD the voltha-base using the command provided below.
+* If voltha-base vagrant box is already available (ON YOUR LOCAL MACHINE), ADD operation produces as error message. To fix it, please UPDATE the voltha-base using the command provided below.
+
+###To ADD voltha-base vagrant box:
+```
+vagrant box add voltha/voltha-base
+```
+
+###To UPDATE voltha-base vagrant box:
+```
+vagrant box update --box voltha/voltha-base
+```
+
+## Clean up the vagrant VM (Optional)
+
+To make sure the vagrant VM is stopped and removed, the following commands can be used.
+ 
+ ```
+ vagrant halt; vagrant destroy -f;
+ ```
diff --git a/vagrant-base/Makefile b/vagrant-base/Makefile
index ba025c6..b0c7f65 100644
--- a/vagrant-base/Makefile
+++ b/vagrant-base/Makefile
@@ -30,6 +30,48 @@
 PROTOC_DOWNLOAD_URI := $(PROTOC_DOWNLOAD_PREFIX)/v$(PROTOC_VERSION)/$(PROTOC_TARBALL)
 PROTOC_BUILD_TMP_DIR := "/tmp/protobuf-build-$(shell uname -s | tr '[:upper:]' '[:lower:]')"
 
+TOKEN_FILE := vagrant-token
+TOKEN := `cat $(TOKEN_FILE)`
+
+VAGRANT_VOLTHA_BASE_INFO="$(shell vagrant box list | grep voltha-base | tail -n 1 | cut -d ',' -f 2 | cut -d ' ' -f 2)"
+CURRENT_VERSION=`echo $(VAGRANT_VOLTHA_BASE_INFO) | cut -d ')' -f 1`
+CURRENT_VERSION_DYNAMIC=`echo $$(vagrant box list | grep voltha-base | tail -n 1 | cut -d ',' -f 2 | cut -d ' ' -f 2) | cut -d ')' -f 1`
+TIME := $(shell date +"%y%m%d.%H%M%S.0")
+VERSION := `echo $(TIME)`
+VERSION_DYNAMIC := `echo $$(date +"%y%m%d.%H%M%S.0")`
+
+default: build
+
+build: add-update-voltha-base build-core
+
+print-info:
+	@echo "TOKEN ---------------------->  "$(TOKEN)
+	@echo "TIME ----------------------->  "$(TIME)
+	@echo "VERSION -------------------->  "$(VERSION)
+	@echo "VERSION_DYNAMIC ------------>  "$(VERSION_DYNAMIC)
+	@echo "CURRENT_VERSION ------------>  "$(CURRENT_VERSION)
+	@echo "CURRENT_VERSION_DYNAMIC ---->  "$(CURRENT_VERSION_DYNAMIC)
+
+help:
+	@echo
+	@echo "Usage: make [<target>] [VERSION='X.X.X']"
+	@echo "where available targets are:"
+	@echo
+	@echo "help                 : Print this help"
+	@echo "build                : Build the Voltha-base vagrant box and upload it to Vagrant Cloud (default target)"
+	@echo "build-core           : Execute the main tasks to build theVoltha-base vagrant box and upload it to Vagrant Cloud (default target)"
+	@echo "clean                : Stop/Remove the Voltha-base virtual machine"
+	@echo "start-message        : Print Voltha-base banner"
+	@echo "update-voltha-base   : Update the Voltha-base (voltha/voltha-base) box to the latest version"
+	@echo "test-voltha-base     : Test the Voltha-base vagrant image"
+	@echo "run-voltha-base      : Run the Voltha-base (vagrant up)"
+	@echo "package-voltha-base  : Package the Voltha-base vagrant image to a .box package"
+	@echo "create-version       : Create a new version on Vagrant Cloud"
+	@echo "create-provider      : Create & assign an provider to a version on Vagrant Cloud"
+	@echo "upload-package       : Upload a package.box file to Vagrant Cloud and assign it to a version and provider"
+	@echo "release-version      : Release a verson as the available version of the Voltha-base on Vagrant Cloud"
+	@echo
+
 $(PROTOC):
 	@echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
 	@echo "It looks like you don't have protocol buffer tools installed."
@@ -50,4 +92,88 @@
 	    cd $(PROTOC_DIR); \
 	    ./configure --prefix=$(PROTOC_PREFIX); \
 	    make; \
-	    sudo make install
\ No newline at end of file
+	    sudo make install
+
+build-core: banner run-voltha-base package-voltha-base create-version create-provider upload-package release-version clean update-voltha-base
+
+add-update-voltha-base:
+	@echo "Adding/Updating voltha-base vagrant box"; \
+	vagrant box update --box voltha/voltha-base || vagrant box add voltha/voltha-base
+
+update-voltha-base:
+	@echo "Updating voltha-base vagrant box"; \
+	vagrant box update --box voltha/voltha-base
+
+add-voltha-base:
+	@echo "Adding voltha-base vagrant box"; \
+	vagrant box add voltha/voltha-base
+
+remove-voltha-base:
+	@echo "Removing all the voltha-base vagrant box versions"; \
+	vagrant box remove voltha/voltha-base --all
+
+banner:
+	@echo
+	@echo "|------------------------------------------------------------------------|"
+	@echo "|  _    ______  __  ________  ______           ____   ______ _____ ____  |"
+	@echo "| | |  / / __ \/ / /_  __/ / / / /| |         / ___ \/ ___  / ____/ ___| |"
+	@echo "| | | / / / / / /   / / / /_/ / /_| |  ___   / /__/ / /__/ / /___  /___  |"
+	@echo "| | |/ / /_/ / /___/ / / __  / ___  | |___| / /__/ / ___   \__  / /____  |"
+	@echo "| |___/\____/_____/_/ /_/ /_/_/  |__|      /______/_/  /_/\____/_______| |"
+	@echo "|                                                                        |"
+	@echo "|------------------------------------------------------------------------|"
+	@echo "|                MAKING & UPLOADING VOLTHA-BASE VAGRANT BOX              |"
+	@echo "|------------------------------------------------------------------------|"
+	@echo
+	@echo "VERSION ----> $(VERSION)"
+
+test-voltha-base:
+	@echo "Testing the recently created voltha-base"; \
+	vagrant up; \
+		./test_script.sh
+
+run-voltha-base:
+	@echo "Running voltha-base vagrant box"
+	@echo "Stopping any running voltha-base vagrant box"; \
+	vagrant halt; \
+		vagrant destroy -f; \
+		vagrant up
+
+package-voltha-base:
+	@echo "Packaging vagrant VM"
+	@echo "Removing previous package.box, if exists"; \
+	rm package.box; \
+		vagrant package
+
+create-version:
+	@echo "Creating a new version on Vagrant Cloud"
+	@echo "Version: $(VERSION)"
+	curl https://atlas.hashicorp.com/api/v1/box/voltha/voltha-base/versions \
+		-X POST -H "X-Atlas-Token: $(TOKEN)" \
+		-d version[version]=$(VERSION) \
+		-d version[description]='This is the new version' | jq
+
+create-provider:
+	@echo "Creating a provider for the new version on Vagrant Cloud"
+	@echo "We have virtualbox as provider"; \
+	curl https://atlas.hashicorp.com/api/v1/box/voltha/voltha-base/version/$(VERSION)/providers \
+		-X POST -H "X-Atlas-Token: $(TOKEN)" \
+		-d provider[name]='virtualbox' | jq
+
+upload-package:
+	@echo "Uploading the new package.box on Vagrant Cloud"; \
+	echo $(VERSION); \
+		url=$(shell curl https://atlas.hashicorp.com/api/v1/box/voltha/voltha-base/version/$(VERSION)/provider/virtualbox/upload?access_token=$(TOKEN) | jq '.["upload_path"]'); \
+			echo $$url; \
+			echo 'WAIT FOR IT....'; \
+			curl -X PUT --upload-file package.box $$url
+
+release-version:
+	@echo "Releasing the version, so the box will be accessible to all users"; \
+	curl https://atlas.hashicorp.com/api/v1/box/voltha/voltha-base/version/$(VERSION)/release \
+		-X PUT -H "X-Atlas-Token: $(TOKEN)" | jq
+
+clean:
+	@echo "Cleaning up the voltha-base vagrant image"; \
+	vagrant halt; \
+		vagrant destroy -f
diff --git a/vagrant-base/Vagrantfile b/vagrant-base/Vagrantfile
index fea11f1..5ee2d13 100644
--- a/vagrant-base/Vagrantfile
+++ b/vagrant-base/Vagrantfile
@@ -10,10 +10,13 @@
   end
 
   config.vm.define "voltha" do |d|
-    d.vm.box = "rrashidi/voltha-base"
+    d.vm.box = "voltha/voltha-base"
     d.vm.hostname = "voltha"
     d.vm.network "private_network", ip: "10.100.198.220"
     d.vm.provision :shell, inline: "cd /voltha && source env.sh && chmod 777 /tmp/fluentd"
+    d.vm.provider "virtualbox" do |v|
+      v.name = "voltha"
+    end
   end
 
   config.ssh.shell = "bash -c 'BASH_ENV=/etc/profile exec bash'"