Mike Purcell

Mike Purcell

(100 comments, 185 posts)

This user hasn't shared any profile information

Posts by Mike Purcell

Git – Remove all untracked files


Sometimes you start down a path, and realize that there is a better approach or you just don’t have the time to continue. In git it’s easy to do a

git checkout -- .

for all existing files, but more difficult for untracked files. To remove all untracked files try this:

rm -rf $(git ls-files --others --exclude-standard)

Mac – XTerm – Change default cursor


Add any of the following to ~/.bash_profile to change the default cursor. Be sure to source the file after edit.

echo -e -n "\x1b[\x30 q" # changes to blinking block
echo -e -n "\x1b[\x31 q" # changes to blinking block also
echo -e -n "\x1b[\x32 q" # changes to steady block
echo -e -n "\x1b[\x33 q" # changes to blinking underline
echo -e -n "\x1b[\x34 q" # changes to steady underline
echo -e -n "\x1b[\x35 q" # changes to blinking bar
echo -e -n "\x1b[\x36 q" # changes to steady bar
Linux Logo

virsh – Refusing to undefine while domain managed save image exists


If you get a “Refusing to undefine while domain managed save image exists” message whilst trying to undefine an image using virsh, try passing the –managed-save flag like so:

$ ->   virsh undefine service-a-3 --managed-save 

Flag was added as part of this bug.

General Category

General – Downtime


Sorry for the recent outage. Got fed up with godaddy so been moving all of my domains over to namecheap.com, and this one slipped through the cracks, and I forgot about moving it until it started white-paging on Sunday. I will be posting more in the coming weeks.

General Category

Git – Revert Multiple Locally Changed Files


I use a windoze box at home and by default it uses the lame microsoft line endings vs linux line endings, which can wreck havoc on bash scripts. So I recently ran a command similar to the following:

find /path/to/files -type f -exec dos2unix {} \;

Much to my dismay, I found that it also converted binary files (.pngs, .gifs, etc), even though, according to the docs, it’s supposed to skip binary files by default. So I had to revert back all the binary files which were converted. Rather than do them one by one I tested and issued the following command (your mileage may vary):

gstatus | fgrep -is png | git checkout -- $(awk '{ print $3 }')
Linux Logo

Linux – SELinux – SEModule – Compile .pp module from .te file


I recently ran into a situation where I needed to grant access to certain /home dirs in order to get puppetmaster started with SELinux enforcing. And I had to do it in such a way that I could keep the resulting “type enforcement” (.te) file in version control, this would allow me to track human readable changes.


$ -> cd ~

$ -> echo > /var/log/audit/audit.log # this ensures a clean log for analysis

$ -> /etc/init.d/puppetmaster start # should fail

$ -> audit2allow -i /var/log/audit/audit.log -m puppetmaster # this will output the perms necessary for puppetmaster to access needed resources, copy and paste this into the file you are using in version control

$ -> checkmodule -M -m -o puppetmaster.mod /path/to/your/version/controlled/puppetmaster.te # this will create a .mod file 

$ -> semodule_package -m puppetmaster.mod -o puppetmaster.pp # this will create a compiled semodule

$ -> semodule -i puppetmaster.pp # this will install the module

At this point, you have added a custom puppetmaster selinux module which will allow you to get through the first issue discovered when trying to start the service. From here there are one of two course of action, depending on whether the service starts or not. If the service starts, you are done. If the service does not start, you will need to repeat the above steps to determine which new permissions are required to allow the service to start, rinse and repeat until your service starts.


PHP – Doctrine – Soft Delete – Multiple Rows at a Time


One of the big fallacies of working with an ORM is that behind the scenes there may be high number of queries being executed to support the application. For example, say you want to delete several messages from a user’s inbox. We create a simple form; message subject, time message was sent, status (read, unread) and a checkbox. The checkbox is used for selecting specific messages upon which we wish to conduct a specific action, let’s say delete for example. If I select ten messages at a time, for delete, and follow the normal ORM process, I would have to load each message object, then call the delete() method for each one. If you tail your query log you will notice that you just issued twenty queries; ten queries to load the message, then ten messages to delete the object. Why all this overhead when we can use MySQL’s ‘IN’ clause and issue only one delete query?

At first I added a ‘status_id’ column for each message, which would indicate if the message was in a ‘deleted’ state, otherwise known as a “soft delete”. But then I remembered that Doctrine offers this behavior via the schema config file using the ActAs: SoftDelete construct. When done reading I learned that I needed to add a ‘deleted_at’ column to my user_message table to support Doctrine’s soft delete behavior. After some thought I agreed that this would be a better approach, as opposed to using a ‘status_id’ column. Mainly because there is a precedent regarding datetime columns and Doctrine behaviors, for example created_at and updated_at; timestampable behavior.

