Search code examples
gitbitbucket-serverbitbucket-api

How can I detect if a GIT server is active?


Server: Atlassian Bitbucket v4.14.3 (aka Stash) Client: git version 2.17.1 (on Linux Ubuntu 18LTS)

What is the best way to detect whether the GIT server is alive and providing service?

Use case1: On a daily basis, the Bitbucket server goes down to perform a full backup. During the backup period, it doesn't respond to requests, and any 'git clone and git push' command fails (we issue those commands from scripts). Before issuing a 'clone' or a 'push' , I'd like to know whether the GIT server is in production to avoid an error.

Use case2: Once in a while, the Bitbucket server is brought down (for upgrade, some maintenance, etc). Similar as the situation above, I'd like to know whether it is in production before issuing a 'clone' or ' push' to avoid an error.

Does Bitbucket server have some API to check that? Does the git client have a command to check that? (I'd prefer this option , to avoid dependence on the server)

I've tried 'git ls-remote -h ssh:/reponame --exit-code' , but I'd like to know whether there is a better option where I don't have to provide a repository name.


Solution

  • Here's how I ended up handling it (note that as per comment bellow, the best way is checking for these errors after issuing the desired git command in order to make it an atomic operation):

    1- Execute 'git ls-remote' with a repository name , and detect if it returns a 0 (no issue), or something else. The 'something else' value I've got is 128.

    2a- If 'ls-remote' returned something other than zero, make sure that the server is reachable. Look for 'Git server not found:'

    3b- elif, check if the GIT server is in down in maintenance mode. Since my server is Bitbucket, I look for 'Bitbucket is currently unavailable'.

    3c- elif, check if GIT server is rebooting. I get 'Connection refused' during the reboot.

    3d- elif, confirm that the repository exists. An stderr with 'Repository not found' gives a hint.

    3- Execute the desired git command (and check for errors afterwards).