Browse Source

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

Julien VAUBOURG 9 years ago
parent
commit
8411d9164b
5 changed files with 57 additions and 6370 deletions
  1. 29 78
      conf/ynh-hotspot
  2. BIN
      docs/box-project.png
  3. BIN
      docs/box-project_french.pdf
  4. 0 6268
      docs/box-project_french.svg
  5. 28 24
      sources/controller.php

+ 29 - 78
conf/ynh-hotspot

@@ -261,50 +261,19 @@ stop_hostapd() {
 
 ## Tools
 
-moulinette_get() {
-  var=${1}
-  gotcha=0
+ynh_setting_get() {
+  app=${1}
+  setting=${2}
 
-  while [ "${gotcha}" -eq 0 ]; do
-    value=$(yunohost app setting hotspot "${var}")
-
-    if [[ "${value}" =~ "An instance is already running" ]]; then
-      sleep $(($((RANDOM%5)) + 1))
-    else
-      gotcha=1
-    fi
-  done
-
-  echo "${value}"
-}
-
-moulinette_vpnclient_get() {
-  var=${1}
-  gotcha=0
-
-  while [ "${gotcha}" -eq 0 ]; do
-    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}"
+  grep "^${setting}:" "/etc/yunohost/apps/${app}/settings.yml" | sed s/^[^:]\\+:\\s*[\"\']\\?// | sed s/\\s*[\"\']\$//
 }
 
-moulinette_set() {
-  var=${1}
-  value=${2}
+ynh_setting_set() {
+  app=${1}
+  setting=${2}
+  value=${3}
 
-  msg=$(yunohost app setting hotspot "${var}" -v "${value}")
-
-  if [ ! $? -eq 0 ]; then
-    echo "${msg}" >&2
-    exit 1
-  fi
+  yunohost app setting "${app}" "${setting}" -v "${value}"
 }
 
 if [ "$1" != restart ]; then
@@ -319,23 +288,23 @@ if [ "$1" != restart ]; then
 
   echo -n "Retrieving Yunohost settings... "
 
-  ynh_service_enabled=$(moulinette_get service_enabled)
-  ynh_wifi_device=$(moulinette_get wifi_device)
-  ynh_wifi_channel=$(moulinette_get wifi_channel)
-  ynh_multissid=$(moulinette_get multissid)
-
-  IFS='|' read -a ynh_wifi_ssid <<< "$(moulinette_get wifi_ssid)"
-  IFS='|' read -a ynh_wifi_secure <<< "$(moulinette_get wifi_secure)"
-  IFS='|' read -a ynh_wifi_passphrase <<< "$(moulinette_get wifi_passphrase)"
-  IFS='|' read -a ynh_ip6_addr <<< "$(moulinette_get ip6_addr)"
-  IFS='|' read -a ynh_ip6_net <<< "$(moulinette_get ip6_net)"
-  IFS='|' read -a ynh_ip6_dns0 <<< "$(moulinette_get ip6_dns0)"
-  IFS='|' read -a ynh_ip6_dns1 <<< "$(moulinette_get ip6_dns1)"
-  IFS='|' read -a ynh_ip4_dns0 <<< "$(moulinette_get ip4_dns0)"
-  IFS='|' read -a ynh_ip4_dns1 <<< "$(moulinette_get ip4_dns1)"
-  IFS='|' read -a ynh_ip4_nat_prefix <<< "$(moulinette_get ip4_nat_prefix)"
-
-  old_internet_device=$(moulinette_get internet_device)
+  ynh_service_enabled=$(ynh_setting_get hotspot service_enabled)
+  ynh_wifi_device=$(ynh_setting_get hotspot wifi_device)
+  ynh_wifi_channel=$(ynh_setting_get hotspot wifi_channel)
+  ynh_multissid=$(ynh_setting_get hotspot multissid)
+
+  IFS='|' read -a ynh_wifi_ssid <<< "$(ynh_setting_get hotspot wifi_ssid)"
+  IFS='|' read -a ynh_wifi_secure <<< "$(ynh_setting_get hotspot wifi_secure)"
+  IFS='|' read -a ynh_wifi_passphrase <<< "$(ynh_setting_get hotspot wifi_passphrase)"
+  IFS='|' read -a ynh_ip6_addr <<< "$(ynh_setting_get hotspot ip6_addr)"
+  IFS='|' read -a ynh_ip6_net <<< "$(ynh_setting_get hotspot ip6_net)"
+  IFS='|' read -a ynh_ip6_dns0 <<< "$(ynh_setting_get hotspot ip6_dns0)"
+  IFS='|' read -a ynh_ip6_dns1 <<< "$(ynh_setting_get hotspot ip6_dns1)"
+  IFS='|' read -a ynh_ip4_dns0 <<< "$(ynh_setting_get hotspot ip4_dns0)"
+  IFS='|' read -a ynh_ip4_dns1 <<< "$(ynh_setting_get hotspot ip4_dns1)"
+  IFS='|' read -a ynh_ip4_nat_prefix <<< "$(ynh_setting_get hotspot ip4_nat_prefix)"
+
+  old_internet_device=$(ynh_setting_get hotspot internet_device)
   new_internet_device=$(ip route | awk '/default via/ { print $NF; }')
 
   # Switch the NAT interface if there is a VPN
@@ -345,24 +314,6 @@ if [ "$1" != restart ]; then
   fi
 
   echo "OK"
-
-  # Check IPv6 delegated prefix from vpnclient
-#  vpnclient_ip6_net=$(moulinette_vpnclient_get ip6_net)
-#
-#  if [ ! -z "${vpnclient_ip6_addr}" ]; then
-#    if [ "${ynh_ip6_net}" == none ]; then
-#      ynh_ip6_net=$vpnclient_ip6_net
-#      ynh_ip6_addr=$(moulinette_vpnclient_get ip6_addr)
-#
-#      moulinette_set ip6_net "${ynh_ip6_net}"
-#      moulinette_set ip6_addr "${ynh_ip6_addr}"
-#    else
-#      if [ "${ynh_ip6_net}" != "${vpnclient_ip6_net}" ]; then
-#        echo "[WARN] The IPv6 delegated prefix is different from the vpnclient one"
-#      fi
-#    fi
-#  fi
-
 fi
 
 # Script
@@ -378,9 +329,9 @@ case "$1" in
       touch /tmp/.ynh-hotspot-started
 
       if [ "${new_internet_device}" == tun0 ]; then
-        moulinette_set vpnclient yes
+        ynh_setting_set hotspot vpnclient yes
       else
-        moulinette_set vpnclient no
+        ynh_setting_set hotspot vpnclient no
       fi
 
       # Check old state of the ipv4 NAT settings
@@ -453,7 +404,7 @@ case "$1" in
       done
 
       # Update dynamic settings
-      moulinette_set internet_device "${new_internet_device}"
+      ynh_setting_set hotspot internet_device "${new_internet_device}"
     fi
   ;;
   stop)

BIN
docs/box-project.png


BIN
docs/box-project_french.pdf


File diff suppressed because it is too large
+ 0 - 6268
docs/box-project_french.svg


+ 28 - 24
sources/controller.php

@@ -18,12 +18,16 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-function moulinette_get($var) {
-  return htmlspecialchars(exec('sudo yunohost app setting hotspot '.escapeshellarg($var)));
+function ynh_setting_get($setting) {
+  $value = exec("sudo grep \"^$setting:\" /etc/yunohost/apps/hotspot/settings.yml");
+  $value = preg_replace('/^[^:]+:\s*["\']?/', '', $value);
+  $value = preg_replace('/\s*["\']$/', '', $value);
+
+  return htmlspecialchars($value);
 }
 
-function moulinette_set($var, $value) {
-  return exec('sudo yunohost app setting hotspot '.escapeshellarg($var).' -v '.escapeshellarg($value));
+function ynh_setting_set($setting, $value) {
+  return exec('sudo yunohost app setting hotspot '.escapeshellarg($setting).' -v '.escapeshellarg($value));
 }
 
 function stop_service() {
@@ -107,9 +111,9 @@ dispatch('/', function() {
   $devs = iw_devices();
   $devs_list = '';
 
-  $wifi_device = moulinette_get('wifi_device');
-  $multissid = moulinette_get('multissid');
-  $wifi_channel = moulinette_get('wifi_channel');
+  $wifi_device = ynh_setting_get('wifi_device');
+  $multissid = ynh_setting_get('multissid');
+  $wifi_channel = ynh_setting_get('wifi_channel');
 
   foreach($devs AS $dev) {
     $dev_multissid = iw_multissid($dev);
@@ -118,15 +122,15 @@ dispatch('/', function() {
     $devs_list .= "<li $active data-multissid='$dev_multissid'><a href='#'>$dev</a></li>\n";
   }
 
-  $wifi_ssid = getArray(moulinette_get('wifi_ssid'));
-  $wifi_secure = getArray(moulinette_get('wifi_secure'));
-  $wifi_passphrase = getArray(moulinette_get('wifi_passphrase'));
-  $ip6_net = getArray(moulinette_get('ip6_net'));
-  $ip6_dns0 = getArray(moulinette_get('ip6_dns0'));
-  $ip6_dns1 = getArray(moulinette_get('ip6_dns1'));
-  $ip4_nat_prefix = getArray(moulinette_get('ip4_nat_prefix'));
-  $ip4_dns0 = getArray(moulinette_get('ip4_dns0'));
-  $ip4_dns1 = getArray(moulinette_get('ip4_dns1'));
+  $wifi_ssid = getArray(ynh_setting_get('wifi_ssid'));
+  $wifi_secure = getArray(ynh_setting_get('wifi_secure'));
+  $wifi_passphrase = getArray(ynh_setting_get('wifi_passphrase'));
+  $ip6_net = getArray(ynh_setting_get('ip6_net'));
+  $ip6_dns0 = getArray(ynh_setting_get('ip6_dns0'));
+  $ip6_dns1 = getArray(ynh_setting_get('ip6_dns1'));
+  $ip4_nat_prefix = getArray(ynh_setting_get('ip4_nat_prefix'));
+  $ip4_dns0 = getArray(ynh_setting_get('ip4_dns0'));
+  $ip4_dns1 = getArray(ynh_setting_get('ip4_dns1'));
 
   for($i = 0; $i < $multissid; $i++) {
     $ssid = [
@@ -145,11 +149,11 @@ dispatch('/', function() {
     array_push($ssids, $ssid);
   }
 
-  $ip6_net = moulinette_get('ip6_net');
+  $ip6_net = ynh_setting_get('ip6_net');
   $ip6_net = ($ip6_net == 'none') ? '' : getArray($ip6_net);
-  $ip4_nat_prefix = getArray(moulinette_get('ip4_nat_prefix'));
+  $ip4_nat_prefix = getArray(ynh_setting_get('ip4_nat_prefix'));
 
-  set('service_enabled', moulinette_get('service_enabled'));
+  set('service_enabled', ynh_setting_get('service_enabled'));
   set('ssids', $ssids);
   set('wifi_device', $wifi_device);
   set('wifi_channel', $wifi_channel);
@@ -278,7 +282,7 @@ dispatch_put('/settings', function() {
 
   stop_service();
 
-  moulinette_set('service_enabled', $service_enabled);
+  ynh_setting_set('service_enabled', $service_enabled);
   $settings = array();
 
   if($service_enabled == 1) {
@@ -288,12 +292,12 @@ dispatch_put('/settings', function() {
       }
     }
 
-    moulinette_set('multissid', count($ssids));
-    moulinette_set('wifi_device', $_POST['wifi_device']);
-    moulinette_set('wifi_channel', $_POST['wifi_channel']);
+    ynh_setting_set('multissid', count($ssids));
+    ynh_setting_set('wifi_device', $_POST['wifi_device']);
+    ynh_setting_set('wifi_channel', $_POST['wifi_channel']);
 
     foreach($settings as $setting => $value) {
-      moulinette_set($setting, preg_replace('/\|$/', '', $value));
+      ynh_setting_set($setting, preg_replace('/\|$/', '', $value));
     }
 
     $retcode = start_service();