StephenCuppett.com

January 1, 2010

Mac OS X Reinstall/Restore via Time Machine is Awesome!

Filed under: Personal, Research — scuppett @ 10:17 pm

During the holiday vacation, my Macbook started booting up with a rather lengthy progress bar.  It would take about 10 minutes to complete.  The operating system would come up fine, but it started getting me nervous because it was occurring on every boot, not just once (like an update post-processing after reboot or something).  I had a feeling it was filesystem related just based on my experiences with Unix/Linux in the past.  I figured some kind of fsck was working its magic and some quick searches seemed to confirm the thought.  This was going to be my first attempt and resolving a system issue on the Mac; I’ve been lucky to have been on cruise control on there until now.

I eventually stumbled on the Mac’s console message viewer.  He’s extremely handy.  He zeroed me in on the culprit, “Incorrect number of thread records (#, #)”.  Okay, great message.  Maybe if you know the number is incorrect, you could tell me the correct number?  Maybe if you know the number is incorrect, you could just fix it?  Nah.

After some more research, the next remediation steps were to boot into the install disk, try running Disk Utility and also to start up a console, unmount the main volume and run fsck_hfs manually.  Nada.  All of those seemed to know all about my incorrect thread count, but none could make a correction.  I must admit though, with a lot of Windows (and Linux) diagnostics behind me, the utilities are at least navigable well presented on the install disk.

With the easy stuff behind us, my options now get expensive or drastic.   Most of the forum and support threads I found have DiskWarrior as the next logical step to try and fix the volume.  At $100, I’m not quite ready to give up.  I’m close though because with a BootCamp volume and a pretty important Windows 7 install on the other partition, formatting  and redoing all that work doesn’t sound palatable… yet.

I do have AppleCare.  I use the online form, open a ticket and schedule a callback on 12/31.  Cool!  The phone rings.  The automated attendant informs me they are closed.  What!?  Why can I schedule the call then?  The call ends.  Two minutes later, I’m called again by Mr. Robot.  Ugh.  I re-schedule for another day to avoid infinite recursion!  A slick customer service interface has a bug in it lets me schedule calls when they are on holiday and then has another bug in you can get in a loop.

OCD kicks in.  I want this to go away now.  What do I have unique to the Windows partition, really?  Just a Quickbooks file, that’s not a big deal, back it up separate.  Time Machine backup is a little behind, let’s take a new one.  Okay, I’ll use the install disk to erase the Mac drive and try re-installing Snow Leopard.  I’m sure my users and data will restore from Time Machine.  I may have to do some homework to access my Windows partition (if it survives).  I’ll probably have to redo my network setup, install iLife, Firefox, eclipse, etc.  If my Windows partition doesn’t survive, that’ll be fun the next few days…

The install is pretty quick, 30-60 minutes.  No problem.  It restarts.  Here comes my getting started page.  There’s a migrating/moving via Time Machine button.  Sure.  The next screen was pretty surprising.  I had options for restoring four things… something like users, data, settings and apps!  Are you serious!?  Wow.  Something will probably not be quite right, but this is HUGE and a big divergence from my non-Mac experiences…

The Time Machine restore cranks through my files.  It completes.  I restart.

At the gray startup screen, I hold down “Alt”.  Will Windows still be there?  It is!  Yay!  He comes up just fine.  That, in and of itself, is extremely cool.  I had a feeling the partition would be there just from the screens I was seeing on the OS X install disk.  Not sure about Vista or 7, but Windows prior would always blow away any bootloader I would have, no question.

I then start OS X.  Surprise, surprise, I must have the correct number of thread records now, it boots in normal time :) .  The login screen has my users, their little thumbnails, cool.

Logging on was what really blew my mind.  Once inside, all updates were already applied, the network settings are all there and everything/EVERYTHING was there; iLife (a separate install), an Eclipse I had downloaded and extracted somewhere funny, every other app and setting.  Wow.  I was so dumbfounded and filled with confidence about the whole process if it takes me more than an hour to find a button to undo a setting not 100% to my liking in the future, I will re-install from scratch and use the last backup not containing it in a heartbeat…  For the Mac-savvy, or anybody who read into Time Machine since it came out, this may seem funny, but I still can’t get over it.

It didn’t escape me, going through the motions, OS X was treating it the same as if I had purchased a new computer and was just “moving” to it with my Time Machine backup.  Companies don’t use Linux desktops (yet, ugh), but is it this smooth for the minority that do when going to new hardware?  I doubt it.  For companies using Windows desktops, entire armies of IT guys are still trying to purchase or develop applications/processes/scripts to make moving to new hardware this easy for the employees at large when hardware refreshes are in order.  I know it wasn’t this smooth for me when I got my new gear the last time (XP -> XP).  I have another Windows upgrade coming in January (XP -> 7), we’ll see how that goes…

