Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
21 changes: 14 additions & 7 deletions .github/workflows/jest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ jobs:
test:
runs-on: ubuntu-latest
env:
DATABASE_URL: 'mysql://test:test@localhost:3306/test'
CONFIG_DOCKER_HOST: '///var/run/docker.sock'
DEBUG: 'true'
DATABASE_URL: "mysql://test:test@localhost:3306/test"
CONFIG_DOCKER_HOST: "///var/run/docker.sock"
CONFIG_RESOURCES_PATH: "./resources"
DEBUG: "true"
steps:
- name: Checkout
uses: actions/checkout@v2
Expand All @@ -26,14 +27,20 @@ jobs:
echo "This PR is from the dev branch. Exiting..."
exit 0
fi
- name: Prepare resources folder for tests
run: |
# Create test template folder
mkdir -p ./resources/templates/test
# Copy test template files to the resources folder
cp -r ./dev/templates/test/* ./resources/templates/test/
- name: Shutdown default MySQL
run: sudo service mysql stop
- name: Setup MySQL
uses: mirromutth/mysql-action@v1.1
with:
mysql database: 'test'
mysql user: 'test'
mysql password: 'test'
mysql database: "test"
mysql user: "test"
mysql password: "test"
- name: Install Node.js
uses: actions/setup-node@v6
with:
Expand All @@ -48,4 +55,4 @@ jobs:
run: npm test
- name: Publish Test Summary Results
run: npx github-actions-ctrf ctrf/ctrf-report.json
if: always()
if: always()
12 changes: 6 additions & 6 deletions .github/workflows/push-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
workflow_dispatch:
inputs:
tag:
description: 'Tag to use for the image (optional, defaults to branch name and short SHA)'
description: "Tag to use for the image (optional, defaults to branch name and short SHA)"
required: false

jobs:
Expand All @@ -30,20 +30,20 @@ jobs:
run: |
IMAGE_OWNER=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')
IMAGE_NAME=ghcr.io/$IMAGE_OWNER/node-server-manager

if [ "${{ github.event_name }}" = "release" ]; then
TAG=${{ github.event.release.tag_name }}
echo "IS_RELEASE=true" >> $GITHUB_ENV

elif [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ -n "${{ github.event.inputs.tag }}" ]; then
TAG=${{ github.event.inputs.tag }}
echo "IS_RELEASE=true" >> $GITHUB_ENV

else
TAG=${GITHUB_REF_NAME}-$(echo $GITHUB_SHA | cut -c1-7)
echo "IS_RELEASE=false" >> $GITHUB_ENV
fi

echo "IMAGE_NAME=$IMAGE_NAME" >> $GITHUB_ENV
echo "IMAGE_TAG=$TAG" >> $GITHUB_ENV

Expand All @@ -66,4 +66,4 @@ jobs:
if: env.IS_RELEASE == 'true'
run: |
docker tag $IMAGE_NAME:$IMAGE_TAG $IMAGE_NAME:latest
docker push $IMAGE_NAME:latest
docker push $IMAGE_NAME:latest
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ dist
.pnp.*

# Other
templates
volumes

# Exclude all addons except the example one
Expand Down
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ignore artifacts:
build
coverage
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ COPY index.ts ./

RUN npm run build

CMD npx prisma migrate deploy && npm run start
CMD npm start
34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.PHONY: build test up down restart logs shell ps

ATTACH ?= 0

all: build

build:
docker compose build

test:
ifeq ($(ATTACH),1) # if ATTACH=1, run tests with debugger attached
docker compose run --rm -p 9229:9229 nsm \
sh -c 'npm run migrate && node --inspect-brk=0.0.0.0:9229 ./node_modules/.bin/jest --runInBand $(ARGS)'
else
docker compose run --rm nsm npm run test $(ARGS)
endif

up:
docker compose up -d

down:
docker compose down

restart:
docker compose restart

logs:
docker compose logs -f

shell:
docker compose exec nsm sh

ps:
docker compose ps
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ NSM is a robust service manager built on Docker Engine. Its primary purpose is t
- **Resources usage management**: NSM provides ability to limit or extend resources limits and view current usage.

## API Specification

<a href="https://zortik.github.io/nsm-spec/" target="_blank">Specification is hosted on external repository here</a>

## Prerequisites
Expand All @@ -27,35 +28,42 @@ Ensure you have the following installed before proceeding with the installation:
Follow these steps to install and set up NSM:

1. **Clone the Repository**

```sh
git clone https://github.com/ZorTik/node-server-manager
```

Alternatively, download the latest release from the [NSM repository](https://github.com/ZorTik/node-server-manager) and extract it.

2. **Configure Environment Variables**
Copy the example environment file and fill in the required values:

```sh
cp .env.example .env
```

Open the `.env` file and provide the necessary configuration values.

3. **Edit Configuration**
Adjust the default configuration settings in `resources/config.yml` according to your requirements. You can also override these settings using environment variables. Detailed information about each configuration option is available within the file.

4. **Install Dependencies**
Install the required Node.js packages:

```sh
npm install
```

5. **Generate Prisma Client**
Generate the Prisma client for database interaction:

```sh
npx prisma generate
```

6. **Sync Database Schema**
Apply the database schema migrations:

```sh
npx prisma migrate deploy
```
Expand Down
16 changes: 8 additions & 8 deletions addons/example_addon/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {Addon} from "@nsm/addon";
import { Addon } from "@nsm/addon";
import winston from "winston";

async function initAfterLogger(ctx: { logger: winston.Logger }) {
ctx.logger.info('Hello from example addon!');
ctx.logger.info("Hello from example addon!");
}

export default {
name: 'example_addon',
disabled: true,
steps: {
BEFORE_CONFIG: initAfterLogger,
}
} as Addon;
name: "example_addon",
disabled: true,
steps: {
BEFORE_CONFIG: initAfterLogger,
},
} as Addon;
2 changes: 1 addition & 1 deletion addons/example_addon/libraries.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
express=4.18.2
express=5.2.1
10 changes: 5 additions & 5 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
],
};
presets: [
["@babel/preset-env", { targets: { node: "current" } }],
"@babel/preset-typescript",
],
};
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 'Test'
description: 'A Test template'
name: "Test"
description: "A Test template"
port_range:
min: 22222
max: 33333
Expand All @@ -9,6 +9,6 @@ defaults:
disk: 2000000000 # bytes
meta:
# Stop command to be sent in stop signal endpoint
stopCmd: 'stop'
stopCmd: "stop"
# Optional ENV vars, and their default values
env: {}
env: {}
37 changes: 29 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ services:
nsm:
build: .
volumes:
- '/var/run/docker.sock:/var/run/docker.sock'
- "./dev/templates/test:/data/resources/templates/test:ro"
- "./tests:/data/tests:ro"
ports:
- '3000:3000'
- "3000:3000"
extra_hosts:
- 'docker.host.internal:host-gateway'
- "docker.host.internal:host-gateway"
environment:
- 'CONFIG_DOCKER_HOST=///var/run/docker.sock'
- 'DATABASE_URL=mysql://root:test@db:3306/nsm'
- "CONFIG_DOCKER_HOST=http://docker:2375"
- "CONFIG_RESOURCES_PATH=/data/resources"
- "DATABASE_URL=mysql://root:test@db:3306/nsm"
- "DOCKER_HOST=tcp://docker:2375"
depends_on:
db:
condition: service_healthy
docker:
condition: service_healthy
healthcheck:
test: ["CMD-SHELL", "curl -f http://localhost:3000/ || exit 1"]
interval: 5s
Expand All @@ -28,14 +33,29 @@ services:
- ./logs
- ./node_packages
- ./package-lock.json

docker:
image: docker:29.1.3-dind
privileged: true
volumes:
- docker_data:/var/lib/docker
environment:
# in dev environment, we don't need TLS for Dind.
DOCKER_TLS_CERTDIR: ""
ports:
- "2377:2375"
healthcheck:
test: ["CMD", "docker", "info"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s
db:
image: mariadb:10.4
environment:
MARIADB_ROOT_PASSWORD: test
MARIADB_DATABASE: nsm
ports:
- '3306:3306'
- "3306:3306"
volumes:
- nsm_db:/var/lib/mysql
healthcheck:
Expand All @@ -46,4 +66,5 @@ services:
start_period: 10s

volumes:
nsm_db:
nsm_db:
docker_data:
14 changes: 7 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {init} from "@nsm/app";
import {postInit} from "@nsm/cleanup";
import { init } from "@nsm/app";
import { postInit } from "@nsm/cleanup";
import server from "@nsm/server";

init(server)
// Run some cleanup tasks and register handlers
.then(postInit)
.catch((e) => {
console.log(e);
});
// Run some cleanup tasks and register handlers
.then(postInit)
.catch((e) => {
console.log(e);
});
26 changes: 14 additions & 12 deletions installTempDeps.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
const fs = require("fs");
const npm = require("npm");

console.log('Preinstalling dependencies for build...');
console.log("Preinstalling dependencies for build...");

npm.load().then(() => {
for (let addon of fs.readdirSync(process.cwd() + '/addons')) {
const libFPath = process.cwd() + '/addons/' + addon + '/libraries.txt';
if (!fs.existsSync(libFPath)) {
continue;
}
const libs = fs.readFileSync(libFPath, 'utf8').split('\n')
.map((lib) => lib.split('=')[0] + '@' + lib.split('=')[1]);
npm.commands.install(libs, (err) => {
console.log(err);
});
for (let addon of fs.readdirSync(process.cwd() + "/addons")) {
const libFPath = process.cwd() + "/addons/" + addon + "/libraries.txt";
if (!fs.existsSync(libFPath)) {
continue;
}
});
const libs = fs
.readFileSync(libFPath, "utf8")
.split("\n")
.map((lib) => lib.split("=")[0] + "@" + lib.split("=")[1]);
npm.commands.install(libs, (err) => {
console.log(err);
});
}
});
Loading
Loading