start refactor quicktinc.sh
This commit is contained in:
parent
e4c7c8f1bb
commit
2bc57dc9d0
|
@ -9,7 +9,11 @@ IMAGE="fschl/tinc"
|
||||||
function usage() {
|
function usage() {
|
||||||
echo "
|
echo "
|
||||||
Usage:
|
Usage:
|
||||||
$0 [OPTIONS]
|
$0 run --net=<network> - start a container for $network with existing configuration
|
||||||
|
|
||||||
|
or create configuration with:
|
||||||
|
|
||||||
|
$0 init [OPTIONS]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
--net=NET_NAME Network name (required)
|
--net=NET_NAME Network name (required)
|
||||||
|
@ -18,11 +22,12 @@ Options:
|
||||||
--private-ip=PRIVATE_IP Node's private IP (required)
|
--private-ip=PRIVATE_IP Node's private IP (required)
|
||||||
--connect-to=HOST Name of another node (optional, repeatable)
|
--connect-to=HOST Name of another node (optional, repeatable)
|
||||||
--interface=tun0 Network interface to create (optional, default=tun0)
|
--interface=tun0 Network interface to create (optional, default=tun0)
|
||||||
--config=/srv/tinc Where to save tinc networks (optional, default=$(pwd)/etc-tinc)
|
--config=/etc/tinc Where to save tinc networks (optional, default=/etc/tinc)
|
||||||
--up Also start the daemon
|
--up Also start the daemon
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
$0 --net=demonet --node=node23 --public-ip=8.9.10.11 --private-ip=10.0.0.23 --connect-to=node1 --connect-to=node2 --up
|
$0 --net=demonet --node=node23 --public-ip=8.9.10.11 --private-ip=10.0.0.23 --connect-to=node1 --connect-to=node2 --up
|
||||||
|
$0 --n=demonet --o=node23 --p=8.9.10.11 --v=10.0.0.23 --c=node1 --connect-to=node2 --up
|
||||||
|
|
||||||
Report bugs to <https://github.com/j3k0/quicktinc>
|
Report bugs to <https://github.com/j3k0/quicktinc>
|
||||||
"
|
"
|
||||||
|
@ -80,47 +85,71 @@ if [ "_$INTERFACE" = "_" ]; then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "_$TINC_HOME" = "_" ]; then
|
if [ "_$TINC_HOME" = "_" ]; then
|
||||||
TINC_HOME=$(pwd)/etc-tinc
|
TINC_HOME=/etc/tinc
|
||||||
fi
|
fi
|
||||||
|
|
||||||
function tinc() {
|
function tinc() {
|
||||||
docker run --rm --net=host --device=/dev/net/tun --cap-add NET_ADMIN --volume $TINC_HOME:/etc/tinc $IMAGE -n $NET_NAME "$@"
|
docker run --rm --net=host --device=/dev/net/tun --cap-add NET_ADMIN --volume $TINC_HOME:/etc/tinc $IMAGE -n $NET_NAME "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Initialize configuration file
|
init_node() {
|
||||||
tinc init $NODE_NAME
|
# Initialize configuration file
|
||||||
|
tinc init $NODE_NAME
|
||||||
|
|
||||||
# Setup host file
|
# Setup host file
|
||||||
# Declare public and private IPs in the host file, CONFIG/NET/hosts/HOST
|
# Declare public and private IPs in the host file, CONFIG/NET/hosts/HOST
|
||||||
echo "Address = $PUBLIC_IP" >> $TINC_HOME/$NET_NAME/hosts/$NODE_NAME
|
echo "Address = $PUBLIC_IP" >> $TINC_HOME/$NET_NAME/hosts/$NODE_NAME
|
||||||
echo "Subnet = $PRIVATE_IP/32" >> $TINC_HOME/$NET_NAME/hosts/$NODE_NAME
|
echo "Subnet = $PRIVATE_IP/32" >> $TINC_HOME/$NET_NAME/hosts/$NODE_NAME
|
||||||
|
|
||||||
# Tweak the config to add our particular setup
|
# Tweak the config to add our particular setup
|
||||||
tinc add AddressFamily ipv4
|
tinc add AddressFamily ipv4
|
||||||
tinc add Device /dev/net/tun
|
tinc add Device /dev/net/tun
|
||||||
tinc add Interface $INTERFACE
|
tinc add Interface $INTERFACE
|
||||||
if [ "_$CONNECT_TO" != "_" ]; then
|
if [ "_$CONNECT_TO" != "_" ]; then
|
||||||
for i in $CONNECT_TO; do
|
for i in $CONNECT_TO; do
|
||||||
tinc add ConnectTo $i
|
tinc add ConnectTo $i
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Edit the tinc-up script
|
# Edit the tinc-up script
|
||||||
cat << EOF > $TINC_HOME/$NET_NAME/tinc-up
|
cat << EOF > $TINC_HOME/$NET_NAME/tinc-up
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
ifconfig \$INTERFACE $PRIVATE_IP netmask 255.255.255.0
|
ifconfig \$INTERFACE $PRIVATE_IP netmask 255.255.255.0
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
cat << EOF > $TINC_HOME/$NET_NAME/tinc-down
|
cat << EOF > $TINC_HOME/$NET_NAME/tinc-down
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
ifconfig \$INTERFACE down
|
ifconfig \$INTERFACE down
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
chmod +x $TINC_HOME/$NET_NAME/tinc-up
|
chmod +x $TINC_HOME/$NET_NAME/tinc-up
|
||||||
chmod +x $TINC_HOME/$NET_NAME/tinc-down
|
chmod +x $TINC_HOME/$NET_NAME/tinc-down
|
||||||
|
}
|
||||||
|
|
||||||
if [ "_$TINC_UP" != "_" ]; then
|
run_container() {
|
||||||
|
# if [ "_$TINC_UP" != "_" ]; then
|
||||||
NAME=tinc_$NET_NAME_$NODE_NAME
|
NAME=tinc_$NET_NAME_$NODE_NAME
|
||||||
docker run -d --restart=always --name=$NAME --net=host --device=/dev/net/tun --cap-add NET_ADMIN --volume $TINC_HOME:/etc/tinc $IMAGE -n $NET_NAME start -D
|
docker run -d --restart=always --name=$NAME --net=host --device=/dev/net/tun --cap-add NET_ADMIN --volume $TINC_HOME:/etc/tinc $IMAGE -n $NET_NAME start -D
|
||||||
echo "Docker container started with name: $NAME"
|
echo "Docker container started with name: $NAME"
|
||||||
fi
|
# fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
main() {
|
||||||
|
local cmd=$1
|
||||||
|
|
||||||
|
if [[ -z "$cmd" ]]; then
|
||||||
|
usage
|
||||||
|
fi
|
||||||
|
|
||||||
|
case "$cmd" in
|
||||||
|
init)
|
||||||
|
init_node
|
||||||
|
;;
|
||||||
|
run)
|
||||||
|
run_container
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
main "$@"
|
||||||
|
|
Loading…
Reference in New Issue