Using rubyzip to create zip files on the fly

In my Daily Fratze project the users should be able to download their faces as a zip file backup.

Until now they have been able to upload zip files. For that, i used rubyzip which worked quite well.

As a starting point i found a nice article on the joy of rubyzip, but this has a major flaw for me. It uses the Zip::ZipFile interface to create its archives. This interfaces takes a filename as parameter and either creates this file if it doesn’t exists or tries to open it as a zip archive.

I doesn’t want my directories polluted by some random zip files so i tried to use TempFile. Creating a new TempFile leads to an existing file which Zip::ZipFile cannot open.

My solution uses the more basic interface Zip::ZipOutputStream. Further requirements were adding binary files with arbitrary names and not like in the examples of rubyzip, creating new files with some textual content. Here we go:

require 'zip/zip'
require 'zip/zipfilesystem'
 
t = Tempfile.new("some-weird-temp-file-basename-#{request.remote_ip}")
# Give the path of the temp file to the zip outputstream, it won't try to open it as an archive.
Zip::ZipOutputStream.open(t.path) do |zos|
  some_file_list.each do |file|
    # Create a new entry with some arbitrary name
    zos.put_next_entry("some-funny-name.jpg")
    # Add the contents of the file, don't read the stuff linewise if its binary, instead use direct IO
    zos.print IO.read(file.path)
  end
end
# End of the block  automatically closes the file.
# Send it using the right mime type, with a download window and some nice file name.
send_file t.path, :type => 'application/zip', :disposition => 'attachment', :filename => "some-brilliant-file-name.zip"
# The temp file will be deleted some time...
t.close

Edit: The basename given to Tempfile.new is what the name says: A basename. It doesn’t need to denote a full path. Tempfile creates an arbitrary path for you in the default temporary directory.

| Comments (16) »

21-Jan-08


Notes on RedHat / Oracle Enterprise Linux, ADS und Samba

Recently i needed windows clients to access a directory on the database server from which CLOBs where created. Since the server is an Oracle Enterprise Linux, i could follow the steps explained behind the following link to get Samba up and running with the Active Directory run by a Windows 2k3 server:

Which steps must be done to run Samba with AD-Integration

In short:

Setup Kerberos:

[logging]
default = FILE:/var/log/krb5libs.log
kdc = FILE:/var/log/krb5kdc.log
admin_server = FILE:/var/log/kadmind.log
 
[libdefaults]
default_realm = WINDOWS.JARA23.CO.UK
dns_lookup_realm = false
dns_lookup_kdc = false
ticket_lifetime = 24h
forwardable = yes
 
[realms]
WINDOWS.JARA23.CO.UK = {
   kdc = server.windows.jara23.co.uk
   admin_server = server.windows.jara23.co.uk
   default_domain = windows.jara23.co.uk
}
 
[domain_realm]
.kerberos.server = WINDOWS.JARA23.CO.UK
.windows.jara23.co.uk = WINDOWS.JARA23.CO.UK
 
[kdc]
profile = /var/kerberos/krb5kdc/kdc.conf
 
[appdefaults]
pam = {
   debug = false
   ticket_lifetime = 36000
   renew_lifetime = 36000
   forwardable = true
   krb4_convert = false
}

Test this with

kinit admin@WINDOWS.JARA23.CO.UK

After entering a valid password, this command should give no error.

Setup a minimal Samba configuration:

#GLOBAL PARAMETERS
[global]
   workgroup = MIDGARD
   realm = WINDOWS.JARA23.CO.UK
   preferred master = no
   server string = Linux Test Machine
   security = ADS
   encrypt passwords = yes
   log level = 3
   log file = /var/log/samba/%m
   max log size = 50
   printcap name = cups
   printing = cups
   winbind enum users = Yes
   winbind enum groups = Yes
   winbind use default domain = Yes
   winbind nested groups = Yes
   winbind separator = +
   idmap uid = 600-20000
   idmap gid = 600-20000
   ;template primary group = "Domain Users"
   template shell = /bin/bash
 
[homes]
   comment = Home Direcotries
   valid users = %S
   read only = No
   browseable = No
 
[printers]
   comment = All Printers
   path = /var/spool/cups
   browseable = no
   printable = yes
   guest ok = yes

Enable winbind, pam and other parameters with system-config-authentication.

Start Samba and join the domain with:

net ads join -U Administrator

| Comments (0) »

17-Jan-08


Safari Webinspektor

Vor fast 1.5 Jahren schrieb ich im Zusammenhang mit Daily Fratze on rails über den Safari Inspektor, der damals nur in den Nightlies verfügbar war: Damaliges Daily Fratze Preview.

Leider habe ich den Inspektor im aktuellen Safari 3 nie mehr gesehen, bis ich vor ein paar Tagen auf das SülzOMat Blog stieß. Einfach im Terminal folgenden Befehl absetzen:

defaults write com.apple.Safari WebKitDeveloperExtras -bool true

Safari neu starten und man hat über das Kontextmenü den neuen Punkt “Element-Informationen”, der den Inspektor startet, der dem beliebten Plugin “Firebug” für Firefox in kaum etwas mehr nachsteht.

Vielen Dank für diesen Beitrag!

| Comments (0) »

08-Jan-08


Oracle 11g: Default case-sensitive passwords

ORA-01017: invalid username/password; logon denied

Yiipiee… Although i created my schema with the usually scripts we use, i got a logon denied… Switching context from Oracle Forms 6 to Reports 6… (Please, don’t say anything about legacy apps).

