forked from netson/ubuntu-unattended
-
Notifications
You must be signed in to change notification settings - Fork 1
/
start.sh
163 lines (131 loc) · 4.79 KB
/
start.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
#!/bin/bash
set -e
# set defaults
default_hostname="$(hostname)"
default_domain="netson.local"
default_puppetmaster="foreman.netson.nl"
tmp="/home/netson/"
clear
# check for root privilege
if [ "$(id -u)" != "0" ]; then
echo " this script must be run as root" 1>&2
echo
exit 1
fi
# define download function
# courtesy of http://fitnr.com/showing-file-download-progress-using-wget.html
download()
{
local url=$1
echo -n " "
wget --progress=dot $url 2>&1 | grep --line-buffered "%" | \
sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}'
echo -ne "\b\b\b\b"
echo " DONE"
}
# determine ubuntu version
ubuntu_version=$(lsb_release -cs)
# check for interactive shell
if ! grep -q "noninteractive" /proc/cmdline ; then
stty sane
# ask questions
read -ep " please enter your preferred hostname: " -i "$default_hostname" hostname
read -ep " please enter your preferred domain: " -i "$default_domain" domain
# ask whether to add puppetlabs repositories
while true; do
read -p " do you wish to add the latest puppet repositories from puppetlabs? [y/n]: " yn
case $yn in
[Yy]* ) include_puppet_repo=1
puppet_deb="puppetlabs-release-"$ubuntu_version".deb"
break;;
[Nn]* ) include_puppet_repo=0
puppet_deb=""
puppetmaster="puppet"
break;;
* ) echo " please answer [y]es or [n]o.";;
esac
done
if [[ include_puppet_repo ]] ; then
# ask whether to setup puppet agent or not
while true; do
read -p " do you wish to setup the puppet agent? [y/n]: " yn
case $yn in
[Yy]* ) setup_agent=1
read -ep " please enter your puppet master: " -i "$default_puppetmaster" puppetmaster
break;;
[Nn]* ) setup_agent=0
puppetmaster="puppet"
break;;
* ) echo " please answer [y]es or [n]o.";;
esac
done
fi
fi
# print status message
echo " preparing your server; this may take a few minutes ..."
# set fqdn
fqdn="$hostname.$domain"
# update hostname
echo "$hostname" > /etc/hostname
sed -i "[email protected]@$fqdn@g" /etc/hosts
sed -i "s@ubuntu@$hostname@g" /etc/hosts
hostname "$hostname"
# update repos
apt-get -y update > /dev/null 2>&1
apt-get -y upgrade > /dev/null 2>&1
apt-get -y dist-upgrade > /dev/null 2>&1
apt-get -y autoremove > /dev/null 2>&1
apt-get -y purge > /dev/null 2>&1
echo "install more custom packages ..."
# install more packages: wget, curl, git, build-essential, apt-transport-https ca-certificates
apt-get -y install wget curl git build-essential apt-transport-https ca-certificates
# GPG key for docker installation
apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
echo "deb https://apt.dockerproject.org/repo ubuntu-trusty main" > /etc/apt/sources.list.d/docker.list
# install docker
apt-get -y update
apt-get -y install docker-engine
# install docker-compose
curl -L https://github.com/docker/compose/releases/download/1.6.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
echo "finished custom package installation"
# install puppet
if [[ include_puppet_repo -eq 1 ]]; then
# install puppet repo
wget https://apt.puppetlabs.com/$puppet_deb -O $tmp/$puppet_deb > /dev/null 2>&1
dpkg -i $tmp/$puppet_deb > /dev/null 2>&1
apt-get -y update > /dev/null 2>&1
rm $tmp/$puppet_deb
# check to install puppet agent
if [[ setup_agent -eq 1 ]] ; then
# install puppet
apt-get -y install puppet > /dev/null 2>&1
# set puppet master settings
sed -i "s@\[master\]@\
# configure puppet master\n\
server=$puppetmaster\n\
report=true\n\
pluginsync=true\n\
\n\
\[master\]@g" /etc/puppet/puppet.conf
# download the finish script if it doesn't yet exist
if [[ ! -f $tmp/finish.sh ]]; then
echo -n " downloading finish.sh: "
cd $tmp
download "https://raw.githubusercontent.com/netson/ubuntu-unattended/master/finish.sh"
fi
# set proper permissions on finish script
chmod +x $tmp/finish.sh
# connect to master and ensure puppet is always the latest version
echo " connecting to puppet master to request new certificate"
echo " please sign the certificate request on your puppet master ..."
puppet agent --waitforcert 60 --test
echo " once you've signed the certificate, please run finish.sh from your home directory"
fi
fi
# remove myself to prevent any unintended changes at a later stage
rm $0
# finish
echo " DONE; rebooting ... "
# reboot
reboot