How To Instal A Git Repo & Serve Via SSH

Love Git but don’t want to pay GitHub to have a private repo? No problem. Here’s the solution. I was looking for a way to create a repo and serve it on my server via ssh. Git made it really simple and we can do in 3 steps (copied from the [Reference 1]):

1. Create a repo

server $ mkdir ~/repos/
server $ cd ~/repos/
server $ GIT_DIR=project.git git init
server $ cd project.git
server $ git --bare update-server-info
server $ cp hooks/post-update.sample hooks/post-update

2. Clone it on the client side via SSH

client $ git clone user@server:~/repos/project.git # Check the [Reference 2]
client $ mkdir project
client $ cd project
client $ git init
client $ git remote add origin user@server:~/repos/project.git/

3. Code & Push

client $ touch README
client $ git add README
client $ git commit -m "Example."
client $ git push origin master

References:

1. How to serve a Git repo via SSH: http://www.mindfuzz.net/?p=250
2. How to map SSH identity files to SSH servers & usernames: http://stackoverflow.com/questions/2419566/best-way-to-use-multiple-ssh-private-keys-on-one-client?rq=1

Listing all header files your cpp depends on

I needed to extract certain Boost headers from its huge code base for memory mapped file & shared memory containers. The first idea came to my mind was a simple grep command for ‘#include’ statements and then parse the paths. This is simple but not so useful for the cases when headers are conditionally included. For example:


#ifndef NO_STL
#include "my_class_no_stl.h"
#else
#include "my_class_stl.h"
#endif

To be honest, grep won’t be able to handle it. We need a full fledged C++ pre-processor to correctly include headers and pass necessary values/definitions (it’s done with -D for g++). I was struggling a bit and got a tip from Ralph. It turned out to be very simple:


g++ -D NO_STL -I A_PATH -M source.cpp # to get all headers, including system headers
g++ -D NO_STL -I A_PATH -MM source.cpp # to get all headers, except system headers
g++ -D NO_STL -I A_PATH -H source.cpp # to print all headers nested in as a tree

Source: http://stackoverflow.com/questions/5834778/g-how-to-tell-where-a-header-file-is-included-from

How I picked up TDD & Google Test in a few hours

TDD Diagram
TDD Diagram

To confess, this is the first time I write unit testing for Java & C++ and it turned out this was rather simple. This weekend Daniel is hosting hackathon at his place and invited Kong & me to come over. The first thing we did was TDD. Daniel was fluent in TDD & pair-programming as his company used Ruby and actively worshiped agile development.

I was given a task: To implement a set data structure in Java as simple as possible to pass the test cases that he would write to challenge the implementation. The first test case was to assert emptiness of a new set via the method `bool isEmpty`. Returning `true` was easy. Then Daniel wrote another task to add a new element and requested the set must return `false`. I added a count & array as private data members. Daniel said I should implement as simple as possible. So I used private one member _isEmpty and set it to `true` by default and `false` when invoking `add(int value)`.

And we went on to add test cases to cover methods `int count()`, `void remove(int value)`, `int getIndexOf(int value)`… I got 2 bugs and JUnit was able to point out which test cases failed and I was able to locate the bugs with ease.

I got excited and then moved to C++ and installed Google Test framework. It was too simple and I regret not using it earlier. Read my TDD C++ code https://github.com/vietlq/cpp/tree/master/tdd_set, you will find it super simple. Here’s the screenshot of the test program:

Passed All Test Cases (Using Google Test)

The real value is seen when refactoring was involved in the process. TDD approach & the written test cases greatly aided me in verifying that code refactoring worked like the old code. When it comes to real life projects with hundeds of modules and millions of lines of code, refactoring without test coverage is like walking into a minefield. If you were diligent enough to write test cases to cover all functions and methods, then you can refactor at will and then run against the accumulated test cases with confidence.

However, one must be clear that test cases guarantee only the cases they cover. Passing the refactored code against the test cases doesn’t guarantee correct outcomes of untested cases. So, be diligent and creative enough to write quality test cases to cover all possible cases to exactly define the behavior of the functions & methods.

