[Tutorial] Accessing VS Code through a browser

ARM release is out as well, but only 64bit.
https://github.com/cdr/code-server/releases/download/2.1698/code-server2.1698-vsc1.41.1-linux-arm64.tar.gz

1 Like

For those struggling with cdr’s half-assed Docker image (at least in terms of supporting documentation is concerned), use “2.1698” for the version tag. “latest” and “v2” will not get you this latest release, at least not yet.

I’m seriously looking at using linuxserver’s image instead. https://hub.docker.com/r/linuxserver/code-server. It at least appears to have better documentation.

The openHAB extension is still showing up as 0.4.0 for me. I’m going to manually install it and will check back later.

I also struggled with the code-server docker image until I decided to try installing code-server on some other random docker image. This was really simple and you won’t have to worry about the ‘coder’ user. I have not tried it yet but it just might work to install code server directly on one of the openhab images.

We have raised an issue and they are checking why automatc update of their Marketplace is not working.

1 Like

Should be possible, just make shure to run it as user openhab to avoid file permission issues,

Great! Thanks!
There is a install package now and because I installed in on a seperate server, there are some other steps and I thought; I add my tutorial here:

To connect it to openhab, I wanted it to use the existing smb share on the openhab system:

adduser codeserver
usermod -aG openhab codeserver
smbpasswd -a codeserver 
openhabian-config -> apply improvements -> fix permissions 

I installed it on an extra instance/container with a fresh ubuntu: (running with 512MB Ram, 1 CPU nicely).

sudo -i 
apt update
apt install curl
curl -fsSL https://code-server.dev/install.sh | sh
systemctl --user enable --now code-server
mkdir /media/openhab
echo '//10.0.1.2/openHAB-share /media/openhab2 cifs username=codeserver,password=codeserver,iocharset=utf8,file_mode=0777,dir_mode=0777,noperm 0 0'>>/etc/fstab
mount /media/openhab2

then modify the config to enable access from over the network:

sudo nano ~/.config/code-server/config.yaml 
(and repace the 127.0.0.1 with 0.0.0.0
FYI; change the pw here ). 

Then, install the openhab extension in the browser:
then, change the config to your openhab-server to your liking:

image

{
    "openhab.itemCasing": "snake",
    "openhab.useRestApi": true,
    "openhab.sitemapPreviewUI": "basicui",
    "openhab.host": "10.0.1.2",
    "openhab.port": 8080,
    "openhab.remoteLspEnabled": true,
    "openhab.remoteLspPort": 5007,
    "openhab.username": "openhab",
    "openhab.password": "habopen",
    "openhab.karafCommand": "ssh openhab@10.0.1.2 -p 8101 -t",
}

and then, I recommend to add it to your dashboard:


by adding this

codeserver.link-name=openHAB Code Editor
codeserver.link-url=http://10.0.1.9:8080
codeserver.link-imageurl=../static/codeserver.jpg

to your file here: services/dashboard.cfg

2 Likes

Thank you, is it possible to add this installation process to openhabian?

Have a nice weekend!

Johannes

I think that would be helpful for a lot of peoples and the supported hardware would be sufficient. What does @elias_gabrielsson think?

The installation instructions require a machine with:

  • 1 GB RAM
  • 2 CPU cores

Given all the other stuff openHABian as running, not the least of which is openHAB in terms of RAM use, I would only attempt to install VSCode on an RPi 4 with 4GB + of RAM. Given that limitation I’m not certain that is worth supporting.

But if someone wants to try it out and submit a PR I’m sure there will be lots of good discussion and it might be accepted.

2 Likes

2GB is not sufficient?

That might be sufficient but I’ve noticed running Code Server myself on a VM that I had to bump the RAM up to 4 GB to get decent performance. I bet 2.5 or 3 GB might be sufficient. And as always YMMV and it depends on what else you have running. But I find that an RPi 3 with 1 GB RAM is pretty much fully loaded with an openHABian install already. Adding another service that requires 1 GB all by itself is going to cause problems.

3 Likes

hmm, My VS-Server is running on a ubuntu LX-container with 512MB ram and I don’t feel any lag/delay, even though my openhab config is at least medium in size.

I would think adding VS Server as an option would be good, in addition to all the other optional features openhabian is offering, like grafana, influxdb, FIND, etc…

Installing “all” of them might bring your device to a limit, but then again, openhabian as the central tool, good “warn” the user.

does a PR has to be a working prototype or can it be as simple as an idea?

For an idea you can file an issue. A PR is working cure ready to be merged.

If you really want this to happen, you should submit a PR which needs to be a working prototype. You can submit the idea but there is no guarantee that anyone will do anything about just an idea.

1 Like

Thanks for the effort. I tried to do the steps but some things are not clear. Can you please clarify:

  1. Create file code-server.service for autostart → this should go into /usr/lib/systemd/system ?
  2. I could copy the vscode extension (find tthe tiny downloads button in the middle of the screen) and copy it to op/coder ? I did not have a folder /opt/code-server/
  3. How do I actually access it , i.e. what is the browser url ? 192.168.1.30:8080…
  4. It does not show up in the dashboard. There are two dashboard.cfg enties in this tutorial, yours with coder.link… and the one from papaPhil who adds codeserver.link…

Depends on your operating system, so cannot answer.

If installed within VS-Code, you should not need to take care about copying.

Guess what this means:

192.168.1.30:9090

Depends, my tutorial was for openHAB 2.x. This config has changed a bit in openHAB 3.x but the community search will surely lead you to a working solution.

