Skip to content

Commit

Permalink
progress...
Browse files Browse the repository at this point in the history
  • Loading branch information
ya7ya committed Jan 14, 2019
1 parent 333620a commit cca1fc6
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 147 deletions.
1 change: 1 addition & 0 deletions config.toml
Expand Up @@ -5,6 +5,7 @@ title = "LP test harness"
local = true
# change livepeerBinaryPath to point to where the lp binary is, REQUIRED
livepeerBinaryPath = "./containers/lpnode/livepeer_linux/livepeer"
name = "testharness"

[blockchain]
name = "lpTestNet"
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -23,6 +23,7 @@
"ethereumjs-abi": "^0.6.5",
"ethereumjs-util": "^6.0.0",
"ethers": "^4.0.20",
"shortid": "^2.2.14",
"toml": "^2.3.3",
"yaml": "^1.1.0"
}
Expand Down
2 changes: 1 addition & 1 deletion postinstall
@@ -1,4 +1,4 @@
#!/bin/bash

docker pull darkdragon/geth-with-livepeer-protocol:latest
npm run build
# npm run build
4 changes: 2 additions & 2 deletions src/cli/lpth-build.js
Expand Up @@ -35,11 +35,11 @@ if (!program.output || program.output === '') {
program.output = '.'
}

