Skip to content
This repository has been archived by the owner on Oct 23, 2020. It is now read-only.

Commit

Permalink
Daemon stuff that I hope works
Browse files Browse the repository at this point in the history
  • Loading branch information
BradleyChatha committed Mar 10, 2020
1 parent 1518b57 commit dc47d2f
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 6 deletions.
9 changes: 5 additions & 4 deletions dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
targetPath "bin"
5 changes: 4 additions & 1 deletion dub.selections.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
"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",
"libevent": "2.0.2+2.0.16",
"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"
}
}
68 changes: 68 additions & 0 deletions source/aim/daemon/commands.d
Original file line number Diff line number Diff line change
@@ -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;
}
}
45 changes: 45 additions & 0 deletions source/aim/daemon/daemon.d
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
12 changes: 12 additions & 0 deletions source/aim/daemon/data.d
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 3 additions & 0 deletions source/aim/daemon/package.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module aim.daemon;

public import aim.daemon.commands, aim.daemon.daemon, aim.daemon.data;
5 changes: 4 additions & 1 deletion source/app.d
Original file line number Diff line number Diff line change
@@ -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)
{
Expand All @@ -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()
]);

Expand Down
14 changes: 14 additions & 0 deletions views/deploy/systemd.service
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit dc47d2f

Please sign in to comment.