From c6405de75f79d46f185f5e60e4e1bda7caa9ce0d Mon Sep 17 00:00:00 2001 From: Thijmen Stavenuiter Date: Thu, 13 Jun 2024 13:02:58 +0200 Subject: [PATCH 01/17] Add example --- examples/node-fetch-network/.gitignore | 169 +++++++++++++++++++++ examples/node-fetch-network/fetchData.js | 23 +++ examples/node-fetch-network/package.json | 17 +++ examples/node-fetch-network/pnpm-lock.yaml | 59 +++++++ 4 files changed, 268 insertions(+) create mode 100644 examples/node-fetch-network/.gitignore create mode 100644 examples/node-fetch-network/fetchData.js create mode 100644 examples/node-fetch-network/package.json create mode 100644 examples/node-fetch-network/pnpm-lock.yaml diff --git a/examples/node-fetch-network/.gitignore b/examples/node-fetch-network/.gitignore new file mode 100644 index 000000000..f81d56eaa --- /dev/null +++ b/examples/node-fetch-network/.gitignore @@ -0,0 +1,169 @@ +# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore + +# Logs + +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) + +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data + +pids +_.pid +_.seed +\*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover + +lib-cov + +# Coverage directory used by tools like istanbul + +coverage +\*.lcov + +# nyc test coverage + +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + +.grunt + +# Bower dependency directory (https://bower.io/) + +bower_components + +# node-waf configuration + +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) + +build/Release + +# Dependency directories + +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) + +web_modules/ + +# TypeScript cache + +\*.tsbuildinfo + +# Optional npm cache directory + +.npm + +# Optional eslint cache + +.eslintcache + +# Optional stylelint cache + +.stylelintcache + +# Microbundle cache + +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history + +.node_repl_history + +# Output of 'npm pack' + +\*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# dotenv environment variable files + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) + +.cache +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt +dist + +# Gatsby files + +.cache/ + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp +.cache + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.\* diff --git a/examples/node-fetch-network/fetchData.js b/examples/node-fetch-network/fetchData.js new file mode 100644 index 000000000..389be2566 --- /dev/null +++ b/examples/node-fetch-network/fetchData.js @@ -0,0 +1,23 @@ +import fetch from 'node-fetch'; + +const url = process.env.REMOTE_URL; + +if (!url) { + console.error('REMOTE_URL is not defined in the environment variables.'); + process.exit(1); +} + +fetch(url) + .then(response => { + if (!response.ok) { + throw new Error(`Network response was not ok: ${response.statusText}`); + } + return response.statusText; + }) + .then(data => { + console.log('Fetched data:', data); + }) + .catch(error => { + console.error('Fetching data failed:', error); + process.exit(1); + }); \ No newline at end of file diff --git a/examples/node-fetch-network/package.json b/examples/node-fetch-network/package.json new file mode 100644 index 000000000..9d2c8cea6 --- /dev/null +++ b/examples/node-fetch-network/package.json @@ -0,0 +1,17 @@ +{ + "type": "module", + "name": "your-project-name", + "version": "1.0.0", + "description": "A project to fetch data from a remote URL", + "main": "index.js", + "scripts": { + "build": "node fetchData.js", + "fetch": "node fetchData.js" + }, + "dependencies": { + "dotenv": "^10.0.0", + "node-fetch": "^3.0.0" + }, + "author": "Your Name", + "license": "MIT" +} \ No newline at end of file diff --git a/examples/node-fetch-network/pnpm-lock.yaml b/examples/node-fetch-network/pnpm-lock.yaml new file mode 100644 index 000000000..bde2d98e1 --- /dev/null +++ b/examples/node-fetch-network/pnpm-lock.yaml @@ -0,0 +1,59 @@ +lockfileVersion: '6.0' + +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false + +dependencies: + dotenv: + specifier: ^10.0.0 + version: 10.0.0 + node-fetch: + specifier: ^3.0.0 + version: 3.3.2 + +packages: + + /data-uri-to-buffer@4.0.1: + resolution: {integrity: sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==} + engines: {node: '>= 12'} + dev: false + + /dotenv@10.0.0: + resolution: {integrity: sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==} + engines: {node: '>=10'} + dev: false + + /fetch-blob@3.2.0: + resolution: {integrity: sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==} + engines: {node: ^12.20 || >= 14.13} + dependencies: + node-domexception: 1.0.0 + web-streams-polyfill: 3.3.3 + dev: false + + /formdata-polyfill@4.0.10: + resolution: {integrity: sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==} + engines: {node: '>=12.20.0'} + dependencies: + fetch-blob: 3.2.0 + dev: false + + /node-domexception@1.0.0: + resolution: {integrity: sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==} + engines: {node: '>=10.5.0'} + dev: false + + /node-fetch@3.3.2: + resolution: {integrity: sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==} + engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} + dependencies: + data-uri-to-buffer: 4.0.1 + fetch-blob: 3.2.0 + formdata-polyfill: 4.0.10 + dev: false + + /web-streams-polyfill@3.3.3: + resolution: {integrity: sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==} + engines: {node: '>= 8'} + dev: false From 35ad692d883b7dadb8e7b9afe0e8f8a04cfc8f64 Mon Sep 17 00:00:00 2001 From: Thijmen Stavenuiter Date: Thu, 13 Jun 2024 13:03:05 +0200 Subject: [PATCH 02/17] Attempt to add tests --- Cargo.lock | 4 + src/main.rs | 6 ++ .../builder/docker/docker_image_builder.rs | 6 ++ src/nixpacks/builder/docker/mod.rs | 1 + test-helper/Cargo.toml | 6 ++ test-helper/src/docker_helper.rs | 34 +++++++++ test-helper/src/lib.rs | 7 +- tests/docker_run_tests.rs | 73 +++++++++++++++++++ tests/generate_plan_tests.rs | 1 + 9 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 test-helper/src/docker_helper.rs diff --git a/Cargo.lock b/Cargo.lock index 50163a262..dcd779fad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1626,6 +1626,10 @@ dependencies = [ "globset", "proc-macro2", "quote", + "serde", + "serde_json", + "serde_with", + "serde_yaml", "walkdir", ] diff --git a/src/main.rs b/src/main.rs index fce4c18c1..061c3904e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -145,6 +145,10 @@ enum Commands { #[arg(long)] docker_host: Option, + /// Adds hosts to the Docker build + #[arg(long, short, global = true)] + add_hosts: Vec, + /// Specify if Docker client should verify the TLS (Transport Layer Security) certificates #[arg(long)] docker_tls_verify: Option, @@ -255,6 +259,7 @@ async fn main() -> Result<()> { cache_from, docker_host, docker_tls_verify, + add_hosts, inline_cache, no_error_without_start, cpu_quota, @@ -288,6 +293,7 @@ async fn main() -> Result<()> { no_error_without_start, incremental_cache_image, cpu_quota, + add_hosts, memory, verbose, }; diff --git a/src/nixpacks/builder/docker/docker_image_builder.rs b/src/nixpacks/builder/docker/docker_image_builder.rs index af15c9529..9d009bb0d 100644 --- a/src/nixpacks/builder/docker/docker_image_builder.rs +++ b/src/nixpacks/builder/docker/docker_image_builder.rs @@ -144,6 +144,12 @@ impl DockerImageBuilder { docker_build_cmd.arg("--progress=plain"); } + if !self.options.add_hosts.is_empty() { + for host in &self.options.add_hosts { + docker_build_cmd.arg("--add-host").arg(host); + } + } + if self.options.quiet { docker_build_cmd.arg("--quiet"); } diff --git a/src/nixpacks/builder/docker/mod.rs b/src/nixpacks/builder/docker/mod.rs index 8267efe39..b6873387c 100644 --- a/src/nixpacks/builder/docker/mod.rs +++ b/src/nixpacks/builder/docker/mod.rs @@ -23,6 +23,7 @@ pub struct DockerBuilderOptions { pub verbose: bool, pub docker_host: Option, pub docker_tls_verify: Option, + pub add_hosts: Vec } mod cache; diff --git a/test-helper/Cargo.toml b/test-helper/Cargo.toml index e2207cb96..b3123c162 100644 --- a/test-helper/Cargo.toml +++ b/test-helper/Cargo.toml @@ -9,7 +9,13 @@ proc-macro = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +serde_json = "1.0.89" +serde_yaml = "0.9.14" +serde_with = { features = [ + "macros", +], default-features = false, version = "2.1.0" } globset = "0.4.9" proc-macro2 = "1.0.43" quote = "1.0.21" walkdir = "2.3.2" +serde = { version = "1.0.151", features = ["derive"] } diff --git a/test-helper/src/docker_helper.rs b/test-helper/src/docker_helper.rs new file mode 100644 index 000000000..12368316a --- /dev/null +++ b/test-helper/src/docker_helper.rs @@ -0,0 +1,34 @@ +use std::collections::HashMap; +use std::process::Command; + +#[derive(Serialize, Deserialize, Debug)] +struct ContainerInfo { + Name: String, + EndpointID: String, + MacAddress: String, + IPv4Address: String, + IPv6Address: String, +} +type Containers = HashMap; + +#[proc_macro] +pub fn fetch_docker_containers_in_network(network: &str) -> Result> { + let output = Command::new("docker") + .arg("network") + .arg("inspect") + .arg(network) + .arg("-f") + .arg("'{{json .Containers}}'") + .output()?; + + if output.status.success() { + let containers = str::from_utf8(&output.stdout)?; + let v: Containers = serde_json::from_str(containers)?; + println!("{}", containers); + } else { + let err = str::from_utf8(&output.stderr)?; + eprintln!("Docker command failed: {}", err); + } + + Ok(output.status.success()) +} \ No newline at end of file diff --git a/test-helper/src/lib.rs b/test-helper/src/lib.rs index 9bda8709c..6f1107b7e 100644 --- a/test-helper/src/lib.rs +++ b/test-helper/src/lib.rs @@ -1,6 +1,12 @@ +mod docker_helper; + use proc_macro::TokenStream; +use std::collections::HashMap; +use std::process::Command; use quote::{format_ident, quote}; use walkdir::{DirEntry, WalkDir}; +use serde::{Deserialize, Serialize}; + const IGNORE: &[&str] = &[ "custom-plan-path", @@ -35,7 +41,6 @@ fn get_examples() -> Vec { .collect() } -#[proc_macro] pub fn generate_plan_tests(_tokens: TokenStream) -> TokenStream { let mut examples = get_examples(); let mut tests = Vec::with_capacity(examples.len()); diff --git a/tests/docker_run_tests.rs b/tests/docker_run_tests.rs index a7f915dd9..1ea27249a 100644 --- a/tests/docker_run_tests.rs +++ b/tests/docker_run_tests.rs @@ -14,6 +14,7 @@ use wait_timeout::ChildExt; use rand::thread_rng; use rand::{distributions::Alphanumeric, Rng}; +use test_helper::{fetch_docker_containers_in_network}; async fn get_container_ids_from_image(image: &str) -> String { let output = Command::new("docker") @@ -145,6 +146,7 @@ async fn build_with_build_time_env_vars(path: &str, env_vars: Vec<&str>) -> Stri const POSTGRES_IMAGE: &str = "postgres"; const MYSQL_IMAGE: &str = "mysql"; +const NGINX_IMAGE: &str = "nginx"; struct Network { name: String, @@ -322,6 +324,41 @@ fn run_mysql() -> Container { } } +fn run_nginx() -> Container { + let mut docker_cmd = Command::new("docker"); + + let hash = Uuid::new_v4().to_string(); + let container_name = format!("nginx-{hash}"); + + // run + docker_cmd.arg("run"); + + // Run detached + docker_cmd.arg("-d"); + + // attach name + docker_cmd.arg("--name").arg(container_name.clone()); + + // Assign image + docker_cmd.arg(NGINX_IMAGE); + + // Run the command + docker_cmd + .spawn() + .unwrap() + .wait() + .context("Building nginx") + .unwrap(); + + Container { + name: container_name.clone(), + config: Some(Config { + environment_variables: EnvironmentVariables::from([]), + network: None, + }), + } +} + #[tokio::test] async fn test_deno() { let name = simple_build("./examples/deno").await; @@ -477,6 +514,42 @@ async fn test_node_moon_custom_start() { .contains("ready - started server on 0.0.0.0:3000")); } + +#[tokio::test] +async fn test_pnpm_network_call() { + // Create the network + let n = create_network(); + let network_name = n.name.clone(); + + // Create the nginx instance + let c = run_nginx(); + let container_name = c.name.clone(); + + // Attach the postgres instance to the network + attach_container_to_network(n.name, container_name.clone()); + + let containers = fetch_docker_containers_in_network!(&network_name).await; + + // Build the basic example, a function that calls the database + let name = simple_build("./examples/node-prisma-postgres").await; + + // Run the example on the attached network + let output = run_image( + &name, + Some(Config { + environment_variables: c.config.unwrap().environment_variables, + network: Some(network_name.clone()), + }), + ) + .await; + + // Cleanup containers and networks + stop_and_remove_container(container_name); + remove_network(network_name); + + assert!(output.contains("My post content")); +} + #[tokio::test] async fn test_prisma_postgres() { // Create the network diff --git a/tests/generate_plan_tests.rs b/tests/generate_plan_tests.rs index a47c6f368..b8bea40b6 100644 --- a/tests/generate_plan_tests.rs +++ b/tests/generate_plan_tests.rs @@ -3,6 +3,7 @@ use std::env::consts::ARCH; test_helper::generate_plan_tests!(); + #[test] fn test_custom_plan_path() { let plan = generate_build_plan( From 52990dda15a1060d9270c3ff6c18fbd9cec685b6 Mon Sep 17 00:00:00 2001 From: Thijmen Stavenuiter Date: Fri, 14 Jun 2024 17:03:06 +0200 Subject: [PATCH 03/17] Add hosts works, validated by tests --- examples/node-fetch-network/package.json | 1 + src/main.rs | 2 +- src/nixpacks/builder/docker/docker_helper.rs | 67 ++++++++++++++++++++ src/nixpacks/builder/docker/mod.rs | 1 + test-helper/src/docker_helper.rs | 34 ---------- test-helper/src/lib.rs | 3 +- tests/docker_run_tests.rs | 65 +++++++++++++++++-- 7 files changed, 131 insertions(+), 42 deletions(-) create mode 100644 src/nixpacks/builder/docker/docker_helper.rs delete mode 100644 test-helper/src/docker_helper.rs diff --git a/examples/node-fetch-network/package.json b/examples/node-fetch-network/package.json index 9d2c8cea6..502f66459 100644 --- a/examples/node-fetch-network/package.json +++ b/examples/node-fetch-network/package.json @@ -5,6 +5,7 @@ "description": "A project to fetch data from a remote URL", "main": "index.js", "scripts": { + "start": "node fetchData.js", "build": "node fetchData.js", "fetch": "node fetchData.js" }, diff --git a/src/main.rs b/src/main.rs index 061c3904e..1aa2ab1d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -146,7 +146,7 @@ enum Commands { docker_host: Option, /// Adds hosts to the Docker build - #[arg(long, short, global = true)] + #[arg(long, global = true)] add_hosts: Vec, /// Specify if Docker client should verify the TLS (Transport Layer Security) certificates diff --git a/src/nixpacks/builder/docker/docker_helper.rs b/src/nixpacks/builder/docker/docker_helper.rs new file mode 100644 index 000000000..c0f7f1842 --- /dev/null +++ b/src/nixpacks/builder/docker/docker_helper.rs @@ -0,0 +1,67 @@ +use std::collections::HashMap; +use std::error::Error; +use std::process::Command; +use std::str; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug)] +struct ContainerInfoFromDocker { + Name: String, + EndpointID: String, + MacAddress: String, + IPv4Address: String, + IPv6Address: String, +} + +pub struct ContainerInfo { + pub Name: String, + pub EndpointID: String, + pub MacAddress: String, + pub IPv4Address: String, + pub IPv6Address: String, + pub IPv4WithoutMask: String, +} + +type Containers = HashMap; + + +pub struct DockerHelper { + +} + +impl DockerHelper { + pub fn containers_in_network(network: &str) -> Result> { + let output = Command::new("docker") + .arg("network") + .arg("inspect") + .arg(network) + .arg("-f") + .arg("{{json .Containers}}") + .output()?; + + if output.status.success() { + let containers_string = str::from_utf8(&output.stdout)?; + let containers: HashMap = serde_json::from_str(containers_string)?; + + let mut vec = Vec::new(); + for (name, info) in containers.iter() { + let ipv4 = info.IPv4Address.split('/').next().unwrap(); + let container_info = ContainerInfo { + Name: info.Name.clone(), + EndpointID: info.EndpointID.clone(), + MacAddress: info.MacAddress.clone(), + IPv4Address: info.IPv4Address.clone(), + IPv6Address: info.IPv6Address.clone(), + IPv4WithoutMask: ipv4.to_string(), + }; + vec.push(container_info); + } + + return Ok(vec.into_iter().map(|info| (info.Name.clone(), info)).collect()); + } + let err = str::from_utf8(&output.stderr)?; + eprintln!("Docker command failed: {}", err); + + return Err("Docker command failed".into()); + } +} \ No newline at end of file diff --git a/src/nixpacks/builder/docker/mod.rs b/src/nixpacks/builder/docker/mod.rs index b6873387c..285f432d7 100644 --- a/src/nixpacks/builder/docker/mod.rs +++ b/src/nixpacks/builder/docker/mod.rs @@ -32,3 +32,4 @@ mod dockerfile_generation; pub mod file_server; pub mod incremental_cache; pub mod utils; +pub mod docker_helper; diff --git a/test-helper/src/docker_helper.rs b/test-helper/src/docker_helper.rs deleted file mode 100644 index 12368316a..000000000 --- a/test-helper/src/docker_helper.rs +++ /dev/null @@ -1,34 +0,0 @@ -use std::collections::HashMap; -use std::process::Command; - -#[derive(Serialize, Deserialize, Debug)] -struct ContainerInfo { - Name: String, - EndpointID: String, - MacAddress: String, - IPv4Address: String, - IPv6Address: String, -} -type Containers = HashMap; - -#[proc_macro] -pub fn fetch_docker_containers_in_network(network: &str) -> Result> { - let output = Command::new("docker") - .arg("network") - .arg("inspect") - .arg(network) - .arg("-f") - .arg("'{{json .Containers}}'") - .output()?; - - if output.status.success() { - let containers = str::from_utf8(&output.stdout)?; - let v: Containers = serde_json::from_str(containers)?; - println!("{}", containers); - } else { - let err = str::from_utf8(&output.stderr)?; - eprintln!("Docker command failed: {}", err); - } - - Ok(output.status.success()) -} \ No newline at end of file diff --git a/test-helper/src/lib.rs b/test-helper/src/lib.rs index 6f1107b7e..b8aaa2a6c 100644 --- a/test-helper/src/lib.rs +++ b/test-helper/src/lib.rs @@ -1,5 +1,3 @@ -mod docker_helper; - use proc_macro::TokenStream; use std::collections::HashMap; use std::process::Command; @@ -41,6 +39,7 @@ fn get_examples() -> Vec { .collect() } +#[proc_macro] pub fn generate_plan_tests(_tokens: TokenStream) -> TokenStream { let mut examples = get_examples(); let mut tests = Vec::with_capacity(examples.len()); diff --git a/tests/docker_run_tests.rs b/tests/docker_run_tests.rs index 1ea27249a..2e3227263 100644 --- a/tests/docker_run_tests.rs +++ b/tests/docker_run_tests.rs @@ -1,8 +1,10 @@ +use std::collections::HashMap; use anyhow::Context; use nixpacks::{ create_docker_image, nixpacks::{ builder::docker::DockerBuilderOptions, environment::EnvironmentVariables, + builder::docker::docker_helper::DockerHelper, plan::generator::GeneratePlanOptions, }, }; @@ -10,11 +12,25 @@ use std::io::{BufRead, BufReader}; use std::process::{Command, Stdio}; use std::time::Duration; use uuid::Uuid; +use std::str; use wait_timeout::ChildExt; use rand::thread_rng; +use serde::{Deserialize, Serialize}; + use rand::{distributions::Alphanumeric, Rng}; -use test_helper::{fetch_docker_containers_in_network}; + + + +#[derive(Deserialize, Debug)] +struct NetworkInfo { + Name: String, + EndpointID: String, + MacAddress: String, + IPv4Address: String, + IPv6Address: String, +} + async fn get_container_ids_from_image(image: &str) -> String { let output = Command::new("docker") @@ -106,6 +122,30 @@ async fn run_image(name: &str, cfg: Option) -> String { .join("\n") } +async fn build_with_hosts(path: &str, add_hosts: &Vec, nginx_host: String) -> String { + let name = Uuid::new_v4().to_string(); + let mut env: Vec<&str> = Vec::new(); + let env_var = format!("REMOTE_URL=http://{}", nginx_host); + env.push(&*env_var); + + create_docker_image( + path, + env, + &GeneratePlanOptions::default(), + &DockerBuilderOptions { + name: Some(name.clone()), + quiet: true, + add_hosts: add_hosts.clone(), + + ..Default::default() + }, + ) + .await + .unwrap(); + + name +} + /// Builds a directory with default options /// Returns the randomly generated image name async fn simple_build(path: &str) -> String { @@ -516,7 +556,7 @@ async fn test_node_moon_custom_start() { #[tokio::test] -async fn test_pnpm_network_call() { +async fn test_pnpm_network_call_working_with_add_hosts() { // Create the network let n = create_network(); let network_name = n.name.clone(); @@ -528,10 +568,23 @@ async fn test_pnpm_network_call() { // Attach the postgres instance to the network attach_container_to_network(n.name, container_name.clone()); - let containers = fetch_docker_containers_in_network!(&network_name).await; + let containers = DockerHelper::containers_in_network(&network_name); + + if containers.is_err() { + panic!("Failed to fetch containers in network"); + } + + + let mut vec_hosts = Vec::new(); + + for (container,containerinfo) in containers.unwrap() { + let add_host = format!("{}:{}", containerinfo.Name, containerinfo.IPv4WithoutMask); + vec_hosts.push(add_host); + } + // Build the basic example, a function that calls the database - let name = simple_build("./examples/node-prisma-postgres").await; + let name = build_with_hosts("./examples/node-fetch-network", &vec_hosts, container_name.clone()).await; // Run the example on the attached network let output = run_image( @@ -547,9 +600,11 @@ async fn test_pnpm_network_call() { stop_and_remove_container(container_name); remove_network(network_name); - assert!(output.contains("My post content")); + assert!(output.contains("Fetched data: OK")); } + + #[tokio::test] async fn test_prisma_postgres() { // Create the network From 5aacb4f774d32669e3129923fe7e600e13ecb868 Mon Sep 17 00:00:00 2001 From: Thijmen Stavenuiter Date: Fri, 14 Jun 2024 17:22:09 +0200 Subject: [PATCH 04/17] Add test to validate that the build does not work when there are no hosts attached --- tests/docker_run_tests.rs | 54 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/docker_run_tests.rs b/tests/docker_run_tests.rs index 2e3227263..46ac3a7a8 100644 --- a/tests/docker_run_tests.rs +++ b/tests/docker_run_tests.rs @@ -15,6 +15,8 @@ use uuid::Uuid; use std::str; use wait_timeout::ChildExt; + + use rand::thread_rng; use serde::{Deserialize, Serialize}; @@ -146,6 +148,24 @@ async fn build_with_hosts(path: &str, add_hosts: &Vec, nginx_host: Strin name } +async fn build_with_env(path: &str, env: Vec<&str>) -> anyhow::Result<()> { + let name = Uuid::new_v4().to_string(); + + let result = create_docker_image( + path, + env, + &GeneratePlanOptions::default(), + &DockerBuilderOptions { + name: Some(name.clone()), + quiet: true, + ..Default::default() + }, + ) + .await; + + return result; +} + /// Builds a directory with default options /// Returns the randomly generated image name async fn simple_build(path: &str) -> String { @@ -603,7 +623,41 @@ async fn test_pnpm_network_call_working_with_add_hosts() { assert!(output.contains("Fetched data: OK")); } +#[tokio::test] +async fn test_pnpm_network_call_should_not_work_without_hosts() { + // Create the network + let n = create_network(); + let network_name = n.name.clone(); + + // Create the nginx instance + let c = run_nginx(); + let container_name = c.name.clone(); + + // Attach the postgres instance to the network + attach_container_to_network(n.name, container_name.clone()); + + let containers = DockerHelper::containers_in_network(&network_name); + + if containers.is_err() { + panic!("Failed to fetch containers in network"); + } + + let mut vec_hosts = Vec::new(); + + for (container,containerinfo) in containers.unwrap() { + let add_host = format!("{}:{}", containerinfo.Name, containerinfo.IPv4WithoutMask); + vec_hosts.push(add_host); + } + let mut env: Vec<&str> = Vec::new(); + let env_var = format!("REMOTE_URL=http://{}", container_name); + env.push(&*env_var); + + // Build the basic example, a function that calls the database + let build_result = build_with_env("./examples/node-fetch-network", env).await; + + assert!(build_result.is_err()); +} #[tokio::test] async fn test_prisma_postgres() { From ea7f32fd323a41206b87e72e730030244291e38f Mon Sep 17 00:00:00 2001 From: Thijmen Stavenuiter Date: Fri, 14 Jun 2024 17:31:07 +0200 Subject: [PATCH 05/17] Fix naming conventions --- src/nixpacks/builder/docker/docker_helper.rs | 33 ++++++++------------ test-helper/src/lib.rs | 3 -- tests/docker_run_tests.rs | 24 +++----------- 3 files changed, 17 insertions(+), 43 deletions(-) diff --git a/src/nixpacks/builder/docker/docker_helper.rs b/src/nixpacks/builder/docker/docker_helper.rs index c0f7f1842..09f253a0d 100644 --- a/src/nixpacks/builder/docker/docker_helper.rs +++ b/src/nixpacks/builder/docker/docker_helper.rs @@ -6,20 +6,16 @@ use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug)] struct ContainerInfoFromDocker { - Name: String, - EndpointID: String, - MacAddress: String, - IPv4Address: String, - IPv6Address: String, + #[serde(rename = "Name")] + name: String, + #[serde(rename = "IPv4Address")] + ipv4_address: String, } pub struct ContainerInfo { - pub Name: String, - pub EndpointID: String, - pub MacAddress: String, - pub IPv4Address: String, - pub IPv6Address: String, - pub IPv4WithoutMask: String, + pub name: String, + pub ipv4_address: String, + pub ipv4_address_without_mask: String, } type Containers = HashMap; @@ -44,20 +40,17 @@ impl DockerHelper { let containers: HashMap = serde_json::from_str(containers_string)?; let mut vec = Vec::new(); - for (name, info) in containers.iter() { - let ipv4 = info.IPv4Address.split('/').next().unwrap(); + for (_, info) in containers.iter() { + let ipv4 = info.ipv4_address.split('/').next().unwrap(); let container_info = ContainerInfo { - Name: info.Name.clone(), - EndpointID: info.EndpointID.clone(), - MacAddress: info.MacAddress.clone(), - IPv4Address: info.IPv4Address.clone(), - IPv6Address: info.IPv6Address.clone(), - IPv4WithoutMask: ipv4.to_string(), + name: info.name.clone(), + ipv4_address: info.ipv4_address.clone(), + ipv4_address_without_mask: ipv4.to_string(), }; vec.push(container_info); } - return Ok(vec.into_iter().map(|info| (info.Name.clone(), info)).collect()); + return Ok(vec.into_iter().map(|info| (info.name.clone(), info)).collect()); } let err = str::from_utf8(&output.stderr)?; eprintln!("Docker command failed: {}", err); diff --git a/test-helper/src/lib.rs b/test-helper/src/lib.rs index b8aaa2a6c..582a88d2a 100644 --- a/test-helper/src/lib.rs +++ b/test-helper/src/lib.rs @@ -1,9 +1,6 @@ use proc_macro::TokenStream; -use std::collections::HashMap; -use std::process::Command; use quote::{format_ident, quote}; use walkdir::{DirEntry, WalkDir}; -use serde::{Deserialize, Serialize}; const IGNORE: &[&str] = &[ diff --git a/tests/docker_run_tests.rs b/tests/docker_run_tests.rs index 46ac3a7a8..9295afba2 100644 --- a/tests/docker_run_tests.rs +++ b/tests/docker_run_tests.rs @@ -1,4 +1,3 @@ -use std::collections::HashMap; use anyhow::Context; use nixpacks::{ create_docker_image, @@ -15,25 +14,10 @@ use uuid::Uuid; use std::str; use wait_timeout::ChildExt; - - use rand::thread_rng; -use serde::{Deserialize, Serialize}; - use rand::{distributions::Alphanumeric, Rng}; - -#[derive(Deserialize, Debug)] -struct NetworkInfo { - Name: String, - EndpointID: String, - MacAddress: String, - IPv4Address: String, - IPv6Address: String, -} - - async fn get_container_ids_from_image(image: &str) -> String { let output = Command::new("docker") .arg("ps") @@ -597,8 +581,8 @@ async fn test_pnpm_network_call_working_with_add_hosts() { let mut vec_hosts = Vec::new(); - for (container,containerinfo) in containers.unwrap() { - let add_host = format!("{}:{}", containerinfo.Name, containerinfo.IPv4WithoutMask); + for (_,containerinfo) in containers.unwrap() { + let add_host = format!("{}:{}", containerinfo.name, containerinfo.ipv4_address_without_mask); vec_hosts.push(add_host); } @@ -644,8 +628,8 @@ async fn test_pnpm_network_call_should_not_work_without_hosts() { let mut vec_hosts = Vec::new(); - for (container,containerinfo) in containers.unwrap() { - let add_host = format!("{}:{}", containerinfo.Name, containerinfo.IPv4WithoutMask); + for (_, container_info) in containers.unwrap() { + let add_host = format!("{}:{}", container_info.name, container_info.ipv4_address_without_mask); vec_hosts.push(add_host); } From 09cfa6d149fa4816b4c9f8d4c9c79fb7520691d7 Mon Sep 17 00:00:00 2001 From: Thijmen Stavenuiter Date: Fri, 14 Jun 2024 17:36:52 +0200 Subject: [PATCH 06/17] Use the same argument as Docker --- src/main.rs | 6 +++--- src/nixpacks/builder/docker/docker_image_builder.rs | 4 ++-- src/nixpacks/builder/docker/mod.rs | 2 +- tests/docker_run_tests.rs | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1aa2ab1d5..843dc667b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -147,7 +147,7 @@ enum Commands { /// Adds hosts to the Docker build #[arg(long, global = true)] - add_hosts: Vec, + add_host: Vec, /// Specify if Docker client should verify the TLS (Transport Layer Security) certificates #[arg(long)] @@ -259,7 +259,7 @@ async fn main() -> Result<()> { cache_from, docker_host, docker_tls_verify, - add_hosts, + add_host: add_hosts, inline_cache, no_error_without_start, cpu_quota, @@ -293,7 +293,7 @@ async fn main() -> Result<()> { no_error_without_start, incremental_cache_image, cpu_quota, - add_hosts, + add_host: add_hosts, memory, verbose, }; diff --git a/src/nixpacks/builder/docker/docker_image_builder.rs b/src/nixpacks/builder/docker/docker_image_builder.rs index 9d009bb0d..bd24bad37 100644 --- a/src/nixpacks/builder/docker/docker_image_builder.rs +++ b/src/nixpacks/builder/docker/docker_image_builder.rs @@ -144,8 +144,8 @@ impl DockerImageBuilder { docker_build_cmd.arg("--progress=plain"); } - if !self.options.add_hosts.is_empty() { - for host in &self.options.add_hosts { + if !self.options.add_host.is_empty() { + for host in &self.options.add_host { docker_build_cmd.arg("--add-host").arg(host); } } diff --git a/src/nixpacks/builder/docker/mod.rs b/src/nixpacks/builder/docker/mod.rs index 285f432d7..b5f60acc3 100644 --- a/src/nixpacks/builder/docker/mod.rs +++ b/src/nixpacks/builder/docker/mod.rs @@ -23,7 +23,7 @@ pub struct DockerBuilderOptions { pub verbose: bool, pub docker_host: Option, pub docker_tls_verify: Option, - pub add_hosts: Vec + pub add_host: Vec } mod cache; diff --git a/tests/docker_run_tests.rs b/tests/docker_run_tests.rs index 9295afba2..81363d2be 100644 --- a/tests/docker_run_tests.rs +++ b/tests/docker_run_tests.rs @@ -121,7 +121,7 @@ async fn build_with_hosts(path: &str, add_hosts: &Vec, nginx_host: Strin &DockerBuilderOptions { name: Some(name.clone()), quiet: true, - add_hosts: add_hosts.clone(), + add_host: add_hosts.clone(), ..Default::default() }, From 1a6ba324e0f97771a869ddb10ee8227b1b93f067 Mon Sep 17 00:00:00 2001 From: Thijmen Stavenuiter Date: Fri, 14 Jun 2024 17:38:04 +0200 Subject: [PATCH 07/17] Use the same argument as Docker --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 843dc667b..677802697 100644 --- a/src/main.rs +++ b/src/main.rs @@ -259,7 +259,7 @@ async fn main() -> Result<()> { cache_from, docker_host, docker_tls_verify, - add_host: add_hosts, + add_host, inline_cache, no_error_without_start, cpu_quota, @@ -293,7 +293,7 @@ async fn main() -> Result<()> { no_error_without_start, incremental_cache_image, cpu_quota, - add_host: add_hosts, + add_host, memory, verbose, }; From fbf8bc11764bac0da21b2d9688b790af16ca6406 Mon Sep 17 00:00:00 2001 From: Thijmen Stavenuiter Date: Fri, 14 Jun 2024 17:55:54 +0200 Subject: [PATCH 08/17] Clean up after tests --- tests/docker_run_tests.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/docker_run_tests.rs b/tests/docker_run_tests.rs index 81363d2be..cd6ab739f 100644 --- a/tests/docker_run_tests.rs +++ b/tests/docker_run_tests.rs @@ -641,6 +641,9 @@ async fn test_pnpm_network_call_should_not_work_without_hosts() { let build_result = build_with_env("./examples/node-fetch-network", env).await; assert!(build_result.is_err()); + + stop_and_remove_container(container_name); + remove_network(network_name); } #[tokio::test] From 046a14338bc84b1266401e27b326991e8c959ea3 Mon Sep 17 00:00:00 2001 From: Thijmen Stavenuiter Date: Sat, 6 Jul 2024 09:14:05 +0200 Subject: [PATCH 09/17] Clean linting --- src/nixpacks/builder/docker/docker_helper.rs | 17 ++++----- src/nixpacks/builder/docker/mod.rs | 4 +-- test-helper/src/lib.rs | 1 - tests/docker_run_tests.rs | 38 +++++++++++--------- tests/generate_plan_tests.rs | 1 - 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/nixpacks/builder/docker/docker_helper.rs b/src/nixpacks/builder/docker/docker_helper.rs index 09f253a0d..59499c56a 100644 --- a/src/nixpacks/builder/docker/docker_helper.rs +++ b/src/nixpacks/builder/docker/docker_helper.rs @@ -1,8 +1,8 @@ +use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::error::Error; use std::process::Command; use std::str; -use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug)] struct ContainerInfoFromDocker { @@ -20,10 +20,7 @@ pub struct ContainerInfo { type Containers = HashMap; - -pub struct DockerHelper { - -} +pub struct DockerHelper {} impl DockerHelper { pub fn containers_in_network(network: &str) -> Result> { @@ -37,7 +34,8 @@ impl DockerHelper { if output.status.success() { let containers_string = str::from_utf8(&output.stdout)?; - let containers: HashMap = serde_json::from_str(containers_string)?; + let containers: HashMap = + serde_json::from_str(containers_string)?; let mut vec = Vec::new(); for (_, info) in containers.iter() { @@ -50,11 +48,14 @@ impl DockerHelper { vec.push(container_info); } - return Ok(vec.into_iter().map(|info| (info.name.clone(), info)).collect()); + return Ok(vec + .into_iter() + .map(|info| (info.name.clone(), info)) + .collect()); } let err = str::from_utf8(&output.stderr)?; eprintln!("Docker command failed: {}", err); return Err("Docker command failed".into()); } -} \ No newline at end of file +} diff --git a/src/nixpacks/builder/docker/mod.rs b/src/nixpacks/builder/docker/mod.rs index b5f60acc3..891c80c46 100644 --- a/src/nixpacks/builder/docker/mod.rs +++ b/src/nixpacks/builder/docker/mod.rs @@ -23,13 +23,13 @@ pub struct DockerBuilderOptions { pub verbose: bool, pub docker_host: Option, pub docker_tls_verify: Option, - pub add_host: Vec + pub add_host: Vec, } mod cache; +pub mod docker_helper; pub mod docker_image_builder; mod dockerfile_generation; pub mod file_server; pub mod incremental_cache; pub mod utils; -pub mod docker_helper; diff --git a/test-helper/src/lib.rs b/test-helper/src/lib.rs index 582a88d2a..9bda8709c 100644 --- a/test-helper/src/lib.rs +++ b/test-helper/src/lib.rs @@ -2,7 +2,6 @@ use proc_macro::TokenStream; use quote::{format_ident, quote}; use walkdir::{DirEntry, WalkDir}; - const IGNORE: &[&str] = &[ "custom-plan-path", "rust-custom-version", diff --git a/tests/docker_run_tests.rs b/tests/docker_run_tests.rs index cd6ab739f..8000c9ddf 100644 --- a/tests/docker_run_tests.rs +++ b/tests/docker_run_tests.rs @@ -2,22 +2,20 @@ use anyhow::Context; use nixpacks::{ create_docker_image, nixpacks::{ - builder::docker::DockerBuilderOptions, environment::EnvironmentVariables, - builder::docker::docker_helper::DockerHelper, - plan::generator::GeneratePlanOptions, + builder::docker::docker_helper::DockerHelper, builder::docker::DockerBuilderOptions, + environment::EnvironmentVariables, plan::generator::GeneratePlanOptions, }, }; use std::io::{BufRead, BufReader}; use std::process::{Command, Stdio}; +use std::str; use std::time::Duration; use uuid::Uuid; -use std::str; use wait_timeout::ChildExt; use rand::thread_rng; use rand::{distributions::Alphanumeric, Rng}; - async fn get_container_ids_from_image(image: &str) -> String { let output = Command::new("docker") .arg("ps") @@ -126,8 +124,8 @@ async fn build_with_hosts(path: &str, add_hosts: &Vec, nginx_host: Strin ..Default::default() }, ) - .await - .unwrap(); + .await + .unwrap(); name } @@ -145,7 +143,7 @@ async fn build_with_env(path: &str, env: Vec<&str>) -> anyhow::Result<()> { ..Default::default() }, ) - .await; + .await; return result; } @@ -558,7 +556,6 @@ async fn test_node_moon_custom_start() { .contains("ready - started server on 0.0.0.0:3000")); } - #[tokio::test] async fn test_pnpm_network_call_working_with_add_hosts() { // Create the network @@ -578,17 +575,23 @@ async fn test_pnpm_network_call_working_with_add_hosts() { panic!("Failed to fetch containers in network"); } - let mut vec_hosts = Vec::new(); - for (_,containerinfo) in containers.unwrap() { - let add_host = format!("{}:{}", containerinfo.name, containerinfo.ipv4_address_without_mask); + for (_, containerinfo) in containers.unwrap() { + let add_host = format!( + "{}:{}", + containerinfo.name, containerinfo.ipv4_address_without_mask + ); vec_hosts.push(add_host); } - // Build the basic example, a function that calls the database - let name = build_with_hosts("./examples/node-fetch-network", &vec_hosts, container_name.clone()).await; + let name = build_with_hosts( + "./examples/node-fetch-network", + &vec_hosts, + container_name.clone(), + ) + .await; // Run the example on the attached network let output = run_image( @@ -598,7 +601,7 @@ async fn test_pnpm_network_call_working_with_add_hosts() { network: Some(network_name.clone()), }), ) - .await; + .await; // Cleanup containers and networks stop_and_remove_container(container_name); @@ -629,7 +632,10 @@ async fn test_pnpm_network_call_should_not_work_without_hosts() { let mut vec_hosts = Vec::new(); for (_, container_info) in containers.unwrap() { - let add_host = format!("{}:{}", container_info.name, container_info.ipv4_address_without_mask); + let add_host = format!( + "{}:{}", + container_info.name, container_info.ipv4_address_without_mask + ); vec_hosts.push(add_host); } diff --git a/tests/generate_plan_tests.rs b/tests/generate_plan_tests.rs index b8bea40b6..a47c6f368 100644 --- a/tests/generate_plan_tests.rs +++ b/tests/generate_plan_tests.rs @@ -3,7 +3,6 @@ use std::env::consts::ARCH; test_helper::generate_plan_tests!(); - #[test] fn test_custom_plan_path() { let plan = generate_build_plan( From 192c9f022e7a450b20f35d590decb0968687574d Mon Sep 17 00:00:00 2001 From: Thijmen Stavenuiter Date: Tue, 16 Jul 2024 09:34:35 +0200 Subject: [PATCH 10/17] Fix clippy linting --- src/nixpacks/builder/docker/docker_helper.rs | 6 ++---- tests/docker_run_tests.rs | 10 ++++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/nixpacks/builder/docker/docker_helper.rs b/src/nixpacks/builder/docker/docker_helper.rs index 59499c56a..938da47a2 100644 --- a/src/nixpacks/builder/docker/docker_helper.rs +++ b/src/nixpacks/builder/docker/docker_helper.rs @@ -38,7 +38,7 @@ impl DockerHelper { serde_json::from_str(containers_string)?; let mut vec = Vec::new(); - for (_, info) in containers.iter() { + for info in containers.values() { let ipv4 = info.ipv4_address.split('/').next().unwrap(); let container_info = ContainerInfo { name: info.name.clone(), @@ -53,9 +53,7 @@ impl DockerHelper { .map(|info| (info.name.clone(), info)) .collect()); } - let err = str::from_utf8(&output.stderr)?; - eprintln!("Docker command failed: {}", err); - return Err("Docker command failed".into()); + Err("Docker command failed".into()) } } diff --git a/tests/docker_run_tests.rs b/tests/docker_run_tests.rs index a7957f0a6..2dda44603 100644 --- a/tests/docker_run_tests.rs +++ b/tests/docker_run_tests.rs @@ -124,7 +124,7 @@ async fn run_image(name: &str, cfg: Option) -> String { .join("\n") } -async fn build_with_hosts(path: &str, add_hosts: &Vec, nginx_host: String) -> String { +async fn build_with_hosts(path: &str, add_hosts: &[String], nginx_host: String) -> String { let name = Uuid::new_v4().to_string(); let mut env: Vec<&str> = Vec::new(); let env_var = format!("REMOTE_URL=http://{}", nginx_host); @@ -137,7 +137,7 @@ async fn build_with_hosts(path: &str, add_hosts: &Vec, nginx_host: Strin &DockerBuilderOptions { name: Some(name.clone()), quiet: true, - add_host: add_hosts.clone(), + add_host: add_hosts.to_owned(), ..Default::default() }, @@ -151,7 +151,7 @@ async fn build_with_hosts(path: &str, add_hosts: &Vec, nginx_host: Strin async fn build_with_env(path: &str, env: Vec<&str>) -> anyhow::Result<()> { let name = Uuid::new_v4().to_string(); - let result = create_docker_image( + create_docker_image( path, env, &GeneratePlanOptions::default(), @@ -161,9 +161,7 @@ async fn build_with_env(path: &str, env: Vec<&str>) -> anyhow::Result<()> { ..Default::default() }, ) - .await; - - return result; + .await } /// Builds a directory with default options From 9721748041f12e7ba911f4ce6553bfa2d491e023 Mon Sep 17 00:00:00 2001 From: Jake Runzer Date: Wed, 24 Jul 2024 11:26:33 -0400 Subject: [PATCH 11/17] add snapshot test for node fetch network --- ...nerate_plan_tests__node_fetch_network.snap | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/snapshots/generate_plan_tests__node_fetch_network.snap diff --git a/tests/snapshots/generate_plan_tests__node_fetch_network.snap b/tests/snapshots/generate_plan_tests__node_fetch_network.snap new file mode 100644 index 000000000..c2517d29b --- /dev/null +++ b/tests/snapshots/generate_plan_tests__node_fetch_network.snap @@ -0,0 +1,57 @@ +--- +source: tests/generate_plan_tests.rs +expression: plan +--- +{ + "providers": [], + "buildImage": "[build_image]", + "variables": { + "CI": "true", + "NIXPACKS_METADATA": "node", + "NODE_ENV": "production", + "NPM_CONFIG_PRODUCTION": "false" + }, + "phases": { + "build": { + "name": "build", + "dependsOn": [ + "install" + ], + "cmds": [ + "pnpm run build" + ], + "cacheDirectories": [ + "node_modules/.cache" + ] + }, + "install": { + "name": "install", + "dependsOn": [ + "setup" + ], + "cmds": [ + "pnpm i --frozen-lockfile" + ], + "cacheDirectories": [ + "/root/.local/share/pnpm/store/v3" + ], + "paths": [ + "/app/node_modules/.bin" + ] + }, + "setup": { + "name": "setup", + "nixPkgs": [ + "nodejs_18", + "pnpm-8_x" + ], + "nixOverlays": [ + "https://github.com/railwayapp/nix-npm-overlay/archive/main.tar.gz" + ], + "nixpkgsArchive": "[archive]" + } + }, + "start": { + "cmd": "pnpm run start" + } +} From 031c5b7d558a332a0593c11d841b390108b2c95d Mon Sep 17 00:00:00 2001 From: Jake Runzer Date: Wed, 24 Jul 2024 11:52:37 -0400 Subject: [PATCH 12/17] remove build command from node-fetch-network --- examples/node-fetch-network/package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/node-fetch-network/package.json b/examples/node-fetch-network/package.json index 502f66459..ff2d6cd0f 100644 --- a/examples/node-fetch-network/package.json +++ b/examples/node-fetch-network/package.json @@ -6,7 +6,6 @@ "main": "index.js", "scripts": { "start": "node fetchData.js", - "build": "node fetchData.js", "fetch": "node fetchData.js" }, "dependencies": { @@ -15,4 +14,4 @@ }, "author": "Your Name", "license": "MIT" -} \ No newline at end of file +} From a93f0e03d47611fe187e233bb9cd29143234f3d4 Mon Sep 17 00:00:00 2001 From: Jake Runzer Date: Wed, 24 Jul 2024 11:56:35 -0400 Subject: [PATCH 13/17] update snapshot --- tests/snapshots/generate_plan_tests__node_fetch_network.snap | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/snapshots/generate_plan_tests__node_fetch_network.snap b/tests/snapshots/generate_plan_tests__node_fetch_network.snap index c2517d29b..658c07258 100644 --- a/tests/snapshots/generate_plan_tests__node_fetch_network.snap +++ b/tests/snapshots/generate_plan_tests__node_fetch_network.snap @@ -17,9 +17,6 @@ expression: plan "dependsOn": [ "install" ], - "cmds": [ - "pnpm run build" - ], "cacheDirectories": [ "node_modules/.cache" ] From 966d5b932ee048b2ecdf2713db22b8ecbbae9f82 Mon Sep 17 00:00:00 2001 From: Jake Runzer Date: Wed, 24 Jul 2024 12:14:01 -0400 Subject: [PATCH 14/17] add back build command --- examples/node-fetch-network/package.json | 1 + tests/snapshots/generate_plan_tests__node_fetch_network.snap | 3 +++ 2 files changed, 4 insertions(+) diff --git a/examples/node-fetch-network/package.json b/examples/node-fetch-network/package.json index ff2d6cd0f..fcb47721d 100644 --- a/examples/node-fetch-network/package.json +++ b/examples/node-fetch-network/package.json @@ -6,6 +6,7 @@ "main": "index.js", "scripts": { "start": "node fetchData.js", + "build": "node fetchData.js", "fetch": "node fetchData.js" }, "dependencies": { diff --git a/tests/snapshots/generate_plan_tests__node_fetch_network.snap b/tests/snapshots/generate_plan_tests__node_fetch_network.snap index 658c07258..c2517d29b 100644 --- a/tests/snapshots/generate_plan_tests__node_fetch_network.snap +++ b/tests/snapshots/generate_plan_tests__node_fetch_network.snap @@ -17,6 +17,9 @@ expression: plan "dependsOn": [ "install" ], + "cmds": [ + "pnpm run build" + ], "cacheDirectories": [ "node_modules/.cache" ] From 531b469d49b274301f5db5c03c34c0bf7e11aa34 Mon Sep 17 00:00:00 2001 From: Thijmen Stavenuiter Date: Thu, 25 Jul 2024 15:56:20 +0200 Subject: [PATCH 15/17] Fix package.json --- examples/node-fetch-network/package.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/node-fetch-network/package.json b/examples/node-fetch-network/package.json index fcb47721d..c9be713d1 100644 --- a/examples/node-fetch-network/package.json +++ b/examples/node-fetch-network/package.json @@ -5,9 +5,7 @@ "description": "A project to fetch data from a remote URL", "main": "index.js", "scripts": { - "start": "node fetchData.js", - "build": "node fetchData.js", - "fetch": "node fetchData.js" + "start": "node fetchData.js" }, "dependencies": { "dotenv": "^10.0.0", From 0731f1bd5d1369b8763b17b88a46518a39bc271c Mon Sep 17 00:00:00 2001 From: Thijmen Stavenuiter Date: Thu, 25 Jul 2024 22:48:36 +0200 Subject: [PATCH 16/17] Fix build, make sure to use --network=host when providing add hosts --- examples/node-fetch-network/package.json | 1 + src/nixpacks/builder/docker/docker_image_builder.rs | 2 ++ 2 files changed, 3 insertions(+) diff --git a/examples/node-fetch-network/package.json b/examples/node-fetch-network/package.json index c9be713d1..534a314d7 100644 --- a/examples/node-fetch-network/package.json +++ b/examples/node-fetch-network/package.json @@ -5,6 +5,7 @@ "description": "A project to fetch data from a remote URL", "main": "index.js", "scripts": { + "build": "echo $REMOTE_URL && node fetchData.js", "start": "node fetchData.js" }, "dependencies": { diff --git a/src/nixpacks/builder/docker/docker_image_builder.rs b/src/nixpacks/builder/docker/docker_image_builder.rs index 6baf3752c..3b3d3fbc7 100644 --- a/src/nixpacks/builder/docker/docker_image_builder.rs +++ b/src/nixpacks/builder/docker/docker_image_builder.rs @@ -167,6 +167,8 @@ impl DockerImageBuilder { for host in &self.options.add_host { docker_build_cmd.arg("--add-host").arg(host); } + + docker_build_cmd.arg("--network").arg("host"); } if self.options.quiet { From afde70d8957f7940b9016a9a0e6a069e16444b39 Mon Sep 17 00:00:00 2001 From: Thijmen Stavenuiter Date: Thu, 25 Jul 2024 22:59:25 +0200 Subject: [PATCH 17/17] Fix clippy --- src/nixpacks/builder/docker/docker_image_builder.rs | 2 +- src/nixpacks/builder/docker/incremental_cache.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nixpacks/builder/docker/docker_image_builder.rs b/src/nixpacks/builder/docker/docker_image_builder.rs index 3b3d3fbc7..250656206 100644 --- a/src/nixpacks/builder/docker/docker_image_builder.rs +++ b/src/nixpacks/builder/docker/docker_image_builder.rs @@ -155,7 +155,7 @@ impl DockerImageBuilder { .arg("build") .arg(&output.root) .arg("-f") - .arg(&output.get_absolute_path("Dockerfile")) + .arg(output.get_absolute_path("Dockerfile")) .arg("-t") .arg(name); diff --git a/src/nixpacks/builder/docker/incremental_cache.rs b/src/nixpacks/builder/docker/incremental_cache.rs index b4edd6930..ddfe2f697 100644 --- a/src/nixpacks/builder/docker/incremental_cache.rs +++ b/src/nixpacks/builder/docker/incremental_cache.rs @@ -84,7 +84,7 @@ impl IncrementalCache { // #3 Use Docker import: Provide 3 seconds in a sample test for f in files { let mut docker_import_cmd = Command::new("docker"); - docker_import_cmd.arg("import").arg(&f?.path()).arg(tag); + docker_import_cmd.arg("import").arg(f?.path()).arg(tag); let result = docker_import_cmd .spawn()?