Skip to content

Commit 8da7292

Browse files
Merge branch 'master' into master
2 parents 3759c23 + 9af945b commit 8da7292

19 files changed

Lines changed: 1236 additions & 27 deletions

dasboot/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ add_subdirectory(app)
66
add_subdirectory(proto)
77
add_subdirectory(controller)
88

9-
add_executable(dasboot app/main.cpp)
10-
apply_compile_flags(dasboot)
11-
target_link_libraries(dasboot PUBLIC cli)
12-
target_link_libraries(dasboot PUBLIC controller)
139

1410

1511
# core

dasboot/app/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
add_executable(dasbootd dasbootd.cpp)
2+
apply_compile_flags(dasbootd)
3+
target_link_libraries(dasbootd
4+
PRIVATE
5+
daemon
6+
cru
7+
)
8+
9+
add_executable(dasboot dasbootc.cpp)
10+
apply_compile_flags(dasboot)
11+
target_link_libraries(dasboot
12+
PRIVATE
13+
cli
14+
controller
15+
)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <dasboot/cli/cli.hpp>
55
#include <dasboot/controller/controller.hpp>
6+
#include <dasboot/cru/common.hpp>
67

78
int main(int argc, char* argv[]) {
89
NCli::TMainSettings settings;
@@ -16,7 +17,7 @@ int main(int argc, char* argv[]) {
1617
return 0;
1718
}
1819

19-
NCli::TSender Sender("ipc:///tmp/mysocket");
20+
NCli::TSender Sender("ipc://" DASBOOTD_SOCK);
2021
Sender.SendMainSettings(settings, command);
2122

2223
return 0;

dasboot/app/dasbootd.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <dasboot/core/daemon/daemon.hpp>
2+
#include <dasboot/cru/os.hpp>
3+
4+
int main()
5+
{
6+
auto ret = NOs::DaemonizeCurrentProcess();
7+
if(ret.Code == NCommon::TStatus::ECode::Failed)
8+
{
9+
std::cout << "Got error trying to daemonize process: " << ret.Error << std::endl;
10+
11+
return 1;
12+
}
13+
14+
try
15+
{
16+
NDaemon::TDaemon daemon(DASBOOTD_SOCK);
17+
daemon.Run();
18+
}
19+
catch(...)
20+
{
21+
std::cout << "Oops, got an error..." << std::endl;
22+
23+
return 1;
24+
}
25+
26+
return 0;
27+
}

dasboot/cli/cli.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,16 @@ namespace NCli {
199199
}
200200
return result;
201201
}
202+
203+
string TConverter::GetFilename(const string& path) {
204+
size_t pos = path.find_last_of("/\\");
205+
return (pos != string::npos) ? path.substr(pos + 1) : path;
206+
}
202207

