addon-vscode/vscode/rootfs/etc/s6-overlay/s6-rc.d/init-code-server/run
Richard Sperry 29a56f36d8 devenv
2025-04-17 12:23:28 -07:00

207 lines
8.7 KiB
Text

#!/command/with-contenv bashio
# shellcheck shell=bash
# ==============================================================================
# Home Assistant Community Add-on: Studio Code Server
# Sets up code-server.
# ==============================================================================
bashio::log.info "Initalizing code-server configs"
# shellcheck source=../paths.sh
source "/etc/s6-overlay/s6-rc.d/paths.sh"
# List of previous config hashes, to allow upgrade "default" configs.
readonly -a PREVIOUS_DEFAULT_CONFIG_HASHES=(
86776df88391c3d94f79f62b430f9ff8538960628d5e04fb660165a5a46640d2e74f89cd68b3e6985dc59101ae2dda00a1e25aa48381acfd4736858c5f23878b
2be4c50575c05711d30121c3a1320698d3dbb034523c481be3aad3b638af3e0a46113d8c3f8cfc1b0f01e4cc10f32da3a30feca161c31b5b27177aeb7231bf70
d4745002643a0168f65bc5b465a9baff59af0fb4783b50553e1a4e8b3f1a569fc4905df19b7743f7f313a5809a5128d997fc4b5e0a0e5db0a01f47b3b7bec39d
6ed36f832778926fa614fc12eec97b8f813bbb1a04f709769c9e5f54bc8e3cfc05a110155921a3b2ae47fc8389d24fa2bd385e4ce5a6c94850813791a6ac1c82
944d9ba57968666353df7e9dc78ec5d94b9b052e1abf863b51bc1f372d9f35cb2d93259f153e9ab3b3dd1b520bfcd7bddb54803bcfd9eb65975ae8fb5553663c
4ef960e3d6e795adda51d5cbeb18d309fee87ba5cd177292a21b5a70a71a4726ae7053c3793cddc6d63d3b4dacc180ad3ea12d005fc8d63a1bc4cb29f9a17f18
c5b8acf06ef6d9a2435e9ddb92cb9fce7cfbfe4a2206b0e0c3c4ed514cc926f8d3c662e694a995d102b5ba939056f93201c220558e06e1cd0872bfb1995ba148
08d86c84a0d80720b22712e878963e90cbb34b659330dad8a823f3c5c7f0ae043d197a5e3020dd7ab4fda3625e17f794675ec074984951e7107db2488898a8d0
5243d7664d30b5aa0c45fbe1089cccdf85c5ade17cddd97e21b3a29ccb37b20d20bdfecc141ad6e1a7aa5ea8ee61695a79a43140a2d53f9f91687bc61f7e496c
e5106aaaff5f67a693fd49deb7dd52f1cc0f7ba542b33c99c0099bf58f1a33c789d1c5e3803dea2942540c07c734f3ca317c3cc91673aff388cb79ee7112db0b
b28a299111ac74cc9498f76ce2cb437b2cf5e8e7bb9388d9929347e2948bd56591bd499d5de04638553f6e12fa77c53320a046e96cffce9fa4f8c924d5c91581
dbe905f0935b2fea8bb5ba379bff78db9d056ea6d0965c68aa9b07938a52cbbf117186452b2b6c766aa4cf93cc3494ad91b763e8500e515e41d8cde31a3821ae
2212babaa9dc46d38ba239052c9c05151abe517cc8b493d0d351a96bf70c1efa4f7479f6ed0b75811429ba649906eb5657d73b0732b460e718a5c66603dcdbda
)
CODE_SERVER_CONFIG="$DEFAULT_CONFIG_PATH/config.yaml"
run() {
setup_config_path
setup_config_files
setup_code_workspace
setup_clean_view
setup_log_level
setup_user_settings
setup_extensions
}
setup_clean_view() {
# Links some common directories to the user's home folder for convenience
for dir in "${DIRECTORIES[@]}"; do
current="${CODE_SERVER_WORKSPACE_DIR}"/"${dir}"
if [ -e /"${dir}" ]; then
if [ ! -e "${current}" ]; then
bashio::log.warning "Creating link for: ${dir}..."
ln -sn "/${dir}" "$current" ||
bashio::log.warning "Failed linking common directory: ${dir}"
elif [ ! -L "${current}" ]; then
bashio::log.warning "Failed link: ${dir}. Removing and recreating..."
rm -f "${current}"
ln -sn "/${dir}" "$current" ||
bashio::log.warning "Failed linking common directory: ${dir}"
else
bashio::log.info "${dir} has good link."
fi
else
if [ -e "${current}" ]; then
rm "${current}"
bashio::log.warning "${dir} does not exist, cannot link, removed existing link..."
else
bashio::log.warning "${dir} does not exist, cannot link..."
fi
fi
done
if [[ ! -d "${CODE_SERVER_WORKSPACE_DIR}"/code-server-config ]]; then
ln -sn "$DEFAULT_CONFIG_PATH" "${CODE_SERVER_WORKSPACE_DIR}"/code-server-config ||
bashio::log.warning "Failed linking common directory: ${dir}"
fi
}
setup_code_workspace() {
# Create the workspace dir
if [ ! -d "$CODE_SERVER_WORKSPACE_DIR" ]; then
bashio::log.warning "CODE_SERVER_WORKSPACE_DIR does not exist. Creating..."
mkdir -p "$CODE_SERVER_WORKSPACE_DIR"
fi
# Copies the default workspace
if [ ! -e "$CODE_SERVER_WORKSPACE_FILE" ]; then
bashio::log.warning "CODE_SERVER_WORKSPACE_FILE does not exist. Creating..."
cp "$DEFAULT_FILES_PATH/vscode/SERVER.code-workspace" "$CODE_SERVER_WORKSPACE_FILE"
fi
# .vscode dir
if [ ! -d "$CODE_SERVER_VSCODE_DIR" ]; then
bashio::log.warning "CODE_SERVER_VSCODE_DIR does not exist. Creating..."
mkdir -p "$CODE_SERVER_VSCODE_DIR"
fi
# tasks.json
if [ ! -e "$CODE_SERVER_VSCODE_TASKS" ]; then
bashio::log.warning "CODE_SERVER_VSCODE_TASKS does not exist. Creating..."
cp "$DEFAULT_FILES_PATH/vscode/tasks.json" "$CODE_SERVER_VSCODE_TASKS"
fi
}
setup_config_path() {
# Ensure config path exists
if bashio::config.has_value "vscode_config_path"; then
temp_path=$(bashio::config 'vscode_config_path')
if ! bashio::fs.directory_exists "${temp_path}"; then
bashio::exit.nok "Configured config path does not exist"
fi
export CODE_SERVER_CONFIG=$temp_path
else
# otherwise set to local dir
if ! bashio::fs.directory_exists "$DEFAULT_CONFIG_PATH"; then
bashio::log.warning "DEFAULT_CONFIG_PATH path does not exist. Creating... "
mkdir -p "$DEFAULT_CONFIG_PATH" ||
bashio::exit.nok "Could not create persistent storage folder."
fi
fi
bashio::log.info "using code-server config path: $CODE_SERVER_CONFIG"
}
setup_config_files() {
# Ensure code-server config
if [ ! -e "$CODE_SERVER_CONFIG" ]; then
bashio::log.warning "CODE_SERVER_CONFIG does not exist. Creating..."
cp "$DEFAULT_FILES_PATH/core/config.yaml" "$CODE_SERVER_CONFIG"
fi
# Set a link to where code-server expects the config to be
if [ ! -d "$ROOT_CODE_SERVER_CONFIG_PATH" ]; then
mkdir -p "$ROOT_CODE_SERVER_CONFIG_PATH"
ln -s "$CODE_SERVER_CONFIG" "$ROOT_CODE_SERVER_CONFIG_PATH/config.yaml"
fi
}
setup_extensions() {
# Ensure extensions path
if [ ! -e "$CODE_SERVER_EXTENSIONS_LIST" ]; then
bashio::log.warning "CODE_SERVER_EXTENSIONS_LIST does not exist. Creating...."
cp "$DEFAULT_FILES_PATH/vscode/vscode.extensions" "$CODE_SERVER_EXTENSIONS_LIST"
fi
if [ ! -d "$CODE_SERVER_EXTENSIONS_INSTALL_PATH" ]; then
bashio::log.warning "CODE_SERVER_EXTENSIONS_INSTALL_PATH does not exist. Creating..."
mkdir -p "$CODE_SERVER_EXTENSIONS_INSTALL_PATH" ||
bashio::exit.nok "Could not create persistent extensions folder."
touch "$CODE_SERVER_EXTENSIONS_INSTALL_PATH"/extensions.json
fi
sed -i "/extensions-dir:/c extensions-dir: \"$CODE_SERVER_EXTENSIONS_INSTALL_PATH\"" "$CODE_SERVER_CONFIG"
# Install code-server extensions via code-server so terminate is aware of them.
bashio::log.info "Installing extentions listed in addon_configs/vscode/vscode.extensions"
while read -r extention; do
if [ ! -e "$CODE_SERVER_EXTENSIONS_INSTALL_PATH/$extention*" ]; then
code-server --install-extension="$extention"
bashio::log.info "Installed $extention"
else
bashio::log.info "Has $extention installed"
fi
done <"$CODE_SERVER_EXTENSIONS_LIST"
}
setup_log_level() {
if bashio::config.has_value 'log_level'; then
log_level=$(bashio::config 'log_level')
if [ "$log_level" = "warning" ]; then
log_level="warn"
fi
if [ "$log_level" = "notice" ]; then
log_level="warn"
fi
if [ "$log_level" = "fatal" ]; then
log_level="error"
fi
sed -i "/log:/c log: \"$log_level\"" "$CODE_SERVER_CONFIG"
fi
}
setup_user_settings() {
# Ensure User settings path
user_path="$DEFAULT_CONFIG_PATH/user"
if [ ! -d "$user_path" ]; then
mkdir -p "$user_path" ||
bashio::exit.nok "Could not create persistent User folder."
fi
# Sets up default user settings on first start.
if ! bashio::fs.file_exists "$user_path"/settings.json; then
cp "$DEFAULT_FILES_PATH/vscode/settings.json" "$user_path/settings.json"
else
# Upgrade if settings.json is still default from previous version.
current=$(sha512sum "$user_path"/settings.json | cut -d " " -f 1)
if [[ " ${PREVIOUS_DEFAULT_CONFIG_HASHES[*]} " == *" ${current} "* ]]; then
cp "$DEFAULT_FILES_PATH/vscode/settings.json" "$user_path/settings.json"
fi
fi
sed -i "/user-data-dir:/c user-data-dir: \"$user_path\"" "$CODE_SERVER_CONFIG"
if ! bashio::fs.file_exists "$user_path"/workspaces.json; then
cp "$DEFAULT_FILES_PATH/vscode/workspaces.json" "$user_path/workspaces.json"
fi
}
run