<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Text* Snippets: jamiew</title>
    <link>http://textsnippets.com/rss</link>
    <pubDate>Fri, 21 Sep 2007 16:52:24 GMT</pubDate>
    <description>Text* Snippets: jamiew</description>
    <item>
      <title>Capistrano task to load production data</title>
      <link>http://textsnippets.com/posts/show/1215</link>
      <description>&lt;code&gt;&lt;br /&gt;# load production data, still needs some polish&lt;br /&gt;# based on code from http://push.cx/2007/capistrano-task-to-load-production-data&lt;br /&gt;# cap 2.0 compatible&lt;br /&gt;# exec is being weird, had to end w/ a syscall :\ any ideas?&lt;br /&gt;&lt;br /&gt;desc "Load production data into development database"&lt;br /&gt;task :load_production_data, :roles =&gt; :db, :only =&gt; { :primary =&gt; true } do&lt;br /&gt;  require 'yaml'&lt;br /&gt;  ['config/database.yml'].each do |file|&lt;br /&gt;&lt;br /&gt;    database = YAML::load_file(file)&lt;br /&gt;&lt;br /&gt;    filename = "dump.#{Time.now.strftime '%Y-%m-%d_%H:%M:%S'}.sql.gz"&lt;br /&gt;    # on_rollback { delete "/tmp/#{filename}" }&lt;br /&gt;&lt;br /&gt;    # run "mysqldump -u #{database['production']['username']} --password=#{database['production']['password']} #{database['production']['database']} &gt; /tmp/#{filename}" do |channel, stream, data|&lt;br /&gt;    run "mysqldump -h #{database['production']['host']} -u #{database['production']['username']} --password=#{database['production']['password']} #{database['production']['database']} | gzip &gt; /tmp/#{filename}" do |channel, stream, data|&lt;br /&gt;      puts data&lt;br /&gt;    end&lt;br /&gt;    get "/tmp/#{filename}", filename&lt;br /&gt;    # exec "/tmp/#{filename}"&lt;br /&gt;    password = database['development']['password'].nil? ? '' : "--password=#{database['development']['password']}"  # FIXME pass shows up in process list, do not use in shared hosting!!! Use a .my.cnf instead&lt;br /&gt;    # FIXME exec and run w/ localhost as host not working :\&lt;br /&gt;    # exec "mysql -u #{database['development']['username']} #{password} #{database['development']['database']} &lt; #{filename}; rm -f #{filename}"&lt;br /&gt;    `gunzip -c #{filename} | mysql -u #{database['development']['username']} #{password} #{database['development']['database']} &amp;&amp; rm -f gunzip #{filename}`&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 21 Sep 2007 16:52:24 GMT</pubDate>
      <guid>http://textsnippets.com/posts/show/1215</guid>
      <author>jamiew (Jamie Wilkinson)</author>
    </item>
    <item>
      <title>Get remote file size, following redirects (PHP)</title>
      <link>http://textsnippets.com/posts/show/1214</link>
      <description>&lt;code&gt;&lt;br /&gt;function get_remote_file_size($url, $readable = true){&lt;br /&gt;   $parsed = parse_url($url);&lt;br /&gt;   $host = $parsed["host"];&lt;br /&gt;   $fp = @fsockopen($host, 80, $errno, $errstr, 20);&lt;br /&gt;   if(!$fp) return false;&lt;br /&gt;   else {&lt;br /&gt;       @fputs($fp, "HEAD $url HTTP/1.1\r\n");&lt;br /&gt;       @fputs($fp, "HOST: $host\r\n");&lt;br /&gt;       @fputs($fp, "Connection: close\r\n\r\n");&lt;br /&gt;       $headers = "";&lt;br /&gt;       while(!@feof($fp))$headers .= @fgets ($fp, 128);&lt;br /&gt;   }&lt;br /&gt;   @fclose ($fp);&lt;br /&gt;   $return = false;&lt;br /&gt;   $arr_headers = explode("\n", $headers);&lt;br /&gt;   foreach($arr_headers as $header) {&lt;br /&gt;			// follow redirect&lt;br /&gt;			$s = 'Location: ';&lt;br /&gt;			if(substr(strtolower ($header), 0, strlen($s)) == strtolower($s)) {&lt;br /&gt;				$url = trim(substr($header, strlen($s)));&lt;br /&gt;				return get_remote_file_size($url, $readable);&lt;br /&gt;			}&lt;br /&gt;			&lt;br /&gt;			// parse for content length&lt;br /&gt;       $s = "Content-Length: ";&lt;br /&gt;       if(substr(strtolower ($header), 0, strlen($s)) == strtolower($s)) {&lt;br /&gt;           $return = trim(substr($header, strlen($s)));&lt;br /&gt;           break;&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;   if($return &amp;&amp; $readable) {&lt;br /&gt;			$size = round($return / 1024, 2);&lt;br /&gt;			$sz = "KB"; // Size In KB&lt;br /&gt;			if ($size &gt; 1024) {&lt;br /&gt;				$size = round($size / 1024, 2);&lt;br /&gt;				$sz = "MB"; // Size in MB&lt;br /&gt;			}&lt;br /&gt;			$return = "$size $sz";&lt;br /&gt;   }&lt;br /&gt;   return $return;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 20 Sep 2007 19:05:41 GMT</pubDate>
      <guid>http://textsnippets.com/posts/show/1214</guid>
      <author>jamiew (Jamie Wilkinson)</author>
    </item>
    <item>
      <title>Never render the full layout in a Rails view when called via AJAX</title>
      <link>http://textsnippets.com/posts/show/1207</link>
      <description>Essential for degradable javascript, when one never knows if a view or partial is being called via XmlHttpRequest or not. Throw the below code in your ApplicationController (application.rb)&lt;br /&gt;&lt;br /&gt;More info in &lt;a href="http://tramchase.com/blog/always-render-rails-views-without-the-full-layout-when-using-ajax-degradable-javascript"&gt;this blog post&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def render(*args)&lt;br /&gt;  	args.first[:layout] = false if request.xhr? and args.first[:layout].nil?&lt;br /&gt;	super&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 09 Sep 2007 20:08:06 GMT</pubDate>
      <guid>http://textsnippets.com/posts/show/1207</guid>
      <author>jamiew (Jamie Wilkinson)</author>
    </item>
    <item>
      <title>Standard CSS helpers</title>
      <link>http://textsnippets.com/posts/show/1194</link>
      <description>Some really common layout classes I use in almost every CSS file.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/********* helpers *********/&lt;br /&gt;.floatRight { float: right; }&lt;br /&gt;.floatLeft  { float: left; }&lt;br /&gt;.right  { text-align: right; }&lt;br /&gt;.left   { text-align: left; }&lt;br /&gt;.center { text-align: center; }&lt;br /&gt;.clear, .clearer { clear: both; }&lt;br /&gt;.block  { display: block; }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 28 Aug 2007 17:26:09 GMT</pubDate>
      <guid>http://textsnippets.com/posts/show/1194</guid>
      <author>jamiew (Jamie Wilkinson)</author>
    </item>
    <item>
      <title>AppleScript to test if application is running</title>
      <link>http://textsnippets.com/posts/show/1124</link>
      <description>By name:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;tell application "System Events" to (name of processes) contains "iTunes"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;By creator type, in case it might have a version # appended:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;tell application "System Events" to (creator type of processes) contains "InDn"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Grab creator type dynamically like so:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;tell application "Finder" to creator type of (selection as alias)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;From http://applescriptsourcebook.com/viewtopic.php?pid=46830&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 11 Jul 2007 00:24:23 GMT</pubDate>
      <guid>http://textsnippets.com/posts/show/1124</guid>
      <author>jamiew (Jamie Wilkinson)</author>
    </item>
    <item>
      <title>Cache a local copy of Google Analytics' urchin.js</title>
      <link>http://textsnippets.com/posts/show/1117</link>
      <description>Speed up your website by caching Google's urchin.js locally, and then refreshing it @nightly with this here script. Some background: http://tramchase.com/blog/broken-urchintracker-calls-google-analytics-down&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/bin/sh&lt;br /&gt;# urchin.js local caching by Jamie Wilkinson &lt;http://tramchase.com&gt;&lt;br /&gt;&lt;br /&gt;# config&lt;br /&gt;DIR=/directory/where/you/want/js&lt;br /&gt;&lt;br /&gt;# work&lt;br /&gt;URCHIN=$DIR/urchin.js&lt;br /&gt;wget http://www.google-analytics.com/urchin.js -O $URCHIN.tmp&lt;br /&gt;if [ -s $URCHIN.tmp ]; then&lt;br /&gt;        rm $URCHIN&lt;br /&gt;        mv $URCHIN.tmp $URCHIN&lt;br /&gt;        chmod 644 $URCHIN&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;exit 0;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 18:31:41 GMT</pubDate>
      <guid>http://textsnippets.com/posts/show/1117</guid>
      <author>jamiew (Jamie Wilkinson)</author>
    </item>
    <item>
      <title>Handling filenames with spaces in a bash for or while loop</title>
      <link>http://textsnippets.com/posts/show/1105</link>
      <description>// The `find` here gets every directory in my home dir, one level deep, but some of them have spaces... the default 'for i in $(find ...)' behavior treats each individual word boundary as a separate variable, but piping to `read` breaks them on newlines&lt;br /&gt;// Note: OS X `find` does not support "-depth 1" (its -depth opt is for depth-first searching vs. breadth-first), just use an 'ls' with an 'if [ -d $filename]' to test for directories&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;find ~ -type d -depth 1 | while read filename; do&lt;br /&gt;# ls ~ | while read filename; do&lt;br /&gt;	echo $filename&lt;br /&gt;	# other stuff&lt;br /&gt;done&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 26 Jun 2007 16:07:29 GMT</pubDate>
      <guid>http://textsnippets.com/posts/show/1105</guid>
      <author>jamiew (Jamie Wilkinson)</author>
    </item>
    <item>
      <title>MediaWiki LDAP configuration</title>
      <link>http://textsnippets.com/posts/show/1085</link>
      <description>Requires the LdapAuthentication extension. Regarding precise LDAP queries YMMV.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# LDAP authentication&lt;br /&gt;require_once( 'extensions/LdapAuthentication/LdapAuthentication.php' );&lt;br /&gt;$wgAuth = new LdapAuthenticationPlugin();&lt;br /&gt;&lt;br /&gt;$wgLDAPDomainNames = array( "domain-ldap" );&lt;br /&gt;$wgLDAPServerNames = array( "domain-ldap" =&gt; "ldap.domain.org"  );&lt;br /&gt;$wgLDAPEncryptionType = array( "domain-ldap" =&gt; "clear" );   // "testADdomain"=&gt;"tls",&lt;br /&gt;$wgLDAPSearchStrings = array( "domain-ldap"=&gt;"uid=USER-NAME,ou=People,dc=domain,dc=org"  );&lt;br /&gt;&lt;br /&gt;$wgLDAPWriterDN                         = array( "domain-ldap" =&gt; "cn=Manager,dc=domain,dc=org" ); // for changing passwords; AddLDAPUsers below must be true&lt;br /&gt;$wgLDAPWriterPassword = array( "domain-ldap" =&gt; "MANAGERPASSWORD" ); // FIXME&lt;br /&gt;$wgLDAPWriteLocation    = array( "domain-ldap" =&gt; "ou=People,dc=domain,dc=org" );&lt;br /&gt;&lt;br /&gt;$wgLDAPAddLDAPUsers = array( "domain-ldap" =&gt; true ); // allow adding users to LDAP from mediawiki? require WriterDN/Password&lt;br /&gt;$wgLDAPUpdateLDAP = array( "domain-ldap" =&gt; true );    // for updating passwords;&lt;br /&gt;$wgLDAPUseLocal = array( "domain-ldap" =&gt; true ); // failover to local DB?&lt;br /&gt;$wgLDAPMailPassword = array( "domain-ldap" =&gt; true ); // if can't write to LDAP this is basically useless&lt;br /&gt;$wgLDAPRetrievePrefs = array( "domain-ldap" =&gt; true );&lt;br /&gt;$wgMinimalPasswordLength = 1;&lt;br /&gt;&lt;br /&gt;// Don't automatically create an account for a user if the account exists in LDAP but not in MediaWiki. Default is false.&lt;br /&gt;// this totally breaks things, do not turn it on&lt;br /&gt;$wgLDAPDisableAutoCreate = array( "domain-ldap" =&gt; false );&lt;br /&gt;&lt;br /&gt;$wgLDAPDebug = 1; // 3 is a lot&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 12 Jun 2007 16:42:25 GMT</pubDate>
      <guid>http://textsnippets.com/posts/show/1085</guid>
      <author>jamiew (Jamie Wilkinson)</author>
    </item>
    <item>
      <title>vi find/replaces for adding a table prefix to a SQL dump</title>
      <link>http://textsnippets.com/posts/show/1082</link>
      <description>Was importing a DB but wanted to prefix the tables, but the file was too large for TextMate. Here's all the vim substitutions&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;%s/DROP TABLE IF EXISTS `/DROP TABLE IF EXISTS `prefix_/g&lt;br /&gt;%s/CREATE TABLE `/CREATE TABLE `prefix_/g&lt;br /&gt;%s/INSERT INTO `/INSERT INTO `prefix_/g&lt;br /&gt;%s/LOCK TABLES `/LOCK TABLES `prefix_/g&lt;br /&gt;%s/ALTER TABLE `/ALTER TABLE `prefix_/g&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 11 Jun 2007 22:00:37 GMT</pubDate>
      <guid>http://textsnippets.com/posts/show/1082</guid>
      <author>jamiew (Jamie Wilkinson)</author>
    </item>
    <item>
      <title>mongrel_cluster monit script</title>
      <link>http://textsnippets.com/posts/show/1081</link>
      <description>&lt;br /&gt;Copyright (C) 2006 Peter J Jones (pjones@pmade.com).&lt;br /&gt;&lt;br /&gt;################################################################################&lt;br /&gt;# This script will update your monit configuration file to monitor your&lt;br /&gt;# mongrel cluster by reading the mongrel cluster file and generating monit&lt;br /&gt;# check entries.  It places delimiters in the configuration file so that you&lt;br /&gt;# can run this script multiple times without generating duplicate entries.&lt;br /&gt;#&lt;br /&gt;# Run it like this:&lt;br /&gt;#&lt;br /&gt;#  mongrel_monit --mongrel=/path/to/mongrel/config.yml --monit=/path/to/monitrc&lt;br /&gt;#&lt;br /&gt;# You can edit the ERB template below to control the generated monit config.&lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;TEMPLATE = &lt;&lt;EOT&lt;br /&gt;check process mongrel_&lt;%= @port %&gt; with pidfile &lt;%= @pidfile %&gt;&lt;br /&gt;  group mongrel&lt;br /&gt;  start program = "/usr/local/bin/&lt;%= @start %&gt;"&lt;br /&gt;  stop program = "/usr/local/bin/&lt;%= @stop %&gt;"&lt;br /&gt;&lt;br /&gt;  if failed host 127.0.0.1 port &lt;%= @port %&gt; protocol http&lt;br /&gt;    with timeout 10 seconds&lt;br /&gt;    then restart&lt;br /&gt;&lt;br /&gt;  if totalmem &gt; 100 Mb then restart&lt;br /&gt;  if cpu is greater than 60% for 2 cycles then alert&lt;br /&gt;  #if cpu &gt; 90% for 5 cycles then restart&lt;br /&gt;  #if loadavg(5min) greater than 10 for 8 cycles then restart&lt;br /&gt;  if 3 restarts within 5 cycles then timeout&lt;br /&gt;&lt;br /&gt;EOT&lt;br /&gt;################################################################################&lt;br /&gt;require 'erb'&lt;br /&gt;require 'optparse'&lt;br /&gt;require 'fileutils'&lt;br /&gt;require 'rubygems'&lt;br /&gt;require 'mongrel_cluster/init'&lt;br /&gt;################################################################################&lt;br /&gt;module Kernel&lt;br /&gt;  $commands = []&lt;br /&gt;&lt;br /&gt;  # Hack Kernel::` so that we can capture what mongrel_cluster does&lt;br /&gt;  def ` (cmd)&lt;br /&gt;    $commands &lt;&lt; cmd&lt;br /&gt;    system(':') # to set $?&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  # Hack Kernel::puts to shut mongrel_cluster up&lt;br /&gt;  def puts (str)&lt;br /&gt;    true&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;################################################################################&lt;br /&gt;options = OptionParser.new&lt;br /&gt;&lt;br /&gt;options.on('--mongrel=FILE', 'Mongrel configuration file') do |o|&lt;br /&gt;  $mongrel_config = o&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;options.on('--monit=FILE', 'Monit configuration file') do |o|&lt;br /&gt;  $monit_config = o&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;options.parse!&lt;br /&gt;&lt;br /&gt;unless $mongrel_config and $monit_config&lt;br /&gt;  puts "please give --mongrel and --monit"&lt;br /&gt;  exit 1&lt;br /&gt;end&lt;br /&gt;################################################################################&lt;br /&gt;mongrel_config_data = YAML.load_file($mongrel_config)&lt;br /&gt;Dir.chdir(mongrel_config_data['cwd']) if mongrel_config_data['cwd']&lt;br /&gt;################################################################################&lt;br /&gt;# This is the only way to access the cmd that mongrel_cluster uses so that we&lt;br /&gt;# can keep things DRY&lt;br /&gt;start = Cluster::Start.new&lt;br /&gt;start.instance_variable_set(:@config_file, $mongrel_config)&lt;br /&gt;start.run&lt;br /&gt;start_commands = $commands.dup&lt;br /&gt;$commands.clear&lt;br /&gt;&lt;br /&gt;stop = Cluster::Stop.new&lt;br /&gt;stop.instance_variable_set(:@config_file, $mongrel_config)&lt;br /&gt;stop.run&lt;br /&gt;stop_commands = $commands.dup&lt;br /&gt;$commands.clear&lt;br /&gt;################################################################################&lt;br /&gt;template = ERB.new(TEMPLATE)&lt;br /&gt;config = ''&lt;br /&gt;&lt;br /&gt;0.upto(start_commands.length - 1) do |i|&lt;br /&gt;  @port = start_commands[i].match(/-p\s*(\d+)/)[1]&lt;br /&gt;  @pidfile = File.expand_path(start_commands[i].match(/-P\s*(\S+)/)[1])&lt;br /&gt;  @start = start_commands[i]&lt;br /&gt;  @stop = stop_commands[i]&lt;br /&gt;  config &lt;&lt; template.result(binding)&lt;br /&gt;end&lt;br /&gt;################################################################################&lt;br /&gt;monit_config_data = File.open($monit_config) {|f| f.read}&lt;br /&gt;&lt;br /&gt;# TODO: if you have more than one cluster, this won't do&lt;br /&gt;banner_start = "#= Mongrel_Monit Config Start =#\n"&lt;br /&gt;banner_stop  = "#= Mongrel_Monit Config End =#\n"&lt;br /&gt;&lt;br /&gt;if monit_config_data.match(/#{banner_start}.*#{banner_stop}/m)&lt;br /&gt;  monit_config_data.sub!(/#{banner_start}.*#{banner_stop}/m, "#{banner_start}#{config}#{banner_stop}")&lt;br /&gt;else&lt;br /&gt;  monit_config_data &lt;&lt; "#{banner_start}#{config}#{banner_stop}"&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;File.open($monit_config, 'w') {|f| f &lt;&lt; monit_config_data}&lt;br /&gt;################################################################################&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 11 Jun 2007 19:43:23 GMT</pubDate>
      <guid>http://textsnippets.com/posts/show/1081</guid>
      <author>jamiew (Jamie Wilkinson)</author>
    </item>
  </channel>
</rss>
