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

feat: packaging mac #381

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
11 changes: 10 additions & 1 deletion omnibus/config/projects/nodex-agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@
exclude "**/bundler/git"

# handle distribution by environment variable
if ENV['TARGET_PLATFORM'] == 'ubuntu'
if ENV["TARGET_PLATFORM"] == "ubuntu"
package_scripts_path "#{Omnibus::Config.project_root}/package-scripts/nodex-agent-deb"
elsif ENV["TARGET_PLATFORM"] == "mac"
package_scripts_path "#{Omnibus::Config.project_root}/package-scripts/nodex-agent-pkg"
end

package :deb do
compression_level 5
compression_type :xz
end
package :pkg do
identifier 'com.nodex.nodex-agent'
end
compress :dmg do
window_bounds "200, 200, 750, 600"
pkg_position "10, 10"
end
1 change: 0 additions & 1 deletion omnibus/config/software/build-nodex-agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
copy "#{nodex_dir}/Cargo.toml", "#{project_dir}"
copy "#{nodex_dir}/Cargo.lock", "#{project_dir}"


command "cd #{project_dir} && cross build --target #{ENV['TARGET_ARCH']} --release"
copy "#{project_dir}/target/#{ENV['TARGET_ARCH']}/release/nodex-agent", "#{install_dir}/bin"
if ENV['TARGET_PLATFORM'] == 'ubuntu'
Expand Down
8 changes: 8 additions & 0 deletions omnibus/config/software/init-scripts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,13 @@
mode: 0644,
vars: { install_dir: install_dir, etc_dir: etc_dir }
project.extra_package_file "#{systemd_directory}/nodex-agent.service"
elsif ohai['platform_family'] == 'mac_os_x'
conf_dir = "#{install_dir}/etc"
mkdir conf_dir
erb source: "launchd.plist.erb",
dest: "#{conf_dir}/com.nodex.nodex-agent.plist",
mode: 0644,
vars: { install_dir: install_dir }
# project.extra_package_file "#{launchd_directory}/com.nodex.nodex-agent.plist"
end
end
66 changes: 66 additions & 0 deletions omnibus/config/templates/init-scripts/launchd.plist.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.nodex.nodex-agent</string>

<key>ProgramArguments</key>
<array>
<string><%= install_dir %>/bin/nodex-agent</string>
</array>

<key>RunAtLoad</key>
<false/>

<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
<key>CrashThreshold</key>
<integer>5</integer>
<key>RestartInterval</key>
<integer>10</integer>
</dict>

<key>UserName</key>
<string>nodex</string>
<key>GroupName</key>
<string>nodex</string>

<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
</dict>

<key>StandardOutPath</key>
<string>/var/log/nodex-agent.log</string>
<key>StandardErrorPath</key>
<string>/var/log/nodex-agent.err</string>

<key>SessionCreate</key>
<true/>
<key>EnableTransactions</key>
<true/>
<key>LaunchOnlyOnce</key>
<true/>

<key>HardResourceLimits</key>
<dict>
<key>Core</key>
<integer>0</integer>
</dict>

<key>WorkingDirectory</key>
<string>/Users/nodex</string>
<key>RootDirectory</key>
<string>/Users/nodex</string>

<key>MachServices</key>
<dict>
<key>com.nodex.nodex-agent</key>
<true/>
</dict>
</dict>
</plist>
31 changes: 31 additions & 0 deletions omnibus/debug/mac/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Building the nodex-agent with Omnibus
To build the nodex-agent using Omnibus on a macOS system with the ARM architecture, follow these steps:

## Step 1: Set the Target Architecture and Platform

Before starting the build process, you need to export the environment variables to specify the target architecture and platform:

```
export TARGET_ARCH=aarch64-apple-darwin
export TARGET_PLATFORM=mac
```

## Step 2: Navigate to the Omnibus Directory

Change directory to the omnibus directory within the project:

```
cd omnibus
```

## Step 3: Run the Omnibus Build

Use the following command to start the Omnibus build process for the nodex-agent.
The sudo -E command ensures that the environment variables are preserved when running with elevated privileges.

```
sudo -E bin/omnibus build nodex-agent
```

This command will initiate the build process, creating the necessary packages for the nodex-agent.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This will be investigated in the future.
Navigate to the root of the project directory and run the following command to build the Docker image:

```
docker build -t omnibus-ubuntu -f omnibus/images/ubuntu/Dockerfile .
docker build -t omnibus-ubuntu -f omnibus/debug/ubuntu/Dockerfile .
```

