|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# ***************************************************************************** |
| 4 | +# Copyright (c) 2024 IBM Corporation and other Contributors. |
| 5 | +# |
| 6 | +# All rights reserved. This program and the accompanying materials |
| 7 | +# are made available under the terms of the Eclipse Public License v1.0 |
| 8 | +# which accompanies this distribution, and is available at |
| 9 | +# http://www.eclipse.org/legal/epl-v10.html |
| 10 | +# |
| 11 | +# ***************************************************************************** |
| 12 | + |
| 13 | +from kubernetes import client, config |
| 14 | +from kubernetes.config.config_exception import ConfigException |
| 15 | +import argparse |
| 16 | +import logging |
| 17 | +import urllib3 |
| 18 | +urllib3.disable_warnings() |
| 19 | +import os |
| 20 | +import sys |
| 21 | +import json |
| 22 | +import re |
| 23 | +import yaml |
| 24 | + |
| 25 | +from mas.devops.users import MASUserUtils |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +if __name__ == "__main__": |
| 30 | + parser = argparse.ArgumentParser() |
| 31 | + |
| 32 | + # Primary Options |
| 33 | + parser.add_argument("--mas-instance-id", required=True) |
| 34 | + parser.add_argument("--mas-workspace-id", required=True) |
| 35 | + parser.add_argument("--initial-users-yaml-file", required=True) |
| 36 | + parser.add_argument("--log-level", required=False, choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], default="WARNING") |
| 37 | + parser.add_argument("--dry-run", required=False, help="When specified, nothing will actually be deleted from the cluster", action="store_true") |
| 38 | + |
| 39 | + args, unknown = parser.parse_known_args() |
| 40 | + |
| 41 | + log_level = getattr(logging, args.log_level) |
| 42 | + |
| 43 | + logger = logging.getLogger() |
| 44 | + logger.setLevel(log_level) |
| 45 | + |
| 46 | + ch = logging.StreamHandler() |
| 47 | + ch.setLevel(log_level) |
| 48 | + chFormatter = logging.Formatter( |
| 49 | + "%(asctime)-25s %(name)-50s [%(threadName)s] %(levelname)-8s %(message)s" |
| 50 | + ) |
| 51 | + ch.setFormatter(chFormatter) |
| 52 | + logger.addHandler(ch) |
| 53 | + |
| 54 | + |
| 55 | + mas_instance_id = args.mas_instance_id |
| 56 | + mas_workspace_id = args.mas_workspace_id |
| 57 | + initial_users_yaml_file = args.initial_users_yaml_file |
| 58 | + dry_run = args.dry_run |
| 59 | + |
| 60 | + logger.info("Configuration:") |
| 61 | + logger.info("--------------") |
| 62 | + logger.info(f"mas_instance_id: {mas_instance_id}") |
| 63 | + logger.info(f"mas_workspace_id: {mas_workspace_id}") |
| 64 | + logger.info(f"initial_users_yaml_file: {initial_users_yaml_file}") |
| 65 | + logger.info(f"log_level: {log_level}") |
| 66 | + logger.info(f"dry_run: {dry_run}") |
| 67 | + logger.info("") |
| 68 | + |
| 69 | + try: |
| 70 | + # Try to load in-cluster configuration |
| 71 | + config.load_incluster_config() |
| 72 | + logger.info("Loaded in-cluster configuration") |
| 73 | + except ConfigException: |
| 74 | + # If that fails, fall back to kubeconfig file |
| 75 | + config.load_kube_config() |
| 76 | + logger.info("Loaded kubeconfig file") |
| 77 | + |
| 78 | + user_utils = MASUserUtils(mas_instance_id, mas_workspace_id, client.api_client.ApiClient()) |
| 79 | + |
| 80 | + with open(initial_users_yaml_file, 'r') as file: |
| 81 | + initial_users_yaml = yaml.safe_load(file) |
| 82 | + |
| 83 | + user_utils.create_initial_users_for_saas(initial_users_yaml) |
| 84 | + |
0 commit comments