A script that automatically configures opengrok multi-projects

Some time ago, I configured opengrok on the server to read the code. There are many projects, and manual configuration one by one is cumbersome.

It took me almost a whole day from building tomcat and opengrok to configuring and indexing 5 Android projects.

If I were to manually configure a few more projects, I would probably collapse. Seeing that many people have the need to configure opengrok multi-projects, I wrote this script to automatically configure multi-projects.

1. Script source code

/public/opengrok$ cat setup-opengrok-projects.sh 
#!/bin/bash
#
# Copyright (C) 2023, guyongqiangx All Rights Reserved.
# 
# File Name: setup-opengrok-projects.sh 
#    Author: guyongqiangx
#      Blog: https://blog.csdn.net/guyongqiangx
#      Date: 2023-02-11
#      Info: 批量配置 opengrok 项目
#

# opengrok 安装目录
OPENGROK_APP_DIR=/opt/opengrok/dist/opengrok-1.7.42
# tomcat 安装目录
TOMCAT_APP_DIR=/opt/tomcat/apache-tomcat-10.0.27
# 代码项目根目录
PROJECT_ROOT_DIR=/public/opengrok
# 服务器访问地址
SERVER_URL=http://localhost:8000

# sudo 方式下用普通用户执行命令: "sudo -u username command"
USER="sudo -u guyongqiangx"

# 带一个参数,当前项目
setup_opengrok_project() {
    
    
    PROJECT=$1

    DATA_DIR=${PROJECT_ROOT_DIR}/data/${PROJECT}
    ETC_DIR=${PROJECT_ROOT_DIR}/etc/${PROJECT}
    SRC_DIR=${PROJECT_ROOT_DIR}/src/${PROJECT}
    LOG_DIR=${PROJECT_ROOT_DIR}/log/${PROJECT}

    # 1. 准备项目的 data, etc 和 log 目录
    ${
    
    USER} mkdir -p ${DATA_DIR} ${ETC_DIR} ${LOG_DIR}

    # 2. 准备项目的 logging.properties 文件
    ${
    
    USER} cp ${OPENGROK_APP_DIR}/doc/logging.properties ${ETC_DIR}

    # 3. 更新项目的 logging.properties 文件
    PATTERN="java.util.logging.FileHandler.pattern =.*"
    REPLACE="java.util.logging.FileHandler.pattern = ${LOG_DIR}/opengrok%g.%u.log"

    # TODAY=$(date +%F)
    # AUTHOR=guyongqiangx
    # COMMENTS="$TODAY $AUTHOR - Set logging file location to ${PROJECT} etc dir"
    # sed -i "s!^${PATTERN}!# &\n# ${COMMENTS}\n${REPLACE}!" ${ETC_DIR}/logging.properties
    ${
    
    USER} sed -i "s!^${PATTERN}!# &\n${REPLACE}!" ${ETC_DIR}/logging.properties

    # 4. 复制模板 source.war 到 tomcat 的 webapps 目录
    sudo cp ${OPENGROK_APP_DIR}/lib/source.war ${TOMCAT_APP_DIR}/webapps/${PROJECT}.war 

    # 5. 更新项目的 tomcat 配置文件 web.xml
    sudo opengrok-deploy -c ${ETC_DIR}/configuration.xml ${OPENGROK_APP_DIR}/lib/source.war ${TOMCAT_APP_DIR}/webapps/${PROJECT}.war

    # 6. 索引项目数据
    ${
    
    USER} java \
        -Djava.util.logging.config.file=${ETC_DIR}/logging.properties \
        -jar ${OPENGROK_APP_DIR}/lib/opengrok.jar \
        -c /usr/local/bin/ctags \
        -s ${SRC_DIR}  \
        -d ${DATA_DIR} \
        -P -S -G -W ${ETC_DIR}/configuration.xml \
        -U ${SERVER_URL}/${PROJECT} &> ${LOG_DIR}/index.log &
    ${
    
    USER} echo "index project ${PROJECT} in background..."
}

# for project in uboot-v2009.01  uboot-v2013.01  uboot-v2014.01  uboot-v2015.01
for project in $(ls ${
     
     PROJECT_ROOT_DIR}/src)
do
    echo "setup project ${project}..."
    setup_opengrok_project ${project}
done

