aboutsummaryrefslogtreecommitdiffstats
path: root/ci.d/buildkite
blob: bade98684c6ae2e88922d5b72d564da785553305 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env sh

_buildkite_config() {
	BUILDKITE_PATH=/home/buildkite-agent/.buildkite-agent
	if [ "$1" = 'ubuntu' -o "$1" = 'debian' -o "$1" = 'centos' ]
	then
		BUILDKITE_PATH=/etc/buildkite-agent
	fi
	# Update the configuration.
	lxc_do bash -c "echo name=\"$1-%n\" >> $BUILDKITE_PATH/buildkite-agent.cfg"
	lxc_do bash -c "echo tags=\"os=$1\" >> $BUILDKITE_PATH/buildkite-agent.cfg"
	# Generate buildkite-env file.
	lxc_do bash -c " \
		echo \"export BUILDKITE_API_TOKEN=$BUILDKITE_API_TOKEN\" >> /etc/buildkite-env"
	lxc_do bash -c " \
		echo \"export BUILDKITE_LOGS_SERVER=$BUILDKITE_LOGS_SERVER\" >> /etc/buildkite-env"
	# Install post-checkout hook.
	<$( cd "$( dirname "$0" )" && pwd )/priv/buildkite-post-checkout-hook \
		lxc_do bash -c "cat > $BUILDKITE_PATH/hooks/post-checkout"
	lxc_do chmod +x $BUILDKITE_PATH/hooks/post-checkout
	lxc_do chown buildkite-agent:buildkite-agent $BUILDKITE_PATH/hooks/post-checkout
	# Install pre-artifact hook.
	<$( cd "$( dirname "$0" )" && pwd )/priv/buildkite-pre-artifact-hook \
		lxc_do bash -c "cat > $BUILDKITE_PATH/hooks/pre-artifact"
	lxc_do chmod +x $BUILDKITE_PATH/hooks/pre-artifact
	lxc_do chown buildkite-agent:buildkite-agent $BUILDKITE_PATH/hooks/pre-artifact
}

_buildkite_install_centos() {
	lxc_do bash -c 'echo -e "[buildkite-agent]\nname = Buildkite Pty Ltd\nbaseurl = https://yum.buildkite.com/buildkite-agent/stable/x86_64/\nenabled=1\ngpgcheck=0\npriority=1" > /etc/yum.repos.d/buildkite-agent.repo'
	lxc_do yum install -y buildkite-agent
	lxc_do sed -i "s/xxx/$2/g" /etc/buildkite-agent/buildkite-agent.cfg
	lxc_do systemctl enable buildkite-agent
}

_buildkite_install_generic() {
	lxc_do useradd -m -s /bin/bash buildkite-agent
	lxc_do su - buildkite-agent -c 'curl -sLO https://raw.githubusercontent.com/buildkite/agent/master/install.sh'
	lxc_do su - buildkite-agent -c "TOKEN=\"$2\" bash install.sh"
	lxc_do su - buildkite-agent -c 'rm install.sh'
	lxc_do su - buildkite-agent -c 'echo "@reboot /home/buildkite-agent/.buildkite-agent/bin/buildkite-agent start &" >> tmpcron'
	lxc_do su - buildkite-agent -c 'crontab tmpcron'
	lxc_do su - buildkite-agent -c 'rm tmpcron'
}

_buildkite_install_ubuntu() {
	lxc_do bash -c 'echo deb https://apt.buildkite.com/buildkite-agent stable main \
		> /etc/apt/sources.list.d/buildkite-agent.list'
	lxc_do apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 \
		--recv-keys 32A37959C2FA5C3C99EFBC32A79206696452D198
	lxc_do apt-get update
	lxc_do apt-get install -y buildkite-agent
	lxc_do sed -i "s/xxx/$2/g" /etc/buildkite-agent/buildkite-agent.cfg
	lxc_do systemctl enable buildkite-agent
}

# buildkite_install dist token
buildkite_install() {
	if [ "$1" = 'ubuntu' -o "$1" = 'debian' ]
	then
		_buildkite_install_ubuntu "$@"
	elif [ "$1" = 'centos' ]
	then
		_buildkite_install_centos "$@"
	else
		_buildkite_install_generic "$@"
	fi
	_buildkite_config $1
}

# buildkite_stop dist
buildkite_stop() {
	if [ "$1" = 'ubuntu' -o "$1" = 'debian' ]
	then
		lxc_do systemctl stop buildkite-agent || true
	else
		lxc_do killall -q buildkite-agent || true
	fi
}