131 lines
3.2 KiB
Bash
Executable File
131 lines
3.2 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
# Some Global Variables
|
|
|
|
ssh_authorized_keys_path='/root/.ssh/authorized_keys'
|
|
ssh_config_path='/etc/ssh/ssh_config'
|
|
sshd_config_path='/etc/ssh/sshd_config'
|
|
etc_resolv_path='/etc/resolv.conf'
|
|
|
|
|
|
# Functions
|
|
|
|
get_distro() {
|
|
OS=$(cat /etc/*release | grep ID | head -1 | cut -c 4-)
|
|
echo $OS
|
|
}
|
|
|
|
setup_ssh() {
|
|
tput setaf 2; tput bold; echo "Setting up SSH"; tput sgr0;
|
|
|
|
mkdir -p $(dirname $ssh_authorized_keys_path)
|
|
touch $ssh_authorized_keys_path
|
|
|
|
if ! grep -q "PasswordAuthentication no" $sshd_config_path; then
|
|
echo "PasswordAuthentication no" >> $sshd_config_path
|
|
fi
|
|
|
|
if ! grep -q "PermitRootLogin yes" $sshd_config_path; then
|
|
echo "PermitRootLogin yes" >> $sshd_config_path
|
|
fi
|
|
|
|
# TODO: Make sure to replace the following address with http://metadata
|
|
# whenever we got http://metadata resolving to url work successfully.
|
|
|
|
metadata=$(curl http://[2a0a:e5c1:144::]:5000)
|
|
|
|
echo "$metadata" | jq -r '.["ssh-key-list"] | .[]' > ssh-key-list.txt
|
|
while read ssh_key; do
|
|
if ! grep -q "$ssh_key" $ssh_authorized_keys_path; then
|
|
echo $ssh_key >> $ssh_authorized_keys_path
|
|
fi
|
|
|
|
done < ssh-key-list.txt
|
|
rm -f ssh-key-list.txt
|
|
|
|
service sshd restart
|
|
}
|
|
|
|
grow_partition() {
|
|
tput setaf 2; tput bold; echo "Growing Partition"; tput sgr0;
|
|
|
|
# TODO: Try to replace the growpart to parted
|
|
wget https://git.launchpad.net/ubuntu/+source/cloud-utils/plain/bin/growpart -q
|
|
if [ -e growpart ]; then
|
|
sh ./growpart /dev/vda 3; rm growpart
|
|
else
|
|
tput setaf 1; echo "growpart couldn't be downloaded"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
make_script_verbose() {
|
|
# Show output of this script
|
|
if [[ ! -e /etc/conf.d/local ]] && ! grep -q "rc_verbose=yes" /etc/conf.d/local; then
|
|
echo "rc_verbose=yes" >> /etc/conf.d/local
|
|
fi
|
|
}
|
|
|
|
setup_dns() {
|
|
tput setaf 2; tput bold; echo "Setting up DNS"; tput sgr0;
|
|
|
|
# Check if rdnssd is installed, if not put Google's DNS
|
|
# into /etc/resolv.conf and install rdnssd for the next time
|
|
if ! which rdnssd; then
|
|
echo "nameserver 2001:4860:4860::8888" >> $etc_resolv_path
|
|
echo "nameserver 2001:4860:4860::8844" >> $etc_resolv_path
|
|
echo "nameserver 8.8.8.8" >> $etc_resolv_path
|
|
echo "nameserver 8.8.4.4" >> $etc_resolv_path
|
|
fi
|
|
}
|
|
|
|
setup_etc_host() {
|
|
tput setaf 2; tput bold; echo "Setting up /etc/hosts"; tput sgr0;
|
|
|
|
metadata=$(curl -s http://[2a0a:e5c1:144:1::]:5000)
|
|
host_list=$(echo "$metadata" | jq -r '.["host-list"]')
|
|
host_list_len=$(echo "$host_list" | jq -r '. | length')
|
|
|
|
cat > /etc/hosts <<EOF
|
|
127.0.0.1 localhost.my.domain localhost localhost.localdomain localhost
|
|
::1 localhost localhost.localdomain
|
|
|
|
$(
|
|
for i in $(seq 0 $((host_list_len - 1)));
|
|
do
|
|
hostname=$(echo $host_list | jq -r ".[$i][0]")
|
|
ip=$(echo $host_list | jq -r ".[$i][1]")
|
|
printf "%-25s %s\n" $hostname $ip
|
|
done
|
|
)
|
|
EOF
|
|
}
|
|
|
|
|
|
# Main Code Starts here
|
|
|
|
make_script_verbose
|
|
|
|
setup_etc_host
|
|
|
|
setup_dns
|
|
|
|
# Initial Package Installation
|
|
if [[ $(get_distro) = "alpine" ]]; then
|
|
edge_package_flags='--update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ --allow-untrusted'
|
|
|
|
apk update
|
|
apk add ndisc6 $edge_package_flags
|
|
apk add openssh-server sfdisk util-linux jq curl ncurses
|
|
else
|
|
echo "Unsupported OS"
|
|
exit 1
|
|
fi
|
|
|
|
rc-update add rdnssd
|
|
service rdnssd start
|
|
|
|
setup_ssh
|
|
|
|
grow_partition
|