Monday, 7 October 2013

CakePHP locking tables

Here are my two cents on the issue.

The code below is a function from a behaviour that tries to create an additional unique key on a field named code, by counting the number of records created this year. The important part in the locking procedure is that we must specify the AS clause in the LOCK TABLES statement or otherwise the $model->find() function will not work complaining that the table is locked.

    public function getNextCode(&$model)
    {
        $thisYear = date('Y');
        $dbo = $model->getDataSource();
        $dbo->execute(
            sprintf('LOCK TABLES %s AS %s WRITE;',
                $model->table,
                $model->alias
            )
        );
        $recordsThisYear = $model->find(
            'count',
            array(
                'recursive' => -1,
                'conditions' => array(
                    $model->alias .'.code LIKE' => $thisYear.'%'
                )
            )
        );
        $dbo->execute('UNLOCK TABLES');
        return sprintf('%d-%06d', $thisYear, $recordsThisYear + 1);
    }

The original idea for the post and function cake from a doWeb posting available through here.

Friday, 14 June 2013

Using a raspberrypi as an sftp server

Following a previous post regarding how to use your raspberry-pi device as a file server, we are going to continue amd set up sftp service on the same pi device, so that it may be accessible over WAN.

The complete guide comes from a Mark Van den Borre posting available through this link.In our case however the steps are fewer, since raspberry has already the openssh server set up and running and if you have followed from the previous port we already have a user (bill) and a group (microsoft) to use for sftp service.

To get started let;s give our friend Bill a password:

pi@xena ~ $ sudo passwd bill 
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

Next step will be to prevent Bill from interactively logging in. The usual remedy to this problem to use the sftp server as a login shell. After the post is over bill will not be able to access our pi from ssh either

pi@xena ~ $ sudo chsh bill 
Changing the login shell for bill
Enter the new value, or press ENTER for the default
        Login Shell [/usr/lib/tftp-server]: /usr/lib/sftp-server
pi@xena ~ $ 

Now for the sftp configuration itself. (Copying, pasting and adjusting from Mark's post we have something like this:) Open the default OpenSSH server configuration for editing:

pi@xena ~ $ sudo vi /etc/ssh/sshd_config

: and change the default sftp server from:

Subsystem sftp /usr/lib/openssh/sftp-server

to

Subsystem sftp internal-sftp

Some users can only use sftp, but not other OpenSSH features like remote login. Let's create a rule for that group of users. Add the following section to the bottom of /etc/ssh/sshd_config:

Match group microsoft
ChrootDirectory /mnt/SFTP-Data
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
Reboot...

Thursday, 23 May 2013

CakePHP and AJAX submitting a form with jQuery

A couple of years back I wrote an article about how to handle the dependent drop down lists problem using CakePHP's Ajax facilities. Today i will put down a trivial example of how to submit a CakePHP created form using jQuery as a small reference that can be easily pasted.

Let me remind you of the CakePHP ajax way. You start by creating a controller method that will "return" the ajax content. For our trivial example, the following controller will be more than enough.

class AjaxController extends AppController {
   var $uses = NULL;

   public function helloAjax()
   {
       $this->layout='ajax';
       // result can be anything coming from $this->data
       $result =  'Hello Dolly!';
       $this->set("result", $result);
   }
}

The corresponding view file view/ajax/hello_ajax.ctp should contain just the following:

<?php echo $result; ?>

Setting up our Ajax call is now as easy as, creating a link or a button that will invoke the asynchronous call and then setting the id of the field that will receive the result. A typical setup would be that the link looks something like this :

<a href="#" id="performAjaxLink">Do Ajax </a>

And then the target field can be created using:

<?php echo $this->Form->input('your_field', array('id' => 'resultField')); ?>

Finally a little script at the end of the file ...

<script>
    jQuery("#performAjaxLink").click(
            function()
            {                
                jQuery.ajax({
                    type:'POST',
                    async: true,
                    cache: false,
                    url: '<?= Router::Url(['controller' => 'ajax','admin' => FALSE, 'action' => 'helloAjax'], TRUE); ?>',
                    success: function(response) {
                        jQuery('#resultField').val(response);
                    },
                    data:jQuery('form').serialize()
                });
                return false;
            }
    );
</script>

The jQuery Ajax API is available here.

Wednesday, 10 April 2013

Using a raspberry-pi as a UPS server with nut

In this post we will try to install the Network UPS tools on a Raspberry-Pi device, attach a USB connected UPS and use it as a UPS server that will allow all machines sharing the same UPS to shut-down correctly when the UPS runs out of power. Our server will look after two clients; one running EL5 and the other openSUSE 11.4.

At the end of the post we will demonstrate how easy it is to set up your clients once the server is up and running and provide additional instructions for setting up the client software on Fedora 18 and EL6.

Update 2014-02-02: Meanwhile things here at the office have changed. The openSUSE machine is now gone and has been replaced by one running Debian 7. I have now revised the client setup guides for Fedora and EL5, 6 and I also have added one for Debian. The openSUSE "howto" is left as is but I can no longer verify if it works or not :) ..

Server Setup

Before we begin I would like to confess that my first attempt to install a no-name made in China UPS resulted to total failure, so eventually I got an expensive APC BackUPS Pro, that worked without any problems from the beginning, so unless your UPS is one supported by the UPS network tools project drivers, don't even try to follow the tutorial.

A second remark, is that if you are following the tutorial as the standard pi user you will need to prefix almost all commands with sudo. To become root on a standard Raspbian and follow along, you will need to issue sudo su-. (Thanks Derek for pointing it out)

