Skip to content

Commit

Permalink
Complete linux vm setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpfulton committed Aug 17, 2023
1 parent cce35b1 commit 3e8f206
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 0 deletions.
3 changes: 3 additions & 0 deletions linux-scripts/clean-up.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

rm -f *;
18 changes: 18 additions & 0 deletions linux-scripts/setup-eviction-shutdown-system.sh
Original file line number Diff line number Diff line change
@@ -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;
24 changes: 24 additions & 0 deletions linux-scripts/setup-firewall.sh
Original file line number Diff line number Diff line change
@@ -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;
20 changes: 20 additions & 0 deletions linux-scripts/setup-motd.sh
Original file line number Diff line number Diff line change
@@ -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
53 changes: 53 additions & 0 deletions linux-scripts/setup-node-and-yarn.sh
Original file line number Diff line number Diff line change
@@ -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;
36 changes: 36 additions & 0 deletions linux-scripts/setup-sms-notifier.sh
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 8 additions & 0 deletions linux-scripts/update-notifier-config.sh
Original file line number Diff line number Diff line change
@@ -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;
46 changes: 46 additions & 0 deletions scripts/create-linux-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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...";

Expand Down Expand Up @@ -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.";
Expand Down

0 comments on commit 3e8f206

Please sign in to comment.