-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup_puppetserver.sh
executable file
·270 lines (221 loc) · 6.67 KB
/
setup_puppetserver.sh
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/bin/sh
set -ex
repourl=$1
certname=${2:-$(hostname --fqdn)}
cflocation=$3
cflocationpool=$4
http_proxy=${5:-$http_proxy}
autosign=${INSANE_PUPPET_AUTOSIGN:-false}
if test -z "$repourl"; then
echo "Usage: $0 <r10k_repo_url> [<certname=hostname> [<cflocation> [<cflocationpool> [<http_proxy=$http_proxy>] ] ] ]"
exit 1
fi
if test "$(id -un)" != 'root'; then
echo 'This script must run as root'
exit 1
fi
if test ! -z "$cflocation"; then
echo -n $cflocation >/etc/cflocation
fi
if test ! -z "$cflocationpool"; then
echo -n $cflocationpool >/etc/cflocationpool
fi
if test ! -z "$http_proxy"; then
export http_proxy
export https_proxy="$http_proxy"
export HTTP_PROXY="$http_proxy"
export HTTPS_PROXY="$http_proxy"
fi
echo $certname > /etc/hostname
hostname $certname
if ! which lsb-release >/dev/null; then
apt-get install -y lsb-release
fi
export DEBIAN_FRONTEND=noninteractive
codename=$(lsb_release -cs)
if test -z "$codename"; then
echo "Failed to detect correct codename"
exit 1
fi
puppetlabs_deb="puppet5-release-${codename}.deb"
echo "Retrieving $puppetlabs_deb"
if ! wget -q https://apt.puppetlabs.com/$puppetlabs_deb; then
case "$(lsb_release -is)" in
Debian) codename='jessie';;
Ubuntu) codename='bionic';;
esac
puppetlabs_deb="puppet5-release-${codename}.deb"
echo "Re-retrieving $puppetlabs_deb"
wget -q https://apt.puppetlabs.com/$puppetlabs_deb || (
echo "Failed to retrieve puppetlabs release for \\\${codename}";
exit 1
)
fi
echo "Installing $puppetlabs_deb"
dpkg -i $puppetlabs_deb
mkdir -p /etc/puppetlabs/puppet
cat >/etc/puppetlabs/puppet/puppet.conf <<EOF
[main]
client = false
certname = $certname
server = $certname
ca_server = $certname
environment = production
#environment_data_provider = hiera
[master]
vardir = /opt/puppetlabs/server/data/puppetserver
logdir = /var/log/puppetlabs/puppetserver
rundir = /var/run/puppetlabs/puppetserver
pidfile = /var/run/puppetlabs/puppetserver/puppetserver.pid
codedir = /etc/puppetlabs/code
# !!! NOT FOR PRODUCTION !!!
autosign = $autosign
EOF
cat >/etc/apt/preferences.d/puppetlabs.pref <<EOF
Package: *
Pin: origin apt.puppetlabs.com
Pin-Priority: 1001
EOF
echo "Running apt-get update ..."
apt-get update || exit 1
echo "Installing puppet-agent"
apt-get install \
-f -y \
-o Dpkg::Options::="--force-confold" \
git \
puppet-agent \
apt-transport-https \
dirmngr
totalmem=$(( $(grep MemTotal /proc/meminfo | awk '{print $2}') / 1024 ))
psmem=$(( $totalmem / 4 ))
# Setup self
#---
PUPPET=/opt/puppetlabs/bin/puppet
GEM=/opt/puppetlabs/puppet/bin/gem
echo "Disabling puppet agent"
$PUPPET resource service puppet ensure=false enable=false provider=systemd
systemctl mask puppet
echo "Installing puppetserver"
$PUPPET resource package puppetserver ensure=latest
$PUPPET resource package puppetdb-termini ensure=latest
sed -i -e "s/^.*JAVA_ARGS.*$/JAVA_ARGS=\"-Xms${psmem}m -Xmx${psmem}m\"/g" \
/etc/default/puppetserver
echo "Running puppetserver & agent to generate SSL keys for PuppetDB"
$PUPPET resource service puppetserver ensure=running enable=true provider=systemd
$PUPPET resource host $certname ip=$(/opt/puppetlabs/bin/facter networking.ip)
$PUPPET agent --test --trace
# Setup postgres
#---
echo "Installing postgresql"
$PUPPET resource package postgresql ensure=latest
$PUPPET resource package postgresql-contrib ensure=latest
echo "Configuring postgresql"
sudo -u postgres createuser -DRS puppetdb
sudo -u postgres psql -c "ALTER USER puppetdb WITH PASSWORD 'puppetdb';"
sudo -u postgres createdb --locale=en_US.utf8 -E UTF8 -O puppetdb -T template0 puppetdb
sudo -u postgres psql puppetdb -c 'create extension pg_trgm'
for f in /etc/postgresql/*/main/pg_hba.conf; do
cat >$f <<EOCONF
# TYPE DATABASE USER CIDR-ADDRESS METHOD
local all all md5
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
EOCONF
done
sed -i -e "s/^.*shared_buffers.*$/shared_buffers = ${psmem}MB/g" \
/etc/postgresql/*/main/postgresql.conf
systemctl restart postgresql
# Setup puppet DB
#---
echo "Installing puppetdb"
$PUPPET resource package puppetdb ensure=latest
sed -i -e "s/^.*JAVA_ARGS.*$/JAVA_ARGS=\"-Xms${psmem}m -Xmx${psmem}m\"/g" \
/etc/default/puppetdb
cat >/etc/puppetlabs/puppetdb/conf.d/database.ini <<EOCONF
[database]
classname = org.postgresql.Driver
subprotocol = postgresql
subname = //localhost:5432/puppetdb
username = puppetdb
password = puppetdb
log-slow-statements = 10
EOCONF
$PUPPET resource service puppetdb ensure=running enable=true provider=systemd
systemctl restart puppetdb
# Connect PuppetMaster to PuppetDB
cat >>/etc/puppetlabs/puppet/puppet.conf <<EOCONF
# puppetdb-related
storeconfigs = true
storeconfigs_backend = puppetdb
reports = puppetdb
# TO BE OVERWRITTEN
EOCONF
cat >/etc/puppetlabs/puppet/puppetdb.conf <<EOCONF
[main]
server_urls = https://$certname:8081
# TO BE OVERWRITTEN
EOCONF
cat >/etc/puppetlabs/puppet/routes.yaml <<EOCONF
---
master:
facts:
terminus: puppetdb
cache: yaml
# TO BE OVERWRITTEN
EOCONF
echo puppetlabs/code >>/etc/.gitignore
cat >/etc/puppetlabs/code/hiera.yaml <<EOCONF
---
version: 5
hierarchy:
- name: Common
path: common.yaml
defaults:
data_hash: yaml_data
datadir: data
# TO BE OVERWRITTEN
EOCONF
mkdir -p /etc/puppetlabs/code/hieradata
cat >/etc/puppetlabs/code/hieradata/global.yaml <<EOCONF
---
{}
# TO BE OVERWRITTEN
EOCONF
chown -R puppet:puppet `$PUPPET config print confdir`
chown -R puppet:puppet /etc/puppetlabs/code
echo "Enabling puppetdb & puppetserver services"
systemctl enable puppetdb puppetserver
# r10k
#----
echo "Installing r10k"
mkdir -p /etc/puppetlabs/r10k/
cat >/etc/puppetlabs/r10k/r10k.yaml <<EOCONF
# The location to use for storing cached Git repos
:cachedir: '/opt/puppetlabs/r10k/cache'
# A list of git repositories to create
:sources:
:conf:
remote: '$repourl'
basedir: '/etc/puppetlabs/code/environments'
# TO BE OVERWRITTEN
EOCONF
mkdir -p /opt/codingfuture/bin
cat >/opt/codingfuture/bin/cf_r10k_deploy <<EOCONF
#!/bin/bash
export PATH="/opt/puppetlabs/bin/:/opt/puppetlabs/puppet/bin/:\$PATH"
/opt/puppetlabs/puppet/bin/r10k deploy environment
for penv in /etc/puppetlabs/code/environments/*; do
pushd \$penv >/dev/null
/opt/puppetlabs/puppet/bin/librarian-puppet install
popd >/dev/null
done
chown -R puppet:puppet /etc/puppetlabs/code/environments/
# TO BE OVERWRITTEN
EOCONF
chmod 750 /opt/codingfuture/bin/cf_r10k_deploy
$GEM install r10k
echo "Installing librarian-puppet"
$GEM install activesupport --version '<5'
$GEM install librarian-puppet
echo "Restarting puppetdb & puppetserver"
systemctl restart puppetdb puppetserver