Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(containers): ensure containers are destroyed when wing console is interrupted #127

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions containers/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion containers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@winglibs/containers",
"version": "0.0.22",
"version": "0.0.23",
"description": "Container support for Wing",
"repository": {
"type": "git",
Expand Down
20 changes: 10 additions & 10 deletions containers/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const child_process = require("child_process");
const fs = require('fs');
const crypto = require('crypto');
const glob = require('glob');
const path = require('path');
const fs = require("fs");
const crypto = require("crypto");
const glob = require("glob");
const path = require("path");

exports.shell = async function (command, args, cwd) {
return new Promise((resolve, reject) => {
Expand All @@ -18,23 +18,23 @@ exports.shell = async function (command, args, cwd) {
};

exports.entrypointDir = function (scope) {
if (typeof(scope.entrypointDir) == "string") {
if (typeof scope.entrypointDir == "string") {
return scope.entrypointDir;
}

return exports.entrypointDir(scope.node.scope);
};

exports.dirname = function() {
exports.dirname = function () {
return __dirname;
};

exports.contentHash = function(patterns, cwd) {
const hash = crypto.createHash('md5');
exports.contentHash = function (patterns, cwd) {
const hash = crypto.createHash("md5");
const files = glob.sync(patterns, { nodir: true, cwd });
for (const f of files) {
const data = fs.readFileSync(path.join(cwd, f));
hash.update(data);
}
return hash.digest('hex');
};
return hash.digest("hex");
};
4 changes: 2 additions & 2 deletions containers/utils.w
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ pub class Util {
if !Util.isPath(props.image) {
return nil;
}

let sources = props.sources ?? ["**/*"];
let imageDir = props.image;
return props.sourceHash ?? Util.contentHash(sources, imageDir);
}
}
}
180 changes: 95 additions & 85 deletions containers/workload.sim.w
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub class Workload_sim {
imageTag: str;
public: bool;
state: sim.State;

new(props: api.WorkloadProps) {
this.appDir = utils.entrypointDir(this);
this.props = props;
Expand Down Expand Up @@ -51,108 +51,118 @@ pub class Workload_sim {
}

let s = new cloud.Service(inflight () => {
this.start();
return () => { this.stop(); };
});

std.Node.of(s).hidden = true;
std.Node.of(this.state).hidden = true;
}
log("starting workload...");

let opts = this.props;

// if this a reference to a local directory, build the image from a docker file
if utils.isPathInflight(opts.image) {
// check if the image is already built
try {
utils.shell("docker", ["inspect", this.imageTag]);
log("image {this.imageTag} already exists");
} catch {
log("building locally from {opts.image} and tagging {this.imageTag}...");
utils.shell("docker", ["build", "-t", this.imageTag, opts.image], this.appDir);
}
} else {
try {
utils.shell("docker", ["inspect", this.imageTag]);
log("image {this.imageTag} already exists");
} catch {
log("pulling {this.imageTag}");
utils.shell("docker", ["pull", this.imageTag]);
}
}

inflight start(): void {
log("starting workload...");
// start the new container
let dockerRun = MutArray<str>[];
dockerRun.push("run");
dockerRun.push("--detach");
dockerRun.push("--rm");

let opts = this.props;
let name = util.uuidv4();
dockerRun.push("--name", name);

// if this a reference to a local directory, build the image from a docker file
if utils.isPathInflight(opts.image) {
// check if the image is already built
try {
utils.shell("docker", ["inspect", this.imageTag]);
log("image {this.imageTag} already exists");
} catch {
log("building locally from {opts.image} and tagging {this.imageTag}...");
utils.shell("docker", ["build", "-t", this.imageTag, opts.image], this.appDir);
if let port = opts.port {
dockerRun.push("-p");
dockerRun.push("{port}");
}
} else {
try {
utils.shell("docker", ["inspect", this.imageTag]);
log("image {this.imageTag} already exists");
} catch {
log("pulling {this.imageTag}");
utils.shell("docker", ["pull", this.imageTag]);
}
}

// start the new container
let dockerRun = MutArray<str>[];
dockerRun.push("run");
dockerRun.push("--detach");

if let port = opts.port {
dockerRun.push("-p");
dockerRun.push("{port}");
}

if let env = opts.env {
if env.size() > 0 {
dockerRun.push("-e");
for k in env.keys() {
dockerRun.push("{k}={env.get(k)!}");
if let env = opts.env {
if env.size() > 0 {
dockerRun.push("-e");
for k in env.keys() {
dockerRun.push("{k}={env.get(k)!}");
}
}
}
}

dockerRun.push(this.imageTag);
dockerRun.push(this.imageTag);

if let runArgs = this.props.args {
for a in runArgs {
dockerRun.push(a);
if let runArgs = this.props.args {
for a in runArgs {
dockerRun.push(a);
}
}
}

log("starting container from image {this.imageTag}");
log("docker {dockerRun.join(" ")}");
let containerId = utils.shell("docker", dockerRun.copy()).trim();
this.state.set(this.containerIdKey, containerId);
log("starting container from image {this.imageTag}");
log("docker {dockerRun.join(" ")}");
utils.shell("docker", dockerRun.copy());
this.state.set(this.containerIdKey, name);

log("containerId={containerId}");
log("containerName={name}");

let out = Json.parse(utils.shell("docker", ["inspect", containerId]));
return () => {
utils.shell("docker", ["rm", "-f", name]);
};
});
std.Node.of(s).hidden = true;

if let port = opts.port {
let hostPort = out.tryGetAt(0)?.tryGet("NetworkSettings")?.tryGet("Ports")?.tryGet("{port}/tcp")?.tryGetAt(0)?.tryGet("HostPort")?.tryAsStr();
if !hostPort? {
throw "Container does not listen to port {port}";
}
let s2 = new cloud.Service(inflight () => {
let name = this.state.get(this.containerIdKey).asStr();
let opts = this.props;
let var out: Json? = nil;
util.waitUntil(inflight () => {
try {
out = Json.parse(utils.shell("docker", ["inspect", name]));
return true;
} catch {
return false;
}
}, interval: 0.1s);

let publicUrl = "http://localhost:{hostPort!}";
if let port = opts.port {
let hostPort = out?.tryGetAt(0)?.tryGet("NetworkSettings")?.tryGet("Ports")?.tryGet("{port}/tcp")?.tryGetAt(0)?.tryGet("HostPort")?.tryAsStr();
if !hostPort? {
throw "Container does not listen to port {port}";
}

if let k = this.publicUrlKey {
this.state.set(k, publicUrl);
}
let publicUrl = "http://localhost:{hostPort!}";

if let k = this.internalUrlKey {
this.state.set(k, "http://host.docker.internal:{hostPort!}");
}
if let k = this.publicUrlKey {
this.state.set(k, publicUrl);
}

if let readiness = opts.readiness {
let readinessUrl = "{publicUrl}{readiness}";
log("waiting for container to be ready: {readinessUrl}...");
util.waitUntil(inflight () => {
try {
return http.get(readinessUrl).ok;
} catch {
return false;
}
}, interval: 0.1s);
if let k = this.internalUrlKey {
this.state.set(k, "http://host.docker.internal:{hostPort!}");
}

if let readiness = opts.readiness {
let readinessUrl = "{publicUrl}{readiness}";
log("waiting for container to be ready: {readinessUrl}...");
util.waitUntil(inflight () => {
try {
return http.get(readinessUrl).ok;
} catch {
return false;
}
}, interval: 0.1s);
}
}
}
}
}) as "Port Retrieval";
std.Node.of(s2).hidden = true;

inflight stop() {
let containerId = this.state.get(this.containerIdKey).asStr();
log("stopping container {containerId}");
utils.shell("docker", ["rm", "-f", containerId]);
std.Node.of(this.state).hidden = true;
}
}
}
Loading