From 04db5a82b9424ab7801f0c17d62336afc542289d Mon Sep 17 00:00:00 2001 From: Junyuan Feng Date: Tue, 31 May 2022 11:02:59 +0800 Subject: [PATCH] fix docker parse error - rootless docker socket host - version & edition --- README.md | 5 +++- ios/Runner.xcodeproj/project.pbxproj | 12 ++++---- lib/data/provider/docker.dart | 40 +++++++++++++------------- lib/data/provider/server.dart | 9 ++++-- lib/data/res/build_data.dart | 8 +++--- macos/Runner.xcodeproj/project.pbxproj | 12 ++++---- pubspec.lock | 2 +- pubspec.yaml | 2 +- 8 files changed, 48 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index b019a09d..2b954450 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Server Monitor & Toolbox -A new Flutter project which provide a chart view to display server status data and a manager toolbox. +A new Flutter project which provide a chart view to display server status data and a manager toolbox. ## Milestone - [x] Status chart view @@ -71,3 +71,6 @@ Please use `make.dart` to build. ## License `LGPL License. LollipopKit 2021` + +## Thanks +Especially thanks to [xuty](https://github.com/xtyxtyx) and dartssh2. diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj index d979c284..58c1dbe7 100644 --- a/ios/Runner.xcodeproj/project.pbxproj +++ b/ios/Runner.xcodeproj/project.pbxproj @@ -354,7 +354,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; - CURRENT_PROJECT_VERSION = 149; + CURRENT_PROJECT_VERSION = 151; DEVELOPMENT_TEAM = BA88US33G6; ENABLE_BITCODE = NO; INFOPLIST_FILE = Runner/Info.plist; @@ -362,7 +362,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.0.149; + MARKETING_VERSION = 1.0.151; PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; @@ -484,7 +484,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; - CURRENT_PROJECT_VERSION = 149; + CURRENT_PROJECT_VERSION = 151; DEVELOPMENT_TEAM = BA88US33G6; ENABLE_BITCODE = NO; INFOPLIST_FILE = Runner/Info.plist; @@ -492,7 +492,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.0.149; + MARKETING_VERSION = 1.0.151; PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; @@ -508,7 +508,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; - CURRENT_PROJECT_VERSION = 149; + CURRENT_PROJECT_VERSION = 151; DEVELOPMENT_TEAM = BA88US33G6; ENABLE_BITCODE = NO; INFOPLIST_FILE = Runner/Info.plist; @@ -516,7 +516,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.0.149; + MARKETING_VERSION = 1.0.151; PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; diff --git a/lib/data/provider/docker.dart b/lib/data/provider/docker.dart index da0a639c..554a9108 100644 --- a/lib/data/provider/docker.dart +++ b/lib/data/provider/docker.dart @@ -4,7 +4,9 @@ import 'package:toolbox/core/extension/uint8list.dart'; import 'package:toolbox/core/provider_base.dart'; import 'package:toolbox/data/model/docker/ps.dart'; -final dockerNotFound = RegExp(r'command not found|Unknown command'); +final _dockerNotFound = RegExp(r'command not found|Unknown command'); +final _versionReg = RegExp(r'(Version:)\s+([0-9]+\.[0-9]+\.[0-9]+)'); +final _editionReg = RegExp(r'(Client:)\s+(.+-.+)'); class DockerProvider extends BusyProvider { SSHClient? client; @@ -31,32 +33,30 @@ class DockerProvider extends BusyProvider { } final verRaw = await client!.run('docker version'.withLangExport).string; - if (verRaw.contains(dockerNotFound)) { + if (verRaw.contains(_dockerNotFound)) { error = 'docker not found'; notifyListeners(); return; } - final verSplit = verRaw.split('\n'); - if (verSplit.length < 3) { - error = 'invalid version'; - notifyListeners(); - return; - } else { - try { - version = verSplit[1].split(' ').last; - edition = verSplit[0].split(': ')[1]; - } catch (e) { - error = e.toString(); - return; - } - } - final raw = await client!.run('docker ps -a'.withLangExport).string; - final lines = raw.split('\n'); - lines.removeAt(0); - lines.removeWhere((element) => element.isEmpty); + version = _versionReg.firstMatch(verRaw)?.group(2); + edition = _editionReg.firstMatch(verRaw)?.group(2); try { + final cmd = 'docker ps -a'.withLangExport; + final raw = await () async { + final raw = await client!.run(cmd).string; + if (raw.contains('permission denied')) { + return await client! + .run( + 'export DOCKER_HOST=unix:///run/user/1000/docker.sock && $cmd') + .string; + } + return raw; + }(); + final lines = raw.split('\n'); + lines.removeAt(0); + lines.removeWhere((element) => element.isEmpty); items = lines.map((e) => DockerPsItem.fromRawString(e)).toList(); } catch (e) { error = e.toString(); diff --git a/lib/data/provider/server.dart b/lib/data/provider/server.dart index 69f18c01..bf7401c6 100644 --- a/lib/data/provider/server.dart +++ b/lib/data/provider/server.dart @@ -41,8 +41,7 @@ const shellCmd = "export LANG=en_US.utf-8 \necho '$seperator' \n" "cat /sys/class/thermal/thermal_zone*/type \necho $seperator \n" "cat /sys/class/thermal/thermal_zone*/temp"; const shellPath = '.serverbox.sh'; -const memPrefix = 'Mem:'; -final cpuTempReg = RegExp('(x86_pkg_temp|cpu_thermal)'); +final cpuTempReg = RegExp(r'(x86_pkg_temp|cpu_thermal)'); final numReg = RegExp(r'\s{1,}'); final memItemReg = RegExp(r'([A-Z].+:)\s+([0-9]+) kB'); @@ -274,7 +273,11 @@ class ServerProvider extends BusyProvider { } idx++; } - return '${(int.parse(value.split('\n')[idx].trim()) / 1000).toStringAsFixed(1)}°C'; + final valueSplited = value.split('\n'); + if (idx >= valueSplited.length) return ''; + final temp = int.tryParse(valueSplited[idx].trim()); + if (temp == null) return ''; + return '${(temp / 1000).toStringAsFixed(1)}°C'; } void _getCPU( diff --git a/lib/data/res/build_data.dart b/lib/data/res/build_data.dart index 07090c7b..7a9fb08d 100644 --- a/lib/data/res/build_data.dart +++ b/lib/data/res/build_data.dart @@ -2,9 +2,9 @@ class BuildData { static const String name = "ServerBox"; - static const int build = 149; + static const int build = 151; static const String engine = - "Flutter 3.0.1 • channel stable • https://github.com/flutter/flutter.git\nFramework • revision fb57da5f94 (5 days ago) • 2022-05-19 15:50:29 -0700\nEngine • revision caaafc5604\nTools • Dart 2.17.1 • DevTools 2.12.2\n"; - static const String buildAt = "2022-05-25 13:35:39.361656"; - static const int modifications = 4; + "Flutter 3.0.1 • channel stable • https://github.com/flutter/flutter.git\nFramework • revision fb57da5f94 (10 days ago) • 2022-05-19 15:50:29 -0700\nEngine • revision caaafc5604\nTools • Dart 2.17.1 • DevTools 2.12.2\n"; + static const String buildAt = "2022-05-30 15:20:12.101899"; + static const int modifications = 6; } diff --git a/macos/Runner.xcodeproj/project.pbxproj b/macos/Runner.xcodeproj/project.pbxproj index 82914bcc..ee021b78 100644 --- a/macos/Runner.xcodeproj/project.pbxproj +++ b/macos/Runner.xcodeproj/project.pbxproj @@ -420,14 +420,14 @@ CODE_SIGN_ENTITLEMENTS = Runner/DebugProfile.entitlements; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 149; + CURRENT_PROJECT_VERSION = 151; DEVELOPMENT_TEAM = BA88US33G6; INFOPLIST_FILE = Runner/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/../Frameworks", ); - MARKETING_VERSION = 1.0.149; + MARKETING_VERSION = 1.0.151; PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox; PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_VERSION = 5.0; @@ -550,14 +550,14 @@ CODE_SIGN_ENTITLEMENTS = Runner/DebugProfile.entitlements; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 149; + CURRENT_PROJECT_VERSION = 151; DEVELOPMENT_TEAM = BA88US33G6; INFOPLIST_FILE = Runner/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/../Frameworks", ); - MARKETING_VERSION = 1.0.149; + MARKETING_VERSION = 1.0.151; PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox; PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; @@ -574,14 +574,14 @@ CODE_SIGN_ENTITLEMENTS = Runner/Release.entitlements; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 149; + CURRENT_PROJECT_VERSION = 151; DEVELOPMENT_TEAM = BA88US33G6; INFOPLIST_FILE = Runner/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/../Frameworks", ); - MARKETING_VERSION = 1.0.149; + MARKETING_VERSION = 1.0.151; PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox; PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_VERSION = 5.0; diff --git a/pubspec.lock b/pubspec.lock index 8c8c46d3..a85f8878 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -123,7 +123,7 @@ packages: name: dartssh2 url: "https://pub.dartlang.org" source: hosted - version: "2.7.0" + version: "2.7.1" dio: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 22748417..ceed9251 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -43,7 +43,7 @@ dependencies: git: url: https://github.com/Countly/countly-sdk-flutter-bridge.git ref: master - dartssh2: ^2.7.0 + dartssh2: ^2.7.1 logging: ^1.0.2 flutter_material_color_picker: ^1.1.0+2 circle_chart: