From 8d7efb4bbe3cfd68b6b07aa80487ee6293349d15 Mon Sep 17 00:00:00 2001 From: meow Date: Sun, 17 Nov 2019 21:05:58 +0500 Subject: [PATCH] installation script added --- install.sh | 3 ++ ucloud-init.sh | 102 +++++++++++++++++++++++++++++++++++++++++++++ ucloud-init.start | 104 ++++------------------------------------------ 3 files changed, 112 insertions(+), 97 deletions(-) create mode 100755 install.sh create mode 100755 ucloud-init.sh diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..1d53925 --- /dev/null +++ b/install.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +cp ucloud-init.start /etc/local.d/ diff --git a/ucloud-init.sh b/ucloud-init.sh new file mode 100755 index 0000000..16ba912 --- /dev/null +++ b/ucloud-init.sh @@ -0,0 +1,102 @@ +#!/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 -s http://metadata) + + echo "$metadata" | jq -r '.["ssh-keys"] | .[]' > 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 -q sshd restart +} + +grow_partition() { + tput setaf 2; tput bold; echo "Growing Partition"; tput sgr0; + + # TODO: Try to replace the growpart to parted + sh growpart -q /dev/vda 3 > /dev/null; +} + +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 ! apk list | grep -q ndisc6; 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 +} + +# Main Code Starts here + +make_script_verbose + +setup_dns + +# Initial Package Installation +if [[ $(get_distro) = "alpine" ]]; then + tput setaf 2; tput bold; echo "Installing/Updating/Upgrading Packages"; tput sgr0; + + edge_package_flags='--update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ --allow-untrusted' + + apk update -q + apk upgrade + apk add -q ndisc6 $edge_package_flags + apk add -q openssh-server sfdisk util-linux jq curl ncurses +else + echo "Unsupported OS" + exit 1 +fi + +rc-update -q add rdnssd +service -q rdnssd start + +setup_ssh + +grow_partition diff --git a/ucloud-init.start b/ucloud-init.start index 16ba912..0a88653 100755 --- a/ucloud-init.start +++ b/ucloud-init.start @@ -1,102 +1,12 @@ #!/bin/sh -# Some Global Variables +# Clone ucloud-init repo +cd /usr/sbin +git clone https://code.ungleich.ch/ucloud/ucloud-init.git +cd ucloud-init -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' +# Update it +git pull +sh ./ucloud-init.sh -# 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 -s http://metadata) - - echo "$metadata" | jq -r '.["ssh-keys"] | .[]' > 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 -q sshd restart -} - -grow_partition() { - tput setaf 2; tput bold; echo "Growing Partition"; tput sgr0; - - # TODO: Try to replace the growpart to parted - sh growpart -q /dev/vda 3 > /dev/null; -} - -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 ! apk list | grep -q ndisc6; 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 -} - -# Main Code Starts here - -make_script_verbose - -setup_dns - -# Initial Package Installation -if [[ $(get_distro) = "alpine" ]]; then - tput setaf 2; tput bold; echo "Installing/Updating/Upgrading Packages"; tput sgr0; - - edge_package_flags='--update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ --allow-untrusted' - - apk update -q - apk upgrade - apk add -q ndisc6 $edge_package_flags - apk add -q openssh-server sfdisk util-linux jq curl ncurses -else - echo "Unsupported OS" - exit 1 -fi - -rc-update -q add rdnssd -service -q rdnssd start - -setup_ssh - -grow_partition