Skip to content

Conversation

tolbrino
Copy link
Contributor

@tolbrino tolbrino commented Nov 28, 2024

This pull request introduces a new Docker entrypoint script which sets the environment variable HOPRD_DEFAULT_SESSION_LISTEN_HOST to the docker container's IP, unless it was configured by the user beforehand.

This allows session users to only specify the listen port when opening a session, e.g. :50001, and the hoprd node will use the default internal docker ip automatically.

Moreover, other hopr binaries can now be run using the docker image by specifying them as commands. E.g.

docker run -ti --rm hoprd:latest hoprd-api-schema

@tolbrino tolbrino self-assigned this Nov 28, 2024
Copy link
Contributor

coderabbitai bot commented Nov 28, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes involve modifications to the flake.nix and nix/docker-builder.nix files, primarily enhancing Docker integration for the hoprd and hopli applications. A new Docker entry point script is introduced, allowing for dynamic configuration of the entry point and command execution. Additionally, a script for uploading Docker images is added. The nix/docker-builder.nix file is updated to include new parameters and reorder existing ones, improving the structure of the Docker image build process.

Changes

File Change Summary
flake.nix - Added variable: dockerHoprdEntrypoint for a new Docker entry point script.
- Added variable: dockerImageUploadScript for uploading Docker images.
nix/docker-builder.nix - Added parameter: Cmd ? [ ] in the function signature.
- Reordered parameters: Entrypoint, env, extraContents, name, and pkgs in the function signature.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Docker
    participant Registry

    User->>Docker: Build Docker Image
    Docker->>Docker: Execute docker-entrypoint.sh
    Docker->>Docker: Set HOPRD_DEFAULT_SESSION_LISTEN_HOST
    Docker->>Docker: Run hoprd
    Docker->>Registry: Upload Docker Image
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the toolchain Developer and product happiness label Nov 28, 2024
@tolbrino tolbrino added this to the 2.2.0-rc.1 milestone Nov 28, 2024
@tolbrino tolbrino changed the title docker: Preset session list host docker: Preset session listen host Nov 28, 2024
@tolbrino tolbrino marked this pull request as ready for review November 28, 2024 12:59
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
nix/docker-builder.nix (1)

Line range hint 24-30: LGTM! Clean implementation of command configuration.

The integration of Cmd into the Docker image config enables flexible binary execution as intended.

Consider documenting the supported binaries and their usage in the README or Docker-specific documentation to help users understand this new capability.

flake.nix (2)

181-210: Consider enhancing IP address handling and command validation

The entrypoint script implementation looks good overall, but there are a few potential improvements:

  1. The hostname -i command might return multiple IP addresses in some container configurations. Consider handling this case explicitly.
  2. The auxiliary command execution could benefit from additional validation.

Consider applying these improvements:

 dockerHoprdEntrypoint = pkgs.writeShellScriptBin "docker-entrypoint.sh" ''
   set -euo pipefail

   # if the default listen host has not been set by the user,
   # we will set it to the container's ip address

   listen_host="''${HOPRD_DEFAULT_SESSION_LISTEN_HOST:-}"
   listen_host_preset_ip="''${listen_host%%:*}"
   listen_host_preset_port="''${listen_host#*:}"

   if [ -z "''${listen_host_preset_ip:-}" ]; then
-    listen_host_ip="$(hostname -i)"
+    # Take the first IP address if multiple are returned
+    listen_host_ip="$(hostname -i | awk '{print $1}')"
+    
+    # Validate IP format
+    if ! echo "$listen_host_ip" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'; then
+      echo "Error: Invalid IP address format: $listen_host_ip" >&2
+      exit 1
+    fi

     if [ -z "''${listen_host_preset_port:-}" ]; then
       listen_host="''${listen_host_ip}"
     else
       listen_host="''${listen_host_ip}:''${listen_host_preset_port}"
     fi
   fi

   export HOPRD_DEFAULT_SESSION_LISTEN_HOST="''${listen_host}"

   if [ -x "/bin/''${1:-}" ]; then