Lastly, TDD enforces no optimization. TDD requires you to write simplest possible code to pass all the test cases. Refactor along the way and pass the test cases. Profile when you need performance. That’s not to downplay the importance of throughout & mindful design of algorithms and data structures. One must find a good balance between TDD & efficient implementation that is crafted since the very beginning, not leaving refactoring to the very late stage.

Edit: If the above C++ Set example is oversimplified for you, follow its sibling WordPath https://github.com/vietlq/cpp/tree/master/WordPath

WebSocket Being Served Via NginX Proxy Now

The NginX server from the Ubuntu LTS repo was 1.1.x and did not support WebSockets. The minimum version of NginX that supports WebSockets is 1.3.13. Note that supports comes in the proxy module. I upgraded to 1.4.1 by compiling from source and the proxy module is enabled by default. Now you can taste WebSockets via NginX proxy at http://pico.simpleit.us/ . Only valid users can log in and fully experience it. If you want to test our new app, comment below.

The WebSocket proxy configuration I used:

server {
    listen 80;
    root /var/www/pico.simpleit.us;

    server_name pico.simpleit.us;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }
}

For the curious, here are the configuration flags I used to compile NginX 1.4.1 (remember to install ssl dev packages)

./configure --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --prefix=/usr --pid-path=/run/nginx.pid --error-log-path=/var/log/nginx/error.log --with-openssl=/usr/lib/ssl

References:

https://chrislea.com/2013/02/23/proxying-websockets-with-nginx/

http://stackoverflow.com/questions/16469581/nginx-and-proxying-websockets

SimpleIT Blog is Now Powered by NginX & PHP FPM

NginX Logo
NginX Almighty

After much consideration, I finally moved away from Apache & mod_php to NginX & PHP FPM. The site works much faster and no more I/O & CPU issues. I suspect my Apache was hacked last time, resulting in weird I/O behaviour and spikes in CPU usage.

The next steps:

  1. Install uWSGI for Python and Ruby
  2. Run NodeJS behind NginX proxy
  3. Enable WebSockets for NginX & NodeJS

Why does Apache link sites-enabled/000-default to sites-available/default?

I was cleaning up Apache config and removed the default 000-default & default-ssl from the Apache sites-enabled sub-folder. To my surprise the default page now goes to a web app that I did not expect. It took me a sec to realize 2 things:

  1. The first VirtualHost config that has format *:80 (HTTP) or *:443 (HTTPS) was considered the default site
  2. Apache loaded files in alphabetical order

So I prepended my blog config file with 000- and now blog is the default site across simpleit.us

Edit: This site is now powered by NginX

Solving password input problem with Snow Leopard

Snow Leopard is supposed to be a slimmer and more stable version than Leopard. When it comes to default password input, Leopard is far better as Snow Leopard has a few drawbacks:

1. It cannot input password in languages other than English! Leopard did that just smoothly.

2. It does not have input language at password by default like Leopard. If you have tons of unsaved work and your screen is password protected, you are as good as a dead man.

3. Default Vietnamese input sucks, it does not work well in Apple’s own Safari and also on third party apps such as Chrome, TextMate, TextWrangler, etc.

There’s no guide or mentioning why such problems are still around and what’s ETA for fixes.

Today I’m solving only the issue with login dialog to avoid the issue with locked screen and you have lots of unsaved work and your boss is whistling:

1. Go the System Preferences

System Preferences - Accounts
System Preferences - Accounts

2. Select Accounts

3. Select Login Options

Select Login Options
Select Login Options

4. Unlock & input your password (please revert input to English first)

Input password
Input password

5. Tick the option “Show input menu in login window”

Tick the option "Show input in login window"
Tick the option "Show input menu in login window"

Lock the dialog to save the change. Now every time you login from locked screen, you will see language input on your top right hand corner. Change input to English and input your password. Let your Mac sleep and wake it up to test. No more hassle 😉 !

Migrated DNS to Linode

