Initial add-on code

This commit is contained in:
Franck Nijhof 2019-03-24 23:43:22 +01:00
parent 416e586602
commit 344e8f48be
No known key found for this signature in database
GPG key ID: D62583BA8AB11CA3
20 changed files with 810 additions and 0 deletions

82
vscode/.README.j2 Normal file
View file

@ -0,0 +1,82 @@
# Community Hass.io Add-ons: Visual Studio Code
[![Release][release-shield]][release] ![Project Stage][project-stage-shield] ![Project Maintenance][maintenance-shield]
[![Discord][discord-shield]][discord] [![Community Forum][forum-shield]][forum]
[![Buy me a coffee][buymeacoffee-shield]][buymeacoffee]
[![Support my work on Patreon][patreon-shield]][patreon]
Visual Studio Code, accessible through the browser.
## About
This add-on runs Visual Studio Code, allowing you to edit your Home Assistant
configuration straight from the web browser and can be embeded straight
into the Home Assistant frontend UI.
Visual Studio Code runs as a remote server using `code-server`, and is a
fully fletched VSCode experience.
The add-on has the Home Assistant and MDI icons extensions pre-installed
and pre-configured right out of the box. This mean that auto completion works
instantly, without the need for configuring anything.
[Click here for the full documentation][docs]
![Visual Studio Code in the Home Assistant Frontend][screenshot]
{% if channel == "edge" %}
## WARNING! THIS IS AN EDGE VERSION!
This Hass.io Add-ons repository contains edge builds of add-ons. Edge builds
add-ons are based upon the latest development version.
- They may not work at all.
- They might stop working at any time.
- They could have a negative impact on your system.
This repository was created for:
- Anybody willing to test.
- Anybody interested in trying out upcoming add-ons or add-on features.
- Developers.
If you are more interested in stable releases of our add-ons:
<https://github.com/hassio-addons/repository>
{% endif %}
{% if channel == "beta" %}
## WARNING! THIS IS A BETA VERSION!
This Hass.io Add-ons repository contains beta releases of add-ons.
- They might stop working at any time.
- They could have a negative impact on your system.
This repository was created for:
- Anybody willing to test.
- Anybody interested in trying out upcoming add-ons or add-on features.
If you are more interested in stable releases of our add-ons:
<https://github.com/hassio-addons/repository>
{% endif %}
[buymeacoffee-shield]: https://www.buymeacoffee.com/assets/img/guidelines/download-assets-sm-2.svg
[buymeacoffee]: https://www.buymeacoffee.com/frenck
[discord-shield]: https://img.shields.io/discord/478094546522079232.svg
[discord]: https://discord.me/hassioaddons
[docs]: {{ repo }}/blob/{{ version }}/README.md
[forum-shield]: https://img.shields.io/badge/community-forum-brightgreen.svg
[forum]: https://community.home-assistant.io/?u=frenck
[maintenance-shield]: https://img.shields.io/maintenance/yes/2019.svg
[patreon-shield]: https://www.frenck.nl/images/patreon.png
[patreon]: https://www.patreon.com/frenck
[project-stage-shield]: https://img.shields.io/badge/project%20stage-experimental-yellow.svg
[release-shield]: https://img.shields.io/badge/version-{{ version }}-blue.svg
[release]: {{ repo }}/tree/{{ version }}
[screenshot]: {{ repo }}/raw/master/images/screenshot.png

144
vscode/Dockerfile Executable file
View file

