DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • A Comprehensive Guide to GitHub
  • 10 Easy Steps To Start Using Git and GitHub
  • GitOps: Flux vs Argo CD
  • GitHub Security 101: Best Practices for Securing Your Repository

Trending

  • AI Meets Vector Databases: Redefining Data Retrieval in the Age of Intelligence
  • The Human Side of Logs: What Unstructured Data Is Trying to Tell You
  • Hybrid Cloud vs Multi-Cloud: Choosing the Right Strategy for AI Scalability and Security
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Personalized Code Searches Using OpenGrok

Personalized Code Searches Using OpenGrok

Improve your efficiency by deploying OpenGrok to provide code search capabilities on just the code you're interested in and not everything available.

By 
Scott Sosna user avatar
Scott Sosna
DZone Core CORE ·
Jun. 06, 23 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
6.2K Views

Join the DZone community and get the full member experience.

Join For Free

Many organizations implement a full-featured code search/intelligence tool — e.g., SourceGraph or Atlassian Fisheye — allowing engineers to search the enterprise code base. Other orgs just use the native search of the hosted version control system, such as GitHub's or GitLab's search. There are other commercial and open sources ways to search code.

Most of us, however, do not regularly work on code spanning the enterprise and would prefer results related to our current activities. You may also need to search the open-source projects incorporated into your work.  If this describes you, running OpenGrok on your laptop with a limited set of projects might be a solution.

If you've never heard of OpenGrok, it is a fully functioning Open Source code search/intelligence tool that works with many version control systems and multiple languages.

Prerequisites

These instructions were tested using Docker Desktop and should work with others — e.g., Colima — though slight adjustments might be necessary. A helpful page can be found here.

If necessary, install the docker runtime of your choice, i.e., Docker Desktop, Colima, Podman, containerd, etc.

This post uses Docker Desktop as the container runtime but should work with other container runtimes with some adjustments.

Create Docker Container

OpenGrok automatically pulls changes and rebuilds its indexes hourly by default.

While open-source projects usually do not restrict read-only access to their code, organizations secure their code to protect their intellectual property. To allow OpenGrok to access a private GitHub repository requires providing OpenGrok a valid SSH private key associated with your GitHub account. A customized Docker container is required to achieve this.

Clone OpenGrok

The entire OpenGrok code base is in GitHub. Clone the repo.

Shell
 
scsosna@mymachine src % git clone https://github.com/oracle/opengrok.git


Change Directory

Your current working directory must be the just-cloned directory.

Shell
 
scsosna@mymachine src % cd opengrok


Copy SSH Key

These instructions are for accessing a git repository via SSH authentication, such as GitHub.

Copy the private key associated with your GitHub into the OpenGrok directory from your .ssh directory.  For this example, the file is named id_ed25519.

Shell
 
scsosna@mymachine opengrok % cp ~/.ssh/id_ed25519 .


Modify Dockerfile

Two changes are required to the provided Dockerfile:

  • Remove unused version control systems: most common version control systems are supported. Unused ones may be removed from your rebuilt container;
  • Add the SSH key: the SSH key copied into the OpenGrok repository needs to be explicitly included in the built container. 

This git patch can be applied directly to the cloned OpenGrok repo.

diff
 
index ae0b52d76e..e1e3771a1a 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -55,9 +55,12 @@ RUN echo 'deb http://package.perforce.com/apt/ubuntu bionic release' > /etc/apt/
 # install dependencies and Python tools
 # hadolint ignore=DL3008,DL3009
 RUN apt-get update && \
-    apt-get install --no-install-recommends -y git subversion mercurial cvs cssc bzr rcs rcs-blame helix-p4d \
+    apt-get install --no-install-recommends -y git \
     unzip inotify-tools python3 python3-pip \
     python3-venv python3-setuptools openssh-client
