Skip to content

Commit c84e287

Browse files
authored
Merge pull request #826 from dimitrovmaksim/graph_test_docker_adjustments
graph-cli test command docker adjustments
2 parents 5d19a72 + d7b312e commit c84e287

File tree

1 file changed

+54
-51
lines changed

1 file changed

+54
-51
lines changed

src/commands/test.js

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ const { Binary } = require('binary-install-raw')
22
const os = require('os')
33
const chalk = require('chalk')
44
const fetch = require('node-fetch')
5-
const { filesystem, print } = require('gluegun')
5+
const { filesystem, patching, print } = require('gluegun')
66
const { fixParameters } = require('../command-helpers/gluegun')
7+
const path = require('path')
78
const semver = require('semver')
89
const { spawn, exec } = require('child_process')
910
const yaml = require('js-yaml')
@@ -169,27 +170,34 @@ async function runDocker(datasource, opts) {
169170
// Get current working directory
170171
let current_folder = await filesystem.cwd()
171172

172-
// Build the Dockerfile location. Defaults to ./tests/.docker if
173-
// a custom testsFolder is not declared in the subgraph.yaml
174-
let dockerDir = ""
173+
// Declate dockerfilePath with default location
174+
let dockerfilePath = "./tests/.docker/Dockerfile"
175175

176-
try {
177-
let doc = await yaml.load(filesystem.read('subgraph.yaml', 'utf8'))
178-
testsFolder = doc.testsFolder || './tests'
179-
dockerDir = testsFolder.endsWith('/') ? testsFolder + '.docker' : testsFolder + '/.docker'
180-
} catch (error) {
181-
print.error(error.message)
182-
return
176+
// Check if matchstick.yaml config exists
177+
if(filesystem.exists('matchstick.yaml')) {
178+
try {
179+
// Load the config
180+
let config = await yaml.load(filesystem.read('matchstick.yaml', 'utf8'))
181+
182+
// Check if matchstick.yaml is not empty
183+
if(config != null) {
184+
// If a custom tests folder is declared update dockerfilePath
185+
dockerfilePath = path.join(config.testsFolder || 'tests', '.docker/Dockerfile')
186+
}
187+
} catch (error) {
188+
print.info('A problem occurred while reading matchstick.yaml. Please attend to the errors below:')
189+
print.error(error.message)
190+
process.exit(1)
191+
}
183192
}
184193

185-
// Create the Dockerfile
186-
try {
187-
await filesystem.write(`${dockerDir}/Dockerfile`, dockerfile(versionOpt, latestVersion))
188-
print.info('Successfully generated Dockerfile.')
189-
} catch (error) {
190-
print.info('A problem occurred while generating the Dockerfile. Please attend to the errors below:')
191-
print.error(error.message)
192-
return
194+
// Check if the Dockerfil already exists
195+
let dockerfileExists = filesystem.exists(dockerfilePath)
196+
197+
// Generate the Dockerfile only if it doesn't exists,
198+
// version flag and/or force flag is passed.
199+
if(!dockerfileExists || versionOpt || forceOpt) {
200+
await dockerfile(dockerfilePath, versionOpt, latestVersion)
193201
}
194202

195203
// Run a command to check if matchstick image already exists
@@ -212,10 +220,10 @@ async function runDocker(datasource, opts) {
212220

213221
// If a matchstick image does not exists, the command returns an empty string,
214222
// else it'll return the image ID. Skip `docker build` if an image already exists
215-
// If `-v/--version` is specified, delete current image(if any) and rebuild.
223+
// Delete current image(if any) and rebuild.
216224
// Use spawn() and {stdio: 'inherit'} so we can see the logs in real time.
217-
if(stdout === '' || versionOpt || forceOpt) {
218-
if ((stdout !== '' && versionOpt) || forceOpt) {
225+
if(!dockerfileExists || stdout === '' || versionOpt || forceOpt) {
226+
if (stdout !== '') {
219227
exec('docker image rm matchstick', (error, stdout, stderr) => {
220228
print.info(chalk.bold(`Removing matchstick image\n${stdout}`))
221229
})
@@ -224,7 +232,7 @@ async function runDocker(datasource, opts) {
224232
// run a container from that image.
225233
spawn(
226234
'docker',
227-
['build', '--no-cache', '-f', `${dockerDir}/Dockerfile`, '-t', 'matchstick', '.'],
235+
['build', '-f', dockerfilePath, '-t', 'matchstick', '.'],
228236
{ stdio: 'inherit' }
229237
).on('close', code => {
230238
if (code === 0) {
@@ -239,37 +247,32 @@ async function runDocker(datasource, opts) {
239247
})
240248
}
241249

242-
// TODO: Move these in separate file (in a function maybe)
243-
function dockerfile(versionOpt, latestVersion) {
244-
return `
245-
FROM ubuntu:20.04
246-
ENV ARGS=""
247-
248-
# Install necessary packages
249-
RUN apt update
250-
RUN apt install -y nodejs
251-
RUN apt install -y npm
252-
RUN apt install -y git
253-
RUN apt install -y postgresql
254-
RUN apt install -y curl
255-
RUN apt install -y cmake
256-
RUN npm install -g @graphprotocol/graph-cli
250+
// Downloads Dockerfile template from the demo-subgraph repo
251+
// Replaces the placeholders with their respective values
252+
async function dockerfile(dockerfilePath, versionOpt, latestVersion) {
253+
let spinner = print.spin("Generating Dockerfile...")
257254

258-
# Download the latest linux binary
259-
RUN curl -OL https://github.com/LimeChain/matchstick/releases/download/${versionOpt || latestVersion}/binary-linux-20
260-
261-
# Make it executable
262-
RUN chmod a+x binary-linux-20
255+
try {
256+
// Fetch the Dockerfile template content from the demo-subgraph repo
257+
let content = await fetch('https://raw.githubusercontent.com/LimeChain/demo-subgraph/main/Dockerfile')
258+
.then((response) => {
259+
if (response.ok) {
260+
return response.text()
261+
} else {
262+
throw new Error(`Status Code: ${response.status}, with error: ${response.statusText}`);
263+
}
264+
})
263265

264-
# Create a matchstick dir where the host will be copied
265-
RUN mkdir matchstick
266-
WORKDIR matchstick
266+
// Write the Dockerfile
267+
await filesystem.write(dockerfilePath, content)
267268

268-
# Copy host to /matchstick
269-
COPY ../ .
269+
// Replaces the version placeholders
270+
await patching.replace(dockerfilePath, '<MATCHSTICK_VERSION>', versionOpt || latestVersion)
270271

271-
RUN graph codegen
272-
RUN graph build
272+
} catch (error) {
273+
spinner.fail(`A problem occurred while generating the Dockerfile. Please attend to the errors below:\n ${error.message}`)
274+
process.exit(1)
275+
}
273276

274-
CMD ../binary-linux-20 \${ARGS}`
277+
spinner.succeed('Successfully generated Dockerfile.')
275278
}

0 commit comments

Comments
 (0)