August 2, 2009

Burning a Blu-ray in Linux

Filed under: Personal, Research — scuppett @ 9:52 am

I’ve had a blu-ray burner in my Linux system for quite a while. Since about Fedora 8, I’ve been using commands to burn backups onto single-layer BD-RE media.  I gleaned those commands out of a posting about dvd+/-rw tools (google cache).  Here are the basics…

Ad-Hoc Burning

growisofs -Z /dev/dvd -R -J /path/to/files

- later -

growisofs -M /dev/dvd -R -J /more/files

- to finalize -

growisofs -M /dev/dvd=/dev/zero

Writing an ISO

growisofs -dvd-compat -Z /dev/dvd=/path/image.iso

Erase the Disk

growisofs -Z /dev/dvd=/dev/zero

Format

dvd+rw-format -ssa=1G /dev/dvd

I’ve recently been trying to do some new things and I thought I would post that as of Fedora 11, Brasero can recognize and write files to my BD-RE media and also erase the disk to do it again.  K3b is still at 1.0.5 (not a KDE 4 compatible version) on Fedora and does not recognize the disk correctly for type and capacity, nor does it allow burning.

My recent searches pull up the same results from 2007 and 2008 as before where people were unsure, etc.  This is the current state of my world though.

July 27, 2009

Using SOAP (non-REST) web services with CakePHP

Filed under: Development, Research — scuppett @ 8:51 pm

I recently had a need to support a complex SOAP web service from CakePHP.  Cake provides some built-in support for REST based web services; however, this situation required more.  This post should show how to set this up on your own projects and still utilize all your normal controller and model goodness without too much screwing around.

Pleas see this attachment for the source code described in this article.

The method I will outline here requires the php-soap module.

First, the WSDL.  For my project, I started with a WSDL created in another tool.  My WSDL specifies a slightly different object set than my CakePHP application.  I’m sure with PHP5 and some finessing of the Model classes, you could probably use the same set; however, it was easy enough to just create some really vanilla objects to house the transport objects and use those to communicate with the webservice.  Both the WSDL and the receiving controller are present in the attachment.

What you will notice is that the *DTO objects defined in the controller file reciprocate the structure of the objects in the WSDL and the methods also are represented in the controller.  I put them in the controller file because it wasn’t really obvious to me where in Cake’s structure “outside code” should really go.  I have a separate configs.inc.php I pull in up the class hierarchy, but that’s about as non-conventional as I want to get.  Also, this controller is dedicated to just handling webservice requests and I only need these *DTO objects in that case, so locality wins and they are here.  No real engineering genius here, their structure mimics what is defined in the WSDL file.

The real magic is in the controller.  The controller’s remote() method is what handles the POST from the web via the port binding in the WSDL file.  The remote() function sets up some of the basic stuff for SoapServer and is easily identified in the PHP manual.  It’s even pretty easy to deduce we’re going to need to use SoapServer->setClass() somewhere and plug the name of our Controller in.   However, there was one tidbit in the comments section of the manual regarding SoapServer->setObject().  It wasn’t documented (at the time), but after experimenting and looking at the PHP source, it does exactly what we need here, sets the handling class to an instantiated (aka existing) class object instead of trying to spawn a new one.  Because we are already inside the CakePHP framework and running the remote() function, we already have the variables we want from beforeFilter(), we have our models loaded up, we may even have a user context from mod_auth_something.  Perfect!!!  So, we tell SoapServer to use our instantiated Controller.  Once the *DTO classes are mapped and SoapServer is configured, it’s as simple as having it handle STDIN to tickle the rest of the methods in your Controller with the parameters populated.  Two more tricks/problems remain:  debug level & autoRender.

First, debug level.  There’s bound to be a way around it; however, since I test with a web service client, when I do have a problem, I have to debug with lots of $this->log() calls.  Turning up debugging to 1 or 2 is problematic because then CakePHP doesn’t spit back properly formed XML to the web service client and usually the client takes a SoapFault when that happens.  I stick to debug level of 0 during development and deployment wrt the web service stuff.

Second, autoRender.  Because SoapServer does the actual outputting of XML response to the client, I set the layout in the Controller to Ajax and also explicitly call exit() at the end of the remote() method.  This ensures that CakePHP doesn’t send back a “Missing View”, half rendered $layout, or any other kind of automatic goodies.

I hope this article is helpful for anybody who might want/need to integrate a more elegant/esoteric webservice into their CakePHP architectures.  I’m sure there are probably cleaner ways to put this into custom View classes, utilize Components, etc… however, this was a straightforward approach I found has been working really well for one of my applications.

