Archives

Dump A MySQL Database To YAML Using PHP

The Problem

I was recently working on a project that is built on WordPress / BuddyPress. I built up some content using the CMS features of WordPress in my development server and then I transfered the WordPress content to a beta server using the export/import functionality. That kind of stinks having to do that every time I want to push updates to my beta server. So I looked into using Doctrine to generate data fixtures which I could repeatedly run against the different database. Doctrine supports YAML files, which are extremely easy to edit by hand, so it seemed like a good idea.

So what’s the problem? Well, I didn’t currently have any of my data in YAML files. I didn’t want to had edit them or do some sort of copy and paste madness. So, I wrote a script in PHP to dump my data into a YAML file.

Continue Reading

Modify PHP CLI Include Path Dynamically

I was working with an application platform that I had just downloaded today. I just wanted to check it out and play with it a bit. In the application framework was an executable script that provided some command line utility. I fired up the script and received an error like “PHP Fatal error: require_once(): Failed opening required…”. Of course non of the files from the downloaded archived were in the include path for my system, but I didn’t want to just go adding it to my php.ini file just to play around with it short term. What to do.

Command Line Options

First I wanted to see if there was a command line option that I could pass to PHP to tell it what include_path to use for just this one execution. If you execute the following you’ll see the usage:
php --help
There is no include_path command line option, but there is the -d foo[=bar] option which allows us to modify any of the INI entries by key/value.

Continue Reading

Bash Script Init Style Status Message

Have you ever wondered how the OK and/or FAILED messages are printed out in the initialization of services on a Linux system during bootup? Well… I have. Anyway, I’ve always thought that it was really cool how the OK and FAILED messages printed out at the same column on the screen and in different colors. So of course I wanted to know how to do that so I could use it in my own scripts.

The Source

If you look inside some of your systm init scripts you’ll notice that many of them source the /etc/rc.d/init.d/functions script. This is actually where I found the code to produce neato status messages.

The Code

I could have just sourced that file (included) in my scripts and been fine, but I wanted my scripts to be self-contained so I took the liberty of pulling out the applicable code. Here it is below with comments:

Usage

So you can just drop the above at the top of your script and then call the appropriate functions where needed. For example you could do something like this:

Pretty simple, eh? Now what type of script might you need something like this in? Check out my previous article How To Create A Bash Install Script, which is a perfect example of the type of script in which you may want these status type messages.

Thanks for stopping by! I hope that you’ll leave any comments that you have below.

How To Create A Bash Install Script

Have you ever wanted to create an installer program on a Linux system, but didn’t want all the hassle of an actual install builder? I have. I recently had need of a simple, no frills installation that could:

  1. ask the user a few questions
  2. extract some binary files and
  3. do some stuff with those binary files.

So I’m going to show you how you can create such an installer with very little hassle. I can’t actually claim credit for this method though; I actually got the idea from Sun’s JDK installer for the Linux platform. You download a “.bin” file, change the file mode so that it is executable and then run it. It displays the end user license agreement, gets some feedback and then goes about installing Java for you. Well, if you open that .bin file up in a text editor (say vi) you’ll see that it’s nothing more than a shell script with a binary chunked onto the end. Thus my plans for world domination were born…


Bash Command Line Pro Tips

As someone that has used the Bash shell almost daily for over 15 years, Jason Cannon has accumulated several command line “tricks” that have saved him time and frustration. This guide is a collection of techniques that you can put to use right away to increase your efficiency at the command line. Start using these 10 tactics today to improve your effectiveness.

Click to download this resource for FREE.


The Script

The first part of this process is to create your install script. At the beginning of my script I put any environment variables and setup that I need for the script to run propertly.

For this exercise I chose a zip file as my binary distribution media. The ZIP_FILENAME variable holds the name of the file after extracting it from the binary installer. SCRIPT_LINES holds the number of lines in this script file. Actually it’s the number of lines plus one (more on this later). SUM1 and SUM2 are the two numbers that I got from running /usr/bin/sum on my zip file. We’ll use these numbers to verify the file after extracting it from the archive.

The next line uses the trap statement to execute some instructions in case the file exits. This just lets the script clean up after itself in the event that the user exits the file prematurely (ie using CTRL-C to close).

