From a4b024bae7d4c3374207a1e6cb90b3c691363ac0 Mon Sep 17 00:00:00 2001 From: Keith Null Date: Sun, 28 Jun 2020 20:24:25 +0800 Subject: [PATCH] Add docker file and python script --- Dockerfile | 6 ++++++ refresh_cdn.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 3 files changed, 59 insertions(+) create mode 100644 Dockerfile create mode 100644 refresh_cdn.py create mode 100644 requirements.txt diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9ee07c4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +FROM python:3-alpine +# only copy necessary files for better cache +COPY requirements.txt refresh_cdn.py ./ +RUN python3 --version +RUN pip3 install -r requirements.txt +ENTRYPOINT python3 refresh_cdn.py diff --git a/refresh_cdn.py b/refresh_cdn.py new file mode 100644 index 0000000..6d8869e --- /dev/null +++ b/refresh_cdn.py @@ -0,0 +1,52 @@ +from tencentcloud.common import credential +from tencentcloud.common.profile.client_profile import ClientProfile +from tencentcloud.common.profile.http_profile import HttpProfile +from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException +from tencentcloud.cdn.v20180606 import cdn_client, models +import json +import os + + +def refresh_cdn(secret_id, secret_key, paths, flush_type="flush"): + cred = credential.Credential(secret_id, secret_key) + http_profile = HttpProfile() + http_profile.endpoint = "cdn.tencentcloudapi.com" + + client_profile = ClientProfile() + client_profile.httpProfile = http_profile + client = cdn_client.CdnClient(cred, "", client_profile) + + req = models.PurgePathCacheRequest() + params = { + "Paths": paths, + "FlushType": flush_type, + } + params = json.dumps(params) + req.from_json_string(params) + + return client.PurgePathCache(req) + + +def parse_env(): + secret_id = os.getenv("SECRET_ID", None) + assert secret_id is not None, "Please provide Secret ID" + secret_key = os.getenv("SECRET_KEY", None) + assert secret_key is not None, "Please provide Secret Key" + paths = os.getenv("PATHS", "") + # split and only keep non-whitespaces + paths = filter(lambda pth: len(pth) > 0, map(str.strip, paths.split(","))) + paths = list(paths) + assert len(paths) >= 1, "Please specify at least one path to refresh" + flush_type = os.getenv("FLUSH_TYPE", "flush") + return secret_id, secret_key, paths, flush_type + + +if __name__ == '__main__': + try: + resp = refresh_cdn(*parse_env()) + print("Successfully purged!") + print(resp) + except TencentCloudSDKException as err: + print("Failed to purge:") + print(err) + exit(-1) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..91d0f79 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +tencentcloud-sdk-python~=3.0.203