Skip to content

Commit

Permalink
build: add script to download Saturn L2 node
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <saturn@bajtos.net>
  • Loading branch information
bajtos committed Jun 8, 2022
1 parent 8837c8d commit 89f3e91
Show file tree
Hide file tree
Showing 9 changed files with 624 additions and 41 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ jobs:

- name: Build
run: npm run build
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Test
run: npm run test
Expand Down Expand Up @@ -97,6 +99,8 @@ jobs:

- name: Build
run: npm run build
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Get tag
id: tag
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ dev-app-update.yml
*.njsproj
*.sln
*.sw?

build/saturn
110 changes: 110 additions & 0 deletions build/download-saturn-l2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/usr/bin/env -S node

'use strict'

const SATURN_DIST_TAG = 'v0.0.2'

const { mkdir } = require('node:fs/promises')
const path = require('node:path')
const tar = require('tar-fs')
const gunzip = require('gunzip-maybe')
const { request } = require('undici')
const { pipeline } = require('node:stream/promises')

const githubToken = process.env.GITHUB_TOKEN
const authorization = githubToken ? `Bearer ${githubToken}` : undefined

main().catch(err => {
console.error('Unhandled error:', err)
process.exit(1)
})

async function main () {
console.log('Fetching release metadata for %s', SATURN_DIST_TAG)
const { assets } = await fetchReleaseMetadata()

const outDir = path.resolve(__dirname, 'saturn')
await mkdir(outDir, { recursive: true })

await Promise.all(
assets
.map(async ({ name, browser_download_url: url }) => {
const match = name.match(/^saturn-l2_\d+\.\d+\.\d+_([A-Za-z0-9]+)_([A-Za-z0-9_]+)\.tar\.gz$/)
const platform = match && getPlatform(match[1])
if (!match || platform !== process.platform) {
console.log(' ⨯ skipping %s', name)
return
}

const outName = `l2node-${platform}-${getArch(match[2])}`
console.log(' ⇣ downloading %s', outName)
const res = await request(url, {
headers: { authorization },
maxRedirections: 5
})

if (res.statusCode >= 300) {
throw new Error(
`Cannot fetch saturn-l2 binary ${name}: ${res.statusCode}\n${await res.body.text()}`
)
}

const outFile = path.join(outDir, outName)
await pipeline(res.body, gunzip(), tar.extract(outFile))
console.log(' ✓ %s', outFile)
})
)

console.log('✨ DONE ✨')
}

/**
* @returns {Promise<{
* assets: {name:string, browser_download_url: string}[];
* }>}
*/
async function fetchReleaseMetadata () {
const res = await request(
`https://api.github.com/repos/filecoin-project/saturn-l2/releases/tags/${SATURN_DIST_TAG}`,
{
headers: {
accept: 'application/vnd.github.v3+json',
'user-agent': 'undici',
authorization
}
}
)
if (res.statusCode >= 300) {
throw new Error(`Cannot fetch saturn-l2 release ${SATURN_DIST_TAG}: ${res.statusCode}\n${await res.body.text()}`)
}
const data = await res.body.json()
return data
}

/**
* @param {string} golangOs
*/
function getPlatform (golangOs) {
switch (golangOs) {
case 'Windows': return 'win32'
case 'Linux': return 'linux'
case 'Darwin': return 'darwin'
}
throw new Error(`Unkown OS string: ${golangOs}`)
}

/**
* @param {string} golangArch
*/
function getArch (golangArch) {
switch (golangArch) {
case 'x86_64':
return 'x64'
case 'i386':
return 'ia32'
case 'arm64':
return 'arm64'
}

throw new Error(`Unkown ARCH string: ${golangArch}`)
}
5 changes: 5 additions & 0 deletions electron-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ files:
- node_modules/**/*
- package.json

extraResources:
- from: 'build/saturn/l2node-${platform}-${arch}'
to: 'saturn-l2-node'
filter: ['*']

asarUnpack: 'main/**/scripts/**/*'

afterSign: 'build/notarize-macos.js'
Expand Down
3 changes: 3 additions & 0 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require('node:path')
const setupUI = require('./ui')
const setupTray = require('./tray')
const setupUpdater = require('./updater')
const setupSaturnNode = require('./saturn-node')

const inTest = (process.env.NODE_ENV === 'test')

Expand Down Expand Up @@ -49,6 +50,8 @@ async function run () {
await setupTray(ctx)
await setupUI(ctx)
await setupUpdater(ctx)

await setupSaturnNode(ctx)
} catch (e) {
handleError(e)
}
Expand Down
24 changes: 24 additions & 0 deletions main/saturn-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

const { app } = require('electron')
const path = require('path')
const fs = require('node:fs/promises')

module.exports = async function setupSaturnNode (/** @type {import('./typings').Context} */ ctx) {
const saturnBinaryPath = getSaturnBinaryPath()
console.log('Using Saturn L2 Node binary: %s', saturnBinaryPath)

const stat = await fs.stat(saturnBinaryPath)
if (!stat) {
throw new Error(`Invalid configuration or deployment. Saturn L2 Node was not found: ${saturnBinaryPath}`)
}

console.log('todo: start saturn node')
}

function getSaturnBinaryPath () {
const name = 'saturn-l2' + (process.platform === 'win32' ? '.exe' : '')
return app.isPackaged
? path.resolve(process.resourcesPath, 'saturn-l2-node', name)
: path.resolve(__dirname, '..', 'build', 'saturn', `l2node-${process.platform}-${process.arch}`, name)
}
Loading

0 comments on commit 89f3e91

Please sign in to comment.