summarylogtreecommitdiffstats
path: root/ivpn.install
blob: ae9ac6d6210723d7320d24aeb26db19e1216cdaa (plain)
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
#!/usr/bin/env bash

silent() {
  "$@" > /dev/null 2>&1
}

has_systemd() {
  # Some OS vendors put systemd in ... different places ...
  [ -d "/lib/systemd/system/" -o -d "/usr/lib/systemd/system" ] && silent which systemctl
}

ivpnlogout() {
  if [ -f /usr/bin/ivpn ]; then
      echo "[+] Logging out..."
      /usr/bin/ivpn logout || echo "[-] Failed to log out"
  fi
}

disconnect() {
  if [ -f /usr/bin/ivpn ]; then
    echo "[+] Trying to disable firewall..."
    /usr/bin/ivpn firewall -off || echo "[-] Failed to disable firewall"

    echo "[+] Trying to disconnect..."
    /usr/bin/ivpn disconnect || echo "[-] Failed to disconnect"
  fi
}

try_systemd_stop() {
    if has_systemd ; then
        echo "[ ] systemd detected. Trying to stop service ..."

        echo "[+] Stopping service"
        silent systemctl stop ivpn-service

        echo "[+] Disabling service"
        silent systemctl disable ivpn-service
    fi
}

erase_leftovers() {
  local _IVPN_TMP="/etc/opt/ivpn"
  local _IVPN_LOG="/var/log/ivpn"
  local _IVPN_DIR="/opt/ivpn"

  # Normally, all files which were installed, deleted automatically
  # But ivpn-service also writing to 'mutable' additional temporary files (uninstaller know nothing about them)
  # Therefore, we are completely removing all content of '/etc/opt/ivpn' and '/var/log/ivpn'

  echo "[+] Removing leftovers ..."
  if [ -d $_IVPN_TMP ] ; then
    rm -rf $_IVPN_TMP|| echo "[-] Removing '$_IVPN_TMP' folder failed"
  fi
  if [ -d $_IVPN_LOG ] ; then
    rm -rf $_IVPN_LOG|| echo "[-] Removing '$_IVPN_LOG' folder failed"
  fi

  #remove 'ivpn' folder (if empy)
  silent sudo rmdir $_IVPN_DIR
}

# --- install hooks ---

pre_upgrade () {
  disconnect;
  # ########################################################################################
  #
  # Next lines is in use only for compatibility with old package versions (v3.10.10 and older)
  #
  # ########################################################################################
  # Folders changed:
  # "/opt/ivpn/mutable" -> "/etc/opt/ivpn/mutable" 
  # "/opt/ivpn/log"     -> "/var/log/ivpn" 
  if [ -d /opt/ivpn/mutable ]; then 
    echo "[+] Migrating old-style mutable data from the previous installation ..."
    mkdir -p /etc/opt/ivpn
    mv /opt/ivpn/mutable /etc/opt/ivpn/mutable
  fi
  if [ -d /opt/ivpn/log ]; then 
    echo "[+] Migrating old-style logs from the previous installation ..." 
    mv /opt/ivpn/log /var/log/ivpn
  fi
  # ########################################################################################
}

pre_remove() {
  disconnect;
  ivpnlogout;
  try_systemd_stop;
}

post_install () {
  if has_systemd ; then
    echo "[+] Enabling ivpn-service.service..."
    systemctl enable --now ivpn-service
  else
    echo "[!] WARNING! systemd not detected. Please start ivpn-service manually."
  fi
}


post_upgrade () {
  if has_systemd ; then
    echo "[+] Restarting ivpn-service.service..."
    systemctl daemon-reload
    systemctl restart ivpn-service
  else
    echo "[!] WARNING! systemd not detected. Please restart ivpn-service manually."
  fi
}

post_remove() {
  erase_leftovers;
}