changeset 7182:f7a7ff507c0e

RT-37384: SERVICE_HINT option is broken in OEL Summary: Deb fixes - daemon name needs to be 16 characters or less, also stop on uninstall broken. Add some test packagers for linux daemons
author shemnon
date Tue, 03 Jun 2014 18:33:38 -0600
parents d674eb219ae7
children 9d8a6ca92731
files modules/fxpackager/src/main/java/com/oracle/tools/packager/linux/LinuxDebBundler.java modules/fxpackager/src/main/resources/com/oracle/tools/packager/linux/LinuxDebBundler.properties modules/fxpackager/src/main/resources/com/oracle/tools/packager/linux/template.prerm modules/fxpackager/src/test/java/com/oracle/tools/packager/linux/LinuxDebBundlerTest.java modules/fxpackager/src/test/java/com/oracle/tools/packager/linux/LinuxRpmBundlerTest.java modules/fxpackager/src/test/java/hello/HelloService.java
diffstat 6 files changed, 172 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/modules/fxpackager/src/main/java/com/oracle/tools/packager/linux/LinuxDebBundler.java	Tue Jun 03 08:32:22 2014 -0600
+++ b/modules/fxpackager/src/main/java/com/oracle/tools/packager/linux/LinuxDebBundler.java	Tue Jun 03 18:33:38 2014 -0600
@@ -223,6 +223,13 @@
                 Log.info(I18N.getString("message.debs-like-licenses"));
             }
 
+            // for services, the app launcher must be less than 16 characters or init.d complains
+            if (p.containsKey(SERVICE_HINT.getID()) && SERVICE_HINT.fetchFrom(p) && BUNDLE_NAME.fetchFrom(p).length() > 16) {
+                throw new ConfigException(
+                        MessageFormat.format(I18N.getString("error.launcher-name-too-long"), BUNDLE_NAME.fetchFrom(p)),
+                        MessageFormat.format(I18N.getString("error.launcher-name-too-long.advice"), BUNDLE_NAME.getID()));
+            }
+
             return true;
         } catch (RuntimeException re) {
             if (re.getCause() instanceof ConfigException) {
--- a/modules/fxpackager/src/main/resources/com/oracle/tools/packager/linux/LinuxDebBundler.properties	Tue Jun 03 08:32:22 2014 -0600
+++ b/modules/fxpackager/src/main/resources/com/oracle/tools/packager/linux/LinuxDebBundler.properties	Tue Jun 03 18:33:38 2014 -0600
@@ -46,10 +46,16 @@
 
 error.parameters-null=Parameters map is null.
 error.parameters-null.advice=Pass in a non-null parameters map.
+
 error.tool-not-found=Can not find {0}.
 error.tool-not-found.advice=\  Please install required packages.
+
 error.license-missing=Specified license file is missing.
 error.license-missing.advice=Make sure that "{0}" references a file in the app resources, and that it is relative to the basedir "{1}".
+
+error.launcher-name-too-long=The bundle name "{0}" is too long for a daemon.
+error.launcher-name-too-long.advice=Set a bundler argument "{0}" to a bundle name that is shorter than 16 characters.
+
 error.cannot-create-output-dir=Output directory {0} cannot be created.
 error.cannot-write-to-output-dir=Output directory {0} is not writable.
 
--- a/modules/fxpackager/src/main/resources/com/oracle/tools/packager/linux/template.prerm	Tue Jun 03 08:32:22 2014 -0600
+++ b/modules/fxpackager/src/main/resources/com/oracle/tools/packager/linux/template.prerm	Tue Jun 03 18:33:38 2014 -0600
@@ -19,6 +19,18 @@
 
 case "$1" in
     remove|upgrade|deconfigure)
+
+        if [ "SERVICE_HINT" = "true" ]; then
+            if [ "STOP_ON_UNINSTALL" = "true" ]; then
+                echo Stopping daemon
+                if which invoke-rc.d >/dev/null 2>&1; then
+                    invoke-rc.d APPLICATION_PACKAGE stop
+                else
+                    /etc/init.d/APPLICATION_PACKAGE stop
+                fi
+            fi
+        fi
+
     ;;
 
     failed-upgrade)
--- a/modules/fxpackager/src/test/java/com/oracle/tools/packager/linux/LinuxDebBundlerTest.java	Tue Jun 03 08:32:22 2014 -0600
+++ b/modules/fxpackager/src/test/java/com/oracle/tools/packager/linux/LinuxDebBundlerTest.java	Tue Jun 03 18:33:38 2014 -0600
@@ -48,6 +48,7 @@
 import java.util.TreeMap;
 
 import static com.oracle.tools.packager.StandardBundlerParam.*;
+import static com.oracle.tools.packager.linux.LinuxAppBundler.APP_FS_NAME;
 import static com.oracle.tools.packager.linux.LinuxDebBundler.BUNDLE_NAME;
 import static com.oracle.tools.packager.linux.LinuxDebBundler.EMAIL;
 import static org.junit.Assert.*;
@@ -264,4 +265,57 @@
         assertNotNull(result);
         assertTrue(result.exists());
     }
