|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import logging |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +from datetime import datetime, timedelta |
| 8 | + |
| 9 | +from argo_config import ArgoConfig |
| 10 | +from argo_web_api import ArgoWebApi |
| 11 | + |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +def run_batch( |
| 16 | + config: ArgoConfig, |
| 17 | + tenant_name: str, |
| 18 | + report_id: str, |
| 19 | + cur_date_str: str, |
| 20 | + prev_date_str: str, |
| 21 | + tenant_web_api_token: str, |
| 22 | + dry_run: bool, |
| 23 | +): |
| 24 | + """Function that composes the appropriate cli command to submit a batch job execution in flink""" |
| 25 | + |
| 26 | + cmd = [ |
| 27 | + config.flink_path, |
| 28 | + "run", |
| 29 | + "-c", |
| 30 | + "argo.batch.ArgoMultiJob", |
| 31 | + config.batch_jar_path, |
| 32 | + "--run.date", |
| 33 | + cur_date_str, |
| 34 | + "--mongo.url", |
| 35 | + f"{config.mongodb_url}/{config.tenant_db_prefix}{tenant_name}", |
| 36 | + "--pdata", |
| 37 | + f"{config.hdfs_path}/{tenant_name}/mdata/{prev_date_str}", |
| 38 | + "--mdata", |
| 39 | + f"{config.hdfs_path}/{tenant_name}/mdata/{cur_date_str}", |
| 40 | + "--api.endpoint", |
| 41 | + config.web_api_endpoint, |
| 42 | + "--mongo.method", |
| 43 | + "insert", |
| 44 | + "--clear.mongo", |
| 45 | + "true", |
| 46 | + "--api.token", |
| 47 | + tenant_web_api_token, |
| 48 | + "--report.id", |
| 49 | + report_id, |
| 50 | + ] |
| 51 | + |
| 52 | + if dry_run: |
| 53 | + print(("\033[92m" + " ".join(str(x) for x in cmd) + "\033[0m")) |
| 54 | + return 0 |
| 55 | + else: |
| 56 | + try: |
| 57 | + subprocess.run(cmd, check=True) |
| 58 | + except Exception as e: |
| 59 | + logger.error(f"Batch Job Error: {e}") |
| 60 | + return 1 |
| 61 | + |
| 62 | + |
| 63 | +def main(): |
| 64 | + parser = argparse.ArgumentParser(description="Argo Automator") |
| 65 | + parser.add_argument( |
| 66 | + "-c", |
| 67 | + "--config", |
| 68 | + default="config.yml", |
| 69 | + help="Path to configuration file (default: config.yml)", |
| 70 | + ) |
| 71 | + parser.add_argument( |
| 72 | + "-l", |
| 73 | + "--log-level", |
| 74 | + default="INFO", |
| 75 | + choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], |
| 76 | + help="Set logging level (default: INFO)", |
| 77 | + ) |
| 78 | + parser.add_argument( |
| 79 | + "-d", |
| 80 | + "--date", |
| 81 | + type=str, |
| 82 | + help="set computation date as YYYY-MM-DD", |
| 83 | + ) |
| 84 | + parser.add_argument( |
| 85 | + "-t", |
| 86 | + "--tenant", |
| 87 | + required=True, |
| 88 | + type=str, |
| 89 | + help="select tenant name for computation", |
| 90 | + ) |
| 91 | + parser.add_argument( |
| 92 | + "-r", |
| 93 | + "--report", |
| 94 | + required=True, |
| 95 | + type=str, |
| 96 | + help="select report name for computation", |
| 97 | + ) |
| 98 | + parser.add_argument( |
| 99 | + "--dry-run", |
| 100 | + help="Runs in test mode without actually submitting the job", |
| 101 | + action="store_true", |
| 102 | + dest="dry_run", |
| 103 | + ) |
| 104 | + |
| 105 | + args = parser.parse_args() |
| 106 | + |
| 107 | + logging.basicConfig( |
| 108 | + level=getattr(logging, args.log_level), |
| 109 | + format="%(asctime)s - %(levelname)s - %(message)s", |
| 110 | + ) |
| 111 | + |
| 112 | + config = ArgoConfig(args.config) |
| 113 | + |
| 114 | + if not config.flink_path: |
| 115 | + logging.error( |
| 116 | + f"You need to set-up the run.flink_path parameter in your configuration file {args.config}" |
| 117 | + ) |
| 118 | + return 1 |
| 119 | + |
| 120 | + if not config.batch_jar_path: |
| 121 | + logging.error( |
| 122 | + f"You need to set-up the run.batch_jar_path parameter in your configuration file {args.config}" |
| 123 | + ) |
| 124 | + return 1 |
| 125 | + |
| 126 | + web_api = ArgoWebApi(config) |
| 127 | + |
| 128 | + ref_date = datetime.strptime(args.date, "%Y-%m-%d") if args.date else datetime.now() |
| 129 | + prev_date_str = (ref_date - timedelta(days=1)).strftime("%Y-%m-%d") |
| 130 | + cur_date_str = ref_date.strftime("%Y-%m-%d") |
| 131 | + |
| 132 | + # check if tenant exists in config |
| 133 | + tenant = config.tenants.get(args.tenant, {}) |
| 134 | + if not tenant or not tenant.get("id", {}) or not tenant.get("web_api_token", {}): |
| 135 | + logging.error(f"Tenant {args.tenant} not configured in compute engine") |
| 136 | + return 1 |
| 137 | + |
| 138 | + # check if report exists |
| 139 | + report_id = ( |
| 140 | + config.tenants.get(args.tenant, {}).get("reports", {}).get(args.report, {}) |
| 141 | + ) |
| 142 | + if not report_id: |
| 143 | + # try to update the tenant report configuration |
| 144 | + reports = web_api.get_reports( |
| 145 | + tenant["id"], args.tenant, tenant["web_api_token"] |
| 146 | + ) |
| 147 | + config.set_tenant_reports(tenant["id"], args.tenant, reports) |
| 148 | + |
| 149 | + # check again if report is included in the updated web-api result |
| 150 | + report_id = ( |
| 151 | + config.tenants.get(args.tenant, {}).get("reports", {}).get(args.report, {}) |
| 152 | + ) |
| 153 | + if not report_id: |
| 154 | + logging.error( |
| 155 | + f"Tenant {args.tenant} does not include a report named: {args.report}" |
| 156 | + ) |
| 157 | + return 1 |
| 158 | + |
| 159 | + return run_batch( |
| 160 | + config, args.tenant, report_id, cur_date_str, prev_date_str, tenant.get("web_api_token"), args.dry_run |
| 161 | + ) |
| 162 | + |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + sys.exit(main()) |
0 commit comments