Sunday, June 6, 2010

Mercurial Helper Script

Explanation

This is the current version of the mercurial-helper script that I have created. The general purpose of this script is to aid users in creating and adding users to the password file hgusers (typically called that), as well as setting up a repository.

Unfortunately I have not added any functionality to remove users from the password file, though this is not a difficult task to carry out. Users should be able to change/update their passwords in the hgusers file just by re-entering their credentials. A second problem is that script requires sudo access to make sure that the directories and permissions can be altered correctly.

How to Use

Acquire the script by creating a file called mercurial-helper.sh and pasting in the following section's code into it. Then place this file where the mercurial repositories are going to be found (if the earlier blog entries were followed that would be /srv/redmine/extra/hg/). Now to execute it:
# sudo sh mercurial-script.sh

Then the script should guide you through the rest of the steps to create and add users to the password file, as well as creating a repository.

mercurial-script.sh

#!/bin/bash
#-----------------------
# Author: Kevin Jalbert
# Date: June 6th, 2010
#-----------------------

# Variables that are used
choice=4
name="----"
passwordFile="----"
realm="----"
repository="----"

# Present the user with potential options
echo "1. Add/Change User"
echo "2. Create New Repository"
echo "3. Exit"
echo -n "Please choose an option [1,2, or 3]? "

# Keep looping till a valid choice is made
while [ $choice -eq 4 ]; do

 read choice

 # Adding/changing a user
 if [ $choice -eq 1 ] ; then

  echo "--Adding/changing a user--"

  # Acquire the name
  echo "Enter the user's account name"
  read name

  # Acquire repository's realm
  echo "Enter the repository's realm (should be mercurial)"
  read realm

  # Acquire file's name
  echo "Enter name of password file (should be hgusers)"
  read passwordFile

  # Check to see if this file already exists, and perform appropriate action
  if [ -e $passwordFile ]; then
   echo `htdigest $passwordFile $realm $name`
  else
   echo `htdigest -c $passwordFile $realm $name`
  fi

  echo "User [$name] has been added/changed within password file [$passwordFile]"

 # Creating a new repository
 elif [ $choice -eq 2 ] ; then

  echo "--Creating a new repository--"

  # Acquire the repository's name
  echo "Enter the repository's name"
  read repository

  # Acquire the users involved in this repository
  echo "Enter the users involved in this repository (Comma Separated Values)"
  read name

  # Set up the repository
  echo `hg init $repository`
  echo `touch ./$repository/.hg/hgrc`
  echo "[paths]
default = $PWD/$repository/

[web]
allow_push = $name
contact = $name
description = Fill me out (in ./project/.hg/hgrc)
push_ssl = false
style = gitweb

[trusted]
users = *
groups = *

[hooks]
changegroup = hg update" > ./$repository/.hg/hgrc

  # Change the ownership permissions
  echo `chown -R www-data ./$repository/`
  echo `chmod -R 755 ./$repository`

  echo "The repository is created. The configuration file located at ./$repository/.hg/hgrc
  needs to be edited to ensure that the path to this repository is set as well as any
  additional changes that are desired."

 # Exiting
 elif [ $choice -eq 3 ] ; then

  echo "Exiting"
  exit;

 # Script input mismatch
 else

  $choice=4
  echo "Incorrect input, try again"
  echo "1. Add/Change User"
  echo "2. Create new Repository"
  echo "3. Exit"
  echo -n "Please choose an option [1,2 or 3]? "

 fi
done