The Big Merge©®️™!
Integrated Mark's files, and it all works !
This commit is contained in:
parent
9ab0f54f14
commit
989eb69f1f
|
|
@ -0,0 +1,5 @@
|
||||||
|
/// Store info about a running vm, such as connection ports.
|
||||||
|
class VmInfo {
|
||||||
|
String? sshPort;
|
||||||
|
String? spicePort;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,237 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:core';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:path/path.dart' as path;
|
||||||
|
import 'package:file_picker/file_picker.dart';
|
||||||
|
import 'dart:io';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
import 'package:quickgui/src/model/vminfo.dart';
|
||||||
|
|
||||||
|
/// VM manager page.
|
||||||
|
/// Displays a list of available VMs, running state and connection info,
|
||||||
|
/// with buttons to start and stop VMs.
|
||||||
|
class Manager extends StatefulWidget {
|
||||||
|
const Manager({Key? key}) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<Manager> createState() => _ManagerState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _ManagerState extends State<Manager> {
|
||||||
|
List<String> _currentVms = [];
|
||||||
|
Map<String, VmInfo> _activeVms = {};
|
||||||
|
final List<String> _spicyVms = [];
|
||||||
|
Timer? refreshTimer;
|
||||||
|
static const String prefsWorkingDirectory = 'workingDirectory';
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_getCurrentDirectory();
|
||||||
|
Future.delayed(Duration.zero, () => _getVms(context)); // Reload VM list when we enter the page.
|
||||||
|
refreshTimer = Timer.periodic(const Duration(seconds: 5), (Timer t) {
|
||||||
|
_getVms(context);
|
||||||
|
}); // Reload VM list every 5 seconds.
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
refreshTimer?.cancel();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _saveCurrentDirectory() async {
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
prefs.setString(prefsWorkingDirectory, Directory.current.path);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _getCurrentDirectory() async {
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
if (prefs.containsKey(prefsWorkingDirectory)) {
|
||||||
|
setState(() {
|
||||||
|
final directory = prefs.getString(prefsWorkingDirectory);
|
||||||
|
if (directory != null) {
|
||||||
|
Directory.current = directory;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
VmInfo _parseVmInfo(name) {
|
||||||
|
String shellScript = File(name + '/' + name + '.sh').readAsStringSync();
|
||||||
|
RegExpMatch? sshMatch = RegExp('hostfwd=tcp::(\\d+?)-:22').firstMatch(shellScript);
|
||||||
|
RegExpMatch? spiceMatch = RegExp('-spice.+?port=(\\d+)').firstMatch(shellScript);
|
||||||
|
VmInfo info = VmInfo();
|
||||||
|
if (sshMatch != null) {
|
||||||
|
info.sshPort = sshMatch.group(1);
|
||||||
|
}
|
||||||
|
if (spiceMatch != null) {
|
||||||
|
info.spicePort = spiceMatch.group(1);
|
||||||
|
}
|
||||||
|
return info;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _getVms(context) async {
|
||||||
|
List<String> currentVms = [];
|
||||||
|
Map<String, VmInfo> activeVms = {};
|
||||||
|
|
||||||
|
await for (var entity in Directory.current.list(recursive: false, followLinks: true)) {
|
||||||
|
if (entity.path.endsWith('.conf')) {
|
||||||
|
String name = path.basenameWithoutExtension(entity.path);
|
||||||
|
currentVms.add(name);
|
||||||
|
File pidFile = File(name + '/' + name + '.pid');
|
||||||
|
if (pidFile.existsSync()) {
|
||||||
|
String pid = pidFile.readAsStringSync().trim();
|
||||||
|
Directory procDir = Directory('/proc/' + pid);
|
||||||
|
if (procDir.existsSync()) {
|
||||||
|
if (_activeVms.containsKey(name)) {
|
||||||
|
activeVms[name] = _activeVms[name]!;
|
||||||
|
} else {
|
||||||
|
activeVms[name] = _parseVmInfo(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currentVms.sort();
|
||||||
|
setState(() {
|
||||||
|
_currentVms = currentVms;
|
||||||
|
_activeVms = activeVms;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _buildVmList() {
|
||||||
|
List<Widget> _widgetList = [];
|
||||||
|
_widgetList.add(TextButton(
|
||||||
|
onPressed: () async {
|
||||||
|
String? result = await FilePicker.platform.getDirectoryPath();
|
||||||
|
if (result != null) {
|
||||||
|
Directory.current = result;
|
||||||
|
_saveCurrentDirectory();
|
||||||
|
_getVms(context);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
child: Text(Directory.current.path)));
|
||||||
|
List<List<Widget>> rows = _currentVms.map((vm) {
|
||||||
|
return _buildRow(vm);
|
||||||
|
}).toList();
|
||||||
|
for (var row in rows) {
|
||||||
|
_widgetList.addAll(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ListView(
|
||||||
|
padding: const EdgeInsets.all(16.0),
|
||||||
|
children: _widgetList,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Widget> _buildRow(String currentVm) {
|
||||||
|
final bool active = _activeVms.containsKey(currentVm);
|
||||||
|
final bool spicy = _spicyVms.contains(currentVm);
|
||||||
|
String connectInfo = '';
|
||||||
|
if (active) {
|
||||||
|
VmInfo vmInfo = _activeVms[currentVm]!;
|
||||||
|
if (vmInfo.sshPort != null) {
|
||||||
|
connectInfo += 'SSH port: ' + vmInfo.sshPort! + ' ';
|
||||||
|
}
|
||||||
|
if (vmInfo.spicePort != null) {
|
||||||
|
connectInfo += 'SPICE port: ' + vmInfo.spicePort! + ' ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return <Widget>[
|
||||||
|
ListTile(
|
||||||
|
title: Text(currentVm),
|
||||||
|
trailing: Row(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: <Widget>[
|
||||||
|
IconButton(
|
||||||
|
icon: Icon(Icons.monitor, color: spicy ? Colors.red : null, semanticLabel: spicy ? 'Using SPICE display' : 'Click to use SPICE display'),
|
||||||
|
tooltip: spicy ? 'Using SPICE display' : 'Use SPICE display',
|
||||||
|
onPressed: () {
|
||||||
|
if (spicy) {
|
||||||
|
setState(() {
|
||||||
|
_spicyVms.remove(currentVm);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
setState(() {
|
||||||
|
_spicyVms.add(currentVm);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
IconButton(
|
||||||
|
icon: Icon(
|
||||||
|
active ? Icons.play_arrow : Icons.play_arrow_outlined,
|
||||||
|
color: active ? Colors.green : null,
|
||||||
|
semanticLabel: active ? 'Running' : 'Run',
|
||||||
|
),
|
||||||
|
onPressed: () async {
|
||||||
|
if (!active) {
|
||||||
|
Map<String, VmInfo> activeVms = _activeVms;
|
||||||
|
List<String> args = ['--vm', currentVm + '.conf'];
|
||||||
|
if (spicy) {
|
||||||
|
args.addAll(['--display', 'spice']);
|
||||||
|
}
|
||||||
|
await Process.start('quickemu', args);
|
||||||
|
VmInfo info = _parseVmInfo(currentVm);
|
||||||
|
activeVms[currentVm] = info;
|
||||||
|
setState(() {
|
||||||
|
_activeVms = activeVms;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
IconButton(
|
||||||
|
icon: Icon(
|
||||||
|
active ? Icons.stop : Icons.stop_outlined,
|
||||||
|
color: active ? Colors.red : null,
|
||||||
|
semanticLabel: active ? 'Stop' : 'Not running',
|
||||||
|
),
|
||||||
|
onPressed: () {
|
||||||
|
if (active) {
|
||||||
|
showDialog<bool>(
|
||||||
|
context: context,
|
||||||
|
builder: (BuildContext context) => AlertDialog(
|
||||||
|
title: const Text('Stop The Virtual Machine?'),
|
||||||
|
content: Text('You are about to terminate the virtual machine $currentVm'),
|
||||||
|
actions: <Widget>[
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.pop(context, false),
|
||||||
|
child: const Text('Cancel'),
|
||||||
|
),
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.pop(context, true),
|
||||||
|
child: const Text('OK'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
).then((result) {
|
||||||
|
result = result ?? false;
|
||||||
|
if (result) {
|
||||||
|
Process.run('killall', [currentVm]);
|
||||||
|
setState(() {
|
||||||
|
_activeVms.remove(currentVm);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)),
|
||||||
|
if (connectInfo.isNotEmpty)
|
||||||
|
ListTile(
|
||||||
|
title: Text(connectInfo),
|
||||||
|
),
|
||||||
|
const Divider()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(
|
||||||
|
title: const Text('Manager'),
|
||||||
|
),
|
||||||
|
body: _buildVmList(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:quickgui/src/pages/downloader_page.dart';
|
import 'package:quickgui/src/pages/downloader_page.dart';
|
||||||
|
import 'package:quickgui/src/pages/manager.dart';
|
||||||
import 'package:quickgui/src/widgets/home_page/home_page_button.dart';
|
import 'package:quickgui/src/widgets/home_page/home_page_button.dart';
|
||||||
|
|
||||||
class MainMenu extends StatelessWidget {
|
class MainMenu extends StatelessWidget {
|
||||||
|
|
@ -16,7 +17,15 @@ class MainMenu extends StatelessWidget {
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
children: [
|
children: [
|
||||||
HomePageButton(
|
HomePageButton(
|
||||||
onPressed: () {},
|
onPressed: () {
|
||||||
|
Navigator.of(context).push(
|
||||||
|
PageRouteBuilder(
|
||||||
|
fullscreenDialog: true,
|
||||||
|
pageBuilder: (context, animation1, animation2) => const Manager(),
|
||||||
|
transitionDuration: Duration.zero,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
text: 'Manage existing machines',
|
text: 'Manage existing machines',
|
||||||
),
|
),
|
||||||
HomePageButton(
|
HomePageButton(
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@ dependencies:
|
||||||
quiver: ^3.0.1+1
|
quiver: ^3.0.1+1
|
||||||
tuple: ^2.0.0
|
tuple: ^2.0.0
|
||||||
file_picker: ^4.1.6
|
file_picker: ^4.1.6
|
||||||
|
shared_preferences: ^2.0.8
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue