Friday 31 August 2012

Try protojs serials : B. support C++

I will develop a web site using C++. I need a compiler for generating C++ codes from group.proto file.
A standard protocol buffer from google will be installed. I didn't describe how to install this here. Please refer to the official site:
http://code.google.com/p/protobuf/

Create a build.sh file under proto folder. Add some scripts into it.


rm output/js/*
rm output/c++/*
protoc --proto_path=./input --cpp_out=./output/c++ ./input/group.proto
pbj input/group.proto output/js/group.js

chmod +x ./build.sh

Now execute build.sh, you will see C++ and JavaScript files are both generated.

proto/
├── build.sh
├── input
│   └── group.proto
└── output
    ├── c++
    │   ├── group.pb.cc
    │   └── group.pb.h
    └── js
        └── group.js

4 directories, 5 files

Try protojs serials : A. install

Protojs is a JavaScript add-on for protocol buffer. I try it because I want to replace JSON with protocol buffer when developing web site later.

This article shows how to setup protojs and test it on Ubuntu.

Clone the repository from github first:
sudo -s
cd /usr/src
git clone https://github.com/sirikata/protojs.git

Compile it.
curl is required.install it using 'apt-get install curl'.
cd protojs
./bootstrap.sh  (This will download antlr-3.2 from antlr.org)
make

After a while, I got pbj binary. It's a protojs compiler.

Add the following into ~/.bashrc file

export PBJ_HOME=/usr/src/protojs
export PATH=$PBJ_HOME:$PATH


source ~/.bashrc


Let's use it. I have a folder tree looks like so:


proto/
├── input
│   └── group.proto
└── output
    ├── c++
    └── js

4 directories, 1 file

I defined a group.proto for generating C++ and JavaScript files.

package freebird;

message group {
  required string id = 1;
  required string name = 2;
  required string description = 3;
  required string device_number = 4;
}

In proto folder, execute the following command to generate js file:
pbj input/group.proto output/js/group.js

group.js 's content:



"use strict";
/** @suppress {duplicate}*/var freebird;
if (typeof(freebird)=="undefined") {freebird = {};}

freebird.group = PROTO.Message("freebird.group",{
id: {
options: {},
multiplicity: PROTO.required,
type: function(){return PROTO.string;},
id: 1
},
name: {
options: {},
multiplicity: PROTO.required,
type: function(){return PROTO.string;},
id: 2
},
description: {
options: {},
multiplicity: PROTO.required,
type: function(){return PROTO.string;},
id: 3
},
device_number: {
options: {},
multiplicity: PROTO.required,
type: function(){return PROTO.string;},
id: 4
}});








Monday 27 August 2012

Nginx with basic authentication

Assume we have Nginx installed on Ubuntu 12.04 server. Now I will show you how to configure HTTP basic authentication in Nginx.

step1:
Open conf/nginx.conf file, and add some directives into locatiion / section:

        location / {
            auth_basic "Restricted";
            auth_basic_user_file pwd;
            ...
        }

"Restricted" word will appear in the popup dialog when you try to access the Nginx site the first time.
pwd is a file located at conf folder. Note if you type conf/pwd here, the pwd file must be located at /conf/conf folder.

step2:
Create a pwd file. Add your user name and password(plaintext).
The password should be replaced by the step3.
chenshu:770328

step3:
Install Apache2 tools.
apt-get install apache2-utils

step4:
Update password using apache2 tools.
htpasswd /usr/nginx/conf/pwd chenshu
You will be asked to input password twice.
Now the pwd file's content changed:
chenshu:$apr1$I2FIVtPG$I51oSU4eatH.tJdnmxG6K0

step5:Restart Nginx service.
service nginx restart

step6: Log in your site.
Very easy!




Saturday 25 August 2012

Flexigrid example two:in-place editor

Sometimes we want to edit the flexigrid' data. An in-place editor is required, don't need to pop up a dialog any more. I will show you how to do this here.

I use jquery-in-place-editor library. Please refer to the official site:
http://code.google.com/p/jquery-in-place-editor/

step1: add a process function for the column of flexigrid when you define the flexigrid model.