const nc = new NetworkCreator(parsePath(configFile))
const nc = new NetworkCreator(parsePath(configFile), true) // true for toml
nc.generateComposeFile(program.output, (err) => {
if (err) throw err
console.log('all good...building LPNODE image')
nc.loadBinaries((err, stdout) =>{
nc.loadBinaries('./containers/lpnode/binaries', (err, stdout) =>{
if (err) throw err
nc.buildLpImage((err, stdout) =>{
if (err) throw err
Expand Down
214 changes: 90 additions & 124 deletions src/index.js
@@ -1,135 +1,101 @@
'use strict'

const fs = require('fs')
const toml = require('toml')
const composefile = require('composefile')
let usedPorts = []


const DEFAULT_CONFIG_PATH = './config.toml'

// fs.readFile('./config.toml', (err, config) => {
// if (err) throw err
//
// let parsed = toml.parse(config)
// console.dir(parsed)
// })


function generateDockerCompose (configPath, cb) {
const defaults = {
version: 3,
outputFolder: __dirname,
filename: 'docker-compose.yml',
services: {},
network_mode: 'host',
// networks: {
// outside: {
// external: true,
// }
// }
}

let config, configStr
if (!config) {
configStr = fs.readFileSync(DEFAULT_CONFIG_PATH)
}

try {
config = toml.parse(configStr)
} catch (e) {
console.error("Parsing error on line " + e.line + ", column " + e.column +
": " + e.message)
}
const NetworkCreator = require('./networkcreator')
const Streamer = require('./streamer')
const Swarm = require('./swarm')
const utils = require('./utils/helpers')

console.dir(config)
defaults.services = generateDockerService(config)
console.log('defaults: ', defaults)
const DIST_DIR = './dist'

composefile(defaults,cb)
}

function generateDockerService (config) {
let output = {}
if (config.blockchain && config.blockchain.controllerAddress === "") {
// output.geth = generateGethService()
class TestHarness {
constructor () {
this.swarm = new Swarm()
}

for (let i = 0; i < config.nodes.transcoders.instances; i++) {
// generate separate services with the forwarded ports.
// append it to output as output.<node_generate_id> = props
output['lp_t_' + i] = {
image: 'lpnode:latest',
ports: [
`${getRandomPort(8935)}:8935`,
`${getRandomPort(7935)}:7935`,
`${getRandomPort(1935)}:1935`,
],
// TODO fix the serviceAddr issue
command: '-transcoder -rinkeby -datadir /lpData --rtmpAddr 0.0.0.0:1935 --cliAddr 0.0.0.0:7935 --httpAddr 0.0.0.0:8935',
// networks: [ 'outside']
}
}

for (let i = 0; i < config.nodes.orchestrators.instances; i++) {
// generate separate services with the forwarded ports.
// append it to output as output.<node_generate_id> = props
output['lp_o_' + i] = {
image: 'lpnode:latest',
ports: [
`${getRandomPort(8935)}:8935`,
`${getRandomPort(7935)}:7935`,
`${getRandomPort(1935)}:1935`,
],
command: '-rinkeby -datadir /lpData --rtmpAddr 0.0.0.0:1935 --cliAddr 0.0.0.0:7935 --httpAddr 0.0.0.0:8935',
// networks: [ 'outside']
}
}

for (let i = 0; i < config.nodes.broadcasters.instances; i++) {
// generate separate services with the forwarded ports.
// append it to output as output.<node_generate_id> = props
output['lp_b_' + i] = {
image: 'lpnode:latest',
ports: [
`${getRandomPort(8935)}:8935`,
`${getRandomPort(7935)}:7935`,
`${getRandomPort(1935)}:1935`,
],
command: '-rinkeby -datadir /lpData --rtmpAddr 0.0.0.0:1935 --cliAddr 0.0.0.0:7935 --httpAddr 0.0.0.0:8935',
// networks: [ 'outside']
}
}

return output
}

function generateGethService () {
return {
image: 'geth-dev:latest',
ports: [
'8545:8545'
],
// networks: ['outside']
run (config, cb) {
// 1. [ ] validate the configurations
// 2. [x] provision GCP machines
// 3. scp docker-compose.yml, livepeer binary and git test-harness
// 4. create throwaway docker registry
// 5. initiate swarm, add workers
// 6. build lpnode with lp binary, push it to registry
// 7. deploy geth-with-protocol, fund accounts.
// 8. deploy lpnodes
// 9. setup transcoders/orchestrators
// 10. setup broadcasters.
// 11. initializeRound.
// 12. start streams.
// 13. pipe logs to bucket or download them.
// 14. teardown the cluster.
// 15. callback.
config.name = config.name || 'testharness'
this.swarm._managerName = `${config.name}-manager`

this.networkCreator = new NetworkCreator(config)
this.networkCreator.generateComposeFile(`${DIST_DIR}/${config.name}`, (err) => {
if (err) return handleError(err)

if (config.local) {

} else {
this.networkCreator.loadBinaries(`${DIST_DIR}/${config.name}`, (err) => {
if (err) throw err
})

// provision GCP machines
this.swarm.createMachines({
machines: 2,
name: config.name || 'testharness'
}, (err) => {
if (err) throw err

// machines are ready.
this.swarm.scp(
`${DIST_DIR}/${config.name}`,
`${config.name}-manager:/tmp/config`,
`-r`,
(err, stdout) => {
if (err) throw err
// dist folder should be available to the manager now.

// init Swarm
this.swarm.init(`${config.name}-manager`, (err, stdout) => {
if (err) throw err

console.log('swarm initiated. ', stdout)

// create network
this.swarm.createNetwork('testnet', (err, stdout) => {
if (err) throw err
console.log('networkid: ', stdout)

// create throwaway registry
this.swarm.createRegistry((err, stdout) => {
if (err) throw err
console.log('registry stdout: ', stdout)
// now we should be able to build the image on manager and
// push it to the registry

// git clone test-harness. remotely.
// then build it.


})
})

})
}
)
cb()
})
}
})
}
}

function getRandomPort(origin) {
// TODO, ugh, fix this terrible recursive logic, use an incrementer like a gentleman
let port = origin + Math.floor(Math.random() * 999)
if (usedPorts.indexOf(port) === -1) {
usedPorts.push(port)
return port
} else {
return getRandomPort(origin)
}
}

function generateStreamSimulatorService () {

function handleError (err) {
// TODO handle errors gracefully
throw err
}

generateDockerCompose({}, (err) => {
if (err) throw err
console.log('done')
})
module.exports = TestHarness
27 changes: 15 additions & 12 deletions src/networkcreator.js
Expand Up @@ -6,17 +6,20 @@ const path = require('path')
const toml = require('toml')
const composefile = require('composefile')
const ethers = require('ethers')
const { timesLimit, each } = require('async')
const { timesLimit, each, eachLimit } = require('async')
const log = require('debug')('livepeer:test-harness:network')

class NetworkCreator extends EventEmitter {
constructor (config) {
constructor (config, isToml) {
super()

try {
this.config = toml.parse(config)
} catch (e) {
throw e
if (isToml) {
try {
this.config = toml.parse(config)
} catch (e) {
throw e
}
} else {
this.config = config
}

this.ports = {}
Expand All @@ -31,10 +34,10 @@ class NetworkCreator extends EventEmitter {
return true
}

loadBinaries (cb) {
loadBinaries (dist, cb) {
// copy livepeer binaries to lpnode image folder
console.log(`copying LP binary from ${this.config.livepeerBinaryPath}`)
exec(`cp ${this.config.livepeerBinaryPath} ./containers/lpnode/binaries`,
exec(`cp ${this.config.livepeerBinaryPath} ${dist}`,
(err, stdout, stderr) => {
if (err) throw err
console.log('stdout: ', stdout)
Expand All @@ -61,8 +64,8 @@ class NetworkCreator extends EventEmitter {
})

builder.on('close', (code) => {
console.log(`${output} child process exited with code ${code}`)
cb(null, stdout)
console.log(`child process exited with code ${code}`)
cb(null)
})
//
// exec(`docker build -t lpnode:latest ./containers/lpnode/`, (err, stdout, stderr) => {
Expand Down Expand Up @@ -138,7 +141,7 @@ class NetworkCreator extends EventEmitter {
this.hasGeth = true
}

each(['transcoder', 'orchestrator', 'broadcaster'], (type, callback) => {
eachLimit(['transcoder', 'orchestrator', 'broadcaster'], 1, (type, callback) => {
console.log(`generating ${type} nodes ${this.config.nodes[`${type}s`].instances}`)
timesLimit(
this.config.nodes[`${type}s`].instances,
Expand Down

0 comments on commit cca1fc6

Please sign in to comment.