import '../../../about/external/data/Docker.dart'; import '../../../about/external/data/EnvCheck.dart'; import '../../../about/internal/application/ServerProfile.dart'; import '../../parent/ParentViewModel.dart'; import 'ConnectDockerManager.dart'; class ViewDockerManager extends ParentViewModel { ConnectDockerManager connection; ViewDockerManager(super.context, this.connection); /// Reads Docker's state and, when the daemon is up, its containers. void loadDocker(ServerProfile server) async { if (!await canReach(server, () => loadDocker(server))) return; try { final status = await getDataManager().dockerStatus(server); var containers = []; if (status.installed && status.running) { containers = await getDataManager().dockerPs(server, all: true); } connection.onDockerLoaded(status, containers); } catch (error) { connection.onDockerFailed(error.toString()); } } /// start | stop | restart | rm on one container. void runAction( ServerProfile server, String containerId, String action, ) async { try { final output = await getDataManager().dockerAction( server, containerId, action, ); connection.onActionFinished(output); } catch (error) { handleError( error, () => runAction(server, containerId, action), () {}, "Retry", ); } } void loadContainerLogs( ServerProfile server, String containerId, String name, ) async { try { final output = await getDataManager().dockerLogs(server, containerId); connection.onOutputReady( '$name · logs', output.isEmpty ? '(no output)' : output, ); } catch (error) { handleError( error, () => loadContainerLogs(server, containerId, name), () {}, "Retry", ); } } /// Runs `hello-world` to prove the daemon can pull and execute an image. void runTest(ServerProfile server) async { showLoading("Running hello-world …"); try { final output = await getDataManager().dockerTest(server); closeLoading(); connection.onOutputReady('docker run --rm hello-world', output); } catch (error) { handleError(error, () => runTest(server), () {}, "Retry"); } } /// Resolves the right package-manager command for this distro, then hands /// it back so the screen can confirm before anything is installed. void prepareInstall(ServerProfile server) async { if (!await canReach(server, () => prepareInstall(server))) return; try { final manager = await getDataManager().detectPackageManager(server); if (manager == 'unknown') { connection.onDockerFailed( 'Unknown package manager — install Docker manually.', ); return; } final tool = kEnvTools.firstWhere((t) => t.key == 'docker'); connection.onInstallCommandReady( installCommandFor(manager, tool.packagesFor(manager)), ); } catch (error) { handleError(error, () => prepareInstall(server), () {}, "Retry"); } } void installDocker(ServerProfile server, String command) async { try { await getDataManager().installPackages( server: server, command: command, enableUnit: 'docker', ); connection.onInstallFinished(); } catch (error) { connection.onInstallFinished(); handleError(error, () => installDocker(server, command), () {}, "Retry"); } } }