Skip to content

Commit 1099a60

Browse files
committed
added a function to automatically install cluster configuration and install npm dependencies upon setup
1 parent 85f1272 commit 1099a60

File tree

5 files changed

+144
-13
lines changed

5 files changed

+144
-13
lines changed

src/OneOS-Core/Program.cs

+77
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Threading;
88
using System.Threading.Tasks;
99
using System.Diagnostics;
10+
using Newtonsoft.Json.Linq;
1011

1112
using OneOS.Runtime;
1213
using OneOS.Common;
@@ -156,6 +157,8 @@ files including the configuration file. If the configuration file does
156157
// if first argument is "config", run the interactive setup
157158
else if (arguments[0] == "config")
158159
{
160+
Helpers.PrintPlatformInformation();
161+
159162
Console.WriteLine($"Running OneOS Configuration Setup");
160163
Configuration.CreateOrUpdateConfig(mountPath);
161164
}
@@ -164,7 +167,10 @@ files including the configuration file. If the configuration file does
164167
{
165168
Helpers.PrintPlatformInformation();
166169

170+
EnsureTestClusterConfigs(mountPath);
171+
167172
Console.WriteLine($"Starting OneOS Test Cluster");
173+
Console.WriteLine($"(this cluster runs locally -- use this only during development)");
168174

169175
var config_0 = Configuration.LoadConfig(Path.Combine(mountPath, "test_0"));
170176
var config_1 = Configuration.LoadConfig(Path.Combine(mountPath, "test_1"));
@@ -452,5 +458,76 @@ files including the configuration file. If the configuration file does
452458
}
453459
}
454460
}
461+
462+
static void EnsureTestClusterConfigs(string mountPath)
463+
{
464+
if (!Directory.Exists(mountPath))
465+
{
466+
Console.WriteLine($"{mountPath} directory not found. Creating ...");
467+
Directory.CreateDirectory(mountPath);
468+
};
469+
470+
var cores = Configuration.LookupCores();
471+
472+
for (var i = 0; i < 5; i++)
473+
{
474+
var peerId = $"test_{i}";
475+
var baseDirPath = Path.Combine(mountPath, peerId);
476+
var configPath = Path.Combine(baseDirPath, "config.json");
477+
478+
if (!Directory.Exists(baseDirPath))
479+
{
480+
Console.WriteLine($"{baseDirPath} directory not found. Creating ...");
481+
Directory.CreateDirectory(baseDirPath);
482+
};
483+
484+
// look for config file
485+
if (!File.Exists(configPath))
486+
{
487+
var json = JObject.Parse("{}");
488+
json["domain"] = "default.domain";
489+
json["id"] = peerId;
490+
json["port"] = $"800{i}";
491+
json["storage"] = Path.Combine(baseDirPath, "data");
492+
json["vms"] = JArray.Parse("[]");
493+
json["io"] = JArray.Parse("[]");
494+
json["cores"] = JArray.Parse($"[ 1, {cores.Item2} ]");
495+
json["memory"] = 512;
496+
json["disk"] = 1024;
497+
json["tags"] = JArray.Parse("[]");
498+
json["peers"] = JObject.Parse("{}");
499+
500+
var vmAgents = (JArray)json["vms"];
501+
foreach (var lang in Configuration.LanguageMap)
502+
{
503+
try
504+
{
505+
var bin = Configuration.LookupVM(lang.Value);
506+
var vmConfig = new JObject();
507+
vmConfig["name"] = lang.Key;
508+
vmConfig["language"] = lang.Key;
509+
vmConfig["bin"] = bin;
510+
vmAgents.Add(vmConfig);
511+
}
512+
catch (NullReferenceException ex)
513+
{
514+
// do nothing
515+
}
516+
}
517+
518+
var peers = (JObject)json["peers"];
519+
for (var j = 0; j < 5; j++)
520+
{
521+
var otherId = $"test_{j}";
522+
if (peerId != otherId)
523+
{
524+
peers.Add(otherId, $"127.0.0.1:800{j}");
525+
}
526+
}
527+
528+
File.WriteAllText(configPath, json.ToString());
529+
}
530+
}
531+
}
455532
}
456533
}

src/OneOS/Runtime/Configuration.cs

+63-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Text;
43
using System.Linq;
54
using System.IO;
65
using System.Net;
76
using System.Diagnostics;
87
using System.Runtime.InteropServices;
8+
using System.Threading.Tasks;
99
using Newtonsoft.Json.Linq;
1010

