refactor in preperation for tilde switching support

This commit is contained in:
sose 2020-09-10 16:14:30 -07:00
parent 87391c7981
commit 0b7fb8cf61
5 changed files with 90 additions and 252 deletions

View File

@ -1,161 +0,0 @@
#!/bin/sh
# This script is to be run on the remote host in order to create a weechat session
# NOTE: This script only works with tmux, mosh and screen are not supported
# TODO: Cleanup function and robustness
#. util.sh
#weechat_relay_port="9000"
#weechat_relay_password="password1234"
# these are set when running the script
create_tmux_session() {
new_session_name="weechat"
echo "Creating new tmux session..."
while tmux list-sessions | grep -q "^$new_session_name"
do
new_session_name="$new_session_name-"
done
tmux new-session -d -s "$new_session_name"
tmux send -t "$new_session_name":0 'weechat' ENTER
}
attatch_tmux_session() {
choice=""
tmux_sessions=""
session_name=""
window_number=""
echo "Attatching to existing tmux session..."
if tmux list-sessions >/dev/null
then
tmux_sessions="$(tmux list-sessions \
| cut -f 1 -d ' ' \
| rev | cut -c 2- | rev)"
clear
echo "Which tmux session would you like to attach to?"
echo "$tmux_sessions"
read -r choice
if is_substring "$choice" "$tmux_sessions"
then
session_name="$choice"
else
echo "\"$choice\" is not recognized as a valid session, please try again"
attatch_tmux_session
fi
else
echo "No tmux session appears to be running, a new session will be created"
create_tmux_session
exit
fi
window_number="$(( "$(tmux list-sessions | grep "^$session_name" | cut -f 2 -d ' ')" + 1 ))"
tmux new-window -t "$session_name:$window_number"
tmux send -t "$session_name:$window_number" 'weechat' ENTER
echo "Waiting for weechat session..."
while ! ps xo 'cmd=' | grep -q '^weechat'
do
sleep 0.5
done
}
create_weechat_relay(){
echo "Configuring new WeeChat relay..."
if [ -z "$weechat_relay_port" ]
then
echo "weechat_relay_port is not set! exiting..."
exit
fi
if [ -z "$weechat_relay_password" ]
then
echo "weechat_relay_password is not set! exiting..."
exit
fi
weechat_pane_name="WeeChat $(weechat -v)"
weechat_pane_id="$(tmux list-panes -a -F '#{pane_id} #{pane_title}' \
| grep "$weechat_pane_name" \
| cut -f 1 -d ' ')"
if [ "$weechat_pane_id" = " " ]
then
echo "Failed to find WeeChat session, exiting..."
exit
fi
tmux send -t "$weechat_pane_id" '^W'
tmux send -t "$weechat_pane_id" "/relay add weechat $weechat_relay_port" ENTER
tmux send -t "$weechat_pane_id" "/secure set relay $weechat_relay_password" ENTER
tmux send -t "$weechat_pane_id" '/set relay.network.password "${sec.data.relay}"' ENTER
tmux send -t "$weechat_pane_id" "/relay start weechat" ENTER
sleep 1
tmux send -t "$weechat_pane_id" '^Y'
echo "WeeChat relay configured!"
}
configure_weechat_session() {
choice=""
clear
echo "=== WeeChat Relay Setup ==="
echo "Do you enter a password to start WeeChat? (encrypted config)"
echo "1. Yes"
echo "2. No"
while :
do
read -r choice
case "$choice" in
"1")
echo "In this case you will need to start WeeChat manually inside tmux or this script will break. Once you have done this press ENTER"
read
break
;;
"2")
echo "Starting config wizard..."
echo
break
;;
*)
echo "Please enter a valid option"
;;
esac
done
if ! ps xo 'cmd=' | grep -q '^weechat'
then
if ! tmux list-sessions >/dev/null
then
create_tmux_session
else
while :
do
clear
echo "Found existing tmux sessions:"
echo "$(tmux list-sessions)"
echo
echo "tmux is already running, what would you like to do?"
echo "1. Start WeeChat in a new window in an existing tmux session"
echo "2. Start WeeChat in new tmux session"
read -r choice
case "$choice" in
"1")
clear
attatch_tmux_session
create_weechat_relay
break
;;
"2")
clear
create_tmux_session
create_weechat_relay
break
;;
"*")
echo "Please enter a valid option"
;;
esac
done
fi
else
clear
echo "A WeeChat process was found, configuring relay..."
create_weechat_relay
fi
}
configure_weechat_session

41
connection.sh Executable file
View File

