Replace 'yunohost app setting' by a good old grep for more speed (close #11)

This commit is contained in:
Julien VAUBOURG
2015-07-24 20:56:40 +02:00
parent 09f23b5a08
commit 8cf1917cbc
2 changed files with 46 additions and 66 deletions

View File

@@ -33,19 +33,9 @@ has_hotspot_app() {
} }
is_hotspot_knowme() { is_hotspot_knowme() {
gotcha=0 hotspot_vpnclient=$(ynh_setting_get hotspot vpnclient)
while [ "${gotcha}" -eq 0 ]; do [ "${hotspot_vpnclient}" == yes ]
value=$(yunohost app setting hotspot vpnclient)
if [[ "${value}" =~ "An instance is already running" ]]; then
sleep $(($((RANDOM%5)) + 1))
else
gotcha=1
fi
done
[ "${value}" == yes ]
} }
is_ip6addr_set() { is_ip6addr_set() {
@@ -145,33 +135,19 @@ stop_openvpn() {
## Tools ## Tools
moulinette_get() { ynh_setting_get() {
var=${1} app=${1}
gotcha=0 setting=${2}
while [ "${gotcha}" -eq 0 ]; do grep "^${setting}:" "/etc/yunohost/apps/${app}/settings.yml" | sed s/^[^:]\\+:\\s*[\"\']\\?// | sed s/\\s*[\"\']\$//
value=$(yunohost app setting vpnclient "${var}")
if [[ "${value}" =~ "An instance is already running" ]]; then
sleep $(($((RANDOM%5)) + 1))
else
gotcha=1
fi
done
echo "${value}"
} }
moulinette_set() { ynh_setting_set() {
var=${1} app=${1}
value=${2} setting=${2}
value=${3}
msg=$(yunohost app setting vpnclient "${var}" -v "${value}") yunohost app setting "${app}" "${setting}" -v "${value}"
if [ ! $? -eq 0 ]; then
echo "${msg}" >&2
exit 1
fi
} }
if [ "$1" != restart ]; then if [ "$1" != restart ]; then
@@ -205,16 +181,16 @@ if [ "$1" != restart ]; then
echo -n "Retrieving Yunohost settings... " echo -n "Retrieving Yunohost settings... "
ynh_service_enabled=$(moulinette_get service_enabled) ynh_service_enabled=$(ynh_setting_get vpnclient service_enabled)
ynh_server_name=$(moulinette_get server_name) ynh_server_name=$(ynh_setting_get vpnclient server_name)
ynh_server_port=$(moulinette_get server_port) ynh_server_port=$(ynh_setting_get vpnclient server_port)
ynh_server_proto=$(moulinette_get server_proto) ynh_server_proto=$(ynh_setting_get vpnclient server_proto)
ynh_ip6_addr=$(moulinette_get ip6_addr) ynh_ip6_addr=$(ynh_setting_get vpnclient ip6_addr)
ynh_login_user=$(moulinette_get login_user) ynh_login_user=$(ynh_setting_get vpnclient login_user)
old_ip6_gw=$(moulinette_get ip6_gw) old_ip6_gw=$(ynh_setting_get vpnclient ip6_gw)
old_wired_device=$(moulinette_get wired_device) old_wired_device=$(ynh_setting_get vpnclient wired_device)
old_server_ip6=$(moulinette_get server_ip6) old_server_ip6=$(ynh_setting_get vpnclient server_ip6)
new_ip6_gw=$(ip -6 route | grep default\ via | awk '{ print $3 }') new_ip6_gw=$(ip -6 route | grep default\ via | awk '{ print $3 }')
new_wired_device=$(ip route | awk '/default via/ { print $NF; }') new_wired_device=$(ip route | awk '/default via/ { print $NF; }')
@@ -280,9 +256,9 @@ case "${1}" in
fi fi
# Update dynamic settings # Update dynamic settings
moulinette_set server_ip6 "${new_server_ip6}" ynh_setting_set vpnclient server_ip6 "${new_server_ip6}"
moulinette_set ip6_gw "${new_ip6_gw}" ynh_setting_set vpnclient ip6_gw "${new_ip6_gw}"
moulinette_set wired_device "${new_wired_device}" ynh_setting_set vpnclient wired_device "${new_wired_device}"
# Fix configuration # Fix configuration
if has_hotspot_app && ! is_hotspot_knowme; then if has_hotspot_app && ! is_hotspot_knowme; then

View File

@@ -18,12 +18,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
function moulinette_get($var) { function ynh_setting_get($setting) {
return htmlspecialchars(exec('sudo yunohost app setting vpnclient '.escapeshellarg($var))); $value = exec("sudo grep \"^$setting:\" /etc/yunohost/apps/vpnclient/settings.yml");
$value = preg_replace('/^[^:]+:\s*["\']?/', '', $value);
$value = preg_replace('/\s*["\']$/', '', $value);
return htmlspecialchars($value);
} }
function moulinette_set($var, $value) { function ynh_setting_set($setting, $value) {
return exec('sudo yunohost app setting vpnclient '.escapeshellarg($var).' -v '.escapeshellarg($value)); return exec('sudo yunohost app setting vpnclient '.escapeshellarg($setting).' -v '.escapeshellarg($value));
} }
function stop_service() { function stop_service() {
@@ -61,16 +65,16 @@ function ipv6_compressed($ip) {
} }
dispatch('/', function() { dispatch('/', function() {
$ip6_net = moulinette_get('ip6_net'); $ip6_net = ynh_setting_get('ip6_net');
$ip6_net = ($ip6_net == 'none') ? '' : $ip6_net; $ip6_net = ($ip6_net == 'none') ? '' : $ip6_net;
$raw_openvpn = file_get_contents('/etc/openvpn/client.conf.tpl'); $raw_openvpn = file_get_contents('/etc/openvpn/client.conf.tpl');
set('service_enabled', moulinette_get('service_enabled')); set('service_enabled', ynh_setting_get('service_enabled'));
set('server_name', moulinette_get('server_name')); set('server_name', ynh_setting_get('server_name'));
set('server_port', moulinette_get('server_port')); set('server_port', ynh_setting_get('server_port'));
set('server_proto', moulinette_get('server_proto')); set('server_proto', ynh_setting_get('server_proto'));
set('login_user', moulinette_get('login_user')); set('login_user', ynh_setting_get('login_user'));
set('login_passphrase', moulinette_get('login_passphrase')); set('login_passphrase', ynh_setting_get('login_passphrase'));
set('ip6_net', $ip6_net); set('ip6_net', $ip6_net);
set('crt_client_exists', file_exists('/etc/openvpn/keys/user.crt')); set('crt_client_exists', file_exists('/etc/openvpn/keys/user.crt'));
set('crt_client_key_exists', file_exists('/etc/openvpn/keys/user.key')); set('crt_client_key_exists', file_exists('/etc/openvpn/keys/user.key'));
@@ -144,16 +148,16 @@ dispatch_put('/settings', function() {
stop_service(); stop_service();
moulinette_set('service_enabled', $service_enabled); ynh_setting_set('service_enabled', $service_enabled);
if($service_enabled == 1) { if($service_enabled == 1) {
moulinette_set('server_name', $_POST['server_name']); ynh_setting_set('server_name', $_POST['server_name']);
moulinette_set('server_port', $_POST['server_port']); ynh_setting_set('server_port', $_POST['server_port']);
moulinette_set('server_proto', $_POST['server_proto']); ynh_setting_set('server_proto', $_POST['server_proto']);
moulinette_set('login_user', $_POST['login_user']); ynh_setting_set('login_user', $_POST['login_user']);
moulinette_set('login_passphrase', $_POST['login_passphrase']); ynh_setting_set('login_passphrase', $_POST['login_passphrase']);
moulinette_set('ip6_net', $ip6_net); ynh_setting_set('ip6_net', $ip6_net);
moulinette_set('ip6_addr', $ip6_addr); ynh_setting_set('ip6_addr', $ip6_addr);
file_put_contents('/etc/openvpn/client.conf.tpl', $_POST['raw_openvpn']); file_put_contents('/etc/openvpn/client.conf.tpl', $_POST['raw_openvpn']);