OpenVINO Ubuntu Xenial, Virtualbox and Vagrant Install, Intel NCS2 (Neural Compute Stick 2)

#OpenVINO Ubuntu Xenial, Virtualbox and Vagrant Install, Intel NCS2 (Neural Compute Stick 2)

Prerequsites

Download Latest VirtualBox from [https://www.virtualbox.org/wiki/Downloads] https://www.virtualbox.org/wiki/Downloads

Make sure to download extension pack

Oracle VM VirtualBox Extension Pack

Download and Install Vagrant from

[https://www.vagrantup.com/downloads.html] https://www.vagrantup.com/downloads.html

First step is to setup VM.

vagrant up

Vagrant is configured to apply all usb filters required to access Neural Compute Stick 1 and 2 inside Virtualbox Ubuntu Xenial VM

This will create VM on your host machine with name “OpenVinoVM”

This will also automatically download OpenVINO to /home/vagrant/openvino

Setup

Install OpenVINO Dependencies

cd /home/vagrant/openvino/l_openvino_toolkit_p_2019.1.094/ && sudo ./install_openvino_dependencies.sh

Install OpenVINO

cd /home/vagrant/openvino/l_openvino_toolkit_p_2019.1.094/ && sudo ./install.sh

This will have multiple manual steps like accepting license and selecting kind of installation

Default installation path the download configured in Vagrantfile

/opt/intel/openvino_2019.1.094

Setup Vars

source /opt/intel/openvino_2019.1.094/bin/setupvars.sh

echo “source /opt/intel/openvino_2019.1.094/bin/setupvars.sh” >> /home/vagrant/.bashrc

Install UDEV Rules

THese are required for USB to be activated and used

sh /opt/intel/openvino_2019.1.094/install_dependencies/install_NCS_udev_rules.sh

Update UDEV Rules are below

sudo vi /etc/udev/rules.d/97-myriad-usbboot.rules

I modified MODE to 666 from 660. OpenVINO has 660 as default.

SUBSYSTEM==“usb”, ATTRS{idProduct}“2150”, ATTRS{idVendor}“03e7”, GROUP=“users”, MODE=“0666”, ENV{ID_MM_DEVICE_IGNORE}=“1”

SUBSYSTEM==“usb”, ATTRS{idProduct}“2485”, ATTRS{idVendor}“03e7”, GROUP=“users”, MODE=“0666”, ENV{ID_MM_DEVICE_IGNORE}=“1”

SUBSYSTEM==“usb”, ATTRS{idProduct}“f63b”, ATTRS{idVendor}“03e7”, GROUP=“users”, MODE=“0666”, ENV{ID_MM_DEVICE_IGNORE}=“1”

Reload UDEV

sudo udevadm control –reload-rules && sudo udevadm trigger && sudo ldconfig

Configure Model Optimizer

cd /opt/intel/openvino_2019.1.094/deployment_tools/model_optimizer/install_prerequisites && sudo ./install_prerequisites.sh

If you dont want to run optimizer for all different kind of model you can choose specific optimizer

Example: sudo ./install_prerequisites_caffe.sh ( For Caffe Model)

Example: sudo ./install_prerequisites_tf.sh ( For Tensorflow Model)

Verify if USB is attached

Type lsusb

You should see some USB device with vendor id like

Bus 002 Device 002: ID 03e7:2485

Finally Test

cd /opt/intel/openvino_2019.1.094/deployment_tools/demo && ./demo_squeezenet_download_convert_run.sh

This should print something like this

Image /opt/intel/openvino_2019.1.094/deployment_tools/demo/car.png

classid probability label
817 0.8363345 sports car, sport car
511 0.0946488 convertible
479 0.0419131 car wheel
751 0.0091071 racer, race car, racing car
436 0.0068161 beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon
656 0.0037564 minivan
586 0.0025741 half track
717 0.0016069 pickup, pickup truck
864 0.0012027 tow truck, tow car, wrecker
581 0.0005882 grille, radiator grille

total inference time: 11.7261708

Average running time of one iteration: 11.7261708 ms

Throughput: 85.2793311 FPS

Downloading Public Model and Running Test

cd /opt/intel/openvino_2019.1.094/deployment_tools/tools/model_downloader/

List public models that are known to work with OpenVINO

python3 downloader.py –print_all

Download a specific model, say GoogLeNet V2

Make sure vagrant has access to folder

sudo chmod -R 777 /opt/intel/openvino_2019.1.094/deployment_tools/tools/model_downloader/

python3 downloader.py –name googlenet-v2

Convert Pretained Model to IR ( Intermediate Represenation. This can run on multiple hardware)

cd /opt/intel/openvino_2019.1.094/deployment_tools/tools/model_downloader/classification/googlenet/v2/caffe

Use model optimizer to convert googlenet.caffemodel to IR

/opt/intel/openvino_2019.1.094/deployment_tools/model_optimizer/mo.py –data_type FP16 –input_model googlenet-v2.caffemodel –input_proto googlenet-v2.prototxt

Deploy the converted IR model onto Intel NCS 2 using the toolkit’s IE API

cd /opt/intel/openvino_2019.1.094/deployment_tools/inference_engine/samples/python_samples

Download a test image from the internet

sudo wget -N https://upload.wikimedia.org/wikipedia/commons/b/b6/Felis_catus-cat_on_snow.jpg

Run an inference on this image using a built-in sample code

python3 classification_sample/classification_sample.py -m /opt/intel/openvino_2019.1.094/deployment_tools/tools/model_downloader/classification/googlenet/v2/caffe/./googlenet-v2.xml -i Felis_catus-cat_on_snow.jpg -d MYRIAD

This should give results like

Image Felis_catus-cat_on_snow.jpg

classid probability
173 0.4843750
54 0.2985840
7 0.1647949
200 0.0359497
66 0.0035839
10 0.0024872
473 0.0024281
84 0.0016794
198 0.0014591
152 0.0006762

Look at [GIST] https://gist.github.com/ashwinrayaprolu1984/7245a37b86e5fd1920f8e4409e276132

Next write up will be on Image Classification Using OpenCV and OpenVINO

 

QuickBooks and Sage Data Exporter

Many small and medium business work with QuickBooks for their accounting module.

Similarly many companies also use Sage for their accounting.  Most of them face problem when they need to export data out of these systems.

Many of the connectors that are available online are costly and don’t meet exact requirements.

Attached is small snippets of code that explains what was done to export their data into CSV . I’ve also attached github links to download the code.

Both SAGE and Quickbooks come with ODBC Drivers that can be configured and programmatically queried

#Sage

Create a silent ODBC DSN in your ODBC DataSource.

SageODBC1

Configure silent mode in Options Tab.

SageODBC2.png

Now we are going to use below datasource to load and export data.

We are going to DotNet Core to write our code as Its best language to talk to DSN on windows

I broke the problem into 3 different parts

  1. Load TableNames from database
  2. Load DataSet for each Table
  3. Export each table to CSV from DataSet

       private static List loadTableNames(string connectionString){
            var tableNames = new List();

            using (OdbcConnection connection =
					   new OdbcConnection(connectionString))
			{
                try
				{
					connection.Open();
					using(DataTable tableschema = connection.GetSchema("Tables"))
                    {
                        // first column name
                        foreach(DataRow row in tableschema.Rows)
                        {
                            tableNames.Add(row["TABLE_NAME"].ToString());
                            //Console.WriteLine(row["TABLE_NAME"].ToString());
                        }
                    }
				}
				catch (Exception ex)
				{
					Console.WriteLine(ex.Message);
				}

            }

            return tableNames;

        }

Now we need to write code to load data for given table. I’m going to use DataSet in this case. There are many ways to do this..


       public static DataSet GetDataSetFromAdapter(
			DataSet dataSet, string connectionString, string queryString)
		{
			using (OdbcConnection connection =
					   new OdbcConnection(connectionString))
			{

				OdbcDataAdapter adapter =
					new OdbcDataAdapter(queryString, connection);

				// Open the connection and fill the DataSet.
				try
				{
					connection.Open();
					adapter.Fill(dataSet);
				}
				catch (Exception ex)
				{
					Console.WriteLine(ex.Message);
				}
				// The connection is automatically closed when the
				// code exits the using block.
			}
			return dataSet;
		}        

And finally below is function to export all data to CSV

<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;">&#65279;</span>
        private static string ConvertToCSV(DataSet objDataSet)
        {
            StringBuilder content = new StringBuilder();

            if (objDataSet.Tables.Count &gt;= 1)
            {
                DataTable table = objDataSet.Tables[0];

                if (table.Rows.Count &gt; 0)
                {
                    DataRow dr1 = (DataRow) table.Rows[0];
                    int intColumnCount = dr1.Table.Columns.Count;
                    int index=1;

                    //add column names
                    foreach (DataColumn item in dr1.Table.Columns)
                    {
                        content.Append(String.Format("\"{0}\"", item.ColumnName));
                        if (index &lt; intColumnCount)
                            content.Append(&quot;,&quot;);
                        else
                            content.Append(&quot;\r\n&quot;);
                        index++;
                    }

                    //add column data
                    foreach (DataRow currentRow in table.Rows)
                    {
                        string strRow = string.Empty;
                        for (int y = 0; y &lt;= intColumnCount - 1; y++)
                        {
                            strRow += &quot;\&quot;&quot; + currentRow[y].ToString() + &quot;\&quot;&quot;;

                            if (y = 0)
                                strRow += ",";
                        }
                        content.Append(strRow + "\r\n");
                    }
                }
            }

            return content.ToString();
        }

https://github.com/ashwinrayaprolu1984/SageDataExporter.git

#QuickBooks

We follow same approach for QuickBooks.

  1. Load TableNames from file ( Quickbooks doesn’t export Schema in its ODBC Datasource)
  2. Load DataSet for each Table
  3. Export each table to CSV from DataSet

Below link in git hub has code to do this

https://github.com/ashwinrayaprolu1984/QuickBooksDesktopConnector.git

 

Invoking Ansible Inside Python Flask

Pasting sample code that can be used as is to run ansible inside python flask app.
This would be a very useful base implementation when devops need to control system over the web or someone tries to control systems over internet without cli

Prerequisites

Install Python3

#PYTHON3 ( Assuming Centos7 or RHEL Flavor). Change accordingly for your env
yum -y install https://centos7.iuscommunity.org/ius-release.rpm
yum -y install python36u
yum -y install python36u-pip
yum -y install python36u-devel

virtualenv –python=/usr/bin/python3.6 venv

 

from flask import (
    Blueprint
)

import json
import shutil
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager
from ansible.playbook.play import Play
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.plugins.callback import CallbackBase
import ansible.constants as C


#Blueprint URL Configuration routes
bp = Blueprint('AnsibleController', __name__, url_prefix='/AnsibleController')


# Ansible Return callback
class ResultCallback(CallbackBase):
    """A sample callback plugin used for performing an action as results come in

    If you want to collect all results into a single object for processing at
    the end of the execution, look into utilizing the ``json`` callback plugin
    or writing your own custom callback plugin
    """
    def __init__(self):
        self.resp = []

    def v2_runner_on_ok(self, result, **kwargs):
        """Print a json representation of the result

        This method could store the result in an instance attribute for retrieval later
        """
        host = result._host
        self.resp.append(json.dumps({host.name: result._result}, indent=4))





#print (json.dumps(out, sort_keys=True, indent=4, separators=(',', ': ')))

@bp.route('/ping', methods=('GET', 'POST'))
def ping():
    # since API is constructed for CLI it expects certain options to always be set, named tuple 'fakes' the args parsing options object
    Options = namedtuple('Options',
                         ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check',
                          'diff'])
    options = Options(connection='local', module_path=['/etc/swiftui/.swiftui/ansible/mymodules'], forks=100, become=None, become_method=None,
                      become_user=None, check=False, diff=False)

    # initialize needed objects
    loader = DataLoader()  # Takes care of finding and reading yaml, json and ini files
    passwords = dict(vault_pass='secret')

    # Instantiate our ResultCallback for handling results as they come in. Ansible expects this to be one of its main display outlets
    results_callback = ResultCallback()

    # create inventory, use path to host config file as source or hosts in a comma separated string
    inventory = InventoryManager(loader=loader, sources='localhost,')

    # variable manager takes care of merging all the different sources to give you a unifed view of variables available in each context
    variable_manager = VariableManager(loader=loader, inventory=inventory)

    # create datastructure that represents our play, including tasks, this is basically what our YAML loader does internally.
    play_source = dict(
        name="Ansible Play",
        hosts='localhost',
        gather_facts='no',
        tasks=[
            dict(action=dict(module='shell', args='ls'), register='shell_out'),
            dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')))
        ]
    )

    # Create play object, playbook objects use .load instead of init or new methods,
    # this will also automatically create the task objects from the info provided in play_source
    play = Play().load(play_source, variable_manager=variable_manager, loader=loader)

    # Run it - instantiate task queue manager, which takes care of forking and setting up
    # all objects to iterate over host list and tasks
    tqm = None
    try:
        tqm = TaskQueueManager(
            inventory=inventory,
            variable_manager=variable_manager,
            loader=loader,
            options=options,
            passwords=passwords,
            stdout_callback=results_callback,
            # Use our custom callback instead of the ``default`` callback plugin, which prints to stdout
        )
        result = tqm.run(play)  # most interesting data for a play is actually sent to the callback's methods
    finally:
        # we always need to cleanup child procs and the structres we use to communicate with them
        if tqm is not None:
            tqm.cleanup()

        # Remove ansible tmpdir
        shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)

    return ''.join(results_callback.resp)

 

