-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.sh
executable file
·167 lines (144 loc) · 3.84 KB
/
bootstrap.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
#!/bin/bash -e
[ "$1" = "-n" ] && DRY_RUN=1
# ==================================================
# Check
if [ ! -f /root/.bootstrapped ]; then
echo "Start bootstraping system..."
else
echo "The system is bootstrapped."
exit 0
fi
# ==================================================
# 1. Install rvm and ruby 2.0.0-p247
# 2. Install bundler chef gems
# 3. Setup chef user for chef-solo automation
# ==================================================
# Config
ruby_version='ruby-2.0.0-p247'
gems=(bundler chef ruby-shadow)
chef_user=chef
chef_group=chef
# ==================================================
# Helpers
COLUMNS=${COLUMNS-80}
CLOSE_STEP=
SKIP_STEP=
SKIP_RUN=
SKIP_THIS_RUN=
function line() {
printf "%0${COLUMNS}d\n" 0 | sed 's/./-/g'
}
function fold_lines() {
fold -s -w "${COLUMNS}"
}
function wrap_lines() {
fold_lines | sed "s/.*/${1}&${2}/"
}
function close_step() {
line | sed 's/^./\`/'
echo
}
function step() {
if [ -n "$CLOSE_STEP" ]; then
close_step
else
trap 'close_step' EXIT
fi
SKIP_RUN="$SKIP_STEP"
line | sed 's/^./\//'
if [ -n "$SKIP_STEP" ]; then
echo '| [033mSkipped[0m'
fi
echo "$*" | wrap_lines '| [032m' '[0m'
SKIP_STEP=
CLOSE_STEP=1
}
function xstep() {
SKIP_STEP=1
step "$@"
}
function run() {
echo "$*" | wrap_lines '[036;1m$[0m ' | sed '2,$s/\$/> /'
if [ -z "$DRY_RUN" -a -z "$SKIP_RUN" -a -z "$SKIP_THIS_RUN" ]; then
eval "$*" || exit
fi
}
function xrun() {
SKIP_THIS_RUN=1
run "$@"
SKIP_THIS_RUN=
}
function info() {
echo "$*" | wrap_lines '[036;1m*[0m '
}
function cat_error() {
cat | wrap_lines '[031;1m>>> ' '[0m' >&3
}
exec 3>&2
exec 2> >(cat_error)
[ "$UID" = "0" ] || {
echo "Require root privilege to bootstrap" >&2
exit 1
}
step "Install nessesary packages"
run apt-get update
run aptitude install -y \
libgdbm-dev libyaml-dev libffi-dev \
libncurses5-dev libtool pkg-config \
gawk autoconf automake bison \
python-software-properties \
build-essential zlib1g-dev \
libxslt1-dev libxml2-dev \
sqlite3 libsqlite3-dev \
libssl-dev openssl \
curl wget git-core \
libmysqlclient-dev \
libreadline-dev \
openssh-server
step "Install/Upgrade rvm in /usr/local/rvm"
if [ -d /usr/local/rvm ]; then
info 'upgrade rvm'
run rvm get stable
else
info 'install rvm'
run curl -L https://get.rvm.io | bash -s -- --branch stable --version head
fi
run source /etc/profile.d/rvm.sh
if ! grep 'rvm' /etc/skel/.bashrc &> /dev/null; then
run 'sed -i "1asource /etc/profile.d/rvm.sh" /etc/skel/.bashrc'
fi
run 'touch "/root/.bashrc"'
if ! grep 'rvm' "/root/.bashrc" &> /dev/null; then
run 'sed -i "1asource /etc/profile.d/rvm.sh" /root/.bashrc'
fi
step "Install ruby $ruby_version"
if [ -f "/usr/local/rvm/rubies/$ruby_version" ]; then
info "ruby $ruby_version is already installed"
else
info 'installation may take a long time, restart bootstrap if failed.'
run rvm install "$ruby_version"
fi
run rvm default "$ruby_version"
info `ruby -v 2>&1`
step "Setup gems ${gems[*]}"
test -s ~/.gemrc || echo 'gem: --no-rdoc --no-ri' >> ~/.gemrc
run gem install "${gems[@]}"
step "Create user ${chef_user}:${chef_group}"
run groupadd -r -f "$chef_group"
if ! id "$chef_user" &> /dev/null; then
run useradd -r -g "$chef_group" -m "$chef_user" -s /bin/bash
fi
step "Setup chef deploy key"
if [ ! -d /home/chef/.ssh ]; then
run mkdir -p /home/chef/.ssh
echo "admin-chef-public-key" > /home/chef/.ssh/authorized_keys
run chown -R "$chef_user:$chef_group" /home/chef/.ssh
run chmod 0700 /home/chef/.ssh
fi
step "Setup permission for chef no password sudo running chef-solo"
echo "chef ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/chef
run chmod 0440 /etc/sudoers.d/chef
step "Configure ssh disallow root logins"
sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config
run service ssh restart
echo "The system is bootstrapped." > /root/.bootstrapped