Posts

Showing posts from April, 2014

Using the return value of a celery task as the result indicator

Sometimes we cannot measure the progress or percentage completed of a job when using celery task (http://iambusychangingtheworld.blogspot.com/2013/07/django-celery-display-progress-bar-of.html). You can only say if it done or not by checking the status of the task.

Here is a case, where the business code returns a failed result, but the task is still executed successfully. How can you tell? Luckily, you can use the result attribute of the task to get the return value of the business code. For example:



Run the celery worker:

celery -A tasks worker --loglevel=info
Try to call the task which will return a fail result:

>> from tasks import add
>> job = add.delay(5,4)
>> job.state
SUCCESS
>> job.result
FAIL

With this you can figure exactly what the result of the task is.

Install and configure JAVA + MAVEN in Ubuntu 14.04

It's pretty easy to install and configure JAVA + MAVEN in Ubuntu 14.04 following these steps:

1. Install OpenJDK 7:

$ sudo apt-get install icedtea-7-plugin openjdk-7-jre openjdk-7-jdk

Check the installed java:

$ java -version

It will show something like:

java version "1.7.0_51"
OpenJDK Runtime Environment (IcedTea 2.4.6) (7u51-2.4.6-1ubuntu4)
OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)


2. Configure JAVA_HOME environment variable:


Open the /etc/environment file:

$ sudo nano /etc/environment
$ source /etc/environment

Add this line:

JAVA_HOME="/usr/lib/jvm/java-7-openjdk-amd64"


Open ~/.bashrc:$ sudo nano ~/.bashrc
Add these lines to the end of the file:export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
$ source ~/.bashrc

3. Install Maven:
$ sudo apt-get install maven
Check the installed maven's version:
$ mvn -version
It will show something like:
Apache Maven 3.0.5Maven home: /usr/share/mavenJava version: 1.7.0_51, vendor: Oracle Corpor…

Happy birthday, 2014

(A girl with smile - Kim Yoon)

"Getting started with MariaDB" (PacktPub) book review

Image
I guess we all know the famous open-sourced DBMS name MySQL. But since Oracle bought Sun Microsystem, the mother company of MySQL, people raises many concerns over the direction of Oracle, a commercial company. They'd already added some closed source extensions to MySQL and bug-fixes without any clear test cases. This attacks the nature of MySQL, an free open-sourced database. Because of that, Michael "Monty" Widenius, the founder of MySQL, decided to folk the source code of MySQL and started the MariaDB.

Widenius built MariaDB as a "drop-in" replacement of MySQL but faster, more efficient, and stay open-sourced. So it's really easy for developers and DBA(s) to move to MariaDB. The people at MariaDB is also working on a separate branch of MariaDB with new features and design without any concerns about the compatibility with MySQL, MariaDB v10. But, if you want to stick with MySQL style, try to use MariaDB v5 instead.

If you are a MySQL developer, a dba, or…

Running the pysaml2 example

Image
Because of a lack of documentations, running the pysaml2 example code is a little bit tricky:


0. Clone the pysaml2 project repo to get the example code:

trinh@trinh-pc:~$ git clone https://github.com/rohe/pysaml2.git

1. You need to install pysaml2 and all of it dependencies, especially the xmlsec1 lib:

trinh@trinh-pc:~$ sudo apt-get install xmlsec1

(myvenv)trinh@trinh-pc:~$ pip install pysaml2

For more information, read this blog post: http://iambusychangingtheworld.blogspot.com/2014/04/install-pysaml2-with-pip.html

2. Install cherrypy:

(myvenv)trinh@trinh-pc:~$ pip install cherrypy

3. Go the example code directory, and start the webserver:

(myvenv)trinh@trinh-pc:~$ cd /pysaml2/example/
(myvenv)trinh@trinh-pc:/pysaml2/example$ ./all.sh start



4. Now, access the service provider in the web browser with this url:

http://localhost:8087/

You will be directed to authenticating process and get the information of the user account:







5. To stop the server:

(myvenv)trinh@trinh-pc:/pysaml2/example$ ./all.sh sto…

Install pysaml2 with pip

You need these packages to be able to install pysaml2 successfully with pip:

trinh@trinh-pc:~$ sudo apt-get install libxml2-dev libxmlsec1-dev libffi-dev xmlsec1

trinh@trinh-pc:~$ source /myvenv/bin/activate

(myvenv)trinh@trinh-pc:~$ pip install repoze.who xmlsec mako python-memcache

(myvenv)trinh@trinh-pc:~$ pip install pysaml2