Amazon Kindle Hack and Download PDF/Image

I usually try to hack/break websites by automating page traversal’s
There are different levels of security in websites

  1. Data Security which is generally taken care by HTTPS
  2. User Authentication for which we generally have session tokens like JSESSIONID,PHPSESSIONID,ASPSESSIONID or Highly scalable approaches like JSON Web Token
  3. Authorization is mostly custom built but there are some standard processes like OAuth or SAML
  4. Code security which is generally taken care URL Masking, Obfuscation of Javascript/html elements
  5. And adding hidden form elements which maintain state which gets exchanged between server and client
  6. Apart from this other kinds of security that can take care of same origin scripts

Even after taking care of all these website that publish copyright content face a different kind of issue.

7. How not to allow user to download/copy/save the copyrighted material on his machine.

My current topic is related to above issue. I assumed google/amazon and other providers which publish copyrighted material online have taken care of this already .

I just wanted to make a simple testcase and found that none of them are secure.

So Authors beware of hackers with similar skillset 

I’ve used simple Utilities that are well known in automation world.

Selenium and CasperJS to test this and found that it just takes make be 1-2 hours to break them.

Problem with these site’s is that they are not at all designed to cover the 7th point in all angles.

Pasting sample code that downloads books from kindle and save’s as PDF

Note: I’m capturing images (Which is increasing size of pdf . I could have directly converted to pdf and merged all pages, But my scope is not effectiveness)


