From dc47d2f5c370ddbc530c30bff84df900dcad4040 Mon Sep 17 00:00:00 2001 From: SealabJaster Date: Tue, 10 Mar 2020 03:33:19 +0000 Subject: [PATCH] Daemon stuff that I hope works --- dub.sdl | 9 ++--- dub.selections.json | 5 ++- source/aim/daemon/commands.d | 68 ++++++++++++++++++++++++++++++++++++ source/aim/daemon/daemon.d | 45 ++++++++++++++++++++++++ source/aim/daemon/data.d | 12 +++++++ source/aim/daemon/package.d | 3 ++ source/app.d | 5 ++- views/deploy/systemd.service | 14 ++++++++ 8 files changed, 155 insertions(+), 6 deletions(-) create mode 100644 source/aim/daemon/commands.d create mode 100644 source/aim/daemon/daemon.d create mode 100644 source/aim/daemon/data.d create mode 100644 source/aim/daemon/package.d create mode 100644 views/deploy/systemd.service diff --git a/dub.sdl b/dub.sdl index b3b658f..171c298 100644 --- a/dub.sdl +++ b/dub.sdl @@ -3,11 +3,12 @@ description "A CLI tool containing a collection of subtools for AIM." authors "Bradley Chatha" copyright "Copyright © 2019, Bradley Chatha" license "MIT" -dependency "jioc" version=">=0.2.0" dependency "jcli" version=">=0.2.1" -dependency "vibe-d:inet" version="0.8.6-beta.1" -dependency "vibe-d:core" version="0.8.6-beta.1" +dependency "jioc" version=">=0.2.0" dependency "vibe-d:http" version="0.8.6-beta.1" dependency "asdf" version="~>0.4.7" +dependency "vibe-d:inet" version="0.8.6-beta.1" +dependency "vibe-d:core" version="0.8.6-beta.1" +dependency "standardpaths" version="~>0.8.1" targetType "executable" -targetPath "bin" \ No newline at end of file +targetPath "bin" diff --git a/dub.selections.json b/dub.selections.json index 9ae5acb..4d7af71 100644 --- a/dub.selections.json +++ b/dub.selections.json @@ -6,6 +6,7 @@ "botan-math": "1.0.3", "diet-ng": "1.6.1", "eventcore": "0.8.48", + "isfreedesktop": "0.1.1", "jcli": "0.2.1", "jioc": "0.2.0", "libasync": "0.8.6", @@ -13,9 +14,11 @@ "memutils": "1.0.4", "mir-linux-kernel": "1.0.1", "openssl": "1.1.6+1.0.1g", + "standardpaths": "0.8.1", "stdx-allocator": "2.77.5", "taggedalgebraic": "0.11.9", "vibe-core": "1.8.1", - "vibe-d": "0.8.6-beta.1" + "vibe-d": "0.8.6-beta.1", + "xdgpaths": "0.2.5" } } diff --git a/source/aim/daemon/commands.d b/source/aim/daemon/commands.d new file mode 100644 index 0000000..e717177 --- /dev/null +++ b/source/aim/daemon/commands.d @@ -0,0 +1,68 @@ +module aim.daemon.commands; + +private +{ + import jaster.cli; + import aim.common, aim.deploy, aim.secrets, aim.daemon; +} + +@Command("daemon run", "Runs the program in daemon mode.") +final class AimDaemonRun : BaseCommand +{ + private AimDaemon _daemon; + + this(AimDaemon daemon) + { + this._daemon = daemon; + } + + override int onExecute() + { + this._daemon.runForeverLoop(); + return 0; + } +} + +@Command("daemon watch", "Register the current directory as a deployment project to the daemon.") +final class AimDaemonWatch : BaseCommand +{ + private IAimCliConfig!AimDaemonConfig _config; + + this(IAimCliConfig!AimDaemonConfig config) + { + this._config = config; + } + + override int onExecute() + { + import std.algorithm : canFind; + import std.file : getcwd; + + this._config.edit((scope ref conf) + { + const dir = getcwd(); + + if(!conf.projectDirs.canFind(dir)) + conf.projectDirs ~= dir; + }); + return 0; + } +} + +@Command("daemon register systemd", "Creates a systemd service that runs AimCLITool in Daemon mode.") +final class AimDaemonRegisterSystemd : BaseCommand +{ + private static immutable SYSTEMD_TEMPLATE = import("deploy/systemd.service"); + private static immutable SERVICE_PATH = "/lib/systemd/system/aimd.service"; + + override int onExecute() + { + import std.file : thisExePath, write; + import aim.common.templater : Templater; + + write(SERVICE_PATH, Templater.resolveTemplate(["$AIM_PATH": thisExePath], SYSTEMD_TEMPLATE)); + Shell.executeEnforceStatusZero("systemctl start aimd"); + + return 0; + } +} \ No newline at end of file diff --git a/source/aim/daemon/daemon.d b/source/aim/daemon/daemon.d new file mode 100644 index 0000000..6f7a8ba --- /dev/null +++ b/source/aim/daemon/daemon.d @@ -0,0 +1,45 @@ +module aim.daemon.daemon; + +private +{ + import jaster.cli; + import aim.common, aim.deploy, aim.secrets, aim.daemon; +} + +final class AimDaemon +{ + private IAimCliConfig!AimDaemonConfig _daemonConfig; + + this(IAimCliConfig!AimDaemonConfig daemonConfig) + { + this._daemonConfig = daemonConfig; + } + + void runForeverLoop() + { + import core.thread : Thread; + import core.time : seconds; + + Shell.useVerboseOutput = true; + while(true) + { + tick(); + Thread.sleep(60.seconds); + this._daemonConfig.reload(); + } + } + + private void tick() + { + import std.file : exists, chdir; + + foreach(dir; this._daemonConfig.value.projectDirs) + { + if(!exists(dir)) + continue; + + chdir(dir); + Shell.execute("aim deploy trigger check -v"); + } + } +} \ No newline at end of file diff --git a/source/aim/daemon/data.d b/source/aim/daemon/data.d new file mode 100644 index 0000000..e227f36 --- /dev/null +++ b/source/aim/daemon/data.d @@ -0,0 +1,12 @@ +module aim.daemon.data; + +private +{ + import jaster.cli; + import aim.common, aim.deploy, aim.secrets, aim.daemon; +} + +struct AimDaemonConfig +{ + string[] projectDirs; +} \ No newline at end of file diff --git a/source/aim/daemon/package.d b/source/aim/daemon/package.d new file mode 100644 index 0000000..1b1426e --- /dev/null +++ b/source/aim/daemon/package.d @@ -0,0 +1,3 @@ +module aim.daemon; + +public import aim.daemon.commands, aim.daemon.daemon, aim.daemon.data; \ No newline at end of file diff --git a/source/app.d b/source/app.d index 6ceb2b0..0b8dabd 100644 --- a/source/app.d +++ b/source/app.d @@ -1,7 +1,8 @@ import jaster.cli.core, jaster.cli.util; import std.algorithm : any; -import aim.secrets, aim.common, aim.deploy; +import aim.secrets, aim.common, aim.deploy, aim.daemon; import jaster.ioc; +import standardpaths; int main(string[] args) { @@ -10,10 +11,12 @@ int main(string[] args) cliConfigure!AimSecretsConfig(AimSecretsConfig.CONF_FILE), cliConfigure!AimSecretsDefineValues(AimSecretsDefineValues.CONF_FILE), cliConfigure!AimDeployConfig(AimDeployConfig.CONF_FILE), + cliConfigure!AimDaemonConfig(writablePath(StandardPath.config, "aimcli", FolderFlag.create)), ServiceInfo.asSingleton!(IFileDownloader, FileDownloader), ServiceInfo.asScoped!(IDeployHandlerFactory, DeployHandlerFactory), ServiceInfo.asScoped!(IAimDeployAddonFactory, AimDeployAddonFactory), ServiceInfo.asScoped!(IAimDeployTriggerFactory, AimDeployTriggerFactory), + ServiceInfo.asSingleton!AimDaemon, addCommandLineInterfaceService() ]); diff --git a/views/deploy/systemd.service b/views/deploy/systemd.service new file mode 100644 index 0000000..48294a4 --- /dev/null +++ b/views/deploy/systemd.service @@ -0,0 +1,14 @@ +$PLACEHOLDERS + $AIM_PATH +$END +$FINISH_CONFIG +[Unit] +Description=Runs AimCLITool in Daemon mode. + +[Service] +Type=simple +ExecStart=$AIM_PATH +Restart=always + +[Install] +WantedBy=multi-user.target \ No newline at end of file