forked from agustim/avahi-ps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avahi-ps
executable file
·205 lines (178 loc) · 5.05 KB
/
avahi-ps
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/bash
#
# avahi-ps: Avahi Publish and Search (Avahi Publicar i Buscar)
#
# This script provides mechanisms to publish and search services over the
# network. It has a modular structure, so different services publishing and
# retrieval means can be used simultaneously in the form of plugs.
#
# (Aquest script proporciona mecanismes per publicar i cercar serveis a través
# de la xarxa. Té una estructura modular, per tal que la publicació i obtenció
# dels serveis pot fer-se simultàniament per diferents mitjans en forma de
# "plugs".
##################
# Main functions #
##################
# avahi-ps-help(): print help and usage examples
avahi-ps-help() {
cat <<EOF
avahi-ps (Avahi Publish and Search) is a system to publish local services and
discover remote ones using Avahi and other available modules plugged-in.
Usage: $0 publish|unpublish|search|info <options>
Examples:
- Publishing a local service to the network:
$0 publish <description> <type> <port> [txt]
<description>: a short text describing the service
<type>: service type
<port>: service port
<txt>: additional information, formatted as
'attribute1=value1&attribute2=value2&...&attributeN=valueN'
- Unpublishing a local service to the network
$0 unpublish [type] [port]
- Searching for services on the network:
$0 search [type] [hostname]
- Showing available information:
$0 info <variable>
<variable>: ip|cloud|tincdev|communitydev
EOF
exit 0
}
# print help if called without parameters
[[ $# -lt 1 ]] && avahi-ps-help
# err(): print error message and exit with error code 1
err() {
echo Error: "$@"
exit 1
}
# filepath(): print the path where this file is stored
filepath() {
local _dir="$(dirname "$0")"
echo $(cd "$_dir" && pwd)
}
#############
# Variables #
#############
FILE_PATH=$(filepath)
INSTALL_PATH="/usr/sbin"
INSTALL_PLUG_PATH="/usr/share/avahi-ps"
if [[ $FILE_PATH == $INSTALL_PATH ]]
then
FILE_PATH="$INSTALL_PLUG_PATH"
fi
# Default values for several values (may be overwritten)
PUBLIC_DEVICE="eth0"
CLOUD_NAME="guifi"
# deprecated: DEVICE_NAME="${CLOUD_NAME}"
DEVICE_NAME="demo"
GETINCONF_FILE=/etc/getinconf-client.conf
CLOUDYCONF_FILE=/etc/cloudy/cloudy.conf
# Import settings from getinconf's configuration file
if [[ -f $GETINCONF_FILE ]]
then
. $GETINCONF_FILE
DEVICE_NAME="${NETWORK_NAME}"
if [[ $GETINCONF_IGNORE != "1" ]]
then
PUBLIC_DEVICE="${INTERNAL_DEV}"
fi
fi
# Import settings from the main Cloudy configuration file
if [[ -f $CLOUDYCONF_FILE ]]
then
. $CLOUDYCONF_FILE
if [[ $PRIMARYINTERFACE != "" ]]
then
PUBLIC_DEVICE="${PRIMARYINTERFACE}"
fi
fi
# Get the CN-wide valid "public" IPv4 address
if [[ -z "$PUBLIC_IP" ]]
then
PUBLIC_IP=$(ip addr show dev $PUBLIC_DEVICE|grep "global.* $PUBLIC_DEVICE\$"|awk '{print $2}'|awk -F "/" {'print $1'})
# deprecated: PUBLIC_IP=$(ifconfig $PUBLIC_DEVICE|grep "inet "|awk '{print $2}'|awk -F ":" '{print $2}')
fi
FILENAME="avahi-ps-"
NODENAME=$(uname -n)
PLUG_DIR="$FILE_PATH/plugs/"
load_plug() {
local _plug_path="${PLUG_DIR}${FILENAME}${1}"
[[ -f $_plug_path ]] || err "\"$_plug_path\" doesn't exist!"
. "$_plug_path"
}
load_plug_if_exists() {
local _plug_path="${PLUG_DIR}${FILENAME}${1}"
[[ -f $_plug_path ]] && . "$_plug_path"
}
#########
# Plugs #
#########
# Define the default plugs for error managing, execution location, service
# storage and information exchange [database] backend
ERRORS_PLUG="errors"
EXECUTE_IN="memory"
SAVE_SERVICE="none"
DATABASE="none"
# Load settings from avahi-ps configuration file
AVAHIPS_FILE=/etc/avahi-ps.conf
if [[ -f $AVAHIPS_FILE ]]
then
. $AVAHIPS_FILE
fi
# Depending on whether SEARCH_ONLY is set or not, and what to, use the specified
# plugin as the "database" backend
if [[ ! -z "$SEARCH_ONLY" ]]; then
if [[ "$SEARCH_ONLY" == "avahi" ]]; then
ERRORS_PLUG="errors"
EXECUTE_IN="memory"
SAVE_SERVICE="none"
DATABASE="none"
else
ERRORS_PLUG="errors"
EXECUTE_IN="none"
SAVE_SERVICE="none"
DATABASE="$SEARCH_ONLY"
fi
fi
# Actually load the plugs
load_plug "$ERRORS_PLUG"
load_plug "$EXECUTE_IN"
load_plug_if_exists "$SAVE_SERVICE"
# The database plugs must not be loaded directly, since there can be many
# database backends. Therefore, we load it one by one
for db in $DATABASE; do
load_plug "$db"
done
##############################
# Perform the actual actions #
##############################
# Get the action from the command line
action=$1
# Available actions
actions="publish search unpublish"
if [[ "$actions" =~ (^| )"$action"($| ) ]]; then
# The called action is one of the available actions
shift
for tipus in ${EXECUTE_IN} ${DATABASE}; do
# Check if the plug+action combination exists and execute it
[ ! -z "$(typeset -f ${tipus}_${action}_service)" ] && ${tipus}_${action}_service "$@"
done
elif [[ "$action" == "info" ]]; then
# "info" action called
case "$2" in
"ip")
echo "$PUBLIC_IP"
;;
"cloud")
echo "$CLOUD_NAME"
;;
"tincdev")
echo "$DEVICE_NAME"
;;
"communitydev")
echo "$PUBLIC_DEVICE"
;;
esac
else
# Unknown action requested; print help
avahi-ps-help
fi