Loading ActiveRecord models in a Rails app from Sinatra

# This example is available on Github at https://github.com/chip/invoicethat-admin

# For simplicity's sake, I put this into app.rb

# Root directory of my Rails app
    RAILS_ROOT = '/Users/deploy/Sites/rails_invoice'

    # Setup load_paths for ActiveSupport
    require 'active_support'
    relative_load_paths = %w(app/models)
    ::ActiveSupport::Dependencies.load_paths = relative_load_paths.map { |path|
      File.expand_path(path, RAILS_ROOT)
    }
    
    # Connect to the Rails app database
    require 'active_record'
    config_path     = File.expand_path('config/database.yml', RAILS_ROOT)
    all_envs_config = YAML.load(File.read(config_path))
    config          = all_envs_config[ENV['RACK_ENV']]
    ::ActiveRecord::Base.establish_connection(config)

Postfix aliases not working for root?

While working on a new mail server this morning, I found that root was not receiving mail, so I needed a quick fix.

Platform issues might be relevant for some admins, so let's find out how the server is configured to run Postfix...

sudo postconf -d | grep ^mail_version
mail_version = 2.3.8
cat /etc/issue
Debian GNU/Linux 4.0

Now that I have those particulars, I need to query the aliases database. But, how do you find that out? Let's look further...

sudo postconf -n | grep ^alias
alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases

Perfect. Let's move on to finding who receives root mail.

postmap -q root hash:/etc/aliases

Wha??? No results? That an easy enough fix. Just fire up your favorite editor for /etc/aliases to add an entry for root.

root: you@domain.com
mailer-daemon: postmaster
postmaster: root

Of course, don't forget that you@domain.com should be changed to suit your needs.

But, we're not done! At this point, Postfix is not aware of our new alias settings, as it reads /etc/aliases.db, not /etc/aliases, so let's fix this.

Some of the documentation I came across referred to using postmap to rebuild aliases.db, but I found that I ran into the 'record is in "key: value" format; is this an alias file?' warning. Well, I probably wasn't doing something right, so instead I tried an alternative:

sudo newaliases
sudo postfix reload

I then check my work...

sudo postmap -q root /etc/aliases

This time, I got the expected results: you@domain.com. Nice. So I watched the mail logs as I sent a test mail message:

sudo tail -f /var/log/mail.log

For me, it worked. Yay!

If your message could not be delivered, make sure you read the mail logs carefully for specific errors. Your server configuration might need to be tuned depending on your needs and the error generated, but that's for another post.

Cheers.

Importing subversion repo into git

Recently I imported a subversion repo for one of my projects into git. However, I had a non-standard directory setup since I did not follow the common /path/to/repo/project/{trunk,tags,branches} directory layout convention. Instead, I had originally setup /path/to/repo. I needed a way to specify that I didn't have a trunk directory.

Here's what I did to import that into a git repo:

git svn clone -t '' -b '' -T '' svn+ssh://host.tld/path/to/repo/
Upon running this command, I did see errors stating that I hadn't specified branches or tags properly, so it's possible that those flags are superfluous in my case, but in any event the -T '' was essential to a successful import.

In the process of finding the magic flag for importing, I found http://jo.irisson.free.fr/?p=64 to be particularly descriptive and helpful in my understanding of the import process. However, git-svnimport has been deprecated, so I used git-svn instead.

MacPorts outdated command

I recently ran into issues reinstalling apache2 on Leopard version 10.5.4 using MacPorts, and found that the "outdated" command came to my rescue!

I had never used this before, but running it as follows showed me a lot of useful information about the ports on my system:

sudo port outdated
I used the results of this command to cherry pick what I needed to upgrade. Apparently, all I needed to do in my situation was upgrade apr, but I went ahead and upgraded a number of other ports for good measure, following this procedure:
sudo port selfupdate
sudo port install db44 +darwin_8
sudo port outdated
sudo port upgrade apr bzip2 gcc_select libevent libpcap \
libxml2 memcached pcre
sudo port upgrade apache2
After all of this, I restarted apache.

I found http://www.nabble.com/Issue-installing-Apache-2-on-Leopard-td16426260.html to be especially helpful during the process, but mainly was happy to discover the "outdated" command. I hope you find this useful, too.

git installation on osx 10.4.11 - fetch failures

I recently ran into trouble installing the Macports git-core on my Powerbook G4 with OS X version 10.4.11, so I followed the instructions at http://forums.macnn.com/82/applications/368517/macports-fixing-fetch-failed-manual-download/ to get started.

Since I found that each git-core dependency failed due to fetch errors, I decided to override that behavior, as described at http://guide.macports.org/#development.examples.eliminate.

Basically, it is a simple 3 step process to follow:

  1. Attempt to install the port
  2. Manually download the source file that could not be fetched
  3. Edit the Portfile and start the process again
===============================================================

Here's exactly how I did it...

1. Attempt to install the port

Here I am attempting to install the git-core port, but it fails on rsync:

chip-castles-powerbook-g4-17:/opt/local/var/macports/sources/rsync.macports.org/release/ports/devel/popt deploy$ sudo port install git-core
Portfile changed since last build; discarding previous state.
---> Fetching rsync
---> Attempting to fetch rsync-3.0.4.tar.gz from http://rsync.samba.org/ftp/rsync/
---> Attempting to fetch rsync-3.0.4.tar.gz from http://rsync.samba.org/ftp/rsync/src/
---> Attempting to fetch rsync-3.0.4.tar.gz from http://svn.macports.org/repository/macports/distfiles/rsync
---> Attempting to fetch rsync-3.0.4.tar.gz from http://svn.macports.org/repository/macports/distfiles/general/
---> Attempting to fetch rsync-3.0.4.tar.gz from http://svn.macports.org/repository/macports/downloads/rsync
Error: Target org.macports.fetch returned: fetch failed
Error: The following dependencies failed to build: rsync
Error: Status 1 encountered during processing.