Now for the unpacking (or extracting) of our binary distribution media (a zip file in this case). Basically we’re using the tail command to do the job. The -n option indicates that our number argument is the number of lines that we want. Of course you see we’re using the $SCRIPT_LINES variable that we defined at the start of the script. We prepend the plus (+) operator in front of that number which tells the tail command that we want the last lines of the file starting at the number indicated. So we’ll get all of the file starting at the line just past our script, which will be the start of our zip file.

This little bit performs a checksum on the unpacked zip file and checks that against the values we set when building the installer.

The last little bit to this script does not really do that much. We’ve unpacked and verified our zip file so now we just unzip it. To finish up the script removes the zip file (we’ve already extracted it’s contents) and exits. Of course at this stage you could do any number of things that you wanted. You could compile some code if you wanted, prompt the user for feedback in order to build a configuration… whatever you need to do (that can be scripted).

Building The Installer

Ok, so we have our install script ready to go and we have a zip file (or binary distribution media). Now what? Building the final installer is actually a trivial matter from here. All we need to do is concatenate the script file (first) and the zip file into our final installer. We could also change the file mode so that it is executable.

Or if you want to get extremely fancy you can create a makefile! With this example a makefile is like hanging a photo on the wall with a jackhammer, but for more complex projects it may actually be warranted.

With this in a file called Makefile all you have to do is type make and it builds the installer for you.

Download

Thank you so much for stopping by and reading. I sincerely hope that I’ve helped someone out there. To conclude here is a download that includes a) the install script b) the zip file I used and c) the Makefile to build the final installer. Enjoy!

BashInstallerTutorial.zip (3 KB)

Before you go… please feel to leave comments below. Especially if you have any suggestions as to how this method could be improved. Thanks!

How To Use Subversion Over SSH

The Problem

I recently had a problem. I am a software developer and as such I use source control. For this purpose I generally prefer Subversion. I have been keeping my source repositories on an external drive with the intention to back them up to another secure location periodically (yeah, that never happens). I looked into online subversion hosting, but it seems a bit pricey for my few meager projects. I have a web host, but they don’t host subversion repositories over HTTPS as I’ve used in other situations. What to do?

The Solution

Subversion over SSH to the rescue! I actually have shell access to my web host (I can login to the server via SSH) and doing some quick research I discovered that I can run Subversion through an SSH tunnel. Cool! So how might one set this up?

Setting Up The Server

Prerequisites for the server are that you can login to your server via ssh. Got that? Good. Now while logged into your (Linux) server make sure that subversion is installed using the following commands:

Can’t find them? Well, my host has them and they have great rates. You can check out 1and1 hosting here. Once you are sure that subversion is installed you just need to create your repository. I did it like this:

Now just note (or write down) the full path to your repository from the pwd command (print working directory) and your off to setup the client.

Setting Up The Client

There are two requirements for the client setup. First you should install subversion. If you just install a client (ie TortoiseSVN) you may be missing some required files so go ahead and install the real deal. The second requirement is an SSH client that can handle tunneling. If you’ve performed the server setup over SSH then chances are you already have this. I use cygwin with OpenSSH installed so this how-to will be based on that.

Just so you know, subversion does not support connecting to the svn+ssh protocol “out of the box”. If it did then I wouldn’t be writing this article! The actual steps are mostly taken care of in the installation of the two requirements. Now you just need to edit the subversion config file which can be found at %USERPROFILE%/Application Data/Subversion/config. Mine is actually under %USERPROFILE%/AppData/Roaming/Subversion/config for Windows Vista. Do a search in this file for the [tunnels] section. You will actually see a nice description of the svn+ssh protocol there which I highly suggest reading. After that add the following line:

If you installed cygwin in another location then use that instead. Make sure that you use forward slashes too as the backslashes act as escape characters and then you have to use double backslashes and it just looks ugly. Save and close the file and you should be good! Yeah, that was easy.

Testing The Connection

Now to see if it actually works. Open up a DOS prompt (if you’re using Windows). Try to query your subversion repository like so:

Now obviously use the values in that URL that make sense for your setup. If you don’t know how to do that then press the red box with the X in it at the top right corner of your web browser and then turn off your computer! 🙂 Hopefully all will go well. The client will try to authenticate with your USERNAME and prompt you for the password. Then it will connect and execute the appropriate svn client calls on your remote repository and tunnel the results back to you. Awesome!

Now if you want to kick it up a notch (as Emeril would say) then you can try integrating your new remote repository with a nice graphical client. I highly suggest TortoiseSVN as it integrates right into your Windows shell. You could also try using Subclipse or any others that you like.

