summarylogtreecommitdiffstats
path: root/anki-sync-server.install
blob: ad5bfc2fe7658088f9929087d4e23da26af5891e (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
171
172
173
174
175
176
177
178
179
180
181
182
183
post_install() {
  # create user and set ownership
  useradd -d /opt/anki-sync-server -r -s /sbin/nologin anki-sync-server
  chown -R anki-sync-server /opt/anki-sync-server
  chgrp -R anki-sync-server /opt/anki-sync-server

  # install prerequisites as stated on the website
  echo "installing python packages as anki-sync-server user..."
  sudo -u anki-sync-server pip install -r /opt/anki-sync-server/requirements.txt --user --no-warn-script-location

  append_nginx_config

  #start nginx
  systemctl enable nginx
  systemctl start nginx

  # link systemd service file
  ln -s /opt/anki-sync-server/plugins/systemd/anki-sync-server.service /etc/systemd/system/
  systemctl enable anki-sync-server
  systemctl start anki-sync-server

  # post installation instructions
  cat << EOF
=======================================================================
------------------
Post Installation:
------------------
You'll need to go to:

    /opt/anki-sync-server

and run:

    sudo -u anki-sync-server ./ankisyncctl.py adduser <username>

to add a new user to the anki database.

--------------------
Plugin Installation:
--------------------
To have anki interface with the server, you'll need to copy the plugin
from /opt/anki-sync-server/plugins to the relevant directory.

Anki 2.1.28 (and up): ~/.local/share/Anki/addons21
    ln -s /opt/anki-sync-server/plugins/anki2.1.28/anki-sync-server \\
        ~/.local/share/Anki2/addons21/

Anki 2.1: ~/.local/share/Anki2/addons21/
    ln -s /opt/anki-sync-server/plugins/anki2.1/anki-sync-server \\
        ~/.local/share/Anki2/addons21/

Anki 2.0: ~/Anki/addons
    ln -s /opt/anki-sync-server/plugins/anki2.0/anki-sync-server.py \\
        ~/Anki/addons

or your OS equivalent.

---------------
Run The Server:
---------------
The server should be set to auto start via systemctl and should be already running.
See:

    systemctl status anki-sync-server
    systemctl status nginx

for details.

Otherwise, to run the server go to:

    /opt/anki-sync-server/

then run:

    sudo -u anki-sync-server python -m ankisyncd

if you run it as another user you may have to install the dependencies again:

  pip install -r /opt/anki-sync-server/requirements.txt [--user]

=== (be aware you may have trouble with permissions on your auth.db if you run as different users!) ===

-----
NGINX
-----
/etc/nginx/nginx.conf has been modified and a backup has been saved to /etc/nginx/nginx.conf.pacsave.  It would be a good idea to diff to see if anything has gone awry.

nginx.service is set to autostart.

=======================================================================
EOF
}

post_remove(){
  # stop service (and remove systemd files)
  systemctl stop anki-sync-server
  systemctl disable anki-sync-server
  echo "Executing systemctl daemon-reload"
  systemctl daemon-reload

  # remove user (& group)
  getent passwd anki-sync-server &>/dev/null && userdel anki-sync-server || true
  getent group anki-sync-server &>/dev/null && groupdel anki-sync-server || true

  echo "==================================================================="
  echo "rm -rf /opt/anki-sync-server to remove the database and cache files"
  echo "==================================================================="
}

pre_upgrade(){
  echo "stopping anki-sync-server.service..."
  sudo systemctl stop anki-sync-server.service

  cd "/opt/anki-sync-server"

  local _auth_="auth.db"
  local _coll_="collections"
  local _sess_="session.db"

  if [ -f "${_auth_}" ]; then
    sudo mv -v "${_auth_}" "${_auth_}.BAK"
  fi

  if [ -d "${_coll_}" ]; then
    sudo mv -v "${_coll_}" "${_coll_}.BAK"
  fi

  if [ -f "${_sess_}" ]; then
    sudo mv -v "${_sess_}" "${_sess_}.BAK"
  fi
}

post_upgrade(){
  # install prerequisites as stated on the website
  echo "installing python packages as anki-sync-server user..."
  sudo -u anki-sync-server pip install -r /opt/anki-sync-server/requirements.txt --user

  append_nginx_config

  cd "/opt/anki-sync-server"

  local _auth_="auth.db"
  local _coll_="collections"
  local _sess_="session.db"

  if [ -f "${_auth_}.BAK" ]; then
    sudo mv -v "${_auth_}.BAK" "${_auth_}"
  fi

  if [ -d "${_coll_}.BAK" ]; then
    sudo mv -v "${_coll_}.BAK" "${_coll_}"
  fi

  if [ -f "${_sess_}.BAK" ]; then
    sudo mv -v "${_sess_}.BAK" "${_sess_}"
  fi

  sudo chown -R anki-sync-server /opt/anki-sync-server
  sudo chgrp -R anki-sync-server /opt/anki-sync-server

  echo "reloading systemd daemon..."
  sudo systemctl daemon-reload
  echo "starting anki-sync-server.service..."
  sudo systemctl start anki-sync-server
  echo "restarting nginx..."
  sudo systemctl restart nginx
}

function append_nginx_config() {
  # append nginx config to nginx config file
  echo "appending nginx server config to /etc/nginx/nginx.conf"
  local _config_dir_="/etc/nginx/"
  local _pacsave_="${_config_dir_}/nginx.conf.pacsave"
  local _config_="${_config_dir_}/nginx.conf"

  if (grep "anki-sync-server-request-unchunker" "${_config_}" --quiet); then
    echo "nginx config aleady updated!"
  else
    sudo cp "${_config_}" "${_pacsave_}"
    sudo awk -f /opt/anki-sync-server/plugins/nginx/append.awk \
      "${_pacsave_}" > "${_config_}"
  fi
}