Unfortunately, in order to take advantage of the SoftDelete behavior, I would have to issue the aforementioned twenty queries, but this time when I call the delete method on each object, it would only update the deleted_at column, vs doing a hard delete (removing it from the table permanently). So how could I take advantage of the SoftDelete behavior AND using only one query, after some tinkering I came up with the following DQL statement:

public function unlink(array $messageIds)
    $q = Doctrine_Query::create()
        ->set('deleted_at',  new Doctrine_Expression('NOW()'))
        ->whereIn('id', $messageIds)

Notice the name of the method, I prefer using link/unlink naming convention, to indicate that I may be deleting one or many rows, and the method is strategically placed within the correct class, in case you have a pivot table breaking up a M:M relationship, or on a source table with no regard to any ancillary tables.

With the above example, you now only need issue one query to affect multiple rows, and still use the MySQL server’s timezone, vs using PHP date functions which rely on the local systems timezone.


PHP – Doctrine – DQL – Select Subquery


Whilst working on a recent project I needed the ability to do a subquery look-up as part of a select statement. Basically I needed to count the number of rows, without having to do a group by on the entire result set. After a few minutes of research I noticed there was no real definitive source on how to do do a subquery as a select using DQL, although there were some references to using a subquery within a where clause.

Below is a code snippet which worked for me:

$q = Doctrine_Query::create()

// You can put this anywhere within the select clauses, but must be done before  you make call to from()        
$subQ = $q->createSubquery()
    ->from('Table3 t3')
    ->innerJoin('t3.Table4 t4')
    ->innerJoin('t4.Table5 t5')            
    ->where('t4.status_id = :statusId')
    ->andWhere('t5.id = t2.code_id');
$q->addSelect(sprintf('(%s) AS my_count', $subQ->getDql()))
    ->from('Table1 t1')
    ->innerJoin('t1.Table2 t2')
    ->where('t2.user_id = :userId', array('statusId' => $statusId, 'userId' => $userId);

// And you should now be able to access the count value using:
$result = $q->execute();


// Depending on how you have your models setup, you may have to do this instead:

Note, the table names are abstract for obvious reasons, but the snippet should demonstrate the ability to add a select subquery to a dql statement.

Here’s another example usage:

$q = Doctrine_Query::create()
$subQ = $q->createSubquery()
    ->from('UserHash uh2')
    ->where('uh2.hash = :hash');
$q->addSelect(sprintf('(%s) AS derived_user_id', $subQ->getDql()))
    ->from('UserHash uh1')            
    ->where('uh1.type_id = :typeId')
    ->having('uh1.user_id = derived_user_id', array('hash' => $hash, 'typeId' => $typeId));
return $q->execute();

Apple – Mac – Lion – Mountain Lion – User Experience Tweaks


Add new spaces

Click the “Mission Control” icon (window with 3 smaller images), from dock. Then, hover mouse in upper right hand corner, after a few seconds a mini-desktop with a “+” (plus) sign will appear, click it, and a new space will be added. You can then use “ctrl + <--” or “ctrl + –>” to traverse across the spaces you created.

Rename your computer

Click “Apple Icon” -> System Preferences -> Sharing. You can then set the name of the computer, if you want to change how it will appear on the network, click the edit button. These two values can be different, but should effectively identify your computer.

Change Password

Click “Apple Icon” -> System Preferences -> Users and Groups. You can now change the password and “short name” of users.

Set middle mouse or mouse side buttons to open mission control

Click “Apple Icon” -> System Preferences -> Mouse. Select the dropdown box in the middle with the two lines pointing to the side buttons, then select “Mission Control”, if you wish to use middle mouse button, do the same but use the dropdown menu with the line leading to the middle mouse button.

Set ability to skip words via terminal

Open Terminal -> Preferences -> Keys. Within the Shortcut Keys view, double click “opt + <--“, then select “Send Escape Sequence” from dropdown menu, then enter “b” into the textfield. Do the same for the “opt + –>“, but this time enter “f” in the textfield.

iTerm2 – Reattach a Detached Window as a Tab to Another Window


I love iTerm2, much more than the default terminal that is shipped with the Mac OS, but I could never figure out how to reattach a detached window as tab to another window. After some futzing around, I finally figured it out.

Go to iTerm -> Preferences -> Appearance. Next de-select the “Hide tab bar when there is only one tab” checkbox. Now you can drag the detached window into the tab bar of the other window, and bam, it is now a tab of the window.

Mike Purcell's RSS Feed
Go to Top