summaryrefslogtreecommitdiffstats
path: root/base/server/upgrade/10.1.99/01-MoveWebApplicationContextFile
blob: b567e03a08870ef9b63d1b690397d3a4bc481cfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/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) 2013 Red Hat, Inc.
# All rights reserved.
#

from __future__ import absolute_import
import os

import pki.server.upgrade


class MoveWebApplicationContextFile(pki.server.upgrade.PKIServerUpgradeScriptlet):

    def __init__(self):
        super(MoveWebApplicationContextFile, self).__init__()
        self.message = 'Move web application context file'

    def upgrade_instance(self, instance):

        self.upgrade_webapp(instance, 'ROOT')
        self.upgrade_webapp(instance, 'pki')

    def upgrade_subsystem(self, instance, subsystem):

        self.upgrade_webapp(instance, subsystem.name)

    def upgrade_webapp(self, instance, webapp):

        metainf_dir = os.path.join(instance.base_dir, 'webapps', webapp, 'META-INF')
        if not os.path.exists(metainf_dir):
            # upgrade already done
            return
        self.backup(metainf_dir)

        old_context_file = os.path.join(metainf_dir, 'context.xml')
        if not os.path.exists(old_context_file):
            # upgrade already done
            return
        self.backup(old_context_file)

        catalina_dir = os.path.join(instance.base_dir, 'conf', 'Catalina')
        self.backup(catalina_dir)

        localhost_dir = os.path.join(catalina_dir, 'localhost')
        self.backup(localhost_dir)

        new_context_file = os.path.join(localhost_dir, webapp + '.xml')
        self.backup(new_context_file)

        # prepare target folder
        if not os.path.exists(localhost_dir):
            os.makedirs(localhost_dir)

        # copy context file, don't overwrite existing file
        pki.util.copyfile(old_context_file, new_context_file, overwrite=False)

        # set file and folder ownership
        pki.util.chown(catalina_dir, instance.uid, instance.gid)

        # remove old context file
        if os.path.exists(old_context_file):
            os.remove(old_context_file)

        # remove empty META-INF
        if not os.listdir(metainf_dir):
            os.rmdir(metainf_dir)