2. Manually download the source file that could not be fetched

Based on the errors above, I pointed my web browser to http://rsync.samba.org/ftp/rsync/ and downloaded rsync-3.0.4.tar.gz.

Since it downloaded to ~/Desktop, I had to copy it to the appropriate location, which in my case was under:

/opt/local/var/macports/distfiles/
More specifically, I did this:

sudo cp ~/Desktop/rsync-3.0.4.tar.gz /opt/local/var/macports/distfiles/rsync/

As a precautionary measure, it is best to run md5 against the downloaded file to be sure that it matches the checksum listed in the Portfile.

3. Edit the Portfile and start the process again

Now I need to find the Portfile. It varies for each dependency, but on my laptop I always use this as a base directory:

/opt/local/var/macports/sources/rsync.macports.org/release/ports/
However, each dependency is not always neatly under this directory, as in the case of rsync, so I had to do a little searching, as follows:

chip-castles-powerbook-g4-17:/opt/local/var/macports/sources/rsync.macports.org/release/ports deploy$ find . -name "rsync*"

./net/rsync
./net/rsync/files/rsyncd.conf.example
./net/rsync-devel
./net/rsync-lart
So I cd into net/rsync and edit the Portfile by adding a "fetch {}" directive to override this stage in the port installation process. I found that it worked successfully when I put it as the first option with braces, generally found after the initial variables are set. In this example, it is between config.cflags and pre-configure directives.

Here's my rsync Portfile:

# $Id: Portfile 39822 2008-09-06 22:37:30Z simon@macports.org $

PortSystem 1.0

name rsync
version 3.0.4
categories net
platforms darwin freebsd sunos
maintainers simon openmaintainer
description Tool that provides fast incremental file transfer.
long_description rsync is an open source utility that provides fast \
incremental file transfer. rsync is freely available \
under the GNU General Public License and is currently \
being maintained by Wayne Davison. \
\
Rsync version 3.0.4 has been released. This is a \
bug-fix release. Related pages: \
http://rsync.samba.org/ftp/rsync/rsync-${version}-NEWS

homepage http://samba.org/rsync/
master_sites http://rsync.samba.org/ftp/rsync/ \
http://rsync.samba.org/ftp/rsync/src/
checksums ${distname}${extract.suffix} \
md5 2b2d159c9bd1b5f8adfd8b13da3a1f3f \
sha1 e89ed8d84fe0b6740208ba8183ee1dbc220ba5e5 \
rmd160 5e0082c7751205e16431471bdc5f48f87ae26b1a
distname rsync-${version}

depends_lib port:popt port:libiconv

patchfiles patch-fileflags.diff \
patch-crtimes.diff
patch.pre_args -p1

configure.args --mandir=${prefix}/share/man \
--with-rsyncd-conf=${prefix}/etc/rsyncd.conf \
--enable-ipv6 \
--enable-xattr-support
configure.cflags "-Os -I${prefix}/include"

fetch {}

pre-configure {
system "cd ${worksrcpath}; ./prepare-source"
}

post-destroot {
xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}
xinstall -m 644 -W ${worksrcpath} \
COPYING INSTALL NEWS OLDNEWS TODO README doc/README-SGML \
doc/profile.txt doc/rsync.sgml \
${destroot}${prefix}/share/doc/${name}
}

platform darwin 7 {
configure.args-delete --enable-xattr-support
configure.args-append --disable-acl-support --disable-xattr-support
}

livecheck.check regex
livecheck.regex "Rsync version (\\d+(?:\\.\\d+)*) released"

variant no_macosx_metadata description {Don't preserve Mac OS X file \
flags and creation times} {
patchfiles-delete fileflags.diff \
crtimes.diff
}

variant rsyncd description {Installs rsyncd.conf and a StartupItem for rsyncd} {
post-destroot {
xinstall -m 644 ${filespath}/rsyncd.conf.example \
${destroot}${prefix}/etc/rsyncd.conf.example
reinplace "s|__PREFIX__|${prefix}|g" \
${destroot}${prefix}/etc/rsyncd.conf.example
}

post-install {
ui_msg "****************************************************************"
ui_msg "* *"
ui_msg "* To use the rsyncd server you must rename *"
ui_msg "* ${prefix}/etc/rsyncd.conf.example to rsyncd.conf and add *"
ui_msg "* your modules there. See 'man rsyncd.conf' for more *"
ui_msg "* information. *"
ui_msg "* *"
ui_msg "****************************************************************"
}

startupitem.create yes
startupitem.name rsyncd
startupitem.logfile ${prefix}/var/log/rsyncd.log
startupitem.start "${prefix}/bin/rsync --daemon --config=${prefix}/etc/rsyncd.conf"
startupitem.stop "kill `cat ${prefix}/var/run/rsyncd.pid`"
startupitem.pidfile auto ${prefix}/var/run/rsyncd.pid
}

==================================

Basically, I repeat this process for each fetch failure. For git-core, there were a good number of them. Off the top of my head: curl, expat, gettext, ncurses, openssh, perl5.10, perl5.8, pkgconfig, popt.

There were probably others, but I think you get the idea.

HTH/chip