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 public import std.typecons : Tuple, tuple;
9 
10 alias HostLoad = Tuple!(Host, Load);
11 
12 immutable globalEnvHostKey = "DISTSSH_HOSTS";
13 immutable globalEnvFileKey = "DISTSSH_IMPORT_ENV";
14 immutable globalEnvFilterKey = "DISTSSH_ENV_EXPORT_FILTER";
15 immutable distShell = "distshell";
16 immutable distCmd = "distcmd";
17 immutable distsshEnvExport = "distssh_env.export";
18 // arguments to ssh that turn off warning that a host key is new or requies a password to login
19 immutable sshNoLoginArgs = [
20     "-oStrictHostKeyChecking=no", "-oPasswordAuthentication=no"
21 ];
22 immutable ulong defaultTimeout_s = 2;
23 
24 struct Host {
25     string payload;
26     alias payload this;
27 }
28 
29 /// The load of a host.
30 struct Load {
31     import std.datetime : Duration;
32 
33     double loadAvg;
34     Duration accessTime;
35     bool unknown;
36 
37     bool opEquals(const typeof(this) o) nothrow @safe pure @nogc {
38         if (unknown && o.unknown)
39             return true;
40         return loadAvg == o.loadAvg && accessTime == o.accessTime;
41     }
42 
43     int opCmp(const typeof(this) o) pure @safe @nogc nothrow {
44         if (unknown && o.unknown)
45             return 0;
46         else if (unknown)
47             return 1;
48         else if (o.unknown)
49             return -1;
50 
51         if (loadAvg < o.loadAvg)
52             return -1;
53         else if (loadAvg > o.loadAvg)
54             return 1;
55 
56         if (accessTime < o.accessTime)
57             return -1;
58         else if (accessTime > o.accessTime)
59             return 1;
60 
61         return this == o ? 0 : 1;
62     }
63 }
64 
65 @("shall sort the loads")
66 unittest {
67     import std.algorithm : sort;
68     import std.array : array;
69     import core.time : dur;
70 
71     {
72         auto raw = [Load(0.6, 500.dur!"msecs"), Load(0.5, 500.dur!"msecs")].sort.array;
73         assert(raw[0].loadAvg == 0.5);
74     }
75 
76     {
77         auto raw = [Load(0.5, 600.dur!"msecs"), Load(0.5, 500.dur!"msecs")].sort.array;
78         assert(raw[0].accessTime == 500.dur!"msecs");
79     }
80 }
81 
82 /// Mirror of an environment.
83 struct Env {
84     string[string] payload;
85     alias payload this;
86 }