mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-18 15:54:35 +01:00
APT/Docker manage
- view apt update - view docker container
This commit is contained in:
48
lib/data/provider/apt.dart
Normal file
48
lib/data/provider/apt.dart
Normal file
@@ -0,0 +1,48 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dartssh2/dartssh2.dart';
|
||||
import 'package:toolbox/core/provider_base.dart';
|
||||
import 'package:toolbox/data/model/apt/upgrade_pkg_info.dart';
|
||||
import 'package:toolbox/data/model/distribution.dart';
|
||||
|
||||
class AptProvider extends BusyProvider {
|
||||
SSHClient? client;
|
||||
Distribution? dist;
|
||||
List<AptUpgradePkgInfo>? upgradeable;
|
||||
|
||||
AptProvider();
|
||||
|
||||
void init(SSHClient client, Distribution dist) {
|
||||
this.client = client;
|
||||
this.dist = dist;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
client = null;
|
||||
dist = null;
|
||||
upgradeable = null;
|
||||
}
|
||||
|
||||
bool get isReady {
|
||||
return upgradeable != null && !isBusy;
|
||||
}
|
||||
|
||||
Future<void> refreshInstalled() async {
|
||||
await update();
|
||||
final result = utf8.decode(await client!.run('apt list --upgradeable'));
|
||||
final list = result.split('\n').sublist(4);
|
||||
list.removeWhere((element) => element.isEmpty);
|
||||
upgradeable = list.map((e) => AptUpgradePkgInfo(e, dist!)).toList();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> update() async {
|
||||
await client!.run('apt update');
|
||||
}
|
||||
|
||||
// Future<void> upgrade() async {
|
||||
// setBusyState();
|
||||
// await client!.run('apt upgrade -y');
|
||||
// refreshInstalled();
|
||||
// }
|
||||
}
|
||||
63
lib/data/provider/docker.dart
Normal file
63
lib/data/provider/docker.dart
Normal file
@@ -0,0 +1,63 @@
|
||||
import 'package:dartssh2/dartssh2.dart';
|
||||
import 'package:toolbox/core/extension/uint8list.dart';
|
||||
import 'package:toolbox/core/provider_base.dart';
|
||||
import 'package:toolbox/data/model/docker/ps.dart';
|
||||
|
||||
class DockerProvider extends BusyProvider {
|
||||
SSHClient? client;
|
||||
List<DockerPsItem>? running;
|
||||
String? version;
|
||||
String? edition;
|
||||
String? error;
|
||||
|
||||
void init(SSHClient client) => this.client = client;
|
||||
|
||||
void clear() {
|
||||
client = null;
|
||||
error = null;
|
||||
running = null;
|
||||
version = null;
|
||||
edition = null;
|
||||
}
|
||||
|
||||
Future<void> refresh() async {
|
||||
if (client == null) {
|
||||
error = 'no client';
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
|
||||
final verRaw = await client!.run('docker version').string;
|
||||
final verSplit = verRaw.split('\n');
|
||||
if (verSplit.length < 3) {
|
||||
error = 'invalid version';
|
||||
notifyListeners();
|
||||
} else {
|
||||
version = verSplit[1].split(' ').last;
|
||||
edition = verSplit[0].split(': ')[1];
|
||||
}
|
||||
|
||||
final raw = await client!.run('docker ps -a').string;
|
||||
if (raw.contains('command not found')) {
|
||||
error = 'docker not found';
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
final lines = raw.split('\n');
|
||||
lines.removeAt(0);
|
||||
lines.removeWhere((element) => element.isEmpty);
|
||||
running = lines.map((e) => DockerPsItem.fromRawString(e)).toList();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> stop(String id) async {
|
||||
if (client == null) {
|
||||
error = 'no client';
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
final result = await client!.run('docker stop $id').string;
|
||||
await refresh();
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user