$(document).ready ( function() {
    $("#displays").flexigrid (
{
   url: '<%=jsonp%>/bindedDisplays',
   method:'POST',
   dataType: 'json',
   width: 400,
   height: 300,
   colModel : [
{hide: '<%=check%>', name: 'check', width: 30, sortable: true, align: 'left'},
{display: 'ID', name: 'id', width: 90, sortable: true, align: 'left'},
{display: '<%=description%>', name: 'description', width: 110, sortable: true, align: 'left',process:editDescription},
{display: '<%=status%>', name: 'status', width: 20, sortable: true, align: 'left'},
{display: '<%=unbind%>', name: 'unbind', width: 20, sortable: true, align: 'left',process:unbindDisplay}
   ]
}
    );
}
 );

step2:Implement the editDescription function using jquery-in-place-editor


function editDescription(celDiv, id){
    $( celDiv ).click( function() {
var idTd = $(celDiv).parent().parent().children()[1];
$(celDiv).editInPlace({
   url: "update_description",
   params: "address="+$(idTd.children).html(),
   error:function(obj){
alert(JSON.stringify(obj));
   },
   success:function(obj){
var str = m[JSON.parse(obj).status+""][window.curLanguage];
alert(str);
$("#displays").flexReload();
   }
});
    });
}

$(celDiv).editInPlace will let see an in-place editor effect in web page.
Ajax request will be sent to the web server's ./update_description path via jquery-in-place-editor.

Very easy! You also need to include the necessary js file like so:

 <script type="text/javascript" src="script/jquery.editinplace.js"></script>



Flexigrid example one: load data without binding server-side request

Let me explain my term here-- bind flexigrid to server-side request
You will find there are a lot examples on Internet, they all use url property like so:

$("#displays").flexigrid(
{
url: 'load_all_displays',
method:'POST',
dataType: 'json',
width: 400,
height: 420,
colModel:[
{display: 'check', name: 'check', width: 30, sortable: true, align: 'left'},
{display: 'ID', name: 'id', width: 90, sortable: true, align: 'left'},
{display: 'description', name: 'description', width: 110, sortable: true, align: 'left'}
]
}
);

The above red words means flexigrid will send a POST request to the load_all_displays path of current web server. Server side should return the data to flexigrid as expected format.

This is easy to understand and good example. But I have good reasons to avoid this way.
Reason 1:
I develop a web service, it will send data to client( browser or not browser). Web server is responsible for generating data, and the client should convert it into expected format for rendering. Maybe one developer use flexigrid to present its data, another guy prefers to use HTML5 table.

Reason 2:
I need a way to add/delete/modify the flexigrid's row without calling server-side API. All these operations should only occur in client-side. After all these are done, client calls server-side API to update data one time.

In my way, JavaScript codes will get all data from web server, translate it into correct format, add the value into flexigrid. Let's see my example below:


var testData = {
"page": 1,
"total": 20,
"rows":
[
{"cell" : ["A", "a group", "0", "d"]},
{"cell" : ["B", "b group", "0", "d"]},
{"cell" : ["C", "c group", "0", "d"]},
{"cell" : ["D", "d group", "0", "d"]}
]
}

function config(groupName, description, deviceNumber, del) {
$("#groups").flexigrid (
{
dataType: 'json',
width: 580,
height: 300,
colModel : [
{display: groupName, name: 'groupName', width: 160, sortable: true, align: 'left'},
{display: description, name: 'description', width: 200, sortable: true, align: 'left'},
{display: deviceNumber, name: 'deviceNumber', width: 100, sortable: true, align: 'left'},
{display: del, name: 'del', width: 40, sortable: true, align: 'left'}
]
}
);
}

function fillData() {
$("#groups").flexAddData(testData);
}


The config function will be called after page loading is done. You will see the simple format for flexigrid in testData variable.
If JavaScript calls fillData function, the testData will be added into flexigrid. Also we need to define a table in web page as following:

<table id="groups" style="display:none" bgcolor="#c4c4c4"></table>

My solution needs more codes, but it also let you control all things when developing your own web site.



Wednesday 22 August 2012

Installing Jenkins plugin manually