Revolution OS – The Linux Story

Revolution OS is a 2001 documentary which traces the history of GNU, Linux, and the open source and free software movements. It features several interviews with prominent hackers and entrepreneurs (and hackers-cum-entrepreneurs), including Richard Stallman, Michael Tiemann, Linus Torvalds, Larry Augustin, Eric S. Raymond, Bruce Perens, Frank Hecker and Brian Behlendorf. The film begins in medias res with an IPO, and then sets the historical stage by showing the beginnings of software development back in the day when software was shared on paper tape for the price of the paper itself. It then segues to Bill Gates’s Open Letter to Hobbyists in which he asks Computer Hobbyists to not share, but to buy software. (This letter was written by Gates when Microsoft was still based in Arizona and spelled “Micro-Soft”.) Richard Stallman then explains how and why he left the MIT Lab for Artificial Intelligence in order to devote his life to the development of free software, as well as how he started with the GNU project. Linus Torvalds is interviewed on his development of the Linux kernel as well as on the GNU/Linux naming controversy and Linux’s further evolution, including its commercialization. Richard Stallman remarks on some of the ideological aspects of open source vis-á-vis Communism and capitalism and well as on several aspects of the development of GNU/Linux. Michael Tiemann (interviewed in a desert) tells how he met Stallman and got an early version of Stallman’s GCC and founded Cygnus Solutions. Larry Augustin tells how he combined the resulting GNU software and a normal PC to create a UNIX-like Workstation which cost one third the price of a workstation by Sun Microsystems even though it was three times as powerful. His narrative includes his early dealings with venture capitalists, the eventual capitalization and commodification of Linux for his own company, VA Linux, and ends with its IPO. Frank Hecker of Netscape tells how Netscape executives released the source code for Netscape’s browser, one of the signal events which made Open Source a force to be reckoned with by business executives, the mainstream media, and the public at large. (this text is available under the terms of the GNU Free Documentation License)

This is the full length feature, so block off some time or bookmark it if you want to watch the whole thing. Enjoy!

* Click through to the site if you cannot see the video

Bash Script Naming – Say My Name, Say My Name!

I use Java as my main Linux server programming language these days and so I usually have a Bash script to go along with each program. The Bash script help encapsulate creating the proper CLASSPATH needed to run the Java program, calls the correct Java class and also passes through any command line parameters that I might need. Recently I had several Java programs that I wrote and bundled inside the same jar file which I needed to call separately. They had all of the same CLASSPATH and environment requirements, so why should I create separate Bash scripts for each one?

What’s In A Name?

Instead of a separate Bash script for each Java program I used a neat little feature of Linux called symbolic linking. I create in a “bin” subdirectory my main Bash script and then link to it using distinct names from the main program directory. The script then detects what names is being used to call it and executes the appropriate Java class. Some of the Bash code looks like this:

Notice the $@ command line parameter in the call to the Java class? That simply passes on the command line parameters used to call the script to the Java program.

So you can see, if I had a Java program in the com.franzone package who’s class name was TestOne, then I could create a symbolic link to my JavaCaller script to call it like this:

ln -s bin/JavaCaller TestOne

Now from the main directory I could call the ./TestOne script (symbolic link) which would actually execute the JavaCaller script with the TestOne name. That would cause my Bash script to call the TestOne Java class. Cool, eh?

Will The Google Phone Overtake The iPhone?

Google PhoneThe release of Apple’s iPhone has made tremendous waves in the cellphone industry. However, the honeymoon is over and the iPhone has been “cracked” in favor or more user customization and third party applications. Apple has announced an SDK (software developer kit) to be release for the iPhone coming in February 2008, but by then there may be a new player in town.

The long anticipated and highly rumored Google Phone is on it’s way! Not only that, but it’s operating system will be based on Linux and Java. That is a freely available and open software development language on top of an open-source and highly customizable operating system. Will the behemoth Google be able to put a dent in the momentum that is the iPhone? Will the openness of the Google Phone be able to woo otherwise iPhone buyers?

See what the world is saying about it:
Google Goes On Disrupting: Last Week OpenSocial, This Week Open Phones (Java Developer’s Journal)
USA Today’s Eleventh Hour Google Phone Facts/Speculation/Hype (Gizmodo)
Google enlists help for Google Phone (USA Today)