1 /**
2 Copyright: Copyright (c) 2019, Joakim Brännström. All rights reserved.
3 License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 */
6 module distssh.types;
7 
8 import core.thread : Thread;
9 import std.datetime : SysTime, dur;
10 
11 public import my.path : Path;
12 
13 immutable globalEnvHostKey = "DISTSSH_HOSTS";
14 immutable globalEnvFileKey = "DISTSSH_IMPORT_ENV";
15 immutable globalEnvFilterKey = "DISTSSH_ENV_EXPORT_FILTER";
16 immutable globalEnvPurge = "DISTSSH_AUTO_PURGE";
17 immutable globalEnvPurgeWhiteList = "DISTSSH_PURGE_WLIST";
18 immutable distShell = "distshell";
19 immutable distCmd = "distcmd";
20 immutable distsshEnvExport = "distssh_env.export";
21 // arguments to ssh that turn off warning that a host key is new or requies a password to login
22 immutable sshNoLoginArgs = [
23     "-oStrictHostKeyChecking=no", "-oPasswordAuthentication=no"
24 ];
25 immutable ulong defaultTimeout_s = 2;
26 /// Number of the top X candidates to choose a server from to put the work on.
27 immutable topCandidades = 3;
28 
29 @safe:
30 
31 struct HostLoad {
32     Host host;
33     Load load;
34     /// When the host was last updated
35     SysTime updated;
36 }
37 
38 struct Host {
39     string payload;
40     alias payload this;
41 }
42 
43 /// The load of a host.
44 struct Load {
45     import std.datetime : Duration;
46 
47     double loadAvg = int.max;
48     Duration accessTime = 1.dur!"hours";
49     bool unknown = true;
50 
51     bool opEquals(const typeof(this) o) nothrow @safe pure @nogc {
52         if (unknown && o.unknown)
53             return true;
54         return loadAvg == o.loadAvg && accessTime == o.accessTime;
55     }
56 
57     int opCmp(const typeof(this) o) pure @safe @nogc nothrow {
58         if (unknown && o.unknown)
59             return 0;
60         else if (unknown)
61             return 1;
62         else if (o.unknown)
63             return -1;
64 
65         if (loadAvg < o.loadAvg)
66             return -1;
67         else if (loadAvg > o.loadAvg)
68             return 1;
69 
70         if (accessTime < o.accessTime)
71             return -1;
72         else if (accessTime > o.accessTime)
73             return 1;
74 
75         return this == o ? 0 : 1;
76     }
77 }
78 
79 @("shall sort the loads")
80 unittest {
81     import std.algorithm : sort;
82     import std.array : array;
83     import core.time : dur;
84     import std.conv : to;
85 
86     {
87         auto raw = [
88             Load(0.6, 500.dur!"msecs", false), Load(0.5, 500.dur!"msecs", false)
89         ].sort.array;
90         assert(raw[0].loadAvg == 0.5, raw[0].to!string);
91     }
92 
93     {
94         auto raw = [
95             Load(0.5, 600.dur!"msecs", false), Load(0.5, 500.dur!"msecs", false)
96         ].sort.array;
97         assert(raw[0].accessTime == 500.dur!"msecs");
98     }
99 }
100 
101 /// Mirror of an environment.
102 struct Env {
103     string[string] payload;
104     alias payload this;
105 }