We used Jenkins as our main CI management tools. It worked fine. But today I found two Jenkins can't upgrade/install plugins.
No way to fix this problem, because we didn't change the configuration , they worked fine before.
After searching in google for a while, I found I can install plugins manually. Let's do it.
Refer to the wiki:https://wiki.jenkins-ci.org/display/JENKINS/Plugins
Log into Jenkins server via SSH, we have tomcat7 installed on Ubuntu server. The Jenkins home folder is located at /usr/share/tomcat7/.jenkins
Enter the plugins folder under Jenkins' home folder.
Download the plugin's hpi file here, e.g.
wget http://mirror.xmission.com/jenkins/plugins/pathignore/0.6/pathignore.hpi

Then reload Jenkins app in tomcat7 admin page, go to the Jenkins page to watch if the plugin is installed or not. I can't see the pathignore plugin in installed tab. Why?
Because pathignore plugin depends on another plugin called ruby-runtime.
Download this plugin using the same way.
wget http://updates.jenkins-ci.org/download/plugins/ruby-runtime/0.10/ruby-runtime.hpi

Reload Jenkins again, it works.


Tuesday 21 August 2012

Upgrade Jenkins on Ubuntu12.04

I just installed Jenkins on my Ubuntu 12.04 server using 'apt-get install jenkins' command.
But it's too old.
How to upgrade it?

Remove your existing jenkins first.
apt-get autoremove jenkins

Follow the steps from official site:


This is the Debian package repository of Jenkins to automate installation and upgrade. To use this repository, first add the key to your system:

wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
Then add the following entry in your /etc/apt/sources.list:
deb http://pkg.jenkins-ci.org/debian binary/
Update your local package index, then finally install Jenkins:

Run two commands below:
apt-get update
apt-get install jenkins

Very easy!

Monday 13 August 2012

Setup Ubuntu Cloud in VirtualBox:3.Add node server

In the previous article, I installed maas server as master successfully, but there is no node server in it. This time I will install three node server. They are still Ubuntu 64bit server.

The dashboard of maas server shows a warning message:
Some of the required system profiles are missing. Run the maas-import-isos script to import Ubuntu isos and create the related profiles:
sudo maas-import-isos

Run the above command, it will download some files from ubuntu servers.
reboot maas server when this command completed. The warning message disappeared.

Fixing this problem is required, otherwise you can't add node server successfully.


First, create a VM for Ubuntu server as usual. I will call it as node1.
Then choose "Multiple server install with MAAS" in the startup menu.
Set the hostname to node1.
Now join the maas server as the MAAS:
You will see the installation SIGKILLs all processes again, but this time no error occured. Refresh the dashboard page, this node has been added into maas. See diagram below:


Now, I continue adding second node server into maas. But this time I didn't see the first option:
"maas MAAS Server -(10.112.x.175:80)"
I choosed "Specify MAAS by name or address" option and entered IP address:10.112.x.175.
The second node was added successfully.

Repeated it again, the third node was added.




Setup Ubuntu Cloud in VirtualBox:2.Install MAAS server

Now, before launching Maas VM, download the Ubuntu 12.04 amd64 server image first.
Then configure it as Mass VM's storage like so:

Start this VM now. Select "Multiple server install with MAAS" in the grub menu. I set the hostname to "mass". And select "Create a new MAAS on this server". The left steps are similar to install normal ubuntu server.

At the end of installation, you will see some messages below:


Let's start mass1 server now. I tried to access it in my browser using the above URL:
http://10.0.2.15/MAAS, but failed.

I changed the network settings of this VM.

Launch maas1 server again and retry it. Unfortunately, I still failed. Because my computer connects to Internet via dial-up program in my home, when my maas1 server launched, it can't connect to Internet using dial-up program because my computer is using it. mass1 server can't get correct IP address. I need to buy a router to set up a small network to solve this problem.

OK, after adding a new router, I configured static IP address for my maas1 server:10.112.x.175.
Open /etc/network/interface file

change "iface eth0 inet dhcp" to "iface eth0 inet static"
add three lines below:
address 10.112.*.175
netmask 255.255.255.0
gateway 10.112.*.254

you can get the gateway for your network in other computer using this command:
route -n

now restart the networking service:
/etc/init.d/networking restart

Execute the following command to reconfigure maas server:
sudo dpkg-reconfigure maas
enter 10.112.x.175 in Package configuration text area.

Now I can access the admin page in my browser:
http://10.112.x.175/MAAS


