Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const Knex = require( 'knex' )( require('./config').database );
- const Crypto = require('crypto');
- const _ = require('lodash');
- createOSsTable( Knex )
- .then( createServersTable )
- .then( createAppsTable )
- .then( createAppsServersTable )
- .then( insertOSs )
- .then( insertApps )
- .then( insertServers )
- .then( insertAppsServers )
- .then( result => {
- console.log('Import Completed');
- Knex.destroy((d) => {
- console.log('Connection pool destroyed');
- });
- })
- .catch( err => {
- console.log('Import Error:', err);
- Knex.destroy((d) => {
- console.log('Connection pool destroyed');
- });
- });
- function createOSsTable( Knex ){
- return new Promise( ( res, rej ) => {
- Knex.schema.createTable('operating_systems', ( table ) => {
- table
- .increments('id')
- .unique()
- .primary();
- table.string('flavor', 30);
- table
- .string('release', 11)
- .notNullable();
- table.string('code_name', 30);
- table
- .string('description', 100)
- .notNullable();
- table.timestamps();
- table.dateTime('deleted_at');
- table.dateTime('restored_at');
- }).then( ( result ) => {
- console.log('Table operating_systems created');
- res( Knex );
- }).catch( ( err ) => {
- console.log('FAILED to create operating_systems table:', err);
- rej( err );
- });
- });
- }
- function insertOSs( Knex ){
- return new Promise( ( res, rej ) => {
- Knex.insert([
- { created_at: Knex.fn.now(), type: 'linux', flavor: 'Ubuntu', release: '7.10', code_name: 'gutsy', description: 'Ubuntu 7.10' },
- { created_at: Knex.fn.now(), type: 'linux', flavor: 'CentOS', release: '6.7', code_name: null, description: 'CentOS release 6.7 (Final)' },
- { created_at: Knex.fn.now(), type: 'linux', flavor: 'RedHat', release: '6', code_name: null, description: 'RedHat Enterprise Linux 6' },
- { created_at: Knex.fn.now(), type: 'microsoft', flavor: '2008', release: 'r2', code_name: null, description: 'Windows Server 2008 r2' }
- ]).into('operating_systems')
- .then( ( cars ) => {
- console.log( 'Inserted operating systems' );
- res( Knex );
- })
- .catch( ( err ) => {
- console.log('FAILED to insert operating systems');
- rej( err );
- });
- });
- }
- function createServersTable( Knex ){
- return new Promise( ( res, rej ) => {
- Knex.schema.createTable('servers', ( table ) => {
- table
- .increments('id')
- .unique()
- .primary();
- table
- .string('server_hostname', 40)
- .unique()
- .notNullable();
- table.string('root_password', 225);
- table
- .boolean('monitoring')
- .defaultTo(1)
- .notNullable();
- table
- .integer('operating_system_id')
- .unsigned()
- .notNullable()
- .references('operating_systems.id')
- .onDelete('cascade');
- table.text('notes');
- table.timestamps();
- table.dateTime('deleted_at');
- table.dateTime('restored_at');
- }).then((result) => {
- console.log('Table servers created');
- res( Knex );
- }).catch((err) => {
- console.log('FAILED to create servers table:', err);
- rej(err);
- });
- });
- }
- function insertServers( Knex ){
- return new Promise( ( res, rej ) => {
- Knex.insert([
- { // 1
- created_at: Knex.fn.now(),
- server_hostname: 'lpweb01.server.ad',
- monitoring: 1,
- operating_system_id: 1,
- root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
- }, { // 2
- created_at: Knex.fn.now(),
- server_hostname: 'ldjen02.server.ad',
- monitoring: 1,
- operating_system_id: 2,
- root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
- }, { // 3
- created_at: Knex.fn.now(),
- server_hostname: 'lpnag01.server.ad',
- monitoring: 1,
- operating_system_id: 3,
- root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
- }, { // 4
- created_at: Knex.fn.now(),
- server_hostname: 'lpnag02.server.ad',
- monitoring: 1,
- operating_system_id: 3,
- root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
- }, { // 5
- created_at: Knex.fn.now(),
- server_hostname: 'lqchef01.server.ad',
- monitoring: 1,
- operating_system_id: 2,
- root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
- }, { // 6
- created_at: Knex.fn.now(),
- server_hostname: 'wpdir01.server.ad',
- monitoring: 1,
- operating_system_id: 4,
- root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
- }, { // 7
- created_at: Knex.fn.now(),
- server_hostname: 'wpdir02.server.ad',
- monitoring: 1,
- operating_system_id: 4,
- root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
- }, { // 8
- created_at: Knex.fn.now(),
- server_hostname: 'lptwst01.server.ad',
- monitoring: 1,
- operating_system_id: 1,
- root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
- }, { // 9
- created_at: Knex.fn.now(),
- server_hostname: 'lpngnx01.server.ad',
- monitoring: 1,
- operating_system_id: 1,
- root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
- }, { // 10
- created_at: Knex.fn.now(),
- server_hostname: 'wqbatch01.server.ad',
- monitoring: 0,
- operating_system_id: 1,
- root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
- }, { // 11
- created_at: Knex.fn.now(),
- server_hostname: 'lscacti01.server.ad',
- monitoring: 0,
- operating_system_id: 1,
- root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
- }, { // 12
- created_at: Knex.fn.now(),
- server_hostname: 'lqrhds01.server.ad',
- monitoring: 1,
- operating_system_id: 1,
- root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
- }, { // 13
- created_at: Knex.fn.now(),
- server_hostname: 'lqrhds02.server.ad',
- monitoring: 1,
- operating_system_id: 1,
- root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
- }, { // 14
- created_at: Knex.fn.now(),
- server_hostname: 'wsbamboo01.server.ad',
- monitoring: 1,
- operating_system_id: 1,
- root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
- }
- ]).into('servers')
- .then( ( cars ) => {
- console.log( 'Inserted servers systems' );
- res( Knex );
- })
- .catch( ( err ) => {
- console.log('FAILED to insert servers');
- rej( err );
- });
- });
- }
- function createAppsTable( Knex ){
- return new Promise( ( res, rej ) => {
- Knex.schema.createTable('applications', ( table ) => {
- table
- .increments('id')
- .unique()
- .primary();
- table
- .string('name', 20)
- .notNullable();
- table.string('version', 15);
- table.timestamps();
- table.dateTime('deleted_at');
- table.dateTime('restored_at');
- }).then((result) => {
- console.log('Table applications created');
- res( Knex );
- }).catch((err) => {
- console.log('FAILED to create applications table:', err);
- rej(err);
- });
- });
- }
- function insertApps( Knex ){
- return new Promise( ( res, rej ) => {
- Knex.insert( [
- { created_at: Knex.fn.now(), name: 'Apache', version: '2' }, // 1
- { created_at: Knex.fn.now(), name: 'Nagios', version: '4' }, // 2
- { created_at: Knex.fn.now(), name: 'Nginx', version: null }, // 3
- { created_at: Knex.fn.now(), name: 'PHP', version: '5' }, // 4
- { created_at: Knex.fn.now(), name: 'Jenkins', version: '1.642' }, // 5
- { created_at: Knex.fn.now(), name: 'Chef Server', version: null }, // 6
- { created_at: Knex.fn.now(), name: 'Tumbleweed', version: 1 }, // 7
- { created_at: Knex.fn.now(), name: 'AD', version: null }, // 8
- { created_at: Knex.fn.now(), name: 'Chef Client', version: null }, // 9
- { created_at: Knex.fn.now(), name: 'Batch Server', version: null }, // 10
- { created_at: Knex.fn.now(), name: 'LDAP', version: null }, // 11
- { created_at: Knex.fn.now(), name: 'Cacti', version: null }, // 12
- { created_at: Knex.fn.now(), name: 'Bamboo', version: null } // 13
- ] ).into( 'applications' )
- .then( function ( cars ) {
- console.log( 'Inserted applications' );
- res( Knex );
- } )
- .catch( ( err ) => {
- console.log( 'FAILED to insert applications', err );
- rej( err );
- } );
- });
- }
- function createAppsServersTable( Knex ){
- return new Promise( ( res, rej ) => {
- Knex.schema.createTable('applications_servers', ( table ) => {
- table
- .increments('id')
- .unique()
- .primary();
- table
- .integer('server_id')
- .unsigned()
- .notNullable()
- .references('servers.id')
- .onDelete('cascade');
- table
- .integer('application_id')
- .unsigned()
- .notNullable()
- .references('applications.id')
- .onDelete('cascade');
- table.timestamps();
- table.dateTime('deleted_at');
- table.dateTime('restored_at');
- }).then((result) => {
- console.log('Table servers_applications created');
- res( Knex );
- }).catch((err) => {
- console.log('FAILED to create servers_applications table:', err);
- rej(err);
- });
- });
- }
- function insertAppsServers( Knex ){
- return new Promise( ( res, rej ) => {
- Knex.insert( [
- // lpweb01: Apache, PHP, Chef Client
- { created_at: Knex.fn.now(), server_id: 1, application_id: 1 },
- { created_at: Knex.fn.now(), server_id: 1, application_id: 4 },
- { created_at: Knex.fn.now(), server_id: 1, application_id: 9 },
- // lpngnx01: Nginx, PHP, Chef Client
- { created_at: Knex.fn.now(), server_id: 9, application_id: 3 },
- { created_at: Knex.fn.now(), server_id: 9, application_id: 4 },
- { created_at: Knex.fn.now(), server_id: 9, application_id: 9 },
- // ldjen02: Jenkins, Chef Client
- { created_at: Knex.fn.now(), server_id: 2, application_id: 5 },
- { created_at: Knex.fn.now(), server_id: 2, application_id: 9 },
- // lpnag01 & lpnag02: Nagios, Chef Client
- { created_at: Knex.fn.now(), server_id: 3, application_id: 2 },
- { created_at: Knex.fn.now(), server_id: 3, application_id: 9 },
- { created_at: Knex.fn.now(), server_id: 4, application_id: 2 },
- { created_at: Knex.fn.now(), server_id: 4, application_id: 9 },
- // lqchef01: Chef Server
- { created_at: Knex.fn.now(), server_id: 5, application_id: 6 },
- // wpdir01 & wpdir02: Active Directory
- { created_at: Knex.fn.now(), server_id: 6, application_id: 8 },
- { created_at: Knex.fn.now(), server_id: 7, application_id: 8 },
- // lptwst01: Tumbleweed, Chef Client
- { created_at: Knex.fn.now(), server_id: 8, application_id: 7 },
- { created_at: Knex.fn.now(), server_id: 8, application_id: 9 },
- // lscacti01: Cacti, Chef Client,
- { created_at: Knex.fn.now(), server_id: 11, application_id: 9 },
- { created_at: Knex.fn.now(), server_id: 11, application_id: 12 },
- // wqbatch01: Batch Server
- { created_at: Knex.fn.now(), server_id: 10, application_id: 10 },
- // lqrhds01 & lqrhds02: LDAP, Chef Client
- { created_at: Knex.fn.now(), server_id: 12, application_id: 11 },
- { created_at: Knex.fn.now(), server_id: 13, application_id: 8 },
- { created_at: Knex.fn.now(), server_id: 12, application_id: 11 },
- { created_at: Knex.fn.now(), server_id: 13, application_id: 8 },
- // wsbamboo01: Bamboo
- { created_at: Knex.fn.now(), server_id: 14, application_id: 13 }
- ] ).into( 'applications_servers' )
- .then( function ( cars ) {
- console.log( 'Inserted servers_applications' );
- res( Knex );
- } )
- .catch( ( err ) => {
- console.log( 'FAILED to insert servers_applications', err );
- rej( err );
- } );
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment