Skip to content

Commit

Permalink
use debian base image for ruby 2 (#726)
Browse files Browse the repository at this point in the history
* use debian base image for ruby 2

* remove ruby version from ruby 2 gemfile lock

* lints and clippy

* add comment
  • Loading branch information
coffee-cup authored Dec 17, 2022
1 parent cb9d095 commit 698230d
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ futures = "0.3"
portpicker = "0.1.1"
tokio = { version = "1.21.2", features = ["full"] }
async-trait = "0.1.58"
semver = "1.0.14"

[dev-dependencies]
dotenv-parser = "0.1.3"
Expand Down
1 change: 1 addition & 0 deletions examples/ruby-2/.ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.6.6
2 changes: 2 additions & 0 deletions examples/ruby-2/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

source 'https://rubygems.org'
11 changes: 11 additions & 0 deletions examples/ruby-2/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
GEM
remote: https://rubygems.org/
specs:

PLATFORMS
arm64-darwin-21

DEPENDENCIES

BUNDLED WITH
2.3.7
1 change: 1 addition & 0 deletions examples/ruby-2/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: ruby app.rb
1 change: 1 addition & 0 deletions examples/ruby-2/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts "Hello from Ruby 2"
15 changes: 8 additions & 7 deletions src/nixpacks/nix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,15 @@ fn nix_expression_for_group(group: &NixGroup) -> String {

// If the openssl library is added, set the OPENSSL_DIR and OPENSSL_LIB_DIR environment variables
// In the future, we will probably want a generic way for providers to set variables based off Nix package locations
let openssl_dirs = if libs.contains("openssl") {
formatdoc! {"
export OPENSSL_DIR=\"${{openssl.dev}}\"
export OPENSSL_LIB_DIR=\"${{openssl.out}}/lib\"
let openssl_dirs =
if let Some(openssl_lib) = group.libs.iter().find(|lib| lib.contains("openssl")) {
formatdoc! {"
export OPENSSL_DIR=\"${{{openssl_lib}.dev}}\"
export OPENSSL_LIB_DIR=\"${{{openssl_lib}.out}}/lib\"
"}
} else {
String::new()
};
} else {
String::new()
};

let name = format!("{archive}-env");
let nix_expression = formatdoc! {"
Expand Down
26 changes: 25 additions & 1 deletion src/providers/ruby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::{node::NodeProvider, Provider};
use crate::nixpacks::{
app::App,
environment::{Environment, EnvironmentVariables},
images::DEBIAN_BASE_IMAGE,
nix::pkg::Pkg,
plan::{
phase::{Phase, StartPhase},
Expand All @@ -10,6 +11,7 @@ use crate::nixpacks::{
};
use anyhow::{bail, Ok, Result};
use regex::Regex;
use semver::Version;

pub struct RubyProvider {}

Expand Down Expand Up @@ -51,6 +53,11 @@ impl Provider for RubyProvider {

plan.add_variables(self.get_environment_variables(app)?);

// Temporary fix to allow using older versions of ruby
if self.requires_openssl_1(app)? {
plan.build_image = Some(DEBIAN_BASE_IMAGE.to_string());
}

Ok(Some(plan))
}
}
Expand Down Expand Up @@ -80,14 +87,16 @@ impl RubyProvider {
setup.add_apt_pkgs(vec![String::from("libicu-dev")]);
}

let ruby_version = self.get_ruby_version(app)?;

setup.add_cmd(format!(
"curl -sSL https://get.rvm.io | bash -s stable \
&& . /etc/profile.d/rvm.sh \
&& rvm install {ruby_version} \
&& rvm --default use {ruby_version} \
&& gem install {bundler_version} \
&& rm -rf /usr/local/rvm/src",
ruby_version = self.get_ruby_version(app)?,
ruby_version = ruby_version,
bundler_version = self.get_bundler_version(app)
));

Expand Down Expand Up @@ -215,6 +224,21 @@ impl RubyProvider {
}
}

fn requires_openssl_1(&self, app: &App) -> Result<bool> {
let ruby_version = self.get_ruby_version(app)?;
match Version::parse(ruby_version.trim_start_matches("ruby-")) {
std::result::Result::Ok(v) => {
// Version 3.1.0 and above work with openssl 3.0
if v.major >= 3 && v.minor >= 1 {
Ok(false)
} else {
Ok(true)
}
}
std::result::Result::Err(_) => Ok(false),
}
}

fn is_rails_app(&self, app: &App) -> bool {
app.includes_file("config/application.rb")
&& app
Expand Down
58 changes: 58 additions & 0 deletions tests/snapshots/generate_plan_tests__ruby_2.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
source: tests/generate_plan_tests.rs
expression: plan
---
{
"providers": [],
"buildImage": "[build_image]",
"variables": {
"BUNDLE_GEMFILE": "/app/Gemfile",
"GEM_HOME": "/usr/local/rvm/gems/2.6.6",
"GEM_PATH": "/usr/local/rvm/gems/2.6.6:/usr/local/rvm/gems/2.6.6@global",
"MALLOC_ARENA_MAX": "2",
"NIXPACKS_METADATA": "ruby"
},
"phases": {
"build": {
"name": "build",
"dependsOn": [
"install"
]
},
"install": {
"name": "install",
"dependsOn": [
"setup"
],
"cmds": [
"bundle install"
],
"onlyIncludeFiles": [
"Gemfile",
"Gemfile.lock"
],
"cacheDirectories": [
"/root/.bundle/cache"
],
"paths": [
"/usr/local/rvm/rubies/2.6.6/bin",
"/usr/local/rvm/gems/2.6.6/bin",
"/usr/local/rvm/gems/2.6.6@global/bin"
]
},
"setup": {
"name": "setup",
"aptPkgs": [
"procps"
],
"cmds": [
"curl -sSL https://get.rvm.io | bash -s stable && . /etc/profile.d/rvm.sh && rvm install 2.6.6 && rvm --default use 2.6.6 && gem install bundler:2.3.7 && rm -rf /usr/local/rvm/src",
"echo 'source /usr/local/rvm/scripts/rvm' >> /root/.profile"
],
"onlyIncludeFiles": []
}
},
"start": {
"cmd": "ruby app.rb"
}
}

0 comments on commit 698230d

Please sign in to comment.