2007-07-11 01:56:25 +02:00
// ConfigUpdate_p.java
// (C) 2007 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
// first published 11.07.2007 on http://yacy.net
//
// This is a part of YaCy, a peer-to-peer based web search engine
//
2009-09-27 00:07:40 +02:00
// $LastChangedDate$
// $LastChangedRevision$
// $LastChangedBy$
2007-07-11 01:56:25 +02:00
//
// LICENSE
2011-12-04 08:22:13 +01:00
//
2007-07-11 01:56:25 +02:00
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import java.io.File ;
import java.io.IOException ;
2011-12-04 08:22:13 +01:00
import java.util.ArrayList ;
2007-07-12 18:23:33 +02:00
import java.util.Date ;
2011-03-15 02:03:35 +01:00
import java.util.NavigableSet ;
2010-11-27 01:54:59 +01:00
import java.util.Set ;
2007-07-11 01:56:25 +02:00
import java.util.TreeSet ;
2013-09-15 00:30:23 +02:00
import net.yacy.cora.document.id.DigestURL ;
2010-08-23 14:32:02 +02:00
import net.yacy.cora.protocol.RequestHeader ;
2013-07-09 14:28:25 +02:00
import net.yacy.cora.util.ConcurrentLog ;
2009-10-10 03:14:19 +02:00
import net.yacy.kelondro.util.FileUtils ;
2009-10-20 00:34:44 +02:00
import net.yacy.kelondro.util.OS ;
2011-09-25 18:59:06 +02:00
import net.yacy.peers.operation.yacyBuildProperties ;
import net.yacy.peers.operation.yacyRelease ;
import net.yacy.peers.operation.yacyVersion ;
import net.yacy.search.Switchboard ;
2012-09-21 15:48:16 +02:00
import net.yacy.server.serverObjects ;
import net.yacy.server.serverSwitch ;
2007-07-11 01:56:25 +02:00
public class ConfigUpdate_p {
2012-07-05 09:14:04 +02:00
public static serverObjects respond ( @SuppressWarnings ( " unused " ) final RequestHeader header , final serverObjects post , final serverSwitch env ) {
2007-07-11 01:56:25 +02:00
// return variable that accumulates replacements
final serverObjects prop = new serverObjects ( ) ;
2009-07-19 22:37:44 +02:00
final Switchboard sb = ( Switchboard ) env ;
2007-07-11 01:56:25 +02:00
2009-07-11 19:03:22 +02:00
// set if this should be visible
if ( yacyBuildProperties . isPkgManager ( ) ) {
prop . put ( " candeploy " , " 2 " ) ;
return prop ;
2013-07-11 21:50:06 +02:00
} else if ( OS . isWindows & & sb . appPath . toString ( ) . indexOf ( " Program Files " ) > - 1 ) {
prop . put ( " candeploy " , " 3 " ) ;
return prop ;
2009-10-20 00:34:44 +02:00
} else if ( OS . canExecUnix | | OS . isWindows ) {
2009-07-11 19:03:22 +02:00
// we can deploy a new system with (i.e.)
// cd DATA/RELEASE;tar xfz $1;cp -Rf yacy/* ../../;rm -Rf yacy
prop . put ( " candeploy " , " 1 " ) ;
} else {
prop . put ( " candeploy " , " 0 " ) ;
}
2011-12-04 08:22:13 +01:00
2009-07-11 19:03:22 +02:00
2007-10-24 23:38:19 +02:00
prop . put ( " candeploy_configCommit " , " 0 " ) ;
prop . put ( " candeploy_autoUpdate " , " 0 " ) ;
2010-10-24 23:43:01 +02:00
prop . put ( " candeploy_downloadsAvailable " , " 0 " ) ;
2011-12-04 08:22:13 +01:00
2007-07-11 01:56:25 +02:00
if ( post ! = null ) {
2010-07-10 20:03:07 +02:00
// check if update is supposed to be installed and a release is defined
2011-03-15 02:03:35 +01:00
if ( post . containsKey ( " update " ) & & ! post . get ( " releaseinstall " , " " ) . isEmpty ( ) ) {
2008-03-15 11:37:20 +01:00
prop . put ( " forwardToSteering " , " 1 " ) ;
2008-09-19 13:45:11 +02:00
prop . putHTML ( " forwardToSteering_release " , post . get ( " releaseinstall " , " " ) ) ;
2008-05-21 17:23:56 +02:00
prop . put ( " deploys " , " 1 " ) ;
prop . put ( " candeploy " , " 2 " ) ; // display nothing else
2008-03-15 11:37:20 +01:00
return prop ;
}
2011-12-04 08:22:13 +01:00
2007-07-11 01:56:25 +02:00
if ( post . containsKey ( " downloadRelease " ) ) {
// download a release
2008-08-02 14:12:04 +02:00
final String release = post . get ( " releasedownload " , " " ) ;
2011-03-15 02:03:35 +01:00
if ( ! release . isEmpty ( ) ) {
2007-07-11 01:56:25 +02:00
try {
2013-09-15 00:30:23 +02:00
yacyRelease versionToDownload = new yacyRelease ( new DigestURL ( release ) ) ;
2011-12-04 08:22:13 +01:00
2009-04-17 11:58:06 +02:00
// replace this version with version which contains public key
2011-12-04 08:22:13 +01:00
final yacyRelease . DevAndMainVersions allReleases = yacyRelease . allReleases ( false , false ) ;
2010-11-27 01:54:59 +01:00
final Set < yacyRelease > mostReleases = versionToDownload . isMainRelease ( ) ? allReleases . main : allReleases . dev ;
for ( final yacyRelease rel : mostReleases ) {
if ( rel . equals ( versionToDownload ) ) {
2009-04-17 17:54:44 +02:00
versionToDownload = rel ;
break ;
}
2009-04-17 11:58:06 +02:00
}
2009-04-17 17:54:44 +02:00
versionToDownload . downloadRelease ( ) ;
2008-08-02 14:12:04 +02:00
} catch ( final IOException e ) {
2009-04-17 17:54:44 +02:00
// TODO Auto-generated catch block
2013-07-09 14:28:25 +02:00
ConcurrentLog . logException ( e ) ;
2007-07-11 01:56:25 +02:00
}
}
}
2011-12-04 08:22:13 +01:00
2007-07-17 01:47:21 +02:00
if ( post . containsKey ( " checkRelease " ) ) {
2009-05-02 14:12:22 +02:00
yacyRelease . allReleases ( true , false ) ;
2007-07-17 01:47:21 +02:00
}
2010-11-27 01:54:59 +01:00
2008-03-15 11:37:20 +01:00
if ( post . containsKey ( " deleteRelease " ) ) {
2008-08-02 14:12:04 +02:00
final String release = post . get ( " releaseinstall " , " " ) ;
2011-03-15 02:03:35 +01:00
if ( ! release . isEmpty ( ) ) {
2008-03-15 11:37:20 +01:00
try {
2013-02-04 19:57:28 +01:00
// only delete files from RELEASE directory
if ( FileUtils . isInDirectory ( new File ( sb . releasePath , release ) , sb . releasePath ) ) {
2009-03-30 17:31:25 +02:00
FileUtils . deletedelete ( new File ( sb . releasePath , release ) ) ;
2009-05-02 14:12:22 +02:00
FileUtils . deletedelete ( new File ( sb . releasePath , release + " .sig " ) ) ;
2013-02-04 19:57:28 +01:00
} else {
2013-07-09 14:28:25 +02:00
sb . getLog ( ) . severe ( " AUTO-UPDATE: could not delete " + release + " : file not in release directory. " ) ;
2013-02-04 19:57:28 +01:00
}
2008-08-02 14:12:04 +02:00
} catch ( final NullPointerException e ) {
2013-07-09 14:28:25 +02:00
sb . getLog ( ) . severe ( " AUTO-UPDATE: could not delete release " + release + " : " + e . getMessage ( ) ) ;
2008-03-15 11:37:20 +01:00
}
}
}
2011-12-04 08:22:13 +01:00
2007-07-17 01:47:21 +02:00
if ( post . containsKey ( " autoUpdate " ) ) {
2009-05-02 14:12:22 +02:00
final yacyRelease updateVersion = yacyRelease . rulebasedUpdateInfo ( true ) ;
2007-07-17 01:47:21 +02:00
if ( updateVersion = = null ) {
2007-10-24 23:38:19 +02:00
prop . put ( " candeploy_autoUpdate " , " 2 " ) ; // no more recent release found
2008-04-08 23:36:33 +02:00
} else {
2007-07-17 01:47:21 +02:00
// there is a version that is more recent. Load it and re-start with it
2013-07-09 14:28:25 +02:00
sb . getLog ( ) . info ( " AUTO-UPDATE: downloading more recent release " + updateVersion . getUrl ( ) ) ;
2009-04-17 11:58:06 +02:00
final File downloaded = updateVersion . downloadRelease ( ) ;
2009-05-02 14:12:22 +02:00
prop . putHTML ( " candeploy_autoUpdate_downloadedRelease " , updateVersion . getName ( ) ) ;
2011-12-06 22:05:22 +01:00
final boolean devenvironment = new File ( sb . getAppPath ( ) , " .git " ) . exists ( ) ;
2007-07-17 01:47:21 +02:00
if ( devenvironment ) {
2013-07-09 14:28:25 +02:00
sb . getLog ( ) . info ( " AUTO-UPDATE: omitting update because this is a development environment " ) ;
2007-10-24 23:38:19 +02:00
prop . put ( " candeploy_autoUpdate " , " 3 " ) ;
2008-04-11 14:37:17 +02:00
} else if ( ( downloaded = = null ) | | ( ! downloaded . exists ( ) ) | | ( downloaded . length ( ) = = 0 ) ) {
2013-07-09 14:28:25 +02:00
sb . getLog ( ) . info ( " AUTO-UPDATE: omitting update because download failed (file cannot be found, is too small or signature was bad) " ) ;
2007-10-24 23:38:19 +02:00
prop . put ( " candeploy_autoUpdate " , " 4 " ) ;
2007-07-17 01:47:21 +02:00
} else {
2009-05-02 14:12:22 +02:00
yacyRelease . deployRelease ( downloaded ) ;
2010-09-14 11:13:28 +02:00
sb . terminate ( 10 , " manual release update to " + downloaded . getName ( ) ) ;
2013-07-09 14:28:25 +02:00
sb . getLog ( ) . info ( " AUTO-UPDATE: deploy and restart initiated " ) ;
2007-10-24 23:38:19 +02:00
prop . put ( " candeploy_autoUpdate " , " 1 " ) ;
2007-07-17 01:47:21 +02:00
}
}
}
2011-12-04 08:22:13 +01:00
2007-07-12 18:23:33 +02:00
if ( post . containsKey ( " configSubmit " ) ) {
2007-10-24 23:38:19 +02:00
prop . put ( " candeploy_configCommit " , " 1 " ) ;
2010-11-27 01:54:59 +01:00
sb . setConfig ( " update.process " , ( " manual " . equals ( post . get ( " updateMode " , " manual " ) ) ) ? " manual " : " auto " ) ;
2007-07-12 18:23:33 +02:00
sb . setConfig ( " update.cycle " , Math . max ( 12 , post . getLong ( " cycle " , 168 ) ) ) ;
sb . setConfig ( " update.blacklist " , post . get ( " blacklist " , " " ) ) ;
2010-11-27 01:54:59 +01:00
sb . setConfig ( " update.concept " , ( " any " . equals ( post . get ( " releaseType " , " any " ) ) ) ? " any " : " main " ) ;
2013-07-31 10:49:26 +02:00
sb . setConfig ( " update.onlySignedFiles " , ( post . getBoolean ( " onlySignedFiles " ) ) ? " 1 " : " 0 " ) ;
2007-07-12 18:23:33 +02:00
}
2007-07-11 01:56:25 +02:00
}
2011-12-04 08:22:13 +01:00
2007-07-11 01:56:25 +02:00
// version information
2009-07-11 19:03:22 +02:00
final String versionstring = yacyBuildProperties . getVersion ( ) + " / " + yacyBuildProperties . getSVNRevision ( ) ;
2008-09-19 13:45:11 +02:00
prop . putHTML ( " candeploy_versionpp " , versionstring ) ;
2011-12-06 22:05:22 +01:00
final boolean devenvironment = new File ( sb . getAppPath ( ) , " .git " ) . exists ( ) ;
2011-02-02 00:49:11 +01:00
float thisVersion = Float . parseFloat ( yacyBuildProperties . getVersion ( ) ) ;
2007-07-11 01:56:25 +02:00
// cut off the SVN Rev in the Version
2010-11-27 01:54:59 +01:00
try {
2011-02-02 00:49:11 +01:00
thisVersion = ( float ) ( Math . round ( thisVersion * 1000 . 0 ) / 1000 . 0 ) ;
2010-11-27 01:54:59 +01:00
} catch ( final NumberFormatException e ) { }
2007-07-11 01:56:25 +02:00
2011-12-04 08:22:13 +01:00
2007-07-12 18:23:33 +02:00
// list downloaded releases
2009-04-17 11:58:06 +02:00
final File [ ] downloadedFiles = sb . releasePath . listFiles ( ) ;
2011-05-14 11:23:19 +02:00
// list can be null if RELEASE directory has been deleted manually
final int downloadedFilesNum = ( downloadedFiles = = null ) ? 0 : downloadedFiles . length ;
2011-12-04 08:22:13 +01:00
2011-05-14 11:23:19 +02:00
prop . put ( " candeploy_deployenabled " , ( downloadedFilesNum = = 0 ) ? " 0 " : ( ( devenvironment ) ? " 1 " : " 2 " ) ) ; // prevent that a developer-version is over-deployed
2011-12-04 08:22:13 +01:00
2011-03-15 02:03:35 +01:00
final NavigableSet < yacyRelease > downloadedReleases = new TreeSet < yacyRelease > ( ) ;
2010-11-27 01:54:59 +01:00
for ( final File downloaded : downloadedFiles ) {
2007-07-12 18:23:33 +02:00
try {
2011-12-04 08:22:13 +01:00
final yacyRelease release = new yacyRelease ( downloaded ) ;
2009-04-17 11:58:06 +02:00
downloadedReleases . add ( release ) ;
2008-08-02 14:12:04 +02:00
} catch ( final RuntimeException e ) {
2007-07-12 18:23:33 +02:00
// not a valid release
2008-05-24 22:03:11 +02:00
// can be also a restart- or deploy-file
2009-04-17 11:58:06 +02:00
final File invalid = downloaded ;
2011-03-20 18:59:58 +01:00
if ( ! ( invalid . getName ( ) . endsWith ( " .bat " ) | | invalid . getName ( ) . endsWith ( " .sh " ) | | invalid . getName ( ) . endsWith ( " .sig " ) ) ) { // Windows & Linux don't like deleted scripts while execution!
2010-11-27 01:54:59 +01:00
invalid . deleteOnExit ( ) ;
}
2007-07-11 01:56:25 +02:00
}
2007-07-12 18:23:33 +02:00
}
2009-04-17 11:58:06 +02:00
// latest downloaded release
2011-12-04 08:22:13 +01:00
final yacyVersion dflt = ( downloadedReleases . isEmpty ( ) ) ? null : downloadedReleases . last ( ) ;
2010-10-24 23:43:01 +02:00
// check if there are any downloaded releases and if there are enable the update buttons
2010-07-25 15:19:16 +02:00
prop . put ( " candeploy_downloadsAvailable " , ( downloadedReleases . isEmpty ( ) ) ? " 0 " : " 1 " ) ;
2010-10-24 23:43:01 +02:00
prop . put ( " candeploy_deployenabled_buttonsActive " , ( downloadedReleases . isEmpty ( ) | | devenvironment ) ? " 0 " : " 1 " ) ;
2011-12-04 08:22:13 +01:00
2007-07-12 18:23:33 +02:00
int relcount = 0 ;
2010-11-27 01:54:59 +01:00
for ( final yacyRelease release : downloadedReleases ) {
2009-05-02 14:12:22 +02:00
prop . put ( " candeploy_downloadedreleases_ " + relcount + " _name " , ( ( release . isMainRelease ( ) ) ? " main " : " dev " ) + " " + release . getReleaseNr ( ) + " / " + release . getSvn ( ) ) ;
prop . put ( " candeploy_downloadedreleases_ " + relcount + " _signature " , ( release . getSignatureFile ( ) . exists ( ) ? " 1 " : " 0 " ) ) ;
prop . putHTML ( " candeploy_downloadedreleases_ " + relcount + " _file " , release . getName ( ) ) ;
2007-10-24 23:38:19 +02:00
prop . put ( " candeploy_downloadedreleases_ " + relcount + " _selected " , ( release = = dflt ) ? " 1 " : " 0 " ) ;
2007-07-12 18:23:33 +02:00
relcount + + ;
}
prop . put ( " candeploy_downloadedreleases " , relcount ) ;
2007-07-11 01:56:25 +02:00
2007-07-12 18:23:33 +02:00
// list remotely available releases
2009-05-02 14:12:22 +02:00
final yacyRelease . DevAndMainVersions releasess = yacyRelease . allReleases ( false , false ) ;
2007-07-12 18:23:33 +02:00
relcount = 0 ;
2011-12-04 08:22:13 +01:00
final ArrayList < yacyRelease > rlist = new ArrayList < yacyRelease > ( ) ;
final Set < yacyRelease > remoteDevReleases = releasess . dev ;
remoteDevReleases . removeAll ( downloadedReleases ) ;
for ( final yacyRelease release : remoteDevReleases ) {
rlist . add ( release ) ;
}
2010-11-27 01:54:59 +01:00
final Set < yacyRelease > remoteMainReleases = releasess . main ;
2009-04-17 11:58:06 +02:00
remoteMainReleases . removeAll ( downloadedReleases ) ;
2010-11-27 01:54:59 +01:00
for ( final yacyRelease release : remoteMainReleases ) {
2011-12-04 08:22:13 +01:00
rlist . add ( release ) ;
2007-07-12 18:23:33 +02:00
}
2011-12-04 08:22:13 +01:00
yacyRelease release ;
for ( int i = rlist . size ( ) - 1 ; i > = 0 ; i - - ) {
release = rlist . get ( i ) ;
2009-05-02 14:12:22 +02:00
prop . put ( " candeploy_availreleases_ " + relcount + " _name " , ( ( release . isMainRelease ( ) ) ? " main " : " dev " ) + " " + release . getReleaseNr ( ) + " / " + release . getSvn ( ) ) ;
prop . put ( " candeploy_availreleases_ " + relcount + " _url " , release . getUrl ( ) . toString ( ) ) ;
prop . put ( " candeploy_availreleases_ " + relcount + " _signatures " , ( release . getPublicKey ( ) ! = null ? " 1 " : " 0 " ) ) ;
2011-12-04 08:22:13 +01:00
prop . put ( " candeploy_availreleases_ " + relcount + " _selected " , ( relcount = = 0 ) ? " 1 " : " 0 " ) ;
2007-07-12 18:23:33 +02:00
relcount + + ;
2007-07-11 01:56:25 +02:00
}
2011-12-04 08:22:13 +01:00
2007-07-12 18:23:33 +02:00
prop . put ( " candeploy_availreleases " , relcount ) ;
// properties for automated system update
2010-11-27 01:54:59 +01:00
prop . put ( " candeploy_manualUpdateChecked " , ( " manual " . equals ( sb . getConfig ( " update.process " , " manual " ) ) ) ? " 1 " : " 0 " ) ;
prop . put ( " candeploy_autoUpdateChecked " , ( " auto " . equals ( sb . getConfig ( " update.process " , " manual " ) ) ) ? " 1 " : " 0 " ) ;
2007-07-12 18:23:33 +02:00
prop . put ( " candeploy_cycle " , sb . getConfigLong ( " update.cycle " , 168 ) ) ;
2008-09-19 13:45:11 +02:00
prop . putHTML ( " candeploy_blacklist " , sb . getConfig ( " update.blacklist " , " " ) ) ;
2010-11-27 01:54:59 +01:00
prop . put ( " candeploy_releaseTypeMainChecked " , ( " any " . equals ( sb . getConfig ( " update.concept " , " any " ) ) ) ? " 0 " : " 1 " ) ;
prop . put ( " candeploy_releaseTypeAnyChecked " , ( " any " . equals ( sb . getConfig ( " update.concept " , " any " ) ) ) ? " 1 " : " 0 " ) ;
2007-10-24 23:38:19 +02:00
prop . put ( " candeploy_lastlookup " , ( sb . getConfigLong ( " update.time.lookup " , 0 ) = = 0 ) ? " 0 " : " 1 " ) ;
2007-07-12 18:23:33 +02:00
prop . put ( " candeploy_lastlookup_time " , new Date ( sb . getConfigLong ( " update.time.lookup " , 0 ) ) . toString ( ) ) ;
2007-10-24 23:38:19 +02:00
prop . put ( " candeploy_lastdownload " , ( sb . getConfigLong ( " update.time.download " , 0 ) = = 0 ) ? " 0 " : " 1 " ) ;
2007-07-12 18:23:33 +02:00
prop . put ( " candeploy_lastdownload_time " , new Date ( sb . getConfigLong ( " update.time.download " , 0 ) ) . toString ( ) ) ;
2007-10-24 23:38:19 +02:00
prop . put ( " candeploy_lastdeploy " , ( sb . getConfigLong ( " update.time.deploy " , 0 ) = = 0 ) ? " 0 " : " 1 " ) ;
2007-07-12 18:23:33 +02:00
prop . put ( " candeploy_lastdeploy_time " , new Date ( sb . getConfigLong ( " update.time.deploy " , 0 ) ) . toString ( ) ) ;
2010-11-27 01:54:59 +01:00
prop . put ( " candeploy_onlySignedFiles " , ( " 1 " . equals ( sb . getConfig ( " update.onlySignedFiles " , " 1 " ) ) ) ? " 1 " : " 0 " ) ;
2011-12-04 08:22:13 +01:00
2007-07-11 01:56:25 +02:00
/ *
if ( ( adminaccess ) & & ( yacyVersion . latestRelease > = ( thisVersion + 0 . 01 ) ) ) { // only new Versions(not new SVN)
if ( ( yacyVersion . latestMainRelease ! = null ) | |
( yacyVersion . latestDevRelease ! = null ) ) {
prop . put ( " hintVersionDownload " , 1 ) ;
} else if ( ( post ! = null ) & & ( post . containsKey ( " aquirerelease " ) ) ) {
yacyVersion . aquireLatestReleaseInfo ( ) ;
prop . put ( " hintVersionDownload " , 1 ) ;
} else {
prop . put ( " hintVersionAvailable " , 1 ) ;
}
}
prop . put ( " hintVersionAvailable " , 1 ) ; // for testing
2011-12-04 08:22:13 +01:00
2007-07-11 01:56:25 +02:00
prop . putASIS ( " hintVersionDownload_versionResMain " , ( yacyVersion . latestMainRelease = = null ) ? " - " : yacyVersion . latestMainRelease . toAnchor ( ) ) ;
prop . putASIS ( " hintVersionDownload_versionResDev " , ( yacyVersion . latestDevRelease = = null ) ? " - " : yacyVersion . latestDevRelease . toAnchor ( ) ) ;
2011-02-02 01:06:29 +01:00
prop . put ( " hintVersionAvailable_latestVersion " , Float . toString ( yacyVersion . latestRelease ) ) ;
2007-07-11 01:56:25 +02:00
* /
2011-12-04 08:22:13 +01:00
2007-07-11 01:56:25 +02:00
return prop ;
}
}