Skip to content
This repository has been archived by the owner on May 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #14 from barrowkwan/master
Browse files Browse the repository at this point in the history
Build RPM installer
  • Loading branch information
wpc committed Jun 1, 2016
2 parents e64d49d + 69b5355 commit 32f5842
Show file tree
Hide file tree
Showing 7 changed files with 367 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -3,3 +3,5 @@
/installers/deb/package/usr/bin/gocd-golang-agent
/gocd-golang-agent
/testreport.xml
.idea/
*.iml
64 changes: 64 additions & 0 deletions installers/rpm/agent.sh
@@ -0,0 +1,64 @@
#!/bin/bash
#*************************GO-LICENSE-START********************************
# Copyright 2016 ThoughtWorks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#*************************GO-LICENSE-END**********************************


SERVICE_NAME=${1:-gocd-golang-agent}

if [ -f /etc/default/${SERVICE_NAME} ]; then
echo "[`date`] using default settings from /etc/default/${SERVICE_NAME}"
. /etc/default/${SERVICE_NAME}
fi

CWD=`dirname "$0"`

GOCD_SERVER_URL=${GOCD_SERVER_URL:-"https://127.0.0.1:8154/go"}
VNC=${VNC:-"N"}
GOCD_AGENT_WORK_DIR=${GOCD_AGENT_WORK_DIR:-"/var/lib/gocd-golang-agent"}

if [ ! -d "${GOCD_AGENT_WORK_DIR}" ]; then
echo Agent working directory ${GOCD_AGENT_WORK_DIR} does not exist
exit 2
fi

GOCD_AGENT_LOG_DIR=/var/log/${SERVICE_NAME}

STDOUT_LOG_FILE=$GOCD_AGENT_LOG_DIR/${SERVICE_NAME}.log

PID_FILE="$GOCD_AGENT_WORK_DIR/gocd-golang-agent.pid"


if [ "$VNC" == "Y" ]; then
echo "[`date`] Starting up VNC on :3"
/usr/bin/vncserver :3
DISPLAY=:3
export DISPLAY
fi

export GOCD_AGENT_LOG_DIR
export LOG_FILE

CMD="/opt/${SERVICE_NAME}/bin/${SERVICE_NAME}"

cd "$GOCD_AGENT_WORK_DIR"

if [ "$DAEMON" == "Y" ]; then
eval exec nohup "$CMD" >>"$STDOUT_LOG_FILE" 2>&1 &
echo $! >"$PID_FILE"
else
eval exec "$CMD"
fi

87 changes: 87 additions & 0 deletions installers/rpm/build.sh
@@ -0,0 +1,87 @@
#!/bin/bash
#
# Copyright 2016 ThoughtWorks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
set -e
# set -x
SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )"
. $SCRIPT_DIR/vars
PROJECT_DIR="${SCRIPT_DIR}/../../"
RPM_BUILD_DIR="${PROJECT_DIR}/tmp_build"
export GOPATH=$PROJECT_DIR

go get golang.org/x/net/websocket
go get github.com/satori/go.uuid
go get github.com/xli/assert
go get github.com/bmatcuk/doublestar
go get github.com/jstemmer/go-junit-report
mkdir -p $PROJECT_DIR/src/github.com/gocd-contrib/gocd-golang-agent/
if [ ! -d $PROJECT_DIR/src/github.com/gocd-contrib/gocd-golang-agent/agent ]; then
ln -s $PROJECT_DIR/agent $PROJECT_DIR/src/github.com/gocd-contrib/gocd-golang-agent/agent
fi
if [ ! -d $PROJECT_DIR/src/github.com/gocd-contrib/gocd-golang-agent/junit ]; then
ln -s $PROJECT_DIR/junit $PROJECT_DIR/src/github.com/gocd-contrib/gocd-golang-agent/junit
fi
if [ ! -d $PROJECT_DIR/src/github.com/gocd-contrib/gocd-golang-agent/protocol ]; then
ln -s $PROJECT_DIR/protocol $PROJECT_DIR/src/github.com/gocd-contrib/gocd-golang-agent/protocol
fi
if [ ! -d $PROJECT_DIR/src/github.com/gocd-contrib/gocd-golang-agent/stream ]; then
ln -s $PROJECT_DIR/stream $PROJECT_DIR/src/github.com/gocd-contrib/gocd-golang-agent/stream
fi