This command will build the Docker image using the Dockerfile located at omnibus/images/ubuntu/Dockerfile. The resulting image will be tagged as omnibus-ubuntu.
Expand Down
4 changes: 2 additions & 2 deletions omnibus/docs/deb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ dpkg -i /path/to/<downloaded_filename>.deb
Run the following commands to set the configuration. Note that these commands must be executed as the nodex user.

```
sudo -u nodex /usr/bin/nodex-agent --config network set --key project_did --value <your project_did>
sudo -u nodex /usr/bin/nodex-agent --config network set --key secret_key --value <your secret_key>
sudo -H -u nodex /usr/bin/nodex-agent --config network set --key project_did --value <your project_did>
sudo -H -u nodex /usr/bin/nodex-agent --config network set --key secret_key --value <your secret_key>
```

Replace <your project_did> and <your secret_key> with the actual values specific to your project.
Expand Down
8 changes: 8 additions & 0 deletions omnibus/omnibus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,11 @@
# ------------------------------
windows_arch %w{x86 x64}.include?((ENV['OMNIBUS_WINDOWS_ARCH'] || '').downcase) ?
ENV['OMNIBUS_WINDOWS_ARCH'].downcase.to_sym : :x86

if ENV['TARGET_PLATFORM'] == 'mac'
# We are distributing only the binary, but the health check is currently set to check links for dylib and bundle files.
# Since lld was used to check the binary in deb packages, I believe we should use otool to check the binary on macOS as well.
# Therefore, we will disable the health check on macOS.
# https://github.com/chef/omnibus/blob/0633d009f24dfced4eb6ab76346b84fbb8c970f0/lib/omnibus/health_check.rb#L298
health_check false
end
93 changes: 93 additions & 0 deletions omnibus/package-scripts/nodex-agent-pkg/postinstall
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/sh
#
# Perform necessary nodex-agent setup steps
# after package is installed.
#

INSTALL_DIR=/opt/nodex-agent
CONF_DIR=$INSTALL_DIR/etc
SERVICE_NAME=com.nodex.nodex-agent
HOME_DIR=/Users/nodex/
GROUP_NAME=nodex

set -e

# Check if the group already exists, if not, create it
if ! dscl . -read /Groups/$GROUP_NAME >/dev/null 2>&1; then
echo "Creating dedicated group for nodex-agent..."
dscl . -create /Groups/$GROUP_NAME
dscl . -create /Groups/$GROUP_NAME RealName "Nodex Agent Group"
dscl . -create /Groups/$GROUP_NAME PrimaryGroupID "510"
else
echo "Group $GROUP_NAME already exists."
fi

# Check if the user already exists, if not, create it
if ! id -u nodex >/dev/null 2>&1; then
echo "Creating dedicated user for nodex-agent..."
dscl . -create /Users/nodex
dscl . -create /Users/nodex UserShell /usr/bin/false
dscl . -create /Users/nodex RealName "Nodex Agent"
dscl . -create /Users/nodex UniqueID "510"
dscl . -create /Users/nodex PrimaryGroupID "510"
dscl . -append /Groups/$GROUP_NAME GroupMembership nodex
dscl . -create /Users/nodex NFSHomeDirectory $HOME_DIR
mkdir -p $HOME_DIR
chown nodex:$GROUP_NAME $HOME_DIR
else
echo "User nodex already exists."
fi

# Ensure the installation directory exists before setting ownership and permissions
if [ ! -d "${INSTALL_DIR}" ]; then
echo "Creating installation directory ${INSTALL_DIR}..."
mkdir -p ${INSTALL_DIR}
else
echo "Installation directory ${INSTALL_DIR} already exists."
fi

# Set ownership and permissions
chown -R root:$GROUP_NAME ${INSTALL_DIR}
chmod -R 750 ${INSTALL_DIR}
mkdir -p ${HOME_DIR}
chown -R nodex:$GROUP_NAME ${HOME_DIR}
chmod -R 740 ${HOME_DIR}

# Create a symlink to the agent's binary, handling existing symlink
if [ -L /usr/local/bin/nodex-agent ]; then
echo "Updating existing symlink for nodex-agent..."
ln -sf $INSTALL_DIR/bin/nodex-agent /usr/local/bin/nodex-agent
elif [ -e /usr/local/bin/nodex-agent ]; then
echo "A file already exists at /usr/local/bin/nodex-agent. Removing it to create a symlink..."
rm /usr/local/bin/nodex-agent
ln -sf $INSTALL_DIR/bin/nodex-agent /usr/local/bin/nodex-agent
else
echo "Creating a new symlink for nodex-agent..."
ln -sf $INSTALL_DIR/bin/nodex-agent /usr/local/bin/nodex-agent
fi

