diff --git a/install/ubuntu-install.sh b/install/ubuntu-install.sh index 0c56993..1dc4efb 100644 --- a/install/ubuntu-install.sh +++ b/install/ubuntu-install.sh @@ -20,6 +20,49 @@ msg_ok "Balíčky nainštalované" # ============================================================================= # Vytvorenie užívateľov z USERS_JSON + SELECTED_USERS # ============================================================================= + +# Funkcia na vytvorenie jedného užívateľa +create_user() { + local username="$1" + local sudo_flag="$2" + shift 2 + local keys=("$@") + + # Vytvorenie užívateľa s náhodným heslom + local random_pw + random_pw=$(openssl rand -base64 16) + useradd -m -s /bin/bash "$username" + echo "${username}:${random_pw}" | chpasswd + + # SSH kľúče + local user_home="/home/${username}" + mkdir -p "${user_home}/.ssh" + chmod 700 "${user_home}/.ssh" + for key in "${keys[@]}"; do + echo "$key" >> "${user_home}/.ssh/authorized_keys" + done + chmod 600 "${user_home}/.ssh/authorized_keys" + chown -R "${username}:${username}" "${user_home}/.ssh" + + # Sudo bez hesla + if [[ "$sudo_flag" == "true" ]]; then + echo "${username} ALL=(ALL) NOPASSWD: ALL" > "/etc/sudoers.d/${username}" + chmod 440 "/etc/sudoers.d/${username}" + fi + + msg_ok "Užívateľ vytvorený: ${username}" + + # Inštalácia Claude Code + msg_info "Inštalujem Claude Code pre ${username}" + su - "$username" -c "curl -fsSL https://claude.ai/install.sh | sh" || { + msg_warn "Claude Code inštalácia zlyhala pre ${username} (nefatálna chyba)" + } + msg_ok "Claude Code nainštalovaný pre ${username}" +} + +# Zoznam vytvorených užívateľov (pre referenciu) +CREATED_USERS=() + if [[ -n "${USERS_JSON:-}" && -n "${SELECTED_USERS:-}" ]]; then msg_info "Vytváram užívateľov" @@ -27,7 +70,6 @@ if [[ -n "${USERS_JSON:-}" && -n "${SELECTED_USERS:-}" ]]; then selected_list=$(echo "$SELECTED_USERS" | tr -d '"') # Parsovanie users.json bez jq — cez grep/sed/awk - # Prechádzame každý blok užívateľa current_user="" current_sudo="false" current_keys=() @@ -38,30 +80,9 @@ if [[ -n "${USERS_JSON:-}" && -n "${SELECTED_USERS:-}" ]]; then if echo "$line" | grep -q '"username"'; then # Ak máme predchádzajúceho užívateľa, spracuj ho if [[ -n "$current_user" ]]; then - # Kontrola, či bol užívateľ vybraný if echo "$selected_list" | grep -qw "$current_user"; then - # Vytvorenie užívateľa s náhodným heslom - random_pw=$(openssl rand -base64 16) - useradd -m -s /bin/bash "$current_user" - echo "${current_user}:${random_pw}" | chpasswd - - # SSH kľúče - user_home="/home/${current_user}" - mkdir -p "${user_home}/.ssh" - chmod 700 "${user_home}/.ssh" - for key in "${current_keys[@]}"; do - echo "$key" >> "${user_home}/.ssh/authorized_keys" - done - chmod 600 "${user_home}/.ssh/authorized_keys" - chown -R "${current_user}:${current_user}" "${user_home}/.ssh" - - # Sudo bez hesla - if [[ "$current_sudo" == "true" ]]; then - echo "${current_user} ALL=(ALL) NOPASSWD: ALL" > "/etc/sudoers.d/${current_user}" - chmod 440 "/etc/sudoers.d/${current_user}" - fi - - msg_ok "Užívateľ vytvorený: ${current_user}" + create_user "$current_user" "$current_sudo" "${current_keys[@]}" + CREATED_USERS+=("$current_user") fi fi current_user=$(echo "$line" | sed 's/.*"username"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/') @@ -87,7 +108,6 @@ if [[ -n "${USERS_JSON:-}" && -n "${SELECTED_USERS:-}" ]]; then in_keys=false continue fi - local key key=$(echo "$line" | sed 's/.*"\(ssh-[^"]*\)".*/\1/') [[ -n "$key" && "$key" != "$line" ]] && current_keys+=("$key") fi @@ -96,25 +116,8 @@ if [[ -n "${USERS_JSON:-}" && -n "${SELECTED_USERS:-}" ]]; then # Spracuj posledného užívateľa if [[ -n "$current_user" ]]; then if echo "$selected_list" | grep -qw "$current_user"; then - random_pw=$(openssl rand -base64 16) - useradd -m -s /bin/bash "$current_user" - echo "${current_user}:${random_pw}" | chpasswd - - user_home="/home/${current_user}" - mkdir -p "${user_home}/.ssh" - chmod 700 "${user_home}/.ssh" - for key in "${current_keys[@]}"; do - echo "$key" >> "${user_home}/.ssh/authorized_keys" - done - chmod 600 "${user_home}/.ssh/authorized_keys" - chown -R "${current_user}:${current_user}" "${user_home}/.ssh" - - if [[ "$current_sudo" == "true" ]]; then - echo "${current_user} ALL=(ALL) NOPASSWD: ALL" > "/etc/sudoers.d/${current_user}" - chmod 440 "/etc/sudoers.d/${current_user}" - fi - - msg_ok "Užívateľ vytvorený: ${current_user}" + create_user "$current_user" "$current_sudo" "${current_keys[@]}" + CREATED_USERS+=("$current_user") fi fi fi