Install Linux updates in place and restart
The Linux app now downloads the release archive to a temporary folder, verifies its checksum, extracts it beside the install folder, swaps the bundle in with two renames (keeping <install>.previous for rollback) and relaunches. A read-only install location is reported with how to fix it. The prompt and Settings button say "Install and restart".
This commit is contained in:
@@ -155,7 +155,9 @@ class _SettingsUpdatesSectionState extends State<SettingsUpdatesSection> {
|
||||
if (_available != null)
|
||||
button(
|
||||
const Key('settings-install-update'),
|
||||
'Download update',
|
||||
_available!.platform == 'linux'
|
||||
? 'Install and restart'
|
||||
: 'Download update',
|
||||
_busy ? null : _install,
|
||||
true,
|
||||
),
|
||||
|
||||
@@ -10,6 +10,7 @@ import 'package:path_provider/path_provider.dart';
|
||||
|
||||
import '../../core/session/server_session.dart';
|
||||
import 'app_updates.dart';
|
||||
import 'linux_self_update.dart';
|
||||
|
||||
bool get supported =>
|
||||
Platform.isAndroid || Platform.isLinux || Platform.isMacOS;
|
||||
@@ -150,6 +151,27 @@ Future<void> downloadAndInstall(
|
||||
AppUpdate update, {
|
||||
void Function(double progress)? onProgress,
|
||||
}) async {
|
||||
if (Platform.isLinux) {
|
||||
// Linux installs in place: download, verify, swap the bundle, relaunch.
|
||||
final installer = LinuxSelfUpdate.current();
|
||||
final problem = await installer.problem();
|
||||
if (problem != null) throw FileSystemException(problem);
|
||||
final file = await downloadUpdate(
|
||||
update,
|
||||
await getTemporaryDirectory(),
|
||||
onProgress: onProgress,
|
||||
);
|
||||
final String executable;
|
||||
try {
|
||||
executable = await installer.install(
|
||||
file,
|
||||
versionCode: update.versionCode,
|
||||
);
|
||||
} finally {
|
||||
if (await file.exists()) await file.delete();
|
||||
}
|
||||
await LinuxSelfUpdate.relaunch(executable);
|
||||
}
|
||||
final dir = Platform.isAndroid
|
||||
? await getTemporaryDirectory()
|
||||
: (await getDownloadsDirectory() ?? await getTemporaryDirectory());
|
||||
|
||||
@@ -22,7 +22,11 @@ Future<bool?> showLaunchUpdateDialog(
|
||||
? 'macOS'
|
||||
: nativePlatform == 'linux'
|
||||
? 'Linux'
|
||||
: 'Android'} build is available${sourceName.isEmpty ? '' : ' from $sourceName'}.${nativePlatform == 'android' ? '' : ' Download and extract the archive to update the app.'}',
|
||||
: 'Android'} build is available${sourceName.isEmpty ? '' : ' from $sourceName'}.${switch (nativePlatform) {
|
||||
'linux' => ' Pipelines installs it and restarts.',
|
||||
'macos' => ' Download and extract the archive to update the app.',
|
||||
_ => '',
|
||||
}}',
|
||||
),
|
||||
actions: [
|
||||
CupertinoDialogAction(
|
||||
@@ -32,7 +36,13 @@ Future<bool?> showLaunchUpdateDialog(
|
||||
CupertinoDialogAction(
|
||||
isDefaultAction: true,
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: Text(web ? 'Refresh' : 'Download'),
|
||||
child: Text(
|
||||
web
|
||||
? 'Refresh'
|
||||
: nativePlatform == 'linux'
|
||||
? 'Install and restart'
|
||||
: 'Download',
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -48,7 +58,11 @@ Future<bool?> showLaunchUpdateDialog(
|
||||
? 'macOS'
|
||||
: nativePlatform == 'linux'
|
||||
? 'Linux'
|
||||
: 'Android'} build is available${sourceName.isEmpty ? '' : ' from $sourceName'}.${nativePlatform == 'android' ? '' : ' Download and extract the archive to update the app.'}',
|
||||
: 'Android'} build is available${sourceName.isEmpty ? '' : ' from $sourceName'}.${switch (nativePlatform) {
|
||||
'linux' => ' Pipelines installs it and restarts.',
|
||||
'macos' => ' Download and extract the archive to update the app.',
|
||||
_ => '',
|
||||
}}',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
@@ -57,7 +71,13 @@ Future<bool?> showLaunchUpdateDialog(
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: Text(web ? 'Refresh' : 'Download'),
|
||||
child: Text(
|
||||
web
|
||||
? 'Refresh'
|
||||
: nativePlatform == 'linux'
|
||||
? 'Install and restart'
|
||||
: 'Download',
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
import 'dart:io';
|
||||
|
||||
/// Installs a downloaded Linux release archive over the running app.
|
||||
///
|
||||
/// The archive holds one `pipelines-linux-x64/` bundle (executable `pipelines-app`,
|
||||
/// `data/`, `lib/`). It is extracted beside the install folder, checked, and
|
||||
/// swapped in with two renames; the previous bundle is kept as
|
||||
/// `<install>.previous` until the next update so a failed swap rolls back.
|
||||
/// The running process keeps its already-open files, so it can relaunch the
|
||||
/// new executable and exit.
|
||||
class LinuxSelfUpdate {
|
||||
LinuxSelfUpdate({
|
||||
required this.installDir,
|
||||
this.executableName = 'pipelines-app',
|
||||
Future<ProcessResult> Function(String, List<String>)? run,
|
||||
}) : _run = run ?? ((exe, args) => Process.run(exe, args));
|
||||
|
||||
/// The folder holding the running executable.
|
||||
final Directory installDir;
|
||||
final String executableName;
|
||||
final Future<ProcessResult> Function(String, List<String>) _run;
|
||||
|
||||
/// The install folder of this process.
|
||||
static LinuxSelfUpdate current() =>
|
||||
LinuxSelfUpdate(installDir: File(Platform.resolvedExecutable).parent);
|
||||
|
||||
/// Why this install cannot update itself, or null when it can.
|
||||
Future<String?> problem() async {
|
||||
final parent = installDir.parent;
|
||||
try {
|
||||
final probe = File(
|
||||
'${parent.path}/.pipelines-update-probe-${DateTime.now().microsecondsSinceEpoch}',
|
||||
);
|
||||
await probe.writeAsString('');
|
||||
await probe.delete();
|
||||
} on FileSystemException {
|
||||
return 'Pipelines is installed in ${installDir.path}, which this user '
|
||||
'cannot change. Reinstall it in your home folder to update it from '
|
||||
'the app.';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Extracts [archive] and swaps it in; returns the new executable path.
|
||||
Future<String> install(File archive, {required int versionCode}) async {
|
||||
final reason = await problem();
|
||||
if (reason != null) throw FileSystemException(reason, installDir.path);
|
||||
final parent = installDir.parent.path;
|
||||
final name = installDir.uri.pathSegments.where((s) => s.isNotEmpty).last;
|
||||
final staging = Directory('$parent/.$name-update-$versionCode');
|
||||
if (await staging.exists()) await staging.delete(recursive: true);
|
||||
await staging.create();
|
||||
try {
|
||||
final result = await _run('tar', [
|
||||
'-xzf',
|
||||
archive.path,
|
||||
'-C',
|
||||
staging.path,
|
||||
]);
|
||||
if (result.exitCode != 0) {
|
||||
throw FileSystemException(
|
||||
'The update archive could not be extracted: ${'${result.stderr}'.trim()}',
|
||||
archive.path,
|
||||
);
|
||||
}
|
||||
final bundle = await _bundleIn(staging);
|
||||
final previous = Directory('$parent/$name.previous');
|
||||
if (await previous.exists()) await previous.delete(recursive: true);
|
||||
await installDir.rename(previous.path);
|
||||
try {
|
||||
await bundle.rename(installDir.path);
|
||||
} catch (_) {
|
||||
await previous.rename(installDir.path);
|
||||
rethrow;
|
||||
}
|
||||
return '${installDir.path}/$executableName';
|
||||
} finally {
|
||||
if (await staging.exists()) await staging.delete(recursive: true);
|
||||
}
|
||||
}
|
||||
|
||||
/// The extracted bundle: the folder holding the executable and its data.
|
||||
Future<Directory> _bundleIn(Directory staging) async {
|
||||
final candidates = [
|
||||
staging,
|
||||
...await staging
|
||||
.list()
|
||||
.where((e) => e is Directory)
|
||||
.cast<Directory>()
|
||||
.toList(),
|
||||
];
|
||||
for (final dir in candidates) {
|
||||
final exe = File('${dir.path}/$executableName');
|
||||
if (await exe.exists() &&
|
||||
await Directory('${dir.path}/data/flutter_assets').exists() &&
|
||||
await Directory('${dir.path}/lib').exists()) {
|
||||
return dir;
|
||||
}
|
||||
}
|
||||
throw FileSystemException(
|
||||
'The update archive has no complete $executableName bundle.',
|
||||
staging.path,
|
||||
);
|
||||
}
|
||||
|
||||
/// Starts [executable] on its own and ends this process.
|
||||
static Future<Never> relaunch(String executable) async {
|
||||
await Process.start(
|
||||
executable,
|
||||
const [],
|
||||
mode: ProcessStartMode.detached,
|
||||
workingDirectory: File(executable).parent.path,
|
||||
);
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user