11-
using OneOS.Common;
12-
1311
namespace OneOS.Runtime
1412
{
1513
public class Configuration
@@ -21,7 +19,7 @@ public class Configuration
2119
static string OneOSPath = Path.Combine(HomePath, ".oneos");
2220
public static string DefaultOneOSPath { get => OneOSPath; }
2321

24-
static Dictionary<string, string> LanguageMap = new Dictionary<string, string>()
22+
public static Dictionary<string, string> LanguageMap = new Dictionary<string, string>()
2523
{
2624
{ "csharp", "dotnet" },
2725
{ "fsharp", "dotnet" },
@@ -110,6 +108,57 @@ public static void CreateOrUpdateConfig(string mountPath = null)
110108
SaveConfig(configPath, json);
111109
}
112110

111+
private static void InstallNodeJSDependencies(string tempDataPath, string dependencies)
112+
{
113+
var tcs = new TaskCompletionSource<object>();
114+
115+
Console.WriteLine($"Installing oneos.js NPM Dependencies");
116+
117+
var startInfo = new ProcessStartInfo();
118+
startInfo.UseShellExecute = false;
119+
startInfo.WorkingDirectory = tempDataPath;
120+
startInfo.RedirectStandardInput = true;
121+
startInfo.RedirectStandardOutput = true;
122+
startInfo.RedirectStandardError = true;
123+
124+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
125+
{
126+
startInfo.FileName = "cmd";
127+
startInfo.Arguments = "/c npm install " + dependencies;
128+
}
129+
else
130+
{
131+
startInfo.FileName = "npm";
132+
startInfo.Arguments = "install " + dependencies;
133+
}
134+
135+
var npm = new Process();
136+
npm.StartInfo = startInfo;
137+
npm.EnableRaisingEvents = true;
138+
npm.Exited += (sender, evt) =>
139+
{
140+
string output;
141+
if (npm.ExitCode != 0)
142+
{
143+
output = npm.StandardError.ReadToEnd();
144+
}
145+
else
146+
{
147+
output = npm.StandardOutput.ReadToEnd();
148+
}
149+
150+
tcs.SetResult(output);
151+
};
152+
153+
npm.Start();
154+
155+
var result = tcs.Task.Result;
156+
157+
Console.WriteLine(result);
158+
159+
return;
160+
}
161+
113162
// ensures that the OneOS directory exists
114163
static void CheckOneOSDirectory(string mountPath)
115164
{
@@ -155,6 +204,9 @@ static void CheckOneOSDirectory(string mountPath)
155204
{
156205
Console.WriteLine($"{npmPackageJson} file not found. Creating ...");
157206
File.WriteAllText(npmPackageJson, "{}");
207+
208+
// install oneos.js dependencies
209+
InstallNodeJSDependencies(tempDirectory, "[email protected] [email protected] [email protected]");
158210
}
159211

160212
// check node_modules (needed for JavaScript agents)
@@ -461,7 +513,7 @@ static void CreateOrUpdateField(JObject json, string key, string fieldName, stri
461513
}
462514
}
463515

464-
static string LookupVM(string binaryName)
516+
public static string LookupVM(string binaryName)
465517
{
466518
Process cmd = new Process();
467519

@@ -480,14 +532,16 @@ static string LookupVM(string binaryName)
480532

481533
cmd.WaitForExit();
482534

483-
string result = cmd.StandardOutput.ReadToEnd().Trim();
535+
string[] result = cmd.StandardOutput.ReadToEnd().Trim().Split('\n');
536+
537+
if (result[0].Contains("Could not find")) throw new NullReferenceException();
484538

485-
if (result.Contains("Could not find")) throw new NullReferenceException();
539+
if (!File.Exists(result[0])) throw new NullReferenceException();
486540

487-
return result;
541+
return result[0];
488542
}
489543

490-
static (int, int) LookupCores()
544+
public static (int, int) LookupCores()
491545
{
492546
Process cmd = new Process();
493547
cmd.StartInfo.RedirectStandardOutput = true;

src/OneOS/Runtime/Kernel/SessionManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public async Task<object> GetShell(string username, string password)
8383
{
8484
if (!Runtime.Registry.Agents.ContainsKey(ShellUri(username)))
8585
{
86-
var env = new Dictionary<string, string>();
86+
var env = new Dictionary<string, string>() { { "CHECKPOINT_INTERVAL", "0" } };
8787
await Request("kernels." + Runtime.Domain + "/RegistryManager", "SpawnAs", ShellUri(username), username, "OneOSKernel", env, "UserShell", username, "interpreter");
8888
}
8989

src/OneOS/Runtime/Registry.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ public enum LanguageInfo
638638
public bool OutputToShell;
639639
public string BinaryPath;
640640
public string Arguments;
641-
public List<string> Subscriptions;
641+
public List<string> Subscriptions = new List<string>();
642642
public int CheckpointInterval;
643643
public float OutputRateLimit;
644644
public string LastCheckpoint;

src/OneOS/Runtime/Runtime.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ internal async Task<List<string>> SyncRegistry()
191191
var tasks = new List<Task>();
192192
var results = new List<string>();
193193

194-
Console.WriteLine($"\n{this} Syncing Registry...");
194+
Console.WriteLine($"\n{this} Syncing Registry with {ActivePeers.Count} peers...");
195195

196196
foreach (var item in ActivePeers)
197197
{
@@ -2223,4 +2223,4 @@ protected override void OnEnd()
22232223
Console.WriteLine($"{this} Stopped");
22242224
}
22252225
}
2226-
}
2226+
}

0 commit comments

Comments
 (0)