jLinux

Untitled

Dec 25th, 2015
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. const Knex   = require( 'knex' )( require('./config').database );
  4. const Crypto = require('crypto');
  5. const _      = require('lodash');
  6.  
  7. createOSsTable( Knex )
  8.     .then( createServersTable )
  9.     .then( createAppsTable )
  10.     .then( createAppsServersTable )
  11.     .then( insertOSs )
  12.     .then( insertApps )
  13.     .then( insertServers )
  14.     .then( insertAppsServers )
  15.     .then( result => {
  16.         console.log('Import Completed');
  17.         Knex.destroy((d) => {
  18.             console.log('Connection pool destroyed');
  19.         });
  20.     })
  21.     .catch( err => {
  22.         console.log('Import Error:', err);
  23.         Knex.destroy((d) => {
  24.             console.log('Connection pool destroyed');
  25.         });
  26.     });
  27.  
  28.  
  29. function createOSsTable( Knex ){
  30.     return new Promise( ( res, rej ) => {
  31.         Knex.schema.createTable('operating_systems', ( table ) => {
  32.             table
  33.                 .increments('id')
  34.                 .unique()
  35.                 .primary();
  36.  
  37.             table.string('flavor', 30);
  38.  
  39.             table
  40.                 .string('release', 11)
  41.                 .notNullable();
  42.  
  43.             table.string('code_name', 30);
  44.  
  45.             table
  46.                 .string('description', 100)
  47.                 .notNullable();
  48.  
  49.             table.timestamps();
  50.  
  51.             table.dateTime('deleted_at');
  52.  
  53.             table.dateTime('restored_at');
  54.         }).then( ( result ) => {
  55.             console.log('Table operating_systems created');
  56.             res( Knex );
  57.         }).catch( ( err ) => {
  58.             console.log('FAILED to create operating_systems table:', err);
  59.             rej( err );
  60.         });
  61.     });
  62. }
  63.  
  64. function insertOSs( Knex ){
  65.     return new Promise( ( res, rej ) => {
  66.         Knex.insert([
  67.             { created_at: Knex.fn.now(), type: 'linux',     flavor: 'Ubuntu', release: '7.10', code_name: 'gutsy', description: 'Ubuntu 7.10'                },
  68.             { created_at: Knex.fn.now(), type: 'linux',     flavor: 'CentOS', release: '6.7',  code_name: null,    description: 'CentOS release 6.7 (Final)' },
  69.             { created_at: Knex.fn.now(), type: 'linux',     flavor: 'RedHat', release: '6',    code_name: null,    description: 'RedHat Enterprise Linux 6'  },
  70.             { created_at: Knex.fn.now(), type: 'microsoft', flavor: '2008',   release: 'r2',   code_name: null,    description: 'Windows Server 2008 r2'     }
  71.         ]).into('operating_systems')
  72.             .then( ( cars ) => {
  73.                 console.log( 'Inserted operating systems' );
  74.                 res( Knex );
  75.             })
  76.             .catch( ( err ) => {
  77.                 console.log('FAILED to insert operating systems');
  78.                 rej( err );
  79.             });
  80.     });
  81. }
  82.  
  83. function createServersTable( Knex ){
  84.     return new Promise( ( res, rej ) => {
  85.         Knex.schema.createTable('servers', ( table ) => {
  86.             table
  87.                 .increments('id')
  88.                 .unique()
  89.                 .primary();
  90.  
  91.             table
  92.                 .string('server_hostname', 40)
  93.                 .unique()
  94.                 .notNullable();
  95.  
  96.             table.string('root_password', 225);
  97.  
  98.             table
  99.                 .boolean('monitoring')
  100.                 .defaultTo(1)
  101.                 .notNullable();
  102.  
  103.             table
  104.                 .integer('operating_system_id')
  105.                 .unsigned()
  106.                 .notNullable()
  107.                 .references('operating_systems.id')
  108.                 .onDelete('cascade');
  109.  
  110.             table.text('notes');
  111.  
  112.             table.timestamps();
  113.  
  114.             table.dateTime('deleted_at');
  115.  
  116.             table.dateTime('restored_at');
  117.  
  118.         }).then((result) => {
  119.             console.log('Table servers created');
  120.             res( Knex );
  121.         }).catch((err) => {
  122.             console.log('FAILED to create servers table:', err);
  123.             rej(err);
  124.         });
  125.     });
  126. }
  127.  
  128. function insertServers( Knex ){
  129.     return new Promise( ( res, rej ) => {
  130.         Knex.insert([
  131.             {    // 1
  132.                 created_at: Knex.fn.now(),
  133.                 server_hostname: 'lpweb01.server.ad',
  134.                 monitoring: 1,
  135.                 operating_system_id: 1,
  136.                 root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
  137.             }, { // 2
  138.                 created_at: Knex.fn.now(),
  139.                 server_hostname: 'ldjen02.server.ad',
  140.                 monitoring: 1,
  141.                 operating_system_id: 2,
  142.                 root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
  143.             }, { // 3
  144.                 created_at: Knex.fn.now(),
  145.                 server_hostname: 'lpnag01.server.ad',
  146.                 monitoring: 1,
  147.                 operating_system_id: 3,
  148.                 root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
  149.             }, { // 4
  150.                 created_at: Knex.fn.now(),
  151.                 server_hostname: 'lpnag02.server.ad',
  152.                 monitoring: 1,
  153.                 operating_system_id: 3,
  154.                 root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
  155.             }, { // 5
  156.                 created_at: Knex.fn.now(),
  157.                 server_hostname: 'lqchef01.server.ad',
  158.                 monitoring: 1,
  159.                 operating_system_id: 2,
  160.                 root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
  161.             }, { // 6
  162.                 created_at: Knex.fn.now(),
  163.                 server_hostname: 'wpdir01.server.ad',
  164.                 monitoring: 1,
  165.                 operating_system_id: 4,
  166.                 root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
  167.             }, { // 7
  168.                 created_at: Knex.fn.now(),
  169.                 server_hostname: 'wpdir02.server.ad',
  170.                 monitoring: 1,
  171.                 operating_system_id: 4,
  172.                 root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
  173.             }, { // 8
  174.                 created_at: Knex.fn.now(),
  175.                 server_hostname: 'lptwst01.server.ad',
  176.                 monitoring: 1,
  177.                 operating_system_id: 1,
  178.                 root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
  179.             }, { // 9
  180.                 created_at: Knex.fn.now(),
  181.                 server_hostname: 'lpngnx01.server.ad',
  182.                 monitoring: 1,
  183.                 operating_system_id: 1,
  184.                 root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
  185.             }, { // 10
  186.                 created_at: Knex.fn.now(),
  187.                 server_hostname: 'wqbatch01.server.ad',
  188.                 monitoring: 0,
  189.                 operating_system_id: 1,
  190.                 root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
  191.             }, { // 11
  192.                 created_at: Knex.fn.now(),
  193.                 server_hostname: 'lscacti01.server.ad',
  194.                 monitoring: 0,
  195.                 operating_system_id: 1,
  196.                 root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
  197.             }, { // 12
  198.                 created_at: Knex.fn.now(),
  199.                 server_hostname: 'lqrhds01.server.ad',
  200.                 monitoring: 1,
  201.                 operating_system_id: 1,
  202.                 root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
  203.             }, { // 13
  204.                 created_at: Knex.fn.now(),
  205.                 server_hostname: 'lqrhds02.server.ad',
  206.                 monitoring: 1,
  207.                 operating_system_id: 1,
  208.                 root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
  209.             }, { // 14
  210.                 created_at: Knex.fn.now(),
  211.                 server_hostname: 'wsbamboo01.server.ad',
  212.                 monitoring: 1,
  213.                 operating_system_id: 1,
  214.                 root_password: Crypto.createHash('md5').update( 'str - ' + _.random(0, 20) ).digest('hex')
  215.             }
  216.         ]).into('servers')
  217.             .then( ( cars ) => {
  218.                 console.log( 'Inserted servers systems' );
  219.                 res( Knex );
  220.             })
  221.             .catch( ( err ) => {
  222.                 console.log('FAILED to insert servers');
  223.                 rej( err );
  224.             });
  225.     });
  226. }
  227.  
  228. function createAppsTable( Knex ){
  229.     return new Promise( ( res, rej ) => {
  230.         Knex.schema.createTable('applications', ( table ) => {
  231.             table
  232.                 .increments('id')
  233.                 .unique()
  234.                 .primary();
  235.  
  236.             table
  237.                 .string('name', 20)
  238.                 .notNullable();
  239.  
  240.             table.string('version', 15);
  241.  
  242.             table.timestamps();
  243.  
  244.             table.dateTime('deleted_at');
  245.  
  246.             table.dateTime('restored_at');
  247.         }).then((result) => {
  248.             console.log('Table applications created');
  249.             res( Knex );
  250.         }).catch((err) => {
  251.             console.log('FAILED to create applications table:', err);
  252.             rej(err);
  253.         });
  254.     });
  255. }
  256.  
  257. function insertApps( Knex ){
  258.     return new Promise( ( res, rej ) => {
  259.         Knex.insert( [
  260.             { created_at: Knex.fn.now(), name: 'Apache',        version: '2'     }, // 1
  261.             { created_at: Knex.fn.now(), name: 'Nagios',        version: '4'     }, // 2
  262.             { created_at: Knex.fn.now(), name: 'Nginx',         version: null    }, // 3
  263.             { created_at: Knex.fn.now(), name: 'PHP',           version: '5'     }, // 4
  264.             { created_at: Knex.fn.now(), name: 'Jenkins',       version: '1.642' }, // 5
  265.             { created_at: Knex.fn.now(), name: 'Chef Server',   version: null    }, // 6
  266.             { created_at: Knex.fn.now(), name: 'Tumbleweed',    version: 1       }, // 7
  267.             { created_at: Knex.fn.now(), name: 'AD',            version: null    }, // 8
  268.             { created_at: Knex.fn.now(), name: 'Chef Client',   version: null    }, // 9
  269.             { created_at: Knex.fn.now(), name: 'Batch Server',  version: null    }, // 10
  270.             { created_at: Knex.fn.now(), name: 'LDAP',          version: null    }, // 11
  271.             { created_at: Knex.fn.now(), name: 'Cacti',         version: null    }, // 12
  272.             { created_at: Knex.fn.now(), name: 'Bamboo',        version: null    }  // 13
  273.         ] ).into( 'applications' )
  274.             .then( function ( cars ) {
  275.                 console.log( 'Inserted applications' );
  276.                 res( Knex );
  277.             } )
  278.             .catch( ( err ) => {
  279.                 console.log( 'FAILED to insert applications', err );
  280.                 rej( err );
  281.             } );
  282.     });
  283. }
  284.  
  285. function createAppsServersTable( Knex ){
  286.     return new Promise( ( res, rej ) => {
  287.         Knex.schema.createTable('applications_servers', ( table ) => {
  288.             table
  289.                 .increments('id')
  290.                 .unique()
  291.                 .primary();
  292.  
  293.             table
  294.                 .integer('server_id')
  295.                 .unsigned()
  296.                 .notNullable()
  297.                 .references('servers.id')
  298.                 .onDelete('cascade');
  299.  
  300.             table
  301.                 .integer('application_id')
  302.                 .unsigned()
  303.                 .notNullable()
  304.                 .references('applications.id')
  305.                 .onDelete('cascade');
  306.  
  307.             table.timestamps();
  308.  
  309.             table.dateTime('deleted_at');
  310.  
  311.             table.dateTime('restored_at');
  312.         }).then((result) => {
  313.             console.log('Table servers_applications created');
  314.             res( Knex );
  315.         }).catch((err) => {
  316.             console.log('FAILED to create servers_applications table:', err);
  317.             rej(err);
  318.         });
  319.     });
  320. }
  321.  
  322. function insertAppsServers( Knex ){
  323.     return new Promise( ( res, rej ) => {
  324.         Knex.insert( [
  325.             // lpweb01: Apache, PHP, Chef Client
  326.             { created_at: Knex.fn.now(), server_id: 1, application_id: 1   },
  327.             { created_at: Knex.fn.now(), server_id: 1, application_id: 4   },
  328.             { created_at: Knex.fn.now(), server_id: 1, application_id: 9   },
  329.             // lpngnx01: Nginx, PHP, Chef Client
  330.             { created_at: Knex.fn.now(), server_id: 9, application_id: 3   },
  331.             { created_at: Knex.fn.now(), server_id: 9, application_id: 4   },
  332.             { created_at: Knex.fn.now(), server_id: 9, application_id: 9   },
  333.             // ldjen02: Jenkins, Chef Client
  334.             { created_at: Knex.fn.now(), server_id: 2, application_id: 5   },
  335.             { created_at: Knex.fn.now(), server_id: 2, application_id: 9   },
  336.             // lpnag01 & lpnag02: Nagios, Chef Client
  337.             { created_at: Knex.fn.now(), server_id: 3, application_id: 2   },
  338.             { created_at: Knex.fn.now(), server_id: 3, application_id: 9   },
  339.             { created_at: Knex.fn.now(), server_id: 4, application_id: 2   },
  340.             { created_at: Knex.fn.now(), server_id: 4, application_id: 9   },
  341.             // lqchef01: Chef Server
  342.             { created_at: Knex.fn.now(), server_id: 5, application_id: 6   },
  343.             // wpdir01 & wpdir02: Active Directory
  344.             { created_at: Knex.fn.now(), server_id: 6, application_id: 8   },
  345.             { created_at: Knex.fn.now(), server_id: 7, application_id: 8   },
  346.             // lptwst01: Tumbleweed, Chef Client
  347.             { created_at: Knex.fn.now(), server_id: 8, application_id: 7   },
  348.             { created_at: Knex.fn.now(), server_id: 8, application_id: 9   },
  349.             // lscacti01: Cacti, Chef Client,
  350.             { created_at: Knex.fn.now(), server_id: 11, application_id: 9  },
  351.             { created_at: Knex.fn.now(), server_id: 11, application_id: 12 },
  352.             // wqbatch01: Batch Server
  353.             { created_at: Knex.fn.now(), server_id: 10, application_id: 10 },
  354.             // lqrhds01 & lqrhds02: LDAP, Chef Client
  355.             { created_at: Knex.fn.now(), server_id: 12, application_id: 11 },
  356.             { created_at: Knex.fn.now(), server_id: 13, application_id: 8  },
  357.             { created_at: Knex.fn.now(), server_id: 12, application_id: 11 },
  358.             { created_at: Knex.fn.now(), server_id: 13, application_id: 8  },
  359.             // wsbamboo01: Bamboo
  360.             { created_at: Knex.fn.now(), server_id: 14, application_id: 13 }
  361.         ] ).into( 'applications_servers' )
  362.             .then( function ( cars ) {
  363.                 console.log( 'Inserted servers_applications' );
  364.                 res( Knex );
  365.             } )
  366.             .catch( ( err ) => {
  367.                 console.log( 'FAILED to insert servers_applications', err );
  368.                 rej( err );
  369.             } );
  370.     });
  371. }
Advertisement
Add Comment
Please, Sign In to add comment