Today is the day to Grunt

grunt-logoWeb development has become much more complex these days.

By “complex”, I mean there are SO many tools to do neat stuff with your web related files.  The focus of User Experience in the past few years has create an explosion in streamlining web development. Anyways, I love to play with cool tools, I am not complaining.

At work I lead a UI project where we re-brand open source web utils (like Bootstrap, jQueryUI, etc) for other developers to use. We’ve been focusing on delivering end-user features all along and did not spend too much time looking into automating some of the process, like packaging files for distribution in our CDN. (yes I made a global CDN too… well I mean national.)

Today is the day!

Ok let’s start with something small. My goal is to combine a couple css files and minify the result.
I will try out Grunt because I’ve seen it too many times on open source projects on GitHub.

Ingredients

  • Installations
    • NodeJS
    • Grunt
      • grunt-cli
      • grunt-contrib-concat
      • grunt-css
  • Creations
    • package.json
    • Gruntfile

Instructions

  1. Get NodeJS – installer available at nodejs.org
    Verify your installation with npm -v on your command line
  2. Install Grunt
    > npm -g install grunt-cli-g will install grunt-cli globally (ie sharable across your computer, not just in your project)
    Verify your install with grunt -v on your command line
  3. Create package.json
    “package.json: This file is used by npm to store metadata for projects published as npm modules. You will list grunt and the Grunt plugins your project needs as devDependencies in this file.” — straight from Grunt’s documentation. In other words, this file is used to describe your project in a way that npm understands it.Make a new file called “package.json” at the root of your project.Here’s the boilerplate you can modify
    {
       "name": "my-project-name",
       "version": "0.1.0"
    }
    We will fill in the dependencies later.
  4. Adding dependencies to package.json
    the reason we don’t include the dependencies when you create the package.json in the above step, is that it can be done automatically with npm. (Why create when you can generate?)run these on your command line at the root of your project
    > npm install grunt-contrib-concat --save-dev
    > npm install grunt-css --save-dev

    You will have installed the two modules we will be using to concatenate the files and minifying. They are installed right in the root of your project, under a /node_modules folder. Global installation of these is not possible at the moment and there’s a constant inquiry, but still not available. So just add the folder to .gitignore for now to avoid checking those files it.the –save-dev option will put those as a devDependencies in your package.json. Your package.json should look like this now{
       "name": "my-project-name",
       "version": "0.1.0",
    "devDependencies": {
    "grunt": "~0.4.5",
    "grunt-css": "~0.5.4",
    "grunt-contrib-concat": "~0.5.1"
    }

    }
  5. Tell Grunt what to do
    create a Gruntfile.js at the root of your project. Here’s more boilerplate code:
    module.exports = function(grunt) {
    // Project configuration.
    grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'), // combine files
    concat: {
    css: {
    src: ['css/first.css', 'css/second.css'], // if order is not important, you can use 'css/*.css' to get all css files
    dest: 'dist/css/mycombined.css' // putting them into a /dist folder
    }
    },
    // minify
    cssmin: {
    css: {
    src: 'dist/css/mycombined.css', // from the just combined file
    dest: 'dist/css/mycombined.min.css' // to a minified file
    }
    }
    });
    // Load the plugin that provides the "concat" and "cssmin" tasks
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-css');
    grunt.registerTask(‘default’, [‘concat’, ‘cssmin’]);   // this is the default task for grunt to run if you do not provide any arguments. (ie. just “grunt”)
    }
  6. Run it
    Type “grunt” at the project root on your command line, and PROFIT!
    > grunt
    The combined and minified files should now be created in the /dist folder

Grunt docs

Tested with npm 1.3.2, grunt-cli v0.1.13,
“devDependencies”: {
“grunt”: “~0.4.5”,
“grunt-css”: “~0.5.4”,
“grunt-contrib-concat”: “~0.5.1”
} on Windows 7

Advertisements

Ways to perform PaaS Auto-Scaling Test – OpenShift

In PaaS, if Auto-Scaling is enabled for an application, the application would dynamically increase / decrease its number of instances in response to load. In Cloud Foundry alike (Pivotal Cloud Foundry, ActiveState Stackato, IBM BlueMix), they are mostly CPU driven. In OpenShift, it is driven by web traffic.

0. F5
No, not your F5 load balancer, but simply pressing F5 repeatedly against your browser to refresh the page. This works sometimes depending your finger speed, but usually not enough, there are other ways to generate mass traffic against your application.

1. a simple script that calls your app in a loop
in linux, on your command line:
> for i in {1..10000}; do curl -s http://yourapp.example.com > /dev/null; usleep 50000; done

2. Apache HTTP server benchmarking tool

Because we are talking about OpenShift here, which uses Apache to serve up your pages, you would have the Apache HTTP Server Benchmarking tool – ab installed on one of your nodes.

Log on to your node and run on the command line:
> ab -t 100000 -c 5 http://yourapp.example.com

