From 14f899daa2aafc8153499ff4e09063dea5b2eead Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Thu, 16 Jun 2016 19:07:40 -0500
Subject: [PATCH] Added upgrade script to fix JAVA_HOME.

https://fedorahosted.org/pki/ticket/2363
---
 base/common/upgrade/10.3.1/.gitignore     |  4 ++
 base/common/upgrade/10.3.2/.gitignore     |  4 ++
 base/common/upgrade/10.3.3/.gitignore     |  4 ++
 base/server/upgrade/10.3.1/.gitignore     |  4 ++
 base/server/upgrade/10.3.2/.gitignore     |  4 ++
 base/server/upgrade/10.3.3/01-FixJavaHome | 99 +++++++++++++++++++++++++++++++
 6 files changed, 119 insertions(+)
 create mode 100644 base/common/upgrade/10.3.1/.gitignore
 create mode 100644 base/common/upgrade/10.3.2/.gitignore
 create mode 100644 base/common/upgrade/10.3.3/.gitignore
 create mode 100644 base/server/upgrade/10.3.1/.gitignore
 create mode 100644 base/server/upgrade/10.3.2/.gitignore
 create mode 100644 base/server/upgrade/10.3.3/01-FixJavaHome

diff --git a/base/common/upgrade/10.3.1/.gitignore b/base/common/upgrade/10.3.1/.gitignore
new file mode 100644
index 0000000000000000000000000000000000000000..5e7d2734cfc60289debf74293817c0a8f572ff32
--- /dev/null
+++ b/base/common/upgrade/10.3.1/.gitignore
@@ -0,0 +1,4 @@
+# Ignore everything in this directory
+*
+# Except this file
+!.gitignore
diff --git a/base/common/upgrade/10.3.2/.gitignore b/base/common/upgrade/10.3.2/.gitignore
new file mode 100644
index 0000000000000000000000000000000000000000..5e7d2734cfc60289debf74293817c0a8f572ff32
--- /dev/null
+++ b/base/common/upgrade/10.3.2/.gitignore
@@ -0,0 +1,4 @@
+# Ignore everything in this directory
+*
+# Except this file
+!.gitignore
diff --git a/base/common/upgrade/10.3.3/.gitignore b/base/common/upgrade/10.3.3/.gitignore
new file mode 100644
index 0000000000000000000000000000000000000000..5e7d2734cfc60289debf74293817c0a8f572ff32
--- /dev/null
+++ b/base/common/upgrade/10.3.3/.gitignore
@@ -0,0 +1,4 @@
+# Ignore everything in this directory
+*
+# Except this file
+!.gitignore
diff --git a/base/server/upgrade/10.3.1/.gitignore b/base/server/upgrade/10.3.1/.gitignore
new file mode 100644
index 0000000000000000000000000000000000000000..5e7d2734cfc60289debf74293817c0a8f572ff32
--- /dev/null
+++ b/base/server/upgrade/10.3.1/.gitignore
@@ -0,0 +1,4 @@
+# Ignore everything in this directory
+*
+# Except this file
+!.gitignore
diff --git a/base/server/upgrade/10.3.2/.gitignore b/base/server/upgrade/10.3.2/.gitignore
new file mode 100644
index 0000000000000000000000000000000000000000..5e7d2734cfc60289debf74293817c0a8f572ff32
--- /dev/null
+++ b/base/server/upgrade/10.3.2/.gitignore
@@ -0,0 +1,4 @@
+# Ignore everything in this directory
+*
+# Except this file
+!.gitignore
diff --git a/base/server/upgrade/10.3.3/01-FixJavaHome b/base/server/upgrade/10.3.3/01-FixJavaHome
new file mode 100644
index 0000000000000000000000000000000000000000..23a5aadffb00389a546d8d54eb873fb0fab60567
--- /dev/null
+++ b/base/server/upgrade/10.3.3/01-FixJavaHome
@@ -0,0 +1,99 @@
+#!/usr/bin/python
+# Authors:
+#     Endi S. Dewata <edewata@redhat.com>
+#
+# 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; version 2 of the License.
+#
+# 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.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright (C) 2016 Red Hat, Inc.
+# All rights reserved.
+
+from __future__ import absolute_import
+import os.path
+import subprocess
+import sys
+import pki.server.upgrade
+
+
+class FixJavaHome(pki.server.upgrade.PKIServerUpgradeScriptlet):
+
+    def __init__(self):
+        super(FixJavaHome, self).__init__()
+        self.message = 'Fix JAVA_HOME path'
+
+        # read Java home from pki.conf
+        value = subprocess.check_output(
+            '. /usr/share/pki/etc/pki.conf && . /etc/pki/pki.conf && echo $JAVA_HOME',
+            shell=True)
+        self.java_home = value.decode(sys.getfilesystemencoding()).strip()
+
+    def upgrade_instance(self, instance):
+
+        systemd_conf = os.path.join('/etc/sysconfig', instance.name)
+        self.fix_tomcat_conf(systemd_conf)
+
+        tomcat_conf = os.path.join(instance.conf_dir, 'tomcat.conf')
+        self.fix_tomcat_conf(tomcat_conf)
+
+    def fix_tomcat_conf(self, filename):
+
+        self.backup(filename)
+
+        with open(filename, 'r') as f:
+            lines = f.readlines()
+
+        with open(filename, 'w') as f:
+
+            # find 'System-wide configuration ...' comment
+            start = self.find_blank_line(lines, 0) + 1
+
+            # if found, replace with 'This file ...' comment
+            if lines[start].startswith('# System-wide configuration'):
+
+                # find the end of old comment
+                end = self.find_blank_line(lines, start)
+
+                # replace comment
+                lines = lines[:start] + \
+                    ['# This file contains instance-specific configuration.\n'] + \
+                    lines[end:]
+
+            # set JAVA_HOME
+            for line in lines:
+
+                if line.startswith('#JAVA_HOME='):
+                    line = 'JAVA_HOME="%s"\n' % self.java_home
+
+                elif line.startswith('#CATALINA_HOME='):
+                    line = '\n'
+
+                elif line.startswith('#JASPER_HOME='):
+                    line = '# Instance tmp dir\n'
+
+                f.write(line)
+
+    def find_blank_line(self, lines, start):
+
+        i = start
+
+        # check all lines
+        while i < len(lines):
+
+            if not lines[i].strip():
+                # blank line found, return index
+                return i
+
+            i = i + 1
+
+        # blank line not found, return -1
+        return -1
-- 
2.4.11

