Skip to content
This repository was archived by the owner on Jan 16, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Bootstrap
run: |
./bin/bootstrap
- name: Setup
run: |
./bin/setup
- name: Build
run: |
./bin/build
- name: Test 2.5
run: |
./bin/test 2.5
- name: Test 2.7
run: |
./bin/test 2.7
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
9 changes: 5 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM lambci/lambda:build-ruby2.5
FROM amazon/aws-sam-cli-build-image-ruby2.5

WORKDIR /build

Expand All @@ -18,6 +18,7 @@ RUN git clone https://github.com/NixOS/patchelf.git && \
make install

RUN echo '== MySQL Connector =='
RUN yum install -y cmake
RUN curl -L https://downloads.mysql.com/archives/get/p/19/file/mysql-connector-c-6.1.11-src.tar.gz > mysql-connector-c-6.1.11-src.tar.gz && \
tar -xf mysql-connector-c-6.1.11-src.tar.gz && \
cd mysql-connector-c-6.1.11-src && \
Expand All @@ -33,13 +34,13 @@ RUN rm -rf /usr/local/mysql/lib/libmysqlclient.so* && \

RUN echo '== Patch MySQL2 Gem =='
RUN patchelf --add-needed librt.so.1 \
/var/runtime/gems/mysql2-0.5.3/lib/mysql2/mysql2.so && \
"/var/lang/lib/ruby/gems/2.5.0/gems/mysql2-${MYSQL_VERSION}/lib/mysql2/mysql2.so" && \
patchelf --add-needed libstdc++.so.6 \
/var/runtime/gems/mysql2-0.5.3/lib/mysql2/mysql2.so
"/var/lang/lib/ruby/gems/2.5.0/gems/mysql2-${MYSQL_VERSION}/lib/mysql2/mysql2.so"

