Saturday, June 20, 2015

How to migrate from the MySQL Extension to PDO

My team's start-up has a web application that is currently using MySQLi Extensions.

Switching to PDO has many advantages:
  • offers a consistent API to work with a variety of databases
  • exposes high-level objects for the programmer to work with database connections
  • low-level drivers perform communication and resource handling with the database server

Basic Workflow

The basic workflow for working with a database can be thought of as a 5-step process:
  1. Establish a connection to the database server and select the database you’ll be working with
  2. Construct a query to send the server
  3. Send the query
  4. Iterate over the returned result rows
  5. Free the resources used by the result and possibly the database connection
With PDO, the same process can be followed and looks like this:

















<?php
// Step 1: Establish a connection
$db = new PDO("mysql:host=localhost;dbname=testdb", "testusr", "secretpass");
// Step 2: Construct a query
$query = "SELECT * FROM foo WHERE bar = " . $db->quote($zip);
// Step 3: Send the query
$result = $db->query($query);
// Step 4: Iterate over the results
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
    print_r($row);
}
// Step 5: Free used resources
$result->closeCursor();
$db = null;

Friday, June 19, 2015

How to create a StackStorm Pack

What is a Pack?

Pack is the unit of deployment for integrations and automations in order to extend StackStorm. Typically a pack is organized along service or product boundaries e.g. AWS, Docker, Sensu etc. A pack can contain ActionsWorkflowsRulesSensors.
It is best to view a pack as the means to extend StackStorm and allow it to integrate with an external systems such as Git, Google. AWS, Jenkins, etc.

Create and Contribute a Pack

Packs have a defined structure that is prescribed by StackStorm. It is required to follow this structure while creating your own pack and is also helpful to know while debugging issues with packs.
# contents of a pack folder
actions/
rules/
sensors/
config.yaml
pack.yaml
requirements.txt
In the classic Proto-Hack fashion, let's just dive right in and build your first pack!

My first pack

If you would like to create a pack yourself then follow these simple steps. In the example below, we will create a simple pack named hello-st2. The full example is also available atst2/contrib/hello-st2.
  1. First, let’s create the pack folder structure and related files. Let’s keep the metadata files such as pack.yaml, config.yaml, and requirements.txt empty for now.
# Use the name of the pack for the folder name.
mkdir hello-st2
cd hello-st2
mkdir actions
mkdir rules
mkdir sensors
touch pack.yaml
touch config.yaml
touch requirements.txt


Monday, June 8, 2015

How to continuously integrate, test and deploy

This post will walk through the steps of setting up a continuous integration server starting with a codebase from Github, automated by StackStorm.

Step 1: Deploy stackstorm on a server, following this guide.
  • There are many ways to install/deploy stackstorm, I'll be using vagrant.
  • I'm using my Windows based work computer to act as the server for this example, so vagrant is a great choice for getting things up and running.
  • First things first, clone the project, cd to the vagrant folder within, and "vagrant up" to get started!
And we're off!

Whoops, looks like vagrant was updated since I cloned the project! Let's fix that...
Great success! It's installed!

This will install all StackStorm components along with the Mistral workflow engine on Ubuntu 14.04 virtual machine. The setup will download additional packages from the internet. While waiting, check out StackStorm 101 Video for quick intro. If setup is successful, you will see the following console output.

Step 2: Create a sensor to integrate with repository commits.

Step 3: Create a rule to merge all branches into the master branch.

Step 4: Create a rule to run all test cases, and if successful, deploy to production server.