-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.cs
152 lines (122 loc) · 4.29 KB
/
Main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json.Linq;
namespace akash_dep
{
class MainClass
{
public static int kMaxCreateRetry = 3;
public static int kMaxLeaseRetry = 3;
public static int kMaxLeaseter = 10000;
public static int kMaxManifestRetry = 1;
public static int kMaxManifestIter = 1000;
// -1 create error, -2 Lease Error, -3 Manifest error, -4 exception
public static int NewDeployment(ref Wallet wl, long numInstances)
{
try
{
Instance inst = new Instance(ref wl);
inst.ClearCaches();
for (int i = 0; i < kMaxCreateRetry && !inst.Create(numInstances); i++)
{
}
var retry = 0;
while (!inst.CreateLease() || !inst.SelectLease() || !inst.CheckLease())
{
// try one more time
System.Threading.Thread.Sleep(kMaxLeaseter);
retry++;
if (retry > kMaxLeaseRetry)
{
inst.Close();
return -2;
}
}
retry = 0;
while (!inst.SendManifest())
{
System.Threading.Thread.Sleep(kMaxManifestIter); // Must also wait to confirm
retry++;
if (retry > kMaxManifestRetry)
{
inst.MarkBad(); // bad manifest submission should be marked
inst.Close();
return -3;
}
}
Console.WriteLine("deployment done");
return 0;
}
catch
{
Console.WriteLine("deployment exception");
return -4;
}
}
public static void Main(string[] args)
{
int numParams = args.Count();
String configText = File.ReadAllText("./config.js");
JToken mainConfig = Converters.STRtoJS(configText);
Akash.LoadCfg(mainConfig); // Load shared params
Instance.LoadCfg(mainConfig); // Load shared params
long DEFAULT_CORES = mainConfig["DEFAULT_CORES"].ToObject<long>();
Akash.Connect();
Akash.EvalVars();
Wallet wl = new Wallet();
wl.LoadCfg(mainConfig); // Load wallet specific
wl.ClearCache();
wl.EvalVars();
wl.Update();
Instance.LoadBad(); // Load banlist
InstanceList lst = new InstanceList(ref wl);
Console.WriteLine("numParams " + numParams);
if (numParams == 1)
{
lst.Query();
var vars = args[0];
if (vars == "cleanup")
{
lst.CleanDeployments();
}
else if (vars == "deposits")
{
lst.DoDeposits(5);
}
else if (vars == "manifests")
{
lst.UpdateManifests();
}
else if (vars == "info")
{
lst.Stats();
return;
}
else
{
Console.WriteLine("invalid params");
}
}
int CREATE_DEPLOYMENTS = mainConfig["CREATE_DEPLOYMENTS"].ToObject<int>();
var progress = new ProgressConsole(CREATE_DEPLOYMENTS, "creating deployments");
int numGood = 0;
for (int i = 0; i < CREATE_DEPLOYMENTS; i++)
{
progress.Increment();
int depStatus = NewDeployment(ref wl, DEFAULT_CORES);
if (depStatus == 0)
{
numGood++;
}
else if (depStatus == -2) // Lease error, stop
{
Console.WriteLine("no more leases available");
break;
}
}
Console.WriteLine("Deployed " + numGood + " from " + CREATE_DEPLOYMENTS);
}
}
}