+RUN mkdir -p /root/.ssh
+COPY id_ed25519 /root/.ssh/id_ed25519
+RUN chmod 600 /root/.ssh
 
 # compile and install universal-ctags
 # hadolint ignore=DL3003,DL3008


Build

Shell
 
scsosna@mymachine opengrok % docker build -t opengrok .


Clone Repos

A fully-qualified directory path is provided when starting the container, which identifies the directory for OpenGrok to index.

I recommend using a dedicated, separate directory into which the repos of interest are cloned. OpenGrok processes each subdirectory as a separate project pulls recent changes, and reindexes.

Shell
 
scsosna@mymachine opengrok % cd ~/data/src
scsosna@mymachine src % mkdir repos
scsosna@mymachine src % cd repos
scsosna@mymachine repos % git clone git@github.com:spring-projects/spring-boot.git
scsosna@mymachine repos % git clone git@github.com:spring-projects/spring-framework.git
scsosna@mymachine repos % git clone git@github.com:spring-projects/spring-security.git
scsosna@mymachine repos % git clone git@github.com:square/retrofit.git
scsosna@mymachine repos % git clone git@github.com:scsosna99/neo4j-gradle-dependencies.git


Start OpenGrok

Run Container

A simple command provides the local directory in which the repos to the index are located. In my example, it’s /data/src/repos.

Shell
 
scsosna@mymachine repos % docker run -d -v ~/data/src/repos:/opengrok/src -p 8080:8080 opengrok


Note: the -p 8080:8080 maps the container port 8080 to machine port 8080. Choose another port if 8080 is unavailable, preferably a non-privileged port above 1023, such as -p 8080:1234.

Update Known Hosts

Initially, your SSH key is not known or approved and needs to be confirmed. OpenGrok will attempt its git pull but the command fails because the SSH key is still unrecognized. To access the container via the docker shell, you need to execute a manual git pull to approve the key. You'll only need to do this once.

The docker ps command shows the names of running containers, in our example bold_chandrasekhar. After entering the docker shell, change to any source repo and execute a git pull.

Shell
 
scsosna@mymachine repos % docker ps                         
CONTAINER ID   IMAGE      COMMAND               CREATED          STATUS          PORTS                    NAMES
a77a49d1d675   opengrok   "/scripts/start.py"   58 seconds ago   Up 56 seconds   0.0.0.0:8080->8080/tcp   bold_chandrasekhar
scsosna@mymachine repos % docker exec -it bold_chandrasekhar bash
root@a77a49d1d675:/usr/local/tomcat# cd /opengrok/src
root@a77a49d1d675:/opengrok/src# cd user-service
root@a77a49d1d675:/opengrok/src/user-service# git pull
The authenticity of host 'github.com (140.82.112.3)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
Already up to date.
root@a77a49d1d675:/opengrok/src/user-service# exit
scsosna@mymachine repos %


Restart Container

Since OpenGrok indexes every hour, you can wait an hour for the next re-indexing or restart the container to force an immediate re-index.

Shell
 
scsosna@mymachine repos % docker restart bold_chandrasekhar
bold_chandrasekhar
scsosna@mymachine repos % 


Browse OpenGrok

Navigate to http://localhost:8080 to access OpenGrok:

OpenGrok search

Select the project(s) to include in your search:

Select project(s)

Enter a search and review the results:

Enter search

Conclusions

I'm a long-time advocate of OpenGrok but hadn't used it recently, so was pleasantly surprised to see it so easy to set up in a local environment using Docker.

Using it locally in this manner has definitely helped productivity for the reasons described: I'm only interested in a subset of the enterprise code base.

I have noticed that indices appear to be corrupted over time, requiring starting a new image, but that effort is fairly minor.

Git GitHub Docker (software) Open source

Opinions expressed by DZone contributors are their own.

Related

  • A Comprehensive Guide to GitHub
  • 10 Easy Steps To Start Using Git and GitHub
  • GitOps: Flux vs Argo CD
  • GitHub Security 101: Best Practices for Securing Your Repository

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!