+    # Validate command is in the allowed list
+    case "$1" in
+      hoprd|hoprd-api-schema)
+        exec "$@"
+        ;;
+      *)
+        echo "Error: Command not allowed: $1" >&2
+        exit 1
+        ;;
+    esac
-    # allow execution of auxiliary commands
-    exec "$@"
   else
     # default to hoprd
     exec /bin/hoprd "$@"
   fi
 '';

180-225: LGTM! Components are well integrated

The integration of the new Docker entrypoint:

  1. Correctly applies only to hoprd images
  2. Maintains backward compatibility
  3. Successfully implements the PR objective of allowing execution of other hopr binaries

This is a good architectural decision as it:

  • Simplifies the user experience by auto-configuring the listen host
  • Maintains flexibility by allowing user override
  • Enables auxiliary command execution while keeping the default behavior
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between f1d908c and 7a1c144.

📒 Files selected for processing (2)
  • flake.nix (1 hunks)
  • nix/docker-builder.nix (2 hunks)
🔇 Additional comments (3)
nix/docker-builder.nix (2)

8-16: LGTM! Package additions support new functionality.

The addition of nettools is appropriate as it provides necessary utilities for the entrypoint script to determine the container's IP address.


1-6: LGTM! Parameter changes align with PR objectives.

The addition of the optional Cmd parameter and parameter reordering improves the interface while maintaining backward compatibility.

Let's verify the usage of this new parameter:

✅ Verification successful

Parameter changes are properly implemented and used

The Cmd parameter is correctly used in the codebase:

  • Used in hoprd Docker image configuration with default command ["hoprd"]
  • Also utilized in another Docker configuration with a different command sequence
  • Implementation maintains backward compatibility while adding flexibility for different commands
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check how the new Cmd parameter is used across the codebase
# Expected: Find references showing how different binaries are specified

rg -l "Cmd\s*=" --type nix

Length of output: 38


Script:

#!/bin/bash
# Let's check the actual usage in flake.nix to understand how Cmd is being used
rg "Cmd\s*=" --type nix -A 5 -B 5

Length of output: 1038

flake.nix (1)

219-225: LGTM! Docker configuration is properly set up

The Docker arguments configuration correctly:

  1. Includes the entrypoint script in the image
  2. Sets up the entrypoint to use the new script
  3. Configures the default command to run hoprd

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
flake.nix (1)

181-211: LGTM! The entrypoint script implementation looks solid.

The script effectively handles the container's IP address configuration and provides flexibility for running auxiliary commands. Good use of set -euo pipefail for error handling.

Consider adding these improvements:

  1. Add a comment explaining why port 50000 is the default
  2. Add port number validation
 if [ -z "${listen_host_preset_ip:-}" ]; then
   listen_host_ip="$(hostname -i)"
+  # Validate that we got exactly one IP
+  if [[ "$listen_host_ip" =~ [[:space:]] ]]; then
+    echo "Error: Multiple IPs returned by hostname -i" >&2
+    exit 1
+  fi
 
   if [ -z "${listen_host_preset_port:-}" ]; then
+    # Default port for HOPR sessions
     listen_host="${listen_host_ip}:50000"
   else
+    # Validate port number
+    if ! [[ "${listen_host_preset_port}" =~ ^[0-9]+$ ]] || \
+       [ "${listen_host_preset_port}" -lt 1 ] || \
+       [ "${listen_host_preset_port}" -gt 65535 ]; then
+      echo "Error: Invalid port number: ${listen_host_preset_port}" >&2
+      exit 1
+    fi
     listen_host="${listen_host_ip}:${listen_host_preset_port}"
   fi
 fi
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 7a1c144 and 7c8fb96.

📒 Files selected for processing (1)
  • flake.nix (1 hunks)
🔇 Additional comments (2)
flake.nix (2)

219-226: LGTM! Docker configuration changes are well-structured.

The changes correctly integrate the new entrypoint script and maintain the default behavior while enabling auxiliary command execution.


219-226: Verify binary availability for auxiliary commands.

The PR description mentions running commands like hoprd-api-schema, but we need to verify if these binaries are included in the Docker image.

@tolbrino tolbrino added this pull request to the merge queue Nov 28, 2024
Merged via the queue into master with commit 462e588 Nov 28, 2024
28 checks passed
@tolbrino tolbrino deleted the tb/20241128-docker-ip branch November 28, 2024 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
toolchain Developer and product happiness
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants