mark :: blog :: perl

[ 1 ]

tracy My wife is part of a competition to be the next face representing her college; with the final number of positive 'likes' on YouTube videos determining the winners. I thought it would be neat to create a scoreboard to track her progress, but also to learn how to use OpenShift all in one. It took me longer to write this blog than to write the code and deploy it live!

After creating an account on Red Hat OpenShift I created a new app based on perl and added a cron container so we can run our script every few minutes (to stop overloading the YouTube API if the site gets popular):

rhc-create-app -a fomc -t perl-5.10
rhc-ctl-app -a fomc -e add-cron-1.4
cd fomc
That was all the configuration needed, the 'fomc' directory is populated with everything you need. OpenShift when you deploy your app, figures out your perl dependencies and grabs and builds them for you, so no messing around needing root or even logging into the host.

By default the file perl/ will get served, but since we're going to cache the output from the script let's make it a html file index. To do this simply add to the existing perl/.htaccess file:

DirectoryIndex index.html
I wrote this quick perl program to query the YouTube API and do a simplistic HTML page of the number of likes for each of the videos in the playlist, and placed it in the main directory as Now to get it created every ten minutes we use the cron container by creating a file .openshift/cron/minutely/perljob with the contents:
MIN=`date +%M`
if [ $((${MIN#0} % 10)) == 0 ]; then
    perl ${OPENSHIFT_REPO_DIR}/ > ${OPENSHIFT_REPO_DIR}/index.html
    mv -f ${OPENSHIFT_REPO_DIR}/index.html ${OPENSHIFT_REPO_DIR}/perl/index.html
The cron job is run every minute, but the "modulus 10" ensures we only run the perl script once every 10 minutes. We use an intermediate file so that anyone visiting the site during the time it takes to create the file doesn't get a blank page. Finally we want to make sure that we don't have to wait ten minutes for the file to be created when we push the app, and give people the ability to see the source, so we create .openshift/action_hooks/post_deploy with the contents:
cp ${OPENSHIFT_REPO_DIR}/ ${OPENSHIFT_REPO_DIR}/perl/youtubepl.txt
perl ${OPENSHIFT_REPO_DIR}/ > ${OPENSHIFT_REPO_DIR}/perl/index.html
And that's it. Just run
git add .
git commit -a -m 'first app'
git push
And the app is up and running; here it is: If this blog was useful (blatent plug!) please click on "Tracy Cox", make sure you're logged in, and click "like" ;)

[ 1 ]

Hi! I'm Mark Cox. This blog gives my thoughts and opinions on my security work, open source, fedora, home automation, and other topics.