| 1759 | } |
| 1760 | |
| 1761 | func buildDirectBootstrapScript(cfg cfgpkg.AnsibleBootstrapConfig, keyContent string, dryRun bool) string { |
| 1762 | userQuoted := shellSingleQuote(strings.TrimSpace(cfg.Username)) |
| 1763 | uidQuoted := shellSingleQuote(formatBootstrapUID(cfg.UID)) |
| 1764 | shellQuoted := shellSingleQuote(strings.TrimSpace(cfg.Shell)) |
| 1765 | passQuoted := shellSingleQuote(strings.TrimSpace(cfg.Password)) |
| 1766 | keyQuoted := shellSingleQuote(strings.TrimSpace(keyContent)) |
| 1767 | modeQuoted := shellSingleQuote(strings.TrimSpace(cfg.SudoersFileMode)) |
| 1768 | |
| 1769 | return fmt.Sprintf(`#!/bin/sh |
| 1770 | set -eu |
| 1771 | |
| 1772 | BOOTSTRAP_USER=%s |
| 1773 | BOOTSTRAP_UID=%s |
| 1774 | BOOTSTRAP_SHELL=%s |
| 1775 | BOOTSTRAP_PASS=%s |
| 1776 | BOOTSTRAP_KEY=%s |
| 1777 | SUDOERS_MODE=%s |
| 1778 | DRY_RUN=%t |
| 1779 | CREATE_HOME=%t |
| 1780 | INSTALL_KEY=%t |
| 1781 | SET_PASSWORD=%t |
| 1782 | GRANT_SUDO=%t |
| 1783 | |
| 1784 | changed=0 |
| 1785 | applied_create_user=0 |
| 1786 | applied_update_shell=0 |
| 1787 | applied_install_authorized_key=0 |
| 1788 | applied_set_password=0 |
| 1789 | applied_create_sudoers=0 |
| 1790 | |
| 1791 | if [ "$DRY_RUN" = "true" ]; then |
| 1792 | would_change=0 |
| 1793 | |
| 1794 | if ! id -u "$BOOTSTRAP_USER" >/dev/null 2>&1; then |
| 1795 | echo "would_create_user=1" |
| 1796 | if [ -n "$BOOTSTRAP_UID" ]; then |
| 1797 | echo "plan_useradd=useradd -u $BOOTSTRAP_UID -s $BOOTSTRAP_SHELL $BOOTSTRAP_USER" |
| 1798 | else |
| 1799 | echo "plan_useradd=useradd -s $BOOTSTRAP_SHELL $BOOTSTRAP_USER" |
| 1800 | fi |
| 1801 | would_change=$((would_change+1)) |
| 1802 | else |
| 1803 | echo "would_create_user=0" |
| 1804 | if [ -n "$BOOTSTRAP_UID" ]; then |
| 1805 | CURRENT_UID="$(id -u "$BOOTSTRAP_USER")" |
| 1806 | if [ "$CURRENT_UID" != "$BOOTSTRAP_UID" ]; then |
| 1807 | echo "would_update_uid=1" |
| 1808 | echo "plan_usermod=usermod -u $BOOTSTRAP_UID $BOOTSTRAP_USER" |
| 1809 | would_change=$((would_change+1)) |
| 1810 | else |
| 1811 | echo "would_update_uid=0" |
| 1812 | fi |
| 1813 | fi |
| 1814 | fi |
| 1815 | |
| 1816 | if [ "$INSTALL_KEY" = "true" ] && [ -n "$BOOTSTRAP_KEY" ]; then |
| 1817 | if id -u "$BOOTSTRAP_USER" >/dev/null 2>&1; then |
| 1818 | HOME_DIR="$(getent passwd "$BOOTSTRAP_USER" | cut -d: -f6)" |