Pages

Monday 10 December 2012

PIL jpeg support on 64 bit Precise

If you have problems with installing PIL, look at this site
In short:
sudo ln -s /usr/lib/`uname -i`-linux-gnu/libfreetype.so /usr/lib/
sudo ln -s /usr/lib/`uname -i`-linux-gnu/libjpeg.so /usr/lib/
sudo ln -s /usr/lib/`uname -i`-linux-gnu/libz.so /usr/lib/

Sunday 9 December 2012

My First Android App

Yesterday, I decided to keep up with the rest of the world in some ways, so I bought a Samsung Galaxy ACE smartphone. I know, it is not a really high-end stuff, according to the current state of the smartphone market. Anyhow, I think it is a great value for 85 pounds, with a pay as you go contract. I am really impressed by this phone, and I decided, to develop a hello world application. So, I first downloaded the Android SDK. As I extracted the tar.gz, the Readme file told me how to carry on with the installation. So I followed the instructions, first with the UI:
tools/android update sdk
With the UI, I selected to install:
  • Android SDK Tools
  • Android SDK Platform-tools
  • For Android 2.3.3
    • SDK Platform
    • Samples for SDK
After the process finished, the log window had a warning:
Stopping ADB server failed (code -1).
I will ignore it for now. The whole stuff is consuming around 307 megs at the moment.
cd tools
./android list targets
And I created a project:
./android create project --target "android-10" --name HelloWorldApp \
--path ~/Documents/android/workspace/HelloWorldApp \
--activity MainActivity --package eu.lakat.example.helloworldapp
Let's go back to the "root":
cd ..
I created a small script, to include all the paths in one shot:
export PATH=$PATH:"$(pwd)/android-sdk-linux/tools/":"$(pwd)/android-sdk-linux/platform-tools/"
Changed the directory to the applications:
cd workspace/HelloWorldApp/
Compiled it to debug:
ant debug
And installed it on my device:
adb install bin/HelloWorldApp-debug.apk
Everything worked perfectly fine, I found the app in my device, and was able to run that easily. Cool!

Virtual device

android avd
Use th UI to create a new device, and simply execute:
adb install bin/HelloWorldApp-debug.apk

Monday 3 December 2012

NFS installation

