|
| 1 | +#!/bin/bash |
| 2 | +# Copyright 2025 rising3(Michio Nakagawa) |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +set -e |
| 18 | + |
| 19 | +ESC=$(printf '\033') |
| 20 | +TARGET_DIR=/home/$USER/.config/systemd/user |
| 21 | + |
| 22 | +function helper_gen() { |
| 23 | + cat <<EOS |
| 24 | +#!/bin/bash |
| 25 | +set -e |
| 26 | +
|
| 27 | +ip=\$(ip -4 addr | awk '/state UP/ {i=\$2} /inet / && !f {sub(/\/.*/, "", \$2); if(i~/^e/||i~/^en/) {print \$2; f=1} else if(i~/^w/||i~/^wl/) {print \$2; f=1}} END{if(!f) print "0.0.0.0"}') |
| 28 | +
|
| 29 | +echo "\$ip" > "${TARGET_DIR}/code-tmp" |
| 30 | +
|
| 31 | +exit 0 |
| 32 | +EOS |
| 33 | +} |
| 34 | + |
| 35 | +function service_gen() { |
| 36 | + cat <<EOS |
| 37 | +[Unit] |
| 38 | +Description=VSCode Server service |
| 39 | +After=network-online.target |
| 40 | +Wants=network-online.target |
| 41 | +
|
| 42 | +[Service] |
| 43 | +Type=simple |
| 44 | +ExecStartPre=/bin/bash "${TARGET_DIR}/code-helper" |
| 45 | +ExecStart=/bin/bash -c '/usr/bin/code serve-web --without-connection-token --accept-server-license-terms --host \$(cat ${TARGET_DIR}/code-tmp)' |
| 46 | +ExecStartPost=/bin/bash -c "rm -f ${TARGET_DIR}/code-tmp" |
| 47 | +Restart=always |
| 48 | +RestartSec=5 |
| 49 | +
|
| 50 | +[Install] |
| 51 | +WantedBy=default.target |
| 52 | +EOS |
| 53 | +} |
| 54 | + |
| 55 | +function enable_linger() { |
| 56 | + if ! sudo loginctl enable-linger "$USER"; then |
| 57 | + printf "${ESC}[31m%s${ESC}[m\n" "✖ enable linger failed" |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | + printf "${ESC}[33m%s${ESC}[m\n" "✅ enable linger" |
| 61 | + exit 0 |
| 62 | +} |
| 63 | + |
| 64 | +# Array of required commands |
| 65 | +required_commands=("awk" "code") |
| 66 | + |
| 67 | +# Check if each command is installed |
| 68 | +for cmd in "${required_commands[@]}"; do |
| 69 | + if ! command -v "$cmd" &>/dev/null; then |
| 70 | + printf "${ESC}[31m%s${ESC}[m\n" "✖ $cmd is not installed. Please install it and try again." |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | +done |
| 74 | + |
| 75 | +if [ "$(ps -p 1 -o comm=)" != "systemd" ]; then |
| 76 | + printf "${ESC}[31m%s${ESC}[m\n" "✖ This script is only for systemd" |
| 77 | + exit 1 |
| 78 | +fi |
| 79 | + |
| 80 | +if [ "$(loginctl show-user "$USER" | grep 'Linger=yes')" != "Linger=yes" ]; then |
| 81 | + enable_linger |
| 82 | +fi |
| 83 | + |
| 84 | +if [ "$(systemctl --user is-active code.service)" == "active" ]; then |
| 85 | + printf "${ESC}[33m%s${ESC}[m\n" "✅ code.service is already running" |
| 86 | + exit 0 |
| 87 | +fi |
| 88 | + |
| 89 | +mkdir -p "${TARGET_DIR}" |
| 90 | + |
| 91 | +echo -e "$(helper_gen)" >"${TARGET_DIR}/code-helper" |
| 92 | +echo -e "$(service_gen)" >"${TARGET_DIR}/code.service" |
| 93 | + |
| 94 | +systemctl --user daemon-reload |
| 95 | +systemctl --user enable code.service |
| 96 | +systemctl --user start code.service |
| 97 | +exit 0 |
0 commit comments