Skip to content

Conversation

Copy link

Copilot AI commented Oct 9, 2025

Problem

The fips.sh scripts in all four modules (gateway, traffic-manager, all-in-one-apim, and api-control-plane) failed when executed from outside their /bin directory. This was caused by the scripts using the $PRG variable without initializing it, resulting in incorrect CARBON_HOME path calculation.

Example of the Issue

# This worked fine
cd /path/to/wso2am/bin
./fips.sh

# But this failed with incorrect CARBON_HOME
cd /path/to/wso2am
./bin/fips.sh

When $PRG is uninitialized:

  • PRGDIR defaults to . (current directory)
  • CARBON_HOME is incorrectly calculated as / or the wrong path
  • The script fails to locate required components and resources

Solution

Added the missing PRG variable initialization and symlink resolution logic to all four fips.sh scripts, following the same pattern used in other startup scripts like gateway.sh, api-cp.sh, and key-manager.sh:

# resolve links - $0 may be a softlink
PRG="$0"

while [ -h "$PRG" ]; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '.*/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done

# Get standard environment variables
PRGDIR=`dirname "$PRG"`

# Only set CARBON_HOME if not already set
[ -z "$CARBON_HOME" ] && CARBON_HOME=`cd "$PRGDIR/.." ; pwd`

This ensures:

  1. The script path is properly captured regardless of how it's invoked
  2. Symlinks are resolved to the actual script location
  3. CARBON_HOME is correctly calculated relative to the script location

Changes

  • gateway/modules/distribution/product/src/main/startup-scripts/fips.sh
  • traffic-manager/modules/distribution/product/src/main/startup-scripts/fips.sh
  • all-in-one-apim/modules/distribution/product/src/main/startup-scripts/fips.sh
  • api-control-plane/modules/distribution/product/src/main/startup-scripts/fips.sh

Testing

All test scenarios verified:

  • ✅ Script execution from within bin directory
  • ✅ Script execution from outside bin directory (previously failing)
  • ✅ Script execution with absolute paths
  • ✅ Script execution through symlinks
  • ✅ No syntax errors in any updated script

Impact

  • Minimal changes: 52 lines added across 4 files (13 lines each)
  • No breaking changes: Existing functionality preserved
  • Consistent behavior: Scripts now align with other startup scripts in the codebase
  • Improved usability: Scripts can be executed from any directory location

Fixes the issue where fips.sh scripts only worked when executed from within the /bin directory.

Original prompt

This section details on the original issue you should resolve

<issue_title>The fips.sh script fails when executed outside the /bin directory</issue_title>
<issue_description>### Current Limitation

Currently, the fips.sh script works only inside <APIM_HOME>/bin directory. When running the script from any other path (using a relative path) leads to failures.

Steps to Reproduce

  1. Go to <APIM_HOME>
  2. Execute the command ./bin/fips.sh

Suggested Improvement

Added a link-resolution section at the beginning of the script to correctly identify the actual script location even when it is invoked via a symlink or from a different working directory.

# resolve links - $0 may be a softlink
PRG="$0"

while [ -h "$PRG" ]; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '.*/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done

Version

4.5.0</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes wso2/api-manager#4417


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@HeshanSudarshana
Copy link
Contributor

@copilot verify whether the same issue is there for fips.bat file as well

Copilot AI changed the title [WIP] Fix fips.sh execution when outside /bin directory Fix fips.sh scripts to work when executed from outside the /bin directory Oct 9, 2025
Copilot AI requested a review from HeshanSudarshana October 9, 2025 19:42
Copilot finished work on behalf of HeshanSudarshana October 9, 2025 19:42
Copy link
Author

Copilot AI commented Oct 9, 2025

@copilot verify whether the same issue is there for fips.bat file as well

The fips.bat files do not have the same issue. They already use the correct pattern at line 29:

if "%CARBON_HOME%"=="" set CARBON_HOME=%~sdp0..

The %~sdp0 is a Windows batch parameter that automatically:

  • Resolves the script's directory path (equivalent to dirname "$0")
  • Handles paths with spaces
  • Works regardless of where the script is called from

This is the Windows equivalent of the fix we applied to the .sh files. All four fips.bat files (gateway, traffic-manager, all-in-one-apim, api-control-plane) already have this correct implementation, matching the pattern used in other .bat startup scripts like gateway.bat and api-cp.bat.

Verified: ✓ gateway/fips.bat ✓ traffic-manager/fips.bat ✓ all-in-one-apim/fips.bat ✓ api-control-plane/fips.bat

Copilot finished work on behalf of HeshanSudarshana October 9, 2025 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The fips.sh script fails when executed outside the /bin directory

3 participants