Skip to content

Commit f0c2c94

Browse files
rakillenrobertpatrick
authored andcommitted
Refactor common deployer archive extract methods
1 parent e7b980a commit f0c2c94

File tree

4 files changed

+152
-275
lines changed

4 files changed

+152
-275
lines changed

core/src/main/python/wlsdeploy/tool/deploy/applications_deployer.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55
import os
66
import re
7+
import shutil
78

89
from java.io import File
910
from java.io import FileOutputStream
@@ -16,6 +17,7 @@
1617
from oracle.weblogic.deploy.util import WLSDeployArchive
1718

1819
from wlsdeploy.aliases.location_context import LocationContext
20+
from wlsdeploy.aliases.model_constants import APPLICATION
1921
from wlsdeploy.aliases.model_constants import PARTITION
2022
from wlsdeploy.aliases.model_constants import PLAN_DIR
2123
from wlsdeploy.aliases.model_constants import PLAN_PATH
@@ -309,6 +311,89 @@ def _substitute_appmodule_token(self, path, module_type):
309311
newfh.write(newtext)
310312
newfh.close()
311313

314+
def _extract_deployment_from_archive(self, deployment_name, deployment_type, deployment_dict):
315+
_method_name = '_extract_deployment_from_archive'
316+
self.logger.entering(deployment_name, deployment_type, deployment_dict,
317+
class_name=self._class_name, method_name=_method_name)
318+
319+
source_path = dictionary_utils.get_element(deployment_dict, SOURCE_PATH)
320+
combined_path_path = self._get_combined_model_plan_path(deployment_dict)
321+
if deployer_utils.is_path_into_archive(source_path) or deployer_utils.is_path_into_archive(combined_path_path):
322+
if self.archive_helper is None:
323+
ex = exception_helper.create_deploy_exception(
324+
'WLSDPLY-09303', deployment_type, deployment_name)
325+
self.logger.throwing(ex, class_name=self._class_name, method_name=_method_name)
326+
raise ex
327+
328+
is_structured_app = False
329+
structured_app_dir = None
330+
if deployment_type == APPLICATION:
331+
is_structured_app, structured_app_dir = self._is_structured_app(deployment_dict)
332+
333+
if is_structured_app:
334+
# is_structured_app() only returns true if both the app and the plan have similar paths.
335+
# Since the caller already verified the app or plan was in the archive, it is safe to assume
336+
# both are in the archive.
337+
if self.archive_helper.contains_path(structured_app_dir):
338+
self._extract_directory_from_archive(structured_app_dir, deployment_name, deployment_type)
339+
else:
340+
model_source_path = dictionary_utils.get_element(deployment_dict, SOURCE_PATH)
341+
plan_file_to_extract = self._get_combined_model_plan_path(deployment_dict)
342+
343+
if not string_utils.is_empty(model_source_path) and \
344+
(model_source_path.endswith('/') or model_source_path.endswith('\\')):
345+
# model may have trailing slash on exploded source path
346+
source_path_to_extract = model_source_path[:-1]
347+
else:
348+
source_path_to_extract = model_source_path
349+
350+
# The caller only verified that either the app or the plan was in the archive; therefore,
351+
# we have to validate each one before trying to extract.
352+
if self.archive_helper.is_path_into_archive(source_path_to_extract):
353+
# source path may be a single file (jar, war, etc.) or an exploded directory
354+
if self.archive_helper.contains_file(source_path_to_extract):
355+
self._extract_file_from_archive(source_path_to_extract, deployment_name, deployment_type)
356+
elif self.archive_helper.contains_path(source_path_to_extract):
357+
self._extract_directory_from_archive(source_path_to_extract, deployment_name, deployment_type)
358+
else:
359+
ex = exception_helper.create_deploy_exception(
360+
'WLSDPLY-09330', deployment_type, deployment_name, source_path_to_extract)
361+
self.logger.throwing(ex, class_name=self._class_name, method_name=_method_name)
362+
raise ex
363+
364+
if self.archive_helper.is_path_into_archive(plan_file_to_extract):
365+
self._extract_file_from_archive(plan_file_to_extract, deployment_name, deployment_type)
366+
367+
self.logger.exiting(class_name=self._class_name, method_name=_method_name)
368+
369+
def _extract_directory_from_archive(self, directory_path, _deployment_name, _deployment_type):
370+
"""
371+
Extract the specified directory path from the archive.
372+
The default behavior is to extract to the local file system.
373+
Subclasses can this method for other cases (ssh and remote).
374+
:param directory_path: the path to extract
375+
:param _deployment_name: the deployment name (myApp), can be used for logging
376+
:param _deployment_type: the deployment type (Application, etc.), can be used for logging
377+
"""
378+
# When extracting a directory, delete the old directory if it already exists so that the
379+
# extracted directory is exactly what is in the archive file.
380+
existing_directory_path = \
381+
self.path_helper.local_join(self.model_context.get_domain_home(), directory_path)
382+
if os.path.isdir(existing_directory_path):
383+
shutil.rmtree(existing_directory_path)
384+
self.archive_helper.extract_directory(directory_path)
385+
386+
def _extract_file_from_archive(self, file_path, _deployment_name, _deployment_type):
387+
"""
388+
Extract the specified file path from the archive.
389+
The default behavior is to extract to the local file system.
390+
Subclasses can this method for other cases (ssh and remote).
391+
:param file_path: the path to extract
392+
:param _deployment_name: the deployment name (myApp), can be used for logging
393+
:param _deployment_type: the deployment type (Application, etc.), can be used for logging
394+
"""
395+
self.archive_helper.extract_file(file_path)
396+
312397
###########################################################################
313398
# Private utility methods #
314399
###########################################################################