May 20, 2009

Avoid common querying mistakes…

Filed under: Development, Research — scuppett @ 6:23 pm

Avoid common querying mistakes with better Django models .. http://tinyurl.com/pv354k

December 18, 2008

HowTo: PostgreSQL – Adding more values to an ENUM type

Filed under: Development, Research — scuppett @ 12:40 pm

I recently had trouble manipulating an ENUM field I had created in PostgreSQL.  I couldn’t find any suggestions or samples easily on Google or in the manual and was able to get it to work, so I post it here.  The basic premise is there is an ENUM field type created, I need more possible values and to preserve the existing values I already have to keep code working.

Initial creation of the type and table:

CREATE TYPE var_type AS ENUM('text', 'number', 'date', 'boolean');

CREATE TABLE custom_fields (
    id bigserial PRIMARY KEY,
    name varchar(50) NOT NULL,
    pdf_type var_type NOT NULL
);

Running with this table for some time, invariably, new rows are created and there’s now a migration consideration.  As long as you are not using the table column as a reference in a foreign key, the following should work to preserve the data, drop and re-create the type.

The following creates a new column to hold the original text value:

ALTER TABLE custom_fields ADD COLUMN type_text varchar(15);
UPDATE custom_fields SET type_text = pdf_type::text;

We, then, need to drop the existing type and re-create it with the new values we want.  CASCADE automatically drops columns that depend on the type:

DROP TYPE var_type CASCADE;
CREATE TYPE var_type AS ENUM('text', 'number', 'date', 'boolean', 'list');

This last part was what I couldn’t figure out without thinking a little more.  When you add it back, you have to cast the varchar column back into the ENUM type.  I had tried a variety of concoctions here before getting this to work:

ALTER TABLE custom_fields ADD COLUMN pdf_type var_type;
UPDATE custom_fields SET pdf_type = type_text::var_type;
ALTER TABLE custom_fields ALTER pdf_type SET NOT NULL;
ALTER TABLE custom_fields DROP COLUMN type_text;

June 3, 2008

Web log anonymizer

Filed under: Development, Research — scuppett @ 8:48 pm

I recently had need to anonymize the IP addresses in an Apache access log.  It seemed like a simple task; however, there weren’t any really good code samples out there directly for it.  It’s a pretty simple exercise; however, given there wasn’t anything readily available, I figured I’d post it here so others might make use of it.  The only requirement it really had was to be able to process large logs rather fast and to maintain the same IP address mappings for multiple entries in the logs in order to preserve the actual traffic data as it relates to sessions.  With a little more work, I’m sure it could select random IP addresses in the same geo as the original one whereas this will probably evenly distribute the IPs across the globe (skewed for actual ownership of the ranges).

So here are the few lines of Perl that got the job done:

#!/usr/bin/perl
if ($#ARGV + 1 < 1) {
        print "\n\tUsage:\n";
        print "\t------\n\n";
        print "\tperl log_anonymize.pl file1 [file2 [file3 [...]]]\n\n";
        die "Please specify at least one file to use this script.\n\n";
}

my %forward = ();
my %reverse = ();

foreach (@ARGV) {
        open(ORIG, $_)
          or die "Failed to open input file for reading.";
        open(ANON, "+>", $_.".anon")
          or die "Failed to open destination file for writing.";
        while (<ORIG>) {
                if (/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/) {
                        if (!($forward->{$1})) {
                                $newIp = getNewIp();
                                while ($reverse->{$newIp}) {
                                        $newIp = getNewIp();
                                }
                                print "New mapping created: $1 -> $newIp\n";
                                $forward->{$1} = $newIp;
                                $reverse->{$newIp} = $1;
                        }
                        $repl = $forward->{$1};
                        $_ =~ s/$1/$repl/;
                }
                print ANON $_;
        }
        close(ORIG);
        close(ANON);
}

exit 0;

sub getNewIp {
        return int(rand(256)) . "." . int(rand(256)) . "." . int(rand(256)) . "." . int(rand(256));
}

It is fairly straightforward.  You invoke the Perl script with one or more arguments.  Every argument should be a path to an access log.  For each file, a new file of the same name and “.anon” appended gets created.  Across all those files, the script maintains an internal hash of the IPs it has mapped to a new, random IP address and will re-use those mappings as they are encountered.  It spits out a little message when the mappings occur so you could do some counts using ‘wc’ or something similar to see how many you had… or you could make it output a count at the end, it’s pretty simple to do either.

So that’s it, easy web log anonymizing via random IP address remapping.

Powered by WordPress