@ -0,0 +1,41 @@
#!/bin/sh
connection_test() {
ssh_command="$1"
while ! $ssh_command 'exit'
do
echo "There seems to be a problem connecting to the tilde"
echo "Use this shell to try and fix any problems, then try connecting again by exiting the shell"
bash
done
echo "Connection success!"
}
check_key_file() {
ssh_command=""
chmod 700 /tilde/key/*
tilde_username="$1"
tilde_name="$2"
key_file="$(find /tilde/key | tail -n1)"
if [ -f "$key_file" ]
then
while [ -z "$(cat $key_file | head -n1 | grep -- '-----BEGIN .* PRIVATE KEY-----')" ]
do
echo "$key_file does not look like a private key file, try again"
key_file="$(find /tilde/key | tail -n1)"
bash
done
ssh_command="ssh -i $key_file $tilde_username@$tilde_name"
echo "Key file located!"
fi
echo "Testing ssh connection to $tilde_name as user $tilde_username..."
connection_test "$ssh_command"
}
configure_network() {
echo "Press ENTER to configure network"
read dummy_var
nmtui
}

View File

@ -5,5 +5,3 @@ not thoroughly tested, but have been proven to work at least twice
## todo: ## todo:
- exit traps/cleanup functions - exit traps/cleanup functions
- remove commented code
- make it prettier w/ more `clear`s and cool headers

View File

@ -1,16 +1,13 @@
#!/bin/sh #!/bin/sh
export tilde_name="" tilde_name=""
export tilde_username="" tilde_username=""
export key_file="" key_file=""
export startx_command="startlxqt" startx_command="startlxqt"
. ./util.sh . ./util.sh
. ./basics.sh . ./connection.sh
. ./irc.sh . ./user.sh
#configure_tilde
#configure_irc
choice="" choice=""
@ -18,50 +15,26 @@ clear
echo "=== Welcome to Tildelinux! ===" echo "=== Welcome to Tildelinux! ==="
configure_network || fail configure_network || fail
clear clear
echo "What tilde would you like to connect to? (use the domain name as you would for ssh)" echo "What tilde would you like to connect to? (use the domain name as you would for ssh)"
read tilde_name read tilde_name
echo "What is your username for $tilde_name?" echo "What is your username for $tilde_name?"
read tilde_username read tilde_username
echo "Now you will need to provide tildelinux with your ssh private key" echo "Now you will need to provide tildelinux with your ssh private key"
echo "By whatever means, place your private key in the /tilde/key directory" echo "By whatever means, place your private key in the /tilde/key directory"
echo "Here is a shell, once the key is in the proper location, simply type \"exit\" to close the shell and continue" echo "Here is a shell, once the key is in the proper location, simply type \"exit\" to close the shell and continue"
bash bash
echo "Looking for keyfile..." echo "Looking for keyfile..."
check_key_file || fail check_key_file "$tilde_username" "$tilde_name" || fail
echo "Key file located!" # the key_file variable has now been set by check_key_file
echo "Testing connection to the tilde..."
connection_test || fail
echo "Connection success!"
echo "Configuring user..." echo "Configuring user..."
configure_user || fail configure_user "$key_file" "$tilde_username" "$tilde_name" "$startx_command" || fail
echo "User configured!" echo "User configured!"
#clear
#echo "=== IRC Configuration ==="
#echo "Now we will begin configuring a WeeChat IRC relay"
#echo "Select the option that applies to you:"
#echo "1. I have a WeeChat relay configured on my chosen tilde"
#echo "2. I do not have a WeeChat relay configured on my chosen tilde"
#read -r choice
#while :
#do
# case "$choice" in
# "1")
# clear
# set_relay_config "existing"
# break
# ;;
# "2")
# clear
# echo "Since no WeeChat relay exists, a new one will be configured"
# echo "First, provide a port number and password for this relay"
# set_relay_config "new"
# break
# ;;
# "*")
# echo "Please enter a valid option"
# ;;
# esac
#done
echo "All done! Enjoy using Tildelinux!" echo "All done! Enjoy using Tildelinux!"
rm ./setup_incomplete rm ./setup_incomplete
systemctl restart 'getty@tty1.service' systemctl restart 'getty@tty1.service'

View File

@ -1,66 +1,59 @@
#!/bin/sh #!/bin/sh
create_user_files() {
ssh_command="" key_file="$1"
tilde_username="$2"
check_key_file() { tilde_name="$3"
chmod 700 /tilde/key/* user_homedir="$4"
key_file="$(find /tilde/key | tail -n1)"
if [ -f "$key_file" ]
then
while [ -z "$(cat $key_file | head -n1 | grep -- '-----BEGIN .* PRIVATE KEY-----')" ]
do
echo "$key_file does not look like a private key file, try again"
key_file="$(find /tilde/key | tail -n1)"
bash
done
ssh_command="ssh -i $key_file $tilde_username@$tilde_name"
fi
}
connection_test() {
while ! $ssh_command 'exit'
do
echo "There seems to be a problem connecting to the tilde"
echo "Use this shell to try and fix any problems, then try connecting again by exiting the shell"
bash
done
}
configure_user_files() {
# TODO: refactor file creation to be script-independant
# user_homedir=$1
# tilde_homedir=$2
user_homedir="/home/$tilde_username" user_homedir="/home/$tilde_username"
tilde_homedir="/tilde/$tilde_name/home/$tilde_username" tilde_homedir="/tilde/$tilde_name/home/$tilde_username"
sed "s|{{ TILDE_USERNAME }}|$tilde_username|g" "./dotprofile.template" > "$user_homedir/.profile" sed "s|{{ TILDE_USERNAME }}|$tilde_username|g" "./templates/dotprofile.template" > "$user_homedir/.profile"
sed -i "s|{{ TILDE_NAME }}|$tilde_name|g" "$user_homedir/.profile" sed -i "s|{{ TILDE_NAME }}|$tilde_name|g" "$user_homedir/.profile"
sed -i "s|{{ KEY_FILE }}|$key_file|g" "$user_homedir/.profile" sed -i "s|{{ KEY_FILE }}|$key_file|g" "$user_homedir/.profile"
sed -i "s|{{ TILDE_HOMEDIR }}|$tilde_homedir|g" "$user_homedir/.profile" sed -i "s|{{ TILDE_HOMEDIR }}|$tilde_homedir|g" "$user_homedir/.profile"
sed -i "s|{{ USER_HOMEDIR }}|$user_homedir|g" "$user_homedir/.profile" sed -i "s|{{ USER_HOMEDIR }}|$user_homedir|g" "$user_homedir/.profile"
sed "s|{{ TILDE_HOMEDIR }}|$tilde_homedir|g" "./user-dirs.dirs.template" > "$user_homedir/.config/user-dirs.dirs" sed "s|{{ TILDE_HOMEDIR }}|$tilde_homedir|g" "./templates/user-dirs.dirs.template" > "$user_homedir/.config/user-dirs.dirs"
sed "s|{{ TILDE_HOMEDIR }}|$tilde_homedir|g" "./dotbashrc.template" > "$user_homedir/.bashrc" sed "s|{{ TILDE_HOMEDIR }}|$tilde_homedir|g" "./templates/dotbashrc.template" > "$user_homedir/.bashrc"
sed "s|{{ TILDE_USERNAME }}|$tilde_username|g" "./templates/remote_command.template" > "/tilde/bin/$tilde_name"
sed -i "s|{{ TILDE_NAME }}|$tilde_name|g" "/tilde/bin/$tilde_name"
sed -i "s|{{ KEY_FILE }}|$key_file|g" "/tilde/bin/$tilde_name"
chmod +x "/tilde/bin/$tilde_name"
} }
configure_user() { configure_user() {
key_file="$1"
tilde_username="$2"
tilde_name="$3"
startx_command="$4"
user_homedir="/home/$tilde_username" user_homedir="/home/$tilde_username"
tilde_homedir="/tilde/$tilde_name/home/$tilde_username" tilde_homedir="/tilde/$tilde_name/home/$tilde_username"
mkdir -p "$tilde_homedir" mkdir -p "$tilde_homedir"
mkdir -p "$user_homedir"
mkdir "/tilde/$tilde_name/key/"
mv "$key_file" "/tilde/$tilde_name/key/"
key_file="/tilde/$tilde_name/key/$(basename $key_file)"
if ! test -d "$user_homedir"
then
mkdir -p "$user_homedir"
cp "/root/.gtkrc-2.0" "$user_homedir"
cp -r "/root/.config" "$user_homedir"
fi
touch "$user_homedir/first_login" touch "$user_homedir/first_login"
cp "/root/.gtkrc-2.0" "$user_homedir" create_user_files "$key_file" "$tilde_username" "$tilde_name" "$user_homedir"
cp -r "/root/.config" "$user_homedir"
configure_user_files # this will fail when configuring a new tilde for an existing user
useradd "$tilde_username" -d "$user_homedir"
useradd "$tilde_username" -d "$user_homedir"
chsh -s '/bin/bash' "$tilde_username"
chown -R "$tilde_username:$tilde_username" "$user_homedir" chown -R "$tilde_username:$tilde_username" "$user_homedir"
chsh -s '/bin/bash' "$tilde_username"
chown -R "$tilde_username:$tilde_username" "$tilde_homedir" chown -R "$tilde_username:$tilde_username" "$tilde_homedir"
chown "$tilde_username:$tilde_username" "$key_file" chown "$tilde_username:$tilde_username" "$key_file"
@ -74,9 +67,3 @@ configure_user() {
echo "Type=idle" >> '/etc/systemd/system/getty@tty1.service.d/override.conf' echo "Type=idle" >> '/etc/systemd/system/getty@tty1.service.d/override.conf'
systemctl daemon-reload systemctl daemon-reload
} }
configure_network() {
echo "Press ENTER to configure network"
read dummy_var
nmtui
}