Changeset 29342 for trunk/base


Ignore:
Timestamp:
Sep 21, 2007, 6:05:40 PM (12 years ago)
Author:
jmpp@…
Message:

Proper error detection and reporting comes to the PortIndex2MySQL script, with macports1.0
ui initialization to catch the messages and direct them to wherever we want, finally!
Currently messages are logged to a runlog file and then piped to mail(1) on exit if any
failure occurs, so that the recipient is hinted at what went wrong in the db update job.

On a related note, the script was somewhat reorganized to make room for the error detection
& reporting to work throughout and with a new cleanup proc that handles housekeeping on exit.

This commit will finally let us run this script unattended off cron/launchd so that the ports.php
page is updated on an automated basis (and reliably ;-), woot!

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/base/portmgr/PortIndex2MySQL.tcl

    r29193 r29342  
    5858
    5959
    60 # Error messages reciepient.
    61 set SPAM_LOVERS macports-dev@lists.macosforge.org
    62 
    63 # Place holder proc for error catching and processing.
    64 proc bail_on_error {error_log} {
    65    
    66 }
    67 
    68 
    6960# Load macports1.0 so that we can use some of its procs and the portinfo array.
    7061catch {source \
     
    7263package require macports
    7364
    74 # Initialize MacPorts to find the sources.conf file, wherefrom we'll
    75 # get the PortIndex that'll feed the database.
    76 #more work needs to be done than just initializing and passing the
    77 #ui_options array to get mportinit to output verbose/debugging info;
    78 #I'm currently looking into this.
     65
     66# Runtime information log file and reciepient.
     67set runlog "/tmp/portsdb.log"
     68set runlog_fd [open $runlog w+]
     69set lockfile "/tmp/portsdb.lock"
     70set DATE [clock format [clock seconds] -format "%A %Y-%m-%d at %T"]
     71set subject "PortIndex2MySQL run failure on $DATE"
     72set SPAM_LOVERS macports-dev@lists.macosforge.org
     73
     74# House keeping on exit.
     75proc cleanup {} {
     76    global sqlfile sqlfile_fd
     77    global lockfile lockfile_fd
     78    close $sqlfile_fd
     79    close $lockfile_fd
     80    file delete -force $sqlfile $lockfile
     81}
     82
     83# What to do when terminating execution, depending on the $exit_status condition.
     84proc terminate {exit_status} {
     85    global runlog runlog_fd
     86    global subject SPAM_LOVERS
     87    if {$exit_status} {
     88        seek $runlog_fd 0 start
     89        exec -- mail -s $subject $SPAM_LOVERS <@ $runlog_fd
     90    }
     91    close $runlog_fd
     92    file delete -force $runlog
     93    exit $exit_status
     94}
     95
     96
     97# UI instantiation to route information/error messages wherever we want.
     98proc ui_channels {priority} {
     99    global ui_options runlog_fd
     100    switch $priority {
     101        debug {
     102            if {[macports::ui_isset ui_options ports_debug]} {
     103                return $runlog_fd
     104            } else {
     105                return {}
     106            }
     107        }
     108        info {
     109            if {[macports::ui_isset ui_options ports_verbose]} {
     110                return $runlog_fd
     111            } else {
     112                return {}
     113            }
     114        }
     115        msg {
     116            if {[macports::ui_isset ui_options ports_quiet]} {
     117                return $runlog_fd
     118            } else {
     119                return {}
     120            }
     121        }
     122        error {
     123            return $runlog_fd
     124        }
     125        default {
     126            return {}
     127        }
     128    }
     129}
     130
     131
     132# Check if there are any stray sibling jobs before moving on, bail in such case.
     133if {[file exists $lockfile]} {
     134    ui_error "PortIndex2MySQL lock file found, is another job running?"
     135    terminate 1
     136} else {
     137    set lockfile_fd [open $lockfile a]
     138}
     139
     140
     141# Initialize macports1.0 and its UI, in order to find the sources.conf file
     142# (which is what will point us to the PortIndex we're gonna use) and use
     143# the runtime information.
    79144array set ui_options {ports_verbose yes}
    80 mportinit
     145if {[catch {mportinit ui_options} errstr]} {
     146    ui_error "${::errorInfo}"
     147    ui_error "Failed to initialize MacPorts, $errstr"
     148    terminate 1
     149}
    81150
    82151# Call the selfupdate procedure to make sure the MacPorts installation
    83152# is up-to-date and with a fresh ports tree.
    84 macports::selfupdate
     153if {[catch {macports::selfupdate} errstr]} {
     154    ui_error "${::errorInfo}"
     155    ui_error "Failed to update the ports tree, $errstr"
     156    terminate 1
     157}
    85158
    86159
     
    89162    if {[catch {open $passwdfile r} passwdfile_fd]} {
    90163        ui_error "${::errorCode}: $passwdfile_fd"
    91         exit 1
     164        terminate 1
    92165    }
    93166    if {[gets $passwdfile_fd passwd] <= 0} {
     167        close $passwdfile_fd
    94168        ui_error "No password found in $passwdfile!"
    95         exit 1
     169        terminate 1
    96170    }
    97171    close $passwdfile_fd
     
    100174
    101175# Database abstraction variables:
    102 set sqlfile [file join /tmp ports.sql]
     176set sqlfile "/tmp/portsdb.sql"
    103177set dbcmd [macports::findBinary mysql5]
    104178set dbhost 127.0.0.1
    105179set dbuser macports
    106 set passwdfile [file join . password_file]
     180set passwdfile "./password_file"
    107181set dbpasswd [getpasswd $passwdfile]
    108182set dbname macports_ports
     183
    109184
    110185# Flat text file to which sql statements are written.
    111186if {[catch {open $sqlfile w+} sqlfile_fd]} {
    112187    ui_error "${::errorCode}: $sqlfile_fd"
    113     exit 1
    114 }
    115 
     188    terminate 1
     189}
    116190
    117191# SQL string escaping.
     
    148222
    149223
    150 # Load every port in the index through a search matching everything.
     224# Load every port in the index through a search that matches everything.
    151225if {[catch {set ports [mportsearch ".+"]} errstr]} {
     226    ui_error "${::errorInfo}"
    152227    ui_error "port search failed: $errstr"
    153     exit 1
     228    cleanup
     229    terminate 1
    154230}
    155231
     
    257333if {[catch {seek $sqlfile_fd 0 start} errstr]} {
    258334    ui_error "${::errorCode}: $errstr"
    259     exit 1
     335    cleanup
     336    terminate 1
    260337}
    261338if {[catch {exec -- $dbcmd --host=$dbhost --user=$dbuser --password=$dbpasswd --database=$dbname <@ $sqlfile_fd} errstr]} {
    262339    ui_error "${::errorCode}: $errstr"
    263     exit 1
    264 }
    265 
    266 
    267 # And we're done regen'ing the MacPorts dabase! (cleanup)
    268 close $sqlfile_fd
    269 file delete -force $sqlfile
     340    cleanup
     341    terminate 1
     342}
     343
     344
     345# And we're done regen'ing the MacPorts dabase! Cleanup and exit successfully.
     346cleanup
     347terminate 0
Note: See TracChangeset for help on using the changeset viewer.