cic-internal-integration/apps/contract-migration/scripts/import_meta.js

118 lines
2.7 KiB
JavaScript
Raw Normal View History

2021-02-12 20:40:18 +01:00
const fs = require('fs');
const path = require('path');
const http = require('http');
2021-02-18 10:11:46 +01:00
const cic = require('cic-client-meta');
2021-02-12 20:40:18 +01:00
//const conf = JSON.parse(fs.readFileSync('./cic.conf'));
const config = new cic.Config('./config');
config.process();
console.log(config);
2021-02-18 10:11:46 +01:00
function sendit(uid, envelope) {
2021-02-12 20:40:18 +01:00
const d = envelope.toJSON();
const opts = {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Content-Length': d.length,
'X-CIC-AUTOMERGE': 'client',
},
};
let url = config.get('META_URL');
url = url.replace(new RegExp('^(.+://[^/]+)/*$'), '$1/');
2021-02-18 10:11:46 +01:00
console.debug('url: ' + url + uid);
2021-02-12 20:40:18 +01:00
const req = http.request(url + uid, opts, (res) => {
res.on('data', process.stdout.write);
res.on('end', () => {
console.log('result', res.statusCode, res.headers);
});
});
req.write(d);
req.end();
}
2021-02-18 10:11:46 +01:00
function doOne(keystore, filePath) {
const signer = new cic.PGPSigner(keystore);
const parts = path.basename(filePath).split('.');
const ethereum_address = path.basename(parts[0]);
cic.User.toKey('0x' + ethereum_address).then((uid) => {
const d = fs.readFileSync(filePath, 'utf-8');
2021-02-12 20:40:18 +01:00
const o = JSON.parse(d);
console.log(o);
2021-02-18 10:11:46 +01:00
fs.unlinkSync(filePath);
2021-02-12 20:40:18 +01:00
const s = new cic.Syncable(uid, o);
s.setSigner(signer);
s.onwrap = (env) => {
sendit(uid, env);
};
s.sign();
});
}
const privateKeyPath = path.join(config.get('PGP_EXPORTS_DIR'), config.get('PGP_PRIVATE_KEY_FILE'));
const publicKeyPath = path.join(config.get('PGP_EXPORTS_DIR'), config.get('PGP_PRIVATE_KEY_FILE'));
pk = fs.readFileSync(privateKeyPath);
pubk = fs.readFileSync(publicKeyPath);
new cic.PGPKeyStore(
config.get('PGP_PASSPHRASE'),
pk,
pubk,
undefined,
undefined,
2021-02-18 10:11:46 +01:00
importMeta,
2021-02-12 20:40:18 +01:00
);
2021-02-18 10:11:46 +01:00
const batchSize = 4;
const batchDelay = 1000;
const total = parseInt(process.argv[3]);
const workDir = path.join(process.argv[2], 'meta');
let count = 0;
let batchCount = 0;
function importMeta(keystore) {
let err;
let files;
try {
err, files = fs.readdirSync(workDir);
} catch {
console.error('source directory not yet ready', workDir);
setTimeout(importMeta, batchDelay, keystore);
return;
}
let limit = batchSize;
if (files.length < limit) {
limit = files.length;
}
for (let i = 0; i < limit; i++) {
const file = files[i];
if (file.substr(-5) != '.json') {
console.debug('skipping file', file);
}
const filePath = path.join(workDir, file);
console.log('file', count, filePath);
doOne(keystore, filePath);
count++;
batchCount++;
if (batchCount == batchSize) {
console.debug('reached batch size, breathing');
batchCount=0;
setTimeout(importMeta, batchDelay, keystore);
return;
}
}
if (count == total) {
return;
}
setTimeout(importMeta, 100, keystore);
}