Esempio n. 1
0
def initialize_db():
    os.umask(0)
    if not os.path.exists('/opt/TopPatch/var/tmp'):
        os.mkdir('/opt/TopPatch/var/tmp')
    if not os.path.exists('/opt/TopPatch/var/log'):
        os.mkdir('/opt/TopPatch/var/log')
    if not os.path.exists('/opt/TopPatch/var/rethinkdb'):
        os.mkdir('/opt/TopPatch/var/rethinkdb')
    if not os.path.exists('/opt/TopPatch/var/scheduler'):
        os.mkdir('/opt/TopPatch/var/scheduler')
    if not os.path.exists('/opt/TopPatch/var/packages'):
        os.mkdir('/opt/TopPatch/var/packages')
    if not os.path.exists('/opt/TopPatch/logs'):
        os.mkdir('/opt/TopPatch/logs')
    if not os.path.exists('/opt/TopPatch/var/packages/tmp'):
        os.mkdir('/opt/TopPatch/var/packages/tmp', 0773)
    if not os.path.exists('/opt/TopPatch/tp/src/plugins/cve/data/xls'):
        os.makedirs('/opt/TopPatch/tp/src/plugins/cve/data/xls', 0773)
    if not os.path.exists('/opt/TopPatch/tp/src/plugins/cve/data/xml'):
        os.mkdir('/opt/TopPatch/tp/src/plugins/cve/data/xml', 0773)
    if not os.path.exists('/opt/TopPatch/tp/src/plugins/cve/data/html/ubuntu'):
        os.makedirs('/opt/TopPatch/tp/src/plugins/cve/data/html/ubuntu', 0773)
    if not os.path.exists('/usr/lib/libpcre.so.1'):
        os.symlink('/opt/TopPatch/lib/libpcre.so.1', '/usr/lib') 
    if not os.path.exists('/etc/init.d/vFense'):
        subprocess.Popen(
            [
                'ln', '-s',
                '/opt/TopPatch/tp/src/daemon/vFense',
                '/etc/init.d/vFense'
            ],
        )
        subprocess.Popen(
            [
                'update-rc.d', 'vFense',
                'defaults'
            ],
        )
    if not os.path.exists('/etc/init.d/nginx'):
        subprocess.Popen(
            [
                'ln', '-s',
                '/opt/TopPatch/tp/src/daemon/nginx',
                '/etc/init.d/nginx'
            ],
        )
        subprocess.Popen(
            [
                'update-rc.d', 'nginx',
                'defaults'
            ],
        )
    try:
        tp_exists = pwd.getpwnam('toppatch')

    except Exception as e:
        subprocess.Popen(
            [
                'adduser', 'toppatch',
            ],
        )

    os.chdir(RETHINK_PATH)
    rethink_init = subprocess.Popen(['./rethinkdb', 'create',
                                     '-d', RETHINK_INSTANCES_PATH],
                                    stdout=subprocess.PIPE)
    rethink_init.poll()
    rethink_init.wait()
    if rethink_init.returncode == 0:
        rethink_start = subprocess.Popen(['./rethinkdb', '--config-file',
                                          RETHINK_CONF,
                                          '--web-static-directory',
                                          RETHINK_WEB])
        rethink_start.poll()
        completed = True
        sleep(2)
        while not db_connect():
            print 'Sleeping until rethink starts'
            sleep(2)
    else:
        completed = False
        msg = 'Failed during Rethink initialization'
        return(completed, msg)
    if completed:
        conn = r.connect(port=9009)
        r.db_create('toppatch_server').run(conn)
        db = r.db('toppatch_server')
        conn.close()
        ci.initialize_indexes_and_create_tables()
        conn = db_connect()

        hierarchy_db.init()
        Hierarchy.create_customer(
            DefaultCustomer,
            {
                CoreProperty.NetThrottle: '0',
                CoreProperty.CpuThrottle: 'idle',
                CoreProperty.PackageUrl: url
            }
        )
        admin_pass = args.admin_password
        Hierarchy.create_user(
            'admin',
            'TopPatch Admin Account',
            '*****@*****.**',
            admin_pass,
            groups=[DefaultGroup.Administrator]
        )

        if args.cve_data:
            print "Updating CVE's..."
            load_up_all_xml_into_db()
            print "Done Updating CVE's..."
            print "Updating Microsoft Security Bulletin Ids..."
            parse_bulletin_and_updatedb()
            print "Done Updating Microsoft Security Bulletin Ids..."
            print "Updating Ubuntu Security Bulletin Ids...( This can take a couple of minutes )"
            begin_usn_home_page_processing(full_parse=True)
            print "Done Updating Ubuntu Security Bulletin Ids..."

        print 'Admin user and password = admin:%s' % (admin_pass)
        agent_pass = generate_pass()
        agent = Hierarchy.create_user(
            'agent',
            'TopPatch Agent Communication Account',
            '*****@*****.**',
            agent_pass,
            groups=[DefaultGroup.Administrator]
        )
        print 'Agent user and password = agent:%s' % (agent_pass)

        monit.monit_initialization()

        conn.close()
        completed = True

        msg = 'Rethink Initialization and Table creation is now complete'
        pid = open(RETHINK_PID_FILE, 'r').read()
        if re.search(r'[0-9]+', pid):
            try:
                os.kill(int(pid), signal.SIGTERM)
                os.remove(RETHINK_PID_FILE)
            except Exception as e:
                if e.errno == 3:
                    os.remove(RETHINK_PID_FILE)
            rql_msg = 'Rethink stopped successfully\n'
        else:
            rql_msg = 'Rethink could not be stopped\n'
        print rql_msg

        return completed, msg
    else:
        completed = False
        msg = 'Failed during Rethink startup process'
        return completed, msg
