/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package install; import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.net.URL; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JOptionPane; import wizard.BIRCHWizard; /** * This class contains any methods specific to updating BIRCH. ** * NOTE: the current update method for BIRCH is to uninstall the current BIRCH * installation, then install a new version of BIRCH in the same * directory. This process includes an optional backup step, in which * the entire BIRCH installation is saved to a zip file. ** * @author Graham Alvare * @author Dale Hamel * @author Brian Fristensky */ public class Update { /** * The BIRCH subdirectories to archive (and, with the exception of * 'local' and 'birch_backup', the BIRCH subdirectories to remove) */ public static final String[] birch_files = { "local-generic", "labace", "script", "install-birch", "bin-linux-intel", "bin-linux-x86_64", "bin-solaris-sparc", "bin-solaris-amd64", "bin-osx-x86_64", "dat", "admin.uninstall", "doc", "pkg", "java", "public_html", "manl", "birchconfig.homedir.target.html", "tutorials", "lib-linux-intel", "lib-linux-x86_64", "lib-solaris-sparc", "lib-solaris-amd64","lib-osx-x86_64", "admin" }; /** * Compresses/backs-up an old BIRCH distribution, specified by the * installDir parameter. ** * @param installDir the installation directory for the BIRCH update * @param logger the log file stream to use for output */ public static void compress_old_birch(File installDir, OutputConsole logger) throws IOException { // print the log header logger.println("********************Archiving old BIRCH********************"); logger.setProgressTask("Archiving old BIRCH"); // generate the target zip file Date date = new Date(); DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); /* File zfile = new File(installDir, "birch_backup-" + dateFormat.format(date) + ".zip"); FileOutputStream fos = new FileOutputStream(zfile); ZipOutputStream zos = new ZipOutputStream(fos); logger.println("Saving old BIRCH in " + zfile); */ File tfile = new File(installDir, "birch_backup-" + dateFormat.format(date) + ".tar"); PrintStream logstream = logger.getLogStream(); // back up each of the BIRCH files from the old distribution for (String each : birch_files) { File backup = new File(installDir, each); if (backup.exists()) { logger.println("Archiving old birch directory "+ each); //try { //BIRCHIO.compress(zos, backup); BIRCHIO.add_tar(tfile, backup, logstream, logstream); /*} catch (IOException ioe) { logger.println("Could not add file: \"" + each + "\" to archive:"); logger.exception(ioe); int continueUpdate = JOptionPane.showConfirmDialog(null, "An IO error occurred, the file \"" + each + "\"could not be added to the archive, do you still wish to proceed?", "Input",JOptionPane.YES_NO_OPTION); if (continueUpdate == JOptionPane.NO_OPTION) { logger.println("User aborted installation"); JOptionPane.showMessageDialog(null, "It is recommended that you archive manually archive your previous BIRCH installation before you proceed with the update."); System.exit(2); } } catch (Exception ex) { logger.exception(ex); logger.println("Unhandled exception occurred, this is a bug. For safety reasons, the update has been aborted"); JOptionPane.showMessageDialog(null, "Unhandled exception occurred, this is a bug. For safety reasons, the update has been aborted"); System.exit(3); }*/ } } //remember to close the zip file //zos.close(); } /** * Checks the version of a BIRCH installation to update */ public static boolean check_version(File birch_path, BIRCHWizard wizard) { Date timestamp; String versionStr; boolean version_ok = true; if (birch_path != null) { File install_birch = new File(birch_path, "install-birch"); File birch_version = new File(install_birch, "BIRCH.version"); File birch_tstamp = new File(install_birch, "BIRCH.version.timestamp"); try { if (birch_version.exists() && birch_version.isFile() && birch_version.canRead()) { versionStr = BIRCHIO.oneLineFile(birch_version.toURL()); if ("D".equalsIgnoreCase(versionStr)) { if (birch_tstamp.exists() && birch_tstamp.isFile() && birch_tstamp.canRead()) { timestamp = Main.TSTAMP_FMT.parse(BIRCHIO.oneLineFile(birch_tstamp.toURL())); if (Main.TIMESTAMP.before(timestamp)) { // CONFRIM THAT THE USER WISHES TO UPDATE OVER A NEWER VERSION JOptionPane.showMessageDialog(null, "Note: the version of BIRCH currently installed is newer than the most stable version.\n" + "You may only update to another development version of BIRCH!"); wizard.ADVANCED_PANEL.setDevelopmentOnly(true); } } } else if (Main.VERSION.equalsIgnoreCase(versionStr)) { // ASK IF THE USER WISHES TO REINSTALL BIRCH version_ok = JOptionPane.showConfirmDialog(null, "The most recent version of BIRCH is the same as the\n" + "one installed on your computer. Updating BIRCH to the\n" + "same version may be used to fix problems that happened\n" + "after installation. Are you sure you sure that you wish\n" + " to \"reinstall to the same version?\n\n" + "(all of your post installation preferences should be preserved)", "GetBIRCH", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION; } } } catch (Exception ex) { version_ok = JOptionPane.showConfirmDialog(null, "An error occurred while reading the version of the directory to update.\n" + "Do you wish to proceed anyways with updating BIRCH?", "GetBIRCH", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION; ex.printStackTrace(System.err); } } return version_ok; } public static boolean is_valid_birch(File installDir) { boolean ok = false; // ensure that the install directory is not null before // testing whether the essential BIRCH directories exist if (installDir != null) { File installBIRCH = new File(installDir, "install-birch"); File installScripts = new File(installDir, "install-scripts"); File localDir = new File(installDir, "local"); // confirm that the following directories exist, // and that they are indeed directories, where installDir // is represented by $BIRCH: // 1. $BIRCH ok = (installDir.exists() && installDir.isDirectory()) // 2. $BIRCH/local && (localDir.exists() && localDir.isDirectory()) // 3. $BIRCH/install-birch - OR - $BIRCH/install-scripts // && (installBIRCH.exists() || installScripts.exists()) ; // end of 'ok' condition } return ok; } /** * Uninstalls a BIRCH installation. * This method will call UNINSTALL-birch.sh on older BIRCH installations, * and UNISTALL-birch.py on newer installations. If neither file exists, * then this method will attempt to download a stand-alone copy of * UNINSTALL-birch.py. The goal is to let the user update any version of * BIRCH, so long as a local directory exists. */ public static void uninstallBIRCH(File installDir, OutputConsole logger, boolean keep_binaries) throws IOException { File installBIRCH = new File(installDir, "install-birch"); File installScripts = new File(installDir, "install-scripts"); File shellUninstall = new File(installBIRCH, "UNINSTALL-birch.sh"); File pythonUninstall = new File(installScripts, "UNINSTALL-birch.py"); PrintStream logstream = logger.getLogStream(); URL location = new URL(Main.BASE_URL + "CURRENT/UNINSTALL-birch.py"); // uninstall the old version of BIRCH if (!installScripts.exists() && installBIRCH.exists() && shellUninstall.exists()) { // handle UNINSTALL-birch.sh on older BIRCH installations. BIRCHIO.runCommand(new String[] { "/bin/sh", "-f", shellUninstall.getCanonicalPath(), "-Q", (keep_binaries ? "-n" : "") }, null, installDir, logstream, logstream); } else { // handle UNINSTALL-birch.py on new BIRCH installations - AND - // BIRCH installations without an install script. // download UNINSTALL-birch.py pythonUninstall = File.createTempFile("UNINSTALL-birch", ".py", installDir); BIRCHIO.wget(location, pythonUninstall, logger, true); pythonUninstall.deleteOnExit(); // run UNINSTALL-birch.py BIRCHIO.runCommand(new String[] { Main.PYTHON_PATH, pythonUninstall.getCanonicalPath(), "-Q", "-d=" + installDir.getCanonicalPath(), (keep_binaries ? "-n" : "") }, null, installDir, logstream, logstream); } } }