Commands.pl

From Organic Design wiki
Revision as of 04:03, 19 July 2006 by Nad (talk | contribs) (make Helios backup Azzuro's file structure)

use Net::SCP::Expect;

  1. Get initial IP from local wiki log

$::IP = ; if ( wikiRawPage( $::wiki, $::wikilog ) =~ /^.+to ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/sm ) { $::IP = $1; logAdd "Last IP obtained from Network Log: $::IP"; }

sub command {

my $command = shift; $command =~ /(.+?)(\((.+)\))?$/; my ($title, $args) = ( $1, $2 ); my $article;

# pon/poff if ($title eq 'ppp') { $article = `ifconfig` } elsif ($title eq 'pon') { `pon`; $article = `tail -n 1 /var/log/syslog|grep pppd`; } elsif ($title eq 'poff') { `poff`; $article = `tail -n 3 /var/log/syslog|grep pppd`; }

# syslog elsif ($title eq 'syslog') { $article = `tail -n 50 /var/log/syslog` }

# peer log elsif ($title eq 'peerlog') { $article = qx( tail -n 50 ../$peer.log ) }

# reboot elsif ($title eq 'reboot') { $article = `shutdown -r now` }

# shutdown elsif ($title eq 'shutdown') { $article = `halt` }

# ps elsif ($title eq 'env') { $^V =~ m/(.)(.)(.)/; my $ver = ord($1).'.'.ord($2).'.'.ord($3); $cmd =~ /^(.+?)\s+(.+)/; $article = "Environment:\nOS:\t$^O\nPERL:\t$1 ($ver)\nDaemon:\t$2\n\n"; $article .= "Current instances of $::peer:\n"; $article .= qx( ps aux|grep "$::daemon\[:] $::peer" ); }

# fileSync elsif ($title eq 'fileSync') { $article = 'Manually executing fileSync()...'; spawn 'fileSync'; }

# wikiSync elsif ($title eq 'wikiSync') { $article = 'Manually executing wikiSync()...'; spawn 'wikiSync'; }

# wikiBackup elsif ($title eq 'wikiBackup') { $article = 'Manually executing wikiBackup()...'; spawn 'wikiBackup', 'od', 'wiki', lc $::peer; }

# peerBackup elsif ($title eq 'peerBackup') { $article = 'Manually executing peerBackup()...'; spawn 'peerBackup'; }

# scpBackups elsif ($title eq 'scpBackups') { $article = 'Manually executing scpBackups()...'; spawn 'scpBackups', 'od', 'gir.peerix.org' if $::peer eq 'Bender'; spawn 'scpBackups', 'gir', 'organicdesign.co.nz' if $::peer eq 'Gir'; spawn 'scpBackups', 'ma', 'organicdesign.co.nz' if $::peer eq 'Helios'; }

# swfCompile elsif ($title eq 'swfCompile') { $article = swfCompile() }

# Restart # - For some reason it fucks up if it doesn't wait for a minute # - 2005-12-09 tried closing handles and server->shutdown(SHUT_RDWR) but made no difference elsif ($title eq 'restart') { restart() }

elsif ($title eq 'stop') { logAdd "$daemon is stopping"; killChildren(); exit; }

else { logAdd $article = "Unknown command: $command" }

return $article; }

  1. Restart the peer

sub restart { logAdd "$daemon is restarting using: $::cmd"; killChildren(); exec "sleep 1; $::cmd"; }

  1. Kill all processes related to this peer

sub killChildren { for ( split /\n/, qx( ps x|grep "$::daemon\[:] $::peer" ) ) { if ( /^\s*([0-9]+).+?\d+:\d\d\s*(.+)/ and $1 != $$ ) { qx( kill $1 ); logAdd "Kill $2 ($1)"; } } logAdd "Kill $0 ($$)"; }

  1. Backup wiki LocalFS cache

sub peerBackup { my $file = '/var/www/wiki/peer.tgz'; qx( tar -czf $file ./ ); my $size = int( [ stat $file ]->[7] / 104857.6 + 0.5 ) / 10; my $comment = "Peer Backup: $file ($size\MB)"; logAdd $comment; wikiLogin( $::wiki, $::peer, $::pwd1 ); wikiPageAppend( $::wiki, $::wikilog, "\n*".localtime()." : $comment", $comment ); }

  1. Backup wiki database

sub wikiBackup { my $lpf = shift; my $db = shift; my $user = shift; my $name = "$lpf-wiki-db-".strftime( "%Y-%m-%d", localtime ); chdir '..'; mkdir $name; qx( cp -R /var/www/wiki/images $name/images ); qx( cp -R /var/www/azzuro/wiki $name/azzuro ) if $::peer eq 'Helios'; qx( mysqldump $db -u $user --password='$::pwd1' > $name/$db.sql ); qx( tar -czf $name.tgz $name ); qx( rm -R $name ); chmod 0600, "$name.tgz"; my $size = int( [ stat "$name.tgz" ]->[7] / 104857.6 + 0.5 ) / 10; my $comment = "Wiki Backup: $name.tgz ($size\MB)"; chdir 'peer'; logAdd $comment; wikiLogin( $::wiki, $::peer, $::pwd1 ); wikiPageAppend( $::wiki, $::wikilog, "\n*".localtime()." : $comment", $comment ); }

  1. Transfer the days backup over SSH using SCP

sub scpBackups { my $lpf = shift; my @domains = @_; my $name = $lpf.'-wiki-db-'.strftime( "%Y-%m-%d", localtime ).'.tgz'; my $lp = lc $::peer; wikiLogin( $::wiki, $::peer, $::pwd1 ); for my $domain ( @domains ) { logAdd "scp ../$name $lp\@$domain:/home/$lp"; my $scp = new Net::SCP::Expect( host => $domain, user => $lp, password => $::pwd1, auto_yes => 1, preserve => 1 ); $scp->scp( "../$name", "/home/$lp" ); my $comment = "$name transferred to $domain over SSH"; wikiPageAppend( $::wiki, $::wikilog, "\n*".localtime()." : $comment", $comment ); } logAdd "Exit."; }

  1. Clear sandbox

sub clearSandbox { wikiLogin( $::wiki, $::peer, $::pwd1 ); wikiPageEdit( $::wiki, $_, '

Welcome to the Organic Design wiki Sandbox! This page allows you to carry out experiments. To edit, click here or edit this page above (or the views section for obscure browsers), make your changes and click the Save page button when finished. Content will not stay permanently; this page is automatically cleaned every 24 hours by User:Bender. If too full please use: 216.73.216.169/Sandbox

Content added here will not stay permanently; this page is cleared regularly. Click here to reset the sandbox.

', "Clearing sandbox: $_" ) for @_;

}

  1. update IP with zoneedit.com/namecheap.com if changed

sub dynamicDNS { my $routerURL = shift; my $subDomain = shift; while (1) {

# Get external IP from local router my $i = qx( lynx -source -auth=admin:$::pwd2 $routerURL ); $i = ($i =~ /([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/) ? $1 : ;

if ( ($i ne ) && ($i ne $::IP) ) {

my $comment = "External IP has changed to $i";

# Zone edit update - not used currently #qx( lynx -source -auth=ADunkley:$::pwd1 http://dynamic.zoneedit.com/auth/dynamic.html?host=www.organicdesign.co.nz );

# NameCheap # - returned: n.n.n.n SETDNSHOST eng 0 0 1 10 Namecheap True True +07.00 0.1088867 true my $url = "http://dynamicdns.park-your-domain.com/update?host=$subDomain&domain=peerix.org&password=$::pwd3"; logAdd 'Namecheap.com update failed'; if ( $::client->request( HTTP::Request->new( GET => $url ) )->is_success ) { $comment .= ', updated DNS records.' } else { $comment .= ', DNS update failed (using namecheap.com)' }

$::IP = $i; # update the IP log on the wiki logAdd $comment; wikiLogin( $::wiki, $::peer, $::pwd1 ); wikiPageAppend( $::wiki, $::wikilog, "\n*".localtime()." : $comment", $comment ); } sleep 10; } }