check here for ways to tweak your ab test.

Go to your application’s Web Load Balancer and look for signs of traffic.
Another instance (“gear”) should spin up itself after a few minutes.
Note, in OpenShift v2, you can only enable auto-scaling when you FIRST create the application. You cannot enable it after. (WHAAAT)

After your traffic has die down, it should also scale down itself. By default I believe it takes 15 minutes after the lessening the traffic to take effect.

Using Spring’s RestTemplate to call RESTful web service with authentication

In most of my projects, I’ve always consumed SOAP web services via client jars generated from WSDL.

Lately we have seen more RESTful services surface in our projects, and Spring’s RestTemplate is an alternative to HttpClient that makes it easy to call the web service in your Java code.

Here’s an example with basic auth:

RestTemplate restTemplate = new RestTemplate();

String url = "http://example.com/restful/endpoint";
Map myMap = new HashMap();
myMap.put("field1","value1");
...

//setting headers because I need basic auth
HttpHeaders headers = new HttpHeaders();

// serialization time
String plainCreds = "username:password";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);

// stick the auth info into header
headers.add("Authorization", "Basic " + base64Creds);
headers.setContentType(MediaType.APPLICATION_JSON);

//set your entity to send
HttpEntity entity = new HttpEntity(myMap, headers);

// send it!
ResponseEntity<String> out = restTemplate.exchange(url, HttpMethod.POST, entity
, String.class);

RHEL: Permission Denied

If you are getting permission denied when executing files that you believe you have rights to, here are some suggestions. You’ll need sudo access to do the following:

check file ownership:

ls -la

change file ownership:

chown user:group filename

add -R to change directory and everything underneath

check that your files have execute rights.
This is often missing when you upload files from Windows:

ls -la

change rights:

chmod +x filename      
      or      
chmod +x -R directoryname

finally, check SELinux context.
SELinux context is often messed up when you use a file transfer tool (ie FTP – FileZilla, SCP – WinSCP) to transfer from Windows to Linux.

ls --lcontext        or         ls -Z

compare your file/directory with other files. If you have no problems executing the other files, use them as the reference context.

chcon -R --reference otherworkingdirectory yourdirectory

Tested on Red Hat Enterprise Linux Server release 6.6 (Santiago)

Setting up a Private PaaS – the journey begins.

Before I started evaluating PaaS, I know I need some servers.
Naturally I approached the infrastructure team for help.

Me: Hey, I have this amazing PaaS that I want to try out. It has a long list of benefits… (repeats to every person I encounter) It can also create and destroy other VMs.
 
Security: Not in our data center! Go isolate yourself in the lab.

 
Infrastructure: So do you need large or medium blades? barebone or VMs? I’d give you 3 ESXi hosts, and do you already have a chassis? what’s the storage requirements? Each server has its own storage but not sharable, so you will need a NAS.

 
Lab: We might have a spare chassis already racked. Your servers will need to be dual-nic between lab and corporate network. You will need a NAT server which also act as a DNS server. How many IPs do you need? A subnet of 24? Do you need a dedicated jump box? and which servers from corporate you need access to? tcp/udp? what direction?

 
Vendor: you need a wildcard DNS entry, and some open ports. We have a virtual appliance you could use. It will with any IaaS you have. Here’s a bunch of scripts to run on different nodes…

 
Me: ???

This is pretty much how the journey to PaaS, Virtualization, Linux, Networking began for me. This conversation went on for loops until we finally settled on some infrastructure design. A few weeks later, with the help of some very helpful folks from infrastructure and lab, I was able to use VMWare vCenter to setup and manage my 40 VMs, setting up a NAT & DNS server, configuring the networking of some VMs, and lots and lots of troubleshooting on different versions of Linux.

And on the journey, since I have access to servers, I also installed a bunch of other cool stuff, like Splunk and OpenAM/OpenSSO server. It is better than submitting and waiting for firewall requests to be completed. *stretch fingers*

the hardest thing in learning is:
you don’t know what you don’t know

Because sometimes you don’t know such things exist, you couldn’t even ask the appropriate questions to learn about it, unless someone brings it up. In this case, my passion for technology & curiosity kept me vigorous and drove the project forward through hurdles.
Fortunately, I eventually get to the other end of the rope.

PaaS Evaluations

These days I’ve been testing out different flavours of PaaS (Platform-as-a-Service) – Cloud Foundry and OpenShift. Specifically, they are Pivotal Cloud Foundry, ActiveState Stackato, and RedHat OpenShift.
I will share my findings in upcoming posts.

In addition, to setup PaaS in our lab, I’ve also become the accidental administrator of network, linux (centos, rhel, ubuntu, suse), infrastruture & virtualization, etc.  
So I also will be jotting down common issues & troubleshooting steps.

Hello world!

This is your very first post. Click the Edit link to modify or delete it, or start a new post. If you like, use this post to tell readers why you started this blog and what you plan to do with it.

Happy blogging!