Friday, October 26, 2012

Getting Started with node.js on Ubuntu

This is a small getting started guide for getting up and running with node.js on Ubunutu.

Step 0: DON'T install node.js from Ubuntu Software Center.
When I started messing about with node.js the first thing I did was to install node.js from Ubuntu Software Center. But soon I started using it I realised that some of the stuff that my friends where using, like npm ( more on that later ) was missing. I check the version and realised that the version I got from UBC was 0.2.6 while the latest verision was 0.8.12. So i quickly removed it from UBC and headed online.

Step 1: Install from nodejs.org  
Head to http://nodejs.org
Clicking the "Install" link will give you the latest version tar.gz
Extract the content to somewhere good, this example i will use ~/code/
Enter your terminal and go to the folder, cd ~/code/node-v0.8.12
Read through README.md carefully ( lol! )
>./configure
stuff happen
>make
more stuff happen
>sudo make install

Restart your terminal


Step 2: Start Server
Copy the following code to a file called node_js_getting_started.js
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Running node.js\n');
}).listen(1234, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1234/');
Or download the file here node_js_getting_started.js

Now run the command >node node_js_getting_started.js
And you should see
Server running at http://127.0.0.1:1234/

Step 3: Test it
Open a browser and go to http://127.0.0.1:1234/
and now you should see "Running node.js" in your browser.
We got a webserver with node.js up and running! Yay!


Friday, September 28, 2012

Shellshocked! - Powershell basics

This is a basic FAQ, reference table, cheat sheet, dummies guide, whatever you want to call it for Microsoft Powershell.


I started using Powershell regularly recently at my current assignment at a company that does Business Intelligence and only use Windows based systems.  I decided to document my recently acquired knowledge for my own good and I thought that maybe it'll be help to someone else out there. This is by no means a complete reference to Powershell, it's just a collection of the stuff I use the most. And I'll be adding more and more to it as time progresses.
If you spot any errors or can suggest improvements please let me know.

Running Scripts

You might get an error the first time you try to run a powershell script on a new computer, the error you get will look something like this.
File C:\scripts\test.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
To fix this you need to open Powershell in Administrator mode and run
Set-ExecutionPolicy RemoteSigned
Now it should be possible to run your scripts.

Commands

Create Directory
Let's say we want to create a folder called "Appname" in our Apps folder.
new-item C:\ProgramData\Corp\Apps\Appname -itemtype directory

Wait for input
If you want user input you can use Read-Host
$a = Read-Host "Press enter to End"

Write to Screen
Write-Host "Hello World"

Download file from Internet
Want to download something from the web? No problem. Create a Webclient object and use DownloadFile.
$webclient = New-Object System.Net.WebClient
$url = "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhspnpZGK25blu0_PtvwrljVqhr7cMKHk30jlTLniStMgmlhIjx5UmnIsU93QBrX0kXXa2xFNlZds9NVM3TnPmxn7BSj0QctDlwKrz3ziAEmMMzQGu1qgc1FfroTDuwlpAwBb17D_HjdqY/s220/jonas_bw.jpg"
$file = "good_looking_guy.jpg"
$webclient.DownloadFile($url,$file)
>> WebClient at MSDN

Fun Trivia 

When writing this Google's Chrome gave me a typo warning for "Powershell" and suggested "Powers-hell". Coincidence? :)

Friday, April 13, 2012

Running SQL commands on Sybase from Hudson

This post means to describe the several problems i found when trying to run SQL commands on Sybase from Hudson.

Problem 1: Host key verification failed”
Problem: When trying to SSH to the server which holds the Sybase database I got "Host key verification failed" even thought they keys were correct.
Solution: The missing piece of the puzzle was a row in the ~/.ssh/known_hosts file.
By using another user, which uses the same private/public key, I could run a ssh to the sybase server and get a row in that users known_hosts file. Then I just copied that row to
/var/lib/hudson/.ssh/known_hosts and voila everything worked.
Learn more:
http://amath.colorado.edu/computing/unix/sshknownhosts.html

Problem 2: Shell profile not loaded when running one-line ssh
Problem: The script we need to run on the server needed a lot of magic that was set up by the shell profile scripts. When ssh:ing to the server the profiles scripts are run and everything is dandy. But from Jenkins I need to use one-line ssh commands like this.
ssh user@server "isql < lots_of_stuff.sql" But when running one-line ssh the profile is NOT loaded and and I get a lot of errors since the magic is not set up. Solution: Load the profile in the ssh one-liner before running the real command. ssh user@server ". ./.profile; isql < lots_of_stuff.sql"

Problem 3: Incorrect Locale
After solving the two problems above I was sure everything would work fine, but no, of course it didn't.
Instead of the expected SQL response I got:
The context allocation routine failed when it tried to load localization files!!
One or more following problems may caused the failure

Your sybase home directory is /opt/sybase. Check the environment variable SYBASE if it is not the one you want!
Using locale name "POSIX" defined in environment variable LC_ALL
An error occurred when attempting to allocate localization-related structures.
Locale name "POSIX" doesn't exist in your /opt/sybase/locales/locales.dat file

POSIX? It could make a grown man cry :~(
Solution: Adding -v, for verbose, to the ssh command I could quickly see the difference between what Hudson was doing and what happened when running it from my own machine.
Hudson:
...
debug1: Sending environment.
debug1: Sending env LC_ALL = POSIX
debug1: Sending command: . ./.profile; isql < lots_of_stuff.sql


Locally:
...
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: . ./.profile; isql < lots_of_stuff.sql

No locale is sent from my local machine so I tried overriding it before sending the command in Hudson and it worked.
export LC_ALL=;
ssh sybase@inttestsbn.its.sec.intra '. ./.profile;isql < lots_of_stuff.sql'

And it works!

Thursday, January 12, 2012

Ultimate Password Solution using Dropbox and KeePassX

Today I found the ultimate password management solution using Dropbox and KeePassX.

I wanted a secure enough solution that can be used and managed on multiple computers. My old solution was a .txt file in Dropbox with a name that hopefully nobody would look in. But today I migrated the solution to KeePassX using a encrypted database saved as a file in Dropbox.

Another problem I had was that I couldn't download my entire Dropbox account to work since it takes to much space. But nowadays Dropbox for Linux can handle Selective Sync, i.e. only check out selected folders and not everything.

Here is how you can get the same set up step by step.
Written assuming you have a Dropbox account and the folder where you want to keep the password file is already created.

Step 1: Install Dropbox
https://www.dropbox.com/install

Step 2: Check out the folder where you want to save the password database
https://www.dropbox.com/help/175

Step 3: Install KeePassX
Use synaptic package manager or link below.
http://keepass.info/download.html

Step 4: Create new database in KeePassX
Generate new file and save it in your Dropbox folder
Add secure password ( http://www.generate-password.com/ )

Step 5: Add your entries to KeePassX

Step 6: Repeat step 1 - 3 on all your computers.
Instead of creating a new database connect to the file you created in your Dropbox folder.

That's It and now you are in password management heaven!

Enjoy!