while [ $(ps -ef | grep -c opengrok.jar) -ne 1 ]
do
    ${
    
    USER} echo -n .
    sleep 2
done

# sudo service tomcat restart
sudo systemctl restart tomcat.service
echo "done!"

2. How to use it?

2.1 Deployment script

You need to install tomcat and opengrok before using it. Please search for how to install it yourself.

Then do the following configuration in the script file:

# opengrok 安装目录
OPENGROK_APP_DIR=/opt/opengrok/dist/opengrok-1.7.42
# tomcat 安装目录
TOMCAT_APP_DIR=/opt/tomcat/apache-tomcat-10.0.27
# 项目根目录
PROJECT_ROOT_DIR=/public/opengrok
# 服务器访问地址
SERVER_URL=http://localhost:8000

# 使用普通用户执行 sudo 命令: "sudo -u username command"
USER="sudo -u guyongqiangx"

The last item is to use sudo to perform normal user operations, so you need to provide the normal user's username.

2.2 Prepare project code

Then put all project-related codes in the subdirectoryPROJECT_ROOT_DIR of the project root directory ( ) , similar to the following:src

/public/opengrok$ tree . -L 2
.
├── setup-opengrok-projects.sh
└── src
    ├── uboot-v2009.01
    ├── uboot-v2013.01
    ├── uboot-v2014.01
    ├── uboot-v2015.01
    ├── uboot-v2016.01
    ├── uboot-v2017.01
    ├── uboot-v2018.01
    ├── uboot-v2019.01
    ├── uboot-v2020.01
    ├── uboot-v2021.01
    ├── uboot-v2022.01
    └── uboot-v2023.01

13 directories, 1 file

By default, the script scans all folders in srcsubdirectories and creates an item with the name of that folder.

If you only want to operate on certain specified folders, you can modify the list of script setting items, similar to the following:

for project in uboot-v2009.01  uboot-v2013.01  uboot-v2014.01  uboot-v2015.01
# for project in $(ls ${PROJECT_ROOT_DIR}/src)
do
    echo "setup project ${project}..."
    setup_opengrok_project ${project}
done

2.3 Execute the script to configure all items

After the code is ready, execute the script with sudo permission in the project root directory, as follows:

/public/opengrok$ sudo bash setup-opengrok-projects.sh 
[sudo] password for guyongqiangx: 
setup project uboot-v2009.01...
index project uboot-v2009.01 in background...
setup project uboot-v2013.01...
index project uboot-v2013.01 in background...
...
setup project uboot-v2022.01...
index project uboot-v2022.01 in background...
setup project uboot-v2023.01...
index project uboot-v2023.01 in background...
..............done!

After running the script is a long wait until the script output prints "done!"

The script must be executed in sudo mode, because some operations require sudo authority to modify files in the tomcat installation directory.

Among them, the log of the index project data is redirected to index.logthe file , for example:

/public/opengrok$ ls -lh log/uboot-v2009.01/
total 16M
-rw-r--r-- 1 root         root  138K Feb 11 23:55 index.log
-rw-r--r-- 1 guyongqiangx users  16M Feb 11 23:55 opengrok0.0.log

2.4 Directory structure of the project

After the project is configured, the directory structure is as follows:

/public/opengrok$ tree . -L 2
.
├── data
│   ├── uboot-v2009.01
│   ...
│   └── uboot-v2023.01
├── etc
│   ├── uboot-v2009.01
│   ...
│   └── uboot-v2023.01
├── log
│   ├── uboot-v2009.01
│   ...
│   └── uboot-v2023.01
├── setup-opengrok-projects.sh
└── src
    ├── uboot-v2009.01
    ├── ...
    └── uboot-v2023.01

52 directories, 1 file

in,

  • src is the code of each project
  • data is the data indexed by each project
  • etc is the corresponding configuration file for each project
  • log is the log of each project index operation and parsing

3. Questions

Currently multi-projects created this way work, but switching between projects while browsing is cumbersome.

I want to be able to switch between the same file in different projects in a relatively simple way, such as the drop-down list below:

dropdown toggle in opengrok project

Click the item in the drop-down list to view the same file in the corresponding project /build/envsetup.sh. It is very convenient when viewing multiple versions of the same file. If you know how to configure it, please let me know. Thank you very much~

Guess you like

Origin blog.csdn.net/guyongqiangx/article/details/128991306