piątek, 10 czerwca 2011

Ruby: installing mysql2 gem on Ubuntu 11.04

I have just faced problem with the installation of the mysql2 gem on my newly installed ubuntu 11.04 natty box.

Fortunately I was abel to solve it by installing the following additional packages:
  • libmysqlclient-dev
  • libmysql-ruby1.9
  • ruby1.9.1-dev
Afterwards the installation/compilation of native code went through fine - although there is some error reported during installation of rdoc but it does not affect the library itself (only the documentation might be corrupted - seems to be a bug). Below the list of commands invoked in my environment:

root@krystianekb:/var/log# apt-get install libmysqlclient-dev
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following NEW packages will be installed:
  libmysqlclient-dev
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 3,174 kB of archives.
After this operation, 9,839 kB of additional disk space will be used.
Get:1 http://pl.archive.ubuntu.com/ubuntu/ natty/main libmysqlclient-dev amd64 5.1.54-1ubuntu4 [3,174 kB]
Fetched 3,174 kB in 12s (256 kB/s)                                            
Selecting previously deselected package libmysqlclient-dev.
(Reading database ... 147843 files and directories currently installed.)
Unpacking libmysqlclient-dev (from .../libmysqlclient-dev_5.1.54-1ubuntu4_amd64.deb) ...
Processing triggers for man-db ...
Setting up libmysqlclient-dev (5.1.54-1ubuntu4) ...

root@krystianekb:/var/log# apt-get install libmysql-ruby1.9 ruby1.9.1-dev
Reading package lists... Done
Building dependency tree      
Reading state information... Done
Note, selecting 'libmysql-ruby1.9.1' for regex 'libmysql-ruby1.9'
The following NEW packages will be installed:
  libmysql-ruby1.9.1 ruby1.9.1-dev
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,222 kB of archives.
After this operation, 4,071 kB of additional disk space will be used.
Get:1 http://pl.archive.ubuntu.com/ubuntu/ natty/universe libmysql-ruby1.9.1 amd64 2.8.2-1 [55.8 kB]
Get:2 http://pl.archive.ubuntu.com/ubuntu/ natty/universe ruby1.9.1-dev amd64 1.9.2.0-2 [1,166 kB]
Fetched 1,222 kB in 4s (249 kB/s)         
Selecting previously deselected package libmysql-ruby1.9.1.
(Reading database ... 147898 files and directories currently installed.)
Unpacking libmysql-ruby1.9.1 (from .../libmysql-ruby1.9.1_2.8.2-1_amd64.deb) ...
Selecting previously deselected package ruby1.9.1-dev.
Unpacking ruby1.9.1-dev (from .../ruby1.9.1-dev_1.9.2.0-2_amd64.deb) ...
Setting up libmysql-ruby1.9.1 (2.8.2-1) ...
Setting up ruby1.9.1-dev (1.9.2.0-2) ...
 

root@krystianekb:/var/log# gem1.9.1 install mysql2 
Building native extensions.  This could take a while...
Successfully installed mysql2-0.3.2
1 gem installed
Installing ri documentation for mysql2-0.3.2...
Enclosing class/module 'mMysql2' for class Client not known
Installing RDoc documentation for mysql2-0.3.2...
Enclosing class/module 'mMysql2' for class Client not known
root@krystianekb:/var/log#

wtorek, 7 czerwca 2011

Ruby: umlify - generate UML model out of the source code

Recently I was looking for some way to generate the UML model out of the source code since I believe this is the best way to keep it up to day (and also the only way ;)). It seems that there is already a gem available that can be used for that purpose - it is called umlify.

In order to install it one needs to invoke:

# gem install umlify
Successfully installed sexp_processor-3.0.5
Successfully installed ruby_parser-2.0.6
Successfully installed umlify-1.2.6
3 gems installed
Installing ri documentation for sexp_processor-3.0.5...
Installing ri documentation for ruby_parser-2.0.6...
Installing ri documentation for umlify-1.2.6...
Updating class cache with 2570 classes...
Installing RDoc documentation for sexp_processor-3.0.5...
Installing RDoc documentation for ruby_parser-2.0.6...
Installing RDoc documentation for umlify-1.2.6...

That's it - now it is only to invoke it. If it is on your path the just invoke:
# umlify ./IpStatistics/*.rb

Afterwards the uml.png image containing the UML model will be generated. Below I enclose example part of the UML generated out of my IpStatistics project.

czwartek, 2 czerwca 2011

Gource: visualization of changes in your project


Today I discovered a nice tool Gource (http://code.google.com/p/gource/) that in a graphical way presents the changes done over time in your project. It currently supports the most popular source control systems (e.g. svn, git, mercurial).

What one needs to do in order to generate a nice looking vid (like the one attached below that represents my IpStats project)? Not much:
  1. Download and install gource from the URL provided above (Windows and Linux versions available)
  2. Checkout the project (e.g. svn co )
  3. Invoke gource: krystianek@ubuntu:~/projects$ gource -800x480 IpStatistics/
  4. Or generate mp4 vid using gource and ffmpeg:
krystianek@ubuntu:~/projects$ gource -800x480 IpStatistics/ -o - | ffmpeg -y -b 3000K -r 60 -f image2pipe -vcodec ppm -i - -vcodec libx264 -threads 0 IpStats_800x480.mp4
 

The quality is not perfect but shows the main idea - I guess it is due to the compression used on blogger because the original quality is much, much better ;)

