cheatsheets devlog projects

sourcegraph.md

Installing Sourcegraph on RHEL 7.4

2022-11-19 4

Bit of an adventure as it requires setting up some things before you can actually launch sourcegraph. This is really only a problem for RHEL 7.4 as it is a bit dated now. I imagine this is much more straightforward in a newer version.

The first thing is you need to install docker.

https://nivethan.dev/devlog/installing-docker-on-rhel-7.4.html

The second thing is you need to update git.

https://nivethan.dev/devlog/updating-git-on-rhel-7.4.html

Now that we have the set up done, we can set up sourcegraph and the repositories it will be indexing.

In my case, I don’t actually have git repositories set up and so I’ll need to use the src-expose utility to generate git repos and it will also serve those repos to source graph.

curl https://storage.googleapis.com/sourcegraph-artifacts/src-expose/latest/linux-amd64/src-expose -o /usr/local/bin/src-expose
chmod +x /usr/local/bin/src-expose

Now that it is installed, we can do the following:

src-expose dir_1 dir_2 dir_3

This will create git repos in ~/.sourcegraph/src-expose-repos of the directories that we want to index. src-expose will also serve these repos on port 3434. This port will needed to be opened.

Next we can install and run sourcegraph through docker.

docker run --detach --restart unless-stopped --publish 7080:7080 --publish 127.0.0.1:3370:3370 --volume ~/.sourcegraph/config:/etc/sourcegraph --volume ~/.sourcegraph/data:/var/opt/sourcegraph sourcegraph/server:4.1.3

This will serve the sourcegraph application on port 7080 which will also need to be opened on the firewall. The –detach and –restart make it so that the application runs in the background and that sourcegraph will restart if it fails or if the machine reboots.

We can check if the docker container is running by doing a ps:

docker ps

We can stop the container manually by doing:

docker stop {CONTAINER_ID}

Now that the sourcegraph container is running we can set up our admin account.

We can navigate to {IP}:7080 and this will give us the admin creation page.

Once the admin account is created, we can then go to the “Manage code hosts” page and add a “Generic Git Host”. Our src-expose command will need to still be running.

This is the config that should be added.

{
    "url": "http://192.168.13.12:3434",
    "repos": ["src-expose"]
}

With that we should be good to go! We should now be able to go to the main sourcegraph page and see a search bar and begin searching and viewing the files.

Now that sourcegraph is working, we need to push src-expose to the background. To do this, I wrote a yaml config file to specify the directories to serve and a systemd unit file.

The yaml file, this goes in /root/src-expose-config.yaml:

root: /home/

destination: /root/.sourcegraph/src-expose-repos

dirs:
    - dir: ./nivethan/projects/project1
    - dir: ./nivethan/projects/project2
    - dir: ./nivethan/projects/project3

The unit file, this goes in /etc/systemd/system/src-expose.service:

[Unit]
Description=src-expose Daemon
After=network.target

[Service]
ExecStart=/usr/local/bin/src-expose -config /root/src-expose-config.yaml
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Now we can do:

systemctl start src-expose.service
systemctl enable src-expose.service

Now both sourcegraph and src-expose are running in the background and will start up when the machine reboots.

To add a directory to sourcegraph, you will need to update the yaml file and the restart the service. Then in the sourcegraph admin you will need to trigger a manual sync under “Manage code hosts”.

You will also need to set up sourcegraph to be able to send out password resets so it will need access to a smtp server.

This configuration can be found under Site Admin -> Site Configuration.

{
    // The externally accessible URL for Sourcegraph (i.e., what you type into your browser)
    // This is required to be configured for Sourcegraph to work correctly.
    // "externalURL": "https://sourcegraph.example.com",
    "externalURL": "http://my.ip.address:7080",
    "auth.providers": [
        {
            "allowSignup": true,
            "type": "builtin"
        }
    ],
    "disablePublicRepoRedirects": true,
    "search.index.enabled": true,
    "email.address": "noreply@example.com",
    "email.smtp": {
        "authentication": "PLAIN",
        "username": "REDACTED",
        "password": "REDACTED",
        "host": "192.168.18.14",
        "port": 25
    },
}