|
| 1 | +# ***************************************************************************** |
| 2 | +# Copyright (c) 2024, 2026 IBM Corporation and other Contributors. |
| 3 | +# |
| 4 | +# All rights reserved. This program and the accompanying materials |
| 5 | +# are made available under the terms of the Eclipse Public License v1.0 |
| 6 | +# which accompanies this distribution, and is available at |
| 7 | +# http://www.eclipse.org/legal/epl-v10.html |
| 8 | +# |
| 9 | +# ***************************************************************************** |
| 10 | + |
| 11 | +from os import path |
| 12 | +from typing import TYPE_CHECKING, Dict, List, Any |
| 13 | +from prompt_toolkit import print_formatted_text |
| 14 | + |
| 15 | + |
| 16 | +if TYPE_CHECKING: |
| 17 | + # Type hints for methods and attributes provided by other mixins |
| 18 | + # These are only used during type checking and have no runtime cost |
| 19 | + from prompt_toolkit.completion import WordCompleter |
| 20 | + from prompt_toolkit.validation import Validator |
| 21 | + |
| 22 | + |
| 23 | +class AiSettingsMixin(): |
| 24 | + if TYPE_CHECKING: |
| 25 | + # Attributes from BaseApp and other mixins |
| 26 | + params: Dict[str, str] |
| 27 | + devMode: bool |
| 28 | + showAdvancedOptions: bool |
| 29 | + localConfigDir: str | None |
| 30 | + templatesDir: str |
| 31 | + dynamicClient: Any |
| 32 | + installAIService: bool |
| 33 | + |
| 34 | + # Methods from BaseApp |
| 35 | + def setParam(self, param: str, value: str) -> None: |
| 36 | + ... |
| 37 | + |
| 38 | + def getParam(self, param: str) -> str: |
| 39 | + ... |
| 40 | + |
| 41 | + # Methods from PrintMixin |
| 42 | + def printH1(self, message: str) -> None: |
| 43 | + ... |
| 44 | + |
| 45 | + def printH2(self, message: str) -> None: |
| 46 | + ... |
| 47 | + |
| 48 | + def printDescription(self, content: List[str]) -> None: |
| 49 | + ... |
| 50 | + |
| 51 | + # Methods from PromptMixin |
| 52 | + def yesOrNo(self, message: str, param: str | None = None) -> bool: |
| 53 | + ... |
| 54 | + |
| 55 | + def promptForString( |
| 56 | + self, |
| 57 | + message: str, |
| 58 | + param: str | None = None, |
| 59 | + default: str = "", |
| 60 | + isPassword: bool = False, |
| 61 | + validator: Validator | None = None, |
| 62 | + completer: WordCompleter | None = None |
| 63 | + ) -> str: |
| 64 | + ... |
| 65 | + |
| 66 | + def promptForListSelect( |
| 67 | + self, |
| 68 | + message: str, |
| 69 | + options: List[str], |
| 70 | + param: str | None = None, |
| 71 | + default: int | None = None |
| 72 | + ) -> str: |
| 73 | + ... |
| 74 | + |
| 75 | + # Methods from ConfigGeneratorMixin or InstallSettingsMixin |
| 76 | + def selectLocalConfigDir(self) -> None: |
| 77 | + ... |
| 78 | + |
| 79 | + def generateAiCfg(self, instanceId: str, scope: str, destination: str, workspaceId: str = "") -> None: |
| 80 | + ... |
| 81 | + |
| 82 | + def configAi(self, silentMode=False) -> None: |
| 83 | + """Configure AiCfg for MAS installation""" |
| 84 | + if not silentMode: |
| 85 | + self.printH1("Configure AiCfg") |
| 86 | + self.printDescription([ |
| 87 | + "The installer can configure AiCfg integration for your MAS instance.", |
| 88 | + "AiCfg provides AI/ML capabilities for MAS applications like Manage, Monitor, and Predict.", |
| 89 | + "AiCfg is configured at system scope and available to all workspaces." |
| 90 | + ]) |
| 91 | + |
| 92 | + # Ask if user wants to configure AiCfg |
| 93 | + if not silentMode: |
| 94 | + # Interactive mode - ask user |
| 95 | + configureAi = self.yesOrNo("Do you want to configure AiCfg") |
| 96 | + else: |
| 97 | + # Silent mode - check if explicitly requested via parameter |
| 98 | + # Default to False (skip) unless parameter says otherwise |
| 99 | + configureAi = self.getParam("configure_aiassistant") not in [None, "none", ""] |
| 100 | + |
| 101 | + if not configureAi: |
| 102 | + self.setParam("configure_aiassistant", "none") |
| 103 | + print_formatted_text("AiCfg configuration skipped") |
| 104 | + return |
| 105 | + |
| 106 | + instanceId = self.getParam('mas_instance_id') |
| 107 | + workspaceId = self.getParam('mas_workspace_id') |
| 108 | + |
| 109 | + # AiCfg is always configured at system scope |
| 110 | + scope = "system" |
| 111 | + self.setParam("ai_scope", "system") |
| 112 | + |
| 113 | + # Check if AI Service is being installed on the same cluster |
| 114 | + if hasattr(self, 'installAIService') and self.installAIService: |
| 115 | + # AI Service will be installed - defer AiCfg generation to pipeline |
| 116 | + if not silentMode: |
| 117 | + self.printH2("AiCfg Configuration (Automatic)") |
| 118 | + self.printDescription([ |
| 119 | + "AI Service is being installed on this cluster.", |
| 120 | + "The AiCfg will be automatically generated and applied by the pipeline", |
| 121 | + "AFTER AI Service installation completes.", |
| 122 | + "", |
| 123 | + "The pipeline will:", |
| 124 | + " 1. Install AI Service first", |
| 125 | + " 2. Auto-detect connection details (URL, API key, certificate)", |
| 126 | + " 3. Generate and apply AiCfg automatically", |
| 127 | + "", |
| 128 | + "No manual configuration needed!" |
| 129 | + ]) |
| 130 | + |
| 131 | + # Set action to indicate pipeline should handle it |
| 132 | + self.setParam("configure_aiassistant", "pipeline") |
| 133 | + print_formatted_text("\n✓ AiCfg will be automatically configured by the pipeline after AI Service installation") |
| 134 | + else: |
| 135 | + # Manual configuration for external AI Service |
| 136 | + if not silentMode: |
| 137 | + self.printH2("AiCfg Configuration") |
| 138 | + self.printDescription([ |
| 139 | + "You can provide connection details for an existing AI Service instance.", |
| 140 | + "The installer will generate the AiCfg YAML file with your connection details.", |
| 141 | + "", |
| 142 | + "IMPORTANT: The AiCfg file must be applied AFTER the MAS Core operator is installed,", |
| 143 | + "as the AiCfg CRD is created by the operator (not during initial config phase).", |
| 144 | + "Do NOT include this file in the initial configuration directory.", |
| 145 | + "Apply it after the operator creates the CRD." |
| 146 | + ]) |
| 147 | + |
| 148 | + createAiConfig = True |
| 149 | + if not silentMode: |
| 150 | + createAiConfig = self.yesOrNo("Generate AiCfg configuration file (apply after operator install)") |
| 151 | + |
| 152 | + if createAiConfig: |
| 153 | + self.setParam("configure_aiassistant", "configure") |
| 154 | + |
| 155 | + self.selectLocalConfigDir() |
| 156 | + |
| 157 | + # Check if a configuration already exists before creating a new one |
| 158 | + assert self.localConfigDir is not None, "localConfigDir must be set" |
| 159 | + |
| 160 | + if scope == "system": |
| 161 | + aiCfgFile = path.join(self.localConfigDir, f"aicfg-{instanceId}-system.yaml") |
| 162 | + print_formatted_text(f"Searching for AiCfg configuration file in {aiCfgFile} ...") |
| 163 | + else: |
| 164 | + aiCfgFile = path.join(self.localConfigDir, f"aicfg-{instanceId}-{workspaceId}.yaml") |
| 165 | + print_formatted_text(f"Searching for AiCfg configuration file in {aiCfgFile} ...") |
| 166 | + |
| 167 | + if path.exists(aiCfgFile): |
| 168 | + if self.yesOrNo("AiCfg configuration file already exists. Do you want to generate a new one"): |
| 169 | + self.generateAiCfg(instanceId=instanceId, scope=scope, destination=aiCfgFile, workspaceId=workspaceId) |
| 170 | + else: |
| 171 | + print_formatted_text(f"Expected file ({aiCfgFile}) was not found, generating a valid AiCfg configuration file now ...") |
| 172 | + self.generateAiCfg(instanceId=instanceId, scope=scope, destination=aiCfgFile, workspaceId=workspaceId) |
| 173 | + |
| 174 | + print_formatted_text(f"\nAiCfg configuration file created: {aiCfgFile}") |
| 175 | + print_formatted_text("This configuration will be applied during MAS installation.") |
| 176 | + else: |
| 177 | + self.setParam("configure_aiassistant", "none") |
| 178 | + print_formatted_text("AiCfg configuration skipped") |
0 commit comments