Skip to content

Commit

Permalink
Generate the Rails application with the same user as the caller
Browse files Browse the repository at this point in the history
Read the user id and group id of the caller and pass them to the Docker
so the generate folders are with the same user as the caller.

Fixes #4.
  • Loading branch information
rafaelfranca committed Mar 27, 2024
1 parent 57af0ab commit 8901a75
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 5 deletions.
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.

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

[dependencies]
clap = { version = "4.5.1", features = ["derive"] }
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
17 changes: 14 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,26 @@ use crate::docker_client::DockerClient;
fn main() {
let cli = Cli::parse();

// read the content of the DOCKERFILE and store it in a variable
let dockerfile = include_bytes!("../Dockerfile");
let dockerfile: &[u8];
let user_id: Option<u32>;
let group_id: Option<u32>;

if cfg!(windows) {
user_id = None;
group_id = None;
dockerfile = include_bytes!("../Dockerfile.windows");
} else {
user_id = Some(users::get_current_uid());
group_id = Some(users::get_current_gid());
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)
let mut child = DockerClient::build_image(&ruby_version, &rails_version, user_id, group_id)
.spawn()
.expect("Failed to execute process");

Expand Down

0 comments on commit 8901a75

Please sign in to comment.