203208
string TConverter::ReadDasbootFile(const string& path) {
204209
std::ifstream DasbootFile(path);
205210
nlohmann::json jsonDasbootFile, resultJson;
206-
std::vector<string> CopyFile;
211+
std::vector<string> CopyFile, CopyFileNames;
207212

208213
try {
209214
jsonDasbootFile = nlohmann::json::parse(DasbootFile);
@@ -233,18 +238,21 @@ namespace NCli {
233238
if (jsonDasbootFile["copy_file"].is_string()) {
234239
string result = GetReadFileResult(jsonDasbootFile["copy_file"]);
235240
CopyFile.push_back(result);
241+
CopyFileNames.push_back(GetFilename(jsonDasbootFile["copy_file"]));
236242
}
237243
else if (jsonDasbootFile["copy_file"].is_array()) {
238244
for (const auto& CodePath : jsonDasbootFile["copy_file"]) {
239245
if (CodePath.is_string()) {
240246
string result = GetReadFileResult(CodePath);
241247
CopyFile.push_back(result);
248+
CopyFileNames.push_back(GetFilename(CodePath));
242249
} else {
243250
throw std::runtime_error("Error: non-string element in copy_file array");
244251
}
245252
}
246253
}
247254
resultJson["copy_file"] = CopyFile;
255+
resultJson["copy_file_names"] = CopyFileNames;
248256
}
249257

250258
return resultJson.dump();
@@ -254,7 +262,7 @@ namespace NCli {
254262
std::ifstream ExecFile(path);
255263
nlohmann::json jsonExecFile, resultJson;
256264
string pathToScript, pathToCopyFile;
257-
std::vector<string> CopyFile;
265+
std::vector<string> CopyFile, CopyFileNames;
258266

259267
try {
260268
jsonExecFile = nlohmann::json::parse(ExecFile);
@@ -276,19 +284,23 @@ namespace NCli {
276284
if (jsonExecFile.contains("copy_file")) {
277285
if (jsonExecFile["copy_file"].is_string()) {
278286
string result = GetReadFileResult(jsonExecFile["copy_file"]);
279-
resultJson["copy_file"] = result;
287+
CopyFile.push_back(result);
288+
CopyFileNames.push_back(GetFilename(jsonExecFile["copy_file"]));
280289
}
281290
else if (jsonExecFile["copy_file"].is_array()) {
282291
for (const auto& CodePath : jsonExecFile["copy_file"]) {
283292
if (CodePath.is_string()) {
284293
string result = GetReadFileResult(CodePath);
285294
CopyFile.push_back(result);
295+
CopyFileNames.push_back(GetFilename(CodePath));
286296
} else {
287297
throw std::runtime_error("Error: non-string element in copy_file array");
288298
}
289299
}
290-
resultJson["copy_file"] = CopyFile;
291300
}
301+
resultJson["copy_file"] = CopyFile;
302+
resultJson["copy_file_names"] = CopyFileNames;
303+
292304
}
293305

294306
if (jsonExecFile.contains("script_file")) {

dasboot/cli/cli.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ namespace NCli {
102102
public:
103103
static string ReadDasbootFile(const string& path);
104104
static string ReadExecFile(const string& path, const bool& isInteractive);
105+
static string GetFilename(const string& path);
105106
static NMessages::TRunOptions ConvertRunOptions(const NCli::TRunOptions& options, NMessages::TRunOptions& protoOptions);
106107
static NMessages::TBuildOptions ConvertBuildOptions(const NCli::TBuildOptions& options, NMessages::TBuildOptions& protoOptions);
107108
static NMessages::TStartOptions ConvertStartOptions(const NCli::TStartOptions& options, NMessages::TStartOptions& protoOptions);

dasboot/cli/cli_ut.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,22 @@ TEST(CliUt, GetProtoCommandBuild0) {
118118
}
119119

120120
TEST(CliUt, GetProtoCommandBuild1) {
121-
string DasbootFile = "DasbootFile", ScriptFile = "script.py";
121+
string DasbootFile = "DasbootFile", ScriptFile = "script.py", CodeFile = "solve.py";
122122
NOs::CreateFile(DasbootFile, false, 0700);
123123
NOs::CreateFile(ScriptFile, false, 0700);
124+
NOs::CreateFile(CodeFile, false, 0700);
124125

125126
string input_1 = R"({
126127
"network" : true,
127-
"script_file" : "script.py"
128+
"script_file" : "script.py",
129+
"copy_file" : "solve.py"
128130
})",
129131
input_2 = R"(n = 3
130132
for i in range(3):
131133
print("HELLO!!!"))";
132134
NOs::WriteToFile(DasbootFile, input_1);
133135
NOs::WriteToFile(ScriptFile, input_2);
136+
NOs::WriteToFile(CodeFile, input_2);
134137

135138
NCli::TMainSettings settings;
136139
auto parser = NCli::MakeDasbootParser(settings);
@@ -149,8 +152,13 @@ for i in range(3):
149152

150153
NMessages::TBuildOptions ExpectedProtoBuildOptions;
151154
nlohmann::json resultJson;
155+
std::vector<string> CopyFile, CopyFileNames;
156+
CopyFile.push_back(input_2);
157+
CopyFileNames.push_back(CodeFile);
152158
resultJson["network"] = true;
153159
resultJson["script_code"] = input_2;
160+
resultJson["copy_file"] = CopyFile;
161+
resultJson["copy_file_names"] = CopyFileNames;
154162
ExpectedProtoBuildOptions.set_dasboot_file(resultJson.dump());
155163
auto expectedText = ExpectedProtoBuildOptions.DebugString();
156164

@@ -610,8 +618,12 @@ TEST(CliUt, GetProtoCommandExec2) {
610618

611619
NMessages::TExecOptions ExpectedProtoExecOptions;
612620
nlohmann::json resultJson;
621+
std::vector<string> CopyFiles, CopyFileNames;
622+
CopyFiles.push_back(input_2);
623+
CopyFileNames.push_back(CopyFile);
613624
resultJson["network"] = true;
614-
resultJson["copy_file"] = input_2;
625+
resultJson["copy_file"] = CopyFiles;
626+
resultJson["copy_file_names"] = CopyFileNames;
615627
resultJson["script_code"] = input_3;
616628
ExpectedProtoExecOptions.set_exec_file(resultJson.dump());
617629
auto expectedText = ExpectedProtoExecOptions.DebugString();
@@ -662,10 +674,12 @@ TEST(CliUt, GetProtoCommandExec3) {
662674

663675
NMessages::TExecOptions ExpectedProtoExecOptions;
664676
nlohmann::json resultJson;
665-
std::vector<string> CodeFile;
677+
std::vector<string> CodeFile, CodeFileNames;
666678
CodeFile = {input_2, input_2};
679+
CodeFileNames = {CopyFile_1, CopyFile_2};
667680
resultJson["network"] = true;
668681
resultJson["copy_file"] = CodeFile;
682+
resultJson["copy_file_names"] = CodeFileNames;
669683
resultJson["script_code"] = input_3;
670684
ExpectedProtoExecOptions.set_exec_file(resultJson.dump());
671685
auto expectedText = ExpectedProtoExecOptions.DebugString();

dasboot/controller/controller_ut.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,12 @@ TEST(ControllerUt, CommandExec) {
178178
NMessages::TExecOptions ExpectedExecOptions;
179179
nlohmann::json resultJson;
180180
string ExpectedString;
181-
std::vector<string> CodeFile, ScriptCode;
181+
std::vector<string> CodeFile, CodeFileNames;
182+
CodeFile.push_back(input_2);
183+
CodeFileNames.push_back(CopyFile);
182184
resultJson["network"] = true;
183-
resultJson["copy_file"] = input_2;
185+
resultJson["copy_file"] = CodeFile;
186+
resultJson["copy_file_names"] = CodeFileNames;
184187
resultJson["script_code"] = input_3;
185188
ExpectedExecOptions.set_name("Container_name");
186189
ExpectedExecOptions.set_id("Container_id");

dasboot/core/coordinator/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ target_link_libraries(coordinator
1010
container
1111
CURL::libcurl
1212
archive
13+
nlohmann_json
1314
)
1415

1516
# tests:
@@ -24,6 +25,8 @@ target_link_libraries(
2425
coordinator
2526
CURL::libcurl
2627
archive
28+
nlohmann_json
2729
)
2830

2931
gtest_discover_tests(imagemanager_ut)
32+
)

dasboot/core/coordinator/coordinator.cpp

Lines changed: 112 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "coordinator.hpp"
2+
#include <chrono>
3+
#include <thread>
24

35
namespace NCoordinator {
46
namespace {
@@ -58,27 +60,125 @@ namespace {
5860
return { result, { TStatus::Success } };
5961
}
6062

63+
TStatus TCoordinator::Deserialize(TBuildOptionsDeserialized& opts, const std::string& stringJson) {
64+
nlohmann::json jsonValue = nlohmann::json::parse(stringJson);
65+
66+
if (jsonValue.contains("copy_file")) {
67+
opts.CopyFiles = jsonValue["copy_file"].array();
68+
}
69+
70+
if (jsonValue.contains("copy_file_names")) {
71+
opts.CopyFilesNames = jsonValue["copy_file_names"].array();
72+
}
73+
74+
if (jsonValue.contains("script_file")) {
75+
opts.Script = jsonValue["script_file"];
76+
}
77+
78+
return { TStatus::Success };
79+
}
80+
6181
TStatus TCoordinator::Build(const NMessages::TBuildOptions& buildOptions) {
62-
{
63-
std::string alphinePath = ImagesPath + "alpine";
64-
if (!NOs::IsDirectoryExists(alphinePath)) {
65-
// ImageManager.Install();
66-
}
82+
// in the future there will be any type of image
83+
std::string alphinePath = ImagesPath + "alpine";
84+
if (!NOs::IsDirectoryExists(alphinePath)) {
85+
// ImageManager.Install();
6786
}
6887

6988
std::string name = buildOptions.name();
70-
std::string fullPath = MakeString() << ContainersPath << name;
71-
72-
if (NOs::IsPathExists(fullPath)) {
89+
if (Containers.contains(name)) {
7390
return { TStatus::Failed, MakeString() << "Container with name " << name << " already exists. "};
7491
}
7592

76-
// TODO(flown4qqqq)
93+
std::string containerPath = MakeString() << ContainersPath << name;
94+
if (auto status = NOs::CreateDirectory(containerPath); status.Code != TStatus::Success) {
95+
return status;
96+
}
97+
98+
TBuildOptionsDeserialized options;
99+
std::string serializedJson = buildOptions.dasboot_file();
100+
Deserialize(options, serializedJson);
101+
102+
std::string rootfsPath = MakeString() << containerPath << "/rootfs";
103+
NOs::Copy(alphinePath, rootfsPath);
104+
105+
for (size_t i = 0; i < options.CopyFilesNames.size(); ++i) {
106+
const std::string name = options.CopyFilesNames[i];
107+
const std::string data = options.CopyFiles[i];
108+
109+
std::string fullPath = MakeString() << rootfsPath << "/" << name;
110+
NOs::CreateFile(fullPath, true, 0755);
111+
NOs::WriteToFile(fullPath, data);
112+
}
113+
114+
NContainer::TContainer::TMetaInfo metaBuildInfo;
115+
metaBuildInfo.Name = name;
116+
metaBuildInfo.RootFsPath = rootfsPath;
117+
Containers[name] = std::make_unique<NContainer::TContainer>(metaBuildInfo);
118+
119+
NContainer::TContainer::TBuildInfo buildInfo;
120+
buildInfo.NeedNetwork = options.NeedNetwork;
121+
buildInfo.StaticScript = options.Script;
122+
return Containers[name]->Build(buildInfo);
123+
}
124+
125+
TStatus TCoordinator::Deserialize(TExecOptionsDeserialized& opts, const std::string& stringJson) {
126+
nlohmann::json jsonValue = nlohmann::json::parse(stringJson);
127+
128+
if (jsonValue.contains("copy_file")) {
129+
opts.CopyFiles = jsonValue["copy_file"].array();
130+
}
131+
132+
if (jsonValue.contains("copy_file_names")) {
133+
opts.CopyFilesNames = jsonValue["copy_file_names"].array();
134+
}
77135

78-
return { TStatus::Failed };
136+
if (jsonValue.contains("script_file")) {
137+
opts.Script = jsonValue["script_file"];
138+
}
139+
140+
return { TStatus::Success };
79141
}
80142

81-
TStatus TCoordinator::Exec(const NMessages::TExecOptions&) {
82-
return { TStatus::Failed };
143+
TStatus TCoordinator::Exec(const NMessages::TExecOptions& execOptions) {
144+
std::string name = execOptions.name();
145+
if (!Containers.contains(name)) {
146+
return { TStatus::Failed, MakeString() << "Container with name " << name << " does not exists. "};
147+
}
148+
149+
TExecOptionsDeserialized options;
150+
151+
if (execOptions.has_is_interactive()) {
152+
options.IsInteractive = execOptions.is_interactive();
153+
}
154+
155+
if (execOptions.has_exec_file()) {
156+
std::string serializedJson = execOptions.exec_file();
157+
Deserialize(options, serializedJson);
158+
}
159+
160+
std::string containerPath = MakeString() << ContainersPath << name;
161+
std::string rootfsPath = MakeString() << containerPath << "/rootfs";
162+
for (size_t i = 0; i < options.CopyFilesNames.size(); ++i) {
163+
const std::string name = options.CopyFilesNames[i];
164+
const std::string data = options.CopyFiles[i];
165+
166+
std::string fullPath = MakeString() << rootfsPath << "/" << name;
167+
NOs::CreateFile(fullPath, true, 0755);
168+
NOs::WriteToFile(fullPath, data);
169+
}
170+
171+
while (true) {
172+
using EState = NContainer::TContainer::EState;
173+
if (Containers[name]->GetState() == EState::Exited) {
174+
NContainer::TContainer::TExecInfo execInfo;
175+
execInfo.NeedNetwork = options.NeedNetwork;
176+
execInfo.DynamicScript = options.Script;
177+
execInfo.IsInteractive = options.IsInteractive;
178+
return Containers[name]->Exec(execInfo);
179+
}
180+
181+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
182+
}
83183
}
84184
} // namespace NCoordinator

0 commit comments

Comments
 (0)