root@raspbx:~# apt-get install nut-client nut-server
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libupsclient1
Suggested packages:
  nut-cgi nut-snmp nut-dev nut-xml
The following NEW packages will be installed:
  libupsclient1 nut-client nut-server
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,583 kB of archives.
After this operation, 3,217 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://archive.raspbian.org/raspbian/ wheezy/main libupsclient1 armhf 2.6.4-2.3 [106 kB]
Get:2 http://archive.raspbian.org/raspbian/ wheezy/main nut-client armhf 2.6.4-2.3 [191 kB]
Get:3 http://archive.raspbian.org/raspbian/ wheezy/main nut-server armhf 2.6.4-2.3 [1,286 kB]
Fetched 1,583 kB in 2s (562 kB/s)     
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libupsclient1.
(Reading database ... 37855 files and directories currently installed.)
Unpacking libupsclient1 (from .../libupsclient1_2.6.4-2.3_armhf.deb) ...
Selecting previously unselected package nut-client.
Unpacking nut-client (from .../nut-client_2.6.4-2.3_armhf.deb) ...
Selecting previously unselected package nut-server.
Unpacking nut-server (from .../nut-server_2.6.4-2.3_armhf.deb) ...
Processing triggers for man-db ...
Setting up libupsclient1 (2.6.4-2.3) ...
Setting up nut-client (2.6.4-2.3) ...
[info] nut-client disabled, please adjust the configuration to your needs.
[info] Then set MODE to a suitable value in /etc/nut/nut.conf to enable it.
Setting up nut-server (2.6.4-2.3) ...
[info] nut-server disabled, please adjust the configuration to your needs.
[info] Then set MODE to a suitable value in /etc/nut/nut.conf to enable it.
root@raspbx:~# 

Don't worry about the nut-server information we shall deal with it later on. Now an optional step that will allow us to to use the lsusb utility will be to install the usbutils package, assuming that it is not already there. So:

root@raspbx:~# apt-get install usbutils

.. and then -- blame me for my Windows habits, I firmly suggest a reboot. When the system is back on, we will make sure that out USB device is nιcely plugged in...

root@raspbx:~# lsusb
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. 
Bus 001 Device 004: ID 051d:0002 American Power Conversion Uninterruptible Power Supply
root@raspbx:~# 

Our UPS is there so let's set up the driver for the nut server. Open file /etc/nut/ups.conf and append the following lines at the end.

[apc1200]
        driver = usbhid-ups
        port = auto
        desc = "APC Back UPS Pro 1200VA supporting the two network servers"

You can name your ups anything you like, as far as the driver is concerned my advice is to browse through the official Network UPS Tools drivers list.

Setting up the UPS name and driver is not enough. I have not tested this on any other Debian box, but on raspberry-pi we need an extra step in order to create the /var/run/nut folder and set correct permissions to it.

root@raspbx:~# mkdir /var/run/nut
root@raspbx:~# chown root.nut /var/run/nut/
root@raspbx:~# chmod 770 /var/run/nut/

Now we are ready to test the UPS driver.

root@raspbx:~# upsdrvctl start
Network UPS Tools - UPS driver controller 2.6.4
Network UPS Tools - Generic HID driver 0.37 (2.6.4)
USB communication driver 0.31
Using subdriver: APC HID 0.95
root@raspbx:~# 

Our next step will be to configure upsd and upsmon. The network UPS tools design dictates that upsd communicates with the UPS driver that we just started and upsmon communicates with upsd and actually shuts down the machine in the event of a power failure. By providing this extra level of indirection, nut allows for multiple instances of upsmon to run on different machines. That way they can allow share the same physical UPS and this is what we said that we are going to demonstrate in this posting.

So to enable accessing the upsd via the network, edit the file /etc/nut/upsd.conf and place the following LISTEN directives.

LISTEN 127.0.0.1 3493
LISTEN 192.168.1.137 3493

192.168.1.137, is my pi's IP address -- replace that with your own. Next, we will need to add some kind of security and the next file that we will need to tamper with will be the /etc/nut/upsd.users. Edit it with your text editor and set up the following users

[admin]
        password = myadmpass
        actions = SET
        instcmds = ALL

#
# --- Configuring for a user who can execute tests only
#
[testuser]
        password  = pass  
        instcmds  = test.battery.start
        instcmds  = test.battery.stop

#
# --- Configuring for upsmon
#
# To add a user for your upsmon, use this example:
#
[upsmon_local]
        password  = local_pass
        upsmon master
[upsmon_remote]
        password  = remote_pass
        upsmon slave

Finally the local UPS monitor daemon will need to specify the UPS to monitor and the user credentials from upsd.users file. Open the /etc/nut/upsmon.conf file, locate the monitor section and add the following line:

MONITOR apc1200@localhost 1 upsmon_local local_pass master 

The number 1 after the ups name and host is the power value. The man page for upsd states clearly that:

The "current overall power value" is the sum of all UPSes that are currently able to supply power to the system hosting upsmon. Any UPS that is either on line or just on battery contributes to this number. If a UPS is critical (on battery and low battery) or has been put into "forced shutdown" mode, it no longer contributes.
A "power value" on a MONITOR line in the config file is the number of power supplies that the UPS runs on the current system.

Final steps: Open the /etc/nut/nut.conf file and change the value of Mode to netserver -- making sure that there are no spaces between each side of the = sign. (See NOTE at end of file) and issue the following commands:

root@raspbx:/etc/nut# service nut-server start
[ ok ] Starting NUT - power devices information server and drivers:  driver(s). upsd.
root@raspbx:/etc/nut# service nut-client start
[ ok ] Starting NUT - power device monitor and shutdown controller: nut-client.
root@raspbx:/etc/nut# 

