From 3e8f20616f0d8807cea6b6e9fb7fae18df806335 Mon Sep 17 00:00:00 2001 From: "J. Patrick Fulton" Date: Wed, 16 Aug 2023 23:29:28 -0500 Subject: [PATCH] Complete linux vm setup. --- linux-scripts/clean-up.sh | 3 ++ .../setup-eviction-shutdown-system.sh | 18 +++++++ linux-scripts/setup-firewall.sh | 24 +++++++++ linux-scripts/setup-motd.sh | 20 +++++++ linux-scripts/setup-node-and-yarn.sh | 53 +++++++++++++++++++ linux-scripts/setup-sms-notifier.sh | 36 +++++++++++++ linux-scripts/update-notifier-config.sh | 8 +++ scripts/create-linux-server.sh | 46 ++++++++++++++++ 8 files changed, 208 insertions(+) create mode 100755 linux-scripts/clean-up.sh create mode 100755 linux-scripts/setup-eviction-shutdown-system.sh create mode 100755 linux-scripts/setup-firewall.sh create mode 100755 linux-scripts/setup-motd.sh create mode 100755 linux-scripts/setup-node-and-yarn.sh create mode 100755 linux-scripts/setup-sms-notifier.sh create mode 100755 linux-scripts/update-notifier-config.sh diff --git a/linux-scripts/clean-up.sh b/linux-scripts/clean-up.sh new file mode 100755 index 0000000..fabb516 --- /dev/null +++ b/linux-scripts/clean-up.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +rm -f *; \ No newline at end of file diff --git a/linux-scripts/setup-eviction-shutdown-system.sh b/linux-scripts/setup-eviction-shutdown-system.sh new file mode 100755 index 0000000..922c810 --- /dev/null +++ b/linux-scripts/setup-eviction-shutdown-system.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +BASE_REPO_URL="https://raw.githubusercontent.com/jpfulton/example-linux-configs/main"; + +# Set up eviction query and shutdown script +EVICTION_QUERY_CRON_SNIPPET_FILE="preempt-query"; +EVICTION_QUERY_SCRIPT="query-for-preempt-event.sh"; +echo "Setting up eviction query script..."; + +sudo wget -q ${BASE_REPO_URL}/usr/local/sbin/${EVICTION_QUERY_SCRIPT}; +sudo chmod ug+x ./${EVICTION_QUERY_SCRIPT} +sudo mv ./${EVICTION_QUERY_SCRIPT} /usr/local/sbin/ + +sudo wget -q ${BASE_REPO_URL}/etc/cron.d/${EVICTION_QUERY_CRON_SNIPPET_FILE}; +sudo mv ./${EVICTION_QUERY_CRON_SNIPPET_FILE} /etc/cron.d/ + +echo "---"; +echo; \ No newline at end of file diff --git a/linux-scripts/setup-firewall.sh b/linux-scripts/setup-firewall.sh new file mode 100755 index 0000000..1a06c05 --- /dev/null +++ b/linux-scripts/setup-firewall.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +BASE_REPO_URL="https://raw.githubusercontent.com/jpfulton/example-linux-configs/main"; + +# Set up local firewall basics +DEFAULTS_PATH="/etc/default/"; +UFW_DEFAULTS_FILE="ufw"; +if [ $(sudo ufw status | grep -c inactive) -ge 1 ] + then + echo "Local firewall is inactive. Configuring and enabling with SSH rule..."; + + sudo wget -q ${BASE_REPO_URL}${DEFAULTS_PATH}${UFW_DEFAULTS_FILE} -O ${UFW_DEFAULTS_FILE}; + sudo mv ${UFW_DEFAULTS_FILE} ${DEFAULTS_PATH}; + + sudo ufw allow ssh; + sudo ufw show added; + sudo ufw --force enable; + sudo ufw status numbered; + + else + echo "Local fireall is active. No configuration or rules applied."; +fi +echo "---"; +echo; \ No newline at end of file diff --git a/linux-scripts/setup-motd.sh b/linux-scripts/setup-motd.sh new file mode 100755 index 0000000..b224ed5 --- /dev/null +++ b/linux-scripts/setup-motd.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +DEBIAN_FRONTEND="noninteractive"; +BASE_REPO_URL="https://raw.githubusercontent.com/jpfulton/example-linux-configs/main"; + +# Set up custom MOTD script +MOTD_PATH="/etc/update-motd.d/"; +MOTD_FILE="01-custom"; +if [ ! -f ${MOTD_PATH}${MOTD_FILE} ] + then + echo "Setting up custom MOTD script..."; + + sudo -E apt-get install -y neofetch inxi; + sudo wget -q ${BASE_REPO_URL}${MOTD_PATH}${MOTD_FILE} -O ${MOTD_FILE}; + sudo chmod a+x ./${MOTD_FILE}; + sudo mv ./${MOTD_FILE} ${MOTD_PATH}${MOTD_FILE}; + + echo "---"; + echo; +fi \ No newline at end of file diff --git a/linux-scripts/setup-node-and-yarn.sh b/linux-scripts/setup-node-and-yarn.sh new file mode 100755 index 0000000..a600a69 --- /dev/null +++ b/linux-scripts/setup-node-and-yarn.sh @@ -0,0 +1,53 @@ +#!/usr/bin/env bash + +DEBIAN_FRONTEND="noninteractive"; +BASE_REPO_URL="https://raw.githubusercontent.com/jpfulton/example-linux-configs/main"; + +setup-nodejs () { + # Install Node as needed + which node >> /dev/null; + if [ $? -eq 0 ] + then + local NODE_VERSION=$(node --version); + if [ $NODE_VERSION == "v12.22.9" ] + then + echo "Default node package detected. Removing."; + sudo -E apt-get remove nodejs; + sudo -E apt-get autoremove; + else + echo "Detected alternate version of node: ${NODE_VERSION}"; + echo "Ensure that version is above v18.0.0 or manually use nvm."; + fi + else + echo "Node not detected. Preparing installation of node v18.x."; + + sudo curl -sL https://deb.nodesource.com/setup_18.x | sudo bash -; + sudo -E apt-get install -y nodejs; + fi + + echo "---"; + echo; +} + +setup-yarn () { + # Install Yarn as needed + which yarn >> /dev/null; + if [ $? -eq 1 ] + then + echo "Yarn not detected. Preparing to install."; + + sudo curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null; + sudo echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list; + sudo -E apt-get update; + sudo -E apt-get install -y yarn; + else + local YARN_VERSION=$(yarn --version); + echo "Found yarn version: ${YARN_VERSION}"; + fi + + echo "---"; + echo; +} + +setup-nodejs; +setup-yarn; \ No newline at end of file diff --git a/linux-scripts/setup-sms-notifier.sh b/linux-scripts/setup-sms-notifier.sh new file mode 100755 index 0000000..58215d8 --- /dev/null +++ b/linux-scripts/setup-sms-notifier.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash + +# Install or upgrade sms-notify-cli utility +which sms-notify-cli >> /dev/null; +if [ $? -eq 1 ] + then + echo "sms-notify-cli utility not detected. Preparing to install."; + sudo yarn global add @jpfulton/net-sms-notifier-cli; + else + echo "Found sms-notify-cli utility. Attempting update."; + sudo yarn global upgrade @jpfulton/net-sms-notifier-cli@latest; +fi +echo "---"; +echo; + +# Initialize sms-notify-cli configuration +NOTIFIER_CONFIG="/etc/sms-notifier/notifier.json"; +if [ -f $NOTIFIER_CONFIG ] + then + echo "Found notifier configuration. Validating with current version."; + + sudo sms-notify-cli validate; + if [ $? -eq 0 ] + then + echo "Configuration file validation passes on current version."; + else + echo "Invalid configuration file. Manually correct."; + fi + else + echo "No notifier configuration found. Initializing..."; + echo "Manual configuration to the ${NOTIFIER_CONFIG} file will be required."; + sudo sms-notify-cli init; +fi + +echo "---"; +echo; diff --git a/linux-scripts/update-notifier-config.sh b/linux-scripts/update-notifier-config.sh new file mode 100755 index 0000000..bbaf39e --- /dev/null +++ b/linux-scripts/update-notifier-config.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +sudo chown root:root ~/notifier.json +sudo chmod 0640 ~/notifier.json +sudo mv ~/notifier.json /etc/sms-notifier/notifier.json + +echo "---"; +echo; diff --git a/scripts/create-linux-server.sh b/scripts/create-linux-server.sh index b8d39c7..a4d3292 100755 --- a/scripts/create-linux-server.sh +++ b/scripts/create-linux-server.sh @@ -234,6 +234,24 @@ run-script-from-admin-home () { echo; } +scp-notifier-config () { + echo "Copying SMS Nofifier config..."; + + local NOTIFIER_CONFIG="/etc/sms-notifier/notifier.json" + local REMOTE_LOCATION="~/" + + if [ -f $NOTIFIER_CONFIG ] + then + scp -i $ADMIN_PRIVATE_KEY_FILE \ + $NOTIFIER_CONFIG \ + ${ADMIN_USERNAME}@${SERVER_FQDN}:${REMOTE_LOCATION}; + + run-script-from-admin-home update-notifier-config.sh; + else + echo "WARN: Manual installation of notifier config will be required."; + fi +} + restart-vm () { echo "Restarting VM to allow settings to take effect..."; @@ -264,9 +282,37 @@ main () { # copy setup scripts to server scp-file-to-admin-home ${CURRENT_SCRIPT_DIR}../linux-scripts/update-base-packages.sh; + scp-file-to-admin-home ${CURRENT_SCRIPT_DIR}../linux-scripts/setup-firewall.sh; + scp-file-to-admin-home ${CURRENT_SCRIPT_DIR}../linux-scripts/setup-motd.sh; + scp-file-to-admin-home ${CURRENT_SCRIPT_DIR}../linux-scripts/setup-node-and-yarn.sh; + scp-file-to-admin-home ${CURRENT_SCRIPT_DIR}../linux-scripts/setup-sms-notifier.sh; + scp-file-to-admin-home ${CURRENT_SCRIPT_DIR}../linux-scripts/setup-eviction-shutdown-system.sh; + scp-file-to-admin-home ${CURRENT_SCRIPT_DIR}../linux-scripts/update-notifier-config.sh; + scp-file-to-admin-home ${CURRENT_SCRIPT_DIR}../linux-scripts/clean-up.sh; # execute remote setup scripts run-script-from-admin-home update-base-packages.sh; + run-script-from-admin-home setup-firewall.sh; + run-script-from-admin-home setup-motd.sh; + run-script-from-admin-home setup-node-and-yarn.sh; + + + if [ "$IS_SPOT" = "true" ] + then + run-script-from-admin-home setup-sms-notifier.sh; + scp-notifier-config; + run-script-from-admin-home setup-eviction-shutdown-system.sh; + + if [ "$SPOT_RESTART" = "true" ] + then + echo "Tagging VM for restart after eviction..."; + + local VM_ID=$(az-get-vm-resource-id $RESOURCE_GROUP $SERVER_NAME); + az-add-tag-to-resource $VM_ID "AttemptRestartAfterEviction=true"; + fi + fi + + run-script-from-admin-home clean-up.sh; echo "---"; echo "Done.";