18
18
# CDDL HEADER END
19
19
20
20
#
21
- # Copyright (c) 2008, 2018 , Oracle and/or its affiliates. All rights reserved.
21
+ # Copyright (c) 2008, 2019 , Oracle and/or its affiliates. All rights reserved.
22
22
# Portions Copyright (c) 2017-2018, Chris Fraire <[email protected] >.
23
23
#
24
24
31
31
from .utils .log import get_console_logger , get_class_basename , \
32
32
fatal
33
33
from .utils .parsers import get_baseparser
34
+ from .utils .xml import insert_file , XMLProcessingException
35
+
36
+ WEB_XML = 'WEB-INF/web.xml'
37
+ DEFAULT_CONFIG_FILE = '/var/opengrok/etc/configuration.xml'
34
38
35
39
"""
36
40
deploy war file
37
41
38
42
"""
39
43
40
44
41
- def repack_war (logger , sourceWar , targetWar , configFile , defaultConfigFile ):
45
+ def repack_war (logger , source_war , target_war , default_config_file ,
46
+ config_file = None , insert_path = None ):
42
47
"""
43
- Repack sourceWar into targetWar, performing substitution of configFile
44
- in the process.
48
+ Repack source_war into target_war, performing substitution of config_file
49
+ and/or inserting XML snippet to the 'web.xml' file in the process.
50
+
51
+ :param logger: logger object
52
+ :param source_war: path to the original WAR file
53
+ :param target_war: path to the destination WAR file
54
+ :param default_config_file: path to default configuration file
55
+ :param config_file: path to new configuration file
56
+ :param insert_path: path to XML file to insert
45
57
"""
46
58
47
- WEB_XML = 'WEB-INF/web.xml'
48
-
49
- with ZipFile (sourceWar , 'r' ) as infile , ZipFile (targetWar , 'w' ) as outfile :
59
+ with ZipFile (source_war , 'r' ) as infile , \
60
+ ZipFile (target_war , 'w' ) as outfile :
50
61
for item in infile .infolist ():
51
62
data = infile .read (item .filename )
63
+
52
64
if item .filename == WEB_XML :
53
- logger .debug ("Performing substitution of '{}' with '{}'" .
54
- format (defaultConfigFile , configFile ))
55
- defaultConfigFile = defaultConfigFile .encode ()
56
- configFile = configFile .encode ()
57
- data = data .replace (defaultConfigFile , configFile )
65
+ if config_file :
66
+ logger .debug ("Performing substitution of '{}' with '{}'" .
67
+ format (default_config_file , config_file ))
68
+ default_config_file = default_config_file .encode ()
69
+ config_file = config_file .encode ()
70
+ data = data .replace (default_config_file , config_file )
71
+
72
+ if insert_path :
73
+ logger .debug ("Inserting contents of file '{}'" .
74
+ format (insert_path ))
75
+ data = insert_file (data , insert_path )
76
+
58
77
outfile .writestr (item , data )
59
78
60
79
61
- def deploy_war (logger , sourceWar , targetWar , configFile = None ):
80
+ def deploy_war (logger , source_war , target_war , config_file = None ,
81
+ insert_path = None ):
62
82
"""
63
83
Copy warSource to warTarget (checking existence of both), optionally
64
84
repacking the warTarget archive if configuration file resides in
65
85
non-default location.
66
86
"""
67
87
68
- if not os .path .isfile (sourceWar ):
69
- logger .error ("{} is not a file" .format (sourceWar ))
88
+ if not os .path .isfile (source_war ):
89
+ logger .error ("{} is not a file" .format (source_war ))
70
90
71
- if os .path .isdir (targetWar ):
72
- orig = targetWar
73
- targetWar = os .path .join (targetWar , os .path .basename (sourceWar ))
91
+ if os .path .isdir (target_war ):
92
+ orig = target_war
93
+ target_war = os .path .join (target_war , os .path .basename (source_war ))
74
94
logger .debug ("Target {} is directory, will use {}" .
75
- format (orig , targetWar ))
95
+ format (orig , target_war ))
76
96
77
97
# If user does not use default configuration file location then attempt to
78
98
# extract WEB-INF/web.xml from the war file using jar or zip utility,
79
99
# update the hardcoded values and then update source.war with the new
80
100
# WEB-INF/web.xml.
81
- tmpWar = None
82
- DEFAULT_CONFIG_FILE = '/var/opengrok/etc/configuration.xml'
83
- if configFile and configFile != DEFAULT_CONFIG_FILE :
101
+ tmp_war = None
102
+
103
+ if (config_file and config_file != DEFAULT_CONFIG_FILE ) or insert_path :
104
+
84
105
# Resolve the path to be absolute so that webapp can find the file.
85
- configFile = os .path .abspath (configFile )
106
+ if config_file :
107
+ config_file = os .path .abspath (config_file )
86
108
87
109
with tempfile .NamedTemporaryFile (prefix = 'OpenGroktmpWar' ,
88
110
suffix = '.war' ,
89
- delete = False ) as tmpWar :
111
+ delete = False ) as tmp_war :
90
112
logger .info ('Repacking {} with custom configuration path to {}' .
91
- format (sourceWar , tmpWar .name ))
92
- repack_war (logger , sourceWar , tmpWar .name , configFile ,
93
- DEFAULT_CONFIG_FILE )
94
- sourceWar = tmpWar .name
113
+ format (source_war , tmp_war .name ))
114
+ repack_war (logger , source_war , tmp_war .name , DEFAULT_CONFIG_FILE ,
115
+ config_file , insert_path )
116
+ source_war = tmp_war .name
95
117
96
- logger .info ("Installing {} to {}" .format (sourceWar , targetWar ))
97
- copyfile (sourceWar , targetWar )
118
+ logger .info ("Installing {} to {}" .format (source_war , target_war ))
119
+ copyfile (source_war , target_war )
98
120
99
- if tmpWar :
100
- os .remove (tmpWar .name )
121
+ if tmp_war :
122
+ os .remove (tmp_war .name )
101
123
102
124
103
125
def main ():
@@ -106,6 +128,9 @@ def main():
106
128
107
129
parser .add_argument ('-c' , '--config' ,
108
130
help = 'Path to OpenGrok configuration file' )
131
+ parser .add_argument ('-i' , '--insert' ,
132
+ help = 'Path to XML file to insert into the {} file' .
133
+ format (WEB_XML ))
109
134
parser .add_argument ('source_war' , nargs = 1 ,
110
135
help = 'Path to war file to deploy' )
111
136
parser .add_argument ('target_war' , nargs = 1 ,
@@ -118,7 +143,14 @@ def main():
118
143
119
144
logger = get_console_logger (get_class_basename (), args .loglevel )
120
145
121
- deploy_war (logger , args .source_war [0 ], args .target_war [0 ], args .config )
146
+ if args .insert and not os .path .isfile (args .insert ):
147
+ fatal ("File '{}' does not exist" .format (args .insert ))
148
+
149
+ try :
150
+ deploy_war (logger , args .source_war [0 ], args .target_war [0 ], args .config ,
151
+ args .insert )
152
+ except XMLProcessingException as e :
153
+ fatal (e )
122
154
123
155
print ("Start your application server (if it is not already running) "
124
156
"or wait until it loads the just installed web application.\n "
0 commit comments