Following the suggestion of this page, I create an admin user for MAAS.
sudo maas createsuperuser
enter user name: cs
enter email address: csfreebird@gmail.com
enter password twice.
I can log into the web site now. And see 0 node in MAAS.




Saturday 11 August 2012

Setup Ubuntu Cloud in VirtualBox:1.Create VirtualBox server

I new project is to setup a cloud environment for all projects in my company. After attending OpenStack APAC conference in Beijing, I heard of Canonical provide good solution for setting up public/private cloud. This solution is MAAS(Metal as a service).
I will try it in my VirtualBox which was installed in my Ubuntu 12.04 Desktop.

We can find some good wizard documents from official site:
https://help.ubuntu.com/community/UbuntuCloudInfrastructure

Here also is a good document that introduces how to set up MAAS in virutabox:
http://marcoceppi.com/2012/05/juju-maas-virtualbox/

Ok, let me try to install my first MAAS server.
Open VirtualBox, and create a new VM named as mass for Ubuntu 64bit server.


Use the default memory size, I don't know what size is best, but I can change it after installation.
Create a new disk for this server

Use VDI as file type

Storage can be allocated dynamically

After clicking some next buttons, I create the server successfully.

Now, I will install MAAS server.




Thursday 9 August 2012

Cross GFW with Chrome

In my previous article, I used FireFox to cross GFW. But my favorite browser is Chrome.
There are a few documents that describe how to cross GFW using Chrome and SSH, they advanced to install swithy proxy plugin. But it works in windows, not in Ubuntu.
After googling for a while, I found switchy sharp plugin. It worked for me.
Below are my configuration diagrams.



Wednesday 8 August 2012

To cross GFW using SSH


Today, I tried to use SSH Forwarding to cross GFW. I don't care Chinese politics, but GFW prevent me from accessing a few good sites, e.g. google search engine, google document, google blogger, facebook, etc.

I succeeded after trying 1 hour. Here is my steps:
My OS is Ubuntu12.04 AMD64 bit Desktop.

1.You should get a remote server outside of China
My company rents a Ubuntu server from Amazon, sshd server is installed already.

2.Using the following command to launch ssh forwarding:
ssh -N -v -i mykey.ssh -D localhost:8888 ubuntu@amazon_server

Some explanations below:
-N means don't execute remote command
-v will display all details for debugging
-i mykey.ssh is my identity file for logging amazon server
-D means dynamic port forwarding
localhost:8888 means the local ssh will listen 8888 port on localhost
all requests to localhost:8888 will be forwarded to amazon_server
ubuntu is the username of amazon_server

3.Configure firefox to access localhost:8080 using socket5.

clieck Edit/Preferences menu, then select Advanced tab, click Network and press the Settings button for Connection.
Configure the dialog as following:


4. Remove any proxy plugin installed in Firefox. This costs me 30 minutes.

Enjoy it now.



Sunday 5 August 2012

China Merchants Bank Professional Edition with Virtual Box

This article describes how to use China Merchants Bank Professional Edition on Ubuntu and VirtualBox.

Environment:
1.Ubuntu 12.04 64 bit desktop edition
2.Oracle VM VirtualBox 4.1.18
3.Windows XP is installed in VirtualBox

Because China Merchants Bank Internet Banking's Professional Edition doesn't support Linux for now. For our Linux users, we have two choices, wine it or using it in VM environment. Wine is not stable, running games on Wine is a good idea, but Internet Banking is not good. I choose VM solution.

Here is my steps:
1.Install the newest version of VirtualBox Download it from official site-- https://www.virtualbox.org
You will see the AMD64 edition for Ubuntu12.04, download it and install it with double-click, very easy.
2.It's easy to install Windows in VirtualBox, I don't describe it here.
3.For supporting USB 2.0, you should install Extension Pack first. Download it from official site, right click it, select VirtualBox to open it. VirtualBox will install it for you.
4.Insert your USB key retrieved from China Merchants Bank now. Launch the VirtualBox and open the settings dialog. Choose USB menu, you will see the configuration for USB. Enable USB and USB2.0 controller. Now, add a USB device filter like so:


Note, you should insert USB key before launching windows in VM every time.

5.Launch your windows in VirtualBox now, enjoy it. Don't forget to install professional edition for China Merchants Bank.







Friday 3 August 2012

Followers

Contributors