From 03212e25b1570ba58bb958e3e86767878886f348 Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Fri, 12 Jan 2024 15:11:56 -0500 Subject: [PATCH 01/12] autostart and docker compose --- src/index.js | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/index.js b/src/index.js index c51e6f1..71e0849 100644 --- a/src/index.js +++ b/src/index.js @@ -406,7 +406,7 @@ class LocalstackPlugin { * Start the LocalStack container in Docker, if it is not running yet. */ startLocalStack() { - if (!this.config.autostart) { + if (!this.config.autostart || !this.isActive()) { return Promise.resolve(); } @@ -452,27 +452,42 @@ class LocalstackPlugin { return containerID; } + const startContainer = () => { + this.log('Starting LocalStack in Docker. This can take a while.'); + const cwd = process.cwd(); + const env = this.clone(process.env); + env.DEBUG = '1'; + env.LAMBDA_EXECUTOR = env.LAMBDA_EXECUTOR || 'docker'; + env.LAMBDA_REMOTE_DOCKER = env.LAMBDA_REMOTE_DOCKER || '0'; + env.DOCKER_FLAGS = (env.DOCKER_FLAGS || '') + ` -d -v ${cwd}:${cwd}`; + env.START_WEB = env.START_WEB || '0'; + const maxBuffer = (+env.EXEC_MAXBUFFER)||50*1000*1000; // 50mb buffer to handle output + if (this.shouldRunDockerSudo()) { + env.DOCKER_CMD = 'sudo docker'; + } + const options = {env: env, maxBuffer}; + return exec('localstack start', options).then(getContainer) + .then((containerID) => addNetworks(containerID)) + .then((containerID) => checkStatus(containerID)); + } + + const startCompose = () => { + return exec(`docker-compose -f ${this.config.compose_file} `).then(getContainer) + } + return getContainer().then( (containerID) => { if(containerID) { return; } - this.log('Starting LocalStack in Docker. This can take a while.'); - const cwd = process.cwd(); - const env = this.clone(process.env); - env.DEBUG = '1'; - env.LAMBDA_EXECUTOR = env.LAMBDA_EXECUTOR || 'docker'; - env.LAMBDA_REMOTE_DOCKER = env.LAMBDA_REMOTE_DOCKER || '0'; - env.DOCKER_FLAGS = (env.DOCKER_FLAGS || '') + ` -d -v ${cwd}:${cwd}`; - env.START_WEB = env.START_WEB || '0'; - const maxBuffer = (+env.EXEC_MAXBUFFER)||50*1000*1000; // 50mb buffer to handle output - if (this.shouldRunDockerSudo()) { - env.DOCKER_CMD = 'sudo docker'; + + if(this.config.compose_file){ + console.log("Using docker-compose file: " + this.config.compose_file) + return startCompose(); } - const options = {env: env, maxBuffer}; - return exec('localstack start', options).then(getContainer) - .then((containerID) => addNetworks(containerID)) - .then((containerID) => checkStatus(containerID)); + console.log("No docker-compose file: ") + + return startContainer(); } ); } From 7c53cfc39e28ac6b330f5950ccf2f35c3ab4adae Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Fri, 12 Jan 2024 23:21:56 -0500 Subject: [PATCH 02/12] remove console log --- example/service/serverless.yml | 12 ++++++------ example/service/test/serverless.yml | 2 ++ 2 files changed, 8 insertions(+), 6 deletions(-) create mode 100644 example/service/test/serverless.yml diff --git a/example/service/serverless.yml b/example/service/serverless.yml index 0858413..858ff6f 100644 --- a/example/service/serverless.yml +++ b/example/service/serverless.yml @@ -4,18 +4,17 @@ provider: name: aws profile: ${opt:profile, self:custom.profile} stage: ${opt:stage, self:custom.defaultStage} - runtime: nodejs14.x + runtime: nodejs16.x lambdaHashingVersion: '20201221' -plugins: - - serverless-localstack - custom: defaultStage: local profile: default localstack: debug: true stages: [local] + autostart: true + compose_file: /home/cristopher/Projects/serverless-localstack/docker-compose.yml functions: hello: @@ -23,5 +22,6 @@ functions: environment: SSM_VAR: ${ssm:abc} # CF_VAR: ${cf:def} - events: - - sns: ${env:EXISTING_TOPIC_ARN} + +plugins: + - serverless-localstack diff --git a/example/service/test/serverless.yml b/example/service/test/serverless.yml new file mode 100644 index 0000000..e31ae3d --- /dev/null +++ b/example/service/test/serverless.yml @@ -0,0 +1,2 @@ +component: test +inputs: From bd0aec527019643b93593d23d3e8331904b5e96b Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Fri, 12 Jan 2024 23:24:05 -0500 Subject: [PATCH 03/12] remove console log --- src/index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/index.js b/src/index.js index 71e0849..065420a 100644 --- a/src/index.js +++ b/src/index.js @@ -482,10 +482,8 @@ class LocalstackPlugin { } if(this.config.compose_file){ - console.log("Using docker-compose file: " + this.config.compose_file) return startCompose(); } - console.log("No docker-compose file: ") return startContainer(); } From 296b1ed6329b87464b85a6aec66c18381cde1f70 Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Mon, 22 Jan 2024 16:14:57 -0500 Subject: [PATCH 04/12] readme and move config inside docker obj --- README.md | 3 ++- src/index.js | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 16c0635..81d1464 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ custom: docker: # Enable this flag to run "docker ..." commands as sudo sudo: False + compose_file: /home/localstack_compose.yml # optional to use docker compose instead of docker or localstack cli stages: local: ... @@ -207,7 +208,7 @@ custom: ``` ## Change Log - +* v1.2.1: Add docker_compose config and fix autostart when plugin is not active * v1.1.3: Fix replacing host from environment variable `AWS_ENDPOINT_URL` * v1.1.2: Unify construction of target endpoint URL, add support for configuring `AWS_ENDPOINT_URL` * v1.1.1: Fix layer deployment if `mountCode` is enabled by always packaging and deploying diff --git a/src/index.js b/src/index.js index 065420a..34f50ec 100644 --- a/src/index.js +++ b/src/index.js @@ -472,7 +472,7 @@ class LocalstackPlugin { } const startCompose = () => { - return exec(`docker-compose -f ${this.config.compose_file} `).then(getContainer) + return exec(`docker-compose -f ${this.config.docker.compose_file} `).then(getContainer) } return getContainer().then( @@ -481,7 +481,7 @@ class LocalstackPlugin { return; } - if(this.config.compose_file){ + if(this.config.docker && this.config.docker.compose_file){ return startCompose(); } From 475c7f766906f9ee7a6123f74c7275c38cd9277e Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Mon, 22 Jan 2024 16:15:06 -0500 Subject: [PATCH 05/12] 1.2.1 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5014cab..4010254 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "serverless-localstack", - "version": "1.1.3", + "version": "1.2.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "serverless-localstack", - "version": "1.1.3", + "version": "1.2.1", "license": "MIT", "dependencies": { "adm-zip": "^0.5.10", diff --git a/package.json b/package.json index 82c6e49..280b02f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-localstack", - "version": "1.1.3", + "version": "1.2.1", "description": "Connect Serverless to LocalStack!", "main": "src/index.js", "scripts": { From ab836ac61df8151020e1d2e55306734cb971df82 Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Wed, 24 Jan 2024 12:50:30 -0500 Subject: [PATCH 06/12] fix docker compose command --- docker-compose.yml | 18 ------------------ src/index.js | 2 +- 2 files changed, 1 insertion(+), 19 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index b8486d7..a046248 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,23 +1,5 @@ version: '2' services: - dev-env: - build: - context: . - working_dir: /app - volumes: - - .:/app - - ~/.aws/:/root/.aws - - ~/.gitconfig:/root/.gitconfig - - ~/.gitconfig.local:/root/.gitconfig.local - environment: - - AWS_ACCESS_KEY_ID - - AWS_SECRET_ACCESS_KEY - - AWS_PROFILE - - AWS_SESSION_TOKEN - - AWS_SECURITY_TOKEN - - SNYK_TOKEN - entrypoint: - - /bin/bash localstack: image: localstack/localstack ports: diff --git a/src/index.js b/src/index.js index 34f50ec..2bd943f 100644 --- a/src/index.js +++ b/src/index.js @@ -472,7 +472,7 @@ class LocalstackPlugin { } const startCompose = () => { - return exec(`docker-compose -f ${this.config.docker.compose_file} `).then(getContainer) + return exec(`docker-compose -f ${this.config.docker.compose_file} up -d`).then(getContainer) } return getContainer().then( From b19122f55ed23c8ef3f817abc439bef391209025 Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Wed, 24 Jan 2024 13:18:21 -0500 Subject: [PATCH 07/12] fix version, delete unused file and update compose file --- README.md | 2 +- docker-compose.yml | 23 ++++++++++------------- example/service/test/serverless.yml | 2 -- package-lock.json | 2 +- package.json | 2 +- src/index.js | 2 +- 6 files changed, 14 insertions(+), 19 deletions(-) delete mode 100644 example/service/test/serverless.yml diff --git a/README.md b/README.md index 81d1464..1dd5513 100644 --- a/README.md +++ b/README.md @@ -208,7 +208,7 @@ custom: ``` ## Change Log -* v1.2.1: Add docker_compose config and fix autostart when plugin is not active +* v1.2.0: Add docker-compose config and fix autostart when plugin is not active * v1.1.3: Fix replacing host from environment variable `AWS_ENDPOINT_URL` * v1.1.2: Unify construction of target endpoint URL, add support for configuring `AWS_ENDPOINT_URL` * v1.1.1: Fix layer deployment if `mountCode` is enabled by always packaging and deploying diff --git a/docker-compose.yml b/docker-compose.yml index a046248..cdc4442 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,18 +1,15 @@ -version: '2' +version: "3.8" + services: localstack: + container_name: "${LOCALSTACK_DOCKER_NAME:-localstack-main}" image: localstack/localstack ports: - - "4567-4582:4567-4582" - - "8080:8080" + - "127.0.0.1:4566:4566" # LocalStack Gateway + - "127.0.0.1:4510-4559:4510-4559" # external services port range environment: - # Only start a subset of services required for testing. - - SERVICES=s3,sns,sqs,apigateway,lambda,dynamodb,dynamodbstreams,cloudformation - # - DEBUG=${DEBUG- } - # - DATA_DIR=${DATA_DIR- } - # - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR- } - # - KINESIS_ERROR_PROBABILITY=${KINESIS_ERROR_PROBABILITY- } - # - DOCKER_HOST=unix:///var/run/docker.sock - # volumes: - # - "${TMP_DIR:-/tmp/localstack}:${TMP_DIR:-/tmp/localstack}" - # - "/var/run/docker.sock:/var/run/docker.sock" + # LocalStack configuration: https://docs.localstack.cloud/references/configuration/ + - DEBUG=${DEBUG:-0} + volumes: + - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack" + - "/var/run/docker.sock:/var/run/docker.sock" diff --git a/example/service/test/serverless.yml b/example/service/test/serverless.yml deleted file mode 100644 index e31ae3d..0000000 --- a/example/service/test/serverless.yml +++ /dev/null @@ -1,2 +0,0 @@ -component: test -inputs: diff --git a/package-lock.json b/package-lock.json index 4010254..f4ed869 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "serverless-localstack", - "version": "1.2.1", + "version": "1.2.0", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index 280b02f..405b804 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-localstack", - "version": "1.2.1", + "version": "1.2.0", "description": "Connect Serverless to LocalStack!", "main": "src/index.js", "scripts": { diff --git a/src/index.js b/src/index.js index 2bd943f..cfb30f9 100644 --- a/src/index.js +++ b/src/index.js @@ -406,7 +406,7 @@ class LocalstackPlugin { * Start the LocalStack container in Docker, if it is not running yet. */ startLocalStack() { - if (!this.config.autostart || !this.isActive()) { + if (!(this.config.autostart && this.isActive())) { return Promise.resolve(); } From 4e4a81924acb70bd7c968fc455f40a037207fbaf Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Wed, 24 Jan 2024 13:29:37 -0500 Subject: [PATCH 08/12] getContainer considers pro image --- src/index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index cfb30f9..7182665 100644 --- a/src/index.js +++ b/src/index.js @@ -413,7 +413,13 @@ class LocalstackPlugin { const getContainer = () => { return exec('docker ps').then( (stdout) => { - const exists = stdout.split('\n').filter((line) => line.indexOf('localstack/localstack') >= 0 || line.indexOf('localstack_localstack') >= 0); + const exists = stdout.split('\n').filter( + (line) => ( + line.indexOf('localstack/localstack') >= 0 || + line.indexOf('localstack/localstack-pro') >= 0 || + line.indexOf('localstack_localstack') >= 0 + ) + ); if (exists.length) { return exists[0].replace('\t', ' ').split(' ')[0]; } From 61fa289229f6cc372ac5b2ef2aa0e2b5242585e8 Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Wed, 24 Jan 2024 15:00:55 -0500 Subject: [PATCH 09/12] remove -d from localstack cli options --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 7182665..0528ce7 100644 --- a/src/index.js +++ b/src/index.js @@ -465,7 +465,7 @@ class LocalstackPlugin { env.DEBUG = '1'; env.LAMBDA_EXECUTOR = env.LAMBDA_EXECUTOR || 'docker'; env.LAMBDA_REMOTE_DOCKER = env.LAMBDA_REMOTE_DOCKER || '0'; - env.DOCKER_FLAGS = (env.DOCKER_FLAGS || '') + ` -d -v ${cwd}:${cwd}`; + env.DOCKER_FLAGS = (env.DOCKER_FLAGS || '') + ` -v ${cwd}:${cwd}`; env.START_WEB = env.START_WEB || '0'; const maxBuffer = (+env.EXEC_MAXBUFFER)||50*1000*1000; // 50mb buffer to handle output if (this.shouldRunDockerSudo()) { From 83de5f5bb5aea60e46842b635e30025052d55916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristopher=20Pinz=C3=B3n?= Date: Fri, 26 Jan 2024 12:43:40 -0500 Subject: [PATCH 10/12] package-lock correct version Co-authored-by: steffyP --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index f4ed869..e603623 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "serverless-localstack", - "version": "1.2.1", + "version": "1.2.0", "license": "MIT", "dependencies": { "adm-zip": "^0.5.10", From 163a4639752ac1451f0b4a3514403badccd1d844 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristopher=20Pinz=C3=B3n?= Date: Fri, 26 Jan 2024 12:44:09 -0500 Subject: [PATCH 11/12] rename sample compose file Co-authored-by: steffyP --- example/service/serverless.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/service/serverless.yml b/example/service/serverless.yml index 858ff6f..ea87fe8 100644 --- a/example/service/serverless.yml +++ b/example/service/serverless.yml @@ -14,7 +14,7 @@ custom: debug: true stages: [local] autostart: true - compose_file: /home/cristopher/Projects/serverless-localstack/docker-compose.yml + compose_file: /home/localstack/Projects/serverless-localstack/docker-compose.yml functions: hello: From 9540e110faac85eaf1fa74fb25d20ecb3b27878c Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Fri, 26 Jan 2024 13:48:37 -0500 Subject: [PATCH 12/12] log and relocate -d option --- src/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 0528ce7..40204ee 100644 --- a/src/index.js +++ b/src/index.js @@ -472,12 +472,13 @@ class LocalstackPlugin { env.DOCKER_CMD = 'sudo docker'; } const options = {env: env, maxBuffer}; - return exec('localstack start', options).then(getContainer) + return exec('localstack start -d', options).then(getContainer) .then((containerID) => addNetworks(containerID)) .then((containerID) => checkStatus(containerID)); } const startCompose = () => { + this.log('Starting LocalStack using the provided docker-compose file. This can take a while.'); return exec(`docker-compose -f ${this.config.docker.compose_file} up -d`).then(getContainer) }