package com.thoughtlane.experiments.kindlehack;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * @author ashwinrayaprolu
 *
 */
public class AmazonKindleBookDownloader {
	public static final String DEST = "results/pdf/multiple_images.pdf";

	/**
	 * @param args
	 */
	public static void main(String... args) {
		System.setProperty("webdriver.chrome.driver", new File("driver/chromedriver").getAbsolutePath());
		File resourcesFolder = new File("resources");

		WebDriver driver = new ChromeDriver();

		driver.manage().window().maximize();
		driver.manage().window().setSize(new Dimension(1279, 682));
		driver.navigate().to("http://read.amazon.com");

		waitSomeTime();

		String appTitle = driver.getTitle();
		System.out.println("Application title is :: " + appTitle);

		driver.findElement(By.id("ap_email")).sendKeys("AMAZON_USERNAME");
		driver.findElement(By.id("ap_password")).sendKeys("AMAZON_PASSWORD");
		driver.findElement(By.id("signInSubmit-input")).click();

		waitSomeTime();

		File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
		try {
			FileUtils.copyFile(screenshot, new File(resourcesFolder, "HomePage.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		driver.switchTo().frame("KindleLibraryIFrame");

		WebElement element = driver.findElement(By.cssSelector("span#kindle_dialog_firstRun_button.chrome_btn"));

		element.click();

		waitSomeTime();

		screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
		try {
			FileUtils.copyFile(screenshot, new File(resourcesFolder, "HomePage2.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		List<WebElement> books = driver.findElements(By.cssSelector("img.book_image.book_click_area"));

		
		/***
		 * Iterate over all books on the dashboard/home page
		 */
		for (WebElement book : books) {

			String bookTitle = book.getAttribute("title").substring(0, 24);
			book.click();

			File bookFolder = new File(resourcesFolder, "" + bookTitle.replaceAll(" ", ""));
			bookFolder.mkdirs();

			try {
				FileUtils.writeStringToFile(new File(resourcesFolder, "HomePage.html"), driver.getPageSource(), Charset.defaultCharset());
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			driver.switchTo().parentFrame();
			driver.switchTo().frame("KindleReaderIFrame");

			// WebElement menuLink =
			// driver.findElement(By.cssSelector("div#kindleReader_button_goto.header_bar_icon"));

			// Actions actions = new Actions(driver);
			// actions.moveToElement(menuLink);

			// menuLink.click();
			waitSomeTime();

			// Goto Cover Page to start capturing
			// WebElement coverLink =
			// driver.findElement(By.cssSelector("div#kindleReader_goToMenuItem_goToCover"));
			// coverLink.click();

			// waitSomeTime();

			screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
			try {
				FileUtils.copyFile(screenshot, new File(bookFolder, "Cover.jpg"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			int pageNumber = 0;
			while (true) {
				pageNumber = pageNumber + 1;
				try {
					WebElement nextArrow = driver.findElement(By.cssSelector("div#kindleReader_pageTurnAreaRight.kindleReader_pageTurnArea.pageArrow"));
					if (nextArrow == null) {
						break;
					}

					nextArrow.click();

					waitSomeTime();

					screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
					try {
						FileUtils.copyFile(screenshot, new File(bookFolder, pageNumber + ".jpg"));
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				} catch (Exception e) {
					e.printStackTrace();
					break;
				}
			}

			try {
				createPdfFromImages(bookFolder.getAbsolutePath(), DEST);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		waitSomeTime(9000);

		screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
		try {
			FileUtils.copyFile(screenshot, new File(resourcesFolder, "SelectedBook.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		driver.quit();
	}

	/**
	 * @param sourceFolder
	 * @param dest
	 * @throws IOException
	 * @throws DocumentException
	 */
	private static void createPdfFromImages(String sourceFolder, String dest) throws IOException, DocumentException {
		File folder = new File(sourceFolder);

		File[] allFiles = folder.listFiles(new FilenameFilter() {

			@Override
			public boolean accept(File dir, String name) {
				if (name.endsWith("jpg")) {
					return true;
				}
				return false;
			}
		});

		Image img = null;
		Document document = new Document();
		PdfWriter.getInstance(document, new FileOutputStream(dest));
		document.open();
		for (File fileObj : allFiles) {
			img = Image.getInstance(fileObj.getAbsolutePath());
			document.setPageSize(img);
			document.newPage();
			img.setAbsolutePosition(0, 0);
			document.add(img);
		}
		document.close();
	}

	/**
	 * 
	 */
	private static void waitSomeTime() {
		try {
			Thread.sleep(7000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @param miliSecons
	 */
	private static void waitSomeTime(int miliSecons) {
		try {
			Thread.sleep(miliSecons);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}


Sample uncompleted code in casperjs. Also added code to handle multiple tries of Captcha


var casper = require('casper').create({
	verbose : true,
	logLevel : 'debug'
});
var system = require('system');
var mouse = require("mouse").create(casper);
var utils = require('utils');

casper.options.viewportSize = {
	width : 1366,
	height : 667
};

var last, list = [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ];

var userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36';
casper.userAgent(userAgent);


var fs = require('fs');
var myFolder      = fs.list(".");

//create array of just images 
try{
	for (var i = 0; i < myFolder.length; i++) {
		this.echo(myFolder[i]);
	    if (myFolder[i].indexOf("pdf") != -1) {
	       myFolder[i].remove();
	    }     
	}
} catch (e) {
	console.log(e);
}


/*******************************************************************************
 * Iterate over all options
 */
var cliOptions = casper.cli.options;

// casper.clear();


casper.on("remote.message", function(msg) {
	console.log(msg);
});

casper.start();

var userName = "AMAZON_USERNAME";
var password = "AMAZON_PASSWORD";

casper
		.thenOpen(
				'https://read.amazon.com',
				function() {

					this.echo("Login Page ");

				});

casper.each(list, function(self, i) {
	self.wait(700, function() {
		last = i;
		this.echo('Using this.wait ' + i);
	});
});

casper.then(function() {
	// casper.exit();
	try {
		casper.evaluate(function(userName, password) {
			document.getElementById("ap_email").value = userName;
			document.getElementById("ap_password").value = password;

			// document.aspnetForm.submit();
		}, userName, password);

	} catch (e) {
		console.log(e);
	}

	this.capture("LoginPage.pdf");
	// this.click('input#signInSubmit-input');
	this.mouse.click("input#signInSubmit-input");
});

casper.each(list, function(self, i) {
	self.wait(700, function() {
		last = i;
		this.echo('Using this.wait ' + i);
	});
});

casper.then(function() {

	// var html = this.getHTML();
	var html = this.getPageContent();
	var f = fs.open('HomePage.html', 'w');
	f.write(html);
	f.close();
	// fs.write('path/to/file', 'your string', 'w');
	this.capture("PostLoginPage.pdf");
	system.stdout.writeLine('Has CaptchaCode?: ');
	var hasCaptcha = system.stdin.readLine();
	
	if (hasCaptcha === 'y') {
		
		system.stdout.writeLine('Enter captcha?: ');
		var captcha = system.stdin.readLine();
		this.echo("Using Captcha " + captcha);
		
		try {
			casper.evaluate(function(userName, password,captcha) {
				document.getElementById("ap_email").value = userName;
				document.getElementById("ap_password").value = password;
				document.getElementById("ap_captcha_guess").value = captcha;
				// document.aspnetForm.submit();
			}, userName, password,captcha);

		} catch (e) {
			console.log(e);
		}
		
		this.capture("PostSecondTry.pdf");
		
		this.echo("Clicking Submit Button");
		this.mouse.click("input#signInSubmit-input");
		
		
	}else{
		this.capture("HomePage.pdf");
	}

	
});

casper.each(list, function(self, i) {
	self.wait(700, function() {
		last = i;
		this.echo('Using this.wait ' + i);
	});
});



casper.then(function() {

	// var html = this.getHTML();
	var html = this.getPageContent();
	var f = fs.open('HomePage2.html', 'w');
	f.write(html);
	f.close();
	// fs.write('path/to/file', 'your string', 'w');
	this.capture("PostLoginPage2.pdf");
	system.stdout.writeLine('Has CaptchaCode2?: ');
	var hasCaptcha = system.stdin.readLine();
	
	if (hasCaptcha === 'y') {
		
		system.stdout.writeLine('Enter captcha2?: ');
		var captcha = system.stdin.readLine();
		this.echo("Using Captcha2 " + captcha);
		
		try {
			casper.evaluate(function(userName, password,captcha) {
				document.getElementById("ap_email").value = userName;
				document.getElementById("ap_password").value = password;
				document.getElementById("ap_captcha_guess").value = captcha;
				// document.aspnetForm.submit();
			}, userName, password,captcha);

		} catch (e) {
			console.log(e);
		}
		
		this.capture("PostSecondTry2.pdf");
		
		this.echo("Clicking Submit Button");
		this.mouse.click("input#signInSubmit-input");
		
		
	}else{
		this.capture("HomePage2.pdf");
	}

	
});

casper.each(list, function(self, i) {
	self.wait(700, function() {
		last = i;
		this.echo('Using this.wait ' + i);
	});
});




casper.then(function() {

	// var html = this.getHTML();
	var html = this.getPageContent();
	var f = fs.open('HomePage3.html', 'w');
	f.write(html);
	f.close();
	// fs.write('path/to/file', 'your string', 'w');
	this.capture("PostLoginPage3.pdf");
	system.stdout.writeLine('Has CaptchaCode3?: ');
	var hasCaptcha = system.stdin.readLine();
	
	if (hasCaptcha === 'y') {
		
		system.stdout.writeLine('Enter captcha3?: ');
		var captcha = system.stdin.readLine();
		this.echo("Using Captcha3 " + captcha);
		
		try {
			casper.evaluate(function(userName, password,captcha) {
				document.getElementById("ap_email").value = userName;
				document.getElementById("ap_password").value = password;
				document.getElementById("ap_captcha_guess").value = captcha;
				// document.aspnetForm.submit();
			}, userName, password,captcha);

		} catch (e) {
			console.log(e);
		}
		
		this.capture("PostSecondTry3.pdf");
		
		this.echo("Clicking Submit Button");
		this.mouse.click("input#signInSubmit-input");
		
		
	}else{
		this.capture("HomePage3.pdf");
	}

	
});

casper.each(list, function(self, i) {
	self.wait(700, function() {
		last = i;
		this.echo('Using this.wait ' + i);
	});
});





casper.then(function() {
	var listItems = [];
	listItems = this.evaluate(function() {
		var nodes = document.querySelectorAll('span');
		return [].map.call(nodes, function(node) {
			return node.textContent;
		});
	});

	//this.echo(listItems);

	this.capture("HomePageFinal.pdf");
	// this.mouse.click("span#kindle_dialog_firstRun_button");

});

casper.run();


Merge Overlapping Interval

I recently came across a problem of merging data with overlapping lower and upperbound data. I tried multiple approaches.

Below code uses Double linked list approach


/**
 * 1. For the original DataNode2 we need to make sure we merge nodes when ever we see overlap
 *
 * @author Ashwin Rayaprolu
 *
 */
public class MergeInsertIntervals {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[][] originalIntervals = { { 94230, 94299 }, { 94289, 94699 }, { 94200, 94240 }, { 94133, 94133 } };
		// Sort our array based on lower bound number
		DataNode2LinkedList2 linkedList = new DataNode2LinkedList2();
		//O(n log n) operation
		for(int[] data:originalIntervals){
			linkedList.insert(new DataNode2(data));
		}

		System.out.println("---------Sorted?merged Intervals----------");
		int[][] sortedIntervals = linkedList.traverse();

		System.out.println("---------Merged Intervals----------");

	}

}

/**
 * @author Ashwin Rayaprolu
 *
 */
class DataNode2LinkedList2 {
	DataNode2 firstNode;
	DataNode2 lastNode;

	int size = 0;
	// O(log n) operation
	void insert(DataNode2 newNode) {
		if (firstNode == null) {
			firstNode = newNode;
			lastNode = newNode;
			return;
		}

		// Keep interval on left if lower bound is < than tempPointer
		DataNode2 tempPointer = firstNode;

		if (newNode.data[0] < tempPointer.data[0]) {
			while (tempPointer.leftPointer != null && newNode.data[0] < tempPointer.data[0]) { 				tempPointer = tempPointer.leftPointer; 			} 			 			//If new node is overlapping then merge with current node and return 			if(newNode.data[1]>=tempPointer.data[0]){
				//tempPointer.data[1]=
				tempPointer.data[0] = newNode.data[0];
				return;
			}

			newNode.rightPointer = tempPointer;

			if (tempPointer.leftPointer == null) {
				firstNode = newNode;
			}

			tempPointer.leftPointer = newNode;
			++size;

		} else {
			while (tempPointer.rightPointer != null && newNode.data[0] >= tempPointer.data[0]) {
				tempPointer = tempPointer.rightPointer;
			}

			//If new node is overlapping then merge with current node and return
			if(tempPointer.data[1]>=newNode.data[0]){
				//tempPointer.data[1]=
				tempPointer.data[1] = newNode.data[1];
				return;
			}

			newNode.leftPointer = tempPointer;

			if (tempPointer.rightPointer == null) {
				lastNode = newNode;
			}

			tempPointer.rightPointer = newNode;
			++size;

		}

	}

	int[][] traverse() {
		DataNode2 tempPointer = firstNode;
		int[][] sortedArray = new int[size + 1][2];
		int index = 0;
		while (tempPointer != null) {
			sortedArray[index] = tempPointer.data;
			++index;
			System.out.println("{" + tempPointer.data[0] + "," + tempPointer.data[1] + "}");
			tempPointer = tempPointer.rightPointer;
		}
		return sortedArray;
	}
}

/**
 * Data Node used for sorting
 *
 * @author Ashwin Rayaprolu
 *
 */
class DataNode2 {
	int[] data = {};
	DataNode2 leftPointer;
	DataNode2 rightPointer;

	public DataNode2(int[] data) {
		this.data = data;
	}

}

Below Code Uses Binary Tree approach



/**
 * 1. For the original MergeBinaryInsertDataNode we need to make sure we merge nodes when ever we see overlap
 * 
 * @author Ashwin Rayaprolu
 *
 */
public class MergeBinaryInsertInterval {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[][] originalIntervals = { { 94230, 94299 }, { 94289, 94699 }, { 94200, 94240 }, { 94133, 94133 } };
		// Sort our array based on lower bound number
		MergeBinaryInsertIntervalDataNode linkedList = new MergeBinaryInsertIntervalDataNode();
		//O(n log n) operation
		for(int[] data:originalIntervals){
			linkedList.insert(linkedList.rootNode,data);
		}
		
		
		System.out.println("---------Sorted?merged Intervals----------");
		linkedList.traverse(linkedList.rootNode);

		System.out.println("---------Merged Intervals----------");

		
	}

}

/**
 * @author Ashwin Rayaprolu
 *
 */
class MergeBinaryInsertIntervalDataNode {
	MergeBinaryInsertDataNode rootNode;
	

	int size = 0;
	
	
	// Inserting or pushing data to binary tree
		public void insert(MergeBinaryInsertDataNode currentNode, int[] newData) {
			MergeBinaryInsertDataNode tempNode = new MergeBinaryInsertDataNode(newData);
			// If first Node then make it root node
			if (rootNode == null) {
				rootNode = tempNode;
				return;
			}
			

			// If new node data >= root node data move to right
			if (currentNode.data[0] <= tempNode.data[0]) {
				
				//If new node is overlapping then merge with current node and return
				if(currentNode.data[1]>=tempNode.data[0]){
					//tempPointer.data[1]=
					currentNode.data[1] = tempNode.data[1];
					return;
				}
				
				
				if (currentNode.rightPointer == null) {
					currentNode.rightPointer= tempNode;
				} else {
					insert(currentNode.rightPointer, newData);
				}
			} else {
				//If new node is overlapping then merge with current node and return
				if(tempNode.data[1]>=currentNode.data[0]){
					//tempPointer.data[1]=
					currentNode.data[0] = tempNode.data[0];
					return;
				}
				
				if (currentNode.leftPointer == null) {
					currentNode.leftPointer = tempNode;
				} else {
					insert(currentNode.leftPointer, newData);
				}
			}

		}
		
		
		/**
		 * @param currentNode
		 */
		public void traverse(MergeBinaryInsertDataNode currentNode) {
			if (currentNode == null) {
				return;
			}

			traverse(currentNode.leftPointer);
			System.out.println("{"+currentNode.data[0]+","+currentNode.data[1]+"}");
			traverse(currentNode.rightPointer);

		}
	
	
	
	

}

/**
 * Data Node used for sorting
 * 
 * @author Ashwin Rayaprolu
 *
 */
class MergeBinaryInsertDataNode {
	int[] data = {};
	MergeBinaryInsertDataNode leftPointer;
	MergeBinaryInsertDataNode rightPointer;

	public MergeBinaryInsertDataNode(int[] data) {
		this.data = data;
	}

}

Linux Box as Router

In continuation of my previous posts where i’m create a distributed cloud infrastructure i need to connect VM’s on multiple Host Machines

Pre knowledge of routing (IPTables) and networking is required for information below. Technically this is what containers like docker or software routers do internally when they need to connect 2 different network’s

distributedhostrouting

Lets assume

HostMachine1 has VM’s on network 10.0.0.1/24
HostMachine2 has VM’s on network 192.168.0.1/24

Now our Gateway1 and Gateway2 have 2 Network Interfaces/NIC cards.

Gateway1 and Gateway2 are connected by switch hence on same network as well as their respective VM networks as they have 2 NIC cards connected.

Let assume Gateway1 has IP 10.0.0.2
Let assume Gateway2 has IP 192.168.0.2

Both Gateway1 and Gateway2 can connect to each other as they are directly connected.

My Current Configuration on Gateway1 which is our target router and i’ve below Network Interfaces on that


enp0s9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:9d:08:3f brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.169/24 brd 10.0.0.255 scope global enp0s9
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe9d:83f/64 scope link
       valid_lft forever preferred_lft forever
 enp0s10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:7b:12:89 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.120/16 brd 192.168.255.255 scope global enp0s10
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe7b:1289/64 scope link 

Now we need to add routing table configurations on either gateway1 or gateway2 to forward packets from one network to another.

We can do this 2 ways.

Either by creating network bridge


# Install brige utils that gives us easy commands to creatr bridge network
apt-get install bridge-utils
or
yum install bridge-utils

# Create new Bridge
brctl addbr br0

# Enable Spanning Tree Support if you need
brctl stp br0 on

# Make sure you get your Devices down before creating bridge and explicity assign 0.0.0.0 to make sure they loose ip
ifconfig enp0s9 0.0.0.0 down
ifconfig enp0s10 0.0.0.0 down

# Add them to our newly created bridge network
brctl addif br0 enp0s9
brctl addif br0 enp0s10

# Finally get all interfaces up.
ifconfig enp0s9 up
ifconfig enp0s10 up
ifconfig br0 up

or

by modifying routing table. I’m explaining second concept here

Enable forwarding in the kernel:


echo 1 >> /proc/sys/net/ipv4/ip_forward

To set this value on boot uncomment this line in/etc/sysctl.conf

#net.ipv4.ip_forward=1

Now i need to route traffic from one Interface to another using routing tables
Below are statements that can do that


# Always accept loopback traffic
iptables -A INPUT -i lo -j ACCEPT

# We allow traffic from the HostMachine1 side
iptables -A INPUT -i enp0s9  -j ACCEPT

# We allow traffic from the HostMachine2 side
iptables -A INPUT -i enp0s10  -j ACCEPT

######################################################################
#
#                         ROUTING
#
######################################################################

# enp0s9 is HostMachine1 Network
# enp0s10 is HostMachine2 Network

# Allow established connections
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Masquerade.
iptables -t nat -A POSTROUTING -o enp0s10  -j MASQUERADE

# fowarding
iptables -A FORWARD -i enp0s9 -o enp0s10  -m state --state RELATED,ESTABLISHED -j ACCEPT

# Allow outgoing connections from the HostMachine1 side.
iptables -A FORWARD -i enp0s10 -o enp0s9  -j ACCEPT


# Repeat same steps on reverse route
iptables -t nat -A POSTROUTING -o enp0s9   -j MASQUERADE

iptables -A FORWARD -i enp0s10 -o enp0s9  -m state --state RELATED,ESTABLISHED -j ACCEPT

iptables -A FORWARD -i enp0s10 -o enp0s9  -j ACCEPT

Finally on HostMachine1 route all traffic on subnet 192.168.0.1/24 to Gateway1

I use Mac as one my host machine hence below command

sudo route -n add -net 192.168.1.1/24 10.0.0.193

If you are using any *nix systems command would be

ip route add 192.168.1.1/24 via 10.0.0.169 dev

Linux Networking Fundamentals Part 2

This is in continuation of previous article. I’m going to start from scratch.

I’m going to build

  1. Two datacenters with name DC1 & DC2 by creating 2 different Vagrant VM networks
  2. Two Rack’s per Datacenter say DC1-RC1, DC1-RC2  and DC2-RC1,DC2-RC2
  3. Each Rack is connected by a Gateway
  4. Each Datacenter is connected by a Router
  5. Finally openvpn to connect both datacenter’s

distributedsystemarch1

All the hardware node and device cooking is mostly done via shell scripts and ruby and vagrant coding.

I’m assuming who ever is interested to go over this first understand basics of networking, Ruby, ShellScripting and Vagrant and Docker Environments.

Before moving ahead i need a simple utility to generate IP address range for given CIDR

Wrote a basic code in ruby that generates that.

# Generate IP's in given Range
# IpList = Nodemanager.convert_ip_range('192.168.1.2', '192.168.1.20')

module Nodemanager

	# Generates range of ips from start to end. Assumption is that i'm only using IPv4 address
	  
  def convertIPrange first, last
    first, last = [first, last].map{|s| s.split(".").inject(0){|i, s| i = 256 * i + s.to_i}}
    (first..last).map do |q|
      a = []
      (q, r = q.divmod(256)) && a.unshift(r) until q.zero?
      a.join(".")
    end
  end

Now i need to load all dependencies in by Berksfile. Berksfile is like a dependency manger for chef (Provisioning tool)

It can be compared with Maven/Gradle(Java), Nuget(Dotnet),Composer (PHP), Bundler (Ruby) , Grunt/Gulp (NodeJS)

name             'basedatacenter'
maintainer       'Ashwin Rayaprolu'
maintainer_email 'ashwin.rayaprolu@gmail.com'
license          'All rights reserved'
description      'Installs/Configures Distributed Workplace'
long_description 'Installs/Configures Distributed Workplace'
version          '1.0.0'


depends 'apt', '~> 2.9'
depends 'firewall', '~> 2.4'
depends 'apache2', '~> 3.2.2'
depends 'mysql', '~> 8.0'  
depends 'mysql2_chef_gem', '~> 1.0'
depends 'database', '~> 5.1'  
depends 'java', '~> 1.42.0'
depends 'users', '~> 3.0.0'
depends 'tarball'


Before moving ahead i want to list my base environment.
I have 2 host machines. One on CentOS 7 and other one on CentOS 6


[ashwin@localhost distributed-workplace]$ uname -r
3.10.0-327.22.2.el7.x86_64
[ashwin@localhost distributed-workplace]$ vboxmanage --version
5.1.2r108956
[ashwin@localhost distributed-workplace]$berks --version
4.3.5
[ashwin@localhost distributed-workplace]$ vagrant --version
Vagrant 1.8.5
[ashwin@localhost distributed-workplace]$ ruby --version
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
[ashwin@localhost distributed-workplace]$ vagrant plugin list
vagrant-berkshelf (5.0.0)
vagrant-hostmanager (1.8.5)
vagrant-omnibus (1.5.0)
vagrant-share (1.1.5, system)

Now let me write a basic Vagrant file to start my VM’s

# -*- mode: ruby -*-
# vi: set ft=ruby :

require './modules/Nodemanager.rb'

include Nodemanager

@IPAddressNodeHash = Hash.new {|h,k| h[k] = Array.new }
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = '2'

Vagrant.require_version '&gt;= 1.5.0'

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  # Create Share for us to Share some files
  config.vm.synced_folder &quot;share/&quot;, &quot;/usr/devenv/share/&quot;, disabled: false
  # Disable Default Vagrant Share
  config.vm.synced_folder &quot;.&quot;, &quot;/vagrant&quot;, disabled: true

  # Setup resource requirements
  config.vm.provider &quot;virtualbox&quot; do |v|
    v.memory = 2048
    v.cpus = 2
  end

  # vagrant plugin install vagrant-hostmanager
  config.hostmanager.enabled = false
  config.hostmanager.manage_host = false
  config.hostmanager.manage_guest = true
  config.hostmanager.ignore_private_ip = false
  config.hostmanager.include_offline = true

  # NOTE: You will need to install the vagrant-omnibus plugin:
  #
  #   $ vagrant plugin install vagrant-omnibus
  #
  if Vagrant.has_plugin?(&quot;vagrant-omnibus&quot;)
    config.omnibus.chef_version = '12.13.37'
  end

  config.vm.box = 'bento/ubuntu-16.04'
  config.vm.network :private_network, type: 'dhcp'
  config.berkshelf.enabled = true

  # Assumes that the Vagrantfile is in the root of our
  # Chef repository.
  root_dir = File.dirname(File.expand_path(__FILE__))

  # Assumes that the node definitions are in the nodes
  # subfolder
  nodetypes = Dir[File.join(root_dir,'nodes','*.json')]

  ipindex = 0
  # Iterate over each of the JSON files
  nodetypes.each do |file|
    puts &quot;parsing #{file}&quot;
        node_json = JSON.parse(File.read(file))

        # Only process the node if it has a vagrant section
        if(node_json[&quot;vagrant&quot;])
          @IPAddressNodeHash[node_json[&quot;vagrant&quot;][&quot;name&quot;]] = Nodemanager.convertIPrange(node_json[&quot;vagrant&quot;][&quot;start_ip&quot;], node_json[&quot;vagrant&quot;][&quot;end_ip&quot;])

          1.upto(node_json[&quot;NumberOfNodes&quot;]) do |nodeIndex| 

            ipindex = ipindex + 1

            # Allow us to remove certain items from the run_list if we're
            # using vagrant. Useful for things like networking configuration
            # which may not apply.
            if exclusions = node_json[&quot;vagrant&quot;][&quot;exclusions&quot;]
              exclusions.each do |exclusion|
                if node_json[&quot;run_list&quot;].delete(exclusion)
                  puts &quot;removed #{exclusion} from the run list&quot;
                end
              end
            end

            vagrant_name = node_json[&quot;vagrant&quot;][&quot;name&quot;] + &quot;-#{nodeIndex}&quot;
            is_public = node_json[&quot;vagrant&quot;][&quot;is_public&quot;]
            #vagrant_ip = node_json[&quot;vagrant&quot;][&quot;ip&quot;]
            vagrant_ip = @IPAddressNodeHash[node_json[&quot;vagrant&quot;][&quot;name&quot;]][nodeIndex-1]
            config.vm.define vagrant_name, autostart: true  do |vagrant|

              vagrant.vm.hostname = vagrant_name
              puts  &quot;Working with host #{vagrant_name} with IP : #{vagrant_ip}&quot; 

              # Only use private networking if we specified an
              # IP. Otherwise fallback to DHCP
              # IP/28 is CIDR
              if vagrant_ip
                vagrant.vm.network :private_network, ip: vagrant_ip,  :netmask =&gt; &quot;255.255.255.240&quot;
              end

              if is_public
                config.vm.network &quot;public_network&quot;, type: &quot;dhcp&quot;, bridge: &quot;em1&quot;
              end

              # hostmanager provisioner
              config.vm.provision :hostmanager

              vagrant.vm.provision :chef_solo do |chef|
                chef.data_bags_path = &quot;data_bags&quot;
                chef.json = node_json
              end        

            end  # End of VM Config

          end # End of node interation on count
        end  #End of vagrant found
      end # End of each node type file

end

Finally run vagrant up . Sample output attached below. I’m creating 2 VM’s for 2 Racks and 1 VM for Gateway. There are now 3 VM’s up and running. 2 VM’s represent our 2 virtual racks and third a gateway. If you notice all of them are running on private ip network which is inaccessible from external world except our gateway node. Our gateway node has 2 different ethernet devices 1 connecting private network and other connecting host network. I’ve marked specific lines that define the kind of network that gets created.


# Only use private networking if we specified an
              # IP. Otherwise fallback to DHCP
              # IP/28 is CIDR
              if vagrant_ip
                vagrant.vm.network :private_network, ip: vagrant_ip,  :netmask =&gt; &quot;255.255.255.240&quot;
              end

              if is_public
                config.vm.network &quot;public_network&quot;, type: &quot;dhcp&quot;, bridge: &quot;em1&quot;
              end

Sample output on Vagrant up

VagrantUpOutput.jpg

 

I define node configuration in a json file so as to make it more simple. Attached is sample node type json for both Gateway node and Rack Node
Below is definition for Rack. I tried to add as much comments as possible to explain each field

If you observer below node definition’s i’ve give Node Name prefix in the config file and also from and to range for IP’s in config file. Apart from that i define the kind of recipe that need to loaded by chef for this specific node type.


{
  "NumberOfNodes":2,
  "environment":"production",
  "authorization": {
    "sudo": {
      // the deploy user specifically gets sudo rights
      // if you're using vagrant it's worth adding "vagrant"
      // to this array
      // The password for the dpeloy user is set in data_bags/users/deploy.json
      // and should be generated using:
      // openssl passwd -1 "plaintextpassword"
      "users": ["deploy", "vagrant"]
    }
  },
  // See http://www.talkingquickly.co.uk/2014/08/auto-generate-vagrant-machines-from-chef-node-definitions/ for more on this
  "vagrant" : {
    "exclusions" : [],
    "name" : "dc1-rc",
    "ip" : "192.168.1.2",
    "start_ip":"192.168.1.2",
    "end_ip":"192.168.1.3"
  },
  "mysql": {
      "server_root_password": "rootpass",
      "server_debian_password": "debpass",
      "server_repl_password": "replpass"
  },
  "data_bags_path":"data_bags",
  "run_list":
  [
    "recipe[basedatacenter::platform]",
    "recipe[basedatacenter::users]",
    "recipe[basedatacenter::docker]"
   
  ]
}

Below is node definition for Gateway.


{
  "NumberOfNodes":1,
  "environment":"production",
  "authorization": {
    "sudo": {
      // the deploy user specifically gets sudo rights
      // if you're using vagrant it's worth adding "vagrant"
      // to this array
      // The password for the dpeloy user is set in data_bags/users/deploy.json
      // and should be generated using:
      // openssl passwd -1 "plaintextpassword"
      "users": ["deploy", "vagrant"]
    }
  },
  // See http://www.talkingquickly.co.uk/2014/08/auto-generate-vagrant-machines-from-chef-node-definitions/ for more on this
  "vagrant" : {
    "exclusions" : [],
    "name" : "dc1-gw",
    "ip" : "192.168.1.5",
    "start_ip":"192.168.1.4",
    "end_ip":"192.168.1.4",
    "is_public":true
  },
  "mysql": {
      "server_root_password": "rootpass",
      "server_debian_password": "debpass",
      "server_repl_password": "replpass"
  },
  "data_bags_path":"data_bags",
  "run_list":
  [
    "recipe[basedatacenter::platform]"
  ]
}

 

Before moving on to next step i need to install 5 nodes on each rack. Which is taken care by docker. Docker is a containerization tool that mimic’s VM but very light weight. We are using docker containers to mimic realworld nodes


apt-get install -y curl &&
apt-get install  -y  apt-transport-https ca-certificates &&
apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D &&
touch /etc/apt/sources.list.d/docker.list &&
echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" >> /etc/apt/sources.list.d/docker.list  &&
apt-get update &&
apt-get purge lxc-docker &&
apt-get install -y linux-image-extra-$(uname -r) linux-image-extra-virtual &&
apt-get update &&
apt-get install -y docker-engine &&
curl -L https://github.com/docker/machine/releases/download/v0.7.0/docker-machine-`uname -s`-`uname -m` > /usr/local/bin/docker-machine && 
chmod +x /usr/local/bin/docker-machine &&
curl -L https://github.com/docker/compose/releases/download/1.8.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose &&
chmod +x /usr/local/bin/docker-compose &&
sudo usermod -aG docker docker

Once docker is setup on all racks  we need to install all nodes. Below is base version of docker file that i use
My next step is to setup containers on each of the rack so that we can replicate multiple datacenter’s and multiple rack scenarios

I’m going to create 5 containers on each rack and each one of the container will again be using Ubuntu Xenial as base OS. I’m going to install oracle 7 jdk on all of them.

My usecase for distributed architecture is based on HDFS, Cassandra setup hence i need to install java first . Below install.sh script is run by vagrant/chef to install docker on each of the rack.


FROM ubuntu:16.04
MAINTAINER Ashwin Rayaprolu

RUN apt-get update
RUN apt-get dist-upgrade -y

RUN DEBIAN_FRONTEND=noninteractive apt-get -y dist-upgrade
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install python-software-properties
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install software-properties-common
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install byobu curl git htop man unzip vim wget

# Install Java.
RUN \
  echo oracle-java7-installer shared/accepted-oracle-license-v1-1 select true | debconf-set-selections && \
  add-apt-repository -y ppa:webupd8team/java && \
  apt-get update && \
  apt-get install -y oracle-java7-installer && \
  rm -rf /var/lib/apt/lists/* && \
  rm -rf /var/cache/oracle-jdk7-installer
  
  
# Install InetUtils for Ping/traceroute/ifconfig
RUN apt-get update
# For Ifconfig and other commands
RUN apt-get install -y net-tools
# For ping command
RUN apt-get install -y iputils-ping 
# For Traceroute
RUN apt-get install -y inetutils-traceroute



# Define working directory.
WORKDIR /data

# Define commonly used JAVA_HOME variable
ENV JAVA_HOME /usr/lib/jvm/java-7-oracle

# Define default command.
CMD ["bash"]


 

Docker has a very elegant way of creating network’s. As our
Rack Network is on 192.168.1.*
We want
Node Network on 10.18.1.2/28

We have multiple options to create network in docker. I would like to go with bridge networking. Will discuss on those specific topic later. For now assuming we are using bridge network below is code to create network and attach to some container

We need to make sure we have different range of network on each rack and each datacenter so that we don’t overlap IP’s between different rack’s and datacenter’s

# Below command will create a network in our desired range (dc1-rack1)
# 10.18.1.0  to 10.18.1.15
docker network create -d bridge \
  --subnet=10.18.0.0/16 \
  --gateway=10.18.1.1 \
  --ip-range=10.18.1.2/28 \
  my-multihost-network 

# Below command will create a network in our desired range (dc1-rack2)
# From 10.18.1.16  to 10.18.1.31
docker network create -d bridge \
  --subnet=10.18.0.0/16 \
  --gateway=10.18.1.1 \
  --ip-range=10.18.1.19/28 \
  my-multihost-network    

# Below command will create a network in our desired range (dc2-rack1)
# 10.18.1.32  to 10.18.1.47
docker network create -d bridge \
  --subnet=10.18.0.0/16 \
  --gateway=10.18.1.1 \
  --ip-range=10.18.1.36/28 \
  my-multihost-network    

# Below command will create a network in our desired range (dc2-rack2)
# 10.18.1.48  to 10.18.1.63
docker network create -d bridge \
  --subnet=10.18.0.0/16 \
  --gateway=10.18.1.1 \
  --ip-range=10.18.1.55/28 \
  my-multihost-network      
  
# -d option to run in background  -t option to get a duplicate tty
docker run -itd multinode_node1

# Connect the newly created network on each node to the node name.
docker network connect my-multihost-network docker_node_name

 

I would write code to automate all the above tasks in subsequent articles. I’m going to use docker-compose to build individual nodes in each rack.

Very basic code would look like this


version: '2'
services:
  node1:
    build: node1/
  node2:
    image: node2/
  node3:
    image: node3/

You can checkout First version of code from

https://github.com/ashwinrayaprolu1984/distributed-workplace.git

 

Linux Networking Fundamentals Part 1

I’ve been working on setting up couple of host and guest machines at my home/office for last couple of years. I wanted to replicate real world scenario where we have multiple hosts on multiple subnet and still interacting with each other. While working on this i  found that lot of people are either not aware of basic concepts or confused because of abundant information available online. I want to simplify networking in a short concise manner all in single post.

I would also show application layers along with network layers to make it more clear. I see that these 2 department don’t have any common knowledge in common everywhere in industry. I would love my developers to understand network connectivity while designing distributed application or even basic non distributed applications.

There are 5 major components of that any company or network admin has to look at

  1. Gateway
  2. Firewall
  3. Switch
  4. Router
  5. Hub

and 2 basic concepts

  1. Nic Cards/ Network Interface Cards
  2. Subnets.

 

Gateway : This is the target applicance/device/machine to which source computer sends all its packets for any networking. Assuming there is only one network interface card in source computer. (Note: A machine can have any number of interface cards as hardware supports). If we use gateway we mostly need to add static routes to all machines

Firewall:  This generally sits between cloud/internet and private network which can control flow of data packets from inside/outside interfaces. Example: Cisco ASA Applicance.  Without a firewall every network is susceptible to brute force attacks and heavy load on systems. (This is funny link which shows how everyone around the world are trying to attach each other digitally  http://map.norsecorp.com)

Hub: This is a device to which all networking devices connect to. This is good for lab testing but not efficient way of connecting devices.Major disadvantage it has is that it broadcasts information sent from one device over common channel that is visible to all other devices connected to that hub. This also creates unnecessary bandwidth issues as it floods information to everyone irrespective of who it is destined to.

Switch: This is an efficient way of creating a minimal network. Switches have multiple sockets to connect different available in network. Switches separate communication channels between all devices which makes it very efficient in connecting devices.

Router:  As the name suggests it routes traffic between different networks. This generally sits coupled with gateway and knows where to send packets to. It stores all information in routing table and decides best possible route for given packet. Router automatically takes care of routing removing need for nodes to add static routes

Below diagram explain a very high level connection scenario’s

networklayout

If you observe in above diagram i’ve one network with subnet 192.168.1.0/24 connected to one switch and other network 10.0.0.1/24 connected to different switch.

Both of them are connected via a  router/gateway

And gateway is connected to internet via Firewall.

This explain a minimalistic design of any company.

We can have multiple layers of switches and router sitting on top of each other in tree fashion but basic concepts remain the same.

Now coming to NIC Card/Network Interface Cards

NIC: This is located in each and every networking device and enable the device to get connected via ethernet. These days we can create Virtual NIC cards on any Operating system which behave same as physical NIC. There are other concepts of NIC called Nic Teaming or Bonding which improve bandwidth and performance of Network IO apart from providing failover connectivity. I would explain all these concepts in details in subsequent series.

Subnet:  Subnets are single most important concept that allow us to segregate different kind of networks. Originally when networks originated in 80’s there was concept of Class Based Networking

Example: for 192.168.1.1

Class A:   Network portion is just 192 and rest  168.1.1 defines the node address

Class B:  192.168 defines network portion and rest 1.1 is node address

Class C: 192.168.1 defines network and remaining 1 represents node address

This approach was good till early 90’s but then issue of ip address exhaustion started.

Then IETF introduced a concept of CIDR (Classless Internet Domain Routing). With introduction of this approach now IP Address is never complete without Network Mask. Example 192.168.1.1 doesn’t represent a node unless we define netmask. So now ip address have to defined as 192.168.1.1/24  where 24 represents 24 bits which is prefix or network portion of above address.

With advent of Virtual Machines/Containerization of networks and Operating system like linux we can replicate all above devices with software code.

I would start explain each concept in detail and how to configure and build multiple computers and network routes in subsequent series.

Technologies i’m going to use and topic coverage in next couple of months. I’m going to write each one fresh using latest version so that my code is not outdated.

  1. Vagrant ( To manage Virtual machines)
  2. Oracle VirtualBox ( Virtual machine provider . We can also use HyperV or VMWare)
  3. Docker (Lightweight replica of VM which can provide similar features)
  4. CentOS/Ubuntu ( I’m going to use both flavors of linux in different devices)
  5. Linux Routing Table for basic
  6. Quagga/Zebra to create powerful router
  7. Iptables used as simple firewall
  8. OpenVPN to setup VPN connections.
  9. Chef/Berkshelf (One of the most versatile OS provisioner )
  10. Shell Scripting ( This is obvious)
  11. Apache/PHP/Mysql For a demo website
  12. Cassandra as demo database to show how data gets across different racks in out virtual data centers (VM’s on different networks).

 

I’ve already have one base version written which i’m going to reuse in bits and pieces.

https://github.com/ashwinrayaprolu1984/chef-lamp

Part 2 of the series is at

https://ashwinrayaprolu.wordpress.com/2016/10/01/linux-networking-fundamentals-part-2/

 

Install Virtualbox Extension Commandline

$(vboxmanage -v)

Gives version like this  5.1.4r110228

We need split above version with “r” and use those to generate our required filename

Then get the file from download. virtualbox. org/virtualbox/5.1.4/

Installing is just issuing this command

sudo VBoxManage extpack install <YOUR FILE> –replace

#!/bin/bash
version=$(vboxmanage -v)
echo $version
var1=$(echo $version | cut -d ‘r’ -f 1)
echo $var1
var2=$(echo $version | cut -d ‘r’ -f 2)
echo $var2
file=”Oracle_VM_VirtualBox_Extension_Pack-$var1-$var2.vbox-extpack”
echo $file
wget ***: download. virtualbox. org/virtualbox/$var1/$file -O /tmp/$file
#sudo VBoxManage extpack uninstall “Oracle VM VirtualBox Extension Pack”
sudo VBoxManage extpack install /tmp/$file –replace

Simple Cassandra Template Integrated with ObjectPool

Below is a Cassandra Template Integrated with ObjectPool Created in previous post

This has been tested with 30 parallel sessions opened to cassandra

We create one cluster object per application but we can create as many session as your system and configuration permits..

Note: Best way to create session is not attaching them to any keyspace.. so that we can reuse the same session/connections for multiple threads.

/**
*
*/
package com.linkwithweb.products.daolayer.cassandra;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.HostDistance;
import com.datastax.driver.core.PoolingOptions;
import com.datastax.driver.core.QueryOptions;
import com.datastax.driver.core.Session;
import com.linkwithweb.products.daolayer.ObjectPool;

/**
* @author ashwinrayaprolu
*
*/
@Configuration
@PropertySource(value = { "classpath:cassandra.properties" })
public class CassandraTemplate extends ObjectPool<Session> {
private static final Log LOG = LogFactory.getLog(CassandraTemplate.class);
@Autowired
private Environment env;

public CassandraTemplate() {
this(4, 7, 5, 40);
}

/**
* @param minIdle
* @param maxIdle
* @param validationInterval
* @param maxConnections
*/
public CassandraTemplate(int minIdle, int maxIdle, long validationInterval, int maxConnections) {
super(minIdle, maxIdle, validationInterval, maxConnections);
}

/**
* @return
*/
@Bean
public Cluster cassandraCluster() {

Cluster cluster = null;
try {
PoolingOptions poolingOptions = new PoolingOptions();
;

poolingOptions.setCoreConnectionsPerHost(HostDistance.LOCAL, 4).setMaxConnectionsPerHost(HostDistance.LOCAL, 10)
.setCoreConnectionsPerHost(HostDistance.REMOTE, 2).setMaxConnectionsPerHost(HostDistance.REMOTE, 4).setHeartbeatIntervalSeconds(60);

cluster = Cluster.builder()
// (1)
.addContactPoint(env.getProperty("cassandra.contactpoints")).withQueryOptions(new QueryOptions().setFetchSize(2000))
.withPoolingOptions(poolingOptions).build();
} catch (Exception e) {
e.printStackTrace();
} finally {

}

return cluster;

}

/**
* @return
* @throws Exception
*/
public Session cassandraSession() throws Exception {
Session session = cassandraCluster().connect(); // (2)
return session;
}

@Override
protected Session create() {
try {
return cassandraSession();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

@Override
public void close(Session object) {
object.close();
}
}