echo "################################"
echo "Create Temporary Build Structure"
echo "################################"
mkdir -p $RPM_BUILD_DIR/gocd-golang-agent-$GGA_VERSION/opt/gocd-golang-agent/bin
mkdir -p $RPM_BUILD_DIR/gocd-golang-agent-$GGA_VERSION/etc/init.d
mkdir -p $RPM_BUILD_DIR/gocd-golang-agent-$GGA_VERSION/etc/default
mkdir -p $RPM_BUILD_DIR/BUILD
mkdir -p $RPM_BUILD_DIR/BUILDROOT
mkdir -p $RPM_BUILD_DIR/RPMS
mkdir -p $RPM_BUILD_DIR/SOURCES
mkdir -p $RPM_BUILD_DIR/SPECS
mkdir -p $RPM_BUILD_DIR/SRPMS
mkdir -p $PROJECT_DIR/build_installers

echo "############################"
echo "Cross compiling for linux..."
echo "############################"

cd $PROJECT_DIR
CGO_ENABLED=0 GOOS=linux go build -a -tags netgo -ldflags '-w' .
chmod 0755 gocd-golang-agent
cp gocd-golang-agent $RPM_BUILD_DIR/gocd-golang-agent-$GGA_VERSION/opt/gocd-golang-agent/bin

echo "############################"
echo "Packaging rpm source file..."
echo "############################"
cd $SCRIPT_DIR
install -m 755 agent.sh $RPM_BUILD_DIR/gocd-golang-agent-$GGA_VERSION/opt/gocd-golang-agent/bin
install -m 755 gocd-golang-agent_default $RPM_BUILD_DIR/gocd-golang-agent-$GGA_VERSION/etc/default/gocd-golang-agent
install -m 755 gocd-golang-agent_init.d $RPM_BUILD_DIR/gocd-golang-agent-$GGA_VERSION/etc/init.d/gocd-golang-agent
cat << EOF > $RPM_BUILD_DIR/gocd-golang-agent-$GGA_VERSION/etc/default/gocd-golang-agent
export GOCD_SERVER_URL=https://localhost:8154/go
export GOCD_AGENT_WORKING_DIR=/var/lib/gocd-golang-agent
export GOCD_AGENT_LOG_DIR=/var/log/gocd-golang-agent
EOF
cd $RPM_BUILD_DIR
tar zcvf gocd-golang-agent-$GGA_VERSION.tar.gz gocd-golang-agent-$GGA_VERSION
mv gocd-golang-agent-$GGA_VERSION.tar.gz $RPM_BUILD_DIR/SOURCES
cp $SCRIPT_DIR/gocd-golang-agent.spec $RPM_BUILD_DIR/SPECS
rpmbuild -bb --define '_topdir '$RPM_BUILD_DIR $RPM_BUILD_DIR/SPECS/gocd-golang-agent.spec
mv $RPM_BUILD_DIR/RPMS/`uname -m`/gocd-golang-agent-$GGA_VERSION-$GGA_RPM_VERSION.`uname -m`.rpm $PROJECT_DIR/build_installers
rm -rf $RPM_BUILD_DIR


59 changes: 59 additions & 0 deletions installers/rpm/gocd-golang-agent.spec
@@ -0,0 +1,59 @@
Name: gocd-golang-agent
Version: 0.1
Release: 1
Summary: GoCD Golang Agent
License: Apache License Version 2.0
URL: https://github.com/gocd-contrib/gocd-golang-agent
SOURCE: gocd-golang-agent-0.1.tar.gz

%description
GoCD Golang Agent

%prep
%setup

%install
rm -rf "$RPM_BUILD_ROOT"
mkdir -p "$RPM_BUILD_ROOT/opt/%{name}/bin"
mkdir -p "$RPM_BUILD_ROOT/etc/init.d"
mkdir -p "$RPM_BUILD_ROOT/etc/default"
mkdir -p "$RPM_BUILD_ROOT/var/log/%{name}"
mkdir -p "$RPM_BUILD_ROOT/var/lib/%{name}"
cp opt/%{name}/bin/agent.sh "$RPM_BUILD_ROOT/opt/%{name}/bin"
cp opt/%{name}/bin/gocd-golang-agent "$RPM_BUILD_ROOT/opt/%{name}/bin"
cp etc/init.d/gocd-golang-agent "$RPM_BUILD_ROOT/etc/init.d"
cp etc/default/gocd-golang-agent "$RPM_BUILD_ROOT/etc/default"

%pre
getent group go >/dev/null || groupadd go
getent passwd go >/dev/null || \
useradd -g go -d /var/go -s /bin/bash \
-c "GoCD Golang Agent" go
mkdir -p /var/lib/%{name}
mkdir -p /var/log/%{name}

%preun
service %{name} stop

%postun
rm -rf /var/log/%{name}
rm -rf /var/lib/%{name}

%files
/opt/%{name}
/etc/init.d/gocd-golang-agent
/etc/default/gocd-golang-agent

%attr(755, root, root) /etc/init.d/gocd-golang-agent
%attr(644, go, go) /etc/default/gocd-golang-agent
%attr(755, go, go) /opt/%{name}/bin/gocd-golang-agent
%attr(755, go, go) /opt/%{name}/bin/agent.sh
%attr(755, go, go) /var/log/%{name}
%attr(755, go, go) /var/lib/%{name}

