-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautobootstrap.sh
executable file
·195 lines (169 loc) · 5.22 KB
/
autobootstrap.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
#!/bin/bash
##### LICENSE
# Copyright (c) Skyscrapers (iLibris bvba) 2014 - http://skyscrape.rs
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##### USAGE
# Options
#
# -r "<value>" : repo for puppet install if you want a specific version. Defaults to local apt sources. Example: -r "https://apt.puppetlabs.com/ trusty main"
# -p <value> : puppetmaster address. Defaults to localhost.
# -t <value> : timezone. For example -t "Europe/Brussels". Default = leave it be.
# -h <value> : hostname of the system. Example: -h test-webserver. Default = output of /bin/hostname.
# -f <value> : fqdn of the system. Example: -f webserver.test.domain.com. Default = output of /bin/hostname -f.
#
# Manual or as AWS EC2 UserData
#
# /bin/bash <(/usr/bin/wget -qO- http(s)://<location>/autobootstrap.sh) -r "<package repo URL and release name and section name>" -p <puppetmaster URI> -t <timezone> -h <hostname> -f <fqdn>
##### BEGIN SCRIPT
echo ""
echo "AUTO BOOTSTRAP script"
echo "---------------------"
echo ""
echo "This script will attempt to setup some basic configuration on a new Ubuntu system."
echo "apt, hostname, hosts file, timezone and Puppet agent."
echo ""
/usr/bin/logger -t autobootstrap "STARTING autobootstrap.sh"
##### PARAMS and VARS
PACKAGEREPO=
PUPPETMASTER="localhost"
TIMEZONE=
HOSTNAME=`/bin/hostname`
FQDN=`/bin/hostname -f`
while getopts :r:p:t:h:f: opt; do
case $opt in
r)
PACKAGEREPO=$OPTARG
;;
p)
PUPPETMASTER=$OPTARG
;;
t)
TIMEZONE=$OPTARG
;;
h)
HOSTNAME=$OPTARG
;;
f)
FQDN=$OPTARG
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 2
;;
esac
done
##### FUNCTIONS
# set the hostname
function sethostname {
echo -n "* Setting up hostname to $HOSTNAME"
hostname "$HOSTNAME"
echo "$HOSTNAME" > /etc/hostname
echo " - Done"
/usr/bin/logger -t autobootstrap "hostname set to $HOSTNAME"
}
# set the hosts file
function sethosts {
echo -n "* Adding host to hosts file"
echo "127.0.0.1 $FQDN $HOSTNAME" >> /etc/hosts
echo " - Done"
/usr/bin/logger -t autobootstrap "updated /etc/hosts with $HOSTNAME and $FQDN"
}
# setup apt
function setupapt {
if [ ! -z "$PACKAGEREPO" ]; then
echo "deb $PACKAGEREPO" >> /etc/apt/sources.list.d/autobootstrap.list
echo " - Done"
/usr/bin/logger -t autobootstrap "added custom apt repo to install puppet from"
echo -n "* Executing apt-get update"
apt-get update
echo " - Done"
/usr/bin/logger -t autobootstrap "ran apt-get update"
fi
}
# clean-up apt
# we only use it to install a puppet agent
# the rest (maintenance, versioning) is up to your puppet infra
function cleanapt {
if [ -f "/etc/apt/sources.list.d/autobootstrap.list" ]; then
echo -n "* Removing apt conf used by autobootstrap"
rm /etc/apt/sources.list.d/autobootstrap.list
echo " - Done"
/usr/bin/logger -t autobootstrap "removed custom apt repo"
echo -n "* Executing apt-get update"
apt-get update
echo " - Done"
/usr/bin/logger -t autobootstrap "ran apt-get update"
fi
}
# install and setup puppet
function installpuppet {
echo -n "* Attempting to install puppet"
apt-get install -y --force-yes puppet
echo " - Done"
/usr/bin/logger -t autobootstrap "installed puppet"
echo -n "* Set Puppet to start on boot"
if [ -f "/etc/default/puppet" ]; then
sed -i 's/no/yes/g' /etc/default/puppet
fi
echo " - Done"
/usr/bin/logger -t autobootstrap "puppet agent configured to run at boot"
echo -n "* Specify puppetmaster server \"$PUPPETMASTER\" and certname in Puppet agent config"
echo "" >> /etc/puppet/puppet.conf
echo "[agent]" >> /etc/puppet/puppet.conf
echo "server=$PUPPETMASTER" >> /etc/puppet/puppet.conf
echo "certname=$FQDN" >> /etc/puppet/puppet.conf
echo "report=true" >> /etc/puppet/puppet.conf
echo " - Done"
/usr/bin/logger -t autobootstrap "setup puppet agent to use $PUPPETMASTER as puppetmaster"
echo -n "* Start Puppet agent"
/etc/init.d/puppet restart > /dev/null
echo " - Done"
/usr/bin/logger -t autobootstrap "started puppet agent"
}
# post setup stuff
function post {
echo ""
echo "All DONE"
echo ""
/usr/bin/logger -t autobootstrap "autobootstrap.sh ENDED"
exit 0
}
# set time zone
function settimezone {
if [ ! -z "$TIMEZONE" ]; then
echo "Setup timezone"
echo "$TIMEZONE" > /etc/timezone
rm /etc/localtime
ln -s /usr/share/zoneinfo/$TIMEZONE /etc/localtime
date
echo " - Done"
/usr/bin/logger -t autobootstrap "set time zone to $TIMEZONE"
fi
}
##### EXECUTE
echo ""
echo "Starting setup..."
echo ""
sethostname
sethosts
settimezone
setupapt
installpuppet
cleanapt
post