I didn’t notify that passwords are now case-sensitive in a Oracle database (which is a good thing), because of two reasons: I created the user via SQL*Plus and there is a environment variable called “sqlcase” that converts, if set to upper or lower, all SQL code to the according case. Bummer! Even quoted literals are converted… Stupid shit, i think. But in my case, this thing was set to mixed, so i ended up with a lower case password as with 11g “sec_case_sensitive_logon” is set to true as default:

SQL> SHOW parameter sec_case_sensitive_logon
 
NAME                     TYPE    VALUE
------------------------ ------- -----
sec_case_sensitive_logon BOOLEAN TRUE

Now with forms things are different, again. Forms runtime don’t convert the lowercase password in the connect string so i could log in but it converts the password to uppercase if you switch contexts to Reports runtime which couldn’t connect and ended up with the ORA-01017. Weird stuff, as everything else worked as expected, although Forms and Reports 6 are ages old compared to 11g.

In the end, i changed to user to a an uppercase password, so the password passed from Forms to reports is the right one. I didn’t set sec_case_sensitive_logon to false, as i think, its way better to have standard password handling and to me, this means case-sensitive and nothing else.

I found some valuable information on this topic in the following blog: Mark A. Williams :: Blog, didn’t know about that sqlcase shit before.

For more information see:

| Comments (0) »

08-Jan-08


From Parallels Desktop to VirtualBox

For backup and testing purpose i keep a virtualized version of my webserver running under Parallels Desktop for Mac on my mac.

Recently Dell had some very convenient offers on their PowerEdge machines and i could barely resist to buy one, but finally, i managed to: cut the crap, i don’t want to have even more boxes standing around at home.

But i was thinking again: Whats better than one backup? Redundant backups 🙂

So i downloaded a copy of Parallels Workstation to install it on my PC at work. Equipt with a DVD and my image file from Parallels Desktop, i fired up the PC Version and after fiddling around with some pathes i can confirm that the Parallels Desktop 3.x vms are compatible with Parallels Workstation 2.x vms, at least a vm with a Debian OS inside.

But to me, performance was poor. My mac is a 2Ghz Core2Duo Mac Book with 3GB Ram, my Pc is a 2.66 Core2Duo Dell with 2GB Ram and Parallels Workstation was way slower on the PC than unter OS X. In addition: I didn’t want to spend another 50$ for Parallels (i already bought versions 2 and 3 for Mac, should be enough), so i thought about alternatives on my PC running Windows XP as host.

I already knew about QEMU which in contrast to Parallels and VMWare is a processor emulator and not a virtualization tool and therefore must be slower. I recommend the QEMU Manager for Windows Users, as this thingy already contains the kqemu virtualization extension.

Installation is dead simple and to my surprise, it was enough to convert my Parallels extending image with the Parallels Image Tool to a plain disk which i could use without further changes with QEMU.

Debian is very stable against the few “hardware” changes. The only problem i had that Debian wouldn’t find my eth0 device although the Realtek 8139too module was loaded. Solutions: The ethxxx devices are bound to the hardware (mac) address. I could look up the mac address in the Parallels configuration file, add this number in the QEMU config and voila, everything was up and running.

Somewhat complicated is bridged networking with QEMU. First you need the TAP-Win32 driver which is brought to you by the great OpenVPN project. The most simple way to install is through OpenVPN Gui for Windows.

After install add a new TAP device through the startmenu entry and then, the clou: There are some howtos which recommend enable bridged networking through the Internet Connection Sharing facilities of windows but it’s much simpler than that: Under network connections, select your default LAN Connection, the tap device, right click and choose bridge networks.

After that, change the network mode in your QEMU vm from user networking to tap networking and you’re ready to go.

Performance was quite good (at least at Parallels speed) but i guessed, there was room for more so i look for VMWare. Unfortunately, the QEMU image tool qemu-img.exe repeatedly crashed while converting my Parallels Image to the vmdk format so this was a dead end for me.

Although the Parallels image worked fine with QEMU and later on also in our Oracle VM Server as a hw virtualized XEN machine, i was not content and i looked out for VirtualBox which is available under a GPL license without USB support which i don’t need, but is able to use Intel VT-x and AMD-V technology.

I needed to convert the Parallels image from a raw disk format to innoteks vdi format, following the steps explained here.

All the tools mentioned there are also available under a Windows installation of QEMU and Virtualbox. Commands as follow:

qemu-img.exe convert foobar.hdd foobar.bin
VBoxManage.exe convertdd foobar.bin foobar.vdi

For bridged networking unter VirtualBox the same applies as to QEMU without the need for OpenVPN, you can add a TAP device right from within VirtualBox . If you already have the bridged described above, you can add the VirtualBox interface with a right click to that bridge. Also, to have Debian not change the eth number, add the same mac address in the network tap of VirtualBox.

All this said and done, booting the system was blazing fast and also the backup of my Daily Fratze project which is pulled via rsync from my server, runs extremly smooth and i have absolutely no hassle setting up Ruby on Rails, RMagick and MySQL under the Windows “Operating System” but can use Debian or any other distro with a sane environment.

After fiddling around with both VMWare and VirtualBox, i’d prefer VirtualBox over VMWare Server (which is also available for free {i.e. for giving VMWare your personal data}). VirtualBox has less overhead then VMWare Server, is simple to configure and as i said, amazingly fast.

When the OS X version leaves beta, i’ll switch from Parallels on my Mac to VirtualBox, at least for my server live backup, so i can make it redundant with the one some kilometres away (call me paranoid if you like) at my workplaces PC.

| Comments (17) »

07-Jan-08