From 9541ac32860d6a8775ba943cda28548f04b5b81f Mon Sep 17 00:00:00 2001 From: Elad Ben-Israel Date: Fri, 13 Oct 2023 16:18:53 +0300 Subject: [PATCH] use content hash for local image tag --- sim/workload.w | 12 ++++++------ test/containers.test.w | 5 ----- test/ecr.test.w | 3 ++- test/local-build.test.w | 2 +- utils.w | 2 +- 5 files changed, 10 insertions(+), 14 deletions(-) diff --git a/sim/workload.w b/sim/workload.w index db816fa..fe5de8b 100644 --- a/sim/workload.w +++ b/sim/workload.w @@ -10,13 +10,15 @@ class Workload impl api.IWorkload { urlKey: str; props: api.WorkloadProps; appDir: str; - + imageTag: str; + init(props: api.WorkloadProps) { this.appDir = utils.entrypointDir(this); this.props = props; let hash = util.sha256(Json.stringify(props)); this.containerId = "wing-${this.node.addr.substring(0, 6)}-${hash}"; this.bucket = new cloud.Bucket(); + this.imageTag = utils.resolveContentHash(this, props); this.urlKey = "url"; @@ -39,14 +41,12 @@ class Workload impl api.IWorkload { let opts = this.props; let image = opts.image; - let var imageTag = image; // if this a reference to a local directory, build the image from a docker file log("image: ${image}"); if image.startsWith("./") { - imageTag = this.containerId; - log("building locally from ${image} and tagging ${imageTag}..."); - utils.shell("docker", ["build", "-t", imageTag, image], this.appDir); + log("building locally from ${image} and tagging ${this.imageTag}..."); + utils.shell("docker", ["build", "-t", this.imageTag, image], this.appDir); } else { utils.shell("docker", ["pull", opts.image], this.appDir); } @@ -75,7 +75,7 @@ class Workload impl api.IWorkload { } } - dockerRun.push(imageTag); + dockerRun.push(this.imageTag); if let runArgs = this.props.args { for a in runArgs { diff --git a/test/containers.test.w b/test/containers.test.w index 79b07b4..4cffc9e 100644 --- a/test/containers.test.w +++ b/test/containers.test.w @@ -22,11 +22,6 @@ new containers.Workload( args: ["-text=hello1234"], ) as "http-echo"; -// new containers.Workload( -// image: "./my-app/Dockerfile", -// port: 3000 -// ); - let getBody = inflight (): str? => { if let url = hello.url() { return http.get(url).body; diff --git a/test/ecr.test.w b/test/ecr.test.w index c3aed3b..136afa2 100644 --- a/test/ecr.test.w +++ b/test/ecr.test.w @@ -2,6 +2,7 @@ bring "../tf-aws/ecr.w" as ecr; bring "../utils.w" as utils; new ecr.Repository( + name: "my-repository", directory: utils.dirname() + "/test/my-app", - tag: "t3" + tag: "tag1" ); diff --git a/test/local-build.test.w b/test/local-build.test.w index 660dbc3..ab4a394 100644 --- a/test/local-build.test.w +++ b/test/local-build.test.w @@ -9,5 +9,5 @@ let app = new containers.Workload( test "can access container" { let response = http.get("${app.url()}"); - assert((response.body ?? "") == "Hello, world!"); + assert((response.body ?? "") == "Hello, Wingnuts!"); } \ No newline at end of file diff --git a/utils.w b/utils.w index 4351fc5..ee4bafd 100644 --- a/utils.w +++ b/utils.w @@ -8,7 +8,7 @@ class Util { pub static resolveContentHash(scope: std.IResource, props: utils_api.WorkloadProps): str { if !props.image.startsWith("./") { - throw "image is not a local docker build: ${props.image}"; + return props.image; } let sources = props.sources ?? ["**/*"];