| |
mark :: blog
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/index.pl 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 youtube.pl. Now to get it created every ten minutes
we use the cron container by creating a
file .openshift/cron/minutely/perljob with the contents:
#!/bin/bash
MIN=`date +%M`
if [ $((${MIN#0} % 10)) == 0 ]; then
perl ${OPENSHIFT_REPO_DIR}/youtube.pl > ${OPENSHIFT_REPO_DIR}/index.html
mv -f ${OPENSHIFT_REPO_DIR}/index.html ${OPENSHIFT_REPO_DIR}/perl/index.html
fi
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}/youtube.pl ${OPENSHIFT_REPO_DIR}/perl/youtubepl.txt
perl ${OPENSHIFT_REPO_DIR}/youtube.pl > ${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:
http://fomc-esoom.rhcloud.com/. If this
blog was useful (blatent plug!) please click on
"Tracy Cox", make sure
you're logged in, and click "like" ;)
Created: 09 Oct 2012
Tagged as: cloud, openshift, perl, redhat
1 comment
(new comments disabled)
|
|
|
Hi! I'm Mark Cox. This blog gives my
thoughts and opinions on my security
work, open source, fedora, home automation,
and other topics.
pics from my twitter:
popular tags:
[all],
apache,
apachecon,
apacheweek,
cve,
cvss,
fedora,
financial,
geocaching,
ha,
metrics,
microsoft,
nashville,
north carolina,
red hat summit,
redhat,
security,
trips

|
|
awesome :0)