Saturday, September 08, 2007

Setting Up a Ubuntu Perforce Server

Setting Up a Ubuntu Perforce Server

This is a step-by-step guide to installing and configuring Perforce on a Ubuntu server. It assumes that you have already created a Basic Ubuntu Server. There are several sections below:

  • Download Perforce
  • Configure Perforce
  • Run Perforce Server For the First Time
  • Setup Perforce As Bootup Service
  • ToDo
  • Resources

Download Perforce

  1. Use Lynx to download the P4D software.
      lynx http://www.perforce.com/perforce/downloads/linux26x86.html
    
  2. Enter Y to allow the cookie from Perforce.com.
  3. Press the Down Arrow until the Downloads link is selected. Then press Enter.
  4. Press the Down Arrow until the Linux Kernel 2.6 for x86 link is selected. Then press Enter.
  5. Press the Down Arrow until the Download link is selected. Then press Enter.
  6. Press D to start the download.
  7. Press the Down Arrow to highlight the ''Save to disk'' link. Then press Enter.
  8. Use the default filename of `p4d`.
  9. Press q, then press Enter to exit Lynx.
  10. Use Lynx to download the P4 software by retrace the steps above expect selecting to download `P4` instead of `P4D`.

Configure Perforce

This information is a reprise of the [http://www.perforce.com/perforce/doc.072/manuals/p4sag/index.html Perforce Administration Guide] along with some additional information.
  1. Download the `daemon` utility. This utility allows `p4d` to be run by the `perforce` user instead of `root`.
      sudo apt-get daemon
    
  2. Connect to the directory where the `p4` and `p4d` files where downloaded (probably the home directory).
      cd ~
    
  3. Make the `p4` and `p4d` files executable.
      chmod +x p4 p4d
    
  4. Copy the Perforce executables to `/usr/local/bin` which is already in the system PATH environment variable.
      sudo mv p4 /usr/local/bin
      sudo mv p4d /usr/local/bin
    
  5. Create a group for Perforce files.
      sudo addgroup p4admin
    
  6. Create a user for Perforce administrative work.
      sudo adduser perforce
    
  7. Use `visudo` to give the perforce user account the ability to use `sudo`. Add the following line at the end o f the file.
      perforce ALL = ALL
    
  8. Log off your default user account.
  9. Log in using the `perforce` account.
  10. Create a directory to hold the repository.
      sudo mkdir /perforce_depot
      sudo chown perforce:p4admin /perforce_depot  
    
  11. Create a directory to hold Perforce log files.
      sudo mkdir /var/log/perforce
      sudo chown perforce:p4admin /var/log/perforce
    
  12. Add the following lines to the end of /etc/profile. These settings will be used by client programs run on the Linux server - not by the Perforce server.
      # Perforce Settings
      export P4JOURNAL=/var/log/perforce/journal
      export P4LOG=/var/log/perforce/p4err
      export P4PORT=localhost:1666
      export P4ROOT=/perforce_depot
      export P4USER=perforce
    
  13. Load the Perforce settings.
      source /etc/profile
    
  14. Create a Perforce group to limit resource usage using `sudo p4 group developers` to set the following values. See http://kb.perforce.com/P4dServerReference/Performance/ViewsProtect..Maxlocktime for more information.
      Group: developers
      MaxResults:  50000
      MaxScanRows: 250000
      MaxLocktime: 30000
      Timeout:     4320
      Subgroups:
      Users:
        developer
    

Setup Perforce As Bootup Service

  1. Connect to the initialization control directory.
      cd /etc/init.d
    
  2. Create the Perforce control script using `sudo vi perforce`.
      #!/bin/sh -e
    
      export P4JOURNAL=/var/log/perforce/journal
      export P4LOG=/var/log/perforce/p4err
      export P4ROOT=/perforce_depot
      export P4PORT=1666
    
      PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
    
      . /lib/lsb/init-functions
    
      p4start="p4d -d"
      p4stop="p4 admin stop"
      p4user=perforce
    
      case "$1" in
      start)
        log_action_begin_msg "Starting Perforce Server"
        daemon -u $p4user $p4start;
        ;;
    
      stop)
        log_action_begin_msg "Stopping Perforce Server"
        daemon -u $p4user $p4stop;
        ;;
    
      restart)
        stop
        start
        ;;
    
      *)
        echo "Usage: /etc/init.d/perforce (start|stop|restart)
        exit 1
        ;;
    
    esac
    
    exit 0
    
  3. Integrate the Perforce control script into the boot process.
      sudo update-rc.d perforce defaults
    
  4. Goto your home directory then use the control script to start the Perforce server.
      cd ~
      sudo /etc/init.d/perforce start
    
  5. Use the control script to restart the Perforce server.
      sudo /etc/init.d/perforce restart
    

Run Perforce Server For the First Time

