Skip to content

Make node its own package and make the docker image a base image #3

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 3 commits into from
Mar 31, 2020
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ src/data.json
src/config.json
src/output.json
test.sh
sdk
3 changes: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
FROM node:8.15.0
COPY /src /opt/executor
WORKDIR /opt/executor
RUN if [ ! -d "sdk-node" ]; then git clone --depth 1 https://github.com/ProcessMaker/sdk-node.git; fi
RUN cd /opt/executor/sdk-node; npm install
RUN cd /opt/executor/sdk-node; npm run-script build
RUN npm install
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "processmaker/docker-executor-node",
"friendly_name": "Javascript Docker Executor",
"description": "Javascript script executor for processmaker 4",
"version": "1.0.0",
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"ProcessMaker\\Package\\DockerExecutorNode\\": "src",
"ProcessMaker\\ScriptRunners\\": "src/ScriptRunners"
}
},
"extra": {
"laravel": {
"providers": [
"ProcessMaker\\Package\\DockerExecutorNode\\DockerExecutorNodeServiceProvider"
]
}
}
}
56 changes: 56 additions & 0 deletions src/DockerExecutorNodeServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
namespace ProcessMaker\Package\DockerExecutorNode;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use ProcessMaker\Traits\PluginServiceProviderTrait;
use ProcessMaker\Models\ScriptExecutor;

class DockerExecutorNodeServiceProvider extends ServiceProvider
{
use PluginServiceProviderTrait;

const version = '1.0.0'; // Required for PluginServiceProviderTrait

public function register()
{
}

public function boot()
{
\Artisan::command('docker-executor-node:install', function () {
$scriptExecutor = ScriptExecutor::install([
'language' => 'javascript',
'title' => 'Node Executor',
'description' => 'Default Javascript/Node Executor'
]);

// Build the instance image. This is the same as if you were to build it from the admin UI
\Artisan::call('processmaker:build-script-executor ' . $scriptExecutor->id);

// Restart the workers so they know about the new supported language
\Artisan::call('horizon:terminate');
});

$config = [
'name' => 'JavaScript',
'runner' => 'NodeRunner',
'mime_type' => 'text/javascript',
'options' => ['gitRepoId' => 'sdk-node'],
'init_dockerfile' => [
'ARG SDK_DIR',
'COPY $SDK_DIR /opt/sdk-node',
'WORKDIR /opt/sdk-node',
'RUN npm install',
'RUN npm run build',
'WORKDIR /opt/executor',
'RUN npm install /opt/sdk-node',
],
'package_path' => __DIR__ . '/..',
'package_version' => self::version,
];
config(['script-runners.javascript' => $config]);

$this->completePluginBoot();
}
}
22 changes: 22 additions & 0 deletions src/ScriptRunners/NodeRunner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace ProcessMaker\ScriptRunners;

class NodeRunner extends Base
{
/**
* Configure docker with node executor
*
* @param string $code
* @param array $dockerConfig
*
* @return array
*/
public function config($code, array $dockerConfig)
{
$dockerConfig['image'] = config('script-runners.javascript.image');
$dockerConfig['command'] = '/bin/sh /opt/executor/run.sh';
$dockerConfig['inputs']['/opt/executor/script.js'] = $code;
return $dockerConfig;
}
}
20 changes: 15 additions & 5 deletions src/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
if (process.env['API_SSL_VERIFY'] === '0') {
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
}

var fs = require('fs')
var script = require('./script_wrapped.js')
var api = require('process_maker_api')
try {
var api = require('process_maker_api');
} catch(err) {
var api = null;
}

function getFilePromise(file) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -29,11 +37,13 @@ Promise.all([getConfig, getData]).then(function(values) {
const config = values[0]
const data = values[1]

let client = api.ApiClient.instance
client.basePath = process.env.API_HOST
if (api) {
let client = api.ApiClient.instance
client.basePath = process.env.API_HOST

let auth = client.authentications['pm_api_bearer']
auth.accessToken = process.env.API_TOKEN
let auth = client.authentications['pm_api_bearer']
auth.accessToken = process.env.API_TOKEN
}

const return_value = script.run(data, config, api)

Expand Down
1 change: 0 additions & 1 deletion src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"description": "",
"main": "bootstrap.js",
"dependencies": {
"process_maker_api": "./sdk-node"
},
"devDependencies": {},
"scripts": {
Expand Down