|
| 1 | +""" |
| 2 | +Copyright (C) 2022 Red Hat, Inc. (https://github.com/Commonjava/charon) |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +from charon.config import get_config |
| 18 | +from charon.cmd.internal import _decide_mode, _get_buckets |
| 19 | +from charon.cache import CFClient |
| 20 | +from charon.pkgs.pkg_utils import invalidate_cf_paths |
| 21 | +from click import command, option |
| 22 | +from typing import List |
| 23 | + |
| 24 | +import traceback |
| 25 | +import logging |
| 26 | +import sys |
| 27 | +import os |
| 28 | + |
| 29 | +logger = logging.getLogger(__name__) |
| 30 | + |
| 31 | + |
| 32 | +@option( |
| 33 | + "--target", |
| 34 | + "-t", |
| 35 | + "target", |
| 36 | + help=""" |
| 37 | + The target to do the uploading, which will decide which s3 bucket |
| 38 | + and what root path where all files will be uploaded to. |
| 39 | + Can accept more than one target. |
| 40 | + """, |
| 41 | + required=True |
| 42 | +) |
| 43 | +@option( |
| 44 | + "--path", |
| 45 | + "-p", |
| 46 | + "paths", |
| 47 | + help=""" |
| 48 | + The paths which will be invalidated in CF. The path can use the format as CF defining |
| 49 | + in https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Invalidation.html |
| 50 | + """, |
| 51 | + multiple=True |
| 52 | +) |
| 53 | +@option( |
| 54 | + "--path-file", |
| 55 | + "-f", |
| 56 | + "path_file", |
| 57 | + help=""" |
| 58 | + The file which contain the paths to be invalidated in CF. Pahts in this file follow the |
| 59 | + format of CF defining too, and each path should be in a single line. |
| 60 | + """ |
| 61 | +) |
| 62 | +@option( |
| 63 | + "--debug", |
| 64 | + "-D", |
| 65 | + "debug", |
| 66 | + help="Debug mode, will print all debug logs for problem tracking.", |
| 67 | + is_flag=True, |
| 68 | + default=False |
| 69 | +) |
| 70 | +@option( |
| 71 | + "--quiet", |
| 72 | + "-q", |
| 73 | + "quiet", |
| 74 | + help="Quiet mode, will shrink most of the logs except warning and errors.", |
| 75 | + is_flag=True, |
| 76 | + default=False |
| 77 | +) |
| 78 | +@command() |
| 79 | +def clear_cf( |
| 80 | + target: str, |
| 81 | + paths: List[str], |
| 82 | + path_file: str, |
| 83 | + quiet: bool = False, |
| 84 | + debug: bool = False |
| 85 | +): |
| 86 | + """This command will do invalidating on AWS CloudFront for the specified paths. |
| 87 | + """ |
| 88 | + _decide_mode( |
| 89 | + f"cfclear-{target}", "", |
| 90 | + is_quiet=quiet, is_debug=debug |
| 91 | + ) |
| 92 | + if not paths and not path_file: |
| 93 | + logger.error( |
| 94 | + "No path specified, please specify at least one path " |
| 95 | + "through --path or --path-file.") |
| 96 | + sys.exit(1) |
| 97 | + |
| 98 | + work_paths = [] |
| 99 | + if paths: |
| 100 | + work_paths.extend(paths) |
| 101 | + |
| 102 | + if path_file: |
| 103 | + with open(path_file, "r", encoding="utf-8") as f: |
| 104 | + for line in f.readlines(): |
| 105 | + work_paths.append(str(line).strip()) |
| 106 | + |
| 107 | + try: |
| 108 | + conf = get_config() |
| 109 | + if not conf: |
| 110 | + sys.exit(1) |
| 111 | + |
| 112 | + aws_profile = os.getenv("AWS_PROFILE") or conf.get_aws_profile() |
| 113 | + if not aws_profile: |
| 114 | + logger.error("No AWS profile specified!") |
| 115 | + sys.exit(1) |
| 116 | + |
| 117 | + buckets = _get_buckets([target], conf) |
| 118 | + |
| 119 | + for b in buckets: |
| 120 | + cf_client = CFClient(aws_profile=aws_profile) |
| 121 | + invalidate_cf_paths( |
| 122 | + cf_client, b, work_paths |
| 123 | + ) |
| 124 | + except Exception: |
| 125 | + print(traceback.format_exc()) |
| 126 | + sys.exit(2) |
0 commit comments