Esempio n. 2
0
def initialize_db():
    os.umask(0)
    if not os.path.exists('/opt/TopPatch/var/tmp'):
        os.mkdir('/opt/TopPatch/var/tmp')
    if not os.path.exists('/opt/TopPatch/var/log'):
        os.mkdir('/opt/TopPatch/var/log')
    if not os.path.exists('/opt/TopPatch/var/rethinkdb'):
        os.mkdir('/opt/TopPatch/var/rethinkdb')
    if not os.path.exists('/opt/TopPatch/var/scheduler'):
        os.mkdir('/opt/TopPatch/var/scheduler')
    if not os.path.exists('/opt/TopPatch/var/packages'):
        os.mkdir('/opt/TopPatch/var/packages')
    if not os.path.exists('/opt/TopPatch/logs'):
        os.mkdir('/opt/TopPatch/logs')
    if not os.path.exists('/opt/TopPatch/var/packages/tmp'):
        os.mkdir('/opt/TopPatch/var/packages/tmp', 0773)
    if not os.path.exists('/opt/TopPatch/tp/src/plugins/cve/data/xls'):
        os.makedirs('/opt/TopPatch/tp/src/plugins/cve/data/xls', 0773)
    if not os.path.exists('/opt/TopPatch/tp/src/plugins/cve/data/xml'):
        os.mkdir('/opt/TopPatch/tp/src/plugins/cve/data/xml', 0773)
    if not os.path.exists('/opt/TopPatch/tp/src/plugins/cve/data/html/ubuntu'):
        os.makedirs('/opt/TopPatch/tp/src/plugins/cve/data/html/ubuntu', 0773)
    if not os.path.exists('/usr/lib/libpcre.so.1'):
        os.symlink('/opt/TopPatch/lib/libpcre.so.1', '/usr/lib')
    if not os.path.exists('/etc/init.d/vFense'):
        subprocess.Popen([
            'ln', '-s', '/opt/TopPatch/tp/src/daemon/vFense',
            '/etc/init.d/vFense'
        ], )
        subprocess.Popen(['update-rc.d', 'vFense', 'defaults'], )
    if not os.path.exists('/etc/init.d/nginx'):
        subprocess.Popen([
            'ln', '-s', '/opt/TopPatch/tp/src/daemon/nginx',
            '/etc/init.d/nginx'
        ], )
        subprocess.Popen(['update-rc.d', 'nginx', 'defaults'], )
    try:
        tp_exists = pwd.getpwnam('toppatch')

    except Exception as e:
        subprocess.Popen([
            'adduser',
            'toppatch',
        ], )

    os.chdir(RETHINK_PATH)
    rethink_init = subprocess.Popen(
        ['./rethinkdb', 'create', '-d', RETHINK_INSTANCES_PATH],
        stdout=subprocess.PIPE)
    rethink_init.poll()
    rethink_init.wait()
    if rethink_init.returncode == 0:
        rethink_start = subprocess.Popen([
            './rethinkdb', '--config-file', RETHINK_CONF,
            '--web-static-directory', RETHINK_WEB
        ])
        rethink_start.poll()
        completed = True
        sleep(2)
        while not db_connect():
            print 'Sleeping until rethink starts'
            sleep(2)
    else:
        completed = False
        msg = 'Failed during Rethink initialization'
        return (completed, msg)
    if completed:
        conn = r.connect(port=9009)
        r.db_create('toppatch_server').run(conn)
        db = r.db('toppatch_server')
        conn.close()
        ci.initialize_indexes_and_create_tables()
        conn = db_connect()

        hierarchy_db.init()
        Hierarchy.create_customer(
            DefaultCustomer, {
                CoreProperty.NetThrottle: '0',
                CoreProperty.CpuThrottle: 'idle',
                CoreProperty.PackageUrl: url
            })
        admin_pass = args.admin_password
        Hierarchy.create_user('admin',
                              'TopPatch Admin Account',
                              '*****@*****.**',
                              admin_pass,
                              groups=[DefaultGroup.Administrator])

        if args.cve_data:
            print "Updating CVE's..."
            load_up_all_xml_into_db()
            print "Done Updating CVE's..."
            print "Updating Microsoft Security Bulletin Ids..."
            parse_bulletin_and_updatedb()
            print "Done Updating Microsoft Security Bulletin Ids..."
            print "Updating Ubuntu Security Bulletin Ids...( This can take a couple of minutes )"
            begin_usn_home_page_processing(full_parse=True)
            print "Done Updating Ubuntu Security Bulletin Ids..."

        print 'Admin user and password = admin:%s' % (admin_pass)
        agent_pass = generate_pass()
        agent = Hierarchy.create_user('agent',
                                      'TopPatch Agent Communication Account',
                                      '*****@*****.**',
                                      agent_pass,
                                      groups=[DefaultGroup.Administrator])
        print 'Agent user and password = agent:%s' % (agent_pass)

        monit.monit_initialization()

        conn.close()
        completed = True

        msg = 'Rethink Initialization and Table creation is now complete'
        pid = open(RETHINK_PID_FILE, 'r').read()
        if re.search(r'[0-9]+', pid):
            try:
                os.kill(int(pid), signal.SIGTERM)
                os.remove(RETHINK_PID_FILE)
            except Exception as e:
                if e.errno == 3:
                    os.remove(RETHINK_PID_FILE)
            rql_msg = 'Rethink stopped successfully\n'
        else:
            rql_msg = 'Rethink could not be stopped\n'
        print rql_msg

        return completed, msg
    else:
        completed = False
        msg = 'Failed during Rethink startup process'
        return completed, msg