So I decided to put an NFS server to my DreamPlug. First, an easy step:
apt-get --no-install-recommends install nfs-kernel-server
Let's see what do I have:
root@plugged:~# lsmod | grep nfs
nfsd                  243695  11 
exportfs                3108  1 nfsd
So, next step is to export a directory. I would like to create a read-only export.
cat /etc/exports
/mnt/md2        192.168.0.2(ro,no_subtree_check)
TODO: Some security Okay, so let's restart the nfs server:
/etc/init.d/nfs-kernel-server stop
/etc/init.d/nfs-kernel-server start
Install the client on the other machine:
sudo apt-get --no-install-recommends install nfs-common
And mount the export:
sudo mount 192.168.0.4:/mnt/md2 /mnt/md2
It does not seem to perform very well. For example, from the client, through wifi:
time ls -la /mnt/md2/1/ > /dev/null
Gives
real 1m18.605s
user 0m0.560s
sys 0m2.632s
Whereas running the same stuff on the DreamPlug:
real 0m2.572s
user 0m1.650s
sys 0m0.920s
And that's fast. Let's try to dd some files.
dd if=/mnt/md2/1/ffc4f5bc7f6b78ac371f3ecebdd9701e4cb8c68f of=/dev/null
Gives:
577409024 bytes (577 MB) copied, 103.255 s, 5.6 MB/s
And what is the raw speed? On the server:
nc -l -p 4545 -q 1 < /mnt/md2/1/ffc4f5bc7f6b78ac371f3ecebdd9701e4cb8c68f
On the client:
nc 192.168.0.4 4545 | dd of=/dev/null
And the results:
577409024 bytes (577 MB) copied, 103.84 s, 5.6 MB/s
So nfs gives almost no overhead. (Please note, that the 1 sec was the waiting time added by the netcat server. That's negligable.) Let's see the same thing locally on the server:
dd if=/mnt/md2/1/ffc4f5bc7f6b78ac371f3ecebdd9701e4cb8c68f of=/dev/null
Result:
577409024 bytes (577 MB) copied, 19.4105 s, 29.7 MB/s
NFS over a gigabit connection (after umount-mount, to clear the cache):
577409024 bytes (577 MB) copied, 24.6491 s, 23.4 MB/s
Without NFS (nc way):
577409024 bytes (577 MB) copied, 20.9865 s, 27.5 MB/s
So yes, NFS has some overhead at these rates.

Friday 30 November 2012

MDADM: start raid1 array with one device

When I was home, I copied a lot of pictures to a raid array. As I moved back, I brought only one disk with me. Now, I would like to access the data on that drive, in a read-only fashion.
sudo mdadm --assemble /dev/md0 /dev/sdb1
This printed out the following:
mdadm: /dev/md0 assembled from 1 drive - need all 2 to start it (use --run to insist).
Let's see the status:
cat /proc/mdstat
Gives me:
md0 : inactive sdb1[1](S)
      625129216 blocks
Okay, so this array definitely needs activation. I decided to run this array:
sudo mdadm --run /dev/md0
And now, let's see the status:
cat /proc/mdstat
It seems, that the device is now active.
md0 : active raid1 sdb1[1]
      625129216 blocks [2/1] [_U]
I expected to see a read only mode, and it is a bit annoying, that it seems that it is not r/o. Now, I try to set it to read-only:
sudo mdadm --readonly /dev/md0
And now, it seems better:
cat /proc/mdstat
shows:
md0 : active (read-only) raid1 sdb1[1]
      625129216 blocks [2/1] [_U]
And now, it is time to mount it!
sudo mount --read-only /dev/md0 /mnt/p1
The good stuff, is that I managed to mount it, and hopefully, as I put it back to its original place, it will still be fine.
sudo mdadm --detail /dev/md0
See the "Update Time" (which is exactly the date, when I last accessed the array)
/dev/md0:
        Version : 0.90
  Creation Time : Wed Nov 21 22:52:49 2012
     Raid Level : raid1
     Array Size : 625129216 (596.17 GiB 640.13 GB)
  Used Dev Size : 625129216 (596.17 GiB 640.13 GB)
   Raid Devices : 2
  Total Devices : 1
Preferred Minor : 0
    Persistence : Superblock is persistent

    Update Time : Thu Nov 22 07:10:02 2012
          State : clean, degraded 
 Active Devices : 1
Working Devices : 1
 Failed Devices : 0
  Spare Devices : 0

           UUID : 9d82c2e5:33bd53b5:e4d1075c:94619d7b
         Events : 0.40

    Number   Major   Minor   RaidDevice State
       0       0        0        0      removed
       1       8       17        1      active sync   /dev/sdb1

Monday 19 November 2012

syrep

I always forgot the name of the tool, that I used to sync directory structures. So here is the name and the URL:

SYREP

Sunday 11 November 2012

Dreamplug - overwrite the internal SD card

ssh 192.168.0.4 mkfs.ext3 /dev/sda2
ssh 192.168.0.4 mount /dev/sda1 /mnt/sda1
ssh 192.168.0.4 mount /dev/sda2 /mnt/sda2
sudo kpartx -av debian.phase2.img
sudo mount /dev/mapper/loop0p2 p2
cd p2
sudo tar -czf - . | ssh 192.168.0.4 "cd /mnt/sda2 && tar -xzf -"
cd ..
sudo umount p2
sudo mount /dev/mapper/loop0p1 p1
scp p1/uImage 192.168.0.4:/mnt/sda1/uImage2
sudo umount p1
ssh 192.168.0.4 sync
Reboot the plug, and go to uBoot. Remove all USB drives.
setenv x_bootcmd_kernel fatload usb 0 0x6400000 uImage2
setenv x_bootargs_root root=/dev/sda2 rootdelay=10 panic=10
saveenv
Extract the libs:
mkdir libs
cd libs/
sudo tar -xzf ../sda2.tgz
cd lib/modules
sudo tar -czf - . | ssh 192.168.0.4 "cd /lib/modules && tar -xzf -"

Dreamplug - post setup

Some notes on what I did to the installed debian: - /etc/default/locale
LANG="C"
- /etc/hostname - /etc/apt/sources.list
deb http://mirrors.kernel.org/debian squeeze main contrib non-free
deb http://security.debian.org squeeze/updates main contrib non-free
- /etc/network/interfaces
auto lo eth1

iface lo inet loopback
iface eth1 inet dhcp
- start network
dhclient eth1
- update
apt-get update
apt-get upgrade
- install openssh
apt-get --no-install-recommends install openssh-server

Saturday 10 November 2012

Dreamplug with Debian Squeeze

The goal is to set up a new debian squeeze on the Dreamplug, using the New IT kernel. What is this about? It is about to put some bytes to an USB drive. This is not rocket science. In order to reduce the USB unplugs, I will create the whole image on a virtual disk image (a file), and dd it to the USB drive, as I am ready. With this, I don't have to do expensive file operations on the slow USB drive, only a sequential copy, which is usually fast. So here is a small list of what I have done. Create a root filesystem by using debootstrap:
sudo apt-get install debootstrap
sudo mkdir rootfs
sudo debootstrap --verbose --foreign \
--arch armel squeeze rootfs \
http://mirrors.kernel.org/debian
The root filesystem is not yet ready. As the kernel does not contain the udev system, if this root filesystem were booted, the following message would appear followed by a shutdown:
Warning: unable to open an initial console.
To avoid this, let us create the generic devices:
(
cd rootfs
sudo MAKEDEV generic
)
Now, it is time to create an 1G image:
dd if=/dev/zero of=debian.img bs=1024 count=1000000
Set up this disk as a disk device, and partition it.
sudo apt-get install kpartx
sudo kpartx -av debian.img
(
cat << EOF
,100,6
,,83
EOF
) | sudo sfdisk /dev/loop0 -u M -D
And so I created two primary partitions: 100M FAT16, and the rest as type LINUX. Let's unplug, and replug the device, so partitions are recognised:
sudo kpartx -d debian.img
sudo kpartx -av debian.img
From this point the two partitions are available as /dev/mapper/loop0p1 and /dev/mapper/loop0p2. Now I need to format the filesystems:
sudo mkfs.vfat /dev/mapper/loop0p1
sudo mkfs.ext3 /dev/mapper/loop0p2
sudo e2label /dev/mapper/loop0p2 pluggedroot
Place the kernel image to the first partition:
sudo mkdir p1
sudo mount /dev/mapper/loop0p1 p1
wget -O - \
http://dreamplug.googlecode.com/files/uImage-DreamPlug%20v9 |
 sudo dd of=p1/uImage
sudo umount p1
Please note, that although there is a newer kernel available, I am still using an elder one, because otherwise, the dmesg is full with messages related to gpio. The version that I have is:
Linux xxx 2.6.33.7-dirty #1 PREEMPT Tue Nov 29 06:13:06 EST 2011 armv5tel GNU/Linux
Now, copy the root filesystem to the second one:
sudo mkdir p2
sudo mount /dev/mapper/loop0p2 p2
sudo tar -cf - rootfs | sudo tar -xf - -C p2/ --strip-components=1
sudo umount p2
We can disconnect the virtual disk, and the partition mappings
sudo kpartx -d debian.img
Let us put it on a physical device:
time sudo dd if=debian.img of=/dev/sdb bs=1048576
That took around 40 seconds on my machine, using a Flash Voyager GT
976+1 records in
976+1 records out
1024000000 bytes (1.0 GB) copied, 38.7155 s, 26.4 MB/s

real0m38.734s
user0m0.000s
sys0m1.504s

Part 2 - on the Dreamplug

Let us see if I can use that USB stick at all on the Dreamplug device:
setenv x_bootcmd_kernel fatload usb 2:1 0x6400000 uImage
setenv x_bootargs_root root=/dev/sdc2 rootdelay=10 panic=10 init=/bin/bash
I also tried to specify root="LABEL=pluggedroot" but that failed. After it booted up, I finished the debootstrap:
/debootstrap/debootstrap --second-stage
You should see some lines showing up, and the latest is:
I: Base system installed successfully.
The serial console needs to be set:
echo 'T0:2345:respawn:/sbin/getty -L ttyS0 115200 linux' >> /etc/inittab
As well as the root password (I set it to the standard nosoup4u):
passwd root
Really important to flush the changes made to the filesystem:
sync
And leave the shell simply with:
exit
At this point, I disconnected my USB drive, and made a backup of it. With this action, I will have a snapshot, that could be easily modified according to my needs.
sudo dd if=/dev/sdb of=debian.phase2.img bs=1024 count=1000000
Boot to the new stuff. There is one thing that is interesting:
modprobe: FATAL: Could not load /lib/modules/2.6.33.7-dirty/modules.dep: No such file or directory
That needs to be fixed. The operating system's configuration will be covered in the next post.

References

Dreamplug - Updated Debian - gpio messages

I updated my dreamplug to the latest debian, and I see these messages in dmesg:
the gpio timer is called ! time is 211
This is somewhat really annoying, so I decided to try to install a fresh debian. The instructions I used for the update are: Update guide

Sunday 4 November 2012

Dreamplug serial console, plus faster boot

Some months ago, I put my hands on a Dreamplug device. I haven't done too much with it, just put arch on it. Now I decided to log in again, and play around with it. First I need to log in with the serial console. In order to do that, I need to be able to use my serial (ttyUSB0) as a simple user. I first added myself to the dialout group:
sudo gpasswd dialout -a matelakat
And as I don't want to log on/off, I activated the group memberships:
newgrp dialout
And off you go, I just had to start minicom:
minicom
Let's make booting a bit faster. See here. I will make a note of my original bootcommand here (The whole stuff needs to be one line!)
bootcmd=setenv ethact egiga0; ${x_bootcmd_ethernet}; setenv ethact egiga1;
 ${x_bootcmd_ethernet}; ${x_bootcmd_usb}; ${x_bootcmd_kernel};
 setenv bootargs ${x_bootargs} ${x_bootargs_root};
 bootm 0x6400000;
And so it is modified to:
${x_bootcmd_usb}; ${x_bootcmd_kernel};
 setenv bootargs ${x_bootargs} ${x_bootargs_root};
 bootm 0x6400000;
With this:
Marvell>> setenv bootcmd '${x_bo...
And save the stuff:
Marvell>> saveenv
And reset
Marvell>> reset
So that the device will boot significantly faster.

Wednesday 5 September 2012

vim colorschemes

I know, you can use google to get links, but let me put some bookmarks here, if you want to try out some vi color schemes: VIM color scheme test

Tuesday 28 August 2012

Computer: Give me ű betű

Ah, I am Hungarian, however, recently I changed to a GB keyboard. I was really missing letter ű So I did:
xmodmap -e "keycode  94 = backslash bar backslash bar U00171 U00170 d"
So that when I press Right Alt + \ I have my ű, with Shift, I have my Ű

Special thanks goes to: Black Eternal and Bill Beiga

Wednesday 8 August 2012

tmux, gvim automation

tmux

List panes
tmux list-panes
Select a pane
tmux select-pane -t 0
Send Ctrl-P and Ctrl-N to pane #1
tmux send-keys -t 1 C-p C-m

gvim

you will need gvim to do these things

Start vim as a server
vim --servername "coding"
Send a message to vim
vim --servername "coding" --remote-send '<Esc>:!ls<CR>'

Saturday 30 June 2012

Uncle Bob Videos

I simply like them:

svn in virtualenv

I had some issues having svn in my virtual env - because it is created with the --no-site-packages (as it is the default).
What to do?
From the local python, this script will print out the commands for you:
python - << EOF
from os.path import dirname, join
import svn
dist_packages_dir = dirname(dirname(svn.__file__))
print 'cp -r', ' '.join(
  [join(dist_packages_dir, lib) 
    for lib in ['svn', 'libsvn']]),\
  "yourvirtualenvssitepackagesdir"
EOF
And replace yourvirtualenvssitepackagesdir with the site packages dir of your virtualenv

Saturday 23 June 2012

Useless Tests

Make sure, that all the tested functionality is used

Connect Bluetooth Mouse to Linux

Thanks, Arch wiki
hciconfig
hcitool scan
Scanning ...
       00:02:76:25:A2:23ThinkPad Bluetooth Laser Mouse
echo "0000" | bluez-simple-agent hci0 00:02:76:25:A2:23
RequestPinCode (/org/bluez/899/hci0/dev_00_02_76_25_A2_23)
Enter PIN Code: Release
New device (/org/bluez/899/hci0/dev_00_02_76_25_A2_23)
dbus-send --system --type=method_call --print-reply --dest=org.bluez "/" org.bluez.Manager.ListAdapters
dbus-send --system --type=method_call --print-reply \
--dest=org.bluez "/org/bluez/899/hci0/dev_00_02_76_25_A2_23" \
org.bluez.Device.SetProperty \
string:Trusted variant:boolean:true
sudo hidd --connect 00:02:76:25:A2:23

Saturday 9 June 2012

Python package structure

I always forgot what should be - the project structure - the contents of setup.py Basically it is just a duplication of The Hitchhiker's Guide to Packaging v1.0 documentation/Creating a Package
TowelStuff/
    bin/
    CHANGES.txt
    docs/
    LICENSE.txt
    MANIFEST.in
    README.txt
    setup.py
    towelstuff/
        __init__.py
        location.py
        utils.py
        test/
            __init__.py
            test_location.py
            test_utils.py
And your setup.py should look like this:
from setuptools import setup

setup(
    name="TowelStuff",
    version="0.1",
    packages=["towelstuff"],
    install_requires=["nose"]
    )
The other question: What should the subversion structure look like? I looked the Trac guys, to see how they do:
http://svn.edgewall.org/repos/trac/trunk/
  ...
  trac
  setup.py
  ...
So I will use the same setup.

Sunday 3 June 2012

Specify ssh port for subversion in IDEA

Say you have an ssh server listening on the port 2222. You can edit yout personal config file, located at
~/.subersion/config
but IDEA won't pick that up. Use the global config file instead:
$ grep -B1 222 /etc/subversion/config
[tunnels]
sshtunnel = ssh -p 2222

And now you can do things like:
svn ls svn+sshtunnel://HOSTNAME/repolocation

Sunday 29 April 2012

Openswan configuration files

I wanted to move my openswan ip configuration to my new computer. So this is a short list of the files copied, and actions taken.
sudo apt-get install openswan
I choose not to generate a certificate.
Files needed:
/etc/ipsec.conf
/etc/ipsec.d/certs
/etc/ipsec.secrets
/etc/ipsec.d/private/
Instructions:
ipsec setup --stop
ipsec setup --start
ipsec auto --up connection_name

DWM and java applications

Java applications do not run on dwm, so you need to type:

wmname LG3D
unset AWT_TOOLKIT
./app
Credits should go to these pages:
Jtang
Arch Linux/DWM

Compile DWM on Ubuntu

My Good Friend told me about suckless.org. In the meanwhile, I installed the new Ubuntu version (12.04) on my ssd drive. I was almost happy, but I felt, that this is just too much for me. I don't want to follow the Unity line, I am fed up with the Windows like feeling, I am fed up with the mouse, I want to work with my computer. So let's give a try to a minimalistic approach: dwm.
First, I just did a lazy apt-get install dwm, but after a day, I thought It is a good idea to look at the source code, do some configuration, etc.

hg clone http://hg.suckless.org/dwm
cd dwm
sudo apt-get install libxinerama-dev
sudo make clean install
cat /usr/share/xsessions/dwmcustom.desktop
[Desktop Entry]
Encoding=UTF-8
Name=DwmCustom
Comment=Dynamic window manager
Exec=/usr/local/bin/dwm
Icon=dwm
Type=XSession
That's all you need to have your custom dwm compiled.

Saturday 28 April 2012

WPA on Linux

I have used Ubuntu's builtin WYSIAYG network manager. At some point I needed some more control over my network interfaces. I wanted a dhcpd to listen on eth0, and wanted my wlan to connect to my router. So I disabled the network manager:
sudo /etc/init.d/network-manager stop
And configuring the wpa:
wpa_passphrase ssid passphrase > /etc/wpa_supplicant/wpa_supplicant.conf
wpa_supplicant -iwlan0 -c/etc/wpa_supplicant/wpa_supplicant.conf &
dhclient wlan0

Friday 6 April 2012

Prepare for SSD installation

I have bought an Intel 520 SSD, and I would like to install it professionally. So some notes to consider:
  • EBS (Erase Block Size) - align partitioning to EBS - I was not able to know what is the EBS, so accoding to the ARCH wiki, I will align my first partition to 1MB (latest fdisk automatically aligns all partitions to 2048 sectors, or 1024 KiB, which should work for all EBS sizes)
  • Filesystem: ext4 with noatime (reads wont modify anything)
  • ext4 with discard option - for TRIM support
  • I will let filesystem to do journalling
  • Check TRIM is working
  • seq 1 1000 > testfile
    sync
    hdparm --fibmap testfile
    testfile:
     filesystem blocksize 4096, begins at LBA 2048; assuming 512 byte sectors.
     byte_offset  begin_LBA    end_LBA    sectors
               0   29868576   29868583          8
    hdparm --read-sector 29868576 /dev/sda
    /dev/sda:
    reading sector 29868576: succeeded
    0a31 0a32 0a33 0a34 0a35 0a36 0a37 0a38
    0a39 3031 310a 0a31 3231 310a 0a33 3431
    ...
    
    rm testfile
    sync
    /dev/sda:
    reading sector 29868576: succeeded
    0000 0000 0000 0000 0000 0000 0000 0000
    0000 0000 0000 0000 0000 0000 0000 0000
    ...
    
  • Kernel 2.6.33+ has TRIM
  • Over-Provisioning - I will go for 20% OP (I will have one partition, which consumes 80% of the drive)
  • Change I/O Scheduler to noop or deadline for the ssd device
  • echo noop > /sys/block/sdX/queue/scheduler
 Sources:

Thursday 5 April 2012

Disable mdadm autodetect

I wanted to create a raid array from USB drives. As USB drives are changing, I don't want mdadm to auto-assemble the arrays during system startup.
[root@alarm ~]# cat /etc/mdadm.conf | grep DEVICE
DEVICE /dev/null

Wednesday 28 March 2012

Dreamplug USB drive performance

I attached an USB drive to my new toy (dreamplug) to see what performance can I expect. Let me share the results:

dreamplug-debian:/mnt/disk# dd if=/dev/zero of=j bs=1024 count=1000000         
1000000+0 records in                                                           
1000000+0 records out                                                          
1024000000 bytes (1.0 GB) copied, 61.06 s, 16.8 MB/s                           
dreamplug-debian:/mnt/disk# dd if=j of=/dev/null bs=1024 count=1000000         
1000000+0 records in                                                           
1000000+0 records out                                                          
1024000000 bytes (1.0 GB) copied, 37.4651 s, 27.3 MB/s                         

I think I am happy with these results. The filesystem was reiserfs.

Friday 9 March 2012

Lettuce from jenkins

In order to see, that during the development that I deliver business values, I tried out bdd. Technically lettuce is driving mechanize, which is sending request to the deployed application. The whole stuff is controlled by Jenkins. Lettuce threw an error:


.../lettuce/terminal.py", line 82, in get_terminal_size_unix
    return int(cr[1]), int(cr[0])
TypeError: int() argument must be a string or a number, not 'NoneType'



It happened after Jenkins was started from init.d I performed the following patch:


--- lettuce-0.1.34-py2.6.egg/lettuce/terminal.py.orig 2012-03-08 17:47:21.959735489 +0000
+++ lettuce-0.1.34-py2.6.egg/lettuce/terminal.py 2012-03-08 17:48:23.649788786 +0000
@@ -79,4 +79,4 @@
         except:
             cr = (25, 80)


-    return int(cr[1]), int(cr[0])
+    return int(cr[1] or 25), int(cr[0] or 80)
Anyhow, it seems, that it has already been fixed:

Send flash message from spring security

When I log in, I want to see a flash message, saying "Successful login"
So as you hit the login page, you are served by the secutity filter, and according to the PRG pattern, you will be redirected to the page originally requested. I extended spring's SavedRequestAwareAuthenticationSuccessHandler so that it will ask a SessionFlashMapManager to populate the flash message to the session.
On My context xml:
<bean class="somepackage.AuthenticationSuccessHandler" name="authSucc">
        <property name="messageKey" value="flashmessage"/>
        <property name="message" value="flashmessage.afterlogin"/>
</bean>
...

<security:http auto-config="true" use-expressions="true">
...
    <security:form-login login-page='/login' authentication-success-handler-ref="authSucc"/>
...
</security:http>
You have to define the value in your messages.properties:
flashmessage.afterlogin=Successful login

And its implementation is:


package somepackage;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.web.servlet.FlashMap;
import org.springframework.web.servlet.support.SessionFlashMapManager;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * Created by matelakat at 3/9/12 7:44 PM
 */
public class AuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
    final Logger logger = LoggerFactory.getLogger(AuthenticationSuccessHandler.class);


    private ApplicationContext applicationContext;
    private String messageKey;
    private String message;


    @Autowired
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }


    public String getMessageKey() {
        return messageKey;
    }


    public void setMessageKey(String messageKey) {
        this.messageKey = messageKey;
    }


    public String getMessage() {
        return message;
    }


    public void setMessage(String message) {
        this.message = message;
    }


    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                                        Authentication authentication) throws ServletException, IOException {
        SessionFlashMapManager sessionFlashMapManager = new SessionFlashMapManager();
        FlashMap fm = new FlashMap();
        fm.put(getMessageKey(), getMessage());
        sessionFlashMapManager.saveOutputFlashMap(fm, request, response);
        super.onAuthenticationSuccess(request, response, authentication);
    }
}

