|
| 1 | +#!/bin/bash |
| 2 | +#################################################################################################### |
| 3 | +# |
| 4 | +# Copyright (c) 2022, Jamf Software, LLC. All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are met: |
| 8 | +# * Redistributions of source code must retain the above copyright |
| 9 | +# notice, this list of conditions and the following disclaimer. |
| 10 | +# * Redistributions in binary form must reproduce the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer in the |
| 12 | +# documentation and/or other materials provided with the distribution. |
| 13 | +# * Neither the name of the JAMF Software, LLC nor the |
| 14 | +# names of its contributors may be used to endorse or promote products |
| 15 | +# derived from this software without specific prior written permission. |
| 16 | +# |
| 17 | +# THIS SOFTWARE IS PROVIDED BY JAMF SOFTWARE, LLC "AS IS" AND ANY |
| 18 | +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 | +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | +# DISCLAIMED. IN NO EVENT SHALL JAMF SOFTWARE, LLC BE LIABLE FOR ANY |
| 21 | +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 | +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 24 | +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 26 | +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | +# |
| 28 | +#################################################################################################### |
| 29 | + |
| 30 | +################################################################################ |
| 31 | +# Detects OpenClaw installations including: |
| 32 | +# - CLI binary (npm/pnpm global install) |
| 33 | +# - LaunchAgent services (current and legacy) |
| 34 | +# - macOS companion app |
| 35 | +# - Configuration directories |
| 36 | +# - Running processes/gateway |
| 37 | +# - Docker containers |
| 38 | +################################################################################ |
| 39 | + |
| 40 | +# Initialize detection arrays |
| 41 | +declare -a findings |
| 42 | + |
| 43 | +# Function to check if a command exists |
| 44 | +command_exists() { |
| 45 | + command -v "$1" >/dev/null 2>&1 |
| 46 | +} |
| 47 | + |
| 48 | +# Function to check Docker containers |
| 49 | +check_docker() { |
| 50 | + if command_exists docker; then |
| 51 | + # Check for running OpenClaw containers |
| 52 | + if docker ps --format '{{.Names}}' 2>/dev/null | grep -qi openclaw; then |
| 53 | + container_names=$(docker ps --format '{{.Names}}' 2>/dev/null | grep -i openclaw | tr '\n' ', ' | sed 's/,$//') |
| 54 | + findings+=("Docker-Running:${container_names}") |
| 55 | + fi |
| 56 | + |
| 57 | + # Check for stopped OpenClaw containers |
| 58 | + if docker ps -a --format '{{.Names}}' 2>/dev/null | grep -qi openclaw; then |
| 59 | + all_containers=$(docker ps -a --format '{{.Names}}' 2>/dev/null | grep -i openclaw | wc -l | tr -d ' ') |
| 60 | + if [[ $all_containers -gt 0 ]]; then |
| 61 | + findings+=("Docker-Containers:${all_containers}") |
| 62 | + fi |
| 63 | + fi |
| 64 | + fi |
| 65 | +} |
| 66 | + |
| 67 | +# Function to check for CLI installation |
| 68 | +check_cli() { |
| 69 | + # Check common npm global bin locations |
| 70 | + local npm_prefix="" |
| 71 | + |
| 72 | + if command_exists npm; then |
| 73 | + npm_prefix=$(npm prefix -g 2>/dev/null) |
| 74 | + if [[ -f "${npm_prefix}/bin/openclaw" ]]; then |
| 75 | + # Get version if possible |
| 76 | + local version=$("${npm_prefix}/bin/openclaw" --version 2>/dev/null | head -1) |
| 77 | + findings+=("CLI-NPM:${npm_prefix}/bin/openclaw") |
| 78 | + [[ -n "$version" ]] && findings+=("Version:${version}") |
| 79 | + fi |
| 80 | + fi |
| 81 | + |
| 82 | + # Check standard locations |
| 83 | + if [[ -f "/usr/local/bin/openclaw" ]]; then |
| 84 | + findings+=("CLI-Binary:/usr/local/bin/openclaw") |
| 85 | + fi |
| 86 | + |
| 87 | + # Check if openclaw command is available in PATH |
| 88 | + if command_exists openclaw && [[ ! " ${findings[@]} " =~ " CLI-" ]]; then |
| 89 | + local openclaw_path=$(which openclaw 2>/dev/null) |
| 90 | + findings+=("CLI-Path:${openclaw_path}") |
| 91 | + fi |
| 92 | +} |
| 93 | + |
| 94 | +# Function to check macOS app |
| 95 | +check_macos_app() { |
| 96 | + if [[ -d "/Applications/OpenClaw.app" ]]; then |
| 97 | + # Get app version from Info.plist if available |
| 98 | + local app_version="" |
| 99 | + if [[ -f "/Applications/OpenClaw.app/Contents/Info.plist" ]]; then |
| 100 | + app_version=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "/Applications/OpenClaw.app/Contents/Info.plist" 2>/dev/null) |
| 101 | + fi |
| 102 | + findings+=("App:/Applications/OpenClaw.app") |
| 103 | + [[ -n "$app_version" ]] && findings+=("AppVersion:${app_version}") |
| 104 | + fi |
| 105 | + |
| 106 | + # Check user Applications folders |
| 107 | + for user_home in /Users/*; do |
| 108 | + [[ ! -d "$user_home" ]] && continue |
| 109 | + [[ "$user_home" == "/Users/Shared" ]] && continue |
| 110 | + |
| 111 | + if [[ -d "${user_home}/Applications/OpenClaw.app" ]]; then |
| 112 | + local username=$(basename "$user_home") |
| 113 | + findings+=("App-User:${username}") |
| 114 | + fi |
| 115 | + done |
| 116 | +} |
| 117 | + |
| 118 | +# Function to check LaunchAgents |
| 119 | +check_launch_agents() { |
| 120 | + for user_home in /Users/*; do |
| 121 | + [[ ! -d "$user_home" ]] && continue |
| 122 | + [[ "$user_home" == "/Users/Shared" ]] && continue |
| 123 | + |
| 124 | + local username=$(basename "$user_home") |
| 125 | + local launch_agents_dir="${user_home}/Library/LaunchAgents" |
| 126 | + |
| 127 | + # Check current naming convention |
| 128 | + if [[ -f "${launch_agents_dir}/bot.molt.gateway.plist" ]]; then |
| 129 | + # Check if it's loaded |
| 130 | + local is_loaded=$(sudo -u "$username" launchctl list 2>/dev/null | grep -c "bot.molt.gateway") |
| 131 | + if [[ $is_loaded -gt 0 ]]; then |
| 132 | + findings+=("LaunchAgent-Active:${username}") |
| 133 | + else |
| 134 | + findings+=("LaunchAgent-Installed:${username}") |
| 135 | + fi |
| 136 | + fi |
| 137 | + |
| 138 | + # Check legacy naming convention |
| 139 | + if [[ -f "${launch_agents_dir}/com.openclaw.gateway.plist" ]]; then |
| 140 | + findings+=("LaunchAgent-Legacy:${username}") |
| 141 | + fi |
| 142 | + |
| 143 | + # Check for profile-based agents (bot.molt.*) |
| 144 | + local profile_agents=$(find "${launch_agents_dir}" -name "bot.molt.*.plist" 2>/dev/null | wc -l | tr -d ' ') |
| 145 | + if [[ $profile_agents -gt 0 ]]; then |
| 146 | + findings+=("LaunchAgent-Profiles:${username}:${profile_agents}") |
| 147 | + fi |
| 148 | + done |
| 149 | +} |
| 150 | + |
| 151 | +# Function to check configuration directories |
| 152 | +check_config_dirs() { |
| 153 | + local found_configs=0 |
| 154 | + |
| 155 | + for user_home in /Users/*; do |
| 156 | + [[ ! -d "$user_home" ]] && continue |
| 157 | + [[ "$user_home" == "/Users/Shared" ]] && continue |
| 158 | + |
| 159 | + local username=$(basename "$user_home") |
| 160 | + local config_dir="${user_home}/.openclaw" |
| 161 | + |
| 162 | + if [[ -d "$config_dir" ]]; then |
| 163 | + found_configs=$((found_configs + 1)) |
| 164 | + |
| 165 | + # Check for main config file |
| 166 | + if [[ -f "${config_dir}/openclaw.json" ]]; then |
| 167 | + findings+=("Config:${username}") |
| 168 | + |
| 169 | + # Check workspace |
| 170 | + if [[ -d "${config_dir}/workspace" ]]; then |
| 171 | + findings+=("Workspace:${username}") |
| 172 | + fi |
| 173 | + |
| 174 | + # Check for credentials |
| 175 | + if [[ -f "${config_dir}/credentials/profiles.json" ]]; then |
| 176 | + findings+=("Credentials:${username}") |
| 177 | + fi |
| 178 | + fi |
| 179 | + fi |
| 180 | + |
| 181 | + # Check for workspace directory (could be separate) |
| 182 | + if [[ -d "${user_home}/openclaw/workspace" ]]; then |
| 183 | + findings+=("Workspace-Alt:${username}") |
| 184 | + fi |
| 185 | + done |
| 186 | + |
| 187 | + [[ $found_configs -gt 0 ]] && findings+=("ConfigDirs:${found_configs}") |
| 188 | +} |
| 189 | + |
| 190 | +# Function to check running processes |
| 191 | +check_processes() { |
| 192 | + # Check for openclaw processes |
| 193 | + if pgrep -f "openclaw" >/dev/null 2>&1; then |
| 194 | + local process_count=$(pgrep -f "openclaw" | wc -l | tr -d ' ') |
| 195 | + findings+=("Process-Running:${process_count}") |
| 196 | + |
| 197 | + # Check if Gateway is running on default port |
| 198 | + if lsof -i :18789 >/dev/null 2>&1; then |
| 199 | + findings+=("Gateway-Port:18789") |
| 200 | + fi |
| 201 | + fi |
| 202 | + |
| 203 | + # Check for node processes that might be running openclaw |
| 204 | + if pgrep -f "node.*openclaw" >/dev/null 2>&1; then |
| 205 | + local node_count=$(pgrep -f "node.*openclaw" | wc -l | tr -d ' ') |
| 206 | + [[ $node_count -gt 0 ]] && findings+=("Node-Process:${node_count}") |
| 207 | + fi |
| 208 | +} |
| 209 | + |
| 210 | +# Function to check for source installation |
| 211 | +check_source_install() { |
| 212 | + for user_home in /Users/*; do |
| 213 | + [[ ! -d "$user_home" ]] && continue |
| 214 | + [[ "$user_home" == "/Users/Shared" ]] && continue |
| 215 | + |
| 216 | + local username=$(basename "$user_home") |
| 217 | + |
| 218 | + # Check common locations for git clone |
| 219 | + for dir in "${user_home}/openclaw" "${user_home}/Documents/openclaw" "${user_home}/Projects/openclaw" "${user_home}/src/openclaw"; do |
| 220 | + if [[ -d "$dir" ]] && [[ -f "$dir/package.json" ]]; then |
| 221 | + # Verify it's actually openclaw by checking package.json |
| 222 | + if grep -q '"name": "openclaw"' "$dir/package.json" 2>/dev/null; then |
| 223 | + findings+=("Source:${username}:${dir}") |
| 224 | + fi |
| 225 | + fi |
| 226 | + done |
| 227 | + done |
| 228 | +} |
| 229 | + |
| 230 | +# Run all checks |
| 231 | +check_docker |
| 232 | +check_cli |
| 233 | +check_macos_app |
| 234 | +check_launch_agents |
| 235 | +check_config_dirs |
| 236 | +check_processes |
| 237 | +check_source_install |
| 238 | + |
| 239 | +# Format and return results |
| 240 | +if [[ ${#findings[@]} -eq 0 ]]; then |
| 241 | + echo "<result>Not Installed</result>" |
| 242 | +else |
| 243 | + # Join findings with semicolon separator |
| 244 | + result=$(IFS=';'; echo "${findings[*]}") |
| 245 | + echo "<result>${result}</result>" |
| 246 | +fi |
| 247 | + |
| 248 | +exit 0 |
0 commit comments