diff --git a/lib/src/commands/deploy/systemd.dart b/lib/src/commands/deploy/systemd.dart index 4d4114b..28c496a 100644 --- a/lib/src/commands/deploy/systemd.dart +++ b/lib/src/commands/deploy/systemd.dart @@ -14,6 +14,8 @@ class SystemdCommand extends Command { SystemdCommand() { argParser + ..addOption('install', + abbr: 'i', help: 'A name to install this service as on the system.') ..addOption('user', abbr: 'u', defaultsTo: 'web', @@ -45,8 +47,34 @@ WantedBy=multi-user.target ''' .trim(); - if (!argResults.wasParsed('out')) { + if (!argResults.wasParsed('out') && !argResults.wasParsed('install')) { print(systemdText); + } else if (argResults.wasParsed('install')) { + var systemdPath = argResults.wasParsed('out') + ? argResults['out'] as String + : p.join('etc', 'systemd', 'system'); + var serviceFilename = p.join(systemdPath, + p.setExtension(argResults['install'] as String, '.service')); + var file = new File(serviceFilename); + await file.create(recursive: true); + await file.writeAsString(systemdText); + print(green.wrap( + "$checkmark Successfully generated systemd service in '${file.path}'.")); + + // sudo systemctl daemon-reload + if (await runCommand('sudo', ['systemctl', 'daemon-reload'])) { + // sudo service start + if (await runCommand('sudo', [ + 'service', + p.basenameWithoutExtension(serviceFilename), + 'start' + ])) { + } else { + print(red.wrap('$ballot Failed to install service system-wide.')); + } + } else { + print(red.wrap('$ballot Failed to install service system-wide.')); + } } else { var file = new File(argResults['out'] as String); await file.create(recursive: true); diff --git a/lib/src/util.dart b/lib/src/util.dart index 09ca3a5..f145d2a 100644 --- a/lib/src/util.dart +++ b/lib/src/util.dart @@ -25,3 +25,26 @@ Future savePubspec(Pubspec pubspec) async { // TODO: Save pubspec for real? //var text = toYamlString(pubspec); } + +Future runCommand(String exec, List args) async { + var s = '$exec ${args.join(' ')}'.trim(); + stdout.write(darkGray.wrap('Running `$s`... ')); + + try { + var p = await Process.start(exec, args); + var code = await p.exitCode; + + if (code == 0) { + print(green.wrap(checkmark)); + return true; + } else { + print(red.wrap(ballot)); + await stdout.addStream(p.stdout); + await stderr.addStream(p.stderr); + return false; + } + } catch (e) { + print(red.wrap('$ballot Failed to run process.')); + return false; + } +}