Previously I hosted all DNS records for simpleit.us with NearlyFreeSpeech (https://www.nearlyfreespeech.net/). They provide cheap DNS hosting at 1c/day per zone. Initially I didn’t hosted my DNS records with Linode as I was interested in redundancy. However, later I realized that I didn’t have any back-up/disaster recovery site on par with the VPS hosted with Linode. The  account I have with NearlyFreeSpeech is meant for low performance and HTML-only content did not match with my VPS. Thus having DNS hosting with NFS didn’t give any extra advantage and I migrated to Linode to centralize my control.

Unlike NFS which gave me freedom to add any kind of records, the Linode DNS manager has the most basic records (SOA, NS, A/AAAA, MX, CNAME, TXT and SRV). That would suffice for any daily needs. I had experience with NFS and Zonomi (DNS host provider). They both require technical details such as the trailing dot “.” at the end of each alias/MX mail server. Linode, however, simplified this and caused little confusion. Linode also does not give as much freedom on cache time like NFS or Zonomi do.

Linode was good at hiding technical details for simplicity and I got all SOA, NS and A/AAAA records populated. That saved me lots of typing effort and time as well. I was able to get my domain and subdomains back to running status within 15 minutes. The only issue I had is all emails I sent to @simpleit.us were rejected with 554 error. I have Google Apps hosted on my simpleit.us and enjoyed using emails like abc@simpleit.us. Linode had MX records interface what were little confusing. I had to read their manual http://library.linode.com/email/google-mail to fix the issue of email rejection. I went little further by adding the SPF record as advised by Google manual: http://www.google.com/support/a/bin/answer.py?answer=33786.

There were handy tools that helped me verify my MX records, you may find them useful too:

http://www.dnsstuff.com/tools/legacy/?formaction=DNSLOOKUP&ToolFormName=customlookup&name=simpleit.us&r=235660&detail=0&type=MX

http://www.dnsquery.org/dnsquery/simpleit.us/MX

If you need to read extra on MX records, refer to Google help: https://www.google.com/support/a/bin/answer.py?answer=174125.

Overall, Linode provides good service and I’ve been using Linode VPS since Feb 2010. Free DNS with many record types is a good deal. If you are curious then check their site at : http://www.linode.com/?r=c046af93bfab8095a43d25a23a88a9ba87403040 (my referral link).

Back for good – New features on Posterous

Was not using this blog for quite a while as it lacked the features I needed such as reordering of the images in a gallery. Today I got an email from Posterous announcing a bundle of new features such as updated web editor, more flexible image/photo management and 2 new themes.

I love the new theme named “Minimal” (currently in use). It’s clean & elegant. There are a few important additions to the themes, such as show on click and show on hover widgets. JS is not yet allowed but let’s hope that there are will be more dynamic widgets to compensate the designers and users.

I’m waiting for the moment when designers can add their own JS snippets. The meta language by Posterous for theme design is not feature rich yet. The editor always lacks the “justified” text alignment feature. Let’s wait for a surprise from them.

Posted via email from Everything about passion

Enabling OpenSSL for Qt C++ on Windows

By default, Nokia Qt does not ship its Qt C++ framework with OpenSSL enabled. This degrades value of the framework and you won’t be able to access HTTPS pages or SSL enabled web services. The examples that require SSL won’t work as well. Take examples\webkit\googlechat for example. This post gives an approach to enable OpenSSL for Qt C++ framework on Windows.

Make sure you have readandbefore proceeding.

  1. Open CMD console & go to the directory C:\Qt\2010.02.1\qt\src\network (given that you have Qt 4.6.2. installed using standard options offered by the installer)
  2. Make sure the directories C:\Qt\2010.02.1\qt\bin and C:\Qt\2010.02.1\mingw\bin are in the environment variable PATH. If you are unsure, enter this command:

set PATH=%PATH%;C:\Qt\2010.02.1\mingw\bin;C:\Qt\2010.02.1\qt\bin;

  1. Configure QtNetwork build using this one-liner:

qmake "PWD=C:\Qt\2010.02.1\qt\src\network" "QMAKE_LIBS_NETWORK = -lws2_32 -lcrypto -leay32 -lssl32 -lssl -lgdi32" "DEFINES += QT_LINKED_OPENSSL" "DEFINES -= QT_NO_SSL"

It’s your responsibility to ensure that OpenSSL is installed and visible to MinGW.

  1. Go ahead with building with this single command:

mingw32-make

Wait for 5-10 mins and you have QtNetwork with OpenSSL enabled. Enjoy!