uncloud-init/ucloud-init.start

141 lines
3.5 KiB
Plaintext
Raw Normal View History

2019-08-23 11:01:23 +00:00
#!/bin/sh
2019-09-02 09:14:23 +00:00
# Some Global Variables
2019-08-25 11:21:04 +00:00
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'
2019-08-23 12:54:17 +00:00
2019-08-25 10:50:39 +00:00
2019-09-02 09:14:23 +00:00
# Functions
2019-08-23 11:01:23 +00:00
2019-09-02 09:14:23 +00:00
get_distro() {
OS=$(cat /etc/*release | grep ID | head -1 | cut -c 4-)
echo $OS
}
2019-09-02 09:14:23 +00:00
setup_ssh() {
2019-09-02 13:01:48 +00:00
tput setaf 2; tput bold; echo "Setting up SSH"; tput sgr0;
2019-09-02 10:09:47 +00:00
2019-08-25 10:37:57 +00:00
mkdir -p $(dirname $ssh_authorized_keys_path)
2019-08-23 11:01:23 +00:00
touch $ssh_authorized_keys_path
if ! grep -q "PasswordAuthentication no" $sshd_config_path; then
echo "PasswordAuthentication no" >> $sshd_config_path
2019-08-23 11:01:23 +00:00
fi
2019-09-02 09:14:23 +00:00
if ! grep -q "PermitRootLogin yes" $sshd_config_path; then
echo "PermitRootLogin yes" >> $sshd_config_path
2019-08-23 11:01:23 +00:00
fi
2019-09-02 09:14:23 +00:00
# TODO: Make sure to replace the following address with http://metadata
# whenever we got http://metadata resolving to url work successfully.
2019-10-24 06:47:09 +00:00
metadata=$(curl -s http://metadata)
2019-09-02 13:04:11 +00:00
2019-09-02 09:14:23 +00:00
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
2019-10-24 06:45:11 +00:00
echo $ssh_key >> $ssh_authorized_keys_path
2019-09-02 09:14:23 +00:00
fi
done < ssh-key-list.txt
rm -f ssh-key-list.txt
2019-09-02 17:45:01 +00:00
service -q sshd restart
2019-09-02 09:14:23 +00:00
}
grow_partition() {
2019-09-02 13:01:48 +00:00
tput setaf 2; tput bold; echo "Growing Partition"; tput sgr0;
2019-09-02 13:00:16 +00:00
2019-09-02 09:14:23 +00:00
# TODO: Try to replace the growpart to parted
2019-09-02 13:09:46 +00:00
wget https://git.launchpad.net/ubuntu/+source/cloud-utils/plain/bin/growpart -q
2019-09-02 09:14:23 +00:00
if [ -e growpart ]; then
2019-09-02 17:48:54 +00:00
sh ./growpart -q /dev/vda 3 > /dev/null; rm growpart
2019-09-02 09:14:23 +00:00
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() {
2019-09-02 13:01:48 +00:00
tput setaf 2; tput bold; echo "Setting up DNS"; tput sgr0;
2019-09-02 13:00:16 +00:00
2019-09-02 09:14:23 +00:00
# Check if rdnssd is installed, if not put Google's DNS
# into /etc/resolv.conf and install rdnssd for the next time
2019-09-02 17:39:29 +00:00
if ! apk list | grep -q ndisc6; then
2019-09-02 09:14:23 +00:00
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
}
2019-09-02 17:32:45 +00:00
setup_etc_host() {
2019-09-02 17:35:41 +00:00
tput setaf 2; tput bold; echo "Setting up /etc/hosts"; tput sgr0;
2019-09-02 17:32:45 +00:00
2019-10-24 06:40:08 +00:00
cat > /etc/hosts << EOF
2019-09-02 17:32:45 +00:00
127.0.0.1 localhost.my.domain localhost localhost.localdomain localhost
::1 localhost localhost.localdomain
2019-10-24 06:40:08 +00:00
2a0a:e5c1:144:: metadata
2019-09-02 17:32:45 +00:00
EOF
2019-10-24 06:40:08 +00:00
# metadata=$(curl -s http://[2a0a:e5c1:144::]:9000)
# 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
# ip=$(echo $host_list | jq -r ".[$i][0]")
# hostname=$(echo $host_list | jq -r ".[$i][1]")
# printf "%-25s %s\n" $ip $hostname
# done
# )
# EOF
2019-09-02 17:32:45 +00:00
}
2019-09-02 09:14:23 +00:00
# Main Code Starts here
make_script_verbose
2019-09-02 17:35:07 +00:00
setup_etc_host
2019-09-02 09:14:23 +00:00
setup_dns
# Initial Package Installation
if [[ $(get_distro) = "alpine" ]]; then
2019-10-24 07:49:10 +00:00
tput setaf 2; tput bold; echo "Installing/Updating/Upgrading Packages"; tput sgr0;
2019-09-02 09:14:23 +00:00
edge_package_flags='--update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ --allow-untrusted'
2019-10-24 07:49:10 +00:00
2019-09-02 17:42:08 +00:00
apk update -q
2019-10-24 06:40:08 +00:00
apk upgrade
2019-09-02 17:42:08 +00:00
apk add -q ndisc6 $edge_package_flags
apk add -q openssh-server sfdisk util-linux jq curl ncurses
2019-08-23 11:01:23 +00:00
else
echo "Unsupported OS"
2019-08-23 12:54:17 +00:00
exit 1
2019-08-23 11:01:23 +00:00
fi
2019-09-02 17:44:06 +00:00
rc-update -q add rdnssd
service -q rdnssd start
2019-08-23 12:54:17 +00:00
2019-09-02 09:14:23 +00:00
setup_ssh
2019-08-23 11:01:23 +00:00
2019-09-02 09:14:23 +00:00
grow_partition