Install xmlsec with pip

To be able to install xmlsec with pip, you need these packages:

trinh@trinh-pc:~$ sudo apt-get install libxml2-dev libxmlsec1-dev

trinh@trinh-pc:~$ source /my-virtualenv/bin/activate

(my-virtualenv)trinh@trinh-pc:~$ pip install xmlsec

SSH connection fail-overs between SSH servers using Paramiko

I'm using this utility class to make ssh connection in python using Paramiko module with connection fail-overs between ssh servers. Quite simple:






References: 
[0] http://jessenoller.com/blog/2009/02/05/ssh-programming-with-paramiko-completely-different
[1] http://sebastiandahlgren.se/2012/10/11/using-paramiko-to-send-ssh-commands/
[2] http://www.lag.net/paramiko/

Getting started with Cassandra

Image
Apache Cassandra™ is a massively scalable open source NoSQL database. Cassandra is perfect for managing large amounts of structured, semi-structured, and unstructured data across multiple data centers and the cloud. Cassandra delivers continuous availability, linear scalability, and operational simplicity across many commodity servers with no single point of failure, along with a powerful dynamic data model designed for maximum flexibility and fast response times.

For more information about Cassandra's architecture, please read this documentation:  http://www.datastax.com/documentation/cassandra/2.0/cassandra/gettingStartedCassandraIntro.html

If you want to give it a try without having to allocate a big amount of resource, you can use the pre-built Cassandra virtual machine which can be downloaded at https://s3.amazonaws.com/planetcassandra-downloads/Cassandra-2.0.6.ova


0. First, there are some terms in Cassandra data model you need to know in comparison with Relational model