@ -0,0 +1,144 @@
ARG BUILD_FROM=hassioaddons/ubuntu-base:3.0.0
###############################################################################
# Get the original code-server container.
###############################################################################
FROM codercom/code-server as codeserver
###############################################################################
# Build container to get custom vscode extensions.
###############################################################################
FROM ${BUILD_FROM} as vscode
# Copy in extensions list
COPY vscode.extensions /root/vscode.extensions
# Install the actual VSCode to download configs and extensions
RUN \
apt-get update \
\
&& apt-get install -y --no-install-recommends \
libx11-xcb1 \
libasound2 \
\
&& curl \
-o vscode-amd64.deb \
-L https://vscode-update.azurewebsites.net/latest/linux-deb-x64/stable \
\
&& dpkg -i vscode-amd64.deb || true \
&& apt-get install -y -f \
&& rm -f vscode-amd64.deb \
\
&& code -v --user-data-dir /root/.config/Code
RUN \
while read -r ext; do \
echo "Installing vscode extension: $ext"; \
code --user-data-dir /root/.config/Code --install-extension $ext; \
done < /root/vscode.extensions \
&& ls -la /root/.vscode/extensions
###############################################################################
# Build the actual add-on.
###############################################################################
# hadolint ignore=DL3006
FROM ${BUILD_FROM}
# Confiure locale
ENV LANG=en_US.UTF-8
# Set shell
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
# Copy Python requirements file
COPY requirements.txt /tmp/requirements.txt
# Setup base system
ARG BUILD_ARCH
RUN \
apt-get update \
\
&& apt-get install -y --no-install-recommends \
build-essential=12.4ubuntu1 \
colordiff=1.0.18-1 \
git=1:2.17.1-1ubuntu0.4 \
locales=2.27-3ubuntu1 \
mosquitto-clients=1.4.15-2ubuntu0.18.04.2 \
net-tools=1.60+git20161116.90da8a0-1ubuntu1 \
nmap=7.60-1ubuntu5 \
openssh-client=1:7.6p1-4ubuntu0.3 \
openssl=1.1.0g-2ubuntu4.3 \
python3=3.6.7-1~18.04 \
python3-dev=3.6.7-1~18.04 \
wget=1.19.4-1ubuntu2.1 \
zsh=5.4.2-3ubuntu3.1 \
\
&& curl https://bootstrap.pypa.io/get-pip.py | python3 \
\
&& locale-gen en_US.UTF-8 \
\
&& curl -L -s -o /usr/bin/hassio \
"https://github.com/home-assistant/hassio-cli/releases/download/2.1.0/hassio_${BUILD_ARCH}" \
&& chmod a+x /usr/bin/hassio \
\
&& git clone --branch master --single-branch --depth 1 \
"git://github.com/robbyrussell/oh-my-zsh.git" ~/.oh-my-zsh \
\
&& git clone --branch master --single-branch --depth 1 \
"git://github.com/zsh-users/zsh-autosuggestions" \
~/.oh-my-zsh/custom/plugins/zsh-autosuggestions \
&& git clone --branch master --single-branch --depth 1 \
"git://github.com/zsh-users/zsh-syntax-highlighting.git" \
~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting \
\
&& sed -i -e "s#bin/bash#bin/zsh#" /etc/passwd \
\
&& update-alternatives \
--install /usr/bin/python python /usr/bin/python3 10 \
\
&& pip3 install --no-cache-dir -r /tmp/requirements.txt \
\
&& apt-get purge -y --auto-remove \
python3-dev \
build-essential \
\
&& find /usr/local/lib/python3.6/ -type d -name tests -depth -exec rm -rf {} \; \
&& find /usr/local/lib/python3.6/ -type d -name test -depth -exec rm -rf {} \; \
&& find /usr/local/lib/python3.6/ -name __pycache__ -depth -exec rm -rf {} \; \
&& find /usr/local/lib/python3.6/ -name "*.pyc" -depth -exec rm -f {} \; \
\
&& rm -fr \
/tmp/* \
/var/{cache,log}/* \
/var/lib/apt/lists/*
# Copy root filesystem
COPY rootfs /
# Get the code server binary
COPY --from=codeserver /usr/local/bin/code-server /usr/local/bin/code-server
# Get the custom extensions
COPY --from=vscode /root/.vscode/extensions /root/.code-server/extensions
# Build arguments
ARG BUILD_DATE
ARG BUILD_REF
ARG BUILD_VERSION
# Labels
LABEL \
io.hass.name="Visual Studio Code" \
io.hass.description="Visual Studio Code, accessible through the browser." \
io.hass.arch="${BUILD_ARCH}" \
io.hass.type="addon" \
io.hass.version=${BUILD_VERSION} \
maintainer="Franck Nijhof <frenck@addons.community>" \
org.label-schema.description="Visual Studio Code, accessible through the browser." \
org.label-schema.build-date=${BUILD_DATE} \
org.label-schema.name="Visual Studio Code" \
org.label-schema.schema-version="1.0" \
org.label-schema.url="https://community.home-assistant.io/?u=frenck" \
org.label-schema.usage="https://github.com/hassio-addons/addon-vscode/tree/master/README.md" \
org.label-schema.vcs-ref=${BUILD_REF} \
org.label-schema.vcs-url="https://github.com/hassio-addons/addon-vscode" \
org.label-schema.vendor="Community Hass.io Add-ons"

6
vscode/build.json Normal file
View file

@ -0,0 +1,6 @@
{
"build_from": {
"amd64": "hassioaddons/ubuntu-base-amd64:3.0.0"
},
"args": {}
}

47
vscode/config.json Executable file
View file

@ -0,0 +1,47 @@
{
"name": "Visual Studio Code",
"version": "dev",
"slug": "vscode",
"description": "Fully feature VSCode experience, to edit your HA config in the browser, including autocompletion!",
"url": "https://github.com/hassio-addons/addon-vscode",
"webui": "[PROTO:ssl]://[HOST]:[PORT:1337]",
"startup": "services",
"arch": [
"amd64"
],
"boot": "auto",
"hassio_api": true,
"hassio_role": "default",
"homeassistant_api": true,
"host_network": false,
"auto_uart": true,
"ports": {
"1337/tcp": 1337
},
"map": [
"config:rw",
"ssl:rw",
"addons:rw",
"share:rw",
"backup:rw"
],
"options": {
"password": "",
"ssl": false,
"certfile": "fullchain.pem",
"keyfile": "privkey.pem",
"packages": [],
"init_commands": []
},
"schema": {
"log_level": "match(^(trace|debug|info|notice|warning|error|fatal)$)?",
"password": "str",
"ssl": "bool",
"certfile": "str",
"keyfile": "str",
"packages": ["str"],
"init_commands": ["str"],
"leave_front_door_open": "bool?",
"i_like_to_be_pwned": "bool?"
}
}

BIN
vscode/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

BIN
vscode/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

2
vscode/requirements.txt Normal file
View file

@ -0,0 +1,2 @@
homeassistant_cli==0.6.0
yamllint==1.15.0

View file

@ -0,0 +1,27 @@
#!/usr/bin/with-contenv bashio
# ==============================================================================
# Community Hass.io Add-ons: Visual Studio Code
# This files check if all user configuration requirements are met
# ==============================================================================
bashio::config.require.ssl
if ! bashio::config.true 'leave_front_door_open'; then
if ! bashio::config.true 'i_like_to_be_pwned'; then
bashio::config.require.safe_password
else
bashio::config.require.password
fi
fi
if bashio::config.true 'leave_front_door_open' \
&& bashio::config.true 'ssl';
then
bashio::log.fatal
bashio::log.fatal "Due to a bug in code-server (which this add-on uses),"
bashio::log.fatal "it is impossible to disable authentication while"
bashio::log.fatal "using SSL."
bashio::log.fatal
bashio::log.fatal "Please enable authentication and set a password."
bashio::log.fatal
bashio::exit.nok
fi

View file

@ -0,0 +1,11 @@
#!/usr/bin/with-contenv bashio
# ==============================================================================
# Community Hass.io Add-ons: Visual Studio Code
# Links some common directories to the user's home folder for convenience
# ==============================================================================
readonly -a directories=(addons backup config share ssl)
for dir in "${directories[@]}"; do
ln -s "/${dir}" "${HOME}/${dir}" \
|| bashio::log.warning "Failed linking common directory: ${dir}"
done

View file

@ -0,0 +1,14 @@
#!/usr/bin/with-contenv bashio
# ==============================================================================
# Community Hass.io Add-ons: Visual Studio Code
# This files check if all user configuration requirements are met
# ==============================================================================
# Ensure persistent data folder exists
if ! bashio::fs.directory_exists '/data/vscode'; then
mkdir -p /data/vscode/extensions \
|| bashio::exit.nok "Could not create persistent storage folder."
fi
# Copy in the extensions we deliver
cp -R /root/.code-server/extensions/* /data/vscode/extensions

View file

@ -0,0 +1,17 @@
#!/usr/bin/with-contenv bashio
# ==============================================================================
# Community Hass.io Add-ons: Visual Studio Code
# Sets up the users .ssh folder to be persistent
# ==============================================================================
readonly SSH_USER_PATH=/data/.ssh
if ! bashio::fs.directory_exists "${SSH_USER_PATH}"; then
mkdir -p "${SSH_USER_PATH}" \
|| bashio::exit.nok 'Failed to create a persistent .ssh folder'
chmod 700 "${SSH_USER_PATH}" \
|| bashio::exit.nok \
'Failed setting permissions on persistent .ssh folder'
fi
ln -s "${SSH_USER_PATH}" ~/.ssh

View file

@ -0,0 +1,14 @@
#!/usr/bin/with-contenv bashio
# ==============================================================================
# Community Hass.io Add-ons: Visual Studio Code
# Install user configured/requested packages
# ==============================================================================
if bashio::config.has_value 'packages'; then
apt update \
|| bashio::exit.nok 'Failed updating Ubuntu packages repository indexes'
for package in $(bashio::config 'packages'); do
apt add -y "$package" \
|| bashio::exit.nok "Failed installing package ${package}"
done
fi

View file

@ -0,0 +1,11 @@
#!/usr/bin/with-contenv bashio
# ==============================================================================
# Community Hass.io Add-ons: Visual Studio Code
# Executes user configured/requested commands on startup
# ==============================================================================
if bashio::config.has_value 'init_commands'; then
while read -r cmd; do
eval "${cmd}" \
|| bashio::exit.nok "Failed executing init command: ${cmd}"
done <<< "$(bashio::config 'init_commands')"
fi

12
vscode/rootfs/etc/motd Executable file
View file

@ -0,0 +1,12 @@
_ _ _
| | | | (_)
| |__| | __ _ ___ ___ _ ___
| __ |/ _` / __/ __| | |/ _ \
| | | | (_| \__ \__ \_| | (_) |
|_| |_|\__,_|___/___(_)_|\___/
Our Cli:
$ hassio help

View file

@ -0,0 +1,9 @@
#!/usr/bin/execlineb -S0
# ==============================================================================
# Community Hass.io Add-ons: Visual Studio Code
# Take down the S6 supervision tree when the code server fails
# ==============================================================================
if -n { s6-test $# -ne 0 }
if -n { s6-test ${1} -eq 256 }
s6-svscanctl -t /var/run/s6/services

View file

@ -0,0 +1,32 @@
#!/usr/bin/with-contenv bashio
# ==============================================================================
# Community Hass.io Add-ons: Visual Studio Code
# Runs the code server
# ==============================================================================
declare -a options
bashio::log.info 'Starting the code server...'
# Non-interactive
options+=(--port 1337)
options+=(--data-dir "/data/vscode")
if bashio::config.false 'ssl'; then
options+=(--allow-http)
else
options+=(--cert "/ssl/$(bashio::config 'certfile')")
options+=(--cert-key "/ssl/$(bashio::config 'keyfile')")
fi
if ! bashio::config.has_value 'password'; then
options+=(--no-auth)
else
options+=(--password "$(bashio::config 'password')")
fi
# Export env variables for the Home Assistant extension
export HASS_SERVER="http://hassio/homeassistant"
export HASS_TOKEN="${HASSIO_TOKEN:-}"
# Run the code server
exec code-server "${options[@]}" /config

100
vscode/rootfs/root/.zshrc Executable file
View file

@ -0,0 +1,100 @@
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH=$HOME/.oh-my-zsh
# Set name of the theme to load. Optionally, if you set this to "random"
# it'll load a random theme each time that oh-my-zsh is loaded.
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
ZSH_THEME="robbyrussell"
# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"
# Uncomment the following line to use hyphen-insensitive completion. Case
# sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"
# Uncomment the following line to disable bi-weekly auto-update checks.
DISABLE_AUTO_UPDATE="true"
# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13
# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"
# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"
# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"
# Uncomment the following line to display red dots whilst waiting for completion.
COMPLETION_WAITING_DOTS="true"
# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"
# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# The optional three formats: "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# HIST_STAMPS="mm/dd/yyyy"
# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder
# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
extract
git
nmap
pip
python
rsync
zsh-autosuggestions
zsh-syntax-highlighting
)
source $ZSH/oh-my-zsh.sh
# User configuration
# export MANPATH="/usr/local/man:$MANPATH"
# You may need to manually set your language environment
# export LANG=en_US.UTF-8
# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
# export EDITOR='vim'
# else
# export EDITOR='mvim'
# fi
# Compilation flags
# export ARCHFLAGS="-arch x86_64"
# ssh
# export SSH_KEY_PATH="~/.ssh/rsa_id"
# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
# Home Assistant CLI
eval "$(_HASS_CLI_COMPLETE=source_zsh hass-cli)"
# Show motd on start
cat /etc/motd

2
vscode/vscode.extensions Normal file
View file

@ -0,0 +1,2 @@
keesschollaart.vscode-home-assistant
lukas-tr.materialdesignicons-intellisense