One remark is that the ffmpeg shall support the x264 codec (which was not the case for the package from repository on my ubuntu box). All is needed to do it on ubuntu box can be found here: http://ubuntuforums.org/showthread.php?t=786095

niedziela, 29 maja 2011

Ubuntu: reduce memory consumption - HP ZE5501US with 512 MB

Running Ubuntu 11.04 on my old laptop (although equipped with Pentium 4 and 512 MB of RAM) becomes difficult since the OS is terribly slow. After some analysis it came out that the reason is the 512 MB of RAM (64 MB consumed by the graphic card) which is far too less for the default configuration. Fortunately there is an easy solution to free ~100 MB of additional RAM by disabling the startup applications for gnome environment. You can do it:
  1. Ubuntu Classic: System->Preferences->Startup Applications
  2. Unity: you can just type 'startup' in the search field
In my case I disabled most of the apps (apart from power manager and few other ones). Below you can see how the window looks like:

Ubuntu: remove service from automatic startup

I have recently installed a BOINC client on my Ubuntu box. However later on I decided to turn it off but all I could do is to turn off the service and not disable it at the automatic startup.

After some search on the web I found this link - which is in my opinion very helpful: http://www.unixtutorial.org/2009/01/disable-service-startup-in-ubuntu/

Example from my box:

root@ubuntu:~# update-rc.d -f boinc-client remove
 Removing any system startup links for /etc/init.d/boinc-client ...
   /etc/rc0.d/K20boinc-client
   /etc/rc1.d/K20boinc-client
   /etc/rc2.d/S20boinc-client
   /etc/rc3.d/S20boinc-client
   /etc/rc4.d/S20boinc-client
   /etc/rc5.d/S20boinc-client
   /etc/rc6.d/K20boinc-client

sobota, 28 maja 2011

Ruby: Logging to STDOUT and file using standard ruby Logger

Have you ever wanted to log the message from your application to both standard output and file? If you have and you are using in your application the Logger that comes together with Ruby this article will be helpful for you.

First of all the description of the Logger can be found in the rdoc (http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/). Some of the features are listed below:
  • logging on multiple levels: FATAL, ERROR, WARNING, INFO, DEBUG
  • logging to a user defined device
  • custom formatting
But it does not provide functionalities similar to log4j like appenders, categories etc. (if you would like to use these the logging gem will be helpful: http://logging.rubyforge.org/).

The standard ruby Logger unfortunately does not provide functionality to log to multiple devices out of the box. In my opinion the simplest approach is to define an extension to the Logger like the one below (actually this can be used by you 1:1).

require 'logger'
class EnhancedLogger < Logger

  def initialize(logdev, shift_age = 10, shift_size = 1048576)
    super(logdev, shift_age, shift_size)
    @stdout = nil
    if (logdev == STDOUT)
      @stdout = STDOUT
    end
  end

  def logStd(severity, message = nil, progname = nil, &block)
    msg = format_message(format_severity(severity), Time.now, progname, message)
    self<<(msg)
    if (@stdout != nil)
      @stdout.puts(msg)
    end
  end
end


The EnhancedLogger class extends the standard Logger with an additional function called logStd, which in addition to the standard log device defined in the constructor displays the message also on the STDOUT. One can also think about similar function for STDERR (if required of course).

środa, 25 maja 2011

Encryption/decryption of data in Java

Recently I came to the problem that the one of my colleagues utilities had to store some sensitive data (password) in it's configuration file (property file). Storing it it plain text would be a serious security flaw and therefore we had to find a way to store the password in an encrypted form using some key. Fortunately it is not that difficult in java - the apache common codec library becomes useful here. It provides much more functionality but what we used is the base64 encoding/decoding.

Below I enclose the example logic for encrypting the password:
    public byte[] encryptPassword(String pass, String key) throws Exception {
        DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secKey = keyFactory.generateSecret(keySpec);
        byte[] cleartext = pass.getBytes("UTF8");     

        Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe
        cipher.init(Cipher.ENCRYPT_MODE, secKey);
        return Base64.encodeBase64(cipher.doFinal(cleartext));
    }


And to decrypt the password one can use the following method:
    public byte[] decryptPassword(String encryptedPass, String key) throws Exception {
        DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secKey = keyFactory.generateSecret(keySpec);

        byte[] encrypedPwdBytes = Base64.decodeBase64(encryptedPass);

        Cipher cipher = Cipher.getInstance("DES");// cipher is not thread safe
        cipher.init(Cipher.DECRYPT_MODE, secKey);
        return (cipher.doFinal(encrypedPwdBytes));
       
    }


I hope that these code snippets would be helpful also for you.