Skip to content

Commit

Permalink
Feature(ISSUE 79) - Node runner (#88)
Browse files Browse the repository at this point in the history
* feat: Added NodeJS runner

* feat(deploy-runner.yml): Create deploy runner

* feat: Added stdout response splitted by script

* feat(Dockerfile): Chaged to production env

* feat(pipeline.js): Install packages before run script
  • Loading branch information
jpcarpanezi authored Aug 15, 2023
1 parent 8bc727c commit a57f0d8
Show file tree
Hide file tree
Showing 11 changed files with 1,096 additions and 0 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/deploy-runner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Deploy Runner

on:
workflow_dispatch:
push:
branches: ["dev"]
tags: ["v*"]
paths:
- src/Runner/**
- .github/workflows/deploy-runner.yml
pull_request:
branches: ["dev"]
paths:
- src/Runner/**
- .github/workflows/deploy-runner.yml

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: "src/Runner"
strategy:
matrix:
node-version: [18.x]

steps:
- uses: actions/checkout@v2

- name: Docker Login
if: github.event_name != 'pull_request'
uses: docker/login-action@v1.10.0
with:
username: ${{ secrets.REGISTRY_LOGIN }}
password: ${{ secrets.REGISTRY_PASSWORD }}

- name: Set image tags
if: github.event_name != 'pull_request'
id: tag
run: |
if [ "${{ startsWith(github.ref, 'refs/tags/') }}" = true ]; then
echo "::set-output name=tag::${GITHUB_REF#refs/tags/}"
else
echo "::set-output name=tag::dev"
fi
- name: Build and push Docker images (DEV)
if: github.event_name != 'pull_request' && startsWith(github.ref, 'refs/heads/dev')
uses: docker/build-push-action@v2.7.0
with:
push: ${{ github.event_name != 'pull_request' }}
tags: jpcarpanezi/houston-runner:dev
file: src/Runner/Dockerfile
context: ./src/Runner

- name: Build and push Docker images (TAGS)
if: github.event_name != 'pull_request' && startsWith(github.ref, 'refs/tags/')
uses: docker/build-push-action@v2.7.0
with:
push: ${{ github.event_name != 'pull_request' }}
tags: |
jpcarpanezi/houston-runner:${{ steps.tag.outputs.tag }}
jpcarpanezi/houston-runner:latest
file: src/Runner/Dockerfile
context: ./src/Runner
34 changes: 34 additions & 0 deletions src/Runner/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Logs
logs
*.log
npm-debug.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

server/*.spec.js
kubernetes

# Test scripts
app/scripts/
16 changes: 16 additions & 0 deletions src/Runner/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Editor configuration, see https://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = tab
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true

[*.ts]
quote_type = double

[*.md]
max_line_length = off
trim_trailing_whitespace = false
133 changes: 133 additions & 0 deletions src/Runner/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# Test scripts
app/scripts/
14 changes: 14 additions & 0 deletions src/Runner/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:18.15.0

WORKDIR /usr/src/houston

COPY package*.json ./

RUN npm ci --omit=dev

COPY . .

RUN mkdir -p /usr/src/houston/app/scripts

EXPOSE 50051
CMD [ "node", "app/index.js" ]
29 changes: 29 additions & 0 deletions src/Runner/app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import grpc from "@grpc/grpc-js";
import protoLoader from "@grpc/proto-loader";
import Pipeline from "./services/pipeline.js";
import "dotenv/config";

var PROTO_PATH = "app/protos/server.proto";

var packageDefinition = protoLoader.loadSync(
PROTO_PATH,
{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
}
);
var proto_server = grpc.loadPackageDefinition(packageDefinition).houston;

function main() {
var server = new grpc.Server();
var pipeline = new Pipeline();
server.addService(proto_server.Houston.service, { runPipeline: pipeline.runPipeline });
server.bindAsync(`0.0.0.0:${process.env.SERVER_PORT}`, grpc.ServerCredentials.createInsecure(), () => {
server.start();
});
}

main();
23 changes: 23 additions & 0 deletions src/Runner/app/protos/server.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
syntax = "proto3";

package houston;

service Houston {
rpc RunPipeline (RunPipelineRequest) returns (RunPipelineResponse) { }
}

message RunPipelineRequest {
repeated string scripts = 1;
}

message RunPipelineResponse {
int32 exitCode = 1;
repeated RunPipelineInstruction instructions = 2;
}

message RunPipelineInstruction {
string script = 1;
bool hasError = 2;
optional string stdout = 3;
optional string stderr = 4;
}
42 changes: 42 additions & 0 deletions src/Runner/app/services/pipeline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { execSync } from "child_process";

export default class Pipeline {
constructor() { }

runPipeline(call, callback) {
let response = {
exitCode: 0,
instructions: []
};

for (const script of call.request.scripts) {
let instruction = {
script,
hasError: false,
stdout: null,
stderr: null
};

try {
const options = { encoding: "utf-8", stdio: "pipe", cwd: `app/scripts/${script}/` };
execSync("npm install", options);
var stdout = execSync(`node ${script}.js`, options);

instruction.stdout = stdout;
} catch (error) {
instruction.hasError = true;
instruction.stderr = error.stderr ? error.stderr.toString() : null;
instruction.stdout = error.stdout ? error.stdout.toString() : null;
response.exitCode = error.status || 1;
}

response.instructions.push(instruction);

if (response.exitCode !== 0) {
break;
}
}

callback(null, response);
}
}
8 changes: 8 additions & 0 deletions src/Runner/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"watch": [
"app"
],
"ext": ".js",
"ignore": [],
"exec": "node ./app/index.js"
}
Loading

0 comments on commit a57f0d8

Please sign in to comment.