summaryrefslogtreecommitdiffstats
path: root/base/server/upgrade/10.2.6/02-AddPhoneHomeURLsToTPSsServerXML
blob: d24ebe4f15e7c99eb54b2c01887ff6c5314abffa (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python
# Authors:
# Matthew Harmsen <mharmsen@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) 2015 Red Hat, Inc.
# All rights reserved.
#

from __future__ import absolute_import
import os

import pki.server.upgrade


class AddPhoneHomeURLsToTPSsServerXML(
    pki.server.upgrade.PKIServerUpgradeScriptlet):
    def __init__(self):
        super(AddPhoneHomeURLsToTPSsServerXML, self).__init__()
        self.message = 'Add Phone Home URLs to TPS section of server.xml.'

    def upgrade_instance(self, instance):
        server_xml = os.path.join(instance.conf_dir, 'server.xml')
        # Backup
        self.backup(server_xml)

        # Simply read in the document by lines

        with open(server_xml) as f:
            content = f.readlines()
            f.close()

        tps_statuses_pattern = "<!-- TPS Status Definitions -->"
        tps_end_statuses_pattern = "-->"
        tps_unsecure_phone_home_pattern = "Unsecure PHONE HOME"
        tps_secure_phone_home_pattern = "Secure PHONE HOME"
        tps_secure_url_pattern = "Secure URL"
        tps_unsecure_url_pattern = "Unsecure URL"
        tps_phone_home_path = "phoneHome"

        tps_secure_url = None
        tps_unsecure_url = None

        found_tps_statuses = -1
        # loop through file, looking for TPS settings

        rewrite_server_xml = False
        final_content = []
        for index, line in enumerate(content):

            if found_tps_statuses == -1:
                found_tps_statuses = line.find(tps_statuses_pattern)
            else:
                if line.find(tps_unsecure_phone_home_pattern) != -1:
                    # already upgraded, abort
                    break
                if line.find(tps_secure_phone_home_pattern) != -1:
                    # already upgraded, abort
                    break

                if line.find(tps_unsecure_url_pattern) != -1:
                    splits = line.split("=")
                    if len(splits) == 2:
                        tps_unsecure_url = splits[1].strip()

                if line.find(tps_secure_url_pattern) != -1:
                    splits = line.split("=")
                    if len(splits) == 2:
                        tps_secure_url = splits[1].strip()

                if line.find(tps_end_statuses_pattern) != -1:
                    if tps_unsecure_url and tps_secure_url:
                        # Create the added lines we need
                        # Phone home url is simply a super set of the base url
                        unsec_phone_home_url = tps_unsecure_phone_home_pattern + \
                            ' = ' + tps_unsecure_url + \
                            '/' + tps_phone_home_path + '\n'
                        sec_phone_home_url = tps_secure_phone_home_pattern + \
                            '   = ' + tps_secure_url + \
                            '/' + tps_phone_home_path + '\n'
                        # Spot to add the URLs
                        final_content.append(unsec_phone_home_url)
                        final_content.append(sec_phone_home_url)
                        # Just write the rest of the original to the copy
                        final_content.extend(content[index:])
                        # Indicate that we want to update the server.xml
                        rewrite_server_xml = True
                        # Done
                        break
                    else:
                        # Just give up
                        break

            final_content.append(line)

        # Rewrite the file if needed
        if rewrite_server_xml:
            with open(server_xml, 'w') as fout:
                for line_out in final_content:
                    fout.write(line_out)
            fout.close()