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

Generate the Rails application with the same user as the caller #10

Merged
merged 2 commits into from
Mar 28, 2024
Merged
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
17 changes: 17 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ edition = "2021"

[dependencies]
clap = { version = "4.5.1", features = ["derive"] }
[target.'cfg(unix)'.dependencies]
users = "0.11.0"

[dev-dependencies]
assert_cmd = "2.0.14"
Expand Down
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
ARG RUBY_VERSION=3.2.3
FROM ruby:${RUBY_VERSION}
ARG USER_ID=1000
ARG GROUP_ID=1000
RUN groupadd -g $GROUP_ID app && useradd -u $USER_ID -g app -m app
USER app
ARG RAILS_VERSION
# Install Rails based on the version specified but if not specified, install the latest version.
RUN if [ -z "$RAILS_VERSION" ] ; then gem install rails ; else gem install rails -v $RAILS_VERSION ; fi
5 changes: 5 additions & 0 deletions Dockerfile.windows
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ARG RUBY_VERSION=3.2.3
FROM ruby:${RUBY_VERSION}
ARG RAILS_VERSION
# Install Rails based on the version specified but if not specified, install the latest version.
RUN if [ -z "$RAILS_VERSION" ] ; then gem install rails ; else gem install rails -v $RAILS_VERSION ; fi
14 changes: 12 additions & 2 deletions src/docker_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@ use std::process::{Command, Stdio};
pub struct DockerClient {}

impl DockerClient {
pub fn build_image(ruby_version: &str, rails_version: &str) -> Command {
pub fn build_image(
ruby_version: &str,
rails_version: &str,
user_id: Option<u32>,
group_id: Option<u32>,
) -> Command {
let mut command = Command::new("docker");

command
.arg("build")
.arg("--build-arg")
.arg(format!("RUBY_VERSION={}", ruby_version))
.arg("--build-arg")
.arg(format!("RAILS_VERSION={}", rails_version))
.arg(format!("RAILS_VERSION={}", rails_version));

user_id.map(|id| command.args(["--build-arg", &format!("USER_ID={}", id)]));
group_id.map(|id| command.args(["--build-arg", &format!("GROUP_ID={}", id)]));

command
.arg("-t")
.arg(format!("rails-new-{}-{}", ruby_version, rails_version))
.arg("-")
Expand Down
20 changes: 13 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,30 @@ use clap::Parser;

use crate::docker_client::DockerClient;

#[cfg_attr(unix, path = "unix.rs")]
#[cfg_attr(windows, path = "windows.rs")]
mod os_specific;

fn main() {
let cli = Cli::parse();

// read the content of the DOCKERFILE and store it in a variable
let dockerfile = include_bytes!("../Dockerfile");

let ruby_version = cli.ruby_version;
let rails_version = cli.rails_version;

// Run docker build --build-arg RUBY_VERSION=$RUBY_VERSION --build-arg RAILS_VERSION=$RAILS_VERSION -t rails-new-$RUBY_VERSION-$RAILS_VERSION
// passing the content of DOCKERFILE to the command stdin
let mut child = DockerClient::build_image(&ruby_version, &rails_version)
.spawn()
.expect("Failed to execute process");
let mut child = DockerClient::build_image(
&ruby_version,
&rails_version,
os_specific::get_user_id(),
os_specific::get_group_id(),
)
.spawn()
.expect("Failed to execute process");

let mut stdin = child.stdin.take().expect("Failed to open stdin");
std::thread::spawn(move || {
stdin.write_all(dockerfile).unwrap();
stdin.write_all(os_specific::dockerfile_content()).unwrap();
});

let status = child.wait().expect("failed to wait on child");
Expand Down
11 changes: 11 additions & 0 deletions src/unix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub fn dockerfile_content() -> &'static [u8] {
include_bytes!("../Dockerfile")
}

pub fn get_user_id() -> Option<u32> {
Some(users::get_current_uid())
}

pub fn get_group_id() -> Option<u32> {
Some(users::get_current_gid())
}
11 changes: 11 additions & 0 deletions src/windows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub fn dockerfile_content() -> &'static [u8] {
include_bytes!("../Dockerfile.windows")
}

pub fn get_user_id() -> Option<u32> {
None
}

pub fn get_group_id() -> Option<u32> {
None
}
Loading