Skip to content

Add option to upgrade kss via install script #64

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

Merged
merged 4 commits into from
Jan 25, 2025
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: |
gradleVersion="$(cat gradle.properties | sed -n 's/version=//p')"
gitVersion="${GITHUB_REF#refs/*/}"
if [ "$gradleVersion" != "$gitVersion" ]; then
if [ "$gradleVersion" != "$gitVersion" ]; then
echo "gradle properties version ($gradleVersion) doesn't match git tag version ($gitVersion)"
exit 1
fi
Expand Down
65 changes: 65 additions & 0 deletions .github/workflows/test_install_script.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
pull_request:
paths:
- 'install.sh'
- '.github/workflows/test_install_script.yml'
release:
types: [released]

jobs:
test_install_script:
Expand Down Expand Up @@ -31,6 +34,14 @@ jobs:
distribution: 'temurin'
java-version: 17

# HOME directory needs to be accessible by _www, so the java executable can be used
# not needed for linux, because JDK is not put in user directory
- name: Set launchd path
if: ${{ startsWith(matrix.runner, 'macos') }}
run: |
chmod o+rx "$HOME"
sudo launchctl setenv JAVA_HOME "$JAVA_HOME"

- name: install latest version
if: ${{ matrix.version == '' }}
run: sudo --preserve-env ./install.sh
Expand Down Expand Up @@ -63,3 +74,57 @@ jobs:
echo "expected exit code 124 but was $exit_code"
exit "$exit_code"
fi

- name: setup runner config
id: runner_config
run: |
if [[ '${{ matrix.runner }}' == ubuntu* ]]
then
echo 'config_dir=/usr/share/kss/' >> $GITHUB_OUTPUT
echo 'start_cmd=systemctl start kss' >> $GITHUB_OUTPUT
echo 'user=www-data' >> $GITHUB_OUTPUT
else
echo 'config_dir=/Library/Application Support/kss/' >> $GITHUB_OUTPUT
echo 'start_cmd=launchctl load -w /Library/LaunchDaemons/kss.plist' >> $GITHUB_OUTPUT
echo 'user=_www' >> $GITHUB_OUTPUT
fi

- name: setup socket
run: |
sudo mkdir -p /var/run/kss/
sudo chown '${{ steps.runner_config.outputs.user }}' /var/run/kss/

- name: Start kss
run: |
sudo cp .github/config/logback.xml '${{ steps.runner_config.outputs.config_dir }}kss.logback.xml'
echo "logging.logback.configurationFile=${{ steps.runner_config.outputs.config_dir }}kss.logback.xml" >> kss.properties
echo "dependencies.maven.homeDirectory=${{ steps.runner_config.outputs.config_dir }}.m2" >> kss.properties
sudo chown '${{ steps.runner_config.outputs.user }}' '${{ steps.runner_config.outputs.config_dir }}'
sudo mkdir '${{ steps.runner_config.outputs.config_dir }}.m2'
sudo chown '${{ steps.runner_config.outputs.user }}' '${{ steps.runner_config.outputs.config_dir }}.m2'
sudo mv kss.properties '${{ steps.runner_config.outputs.config_dir }}'
sudo ${{ steps.runner_config.outputs.start_cmd }}
sleep 5

- name: test linux service exists
if: ${{ startsWith(matrix.runner, 'ubuntu') }}
run: systemctl status kss.service

- name: test macos service exists
if: ${{ startsWith(matrix.runner, 'macos') }}
run: sudo launchctl list 'net.edwardday.kss'

- name: upgrade to latest version
run: |
sudo --preserve-env ./install.sh
sleep 5
env:
GH_TOKEN: ${{ github.token }}

- name: test linux service exists after upgrade
if: ${{ startsWith(matrix.runner, 'ubuntu') }}
run: systemctl status kss.service

- name: test macos service exists after upgrade
if: ${{ startsWith(matrix.runner, 'macos') }}
run: sudo launchctl list 'net.edwardday.kss'
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- script exception handling
- add option to import other scripts via `@Import` annotation
- upgrade current service via install script

## [0.5.0] - 2025-01-05

Expand All @@ -17,7 +18,7 @@
- option to install via release archive file
- option to set local maven repository settings
- version and help command line option
- option to specify install version explicitly
- option to specify install version explicitly

### Changed

Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ Kotlin Server Scripts let you run kotlin scripts in response to server requests,
## Setup

There is an easy to use install script to install the latest release on a linux system. It downloads the release
archive, installs the executable and creates and enables a system service.
archive, installs the executable and creates and enables a system service.

