Skip to content

Commit e1d50c8

Browse files
tkedwardssamhed
authored andcommitted
Added a Snap package for noVNC (#1231)
Creating an Ubuntu Snap package to make noVNC easier to deploy. Checks for the websockify binary in both the PATH (using which) and in the location where the Snap package places the binary. This is necessary for noVNC to be usable in a Snap. It doesn't affect the original functionality of git cloning websockify if it's not found in PATH or the Snap location.
1 parent 897b465 commit e1d50c8

File tree

5 files changed

+132
-2
lines changed

5 files changed

+132
-2
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ for a more complete list with additional info and links.
2424
- [Browser Requirements](#browser-requirements)
2525
- [Server Requirements](#server-requirements)
2626
- [Quick Start](#quick-start)
27+
- [Installation from Snap Package](#installation-from-snap-package)
2728
- [Integration and Deployment](#integration-and-deployment)
2829
- [Authors/Contributors](#authorscontributors)
2930

@@ -115,6 +116,66 @@ proxy.
115116
script. Hit the Connect button, enter a password if the VNC server has one
116117
configured, and enjoy!
117118

119+
### Installation from Snap Package
120+
Running the command below will install the latest release of noVNC from Snap:
121+
122+
`sudo snap install novnc`
123+
124+
#### Running noVNC
125+
126+
You can run the Snap-package installed novnc directly with, for example:
127+
128+
`novnc --listen 6081 --vnc localhost:5901 # /snap/bin/novnc if /snap/bin is not in your PATH`
129+
130+
#### Running as a Service (Daemon)
131+
The Snap package also has the capability to run a 'novnc' service which can be
132+
configured to listen on multiple ports connecting to multiple VNC servers
133+
(effectively a service runing multiple instances of novnc).
134+
Instructions (with example values):
135+
136+
List current services (out-of-box this will be blank):
137+
138+
```
139+
sudo snap get novnc services
140+
Key Value
141+
services.n6080 {...}
142+
services.n6081 {...}
143+
```
144+
145+
Create a new service that listens on port 6082 and connects to the VNC server
146+
running on port 5902 on localhost:
147+
148+
`sudo snap set novnc services.n6082.listen=6082 services.n6082.vnc=localhost:5902`
149+
150+
(Any services you define with 'snap set' will be automatically started)
151+
Note that the name of the service, 'n6082' in this example, can be anything
152+
as long as it doesn't start with a number or contain spaces/special characters.
153+
154+
View the configuration of the service just created:
155+
156+
```
157+
sudo snap get novnc services.n6082
158+
Key Value
159+
services.n6082.listen 6082
160+
services.n6082.vnc localhost:5902
161+
```
162+
163+
Disable a service (note that because of a limitation in Snap it's currently not
164+
possible to unset config variables, setting them to blank values is the way
165+
to disable a service):
166+
167+
`sudo snap set novnc services.n6082.listen='' services.n6082.vnc=''`
168+
169+
(Any services you set to blank with 'snap set' like this will be automatically stopped)
170+
171+
Verify that the service is disabled (blank values):
172+
173+
```
174+
sudo snap get novnc services.n6082
175+
Key Value
176+
services.n6082.listen
177+
services.n6082.vnc
178+
```
118179

119180
### Integration and Deployment
120181

snap/hooks/configure

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh -e
2+
3+
snapctl restart novnc.novncsvc

snap/snapcraft.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: novnc
2+
base: core18 # the base snap is the execution environment for this snap
3+
version: '1.1.0'
4+
summary: Open Source VNC client using HTML5 (WebSockets, Canvas)
5+
description: |
6+
Open Source VNC client using HTML5 (WebSockets, Canvas).
7+
noVNC is both a VNC client JavaScript library as well as an application built on top of that library. noVNC runs well in any modern browser including mobile browsers (iOS and Android).
8+
9+
grade: stable
10+
confinement: strict
11+
12+
parts:
13+
novnc:
14+
source: https://github.com/novnc/noVNC.git #https://github.com/novnc/noVNC/archive/v$SNAPCRAFT_PROJECT_VERSION.tar.gz
15+
plugin: dump
16+
stage-packages:
17+
- websockify
18+
- bash
19+
- jq
20+
- python-numpy
21+
- python3-numpy
22+
23+
hooks:
24+
configure:
25+
plugs: [network, network-bind]
26+
27+
apps:
28+
novnc:
29+
command: utils/launch.sh
30+
plugs: [network, network-bind]
31+
novncsvc:
32+
command: utils/svc_wrapper.sh
33+
daemon: forking
34+
plugs: [network, network-bind]

utils/launch.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,12 @@ if [[ -d ${HERE}/websockify ]]; then
139139

140140
echo "Using local websockify at $WEBSOCKIFY"
141141
else
142-
WEBSOCKIFY=$(which websockify 2>/dev/null)
142+
WEBSOCKIFY_FROMSYSTEM=$(which websockify 2>/dev/null)
143+
WEBSOCKIFY_FROMSNAP=${HERE}/../usr/bin/python2-websockify
144+
[ -f $WEBSOCKIFY_FROMSYSTEM ] && WEBSOCKIFY=$WEBSOCKIFY_FROMSYSTEM
145+
[ -f $WEBSOCKIFY_FROMSNAP ] && WEBSOCKIFY=$WEBSOCKIFY_FROMSNAP
143146

144-
if [[ $? -ne 0 ]]; then
147+
if [ ! -f "$WEBSOCKIFY" ]; then
145148
echo "No installed websockify, attempting to clone websockify..."
146149
WEBSOCKIFY=${HERE}/websockify/run
147150
git clone https://github.com/novnc/websockify ${HERE}/websockify

utils/svc_wrapper.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
# `snapctl get services` returns a JSON array, example:
4+
#{
5+
#"n6801": {
6+
# "listen": 6801,
7+
# "vnc": "localhost:5901"
8+
#},
9+
#"n6802": {
10+
# "listen": 6802,
11+
# "vnc": "localhost:5902"
12+
#}
13+
#}
14+
snapctl get services | jq -c '.[]' | while read service; do # for each service the user sepcified..
15+
# get the important data for the service (listen port, VNC host:port)
16+
listen_port="$(echo $service | jq --raw-output '.listen')"
17+
vnc_host_port="$(echo $service | jq --raw-output '.vnc')" # --raw-output removes any quotation marks from the output
18+
19+
# check whether those values are valid
20+
expr "$listen_port" : '^[0-9]\+$' > /dev/null
21+
listen_port_valid=$?
22+
if [ ! $listen_port_valid ] || [ -z "$vnc_host_port" ]; then
23+
# invalid values mean the service is disabled, do nothing except for printing a message (logged in /var/log/system or systemd journal)
24+
echo "novnc: not starting service ${service} with listen_port ${listen_port} and vnc_host_port ${vnc_host_port}"
25+
else
26+
# start (and fork with '&') the service using the specified listen port and VNC host:port
27+
$SNAP/utils/launch.sh --listen $listen_port --vnc $vnc_host_port &
28+
fi
29+
done

0 commit comments

Comments
 (0)