mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-15 13:13:48 +00:00
Support multiple enterprise configurations (#662)
Build and Release / Read metadata (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Build and Release / Read metadata (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
This commit is contained in:
1 parent
48f8cb3285
commit
3671444d28
14 files changed
+479
-236
No files matched your search
@@ -103,6 +103,16 @@ public static partial class PluginFactory
|
||||
}
|
||||
|
||||
LOG.LogInformation($"Successfully loaded plugin: '{pluginMainFile}' (Id='{plugin.Id}', Type='{plugin.Type}', Name='{plugin.Name}', Version='{plugin.Version}', Authors='{string.Join(", ", plugin.Authors)}')");
|
||||
|
||||
// For configuration plugins, validate that the plugin ID matches the enterprise config ID
|
||||
// (the directory name under which the plugin was downloaded):
|
||||
if (plugin.Type is PluginType.CONFIGURATION && pluginPath.StartsWith(CONFIGURATION_PLUGINS_ROOT, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var directoryName = Path.GetFileName(pluginPath);
|
||||
if (Guid.TryParse(directoryName, out var enterpriseConfigId) && enterpriseConfigId != plugin.Id)
|
||||
LOG.LogWarning($"The configuration plugin's ID ('{plugin.Id}') does not match the enterprise configuration ID ('{enterpriseConfigId}'). These IDs should be identical. Please update the plugin's ID field to match the enterprise configuration ID.");
|
||||
}
|
||||
|
||||
AVAILABLE_PLUGINS.Add(new PluginMetadata(plugin, pluginPath));
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace AIStudio.Tools.Rust;
|
||||
|
||||
public sealed record EnterpriseConfig(string Id, string ServerUrl);
|
||||
@@ -4,7 +4,7 @@ namespace AIStudio.Tools.Services;
|
||||
|
||||
public sealed class EnterpriseEnvironmentService(ILogger<EnterpriseEnvironmentService> logger, RustService rustService) : BackgroundService
|
||||
{
|
||||
public static EnterpriseEnvironment CURRENT_ENVIRONMENT;
|
||||
public static List<EnterpriseEnvironment> CURRENT_ENVIRONMENTS = [];
|
||||
|
||||
#if DEBUG
|
||||
private static readonly TimeSpan CHECK_INTERVAL = TimeSpan.FromMinutes(6);
|
||||
@@ -33,84 +33,125 @@ public sealed class EnterpriseEnvironmentService(ILogger<EnterpriseEnvironmentSe
|
||||
try
|
||||
{
|
||||
logger.LogInformation("Start updating of the enterprise environment.");
|
||||
|
||||
Guid enterpriseRemoveConfigId;
|
||||
try
|
||||
{
|
||||
enterpriseRemoveConfigId = await rustService.EnterpriseEnvRemoveConfigId();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Failed to fetch the enterprise remove configuration ID from the Rust service.");
|
||||
await MessageBus.INSTANCE.SendMessage(null, Event.RUST_SERVICE_UNAVAILABLE, "EnterpriseEnvRemoveConfigId failed");
|
||||
return;
|
||||
}
|
||||
|
||||
var isPlugin2RemoveInUse = PluginFactory.AvailablePlugins.Any(plugin => plugin.Id == enterpriseRemoveConfigId);
|
||||
if (enterpriseRemoveConfigId != Guid.Empty && isPlugin2RemoveInUse)
|
||||
{
|
||||
logger.LogWarning("The enterprise environment configuration ID '{EnterpriseRemoveConfigId}' must be removed.", enterpriseRemoveConfigId);
|
||||
PluginFactory.RemovePluginAsync(enterpriseRemoveConfigId);
|
||||
}
|
||||
|
||||
string? enterpriseConfigServerUrl;
|
||||
//
|
||||
// Step 1: Handle deletions first.
|
||||
//
|
||||
List<Guid> deleteConfigIds;
|
||||
try
|
||||
{
|
||||
enterpriseConfigServerUrl = await rustService.EnterpriseEnvConfigServerUrl();
|
||||
deleteConfigIds = await rustService.EnterpriseEnvDeleteConfigIds();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Failed to fetch the enterprise configuration server URL from the Rust service.");
|
||||
await MessageBus.INSTANCE.SendMessage(null, Event.RUST_SERVICE_UNAVAILABLE, "EnterpriseEnvConfigServerUrl failed");
|
||||
logger.LogError(e, "Failed to fetch the enterprise delete configuration IDs from the Rust service.");
|
||||
await MessageBus.INSTANCE.SendMessage(null, Event.RUST_SERVICE_UNAVAILABLE, "EnterpriseEnvDeleteConfigIds failed");
|
||||
return;
|
||||
}
|
||||
|
||||
Guid enterpriseConfigId;
|
||||
try
|
||||
foreach (var deleteId in deleteConfigIds)
|
||||
{
|
||||
enterpriseConfigId = await rustService.EnterpriseEnvConfigId();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Failed to fetch the enterprise configuration ID from the Rust service.");
|
||||
await MessageBus.INSTANCE.SendMessage(null, Event.RUST_SERVICE_UNAVAILABLE, "EnterpriseEnvConfigId failed");
|
||||
return;
|
||||
}
|
||||
|
||||
var etag = await PluginFactory.DetermineConfigPluginETagAsync(enterpriseConfigId, enterpriseConfigServerUrl);
|
||||
var nextEnterpriseEnvironment = new EnterpriseEnvironment(enterpriseConfigServerUrl, enterpriseConfigId, etag);
|
||||
if (CURRENT_ENVIRONMENT != nextEnterpriseEnvironment)
|
||||
{
|
||||
logger.LogInformation("The enterprise environment has changed. Updating the current environment.");
|
||||
CURRENT_ENVIRONMENT = nextEnterpriseEnvironment;
|
||||
|
||||
switch (enterpriseConfigServerUrl)
|
||||
var isPluginInUse = PluginFactory.AvailablePlugins.Any(plugin => plugin.Id == deleteId);
|
||||
if (isPluginInUse)
|
||||
{
|
||||
case null when enterpriseConfigId == Guid.Empty:
|
||||
case not null when string.IsNullOrWhiteSpace(enterpriseConfigServerUrl) && enterpriseConfigId == Guid.Empty:
|
||||
logger.LogInformation("AI Studio runs without an enterprise configuration.");
|
||||
break;
|
||||
|
||||
case null:
|
||||
logger.LogWarning("AI Studio runs with an enterprise configuration id ('{EnterpriseConfigId}'), but the configuration server URL is not set.", enterpriseConfigId);
|
||||
break;
|
||||
|
||||
case not null when !string.IsNullOrWhiteSpace(enterpriseConfigServerUrl) && enterpriseConfigId == Guid.Empty:
|
||||
logger.LogWarning("AI Studio runs with an enterprise configuration server URL ('{EnterpriseConfigServerUrl}'), but the configuration ID is not set.", enterpriseConfigServerUrl);
|
||||
break;
|
||||
|
||||
default:
|
||||
logger.LogInformation("AI Studio runs with an enterprise configuration id ('{EnterpriseConfigId}') and configuration server URL ('{EnterpriseConfigServerUrl}').", enterpriseConfigId, enterpriseConfigServerUrl);
|
||||
|
||||
if(isFirstRun)
|
||||
MessageBus.INSTANCE.DeferMessage(null, Event.STARTUP_ENTERPRISE_ENVIRONMENT, new EnterpriseEnvironment(enterpriseConfigServerUrl, enterpriseConfigId, etag));
|
||||
else
|
||||
await PluginFactory.TryDownloadingConfigPluginAsync(enterpriseConfigId, enterpriseConfigServerUrl);
|
||||
break;
|
||||
logger.LogWarning("The enterprise environment configuration ID '{DeleteConfigId}' must be removed.", deleteId);
|
||||
PluginFactory.RemovePluginAsync(deleteId);
|
||||
}
|
||||
}
|
||||
else
|
||||
logger.LogInformation("The enterprise environment has not changed. No update required.");
|
||||
|
||||
//
|
||||
// Step 2: Fetch all active configurations.
|
||||
//
|
||||
List<EnterpriseEnvironment> fetchedConfigs;
|
||||
try
|
||||
{
|
||||
fetchedConfigs = await rustService.EnterpriseEnvConfigs();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Failed to fetch the enterprise configurations from the Rust service.");
|
||||
await MessageBus.INSTANCE.SendMessage(null, Event.RUST_SERVICE_UNAVAILABLE, "EnterpriseEnvConfigs failed");
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Step 3: Determine ETags and build the next environment list.
|
||||
//
|
||||
var nextEnvironments = new List<EnterpriseEnvironment>();
|
||||
foreach (var config in fetchedConfigs)
|
||||
{
|
||||
if (!config.IsActive)
|
||||
{
|
||||
logger.LogWarning("Skipping inactive enterprise configuration with ID '{ConfigId}'. There is either no valid server URL or config ID set.", config.ConfigurationId);
|
||||
continue;
|
||||
}
|
||||
|
||||
var etag = await PluginFactory.DetermineConfigPluginETagAsync(config.ConfigurationId, config.ConfigurationServerUrl);
|
||||
nextEnvironments.Add(config with { ETag = etag });
|
||||
}
|
||||
|
||||
if (nextEnvironments.Count == 0)
|
||||
{
|
||||
if (CURRENT_ENVIRONMENTS.Count > 0)
|
||||
{
|
||||
logger.LogWarning("AI Studio no longer has any enterprise configurations. Removing previously active configs.");
|
||||
|
||||
// Remove plugins for configs that were previously active:
|
||||
foreach (var oldEnv in CURRENT_ENVIRONMENTS)
|
||||
{
|
||||
var isPluginInUse = PluginFactory.AvailablePlugins.Any(plugin => plugin.Id == oldEnv.ConfigurationId);
|
||||
if (isPluginInUse)
|
||||
PluginFactory.RemovePluginAsync(oldEnv.ConfigurationId);
|
||||
}
|
||||
}
|
||||
else
|
||||
logger.LogInformation("AI Studio runs without any enterprise configurations.");
|
||||
|
||||
CURRENT_ENVIRONMENTS = [];
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Step 4: Compare with current environments and process changes.
|
||||
//
|
||||
var currentIds = CURRENT_ENVIRONMENTS.Select(e => e.ConfigurationId).ToHashSet();
|
||||
var nextIds = nextEnvironments.Select(e => e.ConfigurationId).ToHashSet();
|
||||
|
||||
// Remove plugins for configs that are no longer present:
|
||||
foreach (var oldEnv in CURRENT_ENVIRONMENTS)
|
||||
{
|
||||
if (!nextIds.Contains(oldEnv.ConfigurationId))
|
||||
{
|
||||
logger.LogInformation("Enterprise configuration '{ConfigId}' was removed.", oldEnv.ConfigurationId);
|
||||
var isPluginInUse = PluginFactory.AvailablePlugins.Any(plugin => plugin.Id == oldEnv.ConfigurationId);
|
||||
if (isPluginInUse)
|
||||
PluginFactory.RemovePluginAsync(oldEnv.ConfigurationId);
|
||||
}
|
||||
}
|
||||
|
||||
// Process new or changed configs:
|
||||
foreach (var nextEnv in nextEnvironments)
|
||||
{
|
||||
var currentEnv = CURRENT_ENVIRONMENTS.FirstOrDefault(e => e.ConfigurationId == nextEnv.ConfigurationId);
|
||||
if (currentEnv == nextEnv) // Hint: This relies on the record equality to check if anything relevant has changed (e.g. server URL or ETag).
|
||||
{
|
||||
logger.LogInformation("Enterprise configuration '{ConfigId}' has not changed. No update required.", nextEnv.ConfigurationId);
|
||||
continue;
|
||||
}
|
||||
|
||||
var isNew = !currentIds.Contains(nextEnv.ConfigurationId);
|
||||
if(isNew)
|
||||
logger.LogInformation("Detected new enterprise configuration with ID '{ConfigId}' and server URL '{ServerUrl}'.", nextEnv.ConfigurationId, nextEnv.ConfigurationServerUrl);
|
||||
else
|
||||
logger.LogInformation("Detected change in enterprise configuration with ID '{ConfigId}'. Server URL or ETag has changed.", nextEnv.ConfigurationId);
|
||||
|
||||
if (isFirstRun)
|
||||
MessageBus.INSTANCE.DeferMessage(null, Event.STARTUP_ENTERPRISE_ENVIRONMENT, nextEnv);
|
||||
else
|
||||
await PluginFactory.TryDownloadingConfigPluginAsync(nextEnv.ConfigurationId, nextEnv.ConfigurationServerUrl);
|
||||
}
|
||||
|
||||
CURRENT_ENVIRONMENTS = nextEnvironments;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
@@ -1,71 +1,9 @@
|
||||
namespace AIStudio.Tools.Services;
|
||||
using AIStudio.Tools.Rust;
|
||||
|
||||
namespace AIStudio.Tools.Services;
|
||||
|
||||
public sealed partial class RustService
|
||||
{
|
||||
/// <summary>
|
||||
/// Tries to read the enterprise environment for the current user's configuration ID.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// Returns the empty Guid when the environment is not set or the request fails.
|
||||
/// Otherwise, the configuration ID.
|
||||
/// </returns>
|
||||
public async Task<Guid> EnterpriseEnvConfigId()
|
||||
{
|
||||
var result = await this.http.GetAsync("/system/enterprise/config/id");
|
||||
if (!result.IsSuccessStatusCode)
|
||||
{
|
||||
this.logger!.LogError($"Failed to query the enterprise configuration ID: '{result.StatusCode}'");
|
||||
return Guid.Empty;
|
||||
}
|
||||
|
||||
Guid.TryParse(await result.Content.ReadAsStringAsync(), out var configurationId);
|
||||
return configurationId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to read the enterprise environment for a configuration ID, which must be removed.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Removing a configuration ID is necessary when the user moved to another department or
|
||||
/// left the company, or when the configuration ID is no longer valid.
|
||||
/// </remarks>
|
||||
/// <returns>
|
||||
/// Returns the empty Guid when the environment is not set or the request fails.
|
||||
/// Otherwise, the configuration ID.
|
||||
/// </returns>
|
||||
public async Task<Guid> EnterpriseEnvRemoveConfigId()
|
||||
{
|
||||
var result = await this.http.DeleteAsync("/system/enterprise/config/id");
|
||||
if (!result.IsSuccessStatusCode)
|
||||
{
|
||||
this.logger!.LogError($"Failed to query the enterprise configuration ID for removal: '{result.StatusCode}'");
|
||||
return Guid.Empty;
|
||||
}
|
||||
|
||||
Guid.TryParse(await result.Content.ReadAsStringAsync(), out var configurationId);
|
||||
return configurationId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to read the enterprise environment for the current user's configuration server URL.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// Returns null when the environment is not set or the request fails.
|
||||
/// Otherwise, the configuration server URL.
|
||||
/// </returns>
|
||||
public async Task<string> EnterpriseEnvConfigServerUrl()
|
||||
{
|
||||
var result = await this.http.GetAsync("/system/enterprise/config/server");
|
||||
if (!result.IsSuccessStatusCode)
|
||||
{
|
||||
this.logger!.LogError($"Failed to query the enterprise configuration server URL: '{result.StatusCode}'");
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var serverUrl = await result.Content.ReadAsStringAsync();
|
||||
return string.IsNullOrWhiteSpace(serverUrl) ? string.Empty : serverUrl;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to read the enterprise environment for the configuration encryption secret.
|
||||
/// </summary>
|
||||
@@ -85,4 +23,67 @@ public sealed partial class RustService
|
||||
var encryptionSecret = await result.Content.ReadAsStringAsync();
|
||||
return string.IsNullOrWhiteSpace(encryptionSecret) ? string.Empty : encryptionSecret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads all enterprise configurations (multi-config support).
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// Returns a list of enterprise environments parsed from the Rust runtime.
|
||||
/// The ETag is not yet determined; callers must resolve it separately.
|
||||
/// </returns>
|
||||
public async Task<List<EnterpriseEnvironment>> EnterpriseEnvConfigs()
|
||||
{
|
||||
var result = await this.http.GetAsync("/system/enterprise/configs");
|
||||
if (!result.IsSuccessStatusCode)
|
||||
{
|
||||
this.logger!.LogError($"Failed to query the enterprise configurations: '{result.StatusCode}'");
|
||||
return [];
|
||||
}
|
||||
|
||||
var configs = await result.Content.ReadFromJsonAsync<List<EnterpriseConfig>>(this.jsonRustSerializerOptions);
|
||||
if (configs is null)
|
||||
return [];
|
||||
|
||||
var environments = new List<EnterpriseEnvironment>();
|
||||
foreach (var config in configs)
|
||||
{
|
||||
if (Guid.TryParse(config.Id, out var id))
|
||||
environments.Add(new EnterpriseEnvironment(config.ServerUrl, id, null));
|
||||
else
|
||||
this.logger!.LogWarning($"Skipping enterprise config with invalid ID: '{config.Id}'.");
|
||||
}
|
||||
|
||||
return environments;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads all enterprise configuration IDs that should be deleted.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// Returns a list of GUIDs representing configuration IDs to remove.
|
||||
/// </returns>
|
||||
public async Task<List<Guid>> EnterpriseEnvDeleteConfigIds()
|
||||
{
|
||||
var result = await this.http.GetAsync("/system/enterprise/delete-configs");
|
||||
if (!result.IsSuccessStatusCode)
|
||||
{
|
||||
this.logger!.LogError($"Failed to query the enterprise delete configuration IDs: '{result.StatusCode}'");
|
||||
return [];
|
||||
}
|
||||
|
||||
var ids = await result.Content.ReadFromJsonAsync<List<string>>(this.jsonRustSerializerOptions);
|
||||
if (ids is null)
|
||||
return [];
|
||||
|
||||
var guids = new List<Guid>();
|
||||
foreach (var idStr in ids)
|
||||
{
|
||||
if (Guid.TryParse(idStr, out var id))
|
||||
guids.Add(id);
|
||||
else
|
||||
this.logger!.LogWarning($"Skipping invalid GUID in enterprise delete config IDs: '{idStr}'.");
|
||||
}
|
||||
|
||||
return guids;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user