```shell
curl 'https://raw.githubusercontent.com/EdwarDDay/kotlin-server-scripts/main/install.sh' --output install.sh
chmod u+x install.sh
./install.sh # admin privileges might be needed
./install.sh # admin privileges might be needed
```

The `install.sh` script will by default save the executable in the `/usr/bin/` directory, creates system service file
with `www-data` as service execution user and puts a configuration sample file. If you want to configure any of these
The `install.sh` script will put the executable in a directory, creates a system service file with a service execution
user and puts a configuration sample file in a config directory based on the OS. If you want to configure any of these
settings or see the default values, execute `./install.sh --help` for more information.

To fetch the release asset, the script needs either authorized access to the GitHub API - either via a GH PAT
Expand Down Expand Up @@ -68,6 +68,10 @@ If you want to add a maven dependency, you can add a `@file:DependsOn("<artifact
dependency isn't in the maven central repository, you can add additional repositories via a
`@file:Repository("<repository url>")` annotation at file level.

### Add script dependencies

If you want to execute another `server.kts` script file, you can import it via `@file:Import("<relative script path>")`.

### Change logging configuration

You can reference a [logback configuration file](https://logback.qos.ch/manual/configuration.html) in the
Expand Down
66 changes: 46 additions & 20 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ esac

echo 'extract binary' >&2

if [ -d "${execution_directory}kss_lib/" ]; then
rm "${execution_directory}kss_lib/"*.jar
fi
archiveInternalName="$(tar --list --file "${archive_name}" --exclude="*/?*")"
tar --extract --gunzip --file "${archive_name}" --strip-components 2 --directory "${execution_directory}" "${archiveInternalName}bin/"

Expand All @@ -341,31 +344,54 @@ if [[ -n "${service_directory}" ]]; then
# escape sed escape char
service_user="${service_user/|/\\\|}"
tar --extract --gunzip --file "${archive_name}" --to-stdout "${archiveInternalName}service/$service_file" |\
sed "s|{{DIRECTORY}}|${execution_directory}|g" | \
sed "s|{{WORKING_DIRECTORY}}|${configuration_directory}|g" | \
sed "s|{{LOG_DIRECTORY}}|${log_directory}|g" | \
sed "s|{{USER}}|${service_user}|g" \
> "$service_directory$service_file"
sed "s|{{DIRECTORY}}|${execution_directory}|g" | \
sed "s|{{WORKING_DIRECTORY}}|${configuration_directory}|g" | \
sed "s|{{LOG_DIRECTORY}}|${log_directory}|g" | \
sed "s|{{USER}}|${service_user}|g" \
> "$service_file"
case $service_binary in
systemctl)
systemctl enable kss
echo 'The system service is enabled. Run'
echo ''
echo 'systemctl start kss'
echo ''
echo 'to start the service'
if [ ! -f "$service_directory$service_file" ]; then
mv "$service_file" "$service_directory$service_file"
systemctl enable kss
echo 'The system service is enabled. Run'
echo ''
echo 'systemctl start kss'
echo ''
echo 'to start the service'
elif diff --brief "$service_file" "$service_directory$service_file" > /dev/null; then
rm "$service_file"
echo 'Restart system service'
systemctl restart kss
else
mv "$service_file" "$service_directory$service_file"
echo 'Restart system service'
systemctl daemon-reload
systemctl restart kss
fi
;;
launchctl)
sudo touch "${log_directory}kss.log"
sudo touch "${log_directory}kss-error.log"
sudo chown "$service_user" "${log_directory}kss.log" "${log_directory}kss-error.log"
sudo chmod 600 "$service_directory$service_file"
echo 'The launch daemon is enabled. Run'
echo ''
echo "launchctl load -w $service_directory$service_file"
echo ''
echo 'to start the daemon'
touch "${log_directory}kss.log"
touch "${log_directory}kss-error.log"
chown "$service_user" "${log_directory}kss.log" "${log_directory}kss-error.log"
if [ ! -f "$service_directory$service_file" ]; then
mv "$service_file" "$service_directory$service_file"
chmod 600 "$service_directory$service_file"
echo 'The launch daemon is enabled. Run'
echo ''
echo "launchctl load -w $service_directory$service_file"
echo ''
echo 'to start the daemon'
else
mv "$service_file" "$service_directory$service_file"
chmod 600 "$service_directory$service_file"
echo 'reloading launch daemon'
launchctl unload "$service_directory$service_file"
launchctl load -w "$service_directory$service_file"
fi
;;
*)
mv "$service_file" "$service_directory$service_file"
esac

tar --extract --gunzip --file "${archive_name}" --strip-components 2 --directory "${configuration_directory}" "${archiveInternalName}config/"
Expand Down