Perforce and CentOS 7
Today I had a lot of fun getting my Perforce repository move to a new CentOS 7 server. Moving the data was easy but getting it to run as a service was a pain. This is mainly because the init scripts are no longer used and now the new systemctl system has taken over. I did some searching for answers on Google and Bing but nothing really was found for this specific problem. I guess it is still fairly new and many admins have opted not to migrate to the latest greatest. A similar issue when Linux started using the selinux system and many of my services wouldn't load because they hadn’t been added to the security footprint.
Anyways I digress. Today I wanted to discuss the new systemctl features of CentOS 7 and many new flavors of Linux. I took my simple init script today and even made it smaller with the new systemctl. The first thing to note is that systemctl is not a bash script anymore which makes it a lot easier to work on. The good part of it is the config files are a lot lighter now because there is no need to add custom logic into the file like the init scripts had.
The first section of the script is more Meta data which describes our service. Here is what I created for the Perforce daemon script:
...Unit...
Description=Perforce Server
After=network.target
NOTE: ... is a place holder for brackets because my CRM crashes when I use real brackets...
This section is called the Unit since the systemctl can do a lot more then handle daemons. So in the description I just put in that this was my Perforce Server. You can say anything you want here. The after part tells the system that this service requires that the network be up and running. Not that exciting but it does help reduce the size of our config file.
Here is where the fun begins. The service section tells the systemctl what kind of service it is and how to start it. Here is what I finally added for my portions of the config file:
...Service...
Type=basic
ExecStart=/usr/p4d/bin/p4d -J $P4JOURNAL -p $P4PORT -r $P4ROOT -L $P4LOGS
ExecReload=/usr/p4d/bin/p4 admin restart
ExecStop=/usr/p4d/bin/p4 admin stop
The type is an interesting setting and it is still a little vague what values are accepted here. For now I've been using basic but on the httpd server is set as "notify". I'm sure some research is needed as to the different types and why/when we would use something different in there. The sad thing is I did do some research on this topic and the documentation so far is a little light on the internet. Hopefully this will change as more sysadmins use this new feature.
The real important parts is the ExecStart, ExecReload, and ExecStop variables. These define the different actions you will perform on this service. Hopefully these are easy enough to understand since they are direct commands you enter into the systemctl command. The diffuclt part of the config file was getting the ExecStart command to work properly. The values as you can see are environment variables and hte new config file doesn't allow for direct export of variables. There are a couple of solutions but the one I felt was the best answer for today was to create a new directory called p4d.service.d and add a new file env.conf to it. In there I added these variables to the file:
...Service...
Environment="P4ROOT=/usr/p4d/data"
Environment="P4PORT=ssl::3435"
Environment="P4SSLDIR=/usr/p4d/ssl"
Environment="P4JOURNAL=/usr/p4d/journal/entries"
Environment="P4LOGS=/usr/p4d/logs/p4err"
These automatically get loaded when the command is processed. We could of also just added these to the original p4d.service file but I felt breaking up the script would keep it clean and also be the proper method.
Also note that the '&' character at the end of the command will cause p4d to crash and report an error. In the journalctl log it showed that p4d had a unsupported parameter. Once I removed this character everything started working as I expected.
That is it to the conversion process. Once I got all the right information into the proper places and the syntax correct the systemctl worked perfectly on my Perforce server.