Thank you. I tried 9090, of course, but is not working. The service seems to be running though.

[09:03:03] root@RPi4:~# sudo systemctl status code-server
● code-server.service - Code Server IDE
   Loaded: loaded (/lib/systemd/system/code-server.service; enabled; vendor preset: enabled)
   Active: activating (auto-restart) (Result: exit-code) since Thu 2023-01-12 09:03:04 CET; 1s ago
  Process: 9363 ExecStart=/opt/coder/code-server --port 9090 --auth none --disable-telemetry (code=exited, status=203/EXEC)
 Main PID: 9363 (code=exited, status=203/EXEC)

You could check which port the service is bound to, just can’t remember the correct command…

Update for OpenHabian 3.4.1 (non-docker)

TLDR;
save yourself the trouble. Its not mature. It can get it to partially work until you restart your system.

You still want to try it?
These were the steps that I did, on a plain (non docker) OpenHabian 3.4.0 RPI4 (buster) system:

Install the code server

open a root shell on your openhabian system, then
curl -fsSL https://code-server.dev/install.sh | sh
It issues with multiple errors, you may have to force an update nodejs and the package manager npm. Not sure if the npm update is really mandatory. Anyway, it will give you a hint on the console how to upgrade it.

Edit the configuration (optional)

The configuration is used when you start the code-server manually on the command line. If you start it as a service, you can pass parameters direcly. See below.

nano ~/.config/code-server/config.yaml
change auth to “none” and address to 0.0.0.0:9090
If you leave the configuration at 127.0.0.1, you can only access code server on the machine that it is running on. Mind the port 9090 because 8080 is taken by OpenHab already.

Add autostart

First create the service file
nano /usr/lib/systemd/system/code-server.service

and put the following code there:

[Unit]
Description=Code Server IDE
After=network.target

[Service]
Type=simple
User=openhab
Group=openhab
WorkingDirectory=/opt/coder
Restart=on-failure
RestartSec=10
ExecStart=/usr/bin/code-server --bind-addr 0.0.0.0  --port 9090 --auth none --disable-telemetry
StandardOutput=file:/var/log/code-server-output.log
StandardError=file:/var/log/code-server-error.log

[Install]

WantedBy=multi-user.target

then activate it:
systemctl daemon-reload && systemctl enable code-server.service && systemctl start code-server.service

Check if that was successful with
systemctl status code-server

If the output reads at some point code=exited, status=203/EXEC please check the path of the execStart parameter. First post from @hmerk points to the executable in opt/coder/code-server which did not work for me.
However, you may still be able to start the code server manually by typing code-server in your terminal window.

Now wait a little. When You open the browser at (e.g.) 192.168.1.30:9090 it may show “500” and Cannot find module ‘minimist’. Shell says the same, eventually. There is an issue about this, but no solution. You have to fix this manually:

Optional: install missing files

when starting code-server, you will find some modules are missing. It starts with “minimist” and goes on and on. Long story short, you have to issue these commands.

npm install -g minimist yazl yauzl @microsoft/1ds-core-js spdlog  vscode-regexpp vscode-proxy-agent @vscode/ripgrep xterm-headless  @parcel/watcher
sudo apt-get install libsecret-1-dev
npm install keytar

The VScode files belong to root now. In order to be able to modify settings within the browser editor, we need to change ownership. Most files, also settings.json are in /lib/node_modules.
chown -R openhab:openhab /lib/node_modules

Check port binding

netstat -lnp
it should read port 9090 somewhere.

Install openhab add on

according to this, it should be as simple as calling
code-server --install-extension openhab.openhab
If you installed the missing packages above, this command should complete successfully.

Set up openhab extension

create token (main UI, bottom left click, scroll down, click create token)
In your browser, go to 192.168.1.xx:9090 It should start the Visual Studio Code window. Go to File → preferences → Extensions-> OpenHab → settings.json and enter
{
“workbench.colorTheme”: “Visual Studio Dark”,
“security.workspace.trust.untrustedFiles”: “open”,
“openhab.connection.authToken”: “oh.codeserver.*******”,
“openhab.connection.host”: “192.168.1.30”,
“openhab.connection.port”: 8080,
“openhab.host”: “192.168.1.30”,
“openhab.port”: 8080,
“openhab.password”: “habopen”,
“openhab.username”: “openhab”,
“workbench.startupEditor”: “none”,
“openhab.karafCommand”: “ssh openhab@192.168.1.30 -p 8101 -t ‘log:tail’”
}

Beware ! When you copy this, often the quotes " will be swapped for similar /italic versions ! Make sure you have proper quotes in the config file.

Insecure context !?

VSCode will complain that you are accessing it in an insecure context. This is due to the fact that so far you use http. While I have not encountered any limitations, here is how to fix. (find

What else ?

keep in mind that if you created folders as root, they may not be accessible by the openhabian user which normally runs the system.
To change ownership, do e.g.
sudo chown -R openhab:openhab /opt/coder

On my system everything works as expected, except for the syntax highlighting. It does some highlighting of brackets but nothing else. Mouse - over for items shows their state, code checking is fine.

A word of warning. All this above does not use any password or other security measures. Never expose to the internet. For local use or via VPN is probably ok.

Update / persisting issues

  • syntax highlighting does not work, except for brackets.
  • language setting is not persisted

Overall rating: probably not worth the installation effort. Took me more than a day of work and left me with a unreliable solution.

Thanks for summarizing the new process.
As I am using openHAB 3.4 purely with GUI config, I did not and cannot follow up changes anymore.

If that’s an issue, why not simply create it ???