-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlxc-up
executable file
·68 lines (60 loc) · 1.83 KB
/
lxc-up
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
LXCSTART="sudo /usr/bin/lxc-start"
SSHFS="/usr/bin/sshfs"
SLEEP="/bin/sleep"
TMUX="/usr/bin/tmux"
SSH="/usr/bin/ssh"
PG_TABLE="/tmp/pg_table"
PG_PORT="5432"
# Function that checks if a file names /tmp/pg_table exists.
# If true, returns 5432.
# If false, cheks the higher value in the file and returns the next one.
function get-next-port {
if [ ! -f "/tmp/pg_table" ]
then
echo 5432
else
retour=$(($(awk '{print $2}' $PG_TABLE | sort | awk 'END{print}')+1))
if [ $retour -eq "1" ]
then
echo 5432
else
echo $retour
fi
fi
}
# Function that get the port corresponding to the name passed as argument.
function get-container-port {
echo $(grep $1 $PG_TABLE | awk '{print $2}')
}
arguments=( "$@" )
if [ ${#arguments[@]} -lt "1" ]
then
echo "Missing options"
exit 0
fi
# Starts the container, mounts the homedir of the remote user in sshfs/container_name.
$LXCSTART -n $1
$SLEEP 10
if [ ! -d "$HOME/sshfs/$1" ]
then
echo "Creating $HOME/sshfs/$1"
mkdir $HOME/sshfs/$1
fi
sshfs $1: -o idmap=user $HOME/sshfs/$1
# Do tmux things.
($TMUX has 2> /dev/null || $TMUX start) && $TMUX -2 new-session -d -s $1
$TMUX rename-window -t $1:0 CODE
$TMUX select-window -t $SESSION:CODE
$TMUX new-window -t $1:1 -n SERVER
$TMUX select-window -t $SESSION:SERVER
$TMUX send-keys "ssh -Y $1" C-m
$TMUX select-window -t $SESSION:CODE
$TMUX send-keys "cd sshfs/$1" C-m C-l
# Creates an SSH tunnel betweek localhost:new_port and remote:5432 in a dedicated tmux session.
# Adds a new line in /tmp/pg_table
new_port=$(get-next-port)
$TMUX new-window -t TUNS -n $1 2> /dev/null || ( $TMUX new-session -d -s TUNS && $TMUX rename-window -t TUNS:0 $1 )
$TMUX send-keys -t TUNS:$1 "ssh -N $1 -L $new_port:localhost:5432" C-m C-l
echo "$1 $new_port" >> $PG_TABLE
$TMUX a -t $1