Skip to content

Commit 5bf2d88

Browse files
committed
feat(NodeAllocator): Specify a container's host IP
1 parent a89ee2a commit 5bf2d88

3 files changed

Lines changed: 44 additions & 29 deletions

File tree

src/hosted/NodeAllocator.cc

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,25 @@ ebbrt::NodeAllocator::DockerContainer::~DockerContainer() {
8080
}
8181
}
8282

83+
std::string ebbrt::NodeAllocator::DockerContainer::GetIp() {
84+
auto cip = RunCmd("docker inspect -f '{{range "
85+
".NetworkSettings.Networks}}{{.IPAddress}}{{end}}' " +
86+
cid_ );
87+
assert(cip != "");
88+
return cip;
89+
}
90+
8391
std::string ebbrt::NodeAllocator::DockerContainer::Start() {
8492
if (!cid_.empty() || img_.size() == 0) {
8593
throw std::runtime_error("Error: attempt to start unspecified container ");
8694
}
8795
std::stringstream cmd;
88-
cmd << "docker run -d " << arg_ << " " << img_ << " " << cmd_;
96+
cmd << base_ << " run -d " << arg_ << " " << img_ << " " << cmd_;
8997
cid_ = RunCmd(cmd.str());
9098
std::cerr << "Docker Container:" << std::endl;
91-
std::cerr << "| img:" << img_ << std::endl;
99+
if( host_ != ""){
100+
std::cerr << "| host:" << host_ << std::endl;
101+
}
92102
std::cerr << "| cid: " << cid_.substr(0, 12) << std::endl;
93103
std::cerr << "| log: docker logs " << cid_.substr(0, 12) << std::endl;
94104
return cid_;
@@ -255,14 +265,11 @@ ebbrt::NodeAllocator::AllocateNode(std::string binary_path,
255265
std::to_string(allocation_id);
256266
std::stringstream docker_args;
257267
std::stringstream qemu_args;
268+
258269
#ifndef NDEBUG
259270
docker_args << " --expose 1234 -e DEBUG=true";
260271
#endif
261272

262-
if (!args.constraint_node.empty()) {
263-
docker_args << " -e constraint:node==" << args.constraint_node << " ";
264-
}
265-
266273
if (CustomNetworkNodeArguments.empty()) {
267274
docker_args << " --net=" << network_id_ << " ";
268275
} else {
@@ -287,30 +294,27 @@ ebbrt::NodeAllocator::AllocateNode(std::string binary_path,
287294
<< " -append \"" << cmdline_ << ";allocid=" << allocation_id
288295
<< "\"";
289296

290-
auto c = DockerContainer(repo, docker_args.str(), qemu_args.str());
297+
auto c = DockerContainer(repo, docker_args.str(), qemu_args.str(), args.constraint_node);
291298
auto id = c.Start();
299+
auto cip = c.GetIp(); /* get IP of container */
292300

293301
nodes_.insert(std::make_pair(std::string(id), std::move(c)));
294302
auto rfut = promise_map_[allocation_id].GetFuture();
295303

296-
/* get IP of container */
297-
auto cip = RunCmd("docker inspect -f '{{range "
298-
".NetworkSettings.Networks}}{{.IPAddress}}{{end}}' " +
299-
id);
300-
assert(id != "");
301-
302304
/* transfer image into container */
303305
RunCmd("ping -c 3 -w 30 " + cip);
304306
RunCmd("nc -z -w 30 " + cip + " 22");
305-
RunCmd("scp -q -o UserKnownHostsFile=/dev/null -o "
307+
RunCmd("scp -q -o UserKnownHostsFile=/dev/null -o "
306308
"StrictHostKeyChecking=no " +
307309
binary_path + " root@" + cip + ":/root/img.elf");
308310
/* kick start vm */
309-
RunCmd("docker exec -dt " + id + " touch /tmp/signal");
311+
RunCmd("ssh -q -o UserKnownHostsFile=/dev/null -o "
312+
"StrictHostKeyChecking=no root@"+cip+" 'touch /tmp/signal'");
310313

311314
std::cerr << "Node Allocation Details: " << std::endl;
312315
std::cerr << "| img: " << binary_path << std::endl;
313316
std::cerr << "| id: " << container_name << std::endl;
317+
std::cerr << "| ip: " << cip << std::endl;
314318
#ifndef NDEBUG
315319
std::cerr << "# debug w/ gdb: " << std::endl;
316320
std::cerr << "# gdb " << binary_path.substr(0, binary_path.size() - 2)

src/hosted/NodeAllocator.h

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,33 @@
1919
namespace ebbrt {
2020
class NodeAllocator : public StaticSharedEbb<NodeAllocator> {
2121
struct DockerContainer {
22-
DockerContainer() {}
23-
DockerContainer(std::string repo,
24-
std::string container_args = std::string(),
25-
std::string run_cmd = std::string())
26-
: img_{std::move(repo)}, arg_{std::move(container_args)},
27-
cmd_{std::move(run_cmd)} {}
22+
DockerContainer() = delete;
23+
DockerContainer(std::string img,
24+
std::string arg = std::string(),
25+
std::string cmd = std::string(),
26+
std::string host = std::string())
27+
: arg_{arg}, cmd_{cmd}, host_{host}, img_{img} {
28+
base_ = "docker";
29+
if(host_ != ""){
30+
base_ += " -H "+host_;
31+
}
32+
}
2833
~DockerContainer();
2934
DockerContainer(DockerContainer&& other) noexcept /* move constructor */
30-
: img_{std::move(other.img_)},
31-
arg_{std::move(other.arg_)},
35+
: arg_{std::move(other.arg_)},
36+
base_{std::move(other.base_)},
37+
cid_{std::move(other.cid_)},
3238
cmd_{std::move(other.cmd_)},
33-
cid_{std::move(other.cid_)} {}
39+
host_{std::move(other.host_)},
40+
img_{std::move(other.img_)} {}
3441
DockerContainer&
3542
operator=(DockerContainer&& other) noexcept /* move assignment */ {
36-
img_ = std::move(other.img_);
3743
arg_ = std::move(other.arg_);
38-
cmd_ = std::move(other.cmd_);
44+
base_ = std::move(other.base_);
3945
cid_ = std::move(other.cid_);
46+
cmd_ = std::move(other.cmd_);
47+
host_ = std::move(other.host_);
48+
img_ = std::move(other.img_);
4049
return *this;
4150
}
4251
DockerContainer(const DockerContainer& other) =
@@ -45,13 +54,16 @@ class NodeAllocator : public StaticSharedEbb<NodeAllocator> {
4554
operator=(const DockerContainer& other) = delete; /* copy assignment */
4655
std::string Start();
4756
std::string StdOut();
57+
std::string GetIp();
4858
void Stop() {}
4959

5060
private:
51-
std::string img_ = std::string();
5261
std::string arg_ = std::string();
53-
std::string cmd_ = std::string();
62+
std::string base_ = std::string();
5463
std::string cid_ = std::string();
64+
std::string cmd_ = std::string();
65+
std::string host_ = std::string();
66+
std::string img_ = std::string();
5567
};
5668

5769
static const constexpr uint8_t kDefaultCpus = 2;

src/hosted/PoolAllocator.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ void ebbrt::PoolAllocator::AllocateNode(int i) {
4646
std::string node = (specified_nodes) ? nodes_[i % nodes_.size()]
4747
: std::string();
4848

49-
args.constraint_node = node;
5049
auto nd = ebbrt::node_allocator->AllocateNode(binary_path_, args);
5150

5251
nd.NetworkId().Then(

0 commit comments

Comments
 (0)