%clean
rm -rf "$RPM_BUILD_ROOT"

%changelog
* Fri May 27 2016 Barrow Kwan <bhkwan@thoughtworks.com> 1.0-1
- First Build
3 changes: 3 additions & 0 deletions installers/rpm/gocd-golang-agent_default
@@ -0,0 +1,3 @@
export GOCD_SERVER_URL=https://localhost:8154/go
export GOCD_AGENT_WORKING_DIR=/var/lib/gocd-golang-agent
export GOCD_AGENT_LOG_DIR=/var/log/gocd-golang-agent
150 changes: 150 additions & 0 deletions installers/rpm/gocd-golang-agent_init.d
@@ -0,0 +1,150 @@
#!/bin/bash
#*************************GO-LICENSE-START********************************
# Copyright 2014 ThoughtWorks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#*************************GO-LICENSE-END**********************************

# chkconfig: 2345 90 90
# description: GoCD Golang Agent
# processname: java
### BEGIN INIT INFO
# Provides: gocd-golang-agent
# Required-Start: $network $remote_fs
# Required-Stop: $network $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Start the GoCD Golang Agent
### END INIT INFO
SERVICE_NAME=${0##*/}

# Strips initV leading chars if the script is located in rcX.d (automatically executed on runlevel change)
INIT_DIR=${0%/*}; echo ${INIT_DIR##*/} | grep -e '^rc[0-9]\.d$' >/dev/null && SERVICE_NAME=$(echo "${SERVICE_NAME}" | sed 's/^[SK][0-9][0-9]//');

PID_FILE="/var/run/${SERVICE_NAME}/${SERVICE_NAME}.pid"
CUR_USER=`whoami`

# LSB implimentation is not standard across distros, so we have to roll our own...
killproc() {
pkill -u go -f /opt/${SERVICE_NAME}/bin/${SERVICE_NAME}
}

start_daemon() {
eval "$@"
}

log_success_msg() {
echo "$@"
}

log_failure_msg() {
echo "$@"
}

. /etc/default/${SERVICE_NAME}

check_proc() {
pgrep -u go -f /opt/${SERVICE_NAME}/bin/${SERVICE_NAME} >/dev/null
}

start_go_agent() {
if [ "${CUR_USER}" != "root" ] && [ "${CUR_USER}" != "go" ]; then
log_failure_msg "Go Agent can only be started as 'root' or 'go' user."
exit 4
fi

check_proc
if [ $? -eq 0 ]; then
log_success_msg "Go Agent already running."
exit 0
fi

[ -d /var/run/${SERVICE_NAME} ] || (mkdir /var/run/${SERVICE_NAME} && chown -R go:go /var/run/${SERVICE_NAME})

if [ "${CUR_USER}" == "root" ]; then
start_daemon "/bin/su - go -c '/opt/${SERVICE_NAME}/bin/agent.sh'"
else
start_daemon /opt/${SERVICE_NAME}/bin/agent.sh ${SERVICE_NAME}
fi

# Sleep for a while to see if Cruise bleats about anything
sleep 15
check_proc

if [ $? -eq 0 ]; then
log_success_msg "Started GoCD Golang Agent."
else
log_failure_msg "Error starting GoCD Golang Agent."
exit -1
fi
}

stop_go_agent() {
if [ "${CUR_USER}" != "root" ] && [ "${CUR_USER}" != "go" ]; then
log_failure_msg "You do not have permission to stop the GoCD Golang Agent"
exit 4
fi

check_proc

if [ $? -eq 0 ]; then
killproc -p $PID_FILE /opt/${SERVICE_NAME}/bin/agent.sh >/dev/null

# Make sure it's dead before we return
until [ $? -ne 0 ]; do
sleep 1
check_proc
done

check_proc
if [ $? -eq 0 ]; then
log_failure_msg "Error stopping Go Agent."
exit -1
else
log_success_msg "Stopped GoCD Golang Agent."
fi
else
log_failure_msg "Go is not running or you don't have permission to stop it"
fi
}

go_status() {
check_proc
if [ $? -eq 0 ]; then
log_success_msg "GoCD Golang Agent is running."
else
log_failure_msg "GoCD Golang Agent is stopped."
exit 3
fi
}

case "$1" in
start)
start_go_agent
;;
stop)
stop_go_agent
;;
restart)
stop_go_agent
start_go_agent
;;
status)
go_status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac

exit 0
2 changes: 2 additions & 0 deletions installers/rpm/vars
@@ -0,0 +1,2 @@
export GGA_VERSION=0.1
export GGA_RPM_VERSION=1

0 comments on commit 32f5842

Please sign in to comment.