On my freemarker template:
<#import "spring.ftl" as spring>
<#if flashmessage??>
    <div id="message"><@spring.message flashmessage/></div>
</#if>


Sunday 4 March 2012

Blink Thinkpad's light as a user

I want to get some feedback from my local jenkins instance, so I decided to ask Jenkins to blink my thinkpad's keyboard light.
I can control the light as root
# echo 255 > /sys/class/leds/tpacpi\:\:thinklight/brightness
# echo 0 > /sys/class/leds/tpacpi\:\:thinklight/brightness
But my Jenkins is running with a root account. This is where udev comes to play.
# udevadm info -q all -p /sys/devices/platform/thinkpad_acpi/leds/tpacpi\:\:thinklight/
P: /devices/platform/thinkpad_acpi/leds/tpacpi::thinklight
E: UDEV_LOG=3
E: DEVPATH=/devices/platform/thinkpad_acpi/leds/tpacpi::thinklight
E: SUBSYSTEM=leds
I tried to do it with the MODE keyword - FAIL:
# cat /etc/udev/rules.d/80-thinklight.rules
SUBSYSTEM=="leds", MODE="0666"
For some reason it did not set the permissions. I think some concepts are not clear enough for me.
So I went on another route, created a shell script to manipulate the permissions
$ cat /sbin/led.sh 
/bin/chmod o+w /sys/devices/platform/thinkpad_acpi/leds/tpacpi\:\:thinklight/brightness
And run it from udev:
$ cat /etc/udev/rules.d/99-thinklight.rules
SUBSYSTEM=="leds", RUN+="/bin/sh /sbin/led.sh"
Ugly, but WORKS




Saturday 3 March 2012

Lettuce and IPython interactive shell

Just doing the stuff with mechanize. At some point, it would be great to get an IPython console. so Why not?
@step(u'And give me IPython')                    
def and_continue_here(step):                   
    import IPython                             
    import sys                                 
    shell = IPython.InteractiveShell()         
    ip = IPython.core.ipapi.get()              
    p = IPython.core.debugger.Pdb(ip.colors)   
    p.reset()                                  
    p.interaction(sys._getframe(0), None)      

Friday 2 March 2012

Javascript headless testing

Just a note, so that I won't forget. Use rhino and EnvJs to run jQuery code without a browser. In this way I would be able to test Unobtrusive Javascript code, to see it finds all elements on a rendered page.

BDD testdrive

I gave a try to BDD: looks really good. Fluent textual assertions. I tried out Lettuce. Fine vi syntax highlight stolen from the Cucumber project, see here: gvim syntax file for Gherkin. These are "heavy" functional tests, accessing the depolyed application. Mechanize was used to act as a browser. While I tried to write some assertions, I thought I need the power of jQuery. I asked an old friend of mine, PyQuery to help me. All the stuff was put together very quickly. Ask lettuce to create xUnit output, set up a Jenkins job, run after application deploy - I really like it.