Category Archives: software

create 1px alpha transparent png

We needed a background with opacity x% (where x could vary between 30 and 75). We decided to use a “alpha-transparent” PNG24.

How to generate such a PNG using ImageMagick?
Extremely simple – specify a “8-digit” color code using the syntax:

convert -size 1x1 xc:#123456DD test.png

The last 2 digits in the color code (DD) is the alpha channel(opacity/transparency value).

I also wrote a bash-script that generates 255 images for each value of the alpha-channel for a given color:

#!/bin/bash
#
# generate a directory containing 255 semi-transparent png24-files
# with transparencies from 0 to 100% (0-255) for a given color
# author: Andreas Philippi - Cubus Arts 2010                                                                                                                         
#
                                                                                                                                                                       
#convert(Imagemagick must be installed)
type -P convert &>/dev/null || {                                                                                                                                        
    echo "Imagemagick not found on your system." >&2; 
    echo "Debian/Ubuntu: sudo apt-get install imagemagick" >&2; 
    echo "Centos/Red Hat: yum install ImageMagick" >&2; 
    echo "FreeBSD: pkg_add -r ImageMagick" >&2; 
    exit 1; 
    }

#this script gets 1 parameter: the color code
if [ $# == 0 ]; then
   echo 'Usage: transparent_png_creator.sh ';
   echo 'Example: transparent_png_creator.sh 4F6681';
fi

#check the color code
if ! [[ "$1" =~ ^[0-9,A-F,a-f]+$ ]] ; then
    echo "Invalid color code!! Valid range: 000000 - FFFFFF" >&2; 
    exit 1; 
fi
   

#create the directory 
`mkdir $1`

for i in {0..255}
do
   hex_i=$(printf "%02x" $i)
   convert -size 1x1 xc:#$1$hex_i $1/$1_$hex_i.png
done

echo "Successfully created 255 images in directory $1"

monitor file and send alert mail on change/modify

For some reasons it can be very useful to be alerted on some specific file changes (for instance a database error log).

Using inotify-tools I wrote a script that can monitor a file sending alerts to an email address if that file is changed:


#!/bin/sh
#
# Monitor file $1 for changes
# Send an alert emai to $2 if file $1 changes
# usage: file_change_mail_alert.sh /var/log/messages your.name@domain.com
#

if [ -z "$2" ]; then
echo "Usage: file_change_mail_alert.sh "
exit 1
fi

#if a inotifywait for this file is already running
if [ $(ps aux | grep inotifywait | grep -c "$1" ) -gt '0' ]; then
echo "A process monitoring the file $1 is already running: $(ps aux | grep inotifywait | grep "$1" )";
exit 1;
fi

#if inotifywait exists
type -P inotifywait &>/dev/null || { echo "Error: This script requires inotifywait(http://wiki.github.com/rvoicilas/inotify-tools/) .... apt-get install inotify-tools ... " >&2; exit 1; }

#if the file exists
if [ -f $1 ]; then

echo "Monitoring file $1 for changes - sending alerts to $2"

while inotifywait -e modify $1; do
sleep 1
changes="$(tail -n5 $1)"
echo "The following change occured in the file $1 : $changes" | mail -s "Change in $1" $2
done
else
echo "Error: File $1 not found"
fi

This script can be started on server startup like this:
/path/to/file_change_mail_alert.sh /var/log/something.log your.email@domain.ro

Or put in in the crontab – once a day (it will exit if it detects that another instance of “itself” is already running)
0 6 * * * /path/to/file_change_mail_alert.sh /var/log/something.log your.email@domain.ro

unicorn start/stop and monitoring script

Today I wanted to write a script which could be used to start/stop/restart unicorn for a given application – without root privileges, to get a status and a special action(monitor) which can be called periodically and will restart unicorn automatically in case something went wrong

I found this very usefull blogpost on the net http://rubynyc.wordpress.com/2009/12/24/unicorn-scriptspin/ and modified the script with 2 additional “methods”: status and monitor. (I know … the code I added is pretty primitive… but it works)


#!/usr/local/bin/ruby

UNICORN_RAILS='/usr/local/bin/unicorn_rails'
APP_PATH="#{ARGV[0]}"

class Runner
class << self def start system "cd #{APP_PATH}; #{UNICORN_RAILS} -c ./config/unicorn.rb -E production -D" end def reload system "kill -s USR2 #{pid}" end def restart stop start end def graceful_stop system "kill -s QUIT #{pid}" end def stop system "kill #{pid}" end def pid File.read "#{APP_PATH}/tmp/pids/unicorn.pid" end def status begin system "ps -o user,pid,ppid,command -ax | grep #{pid}" rescue puts 'not started' end end #start if killed def monitor begin #this throws an exception in 2 cases: #1. if no unicorn-process for the current user exists #2. if the pid functions fails(throws an error) due to non-existant unicorn.pid-file throw 'processes killed manually' if (`ps -o user,pid,ppid,command -ax | grep #{pid}`).split("\n").size <= 2 rescue puts 'restarting ... ' start end end end end case ARGV[1] when "start" Runner.start when "reload" Runner.reload when "restart" Runner.restart when "stop" Runner.stop when "status" Runner.status when "monitor" Runner.monitor else STDERR.puts "usage ./unicorn_script.rb absolute_path_to_RAILS_ROOT [start|stop|restart|status|monitor]" exit(1) end

This script can be called from a cron every minute like this


* * * * * /path/to/this_script /path/to/rails_root/ monitor

It baiscally checks if the unicorn-processes exist(the master process and his spawned kids). If it does not detect at least one unicorn-process it will calls the start routine.

Apache: password protect directories except from local ips

We faced the following problem:

1. we want to let our clients test projects on our test webserver – using basic http authentication.

2. users from our local network should not be bothered by the username and password request

The solution we implemented:

Place a .htaccess – file in the webroot of your apache webserver with the following content:

Order deny,allow
Deny from all
AuthName "htaccess password prompt"
AuthUserFile /path/to/your/webroot/.htpasswd
AuthType Basic
Require valid-user
Allow from localhost #that would be me
Allow from 192.168.0.0/24 #suppose the ip range of your local network goes from 192.168.0.2 - 192.168.0.254
Satisfy Any

Use htpasswd to create the file AuthUserFile /path/to/your/webroot/.htpasswd and create at least one user with a password:

htpasswd -c /path/to/your/webroot/.htpasswd username
#you will be promtped for a password

Remarks:

1.depending on your local network configuration you want might to exclude the internal router/gateway IP from the range of IPs(192.168.0.0/24) that do not require authentication

2.of course, the apache virtualhost must be configured in such a manner that .htacces-files are evaluated. Take a look at the AllowOverride directive

avidemux ROCKS

There are a lot of video editing/ripping/encoding, etc… tools for Linux OS.

Some of them are missing basic functions, others have extremely complicated user interfaces with tons of parameters that have to be specified even for the most simple transformation.

Avidemux has an intuitive UI, can convert/encode/export your video in nearly every possible format.

I use avidemux to convert videos recorded with my digital camera (which has little processing power and therefore does a very weak compression) to MPEG-4 ASP(Xvid) – format using a two-pass encoding which allows setting a targeted video size.

Give it a try:
sudo apt-get install avidemux

2010 – Year of DEATH … for IE6 – i hope

Best wishes for 2010 … more time for you, your families + friends 🙂 … and a slow and painfull death for IE6!!!

IE6 (Microsoft Internet Explorer – version 6) tortures thousands of web-developers for years.  Every web-interface needed a lot of  “special hacks” to be rendered reasonably on IE6.

Think of the IE box model bug, the  CSS underscore hack, the <!–[if lte IE 6]>-like Syntaxes for including js or css only for IE6, IFrames to cover select boxes when using a absolutely-positioned div, etc…

The statistics of Factureaza.RO show the following trend of IE6 usage:

  • june 2009: 21%
  • july 2009: 22%
  • august 2009: 19%
  • september 2009: 17%
  • october 2009: 18%
  • november 2009: 18%
  • december 2009: 17%
  • january 2010: 16%

Let us put an end to this agony and join the guys at end6.org. Just include the piece of JS offered by them – and your visitors will be advised to upgrade ther browser.

<!–[if gte IE 7]>
alert(‘Happy new year’);
<![endif]–>