feat: add Control Room with containers and stacks management
- Add Control Room page with stacks sidebar and containers list - Implement container actions (start/stop/restart) with snackbars - Add container logs viewer dialog - Add search/filter for both stacks and containers lists - Add external links for Portainer and Netdata (url_launcher) - Add VS Code launch configuration for Flutter web debugging 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.5
parent
dd6bcbdbda
commit
3b89ed8c18
@@ -0,0 +1,73 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
import '../../../../../core/api/api_client.dart';
|
||||
import '../models/container_model.dart';
|
||||
|
||||
part 'containers_datasource.g.dart';
|
||||
|
||||
/// Remote data source for container operations.
|
||||
class ContainersDatasource {
|
||||
ContainersDatasource(this._dio);
|
||||
|
||||
final Dio _dio;
|
||||
|
||||
/// Gets all containers from the API.
|
||||
Future<List<ContainerModel>> getContainers({bool all = true}) async {
|
||||
final response = await _dio.get<List<dynamic>>(
|
||||
'/infrastructure/containers',
|
||||
queryParameters: {'all': all},
|
||||
);
|
||||
|
||||
return response.data!
|
||||
.map((json) => ContainerModel.fromJson(json as Map<String, dynamic>))
|
||||
.toList();
|
||||
}
|
||||
|
||||
/// Gets a single container by ID.
|
||||
Future<ContainerModel> getContainer(String id) async {
|
||||
final response = await _dio.get<Map<String, dynamic>>(
|
||||
'/infrastructure/containers/$id',
|
||||
);
|
||||
|
||||
return ContainerModel.fromJson(response.data!);
|
||||
}
|
||||
|
||||
/// Performs an action on a container.
|
||||
Future<void> containerAction(String id, String action) async {
|
||||
await _dio.post<void>('/infrastructure/containers/$id/$action');
|
||||
}
|
||||
|
||||
/// Gets container logs.
|
||||
Future<String> getContainerLogs(
|
||||
String id, {
|
||||
int? tail,
|
||||
bool timestamps = false,
|
||||
}) async {
|
||||
final response = await _dio.get<String>(
|
||||
'/infrastructure/containers/$id/logs',
|
||||
queryParameters: {
|
||||
if (tail != null) 'tail': tail,
|
||||
'timestamps': timestamps,
|
||||
},
|
||||
);
|
||||
|
||||
return response.data ?? '';
|
||||
}
|
||||
|
||||
/// Removes a container.
|
||||
Future<void> removeContainer(String id, {bool force = false}) async {
|
||||
await _dio.delete<void>(
|
||||
'/infrastructure/containers/$id',
|
||||
queryParameters: {'force': force},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Provides the containers datasource.
|
||||
@riverpod
|
||||
ContainersDatasource containersDatasource(Ref ref) {
|
||||
final dio = ref.watch(coreApiClientProvider);
|
||||
return ContainersDatasource(dio);
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
|
||||
import '../../domain/entities/container.dart';
|
||||
|
||||
part 'container_model.freezed.dart';
|
||||
part 'container_model.g.dart';
|
||||
|
||||
/// Container data model for API serialization.
|
||||
@freezed
|
||||
class ContainerModel with _$ContainerModel {
|
||||
const factory ContainerModel({
|
||||
@JsonKey(name: 'Id') required String id,
|
||||
@JsonKey(name: 'Names') required List<String> names,
|
||||
@JsonKey(name: 'Image') required String image,
|
||||
@JsonKey(name: 'State') required String state,
|
||||
@JsonKey(name: 'Status') required String status,
|
||||
@JsonKey(name: 'Labels') @Default({}) Map<String, String> labels,
|
||||
@JsonKey(name: 'Ports') @Default([]) List<PortModel> ports,
|
||||
@JsonKey(name: 'Mounts') @Default([]) List<MountModel> mounts,
|
||||
@JsonKey(name: 'NetworkSettings') NetworkSettingsModel? networkSettings,
|
||||
@JsonKey(name: 'Created') int? created,
|
||||
@JsonKey(name: 'SizeRw') int? sizeRw,
|
||||
@JsonKey(name: 'SizeRootFs') int? sizeRootFs,
|
||||
}) = _ContainerModel;
|
||||
|
||||
const ContainerModel._();
|
||||
|
||||
factory ContainerModel.fromJson(Map<String, dynamic> json) =>
|
||||
_$ContainerModelFromJson(json);
|
||||
|
||||
/// Converts to domain entity.
|
||||
Container toEntity() {
|
||||
// Extract stack name from labels (Docker Compose convention)
|
||||
final stackName = labels['com.docker.compose.project'];
|
||||
final stackId = stackName; // Use project name as ID for now
|
||||
|
||||
return Container(
|
||||
id: id.substring(0, 12),
|
||||
fullId: id,
|
||||
name: names.isNotEmpty ? names.first.replaceFirst('/', '') : id,
|
||||
image: image,
|
||||
state: _parseState(state),
|
||||
status: status,
|
||||
stackName: stackName,
|
||||
stackId: stackId,
|
||||
ports: ports.map((p) => p.toEntity()).toList(),
|
||||
labels: labels,
|
||||
mounts: mounts.map((m) => m.toEntity()).toList(),
|
||||
networks: networkSettings?.networks.keys.toList() ?? [],
|
||||
createdAt: created != null
|
||||
? DateTime.fromMillisecondsSinceEpoch(created! * 1000)
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
ContainerState _parseState(String state) {
|
||||
return switch (state.toLowerCase()) {
|
||||
'created' => ContainerState.created,
|
||||
'running' => ContainerState.running,
|
||||
'paused' => ContainerState.paused,
|
||||
'restarting' => ContainerState.restarting,
|
||||
'removing' => ContainerState.removing,
|
||||
'exited' => ContainerState.exited,
|
||||
'dead' => ContainerState.dead,
|
||||
_ => ContainerState.exited,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// Port mapping model.
|
||||
@freezed
|
||||
class PortModel with _$PortModel {
|
||||
const factory PortModel({
|
||||
@JsonKey(name: 'IP') String? ip,
|
||||
@JsonKey(name: 'PrivatePort') required int privatePort,
|
||||
@JsonKey(name: 'PublicPort') int? publicPort,
|
||||
@JsonKey(name: 'Type') @Default('tcp') String type,
|
||||
}) = _PortModel;
|
||||
|
||||
const PortModel._();
|
||||
|
||||
factory PortModel.fromJson(Map<String, dynamic> json) =>
|
||||
_$PortModelFromJson(json);
|
||||
|
||||
PortMapping toEntity() {
|
||||
return PortMapping(
|
||||
hostIp: ip,
|
||||
hostPort: publicPort,
|
||||
containerPort: privatePort,
|
||||
protocol: type,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Mount model.
|
||||
@freezed
|
||||
class MountModel with _$MountModel {
|
||||
const factory MountModel({
|
||||
@JsonKey(name: 'Type') required String type,
|
||||
@JsonKey(name: 'Source') required String source,
|
||||
@JsonKey(name: 'Destination') required String destination,
|
||||
@JsonKey(name: 'Mode') @Default('rw') String mode,
|
||||
@JsonKey(name: 'RW') @Default(true) bool rw,
|
||||
}) = _MountModel;
|
||||
|
||||
const MountModel._();
|
||||
|
||||
factory MountModel.fromJson(Map<String, dynamic> json) =>
|
||||
_$MountModelFromJson(json);
|
||||
|
||||
VolumeMount toEntity() {
|
||||
return VolumeMount(
|
||||
type: type,
|
||||
source: source,
|
||||
destination: destination,
|
||||
mode: rw ? 'rw' : 'ro',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Network settings model.
|
||||
@freezed
|
||||
class NetworkSettingsModel with _$NetworkSettingsModel {
|
||||
const factory NetworkSettingsModel({
|
||||
@JsonKey(name: 'Networks') @Default({}) Map<String, dynamic> networks,
|
||||
}) = _NetworkSettingsModel;
|
||||
|
||||
factory NetworkSettingsModel.fromJson(Map<String, dynamic> json) =>
|
||||
_$NetworkSettingsModelFromJson(json);
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
import '../../domain/entities/container.dart';
|
||||
import '../../domain/repositories/container_repository.dart';
|
||||
import '../datasources/containers_datasource.dart';
|
||||
|
||||
part 'container_repository_impl.g.dart';
|
||||
|
||||
/// Implementation of ContainerRepository using remote datasource.
|
||||
class ContainerRepositoryImpl implements ContainerRepository {
|
||||
ContainerRepositoryImpl(this._datasource);
|
||||
|
||||
final ContainersDatasource _datasource;
|
||||
|
||||
@override
|
||||
Future<List<Container>> getContainers() async {
|
||||
final models = await _datasource.getContainers();
|
||||
return models.map((m) => m.toEntity()).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<Container>> getContainersByStack(String stackId) async {
|
||||
final containers = await getContainers();
|
||||
return containers.where((c) => c.stackId == stackId).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Container> getContainer(String id) async {
|
||||
final model = await _datasource.getContainer(id);
|
||||
return model.toEntity();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> startContainer(String id) async {
|
||||
await _datasource.containerAction(id, 'start');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> stopContainer(String id) async {
|
||||
await _datasource.containerAction(id, 'stop');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> restartContainer(String id) async {
|
||||
await _datasource.containerAction(id, 'restart');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> pauseContainer(String id) async {
|
||||
await _datasource.containerAction(id, 'pause');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> unpauseContainer(String id) async {
|
||||
await _datasource.containerAction(id, 'unpause');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> removeContainer(String id, {bool force = false}) async {
|
||||
await _datasource.removeContainer(id, force: force);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> getContainerLogs(
|
||||
String id, {
|
||||
int? tail,
|
||||
bool timestamps = false,
|
||||
}) async {
|
||||
return _datasource.getContainerLogs(id, tail: tail, timestamps: timestamps);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<String> streamContainerLogs(String id, {bool timestamps = false}) {
|
||||
// TODO: Implement WebSocket/SSE streaming
|
||||
throw UnimplementedError('Log streaming not yet implemented');
|
||||
}
|
||||
}
|
||||
|
||||
/// Provides the container repository.
|
||||
@riverpod
|
||||
ContainerRepository containerRepository(ContainerRepositoryRef ref) {
|
||||
final datasource = ref.watch(containersDatasourceProvider);
|
||||
return ContainerRepositoryImpl(datasource);
|
||||
}
|
||||
Reference in New Issue
Block a user