@@ -2,6 +2,8 @@ const { Binary } = require('binary-install-raw')
2
2
const os = require ( 'os' )
3
3
const chalk = require ( 'chalk' )
4
4
const fetch = require ( 'node-fetch' )
5
+ const { exec } = require ( 'child_process' )
6
+ const fs = require ( 'fs' )
5
7
const semver = require ( 'semver' )
6
8
7
9
const HELP = `
@@ -13,6 +15,7 @@ ${chalk.dim('Options:')}
13
15
-h, --help Show usage information
14
16
-l, --logs Logs to the console information about the OS, CPU model and download url (debugging purposes)
15
17
-v, --version <tag> Choose the version of the rust binary that you want to be downloaded/used
18
+ -d, --docker Launches Matchstick in a docker container
16
19
`
17
20
18
21
module . exports = {
@@ -22,7 +25,7 @@ module.exports = {
22
25
let { print } = toolbox
23
26
24
27
// Read CLI parameters
25
- let { f, force, h, help, l, logs, v, version } = toolbox . parameters . options
28
+ let { f, force, h, help, l, logs, v, version, d , docker } = toolbox . parameters . options
26
29
let datasource = toolbox . parameters . first
27
30
28
31
// Support both long and short option variants
@@ -37,6 +40,101 @@ module.exports = {
37
40
return
38
41
}
39
42
43
+ if ( docker ) {
44
+ let url = `https://github.com/LimeChain/matchstick/releases/download/${ version || '0.2.0a' } /binary-linux-20`
45
+
46
+ if ( version ) {
47
+ await fetch ( url )
48
+ . then ( response => {
49
+ if ( response . status === 404 ) {
50
+ print . info ( `Error: Invalid Matchstick version '${ version } '` )
51
+ process . exit ( 1 )
52
+ }
53
+ } )
54
+ }
55
+
56
+ // TODO: Move these in separate file (in a function maybe)
57
+ let contents = `FROM ubuntu:20.04
58
+ ENV ARGS=""
59
+ RUN apt update
60
+ RUN apt install -y nodejs
61
+ RUN apt install -y npm
62
+ RUN apt install -y cmake
63
+ RUN apt install -y git
64
+ COPY ./ ./
65
+ RUN mkdir ./tests/.tools
66
+ WORKDIR ./tests/.tools
67
+ RUN git clone --recursive https://github.com/WebAssembly/wabt
68
+ WORKDIR wabt
69
+ RUN git submodule update --init
70
+ RUN mkdir build
71
+ WORKDIR build
72
+ RUN cmake ..
73
+ RUN cmake --build .
74
+ WORKDIR /
75
+ RUN npm run codegen
76
+ RUN npm run build
77
+ RUN apt install -y postgresql
78
+ RUN apt install -y curl
79
+ RUN curl -OL ${ url }
80
+ RUN mv binary-linux-20 matchstick
81
+ CMD ./matchstick \${ARGS}`
82
+
83
+ let dir = 'tests/.docker'
84
+
85
+ if ( ! fs . existsSync ( dir ) ) {
86
+ fs . mkdirSync ( dir , { recursive : true } )
87
+ }
88
+
89
+ fs . writeFileSync ( 'tests/.docker/Dockerfile' , contents , ( err ) => {
90
+ if ( err ) {
91
+ print . info ( 'A problem occurred while generating the Dockerfile. Please attend to the errors below.' )
92
+ print . info ( err )
93
+ } else {
94
+ print . info ( 'Successfully generated Dockerfile.' )
95
+ }
96
+ } )
97
+
98
+ exec ( `docker build -f tests/.docker/Dockerfile -t matchstick . ` , ( error , stdout , stderr ) => {
99
+ print . info ( 'Building Matchstick image...' )
100
+
101
+ if ( error ) {
102
+ print . info ( 'A problem occurred while trying to build the Matchstick Docker image. Please attend to the errors below.' )
103
+ print . info ( `error: ${ error . message } ` )
104
+ }
105
+ if ( stderr ) {
106
+ print . info ( 'A problem occurred while trying to build the Matchstick Docker image. Please attend to the errors below.' )
107
+ print . info ( `stderr: ${ stderr } ` )
108
+ }
109
+
110
+ let runCommand = `docker run -e ARGS="${ datasource || '' } ${ coverage ? '-c' : '' } " --rm matchstick`
111
+
112
+ console . log ( 'runCommand output' )
113
+ console . log ( runCommand )
114
+ print . info ( 'runCommand output' )
115
+ print . info ( runCommand )
116
+
117
+ exec ( runCommand , ( error , stdout , stderr ) => {
118
+ print . info ( 'Running Matchstick image...' )
119
+
120
+ if ( error ) {
121
+ print . info ( 'A problem occurred while trying to run the Matchstick Docker image. Please attend to the errors below.' )
122
+ print . info ( `error: ${ error . message } ` )
123
+ process . exit ( 1 )
124
+ }
125
+ if ( stderr ) {
126
+ print . info ( 'A problem occurred while trying to run the Matchstick Docker image. Please attend to the errors below.' )
127
+ print . info ( `stderr: ${ stderr } ` )
128
+ process . exit ( 1 )
129
+ }
130
+ print . info ( stdout )
131
+ process . exit ( )
132
+ } )
133
+ } )
134
+
135
+ return
136
+ }
137
+
40
138
const platform = getPlatform ( logs )
41
139
if ( ! version ) {
42
140
let result = await fetch ( 'https://api.github.com/repos/LimeChain/matchstick/releases/latest' )
@@ -53,7 +151,7 @@ module.exports = {
53
151
let binary = new Binary ( platform , url , version )
54
152
await binary . install ( force )
55
153
datasource ? binary . run ( datasource ) : binary . run ( )
56
- }
154
+ } ,
57
155
}
58
156
59
157
function getPlatform ( logs ) {
@@ -62,7 +160,7 @@ function getPlatform(logs) {
62
160
const release = os . release ( )
63
161
const cpuCore = os . cpus ( ) [ 0 ]
64
162
const majorVersion = semver . major ( release )
65
- const isM1 = cpuCore . model . includes ( " Apple M1" )
163
+ const isM1 = cpuCore . model . includes ( ' Apple M1' )
66
164
67
165
if ( logs ) {
68
166
console . log ( `OS type: ${ type } \nOS arch: ${ arch } \nOS release: ${ release } \nOS major version: ${ majorVersion } \nCPU model: ${ cpuCore . model } ` )
0 commit comments