|
154 | 154 | : (2, [['a', 1, 2], ['b', 2, 3], ['c', 3, 4]]) |
155 | 155 | #+END_SRC |
156 | 156 |
|
| 157 | +*** SSH connection to remote kernels |
| 158 | + |
| 159 | +It is possible to connect to a remote kernel running on some server to which SSH |
| 160 | +tunnels can be created. Usually the kernel process is started in a terminal |
| 161 | +multiplexer like =screen= or =tmux= to keep it running after disconnecting. When |
| 162 | +a kernel is started on the remote server using the following command |
| 163 | + |
| 164 | +#+BEGIN_SRC sh |
| 165 | +ipython kernel |
| 166 | +#+END_SRC |
| 167 | + |
| 168 | +The name of the connection file for later use with the =--existing= option of |
| 169 | +=ipython= is printed out. By default has the form =kernel-${PID}.json=, but a |
| 170 | +custom name can be specified with |
| 171 | + |
| 172 | +#+BEGIN_SRC sh |
| 173 | +ipython kernel --IPKernelApp.connection_file=${custom_name}.json |
| 174 | +#+END_SRC |
| 175 | + |
| 176 | +The connection file is written in the runtime directory on the server when using |
| 177 | +Jupyter (IPython >= 4.0) which is usually =/run/user/${UID}/jupyter/= or falls |
| 178 | +back to =${HOME}/.local/share/jupyter/runtime/=. The exact location can be found |
| 179 | +on the server with |
| 180 | + |
| 181 | +#+BEGIN_SRC sh |
| 182 | +python -c "from jupyter_core.paths import jupyter_runtime_dir as p; print(p())" |
| 183 | +#+END_SRC |
| 184 | + |
| 185 | +When using the older IPython < 4.0, the connection file is written in the |
| 186 | +=security/= subdirectory of the current profile, e.g. |
| 187 | +=$HOME/.ipython/profile_default/security/= with the default profile. The profile |
| 188 | +directory can also be found with =ipython locate=. |
| 189 | + |
| 190 | + |
| 191 | +The connection contains connection information (secret hash, ports) and has to |
| 192 | +be transferred from the server to the appropriate directory on the client |
| 193 | +machine, e.g. with the standard runtime directory with Jupyter it could be |
| 194 | + |
| 195 | +#+BEGIN_SRC sh |
| 196 | +scp server:/run/user/\$UID/jupyter/${connnection_file} /run/user/$UID/jupyter |
| 197 | +#+END_SRC |
| 198 | + |
| 199 | +The backslash before the first =$= makes sure the user id on the server is used |
| 200 | +in the source path. |
| 201 | + |
| 202 | +Finally, the remote session can be connected through specifying both of the |
| 203 | +following arguments in the source blocks: |
| 204 | +- =:session ${connection_file_basename}= :: Will be used to find the |
| 205 | + connection file, specified without the =.json= extension. |
| 206 | +- =:ssh ${server_name_or_ip}= :: Will be passed to the =--ssh= option of |
| 207 | + =ipython console= which will establish needed SSH tunnels (nothing more, no |
| 208 | + shell session is created) and write a modified connection file (that is |
| 209 | + handled internally). It is best to have SSH configured in such a way that |
| 210 | + the user does not have to be specified (option =User= in ssh configuration) |
| 211 | + and possibly password-less login through SSH keys (option =IdentityFile= in |
| 212 | + ssh configuration). |
157 | 213 | ** What features are there outside of Org SRC block evaluation? |
158 | 214 |
|
159 | 215 | * You can ask the running IPython kernel for documentation. Open a |
|
211 | 267 |
|
212 | 268 | * Open a REPL using =C-c C-v C-z= so that you get completion in Python buffers. |
213 | 269 |
|
| 270 | + * The source block header arguments can be set as a special property |
| 271 | + |
| 272 | +#+BEGIN_SRC org |
| 273 | +#+PROPERTY: header-args:ipython :session kernel_name :ssh remote_server |
| 274 | +#+END_SRC |
| 275 | + and then they don't have to be specified for each cell, they are used during |
| 276 | + execution automatically. However, they are set only after (re)loading the Org |
| 277 | + file or by =C-c C-c= on this line. |
| 278 | + |
214 | 279 | ** Help, it doesn't work |
215 | 280 |
|
216 | 281 | First thing to do is check that you have all of the required |
217 | 282 | dependencies. Several common problems have been resolved in the |
218 | 283 | project's issues, so take a look there to see if your problem has a |
219 | 284 | quick fix. Otherwise feel free to cut an issue - I'll do my best to |
220 | 285 | help. |
| 286 | +*** Errors during and/or long session start-up |
| 287 | +This is because it takes a few seconds for the driver server process to bind to |
| 288 | +a port and the REPL to connect to the kernel and possibly establishing SSH |
| 289 | +tunnels. Executing code before the session is fully established can result in |
| 290 | +errors. Because there is no simple way (to the best knowledge of the authors) to |
| 291 | +find out when the session is fully established, the code execution is postponed |
| 292 | +by several seconds. This waiting period can be customized by the |
| 293 | +`ob-ipython-connection-wait` variable in case it is too short or needlessly |
| 294 | +long. |
0 commit comments