As a last check, verify that both services will start automatically on system (using the update-rc.d command) reboot and yes, our server is ready! ...

root@raspbx:~# ps -ef | grep ups
nut       3275     1  0 Apr09 ?        00:08:24 /lib/nut/usbhid-ups -a apc1200
nut       3278     1  0 Apr09 ?        00:00:19 /sbin/upsd
root      3312     1  0 Apr09 ?        00:00:00 /sbin/upsmon
nut       3314  3312  0 Apr09 ?        00:00:09 /sbin/upsmon
root      4721  4711  0 18:44 pts/1    00:00:00 grep ups
root@raspbx:~#

Clients

Client setup requires more or less three things: One will be to edit the nut.conf file and set the mode variable value to netclient. Next will be to place the correct MONITOR line in the upsmon.conf file and the third will be to start the upsmon daemon.

openSUSE

Our first client is an openSUSE 11.4 machine that I keep saying that I must upgrade. To install nut on openSUSE we need to issue the following command as root.

zypper install nut

openSUSE nut stores the configuration files /etc/ups. By the way the file /usr/share/doc/packages/nut/README.SUSE offer excellent detailed and precise information on how to do things right. So to get things started:

  • Add the line MODE=netclient at the end of the /etc/ups/nut.conf file
  • Add MONITOR apc1200@asterisk "UPS supporting the main Servers" to /etc/hosts.conf
  • Comment out any reference to any UPS at the end of /etc/ups/ups.conf
  • Add MONITOR apc1200@asterisk 1 upsom_remote remote_pass slave
  • Start the service with etc/init.d/upsd start. (The reload option can be used to reread updated configuration files
  • Finally change the system config so that the service starts every time you start your system using the following command: chkconfig upsd on

Reboot and verify :

atlas:~ # ps -ef | grep ups
root      2958     1  0 18:06 ?        00:00:00 /usr/sbin/cupsd -C /etc/cups/cupsd.conf
root      3374     1  0 18:06 ?        00:00:00 /usr/sbin/upsmon
upsd      3376  3374  0 18:06 ?        00:00:00 /usr/sbin/upsmon
root      4776  4732  0 18:13 pts/0    00:00:00 grep ups

You might probably want to test the configuration and whether the upsmon daemon can shut-down your server, so go ahead and ...

atlas:~ # upsmon -c fsd
Network UPS Tools upsmon 2.6.0
                                                                               
Broadcast Message from upsd@atlas                                              
        (somewhere) at 13:57 ...                                               
                                                                               
Executing automatic power-fail shutdown                                        
                                                                               
                                                                              

Broadcast message from root@atlas (Wed Apr 10 13:57:06 2013):

The system is going down for system halt NOW!

Debian/Ubuntu

Perhaps the easiest setup is on a Debian system. You only need four steps:

  1. Install just the client: sudo apt-get install nut-client.
  2. Edit the file/etc/nut/nut.conf and set the mode to netclient. MODE=netclient (mind that there must be no spaces around the equals sign).
  3. Add the monitor MONITOR apc900@xena 1 upsom_remote remote_pass slave command in the /etc/nut/upsmon.conf.
  4. Restart the nut-client service
    service nut-client restart
  5. Update the system to start the service automatically update-rc.d nut-client defaults

Fedora and CentOS versions 5 & 6

Fedora also stores the nut related data in /etc/ups. Again here we need to perform the three steps we mentioned before, but this time we will need to start the upsmon daemon by hand. So to set up our fedora box as a network client:

  • Install the software using yum install nut-client
  • Add MONITOR apc1200@asterisk 1 upsom_remote remote_pass slave
  • Add /usr/sbin/upsmon start in /etc/rc.d/rc.local to verify that the monitor program will start again after reboot.
    Note: On my Fedora 20 system the file was not present so I had to create it, turn it into a shell script by adding !/bin/sh at the first line and make it executable.

Verify:

[thanassis@skymnos ~]$ ps -ef | grep upsmon
root      1898     1  0 17:37 ?        00:00:00 /usr/sbin/upsmon start
nut       1900  1898  0 17:37 ?        00:00:00 /usr/sbin/upsmon start
500       2142  2118  0 17:38 pts/0    00:00:00 grep upsmon

NUT Monitor

A very good GUI based tool to help test the ups servers. It can be easily installed using the package manager of your distribution -- just search for the nut-monitor package and after you install and run it, it looks like this:

Windows

Winnut is a Windows client, that runs as a 32bit service on Windows 7. The project has not been updated since February 24, 2011. I did install the software on a Windows machine but have not been able to do any serious testing. The program's configuration follows the same rules as the Linux clients. The only thing you have to is click the edit configuration file button

and then add the correct MONITOR Line in the upsmon.conf file that will appear loaded into notepad. On 64bit systems you will also need to change the line

NOTIFYCMD "\"c:\\Program Files\\WinNUT\\alertPopup.exe\""

to

NOTIFYCMD "\"c:\\Program Files (x86)\\WinNUT\\alertPopup.exe\""

Thursday, 28 February 2013

NVidia on Centos 6

The post documents the steps I followed in order to install the NVidia drivers using kmod-nvidia and ELPrepo on a fresh CentOS installation.

We begin by importing the ELRepo Project's public key

sudo rpm --import http://elrepo.org/RPM-GPG-KEY-elrepo.org

The people at ELRepo suggest that this should also be present, so unless you already have it ...

sudo yum install yum-fastestmirror

The next step is to install the ELRepo repo itself

sudo rpm -Uvh http://elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpm

... and finally the NVidia drivers, which is what we aimed for in the first place.

sudo yum install kmod-nvidia

An optional step would be to install the 32bit compatibility drivers and files

sudo yum install nvidia-x11-drv-32bit

What else is there... ahh yes, reboot!

Wednesday, 16 January 2013

Fedura 18 update with fedup

After updating two fc17 x86_64 KDE PCs over the network using FedUp, I thought that I would put down my experience and prepare anyone trying to do the same.

To get things started, fedup will probably not exist on your system, so, before starting the actual update process you will probably have to install it using the simple sudo yum install fedup command.

An other thing to mention here is that the current version of google-earth does not install on fedora 18.The actual error message is file /usr/bin from install of google-earth-stable-6.0.3.2197-0.x86_64 conflicts with file from package filesystem-3.1-2.fc18.x86_6. So the only way to do a descent update -- at least for the moment, Jan-16-2013 -- is to remove it: (sudo yum erase google-earth-stable) I am certain that the problem will be fixed, but right now we cannot have them both.

For the sake of completion, after installing fedup and removing google-earth, perform a full system update.

sudo yum -y update

After updating and rebooting, start the actual update process with the command:

sudo fedup-cli --network 18 --debuglog fedupdebug.log

This process (depending on your internet speed) will take quite some time. In the case of my desktop development machine, it downloaded 1755 packages plus an additional 728. When it will eventually be over, we will need to reboot and start the system from a specific "System Update (FedUp)" grub menu entry. FedUp will then do it's magic (press the Escape key if you want to see what it;s doing) and then you 'll have to reboot. The tricky part afterwards, is that KDE does not start complaining about various dependencies being let unsatisfied. The solution is start the system in text mode, make sure that you have an active network connection and then run:

yum distro-sync

This will downgrade approximately 90 packages but things will work out. As the fedora wiki advices, try running package-cleanup --orphans to determine packages left over that will receive no further updates and if possible remove them. (speaking of which a package-cleanup --oldkernels wouldn't hurt either...)

Saturday, 15 December 2012

xmllint : Command line XML parser and formatter

So you were given a XML file that came straight out of a windows system with no indentation and incorrect line endings. The simplest way out is xmllint.

The program does much more than formatting its input. Additional functions include: parsing, verifying, dropping empty nodes and many more.

Installing it on CentOS, Fedora and like, is performed via :

sudo yum -y install libxml2

openSUSE users may use yast or type something like:

zypper install libxml2

... while Debian and Ubuntu users will have to go like:

sudo apt-get install libxml2-utils

after you have it on your system, the easy way to fix the badly formated xml file would be :

xmllint --format badlyFormated.xml > wellFormated.xml

Thursday, 13 December 2012

Gnome2 Default Keyring location

Just a quick note for me -- and anyone else out there -- The location of the keyring in gnome2 is in ~/gnome2/keyrings. And if you forget your keyring password then the easy way to start all over with a new keyring is:

rm ~/.gnome2/keyrings/default.keyring 

Thursday, 6 December 2012

Using a raspberrypi as a file server

The idea came to me from a friend. I wanted to install a small network storage system to use merely as a file exchange repository at the office and I was looking to buy some kind of Ethernet disk, when he said to me why don't you do it with a raspberry and a USB flash drive?. The thought was intriguing -- to say the least, so here I am with all the little details:

I am not going to go through the entire process of downloading and preparing the raspberry SD card from the Raspberry Pi Downloads page. The cool thing with raspberry is that once you get it running for the first time, you get a real Debian Linux that does all you expect a descent OS to do.

A pleasant surprise with the raspberry image was that the default set-up registered itself to my dynamic DNS and I was able to log into raspberrypi the moment I plugged it into my LAN without touching a thing.

The first thing that I needed to do was change the hostname of the device. That way there would be no name conflicts when I add a fresh one for the next God knows what project that will come up...

Changing the host name is as simple as editing the file /etc/hostname, changing the single line with the word raspeberrypi to xena (Yes i wanted an ... epic name) and then rebooting. The DNS picked up the name change and logging into xina was as easy as typing ssh -l pi xena -X. (An even easier way to accomplish the task is to use raspi-config. Check the advanced options menu)

Next thing would be a plug the USB disk and make sure it gets mounted every time the system boots. I suppose that the proper way to make this work would be to tamper with /etc/fstab. I chose to do it a bit differently and ended up creating a file containing the last things that the system should do right after booting.

First create a mount point and mount the USB disk. In my case, I created /mnt/SFTP-Data and tested it with a:
mount /dev/sda1 /mnt/STFP-Data

The idea with executing commands right after boot is that we need to create a file, let's call it system-startup.sh that contains the above mount command, plua the necessary LSB comments, place it in /etc/init.d, make it executable and then run insserv to add this command as the last default runlevel action. The format of the comments section is :

#! /bin/sh
### BEGIN INIT INFO
# Provides: system-start.sh
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start up script after boot
# Description: Enable service provided by daemon.
### END INIT INFO
mount /dev/sda1 /mnt/STFP-Data

...and the actual command to make this work is:
sudo insserv /etc/init.d/system-startup.sh

Finally, it time to install and configure Samba. The Debian Administrator's handbook says it all. Just remember that raspebrry-pi only has 256MB of actual RAM so keep away from web configuration tools like swat and go with the debconf and manually editing the the smb.conf file option.

After Samba is up and running edit the file /etc/samba/smb.conf and fix the basic staff like work group name and server description. Mine looks like this:

[global]
## Browsing/Identification ###

# Change this to the workgroup/NT-domain name your Samba server will part of
   workgroup = aryballos

# server string is the equivalent of the NT Description field
   server string = %h Raspberry-PI Server

# Windows Internet Name Serving Support Section:
# WINS Support - Tells the NMBD component of Samba to enable its WINS Server
  wins support = yes

In order to create a read write share that anyone can use, we need to create a new user that has read/write access to the disk and then force that use and group everytime that someone accesses data through the Samba share.

pi@xena /etc/samba $ groupadd microsoft
pi@xena /etc/samba $ useradd -c "Samba user" -m -d /mnt/mdz-disk/shared/ -g microsoft bill

There are two things here. First Bill does not have a password so he can not log in interactively. Second Bill's home is the directory on the disk where I want everybody to have full read/write access. Telling this to the Samba server requires that the following section be placed at the end of the smb.comf file ...

[public]
   comment = Public data in the USB drive for our work-group
   read only = no
   path = /mnt//mnt/STFP-Data/public
   guest ok = yes
   force user = bill
   force group = microsoft

... the directory is made and permissions are set

pi@xena /mnt/SFTP-Data $ sudo mkdir public
pi@xena /mnt/SFTP-Data $ sudo chown bill.microsoft public/

... and finally the samba service is restarted...

pi@xena /etc/samba $ sudo service samba restart
[ ok ] Stopping Samba daemons: nmbd smbd.
[ ok ] Starting Samba daemons: nmbd smbd.
pi@xena /etc/samba $ 

Thursday, 18 October 2012

PHP: Workaround for the mailbox is empty imap error

If you are in the process of learning how to use the PHP imap* family of functions in order to manage a remote mailbox, here is a little hint to let you get away with the first annoying error warning you are likely to run into.

So here is how the story goes. You open am empty mailbox. Do something with it and then when you close it you get a PHP warning like :

Notice (8): Unknown: Mailbox is empty (errflg=1) [Unknown, line ??]

I have read many ways for handling this. The bottom line is that this is only a warning and due to the nature of the PHP imap functions, it will eventually be flashed when you call imap_close() or when your script exits. Fortunately each call to imap_errors() flashes the internal error log, so a simple way to avoid the entire hassle would be to code something like ...

    function connect()
    {
        $mailBoxx = @imap_open($this->server, $this->username, $this->password);

        if ($mailBox) {
            // call this to avoid the mailbox is empty error message
            if (imap_num_msg($mailBox) == 0)
                $errors = imap_errors();
            return TRUE;
        }
        // imap_errors() will contain the list of real errors
        return FALSE;
    }

Sunday, 15 April 2012

bash scrupt to copy all songs in an m3u list into a folder

This is not the first time I thought about it and it took a lot of digging and googling in order to get the right command line to do it. So, all credit goes to the thefekete.net who is the initial poster and my only contribution is turning the initial command line into a script.

#!/bin/bash

# ------------------------------------------------------------------------------------
# Copy all files from a playlist to a dest folder
# ------------------------------------------------------------------------------------
if [ "$#" != 2 ]; then
 echo usage $0 playlist.m3u dest-dir
 exit 1
fi

if [ ! -d "$2" ]; then
    mkdir $2
    echo Creating directory `pwd`"/"$2
fi

if [ -z "$1" ]; then
 echo  $1 is not a valid file
 exit 2
fi

cat "$1" | grep -v '#' | while read i; do cp "${i}" "$2" ; echo "${i}"; done

Thank you the thefekete

Friday, 17 February 2012

CakePHP: Loosing translated texts

This one drove me crazy for the last couple of hours, so I thought I better share it right away.

I have a bilingual CakePHP application that displays content messages translated from English to Greek. To achieve this I wrap all my English texts inside cake's __() function and then run the cake i18n extract script in order to assemble a .pot file. Finally, I translate my original messages to Greek using the POEdit program to create and manage the necessary translations. Everything seemed to work well: Each time I added new strings, I would execute the cake i18n extract script, then open POEdit, update my .po catalogue from the generated .pot file and translate only the new texts.

Except for today. I was asked to asked to add a few more messages, so I followed the standard procedure. but after I updated both my .po and .mo files, the messages on the web page remained in English despite my ... sincere efforts and honest desire to see them in Greek.

It took me a while to figure this out: The solution was as simple as to delete all files from the APP/tmp/cache/persistent directory.

Wednesday, 15 February 2012

Command line to mount USB flash drives in Linux

The subject is not new and nothing beats the device notifier icon in KDE asking you what to do with a newly inserted flash drive. This post however, is meant to serve only as a note to me, regarding the safest procedure of setting up and mounting a flash drive from the command line.

The current state of affairs is like this: We have a 32GB flash drive that is to be plugged in to a remote LAMP server where we want to place daily database backups. We only have access to the server via ssh so in order to automate the procedure, we are going to initially format the drive using the ext4 file system and then label it so that we won't care about the actual SCSI port that he device will use in the future.

Initial formatting and labelling

After the device has been plugged in for the first time, the easiest way to determine the actual SCSI port is to find the lines containing the word SCSI in the kernel displayed messages. This is as easy as :

[root@skymnos ~]# dmesg | grep SCSI
SCSI subsystem initialized
Block layer SCSI generic (bsg) driver version 0.4 loaded (major 252)
scsi0 : SCSI emulation for USB Mass Storage devices
sd 1:0:0:0: [sdb] Attached SCSI disk
sd 0:0:0:0: [sda] Attached SCSI removable disk
[root@skymnos ~]# 

Alternatively we could use the udisks --monitor command before pluggin in the device. This would produce output like the following:

[root@skymnos-wifi ~]# udisks --monitor
Monitoring activity from the disks daemon. Press Ctrl+C to cancel.
added:     /org/freedesktop/UDisks/devices/sde
added:     /org/freedesktop/UDisks/devices/sde1
So now we know that out flush drive is located under /dev/sda and the partition that we are going to work with is /dev/sda1. To format it we will use the mkfs.ext4 command with the -L option to specify a label for our file-system.

[root@skymnos ~]# mkfs.ext4 -L dbBackups /dev/sda1 
mke2fs 1.41.12 (17-May-2010)
Filesystem label=dbBackups
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1949696 inodes, 7790336 blocks
389516 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
238 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@skymnos ~]# 

Mounting

The -L dbBackups switch that we previously used in order to format the drive, tells mkfs to create a file system with a label of dbBackups. Now if you plug this to a system running a decent window manager with udev, this would be automatically mounted under /media/dbBackups. We are going the mimic the same behaviour.

[root@skymnos ~]# mkdir /media/dbBackups/
[root@skymnos ~]# mount -L dbBackups /media/dbBackups/
[root@skymnos ~]# df -h /media/dbBackups
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              30G  172M   28G   1% /media/dbBackups
[root@skymnos ~]# 

A few more details: Since this is going to be backup storage disk, our lives will become much easier if we create a directory with full read-write permissions inside the drive like this:

[root@skymnos ~]# mkdir /media/dbBackups/data
[root@skymnos ~]# chmod o+w /media/dbBackups/data/
[root@skymnos ~]# 

So to sum it up. Mounting the flash drive on the remote server from my office machine requires -- well, apart from having someone plug the drive into a USB port doing something like this:

[thanassis@skymnos ~]$ sudo mount -L dbBackups /media/dbBackups/
[thanassis@skymnos-wifi ~]$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              68G  5.1G   62G   8% /
tmpfs                1006M   88K 1006M   1% /dev/shm
/dev/sdb1              30G  172M   28G   1% /media/dbBackups
[thanassis@skymnos ~]$ 

And what if I wanted the system to attempt to mount the drive each time it boots? In that case all I would have to do is add a line like /bin/mount -L dbBackups /media/dbBackups/ in the /etc/rc.d/rc.local file

Tuesday, 13 December 2011

Java enums: A complete example

I started programming with Pascal, so I have deep feelings for enumerated types. To be honest I feel that they are a much more easy going approach to modeling that the long lists of constants we used in the C programming language or in the pre JDK5 versions of Java.

Java enums are so very flexible and the guide on the Oracle website says it all. What I wanted to do in this post is to provide a full working example, as a reference to myself and hopefully any ... lost soul, that demonstrates how to bind data with each enum type value and also how to implement the previous and next methods,

package gr.abakalidis.sample.model;

/**
 * Possible image states and relative sizes regarding the actual bitmaps
 * loaded from a Web server.
 * 
 * @author Thanassis Bakalidis
 * @version 1.0
 */
public enum ImageState {
    NOT_LOADED(0, 0), SMALL(320, 240), MEDIUM(800, 600), LARGE(1024, 768);

    private final int width;
    private final int height;

    ImageState(int width, int height)
    {
        this.width = width;
        this.height = height;
    }

    public int getHeight()
    {
        return this.height;
    }

    public int getWidth()
    {
        return this.width;
    }

    /**
     * Get the next value ion the series of enumeration 
     * 
     * @return the next value in the series or null if already at end of values
     */
    public ImageState getNext()
    {
        return this.ordinal() < ImageState.values().length - 1 
            ? ImageState.values()[this.ordinal() + 1] 
            : null;
    }
 
    /**
     * Get the previous value in the series of enumeration
     * 
     * @return the next value in the series on null if called for the first va;ue 
     */
    public ImageState getPrevious()
    {
        return this.ordinal() == 0 
            ? null 
            : ImageState.values()[this.ordinal() - 1];
    }
}

Tuesday, 15 November 2011

CentOS: Specifying DDNS hostname

A quick reminder. When using a CentOS machine on network with a DDNS then in order for the host name to appear correctly on the DDNS managed local zones, create or edit the file /etc/sysconfig/network-scripts/ifcfg-deviceID where deviceID is the actual device name of your network interface e.g. eth0, wlan0 etc. Make sure that the line DHCP_HOSTNAME appears as shown below

DEVICE="eth0"
NM_CONTROLLED="yes"
DHCP_HOSTNAME="my-machine"
NAME="System eth0"
BROADCAST=255.255.255.255
ONBOOT=yes

There can be one ifcfg-XXX file per network interface. That way you can have two different names for the same machine connecting to the same network using different network cards like Ethernet and Wi-fi.

Saturday, 5 November 2011

CakePHP: Storing multi-dimentional arrays in cookies

II didn't know that cookies are basically plain text data. This makes it impossible to store complex data structures directly inside a cookie. More information can be found in the archives of the CakePHP google group following this link.

The bottom line is that complex data need to be serialized before being saved in a cookie and unserialized after they are read from one. The serialize() and unserialize() PHP functions are here to do the job and last but not least the third parameter of the Cookie::write call -- the one that instructs cake to encrypt the cookie data -- should be set to true.

So to save a controller's form data you need to write something like this:

    $dataToSave = serialize($this->data);
    $this->Cookie->write( self::SEARCH_DATA_KEY, $dataToSave, true, '1 year');

and to read them back ....

        if (empty($this->data)) {
            // try to see if we have a stored cookie
            $cookieData = $this->Cookie->read(self::SEARCH_DATA_KEY);                        
            if (empty($cookieData)) {
               // provide default values here 
               ...
            } else 
                $this->data = unserialize($cookieData);            
        }

Tuesday, 1 November 2011

CakePHP: An edit form with a cancel button (1.3 and 2.x)

When developing database CRUD applications with CakePHP, sooner or later you end up writing view code looking more or less like this
<div class="my model form">
<?php echo $this->Form->create('MyModel');?>
 <fieldset>
  <legend></legend>
 <?php
  echo $this->Form->input('id');
  echo $this->Form->input('name');
 ?>
 </fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
This creates a nice form with a submit button at the end that every self respecting user can press to create a new record or modify the data of an existing one. It is also logical that you place a link to the index page somewhere near the form, so your users know where to go in case they change their mind about altering the database data.
With my users this time is was different. The form had to contain a cancel button. So how does one do it? If you are using the cake bake script and wish to have two nice round green buttons right at the bottom of your form then replace the last two lines of the previous code fragment with the following:
     ...
     <div class="submit">
         <?php echo $this->Form->submit(__('Submit', true), array('name' => 'ok', 'div' => false)); ?>
         <?php echo $this->Form->submit(__('Cancel', true), array('name' => 'cancel','div' => false)); ?>
     </div>
     </fieldset>                   
 <?php echo $this->Form->end();?>
The next thing to know from inside the controller code, is which button was pressed before the data were posted and that is available inside the 'form' array of the Controller::params property.
So a simple modification like the one on the following code :
        public function edit($id = null)
        {
            if (!$id && empty($this->data)) {
               $this->Session->setFlash(__('Invalid property', true));
               $this->redirect(array('action' => 'index'));
            }
            if (!empty($this->data)) {
                // abort if cancel button was pressed  
                if (isset( $this->params['form']['cancel'])) {
                    $this->Session->setFlash(__('Changes were not saved. User cancelled.', true));
                    $this->redirect( array( 'action' => 'index' ));
                }

                // proceed to save changes as usual
        }
... and everyone is happy.

Edit: Alternatively, if working with CakePHP version 2.4 then the sane info is inside the data array of the controller's request property.
So the previous code gets rewritten like this
        public function edit($id = null)
        {
            if (!$this->MyModel->exists($id)) {
                throw new NotFoundException(__('Invalid record'));
            }
            if ($this->request->is(array( 'post','put'))) {
                if (isset($this->request->data['cancel'])) {
                    $this->Session->setFlash(__('Changes were not saved. User cancelled.'));
                    return $this->redirect( array( 'action' => 'index' ));
                }

                // proceed to save changes as usual
        }
There is one last thing though... the cancel button in your form should also indicate that no form validation should be performed at the browser level. This can be accomplished by setting the 'formnovalidate' key of the Form::input options parameter to TRUE. So the whole cancel button creation tag should now look like this:
        <div class="submit">
            <?php echo $this->Form->submit(__('Create Account'), array('name' => 'ok', 'div' => FALSE)); ?>
            <?php echo $this->Form->submit(__('Cancel'), array('name' => 'cancel', 'formnovalidate' => TRUE, 'div' => FALSE)); ?>
        </div>
    Form->end(); ?>

Wednesday, 19 October 2011

A new version of the CakePHP QBE component

I have just developed and started testing of a new version of my QBE component.

Major changes are that the component now accepts the model name as an initialization parameter, it provides a new ~ X Y operator to implement the between clause and that support for a different search and results page is now more clear.

The code for the component, along with usage details can be found in my GitHub repository available from this link:.

Wednesday, 14 September 2011

ABAP: Check if a customer is blocked for sales support

This is relatively simple, but it's best to keep it here for future use.

FORM check_if_customer_is_blocked USING a_cust_id TYPE kunnr.
 DATA :
   is_blocked TYPE cassd_x.

 SELECT SINGLE cassd
   INTO is_blocked
   FROM kna1
   WHERE kunnr = a_cust_id.

 IF NOT is_blocked IS INITIAL.
   MESSAGE e888(sabapdocu) WITH 'Customer is blocked!'.
 ENDIF.

 SELECT SINGLE cassd
  INTO is_blocked
  FROM knvv
  WHERE kunnr = a_cust_id.

 IF NOT is_blocked IS INITIAL.
   MESSAGE e888(sabapdocu) WITH 'Customer is blocked!'.
 ENDIF.
ENDFORM.

Tuesday, 13 September 2011

Android Development on Fedora 17 Howto

Updated 2012-06-28 The original article was written for and tested on fedora 15. This is the revised version for Fedora 17.

Here are the steps I followed in order to start developing android applications on my Fedora 15 x86_64 box. The actual list is a collection from various sources that I am listing at the end of this post put together in a start-to-finsh manner, so we can start developing almost right away.

Step 1: Install the Sun Java

Download the appropriate rpm for your architecture by visiting the Oracle Java SE Downloads site. Follow this link. Select the latest Java SE 6 Update and download it on your machine.

After download is complete, open a terminal window, cd to your downloads directory and enter the following command :

[thanassis@nb-thanassis Downloads]$ sudo sh jdk-6u27-linux-x64-rpm.bin 

Step 2: Make Sun Java the default Java for your machine

Using the alternatives command -- thanks to Mauriat Miranda -- you may change the default java used by your system like this :

sudo /usr/sbin/alternatives --install /usr/bin/java java /usr/java/default/bin/java 20000

To test, try the following:

[thanassis@nb-thanassis Downloads]$ java -version
java version "1.6.0_27"
Java(TM) SE Runtime Environment (build 1.6.0_27-b07)
Java HotSpot(TM) 64-Bit Server VM (build 20.2-b06, mixed mode)

Step 3: Download and install the Android SDK core support

Start from this page here:

Download the latest android SDK and unpack it in a directory of your choosing. We will need to modify our path variable based on that, so I recommend that you change the base name from android-sdk-linux_x86 to AndrodSDK and move it to /opt.

Next, edit your .bashrc file and modify your path variable to include AndroidSDK/tools and AndroidSDK/platform-tools. The corresponding PATH statement should more or less look like this:

export PATH=$PATH:/opt/AndroidSDK/platform-tools:/opt/AndroidSDK/tools

To start the android emulator on a 64 bit machine we need some additional 32bit packages. These can be installed using the command

sudo yum install glibc.i686 glibc-devel.i686 libstdc++.i686 zlib-devel.i686 ncurses-devel.i686 libX11-devel.i686 libXrender.i686 libXrandr.i686

Step 4: Start the android application, download the SDK files and build a virtual device

Run command /opt/AndroidSDK/tools/android. From the available packages select the SDK version and the documentation for the versions you are planning to work with. Click the install selected button on the right and then accept the license terms and complete the installation of the selected software.

Next create an AVD device. Using the Android SDK manager, click on the Tools menu and then select the Manage AVDs option. Click the New button from the window that pop up and fill in information about the new device

Click create AVD to create your device. After device creation is complete, close the android application and log out and in again to make the paths work.

When you are back in, start the android application directly from a terminal window and start your new device just to be 100% sure that everything works fine.

Step 5: Install eclipse and the ADT plug-in

I do not recommend installing eclipse from the fedora repositories. I have experienced many issues -- especially after updates -- so in my view it is much simpler to download the entire eclipse IDE directly form the downloads page of the eclipse web site, unpack it in a directory of your choosing and finally move it to /opt.

tar -zxvf eclipse-java-juno-linux-gtk-x86_64.tar.gz
sudo mv eclipse /opt/

After eclipse is installed on our Linux box we need to install the android development plug-in. This is done by selecting Help → Install new software from the eclipse main window menu. Click the Available software sites link and make sure that the entry http://download.eclipse.org/releases/juno is enabled. Otherwise install it by clicking the add button and create a new software repository like this:

The images shown here are from the previous version of eclipse (Helios). New version images are similar with slight aesthetic differences

While you are there click the add button again, in order to create the android plug in repository. The repo URL is https://dl-ssl.google.com/android/eclipse/ and the add repository window should be filled like this:

Make sure that both repositories are enabled, by activating the check box on the right and press Ok to dismiss the dialog. Now we are ready to perform the actual plug-in installation. Select the ADT plug-in on the Work with field and then check all available software. Click Next accept the license agreement and after download and install is over restart eclipse..

That should be enough to get you started.

Step 6: Create the Hello world application

At this point we shall create a simple hello world application and deploy it first on the android emulator and next to an actual android device connected via USB. Start the eclipse IDE, click File → New → Project and then select Android project from the dialog that pops up. Click next and fill in the Project name and package name fields wth the values HelloWorld and org.example.hello respectively. Click Finish to create the project.

Just to spice up things a bit more, open the res/values/strings.xml file and change the value of the hello string to Hello Android from Fedora. Save the file and run the application; the android emulator will eventually load and the application will be deployed and run on it, so you 'll probably end up seeing something like this:

Step 7: Connect your android device to Linux.

Our last job will be to connect an actual android device to our Linux box and deploy our HelloWorld application onto that device.

Remember that before we connect the actual device we need to change the following settings on the device:

  • Menu Settings → Applications, enable Unknown (or Untrusted) sources.
  • Menu Settings → Applications → Development, enable USB debugging.

The last part comes directly from Peter Kirns blog post. We need to configure Fedora’s udev rules to support our device in debugging mode.

As the super user create or edit the file /etc/udev/rules.d/51-android.rules and place the following line in it:

SUBSYSTEM=="usb",0bb4",SYMLINK+="android_adb",MODE="0666"

The SYSFS{idVendor}=="xxxx" entry must be filled with the USB Vendor ID of your device manufacturer. (the example here assumes you are using HTC). The list of all devices is available from Google's Using Hardware Devices Android Developer's page via this link. There is one last thing though: Vendor Ids must be typed in small letters, although Google's table lists them in capital.

You can even set up your environment to allow connections to multiple devices by adding more than one line in the 51-android.rules file. As an example have a look at my setup that allows me to connect to a Samsung, a Sony Ericson and an HTC device :

[athanassios@hades rules.d]$ cat 51-android.rules 
SUBSYSTEM=="usb",SYSFS{idVendor}=="04e8",SYMLINK+="android_adb",MODE="0666"
SUBSYSTEM=="usb",SYSFS{idVendor}=="0fce",SYMLINK+="android_adb",MODE="0666"
SUBSYSTEM=="usb",SYSFS{idVendor}=="0bb4",SYMLINK+="android_adb",MODE="0666"

Finally, reload rules by using:

udevadm control --reload-rules

Connect your device. In case it prompts for a connection mode specify that it should act as a USB storage unit (disk drive).

Testing connection can be done by using:

[thanassis@nb-thanassis ~]$ adb devices
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
List of devices attached 
SH16ERT01652    device

Start eclipse and run your hello project again. This time the ADT plugin will detect your device deploy your application onto it and you will see the hello world message right onto your android's screen.

Notes

In order to compile this list I used the following links. Thanks and credits go to the original authors.