Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Form service migration script
// Accepted arguments
// 1. contactPoints: IP with port of DB
// 2. username: username for DB // optional
// 3. password: password for DB // optional
// example: node migration.js 11.7.1.7:9200 username password
const cassandra = require('cassandra-driver');
let cassandraClientOptions = { contactPoints: [process.argv[2]] };
if(process.argv[3] && process.argv[4]){
cassandraClientOptions.authProvider = new cassandra.auth.PlainTextAuthProvider(process.argv[3], process.argv[4]);
}
console.log('connecting to DB with', process.argv[2], process.argv[3], process.argv[4]);
const client = new cassandra.Client(cassandraClientOptions);
let transformed_data = [];
let dest_obj = {
root_org: undefined,
framework: undefined,
type: undefined,
subtype: undefined,
component: undefined,
action: undefined,
created: new Date(),
last_modified: undefined,
data: undefined
}
/* Copyright (c) 2016 Juniper Networks, Inc. All rights reserved. */
var _ = require("lodash");
var fs = require('fs')
var commonUtils = require(process.mainModule.exports.corePath + "/src/serverroot/utils/common.utils");
var configUtils = require(process.mainModule.exports.corePath +
"/src/serverroot/common/config.utils");
var cassandra = require("cassandra-driver");
var uddKeyspace = "config_webui";
var tableName = "user_widgets";
var config = configUtils.getConfig();
var cass_options = { contactPoints: config.cassandra.server_ips, keyspace: "system" };
if (config.cassandra.username && config.cassandra.password) {
var cAuthProvider = new cassandra.auth.PlainTextAuthProvider(config.cassandra.username, config.cassandra.password);
cass_options.authProvider = cAuthProvider;
}
if (config.cassandra.use_ssl) {
cass_options.sslOptions = { rejectUnauthorized: false };
if ('ca_certs' in config.cassandra && config.cassandra.ca_certs) {
cass_options.sslOptions.ca = [ fs.readFileSync(config.cassandra.ca_certs) ];
}
}
var client = new cassandra.Client(cass_options);
client.execute("SELECT keyspace_name FROM system_schema.keyspaces;", function(err, result) {
if (err) {
console.error(err);
return;
}
if (_.isEmpty(_.filter(result.rows, ["keyspace_name", uddKeyspace]))) {
// if (client.metadata.keyspaces[uddKeyspace]) {
self.createIfMissing = !!params.createIfMissing;
self.table = params.table || 'tiles';
self.repclass = params.repclass || 'SimpleStrategy';
self.repfactor = typeof params.repfactor === 'undefined' ? 3 : parseInt(params.repfactor);
let dw = params.durablewrite;
self.durablewrite = (typeof dw === 'undefined' || (dw && dw !== 'false' && dw !== '0')) ? 'true' : 'false';
self.minzoom = typeof params.minzoom === 'undefined' ? 0 : parseInt(params.minzoom);
self.maxzoom = typeof params.maxzoom === 'undefined' ? 22 : parseInt(params.maxzoom);
self.blocksize = typeof params.blocksize === 'undefined' ? 32768 : parseInt(params.blocksize);
self.maxBatchSize = typeof params.maxBatchSize === 'undefined' ? undefined : parseInt(params.maxBatchSize);
self.setLastModified = !!params.setLastModified;
self.copyInfoFrom = params.copyInfoFrom;
let clientOpts = {contactPoints: self.contactPoints};
if (params.username || params.password) {
clientOpts.authProvider = new cassandra.auth.PlainTextAuthProvider(params.username, params.password);
// make sure not to expose it in the error reporting
delete params.password;
}
self.client = new cassandra.Client(clientOpts);
return self.client.connectAsync();
}).then(() => {
if (!self.createIfMissing) {
},
protocolOptions: {
port: cassandraPort,
},
socketOptions: {
connectTimeout: 10000, // milliseconds
readTimeout: 60000 // milliseconds
},
queryOptions: {
autoPage: true,
prepare: true,
fetchSize: fetchSize
}
};
if (cassandraUsername && cassandraPassword) {
options.authProvider = new cassandra.auth.PlainTextAuthProvider(cassandraUsername, cassandraPassword);
}
const client = new cassandra.Client(options);
const status = {
isInitialized: false
};
/**
* @param {Array<{query: string, params: Array}>} mutations
* @returns {Promise}
*/
function executeBatchMutations(mutations) {
return new Promise((resolve, reject) => {
if (!client) {
loggingClient.logCassandraClientUndefined();
return reject('No DB client available');
if (conf.tls) {
try {
clientOpts.sslOptions = sslOptions(conf.tls);
} catch (e) {
return P.reject(e);
}
}
// Increase the schema agreement wait period from the default of 10s
clientOpts.protocolOptions = {
maxSchemaAgreementWaitSeconds: 30,
maxVersion: 3
};
if (conf.username && conf.password) {
clientOpts.authProvider = new cass.auth.PlainTextAuthProvider(
conf.username, conf.password);
}
// Up the maximum number of prepared statements. Driver default is 500.
clientOpts.maxPrepared = conf.maxPrepared || 50000;
clientOpts.promiseFactory = P.fromCallback;
const client = new cass.Client(clientOpts);
client.on('log', (level, message, info) => {
// Re-map levels
/* eslint-disable indent */
switch (level) {
case 'warning':
level = 'warn';
break;
import cassandra from 'cassandra-driver'
import config from 'config'
import Log from './log'
const log = new Log('hubert-cassandra')
// Connect to Cassandra
const db = new cassandra.Client({
contactPoints: config.db.cassandra.hosts,
keyspace: config.db.cassandra.keyspace,
authProvider: new cassandra.auth.PlainTextAuthProvider(
config.db.cassandra.username || null,
config.db.cassandra.password || null
),
})
db.on('connect', () => {
log.info('Cassandra connected')
})
export default db
var HOST = process.env.HOST || '127.0.0.1';
var PORT = process.env.PORT || 9042;
var KEYSPACE = process.env.KEYSPACE;
if (!KEYSPACE) {
console.log('`KEYSPACE` must be specified as environment variable');
process.exit();
}
var USER = process.env.USER;
var PASSWORD = process.env.PASSWORD;
var DIRECTORY = process.env.DIRECTORY || "./data";
var authProvider;
if (USER && PASSWORD) {
authProvider = new cassandra.auth.PlainTextAuthProvider(USER, PASSWORD);
}
var systemClient = new cassandra.Client({contactPoints: [HOST], authProvider: authProvider, protocolOptions: {port: [PORT]}});
var client = new cassandra.Client({ contactPoints: [HOST], keyspace: KEYSPACE, authProvider: authProvider, protocolOptions: {port: [PORT]}});
function processTableExport(table) {
console.log('==================================================');
console.log('Reading table: ' + table);
return new Promise(function(resolve, reject) {
var jsonfile = fs.createWriteStream(DIRECTORY +"/" + table + '.json');
jsonfile.on('error', function (err) {
reject(err);
});
var processed = 0;
var startTime = Date.now();
async function createClient() {
const authProvider = new cassandra.auth.PlainTextAuthProvider(databaseConfig.username, databaseConfig.password);
const config = {
contactPoints: String(databaseConfig.address).split(','),
keyspace: databaseConfig.name,
authProvider,
localDataCenter: databaseConfig.cassandraLocalDataCenter
};
let cassandraClient = new cassandra.Client(config);
return cassandraClient;
}
module.exports = function (conf = {contactPoints: ['localhost'], keyspace: 'ota'}) {
const keyspace = conf.keyspace;
const loadCassandra = loadCassandraFactory(keyspace);
if (conf.username && conf.password){
conf.authProvider = new cassandra.auth.PlainTextAuthProvider(conf.username, conf.password);
}
let client = new cassandra.Client(conf);
const cp = (...args)=> new Promise((resolve, reject)=> client.execute(...args, (e, o)=> e ? reject(e) : resolve(o)));
const order = (queries) => {
let ret = Promise.resolve(true);
for (const q of queries) ret = ret.then(_=>cp(q));
ret = ret.then(()=>cp(`use ${keyspace};`));
return ret;
};
const drop = ()=>new Promise((resolve, reject)=>client.execute(`drop KEYSPACE ${keyspace} ;`, (e,o)=>{
e ? resolve() : resolve()
}));
const reset = ()=>new Promise((resolve, reject)=> {