1414from pantheon_uploader import pantheon
1515from . import api_blueprint
1616from . import executor
17+ from . import drupal_client
18+ from . import akamai_purge_client
1719from .. import utils
18- from ..helpers import FileHelper , GitHelper , MessageHelper
20+ from ..helpers import FileHelper , GitHelper , MessageHelper , CacheObjectHelper
1921from ..messaging import broker
2022from ..models .request_models import RepoSchema
2123from ..models .response_models import Status
2224from ..utils import ApiError , get_docs_path_for
2325from flask import current_app
26+ from decorest import HTTPErrorWrapper
2427
28+ ASSEMBLIES = "assemblies"
29+ MODULES = "modules"
2530logger = logging .getLogger (__name__ )
2631
2732
@@ -44,7 +49,7 @@ def push_repo():
4449
4550def clone_repo (repo_name , repo_url , branch ):
4651 try :
47-
52+
4853 MessageHelper .publish (repo_name + "-clone" ,
4954 json .dumps (dict (current_status = "cloning" , details = "Cloning repo " + repo_name + "" )))
5055 logger .info ("Cloning repo=" + repo_url + " and branch=" + branch )
@@ -81,8 +86,8 @@ def upload_repo(cloned_repo, channel_name):
8186 try :
8287 pantheon .start_process (numeric_level = 10 , pw = current_app .config ['UPLOADER_PASSWORD' ],
8388 user = current_app .config ['UPLOADER_USER' ],
84- server = current_app .config ['PANTHEON_SERVER' ], directory = cloned_repo .working_dir ,
85- use_broker = True , channel = channel_name , broker_host = os .getenv ('REDIS_SERVICE' ) )
89+ server = current_app .config ['PANTHEON_SERVER' ], directory = cloned_repo .working_dir ,
90+ use_broker = True , channel = channel_name , broker_host = os .getenv ('REDIS_SERVICE' ))
8691 except Exception as e :
8792 logger .error ("Upload failed due to error=" + str (e ))
8893 MessageHelper .publish (channel_name ,
@@ -105,7 +110,7 @@ def info():
105110def status ():
106111 status_data , clone_data = get_upload_data ()
107112 current_status = get_current_status (clone_data , status_data )
108- logger .debug ("current status=" + current_status )
113+ logger .debug ("current status=" + current_status )
109114 status_message = Status (clone_status = clone_data .get ('current_status' , "" ),
110115 current_status = current_status ,
111116 file_type = status_data .get ('type_uploading' , "" ),
@@ -120,8 +125,8 @@ def status():
120125
121126
122127def get_current_status (clone_data , status_data ):
123- logger .debug ('upload status data=' + json .dumps (status_data ))
124- logger .debug ('clone status data=' + json .dumps (clone_data ))
128+ logger .debug ('upload status data=' + json .dumps (status_data ))
129+ logger .debug ('clone status data=' + json .dumps (clone_data ))
125130 return status_data .get ('current_status' ) if status_data .get ('current_status' , "" ) != "" else clone_data [
126131 'current_status' ]
127132
@@ -147,7 +152,7 @@ def get_request_data():
147152
148153
149154def reset_if_exists (repo_name ):
150- MessageHelper .publish (repo_name + "-clone" , json .dumps (dict (current_status = '' )))
155+ MessageHelper .publish (repo_name + "-clone" , json .dumps (dict (current_status = '' )))
151156 MessageHelper .publish (repo_name , json .dumps (dict (current_status = '' )))
152157
153158
@@ -157,7 +162,7 @@ def progress_update():
157162 status_data , clone_data = get_upload_data ()
158163 # status_progress: UploadStatus = upload_status_from_dict(status_data)
159164 if "server" in status_data and status_data ["server" ]["response_code" ] and not 200 <= int (status_data ["server" ][
160- "response_code" ]) <= 400 :
165+ "response_code" ]) <= 400 :
161166 return jsonify (
162167 dict (
163168 server_status = status_data ["server" ]["response_code" ],
@@ -265,3 +270,52 @@ def progress_update_resources():
265270 return jsonify (
266271 response_dict
267272 ), 200
273+
274+
275+ @swag_from (get_docs_path_for ('cache_clear_api.yaml' ))
276+ @api_blueprint .route ('/cache/clear' , methods = ['POST' ])
277+ def clear_cache ():
278+ data = get_request_data ()
279+ cache_clear_result = {
280+ "drupal_result_assemblies" :{},
281+ "drupal_result_modules" : {}
282+ }
283+
284+ try :
285+ clear_drupal_cache (data , cache_clear_result )
286+ clear_akamai_cache (data ,cache_clear_result )
287+ except Exception as e :
288+ logger .error ("Exception occurred while trying to clear cache with error=" + str (e ))
289+ raise ApiError ("Upstream Server Error" , 503 , details = str (e ))
290+ return jsonify (cache_clear_result )
291+
292+
293+ def drupal_cache_clear_bulk (cache_clear_result , cache_req_data ):
294+ if "assemblies" in cache_req_data :
295+ cache_clear_result ["drupal_result_assemblies" ] = drupal_client .purge_cache_assembly ("assemblies" )
296+
297+ if "modules" in cache_req_data :
298+ cache_clear_result ["drupal_result_modules" ] = drupal_client .purge_cache_assembly ("modules" )
299+
300+
301+ def clear_drupal_cache (data , cache_clear_result , bulk_clear = False ):
302+ cache_req_data = CacheObjectHelper .get_drupal_req_data (data )
303+ if bulk_clear :
304+ drupal_cache_clear_bulk (cache_clear_result , cache_req_data )
305+ return
306+ drupal_cache_clear_individual (cache_clear_result , cache_req_data )
307+
308+
309+ def drupal_cache_clear_individual (cache_clear_result , cache_req_data ):
310+ if ASSEMBLIES in cache_req_data :
311+ for guid in cache_req_data [ASSEMBLIES ]:
312+ cache_clear_result ["drupal_result_assemblies" ][str (guid )] = drupal_client .purge_cache_assembly (guid )
313+ if MODULES in cache_req_data :
314+ for guid in cache_req_data [MODULES ]:
315+ cache_clear_result ["drupal_result_modules" ][str (guid )] = (drupal_client .purge_cache_module (guid ))
316+
317+
318+ def clear_akamai_cache (data , cache_clear_result ):
319+ cache_req_data = CacheObjectHelper .get_akamai_req_object (data )
320+ cache_clear_result ['akamai_result' ] = akamai_purge_client .purge (cache_req_data )
321+
0 commit comments