Put sources in lamassu.json

This commit is contained in:
Konstantin Mamalakis 2018-03-16 15:35:32 +02:00 committed by Josh Harvey
parent b72f5549a5
commit 65f0e852d0
2 changed files with 56 additions and 47 deletions

View file

@ -80,7 +80,17 @@ cat <<EOF > $CONFIG_DIR/lamassu.json
"lamassuCaPath": "$LAMASSU_CA_PATH", "lamassuCaPath": "$LAMASSU_CA_PATH",
"lamassuServerPath": "$PWD", "lamassuServerPath": "$PWD",
"migrateStatePath": "$MIGRATE_STATE_PATH", "migrateStatePath": "$MIGRATE_STATE_PATH",
"ofacDataDir": "$OFAC_DATA_DIR" "ofacDataDir": "$OFAC_DATA_DIR",
"ofacSources": [
{
"name": "sdn_advanced",
"url": "https://www.treasury.gov/ofac/downloads/sanctions/1.0/sdn_advanced.xml"
},
{
"name": "cons_advanced",
"url": "https://www.treasury.gov/ofac/downloads/sanctions/1.0/cons_advanced.xml"
}
]
} }
EOF EOF

View file

@ -9,11 +9,11 @@ const _ = require('lodash/fp')
const OFAC_DATA_DIR = options.ofacDataDir const OFAC_DATA_DIR = options.ofacDataDir
const OFAC_SOURCES_DIR = path.join(OFAC_DATA_DIR, 'sources') const OFAC_SOURCES_DIR = path.join(OFAC_DATA_DIR, 'sources')
const OFAC_SOURCES_FILE = path.join(OFAC_DATA_DIR, 'sources.json')
const OFAC_ETAGS_FILE = path.join(OFAC_DATA_DIR, 'etags.json') const OFAC_ETAGS_FILE = path.join(OFAC_DATA_DIR, 'etags.json')
const DOWNLOAD_DIR = path.resolve('/tmp') const DOWNLOAD_DIR = path.resolve('/tmp')
const mkdir = util.promisify(fs.mkdir)
const readFile = util.promisify(fs.readFile) const readFile = util.promisify(fs.readFile)
const writeFile = util.promisify(fs.writeFile) const writeFile = util.promisify(fs.writeFile)
const rename = util.promisify(fs.rename) const rename = util.promisify(fs.rename)
@ -47,9 +47,8 @@ const promiseGetEtag = (source) => {
const download = _.curry((dstDir, source) => { const download = _.curry((dstDir, source) => {
console.log("download", source) console.log("download", source)
const {url: sourceUrl} = source const {name, url: sourceUrl} = source
const fileName = path.basename(sourceUrl) const dstFile = path.join(dstDir, name + '.xml')
const dstFile = path.join(dstDir, fileName)
const file = fs.createWriteStream(dstFile) const file = fs.createWriteStream(dstFile)
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -99,15 +98,15 @@ const moveToSourcesDir = srcFile => {
} }
function update () { const update = () => mkdir(OFAC_DATA_DIR).catch(err => null)
.then(() => mkdir(OFAC_SOURCES_DIR)).catch(err => null)
.then(() => writeFile(OFAC_ETAGS_FILE, '{}', {encoding: 'utf-8', flag: 'wx'}))
.catch(err => null)
.then(() => {
const promiseOldEtags = readFile(OFAC_ETAGS_FILE, {encoding: 'utf-8'}) const promiseOldEtags = readFile(OFAC_ETAGS_FILE, {encoding: 'utf-8'})
.then(json => JSON.parse(json) || {}) .then(json => JSON.parse(json) || {})
const promiseNewEtags = readFile(OFAC_SOURCES_FILE, {encoding: 'utf-8'}) const promiseNewEtags = Promise.resolve(options.ofacSources || [])
.then(json => {
const obj = JSON.parse(json)
return obj ? obj.sources : []
})
.then(sources => Promise.all(_.map(promiseGetEtag, sources)) .then(sources => Promise.all(_.map(promiseGetEtag, sources))
.then(etags => _.map( .then(etags => _.map(
([source, etag]) => ({...source, etag}), ([source, etag]) => ({...source, etag}),
@ -150,6 +149,6 @@ function update () {
return Promise.all([updateEtags, ...moves, ...deletions]) return Promise.all([updateEtags, ...moves, ...deletions])
}) })
}) })
} })
module.exports = {update} module.exports = {update}