APT/Docker manage

- view apt update
- view docker container
This commit is contained in:
Junyuan Feng
2022-03-08 14:47:57 +08:00
parent b800bd91fd
commit 34e6b99297
20 changed files with 519 additions and 42 deletions

View File

@@ -11,13 +11,13 @@ class MenuItem {
}
class MenuItems {
static const List<MenuItem> firstItems = [ssh, sftp, snippet, apt];
static const List<MenuItem> firstItems = [sftp, snippet, apt, docker];
static const List<MenuItem> secondItems = [edit];
static const ssh = MenuItem(text: 'SSH', icon: Icons.link);
static const sftp = MenuItem(text: 'SFTP', icon: Icons.insert_drive_file);
static const snippet = MenuItem(text: 'Snippet', icon: Icons.label);
static const apt = MenuItem(text: 'Apt', icon: Icons.system_security_update);
static const docker = MenuItem(text: 'Docker', icon: Icons.view_agenda);
static const edit = MenuItem(text: 'Edit', icon: Icons.edit);
static Widget buildItem(MenuItem item) {

View File

@@ -0,0 +1,33 @@
import 'package:toolbox/data/model/distribution.dart';
class AptUpgradePkgInfo {
final String _raw;
final Distribution _dist;
late String package;
late String nowVersion;
late String newVersion;
late String arch;
AptUpgradePkgInfo(this._raw, this._dist) {
switch (_dist) {
case Distribution.debian:
case Distribution.unknown:
_parseApt();
break;
case Distribution.rehl:
_parseYum();
}
}
void _parseApt() {
final split1 = _raw.split("/");
package = split1[0];
final split2 = split1[1].split(" ");
newVersion = split2[1];
arch = split2[2];
nowVersion = split2[5].replaceFirst(']', '');
}
void _parseYum() {}
}

View File

@@ -0,0 +1,21 @@
enum Distribution {
unknown,
debian,
rehl,
}
const debianDistList = [
'debian',
'ubuntu',
'linuxmint',
'elementary',
'raspbian'
];
const rehlDistList = [
'redhat',
'fedora',
'centos',
'scientificlinux',
'rhel',
'oraclelinux'
];

View File

@@ -0,0 +1,29 @@
class DockerPsItem {
late String containerId;
late String image;
late String command;
late String created;
late String status;
late String ports;
late String name;
DockerPsItem(this.containerId, this.image, this.command, this.created,
this.status, this.ports, this.name);
DockerPsItem.fromRawString(String rawString) {
final List<String> parts = rawString.split(' ');
containerId = parts[0];
image = parts[1];
command = parts[2];
created = parts[3];
status = parts[4];
ports = parts[5];
if (running && parts.length == 9) {
name = parts[8];
} else {
name = parts[6];
}
}
bool get running => status.contains('Up ');
}

View File

@@ -13,6 +13,8 @@ class SFTPSideViewStatus {
AbsolutePath? rightPath;
SftpClient? leftClient;
SftpClient? rightClient;
bool isBusyLeft = false;
bool isBusyRight = false;
SFTPSideViewStatus();
@@ -36,4 +38,8 @@ class SFTPSideViewStatus {
SftpClient? client(bool left) => left ? leftClient : rightClient;
void setClient(bool left, SftpClient? nClient) =>
left ? leftClient = nClient : rightClient = nClient;
bool isBusy(bool left) => left ? isBusyLeft : isBusyRight;
void setBusy(bool left, bool nBusy) =>
left ? isBusyLeft = nBusy : isBusyRight = nBusy;
}

View 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();
// }
}

View 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();
}
}

View File

@@ -2,9 +2,9 @@
class BuildData {
static const String name = "ServerBox";
static const int build = 101;
static const int build = 102;
static const String engine =
"Flutter 2.8.1 • channel stable • https://github.com/flutter/flutter.git\nFramework • revision 77d935af4d (3 months ago) • 2021-12-16 08:37:33 -0800\nEngine • revision 890a5fca2e\nTools • Dart 2.15.1\n";
static const String buildAt = "2022-03-02 11:12:07.958841";
static const int modifications = 4;
"Flutter 2.10.3 • channel stable • https://github.com/flutter/flutter.git\nFramework • revision 7e9793dee1 (5 days ago) • 2022-03-02 11:23:12 -0600\nEngine • revision bd539267b4\nTools • Dart 2.16.1 • DevTools 2.9.2\n";
static const String buildAt = "2022-03-07 19:19:07.115966";
static const int modifications = 18;
}