(src: …

Install psycopg2 inside a virtualenv environment

Image
To be able to install psycopg2 (python postgresql driver) inside a virtualenv environment, you need to install these packages first:

$ sudo apt-get install libpq-dev python-dev

or you will get this error:



With those packages installed, you can now install psycopg2 in virtualenv:

(myvenv) $ pip install psycopg2


Reset MySQL root's password

If you just forgot your MySQL root's password like me yesterday, you can reset it. Don't panic.

Notes:For this method to work, you need to have root access to your server.

1. Stop the MySQL server:
# /etc/init.d/mysql stop
# service mysql stop
2. Start MySQL server without requiring any privileges (aka. password) with mysqld_safe:
# mysqld_safe --skip-grant-tables &
3. Connect to MySQL server:
# mysql -u root
4. Reset root password:
mysql > use mysql;mysql > update user set password=PASSWORD("MY-NEW-ROOT-PASSWORD") where user='root';mysql > flush privileges;mysql > quit
5. Stop and start MySQL server:
# /etc/init.d/mysql stop# /etc/init.d/mysql start
6. Try to connect to MySQL server with the new root password:
# mysql -u root -p'MY-NEW-ROOT-PASSWORD'

Cool!

Fix Nginx error upstream timed out when upgrading Wordpress

Today, I tried to upgrade my blog to the latest version of wordpress, 3.9. But, for some reasons, the page was just blank after I click the Update button in the dashboard. I tracked down the error logs of Nginx and found this error message:

2014/04/19 09:58:28 [error] 2447#0: *14 upstream timed out (110: Connection timed out) while reading upstream, client: 222.255.207.82, server: www.mydomain.com, request: "POST /wp-admin/update-core.php?action=do-core-upgrade HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "mydomain.com", referrer: "http://mydomain.com/wp-admin/update-core.php"

So, the issue is caused by the short timeout setting of my nginx server block. To fix that, try to add these 2 red lines to the server block:

/etc/nginx/sites-available/myserver:

...
      location / {
          ...
proxy_read_timeout 300;
        ...
      }

...

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path…

How to output raw html text with Django template

By default, Django escapes or quotes all the text outputs in the template because of the security matter. It means that if Django template will render any html tag as text, not as html tag. But, if you want, you can display those html outputs as html tags using the Django built-in template tags: safe

For example, I will return this object to the template with:

myobject.mytextfield = '<strong>This is my text</strong>'

+ Without safe filter:

{{ myobject.mytextfield  }}

will be rendered as:

"<strong>This is my text</strong>"

+ With safe filter:

{{ myvar.mytextfield|safe }}

will be rendered as:

"This is my text"

Reference:https://docs.djangoproject.com/en/dev/ref/templates/builtins/#safe

I Love You

I Love You by Chris Hart




ねぇ 君はなぜ 哀しそうに うつむくの? Ne~ kimi wa naze kanashisou ni utsumuku no? Hey, why do you hang your head so sadly? まぶしいほど 青い空 なのに mabushii hodo aoi sora nanoni Even though the sky is dazzlingly blue いつからだろう? 君と手を つないでも itsu kara darou kimi to te o tsunai de mo When did it happen? Even when we were holding hands. ギュッと握り返してはくれないんだね gyutto nigiri kaeshite wa kure nain da ne You didn't tightly hold my hand back. 何を言えたなら あの日に帰れるの? Nani o ietanara ano hi ni kaereru no ? What could I have said to get back to that day? 胸を埋め尽くす不安だけが mune o umetsukusu fuan dake ga Only the uneasy feeling buried in my heart. 泣いても 泣いても 消えてくれないの naite mo naite mo kiete kurenai no No matter how much I cry, it doesn't go away. I love you I love you I need you ずっと愛されたいあの 頃のように I love you I love you I need you zutto aisaretai ano koro no you ni I love you, I need you, I want to be loved forever, like we were then. 叶わない願いでも この気持ちはいつもそうその胸に届 いていますか? kanawanai negai demo kono kimochi wa it…

No sound issue in Ubuntu 14.04

It is an issue with Ubuntu 14.04 beta. I cannot listen any music or videos because the system showed no output device in the audio settings or just some dummy output. There is one way to fix that is to make sure my user is in the pulse and pulse-access group which provide access to audio driver.

To see which groups I am in:

$ sudo grep trinh /etc/group

adm:x:4:trinh,syslog
cdrom:x:24:trinh
sudo:x:27:trinh
dip:x:30:trinh
www-data:x:33:trinh
plugdev:x:46:trinh
lpadmin:x:112:trinh
trinh:x:1000:
sambashare:x:124:trinh


Add myself to pulse and pulse-access group:

$ sudo usermod -a -G pulse,pulse-access trinh


And now I can hear the sound again!




Adding a transparent layer for a PNG image with GIMP

To enable transparent layer for a PNG image in GIMP, you need to add the Alpha Channel to that image:

>> Layer (menu) >> Transparency >> Add Alpha Channel


Move created layer to below the image and you can adjust, delete or modify parts of the original image to show the transparent background.

How to remove delegated Gmail account

The only one that granted my account access permission to her Gmail account can remove me from her delegates list. But, I cannot contact that person anymore. Fortunately, I found a way to not receive emails sent to that account:

1. From my Gmail account web interface, search for the email which has subject like: ABC has granted you access to their Gmail account -- accept or deny? (the email that ask me to accept or reject the permission grant)


2. Then click the reject request linkin that email.

3. Refresh the GMail web page.

Using SimpleSAMLphp to authenticate users in Google Apps with Active Directory

To let my users login to Google Apps using Active Directory accounts, I can use SimpleSAMLphp as an IdP which gets identities from my Active Directory servers. Things are pretty easy with simplesamlphp, of-course:

1.  SimpleSAMLphp configurations:

simplesamlphp/config/authsources.php:



simplesamlphp/config/config.php:




simplesamlphp/www/logout_relay.php: (because Google does not allow log out URLs that have question mark and parameters)




2. Google App settings:

>> Go to https://admin.google.com and login as my Google App administrator account.

>> Security -> Advanced Settings -> Setup Single Sign-on (SSO):


+ Check "Enable Single Sign-on"

+ Sign-in page URL *

https://mydomain.com/simplesaml/saml2/idp/SSOService.php

+ Sign-out page URL *

https://mydomain.com/simplesaml/logout_relay.php

+ Change password URL *

https://mydomain.com/


>> Click "Save changes"


3. NGINX server block for simplesamlphp:




References:

Install and customize ZSH shell in Xubuntu

Image
ZSH shell is a Unix shell comes with a lot of improvements compare with the bash shell. I want to try this shell in my computer since I was pleased by the new experience I had with ZSH in one of my DigitalOcean VPSs.

1. Install ZSH shell:

$ sudo apt-get update && sudo apt-get install zsh
$ wget –no-check-certificate https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O – | sh
$ chsh -s /bin/zsh

=> Restart the computer


2. Customize the prompt's display of zsh as following: to make it's easier to work with long directory paths



Modify the ~/.zshrc:



Apply the changes:

$ source ~/.zshrc

References:
[0] http://www.zsh.org/
[1] http://en.wikipedia.org/wiki/Z_shell
[2] http://www.bash2zsh.com/essays/essay1_file_manager.html
[3] http://monangik.wordpress.com/2011/04/21/install-zsh-shell-on-ubuntu/