Before Perforce can be used by a team there are two housekeeping task that need to be done - creating the journal and closing a security hole.
  1. Set the Perforce environment variables.
      source /etc/profile
    
  2. Start the perforce server. The command line is short because all of the settings are provided by the variables exported by /etc/profile.
      sudo p4d –d
    
  3. Create the journal.
      sudo p4d –jc
    
  4. By default, all users connected to Perforce are administrators. However, that level of security is not acceptable for a normal development environment. Run the `protect` option to switch out of the default security mode. For now, accept the default protections by typing `:wq` and pressing Enter.
      sudo p4 protect
    
  5. Stop the Perforce server.
      sudo p4 admin stop
    

TODO

* Journalling
* checkpointing
* backups

Resources

* Perforce's public depot
* http://kb.perforce.com/AdminTasks/InstallAndUpgrade/RunningAPerf..InetdOnUnix How to start `p4d` via `inetd` instead of a startup script.

2 comments:

DyersGreen said...

I think there are a couple of minor errors in the otherwise excellent guide -

1) 'sudo apt-get daemon' should read 'sudo apt-get install daemon'

2) There's a missing trailing " in the Perforce script on the line -

echo "Usage: /etc/init.d/perforce (start|stop|restart)

Cheers,
Bill.

ozark said...

Thanks! This setup guide was EXACTLY what I was looking for. I wrote this auto-installer based on your guide.

#!/bin/bash

# Make sure we are root
if [[ `whoami` != 'root' ]]
then
echo "Run this installer as root: sudo $0"
exit 1
fi

lin=`uname -a | cut -d ' ' -f 3 | cut -b 3`
if [ "$lin" != "6" ]
then
echo "Dun Dun Daaah!! We're fetching p4d for kernel 2.6 but..."
echo "you're running something else! Got minor version $lin, was"
echo "ignorantly assuming major number 2 (always) and minor number"
echo "6. Look at the p4d and p4 fetch routines to see where they"
echo "need to be changed."
exit 121
fi

if [[ ! -e p4 ]] || [[ ! -e p4d ]]
then
rm -f p4 p4d
perl <<'PERLSCRIPT'
use strict;
use LWP::UserAgent;

my ($wget, $ua, $res, $content);

$wget = '/usr/bin/wget';
$ua = new LWP::UserAgent('agent' => 'w3m/0.5.1');

$ua->timeout(10);
$content = '';

$res = $ua->get('http://www.perforce.com/perforce/downloads/linux26x86.html');
if ($res->is_success) {
$content = $res->content;
} else {
die $res->status_line;
}

my ($p4, $p4d) = ('', '');

if ($content =~ m/href="([^"]+p4)"/) {
$p4 = $1;
}
if ($content =~ m/href="([^"]+p4d)"/) {
$p4d = $1;
}

print "p4 : $p4\n";
print "p4d: $p4d\n";

if ($p4 =~ m/^\s*$/ or $p4d =~ m/^\s*$/) {
print "Failed parsing p4 or p4d from page!\n";
exit 223;
}

system($wget, $p4);
system($wget, $p4d);
PERLSCRIPT
fi

if [[ ! -e p4 ]] || [[ ! -e p4d ]]
then
echo "Error: Failed getting p4 or p4d! Giving up!"
exit 122
fi

apt-get install daemon

chmod +x p4 p4d
cp p4 p4d /usr/bin

# Now add users/groups
groupadd p4g
useradd -g p4g -d /home/perforce p4u
mkdir /home/perforce
chown -R p4u.p4g /home/perforce
chmod 700 /home/perforce
mkdir /var/log/perforce
chown -R p4u.p4g /var/log/perforce

cat >/etc/init.d/perforce <<'PERFORCEEND'
#!/bin/sh -e

export P4JOURNAL=/var/log/perforce/journal
export P4LOG=/var/log/perforce/p4err
export P4ROOT=/home/perforce
export P4PORT=1666

PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"

. /lib/lsb/init-functions

p4start="p4d -d"
p4stop="p4 admin stop"
p4user=p4u

case "$1" in
start)
log_action_begin_msg "Starting Perforce Server"
daemon -u $p4user $p4start;
;;

stop)
log_action_begin_msg "Stopping Perforce Server"
daemon -u $p4user $p4stop;
;;

restart)
stop
start
;;

*)
echo "Usage: /etc/init.d/perforce (start|stop|restart)"
exit 1
;;

esac
exit 0
PERFORCEEND
chmod +x /etc/init.d/perforce

update-rc.d perforce defaults

echo 'Perforce installed with the depot in /home/perforce'
echo 'Environment variables are:'
echo ''
echo 'export P4JOURNAL=/var/log/perforce/journal'
echo 'export P4LOG=/var/log/perforce/p4err'
echo 'export P4ROOT=/home/perforce'
echo 'export P4PORT=1666'
echo ''
echo 'Adding them to /etc/profile...'

cat >>/etc/profile <<P4VARSEND

export P4JOURNAL=/var/log/perforce/journal
export P4LOG=/var/log/perforce/p4err
export P4ROOT=/home/perforce
export P4PORT=1666
P4VARSEND