Skip to content

Commit 33bea52

Browse files
author
Brian Pontarelli
committed
Done refactoring
1 parent fbacb2e commit 33bea52

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea/workspace.xml
22
output
3+
bundle

configure.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,4 @@ chmod +x output/backup.sh
7676
chmod +x output/setup-server.sh
7777

7878
sed "s/@APPLICATION_SERVER_IP@/${application_server_ip}/g" < template/ubuntu-16.04/iptables-database-server.cfg > output/iptables-database-server.cfg
79-
sed "s/@MONIT_EMAIL_SERVER@/${monit_smtp_host}/g;s/@MONIT_EMAIL_PORT@/${monit_smtp_port}/g;s/@MONIT_EMAIL_USERNAME@/${monit_smtp_username}/g;s/@MONIT_EMAIL_PASSWORD@/${monit_smtp_password}/g;s/@MONIT_EMAIL_ENCRYPTION@/${monit_smtp_encryption}/g" < template/ubuntu-16.04/monitrc > output/monitrc
79+
sed "s/@MONIT_EMAIL_SERVER@/${monit_smtp_host}/g;s/@MONIT_EMAIL_PORT@/${monit_smtp_port}/g;s/@MONIT_EMAIL_USERNAME@/${monit_smtp_username}/g;s/@MONIT_EMAIL_PASSWORD@/${monit_smtp_password}/g;s/@MONIT_EMAIL_ENCRYPTION@/${monit_smtp_encryption}/g;s/@MONIT_ALERT_EMAIL@/${monit_alert_email}/g" < template/ubuntu-16.04/monitrc > output/monitrc

setup-new-server.sh

+16-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function bail {
66
}
77

88
if [[ ${#} != 4 && ${#} != 5 ]]; then
9-
echo "Usage: setup-new-server.sh <root@host> <ssh-public-key-file> <iptable-config-file> <ordinary-username> [ordinary-user-password]"
9+
echo "Usage: setup-new-server.sh <root@host> <local-ssh-public-key-file> <local-iptable-config-file> <ordinary-username> [ordinary-user-password]"
1010
echo ""
1111
echo " for example: setup-new-server.sh root@192.168.1.1 ~/.ssh/id_rsa.pub output/iptables-application-server.cfg myuser password"
1212
echo ""
@@ -45,5 +45,18 @@ if ! [ -f ${iptable_cfg_file} ]; then
4545
bail "Invalid IPTables configuration file"
4646
fi
4747

48-
scp output/* ${ssh_key_file} ${root_at_host}:/root
49-
ssh -t ${root_at_host} "/root/setup-server.sh ${ssh_key_file} ${iptable_cfg_file} '${ordinary_user}' '${ordinary_user_password}'"
48+
# Prepare the bundle to go to the server
49+
rm -rf bundle
50+
mkdir bundle
51+
cp output/* bundle
52+
cp ${iptable_cfg_file} bundle/iptables.cfg
53+
cp ${ssh_key_file} bundle/ssh-public-key
54+
rm bundle/iptables-*-server.cfg
55+
56+
# Transfer the bundle and execute it
57+
scp bundle/* ${root_at_host}:/root
58+
echo "Setting up the server. This might take a few minutes. There will be a log file in /root/setup.log after this is complete in case anything failed."
59+
ssh -t ${root_at_host} "/root/setup-server.sh ssh-public-key iptables.cfg '${ordinary_user}' '${ordinary_user_password}' > /root/setup.log 2>&1"
60+
61+
# Clean up
62+
rm -rf bundle

template/ubuntu-16.04/setup-server.sh

+13-8
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,35 @@ if ! [ -f ${iptable_cfg_file} ]; then
2525
bail "Invalid IPTables configuration file"
2626
fi
2727

28-
apt-get -qq -y install libpam-cracklib > /dev/null 2>&1
29-
apt-get -qq -y install libpam-google-authenticator > /dev/null 2>&1
30-
apt-get -qq -y install ntp > /dev/null 2>&1
28+
echo "############################################ Installing packages ############################################"
29+
apt-get -y install libpam-cracklib
30+
apt-get -y install libpam-google-authenticator
31+
apt-get -y install ntp
3132
debconf-set-selections <<< 'iptables-persistent iptables-persistent/autosave_v4 boolean true'
3233
debconf-set-selections <<< 'iptables-persistent iptables-persistent/autosave_v6 boolean true'
33-
apt-get -qq -y install iptables-persistent > /dev/null 2>&1
34-
apt-get -qq -y install monit > /dev/null 2>&1
35-
apt-get -qq -y install ruby > /dev/null 2>&1
34+
apt-get -y install iptables-persistent
35+
apt-get -y install monit
36+
apt-get -y install ruby
3637

38+
echo "############################################ Creating ordinary user ############################################"
3739
ordinary_user_password_encrypted=$(mkpasswd -m sha-512 ${ordinary_user_password})
3840
useradd -m -G sudo -s /bin/bash -p "${ordinary_user_password_encrypted}" ${ordinary_user}
3941
mkdir -p /home/${ordinary_user}/.ssh
4042
cp ${ssh_key_file} /home/${ordinary_user}/.ssh/authorized_keys
4143
chown -R ${ordinary_user}:${ordinary_user} /home/${ordinary_user}/.ssh
4244
chmod 700 /home/${ordinary_user}/.ssh
4345
chmod 600 /home/${ordinary_user}/.ssh/authorized_keys
46+
usermod -p '*' root
4447

4548
# Backup all the configuration files
49+
echo "############################################ Backing up files ############################################"
4650
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.orig
4751
cp /etc/pam.d/sshd /etc/pam.d/sshd.orig
4852
cp /etc/iptables/rules.v4 /etc/iptables/rules.v4.orig
4953
cp /etc/monit/monitrc /etc/monit/monitrc.orig
5054

51-
# SCP over all the files
55+
# Install configuration files
56+
echo "############################################ Installing configuration files ############################################"
5257
cp common-password /etc/pam.d/common-password
5358
cp ${iptable_cfg_file} /etc/iptables/rules.v4
5459
cp monit-ssh-logins.cfg /etc/monit/conf.d/ssh-logins
@@ -59,7 +64,7 @@ if [ -f monit-slack-pushover.rb ]; then
5964
cp monit-slack-pushover.rb /etc/monit/monit-slack-pushover.rb
6065
fi
6166

67+
echo "############################################ Restarting the services ############################################"
6268
service ssh restart
6369
service netfilter-persistent reload
6470
service monit restart
65-
usermod -p '*' root

0 commit comments

Comments
 (0)