|
1 | 1 | #include "coordinator.hpp" |
| 2 | +#include <chrono> |
| 3 | +#include <thread> |
2 | 4 |
|
3 | 5 | namespace NCoordinator { |
4 | 6 | namespace { |
@@ -58,27 +60,125 @@ namespace { |
58 | 60 | return { result, { TStatus::Success } }; |
59 | 61 | } |
60 | 62 |
|
| 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 | + |
61 | 81 | 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(); |
67 | 86 | } |
68 | 87 |
|
69 | 88 | std::string name = buildOptions.name(); |
70 | | - std::string fullPath = MakeString() << ContainersPath << name; |
71 | | - |
72 | | - if (NOs::IsPathExists(fullPath)) { |
| 89 | + if (Containers.contains(name)) { |
73 | 90 | return { TStatus::Failed, MakeString() << "Container with name " << name << " already exists. "}; |
74 | 91 | } |
75 | 92 |
|
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 | + } |
77 | 135 |
|
78 | | - return { TStatus::Failed }; |
| 136 | + if (jsonValue.contains("script_file")) { |
| 137 | + opts.Script = jsonValue["script_file"]; |
| 138 | + } |
| 139 | + |
| 140 | + return { TStatus::Success }; |
79 | 141 | } |
80 | 142 |
|
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 | + } |
83 | 183 | } |
84 | 184 | } // namespace NCoordinator |
0 commit comments