# Load the launchd service
cp -vf "$CONF_DIR/com.nodex.nodex-agent.plist" "/Library/LaunchDaemons/$SERVICE_NAME.plist"
if [ -f "/Library/LaunchDaemons/$SERVICE_NAME.plist" ]; then
echo "Reloading the $SERVICE_NAME service..."
if launchctl list | grep -q "$SERVICE_NAME"; then
echo "Unloading existing $SERVICE_NAME service..."
sudo launchctl unload /Library/LaunchDaemons/$SERVICE_NAME.plist
fi
sudo launchctl load /Library/LaunchDaemons/$SERVICE_NAME.plist
sudo launchctl enable system/$SERVICE_NAME
echo "$SERVICE_NAME service loaded and enabled."
else
echo "[ WARNING ]\tThe launchd service file was not found. Please ensure it is installed correctly."
fi

# Start the service if the configuration file exists
if [ -f "$HOME_DIR/.config/nodex/config.json" ]; then
echo "(Re)starting $SERVICE_NAME now..."
launchctl kickstart -k system/$SERVICE_NAME || true
fi

echo "$SERVICE_NAME has been installed"
echo "Thank you for installing nodex-agent!"

exit 0
76 changes: 76 additions & 0 deletions omnibus/package-scripts/nodex-agent-pkg/postrm
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/sh
# Perform necessary cleanup steps after nodex-agent package is removed, purged, upgraded, or deconfigured.

INSTALL_DIR=/opt/nodex-agent
SERVICE_NAME=com.nodex.nodex-agent
HOME_DIR=/Users/nodex/

set -e

case "$1" in
remove)
echo "Stopping and unloading service $SERVICE_NAME..."
if launchctl list | grep -q "$SERVICE_NAME"; then
if ! launchctl unload /Library/LaunchDaemons/$SERVICE_NAME.plist; then
echo "Failed to unload service $SERVICE_NAME"
fi
fi

if [ -d "$INSTALL_DIR" ]; then
echo "Removing installation directory $INSTALL_DIR..."
if ! rm -rf "$INSTALL_DIR"; then
echo "Failed to remove installation directory"
fi
fi
;;

purge)
echo "Purging configuration and removing directories..."

# Stop and unload the service, if it still exists
if launchctl list | grep -q "$SERVICE_NAME"; then
if ! launchctl unload /Library/LaunchDaemons/$SERVICE_NAME.plist; then
echo "Failed to unload service $SERVICE_NAME"
fi
fi

# Remove the service file
if [ -f "/Library/LaunchDaemons/$SERVICE_NAME.plist" ]; then
echo "Removing service file $SERVICE_NAME.plist..."
if ! rm /Library/LaunchDaemons/$SERVICE_NAME.plist; then
echo "Failed to remove service file"
fi
fi

# Remove user and group
if id -u nodex >/dev/null 2>&1; then
echo "Removing nodex user and group..."
dscl . -delete /Users/nodex
dscl . -delete /Groups/nodex
fi

# Remove the installation directory
if [ -d "$INSTALL_DIR" ]; then
echo "Removing installation directory $INSTALL_DIR..."
if ! rm -rf "$INSTALL_DIR"; then
echo "Failed to remove installation directory"
fi
fi

# Remove configuration files
if [ -d "$HOME_DIR" ]; then
echo "Removing configuration directory $HOME_DIR..."
chmod -R 755 "$HOME_DIR"
if ! rm -rf "$HOME_DIR"; then
echo "Failed to remove configuration directory"
fi
fi
;;

*)
echo "Unknown option: $1"
;;
esac

echo "$SERVICE_NAME post-removal script completed."
exit 0
18 changes: 18 additions & 0 deletions omnibus/package-scripts/nodex-agent-pkg/preinstall
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh
#
# Perform necessary nodex-agent setup steps
# before package is installed.
#

set -e

INSTALL_DIR=/opt/nodex-agent

if [ -d "$INSTALL_DIR" ]; then
echo "Backing up existing installation directory..."
cp -r "$INSTALL_DIR" "$INSTALL_DIR.backup"
fi

echo "You're about to install nodex-agent!"

exit 0
15 changes: 15 additions & 0 deletions omnibus/package-scripts/nodex-agent-pkg/prerm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
#
# Perform necessary nodex-agent setup steps
# prior to installing package.
#

PROGNAME=`basename $0`

error_exit()
{
echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2
exit 1
}

exit 0
Loading