core/src/main/python/wlsdeploy/tool/deploy/applications_offline_deployer.py

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
44
"""
55
import copy
6-
import os
7-
import shutil
86

97
from wlsdeploy.aliases.location_context import LocationContext
108
from wlsdeploy.aliases.model_constants import APPLICATION
@@ -157,97 +155,6 @@ def __delete_existing_deployment(self, existing_deployment_names_list, deploymen
157155

158156
self.logger.exiting(class_name=self._class_name, method_name=_method_name)
159157

160-
# FIXME: duplicate code in online and offline
161-
def _extract_deployment_from_archive(self, deployment_name, deployment_type, deployment_dict):
162-
_method_name = '_extract_deployment_from_archive'
163-
self.logger.entering(deployment_name, deployment_type, deployment_dict,
164-
class_name=self._class_name, method_name=_method_name)
165-
166-
source_path = dictionary_utils.get_element(deployment_dict, SOURCE_PATH)
167-
combined_path_path = self._get_combined_model_plan_path(deployment_dict)
168-
if deployer_utils.is_path_into_archive(source_path) or deployer_utils.is_path_into_archive(combined_path_path):
169-
if self.archive_helper is not None:
170-
self._extract_app_or_lib_from_archive(deployment_name, deployment_type, deployment_dict)
171-
else:
172-
ex = exception_helper.create_deploy_exception('WLSDPLY-09303', deployment_type, deployment_name)
173-
self.logger.throwing(ex, class_name=self._class_name, method_name=_method_name)
174-
raise ex
175-
176-
def _extract_app_or_lib_from_archive(self, model_name, model_type, model_dict):
177-
"""
178-
Extract deployment contents from the archive.
179-
180-
:param model_name: the element name (my-app, etc.), used for logging
181-
:param model_type: the model type (Application, etc.), used for logging
182-
:param model_dict: the model dictionary
183-
"""
184-
_method_name = '_extract_app_or_lib_from_archive'
185-
self.logger.entering(model_name, model_type, model_dict,
186-
class_name=self._class_name, method_name=_method_name)
187-
188-
is_structured_app = False
189-
structured_app_dir = None
190-
if model_type == APPLICATION:
191-
is_structured_app, structured_app_dir = self._is_structured_app(model_dict)
192-
193-
if is_structured_app:
194-
# is_structured_app() only returns true if both the app and the plan have similar paths.
195-
# Since the caller already verified the app or plan was in the archive, it is safe to assume
196-
# both are in the archive.
197-
if self.archive_helper.contains_path(structured_app_dir):
198-
#
199-
# When extracting a directory, delete the old directory if it already exists so that the
200-
# extracted directory is exactly what is in the archive file.
201-
#
202-
existing_directory_path = \
203-
self.path_helper.local_join(self.model_context.get_domain_home(), structured_app_dir)
204-
if os.path.isdir(existing_directory_path):
205-
shutil.rmtree(existing_directory_path)
206-
self.archive_helper.extract_directory(structured_app_dir)
207-
else:
208-
ex = exception_helper.create_deploy_exception('WLSDPLY-09330',
209-
model_type, model_name, structured_app_dir)
210-
self.logger.throwing(ex, class_name=self._class_name, method_name=_method_name)
211-
raise ex
212-
else:
213-
model_source_path = dictionary_utils.get_element(model_dict, SOURCE_PATH)
214-
plan_file_to_extract = self._get_combined_model_plan_path(model_dict)
215-
216-
if not string_utils.is_empty(model_source_path) and \
217-
(model_source_path.endswith('/') or model_source_path.endswith('\\')):
218-
# model may have trailing slash on exploded source path
219-
source_path_to_extract = model_source_path[:-1]
220-
else:
221-
source_path_to_extract = model_source_path
222-
223-
# The caller only verified that either the app or the plan was in the archive; therefore,
224-
# we have to validate each one before trying to extract.
225-
if self.archive_helper.is_path_into_archive(source_path_to_extract):
226-
# source path may be a single file (jar, war, etc.) or an exploded directory
227-
if self.archive_helper.contains_file(source_path_to_extract):
228-
self.archive_helper.extract_file(source_path_to_extract)
229-
elif self.archive_helper.contains_path(source_path_to_extract):
230-
#
231-
# When extracting a directory, delete the old directory if it already exists so that the
232-
# extracted directory is exactly what is in the archive file.
233-
#
234-
existing_directory_path = \
235-
self.path_helper.local_join(self.model_context.get_domain_home(), source_path_to_extract)
236-
if os.path.isdir(existing_directory_path):
237-
shutil.rmtree(existing_directory_path)
238-
self.archive_helper.extract_directory(source_path_to_extract)
239-
else:
240-
ex = exception_helper.create_deploy_exception('WLSDPLY-09330',
241-
model_type, model_name, source_path_to_extract)
242-
self.logger.throwing(ex, class_name=self._class_name, method_name=_method_name)
243-
raise ex
244-
245-
# plan_file_to_extract should always be None for a structured app
246-
if self.archive_helper.is_path_into_archive(plan_file_to_extract):
247-
self.archive_helper.extract_file(plan_file_to_extract)
248-
249-
self.logger.exiting(class_name=self._class_name, method_name=_method_name)
250-
251158
def __validate_deployment_source_path(self, deployment_name, deployment_type, deployment_dict,
252159
existing_deployment_names):
253160
_method_name = '__validate_deployment_source_path'

0 commit comments

Comments
 (0)