RUN echo '== Share Files =='
RUN mkdir -p /build/share && \
cp -r /var/runtime/gems/mysql2-$MYSQL_VERSION/* /build/share && \
cp -r "/var/lang/lib/ruby/gems/2.5.0/gems/mysql2-${MYSQL_VERSION}"/* /build/share && \
rm -rf /build/share/ext \
/build/share/README.md \
/build/share/support
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

[![Test](https://github.com/customink/mysql2-lambda/actions/workflows/test.yml/badge.svg)](https://github.com/customink/mysql2-lambda/actions/workflows/test.yml)

# Mysql2 Lambda Gem

<a href="https://github.com/customink/lamby"><img src="https://user-images.githubusercontent.com/2381/59363668-89edeb80-8d03-11e9-9985-2ce14361b7e3.png" alt="Lamby: Simple Rails & AWS Lambda Integration using Rack." align="right" width="300" /></a>Very simple [Mysql2](https://github.com/brianmario/mysql2) gem precompiled for Amazon Linux 1 & 2 (Ruby 2.5 & 2.7) with statically linked `libmysqlclient` ready for any AWS Lambda usage, including Rails.
Expand All @@ -14,7 +17,7 @@ Part of a suite of open source projects from **[Lamby: Simple Rails & AWS Lambda

## Methodology

We used the `lambci/lambda:build-ruby2.5` Docker image from the [docker-lambda](https://github.com/lambci/docker-lambda) project to build the MySQL Connector/C's `libmysqlclient`. From there we ensure the Mysql2 gem statically includes that library. The resulting packaged gem and `mysql2.so` file looks something like this.
We used the `amazon/aws-sam-cli-build-image-ruby2.5` Docker image from the [SAM CLI](https://github.com/aws/aws-sam-cli) project to build the MySQL Connector/C's `libmysqlclient`. From there we ensure the Mysql2 gem statically includes that library. The resulting packaged gem and `mysql2.so` file looks something like this.

```shell
$ ldd mysql2.so
Expand All @@ -41,7 +44,12 @@ Clone or fork this repository, make sure you have Docker installed, then run the
./bin/build
```

You will now have a packaged gem in the root of your project.
You will now have a packaged gem in the root of your project. Simple tests to make sure it works.

```shell
./bin/test 2.5
./bin/test 2.7
```

## License

Expand Down
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.5.3
13 changes: 13 additions & 0 deletions bin/_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
set -e

MYSQL_VERSION=$(cat VERSION)
MYSQL_BUILD_VERSION=$(cat BUILD)
MYSQL_GEM_VERSION="${MYSQL_VERSION}.${MYSQL_BUILD_VERSION}"
MYSQL_GEM_FILE="mysql2-lambda-${MYSQL_GEM_VERSION}.gem"

echo "Installing ${MYSQL_GEM_FILE} file..."
gem install --local "$MYSQL_GEM_FILE"

sleep 10
ruby ./bin/_test-mysql2.rb
12 changes: 12 additions & 0 deletions bin/_test-mysql2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'mysql2'

EXPECTED = [{"one"=>1}]

client = Mysql2::Client.new host: "mysql", username: "root", password: "root"
result = client.query("SELECT 1 AS one").each

if result == EXPECTED
puts "PASSED! 🎉"
else
abort "Result #{result.inspect} failed to be expected #{EXPECTED.inspect}"
end
1 change: 0 additions & 1 deletion bin/bootstrap
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/bin/bash

set -e

docker -v $1 &> /dev/null
Expand Down
7 changes: 4 additions & 3 deletions bin/build
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#!/bin/bash

set -e

export IMAGE_NAME=mysql2-lambda
export MYSQL_VERSION=${MYSQL_VERSION:=0.5.3}
IMAGE_NAME=mysql2-lambda

rm -rf ./share
mkdir ./share
Expand All @@ -15,9 +13,12 @@ docker run \
sh -c "cp -r /build/share/* /share"

rm -rf ./gem && mkdir ./gem
rm -rf */.gem
cp -r ./share/* ./gem
cp ./README.md ./gem
cp ./CHANGELOG.md ./gem
cp ./VERSION ./gem
cp ./BUILD ./gem
cp ./mysql2-lambda.gemspec ./gem
cp ./lib/mysql2-lambda.rb ./gem/lib

Expand Down
11 changes: 11 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
set -e

IMAGE_NAME=mysql2-lambda

docker run \
--interactive \
--tty \
--rm \
--entrypoint "/bin/bash" \
$IMAGE_NAME
9 changes: 4 additions & 5 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#!/bin/bash

set -e

export IMAGE_NAME=mysql2-lambda
export MYSQL_VERSION=${MYSQL_VERSION:=0.5.3}
IMAGE_NAME=mysql2-lambda
MYSQL_VERSION=$(cat VERSION)

docker pull lambci/lambda:build-ruby2.5
docker pull amazon/aws-sam-cli-build-image-ruby2.5

docker build \
--no-cache \
--build-arg MYSQL_VERSION=${MYSQL_VERSION} \
--build-arg "MYSQL_VERSION=${MYSQL_VERSION}" \
--tag $IMAGE_NAME \
.
7 changes: 7 additions & 0 deletions bin/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -e

COMPOSE_FILE="docker-compose-$1.yml" \
docker-compose \
run \
test$1
1 change: 0 additions & 1 deletion bin/update
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/bin/bash

set -e

./bin/setup
15 changes: 15 additions & 0 deletions docker-compose-2.5.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: "3.8"
services:
test2.5:
image: public.ecr.aws/lambda/ruby:2.5
entrypoint: ./bin/_test
volumes:
- $PWD:/var/task
depends_on:
- mysql
links:
- mysql
mysql:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=root
15 changes: 15 additions & 0 deletions docker-compose-2.7.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: "3.8"
services:
test2.7:
image: public.ecr.aws/lambda/ruby:2.7
entrypoint: ./bin/_test
volumes:
- $PWD:/var/task
depends_on:
- mysql
links:
- mysql
mysql:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=root
2 changes: 1 addition & 1 deletion mysql2-lambda.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'mysql2-lambda'
s.version = '0.5.3.0'
s.version = "#{File.read('VERSION').strip}.#{File.read('BUILD').strip}"
s.authors = ['Ken Collins']
s.license = "MIT"
s.email = ['kcollins@customink.com']
Expand Down