systemd deployment can install directly

This commit is contained in:
Tobe O 2018-10-04 11:54:59 -04:00
parent 1158feb81d
commit 8a89f380e7
2 changed files with 52 additions and 1 deletions

View file

@ -14,6 +14,8 @@ class SystemdCommand extends Command {
SystemdCommand() { SystemdCommand() {
argParser argParser
..addOption('install',
abbr: 'i', help: 'A name to install this service as on the system.')
..addOption('user', ..addOption('user',
abbr: 'u', abbr: 'u',
defaultsTo: 'web', defaultsTo: 'web',
@ -45,8 +47,34 @@ WantedBy=multi-user.target
''' '''
.trim(); .trim();
if (!argResults.wasParsed('out')) { if (!argResults.wasParsed('out') && !argResults.wasParsed('install')) {
print(systemdText); 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 <name> 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 { } else {
var file = new File(argResults['out'] as String); var file = new File(argResults['out'] as String);
await file.create(recursive: true); await file.create(recursive: true);

View file

@ -25,3 +25,26 @@ Future savePubspec(Pubspec pubspec) async {
// TODO: Save pubspec for real? // TODO: Save pubspec for real?
//var text = toYamlString(pubspec); //var text = toYamlString(pubspec);
} }
Future<bool> runCommand(String exec, List<String> 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;
}
}