summarylogtreecommitdiffstats
path: root/jotta-cli.install
blob: cdbe6723f6f0b1e0a8274df1ff242788f4e144e4 (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
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
#!/bin/bash

set -u

post_install() {
  print_first_time
}

post_upgrade() {
  vercomp ${2} "0.13.55213-2"
  if [[ "$?" == "2" ]]; then
    print_upgrade_from_system_slice
    if systemctl is-enabled jottad >/dev/null; then
      printf "Disabling jottad system slice\n"
      sudo systemctl disable jottad
    fi
    if systemctl is-active jottad >/dev/null; then
      printf "Stopping jottad system slice\n"
      sudo systemctl daemon-reload
      sudo systemctl stop jottad
    fi
    printf "\n"
    print_first_time
  else
    safe_restart_systemd_services
  fi
}

pre_remove() {
  safe_stop_systemd_services
}

print_upgrade_from_system_slice() {
  printf "It looks like you are upgrading from a version\n"
  printf "of jottad that runs in system slice. This version\n"
  printf "runs in user slice and thereby datadir has changed.\n"
  printf "Read more here: https://docs.jottacloud.com/en/articles/1461561-release-notes-for-jottacloud-cli\n\n"
  printf "To help you migrate your settings, we provide the\n"
  printf "script /usr/bin/migrate_jottad_settings that you\n"
  printf "can run after you have upgraded this package.\n\n"
}

print_first_time() {
  printf "First time use: run 'run_jottad' as your own user to\n"
  printf "enable systemd-unit and to enable future restart on upgrade.\n\n"
}

vercomp() {
  if [[ $1 == $2 ]]
  then
    return 0
  fi
  local IFS=.-
  local i ver1=($1) ver2=($2)
  for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
  do
    ver1[i]=0
  done
  for ((i=0; i<${#ver1[@]}; i++))
  do
    if [[ -z ${ver2[i]} ]]
    then
      ver2[i]=0
    fi
    if ((10#${ver1[i]} > 10#${ver2[i]}))
    then
      return 1
    fi
    if ((10#${ver1[i]} < 10#${ver2[i]}))
    then
      return 2
    fi
  done
  return 0
}

systemd_exec_as() {
  user=$1
  shift
  user_xdg_runtime_dir="/run/user/$(id -u "$user")" || return 1
  BASH=$(command -v bash)
  # su --login requires the password for each user for which this function executes
  # this can be avoided by going via root
  #su --login "$user" -s "$BASH" -c "XDG_RUNTIME_DIR=$user_xdg_runtime_dir $* 2> /dev/null"
  sudo -u "$user" -s "$BASH" -c "XDG_RUNTIME_DIR=$user_xdg_runtime_dir $* 2> /dev/null"
}

systemd_unit_active_for() {
  user=$1
  service=$2
  command -v systemctl &> /dev/null && systemd_exec_as "$user" "systemctl --user -q is-active $service"
}

SYSTEMD_RESTARTED_REDIRECTOR=
systemd_restart_if_active() {
  user=$1
  service=$2
  if systemd_unit_active_for "$user" "$service"; then
    systemd_exec_as "$user" "systemctl --user restart $service"
  fi
}

safe_restart_systemd_services() {
  while read -r pid; do
    if [ -z "$pid" ]; then
      continue
    fi

    user="$(ps -o user= -p "$pid")"

    if [ -z "$user" ]; then
      continue
    fi

    restart_instructions="Restart jottad manually by running 'run_jottad' as $user.\n"

    if ! systemd_unit_active_for "$user" "jottad.service"; then
      printf "jottad not running via systemd for $user.\n"
      printf "$restart_instructions"
      continue
    fi

    printf "Autorestarting jottad via systemd for $user.\n"
    systemd_exec_as "$user" "systemctl --user daemon-reload"
    systemd_restart_if_active "$user" "jottad.service"
  done <<< "$(pidof /usr/bin/jottad | tr ' ' '\n')"
}

systemd_stop_if_active() {
  user=$1
  service=$2
  if systemd_unit_active_for "$user" "$service"; then
    systemd_exec_as "$user" "systemctl --user stop $service"
  fi
}

# I added this function to disable the daemon for a specific user
systemd_disable() {
  user=$1
  service=$2
  systemd_exec_as "$user" "systemctl --user disable $service"
}

safe_stop_systemd_services() {
  # Disable the service for all non-system users
  for usr in $(awk -F: '($3>=1000)&&($1!="nobody"){print $1}' /etc/passwd); do
    printf "Disabling jottad for user $usr. \n"
    systemd_disable "$usr" "jottad.service"
  done
  # Now stop the service for users that have the service running
  while read -r pid; do
    if [ -z "$pid" ]; then
      continue
    fi

    user="$(ps -o user= -p "$pid")"

    if [ -z "$user" ]; then
      continue
    fi
    
    if ! systemd_unit_active_for "$user" "jottad.service"; then
      continue
    fi

    printf "Autostopping jottad via systemd for $user.\n"
    systemd_exec_as "$user" "systemctl --user daemon-reload"
    systemd_stop_if_active "$user" "jottad.service"
  done <<< "$(pidof /usr/bin/jottad | tr ' ' '\n')"
}