+
+    @Test
+    public void servicePackage() throws Exception {
+        Bundler bundler = new LinuxDebBundler();
+        Collection<BundlerParamInfo<?>> parameters = bundler.getBundleParameters();
+
+        Map<String, Object> bundleParams = new HashMap<>();
+
+        bundleParams.put(SERVICE_HINT.getID(), true);
+        bundleParams.put(START_ON_INSTALL.getID(), true);
+        bundleParams.put(STOP_ON_UNINSTALL.getID(), true);
+        bundleParams.put(RUN_AT_STARTUP.getID(), true);
+
+        bundleParams.put(APP_NAME.getID(), "Java Packager Service Test #1");
+        bundleParams.put(BUNDLE_NAME.getID(), "j-p-daemon-test");
+        bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
+        bundleParams.put(MAIN_CLASS.getID(), "hello.HelloService");
+        bundleParams.put(MAIN_JAR.getID(), "mainApp.jar");
+        bundleParams.put(MAIN_JAR_CLASSPATH.getID(), "mainApp.jar");
+
+        bundleParams.put(DESCRIPTION.getID(), "Does a random heart beat every 30 seconds or so to a log file in tmp");
+        bundleParams.put(LICENSE_FILE.getID(), "LICENSE");
+        bundleParams.put(LICENSE_TYPE.getID(), "GPL v2 + CLASSPATH");
+        bundleParams.put(VENDOR.getID(), "OpenJDK");
+
+        bundleParams.put(BUILD_ROOT.getID(), tmpBase);
+        bundleParams.put(VERBOSE.getID(), true);
+
+        // assert it validates
+        boolean valid = bundler.validate(bundleParams);
+        assertTrue(valid);
+
+        File result = bundler.execute(bundleParams, new File(workDir, "service"));
+        System.err.println("Bundle at - " + result);
+        assertNotNull(result);
+        assertTrue(result.exists());
+    }
+
+    @Test(expected = ConfigException.class)
+    public void invalidServiceAppName() throws ConfigException, UnsupportedPlatformException {
+        Bundler bundler = new LinuxDebBundler();
+
+        Map<String, Object> bundleParams = new HashMap<>();
+
+        bundleParams.put(BUILD_ROOT.getID(), tmpBase);
+        bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
+
+        bundleParams.put(SERVICE_HINT.getID(), true);
+        bundleParams.put(BUNDLE_NAME.getID(), "ThisAppNameIsWayToLongForInitDToHandleGracefully");
+
+        bundler.validate(bundleParams);
+    }
+
 }
--- a/modules/fxpackager/src/test/java/com/oracle/tools/packager/linux/LinuxRpmBundlerTest.java	Tue Jun 03 08:32:22 2014 -0600
+++ b/modules/fxpackager/src/test/java/com/oracle/tools/packager/linux/LinuxRpmBundlerTest.java	Tue Jun 03 18:33:38 2014 -0600
@@ -264,4 +264,40 @@
         assertNotNull(result);
         assertTrue(result.exists());
     }
+
+    @Test
+    public void servicePackage() throws Exception {
+        Bundler bundler = new LinuxRpmBundler();
+        Collection<BundlerParamInfo<?>> parameters = bundler.getBundleParameters();
+
+        Map<String, Object> bundleParams = new HashMap<>();
+
+        bundleParams.put(SERVICE_HINT.getID(), true);
+        bundleParams.put(START_ON_INSTALL.getID(), true);
+        bundleParams.put(STOP_ON_UNINSTALL.getID(), true);
+        bundleParams.put(RUN_AT_STARTUP.getID(), true);
+
+        bundleParams.put(APP_NAME.getID(), "Java Packager Service Test #1");
+        bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
+        bundleParams.put(MAIN_CLASS.getID(), "hello.HelloService");
+        bundleParams.put(MAIN_JAR.getID(), "mainApp.jar");
+        bundleParams.put(MAIN_JAR_CLASSPATH.getID(), "mainApp.jar");
+
+        bundleParams.put(DESCRIPTION.getID(), "Does a random heart beat every 30 seconds or so to a log file in tmp");
+        bundleParams.put(LICENSE_FILE.getID(), "LICENSE");
+        bundleParams.put(LICENSE_TYPE.getID(), "GPL v2 + CLASSPATH");
+        bundleParams.put(VENDOR.getID(), "OpenJDK");
+
+        bundleParams.put(BUILD_ROOT.getID(), tmpBase);
+        bundleParams.put(VERBOSE.getID(), true);
+
+        // assert it validates
+        boolean valid = bundler.validate(bundleParams);
+        assertTrue(valid);
+
+        File result = bundler.execute(bundleParams, new File(workDir, "service"));
+        System.err.println("Bundle at - " + result);
+        assertNotNull(result);
+        assertTrue(result.exists());
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/fxpackager/src/test/java/hello/HelloService.java	Tue Jun 03 18:33:38 2014 -0600
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code 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
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package hello;
+
+import java.io.IOException;
+import java.security.SecureRandom;
+import java.time.LocalDateTime;
+import java.util.Random;
+import java.util.logging.FileHandler;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class HelloService {
+
+    public static void main(String... args) throws IOException {
+        Handler fileHandler = new FileHandler("%t/HelloService-%g.log", 1*1024*1024, 4, true);
+        Logger log = Logger.getLogger("hello");
+        log.addHandler(fileHandler);
+        log.setUseParentHandlers(false);
+        long sleepTime = 1;
+        Random random = new SecureRandom();
+        while (true) {
+            try {
+                Thread.sleep(sleepTime * 1000);
+                log.info("Slept for " + sleepTime + "s and it is now " + LocalDateTime.now());
+                sleepTime = random.nextInt(60);
+            } catch (Exception e) {
+                log.log(Level.SEVERE, "Oppsies!", e);
+            }
+        }
+    }
+}
+