Sfoglia il codice sorgente

storage-node: use debug instead of console.log

Mokhtar Naamani 4 anni fa
parent
commit
14562d4faf

+ 16 - 27
storage-node/packages/cli/bin/cli.js

@@ -19,24 +19,13 @@
 
 'use strict'
 
-const path = require('path')
 const fs = require('fs')
 const assert = require('assert')
-
 const { RuntimeApi } = require('@joystream/runtime-api')
-
 const meow = require('meow')
 const chalk = require('chalk')
 const _ = require('lodash')
-
-const debug = require('debug')('joystream:cli')
-
-// Project root
-const project_root = path.resolve(__dirname, '..')
-
-// Configuration (default)
-const pkg = require(path.resolve(project_root, 'package.json'))
-
+const debug = require('debug')('joystream:storage-cli')
 const dev = require('./dev')
 
 // Parse CLI
@@ -46,7 +35,7 @@ const FLAG_DEFINITIONS = {
 
 const cli = meow(`
   Usage:
-    $ storage-cli command [arguments..] [key_file]
+    $ storage-cli command [arguments..] [key_file] [passphrase]
 
   Some commands require a key file as the last option holding the identity for
   interacting with the runtime API.
@@ -63,7 +52,7 @@ const cli = meow(`
 
   Dev Commands:       Commands to run on a development chain.
     dev-init          Setup chain with Alice as lead and storage provider.
-    dev-check         Check the chain is setup with Alice as lead storage provider.
+    dev-check         Check the chain is setup with Alice as lead and storage provider.
   `,
   { flags: FLAG_DEFINITIONS })
 
@@ -77,7 +66,7 @@ function load_identity (api, filename, passphrase) {
     assert_file('keyfile', filename)
     api.identities.loadUnlock(filename, passphrase)
   } else {
-    console.log('Loading Alice as identity')
+    debug('Loading Alice as identity')
     api.identities.useKeyPair(dev.aliceKeyPair(api))
   }
 }
@@ -100,13 +89,13 @@ const commands = {
     assert_file('file', filename)
 
     const size = fs.statSync(filename).size
-    console.log(`File "${filename}" is ` + chalk.green(size) + ' Bytes.')
+    debug(`File "${filename}" is ${chalk.green(size)} Bytes.`)
 
     if (!do_type_id) {
       do_type_id = 1
     }
 
-    console.log('Data Object Type ID is: ' + chalk.green(do_type_id))
+    debug('Data Object Type ID is: ' + chalk.green(do_type_id))
 
     // Generate content ID
     // FIXME this require path is like this because of
@@ -114,17 +103,17 @@ const commands = {
     const { ContentId } = require('@joystream/types/lib/media')
     var cid = ContentId.generate()
     cid = cid.encode().toString()
-    console.log('Generated content ID: ' + chalk.green(cid))
+    debug('Generated content ID: ' + chalk.green(cid))
 
     // Create Data Object
     const data_object = await api.assets.createDataObject(
       api.identities.key.address, cid, do_type_id, size)
-    console.log('Data object created.')
+    debug('Data object created.')
 
     // TODO in future, optionally contact liaison here?
     const request = require('request')
     url = `${url}asset/v0/${cid}`
-    console.log('Uploading to URL', chalk.green(url))
+    debug('Uploading to URL', chalk.green(url))
 
     const f = fs.createReadStream(filename)
     const opts = {
@@ -146,7 +135,7 @@ const commands = {
           reject(new Error(`${response.statusCode}: ${body.message || 'unknown reason'}`))
           return
         }
-        console.log('Upload successful:', body.message)
+        debug('Upload successful:', body.message)
         resolve()
       })
       f.pipe(r)
@@ -156,7 +145,7 @@ const commands = {
   'download': async (api, url, content_id, filename) => {
     const request = require('request')
     url = `${url}asset/v0/${content_id}`
-    console.log('Downloading URL', chalk.green(url), 'to', chalk.green(filename))
+    debug('Downloading URL', chalk.green(url), 'to', chalk.green(filename))
 
     const f = fs.createWriteStream(filename)
     const opts = {
@@ -170,7 +159,7 @@ const commands = {
           return
         }
 
-        console.log('Downloading', chalk.green(response.headers['content-type']), 'of size', chalk.green(response.headers['content-length']), '...')
+        debug('Downloading', chalk.green(response.headers['content-type']), 'of size', chalk.green(response.headers['content-length']), '...')
 
         f.on('error', (err) => {
           reject(err)
@@ -181,7 +170,7 @@ const commands = {
             reject(new Error(`${response.statusCode}: ${body.message || 'unknown reason'}`))
             return
           }
-          console.log('Download completed.')
+          debug('Download completed.')
           resolve()
         })
       })
@@ -192,7 +181,7 @@ const commands = {
   'head': async (api, url, content_id) => {
     const request = require('request')
     url = `${url}asset/v0/${content_id}`
-    console.log('Checking URL', chalk.green(url), '...')
+    debug('Checking URL', chalk.green(url), '...')
 
     const opts = {
       url: url,
@@ -211,7 +200,7 @@ const commands = {
         }
 
         for (var propname in response.headers) {
-          console.log(`  ${chalk.yellow(propname)}: ${response.headers[propname]}`)
+          debug(`  ${chalk.yellow(propname)}: ${response.headers[propname]}`)
         }
 
         resolve()
@@ -240,7 +229,7 @@ async function main () {
 
 main()
   .then(() => {
-    console.log('Process exiting gracefully.')
+    debug('Process exiting gracefully.')
     process.exit(0)
   })
   .catch((err) => {

+ 32 - 15
storage-node/packages/cli/bin/dev.js

@@ -1,4 +1,4 @@
-const assert = require('assert')
+const debug = require('debug')('joystream:storage-cli:dev')
 
 function aliceKeyPair (api) {
   return api.identities.keyring.addFromUri('//Alice', null, 'sr25519')
@@ -12,15 +12,17 @@ function roleKeyPair (api) {
 // just launched as the storage lead, and a storage provider using the same
 // key as the role key
 const init = async (api) => {
+  try {
+    return await check(api)
+  } catch (err) {
+    // setup is not correct we can try to run setup
+  }
+
   const alice = aliceKeyPair(api).address
   const roleAccount = roleKeyPair(api).address
   const providerId = 0 // first assignable id
 
-  // Check if setup already completed
-  if (await api.workers.isRoleAccountOfStorageProvider(providerId, roleAccount)) {
-    console.log('//Colossus already setup as a storage provider')
-    return
-  }
+  console.log(`Checking for dev chain...`)
 
   // make sure alice is sudo - indirectly checking this is a dev chain
   const sudo = await api.api.query.sudo.key()
@@ -29,17 +31,24 @@ const init = async (api) => {
     throw new Error('Setup requires Alice to be sudo. Are you sure you are running a devchain?')
   }
 
+  console.log('Setting up chain...')
+
+  debug('Transfering tokens to storage role account')
   // Give role account some tokens to work with
   api.balances.transfer(alice, roleAccount, 100000)
 
+  debug('Registering Alice as Member')
   // register alice as a member
-  console.log(`Registering Alice as a member`)
   const aliceMemberId = await api.identities.registerMember(alice, {
     handle: 'alice'
   })
 
+  // if (!aliceMemberId.eq(0)) {
+  //   // not first time script is running!
+  // }
+
   // Make alice the storage lead
-  console.log('Setting Alice as Lead')
+  debug('Setting Alice as Lead')
   // prepare set storage lead tx
   const setLeadTx = api.api.tx.storageWorkingGroup.setLead(aliceMemberId, alice)
   // make sudo call
@@ -53,7 +62,7 @@ const init = async (api) => {
   // first assignable id == 0
   // so we don't await each tx to finalize to get the ids. this allows us to
   // batch all the transactions into a single block.
-  console.log('Making //Colossus account a storage provider')
+  debug('Making //Colossus account a storage provider')
   const openTx = api.api.tx.storageWorkingGroup.addWorkerOpening('CurrentBlock', {
     application_rationing_policy: {
       'max_active_applicants': 1
@@ -77,13 +86,13 @@ const init = async (api) => {
 
   // wait for previous transactions to finalize so we can read correct state
   if (await api.workers.isRoleAccountOfStorageProvider(providerId, roleAccount)) {
-    console.log('Setup //Colossus successfully as storage provider')
+    console.log('Storage Role setup Completed Successfully')
   } else { throw new Error('Setup Failed') }
 
   // set localhost colossus as discovery provider on default port
   // assuming pioneer dev server is running on port 3000 we should run
   // the storage dev server on port 3001
-  console.log('Setting Local development node as bootstrap endpoint')
+  debug('Setting Local development node as bootstrap endpoint')
   await api.discovery.setBootstrapEndpoints(alice, ['http://localhost:3001/'])
 }
 
@@ -93,14 +102,22 @@ const check = async (api) => {
   const alice = aliceKeyPair(api).address
 
   if (await api.workers.isRoleAccountOfStorageProvider(providerId, roleAccountId)) {
-    console.log('//Colossus is correctly setup as a storage provider with providerId = 0')
-  } else { throw new Error('//Colossus is not setup as a storage provider') }
+    console.log(`
+      Chain is setup with Alice as a storage provider:
+      providerId = ${providerId}
+      roleAccount = "//Colossus"
+    `)
+  } else { throw new Error('Alice is not a storage provider') }
 
   const currentLead = await api.api.query.storageWorkingGroup.currentLead()
 
   if (currentLead.isSome && currentLead.unwrap().role_account_id.eq(alice)) {
-    console.log('Alice is correctly setup as the storage lead')
-  } else { throw new Error('Alice is not the storage lead') }
+    console.log(`
+      Alice is correctly setup as the storage lead
+    `)
+  } else {
+    throw new Error('Alice is not the storage lead!')
+  }
 }
 
 module.exports = {

+ 1 - 1
storage-node/packages/colossus/bin/cli.js

@@ -10,7 +10,7 @@ const chalk = require('chalk')
 const figlet = require('figlet')
 const _ = require('lodash')
 
-const debug = require('debug')('joystream:cli')
+const debug = require('debug')('joystream:colossus')
 
 // Project root
 const PROJECT_ROOT = path.resolve(__dirname, '..')

+ 1 - 1
storage-node/packages/colossus/paths/asset/v0/{id}.js

@@ -23,7 +23,7 @@ const path = require('path');
 const file_type = require('file-type');
 const mime_types = require('mime-types');
 
-const debug = require('debug')('joystream:api:asset');
+const debug = require('debug')('joystream:colossus:api:asset');
 
 const util_ranges = require('@joystream/util/ranges');
 const filter = require('@joystream/storage/filter');

+ 1 - 1
storage-node/packages/colossus/paths/discover/v0/{id}.js

@@ -1,5 +1,5 @@
 const { discover } = require('@joystream/discovery')
-const debug = require('debug')('joystream:api:discovery');
+const debug = require('debug')('joystream:colossus:api:discovery');
 
 const MAX_CACHE_AGE = 30 * 60 * 1000;
 const USE_CACHE = true;

+ 2 - 1
storage-node/scripts/run-dev-instance.sh

@@ -13,7 +13,8 @@ docker-compose -f ${script_path}/compose/devchain-and-ipfs-node/docker-compose.y
 docker-compose -f ${script_path}/compose/devchain-and-ipfs-node/docker-compose.yaml up -d
 
 # configure the dev chain
-yarn storage-cli dev-init && yarn storage-cli dev-check
+DEBUG=joystream:storage-cli:dev yarn storage-cli dev-init
+DEBUG=joystream:storage-cli:dev yarn storage-cli dev-check
 
 # Run the server
 DEBUG=* yarn colossus dev-server