install.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. #!/bin/sh
  2. set -e
  3. # Usage:
  4. # curl ... | ENV_VAR=... sh -
  5. # or
  6. # ENV_VAR=... ./install.sh
  7. #
  8. # Example:
  9. # Installing a server without an agent:
  10. # curl ... | INSTALL_K3S_EXEC="--disable-agent" sh -
  11. # Installing an agent to point at a server:
  12. # curl ... | K3S_TOKEN=xxx K3S_URL=https://server-url:6443 sh -
  13. #
  14. # Environment variables:
  15. # - K3S_*
  16. # Environment variables which begin with K3S_ will be preserved for the
  17. # systemd service to use. Setting K3S_URL without explicitly setting
  18. # a systemd exec command will default the command to "agent", and we
  19. # enforce that K3S_TOKEN or K3S_CLUSTER_SECRET is also set.
  20. #
  21. # - INSTALL_K3S_SKIP_DOWNLOAD
  22. # If set to true will not download k3s hash or binary.
  23. #
  24. # - INSTALL_K3S_SKIP_START
  25. # If set to true will not start k3s service.
  26. #
  27. # - INSTALL_K3S_VERSION
  28. # Version of k3s to download from github. Will attempt to download the
  29. # latest version if not specified.
  30. #
  31. # - INSTALL_K3S_BIN_DIR
  32. # Directory to install k3s binary, links, and uninstall script to, or use
  33. # /usr/local/bin as the default
  34. #
  35. # - INSTALL_K3S_BIN_DIR_READ_ONLY
  36. # If set to true will not write files to INSTALL_K3S_BIN_DIR, forces
  37. # setting INSTALL_K3S_SKIP_DOWNLOAD=true
  38. #
  39. # - INSTALL_K3S_SYSTEMD_DIR
  40. # Directory to install systemd service and environment files to, or use
  41. # /etc/systemd/system as the default
  42. #
  43. # - INSTALL_K3S_EXEC or script arguments
  44. # Command with flags to use for launching k3s in the systemd service, if
  45. # the command is not specified will default to "agent" if K3S_URL is set
  46. # or "server" if not. The final systemd command resolves to a combination
  47. # of EXEC and script args ($@).
  48. #
  49. # The following commands result in the same behavior:
  50. # curl ... | INSTALL_K3S_EXEC="--disable-agent" sh -s -
  51. # curl ... | INSTALL_K3S_EXEC="server --disable-agent" sh -s -
  52. # curl ... | INSTALL_K3S_EXEC="server" sh -s - --disable-agent
  53. # curl ... | sh -s - server --disable-agent
  54. # curl ... | sh -s - --disable-agent
  55. #
  56. # - INSTALL_K3S_NAME
  57. # Name of systemd service to create, will default from the k3s exec command
  58. # if not specified. If specified the name will be prefixed with 'k3s-'.
  59. #
  60. # - INSTALL_K3S_TYPE
  61. # Type of systemd service to create, will default from the k3s exec command
  62. # if not specified.
  63. GITHUB_URL=https://github.com/rancher/k3s/releases
  64. # --- helper functions for logs ---
  65. info()
  66. {
  67. echo "[INFO] " "$@"
  68. }
  69. fatal()
  70. {
  71. echo "[ERROR] " "$@"
  72. exit 1
  73. }
  74. # --- fatal if no systemd or openrc ---
  75. verify_system() {
  76. if [ -x /sbin/openrc-run ]; then
  77. HAS_OPENRC=true
  78. return
  79. fi
  80. if [ -d /run/systemd ]; then
  81. HAS_SYSTEMD=true
  82. return
  83. fi
  84. fatal "Can not find systemd or openrc to use as a process supervisor for k3s"
  85. }
  86. # --- add quotes to command arguments ---
  87. quote() {
  88. for arg in "$@"; do
  89. printf "%s\n" "$arg" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/"
  90. done
  91. }
  92. # --- add indentation and trailing slash to quoted args ---
  93. quote_indent() {
  94. printf ' \\'"\n"
  95. for arg in "$@"; do
  96. printf "\t%s "'\\'"\n" "$(quote "$arg")"
  97. done
  98. }
  99. # --- escape most punctuation characters, except quotes, forward slash, and space ---
  100. escape() {
  101. printf "%s" "$@" | sed -e 's/\([][!#$%&()*;<=>?\_`{|}]\)/\\\1/g;'
  102. }
  103. # --- escape double quotes ---
  104. escape_dq() {
  105. printf "%s" "$@" | sed -e 's/"/\\"/g'
  106. }
  107. # --- define needed environment variables ---
  108. setup_env() {
  109. # --- use command args if passed or create default ---
  110. case "$1" in
  111. # --- if we only have flags discover if command should be server or agent ---
  112. (-*|"")
  113. if [ -z "${K3S_URL}" ]; then
  114. CMD_K3S=server
  115. else
  116. if [ -z "${K3S_TOKEN}" ] && [ -z "${K3S_CLUSTER_SECRET}" ]; then
  117. fatal "Defaulted k3s exec command to 'agent' because K3S_URL is defined, but K3S_TOKEN or K3S_CLUSTER_SECRET is not defined."
  118. fi
  119. CMD_K3S=agent
  120. fi
  121. ;;
  122. # --- command is provided ---
  123. (*)
  124. CMD_K3S="$1"
  125. shift
  126. ;;
  127. esac
  128. CMD_K3S_EXEC="${CMD_K3S}$(quote_indent "$@")"
  129. # --- use systemd name if defined or create default ---
  130. if [ -n "${INSTALL_K3S_NAME}" ]; then
  131. SYSTEM_NAME=k3s-${INSTALL_K3S_NAME}
  132. else
  133. if [ "${CMD_K3S}" = "server" ]; then
  134. SYSTEM_NAME=k3s
  135. else
  136. SYSTEM_NAME=k3s-${CMD_K3S}
  137. fi
  138. fi
  139. # --- check for invalid characters in system name ---
  140. valid_chars=$(printf "%s" "${SYSTEM_NAME}" | sed -e 's/[][!#$%&()*;<=>?\_`{|}/[:space:]]/^/g;' )
  141. if [ "${SYSTEM_NAME}" != "${valid_chars}" ]; then
  142. invalid_chars=$(printf "%s" "${valid_chars}" | sed -e 's/[^^]/ /g')
  143. fatal "Invalid characters for system name:
  144. ${SYSTEM_NAME}
  145. ${invalid_chars}"
  146. fi
  147. # --- set related files from system name ---
  148. SERVICE_K3S=${SYSTEM_NAME}.service
  149. UNINSTALL_K3S_SH=${SYSTEM_NAME}-uninstall.sh
  150. KILLALL_K3S_SH=k3s-killall.sh
  151. # --- use sudo if we are not already root ---
  152. SUDO=sudo
  153. if [ `id -u` = 0 ]; then
  154. SUDO=
  155. fi
  156. # --- use systemd type if defined or create default ---
  157. if [ -n "${INSTALL_K3S_TYPE}" ]; then
  158. SYSTEMD_TYPE="${INSTALL_K3S_TYPE}"
  159. else
  160. if [ "${CMD_K3S}" = "server" ]; then
  161. SYSTEMD_TYPE=notify
  162. else
  163. SYSTEMD_TYPE=exec
  164. fi
  165. fi
  166. # --- use binary install directory if defined or create default ---
  167. if [ -n "${INSTALL_K3S_BIN_DIR}" ]; then
  168. BIN_DIR="${INSTALL_K3S_BIN_DIR}"
  169. else
  170. BIN_DIR="/usr/local/bin"
  171. fi
  172. # --- use systemd directory if defined or create default ---
  173. if [ -n "${INSTALL_K3S_SYSTEMD_DIR}" ]; then
  174. SYSTEMD_DIR="${INSTALL_K3S_SYSTEMD_DIR}"
  175. else
  176. SYSTEMD_DIR="/etc/systemd/system"
  177. fi
  178. # --- use servive or environment location depending on systemd/openrc ---
  179. if [ "${HAS_SYSTEMD}" = "true" ]; then
  180. FILE_K3S_SERVICE=${SYSTEMD_DIR}/${SERVICE_K3S}
  181. FILE_K3S_ENV=${SYSTEMD_DIR}/${SERVICE_K3S}.env
  182. elif [ "${HAS_OPENRC}" = "true" ]; then
  183. $SUDO mkdir -p /etc/rancher/k3s
  184. FILE_K3S_SERVICE=/etc/init.d/${SYSTEM_NAME}
  185. FILE_K3S_ENV=/etc/rancher/k3s/${SYSTEM_NAME}.env
  186. fi
  187. # --- get hash of config & exec for currently installed k3s ---
  188. PRE_INSTALL_HASHES=`get_installed_hashes`
  189. # --- if bin directory is read only skip download ---
  190. if [ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = "true" ]; then
  191. INSTALL_K3S_SKIP_DOWNLOAD=true
  192. fi
  193. }
  194. # --- check if skip download environment variable set ---
  195. can_skip_download() {
  196. if [ "${INSTALL_K3S_SKIP_DOWNLOAD}" != "true" ]; then
  197. return 1
  198. fi
  199. }
  200. # --- verify an executabe k3s binary is installed ---
  201. verify_k3s_is_executable() {
  202. if [ ! -x ${BIN_DIR}/k3s ]; then
  203. fatal "Executable k3s binary not found at ${BIN_DIR}/k3s"
  204. fi
  205. }
  206. # --- set arch and suffix, fatal if architecture not supported ---
  207. setup_verify_arch() {
  208. if [ -z "$ARCH" ]; then
  209. ARCH=`uname -m`
  210. fi
  211. case $ARCH in
  212. amd64)
  213. ARCH=amd64
  214. SUFFIX=
  215. ;;
  216. x86_64)
  217. ARCH=amd64
  218. SUFFIX=
  219. ;;
  220. arm64)
  221. ARCH=arm64
  222. SUFFIX=-${ARCH}
  223. ;;
  224. aarch64)
  225. ARCH=arm64
  226. SUFFIX=-${ARCH}
  227. ;;
  228. arm*)
  229. ARCH=arm
  230. SUFFIX=-${ARCH}hf
  231. ;;
  232. *)
  233. fatal "Unsupported architecture $ARCH"
  234. esac
  235. }
  236. # --- fatal if no curl ---
  237. verify_curl() {
  238. if [ -z `which curl || true` ]; then
  239. fatal "Can not find curl for downloading files"
  240. fi
  241. }
  242. # --- create tempory directory and cleanup when done ---
  243. setup_tmp() {
  244. TMP_DIR=`mktemp -d -t k3s-install.XXXXXXXXXX`
  245. TMP_HASH=${TMP_DIR}/k3s.hash
  246. TMP_BIN=${TMP_DIR}/k3s.bin
  247. cleanup() {
  248. code=$?
  249. set +e
  250. trap - EXIT
  251. rm -rf ${TMP_DIR}
  252. exit $code
  253. }
  254. trap cleanup INT EXIT
  255. }
  256. # --- use desired k3s version if defined or find latest ---
  257. get_release_version() {
  258. if [ -n "${INSTALL_K3S_VERSION}" ]; then
  259. VERSION_K3S="${INSTALL_K3S_VERSION}"
  260. else
  261. info "Finding latest release"
  262. VERSION_K3S=`curl -w "%{url_effective}" -I -L -s -S ${GITHUB_URL}/latest -o /dev/null | sed -e 's|.*/||'`
  263. fi
  264. info "Using ${VERSION_K3S} as release"
  265. }
  266. # --- download hash from github url ---
  267. download_hash() {
  268. HASH_URL=${GITHUB_URL}/download/${VERSION_K3S}/sha256sum-${ARCH}.txt
  269. info "Downloading hash ${HASH_URL}"
  270. curl -o ${TMP_HASH} -sfL ${HASH_URL} || fatal "Hash download failed"
  271. HASH_EXPECTED=`grep " k3s${SUFFIX}$" ${TMP_HASH} | awk '{print $1}'`
  272. }
  273. # --- check hash against installed version ---
  274. installed_hash_matches() {
  275. if [ -x ${BIN_DIR}/k3s ]; then
  276. HASH_INSTALLED=`sha256sum ${BIN_DIR}/k3s | awk '{print $1}'`
  277. if [ "${HASH_EXPECTED}" = "${HASH_INSTALLED}" ]; then
  278. return
  279. fi
  280. fi
  281. return 1
  282. }
  283. # --- download binary from github url ---
  284. download_binary() {
  285. BIN_URL=${GITHUB_URL}/download/${VERSION_K3S}/k3s${SUFFIX}
  286. info "Downloading binary ${BIN_URL}"
  287. curl -o ${TMP_BIN} -sfL ${BIN_URL} || fatal "Binary download failed"
  288. }
  289. # --- verify downloaded binary hash ---
  290. verify_binary() {
  291. info "Verifying binary download"
  292. HASH_BIN=`sha256sum ${TMP_BIN} | awk '{print $1}'`
  293. if [ "${HASH_EXPECTED}" != "${HASH_BIN}" ]; then
  294. fatal "Download sha256 does not match ${HASH_EXPECTED}, got ${HASH_BIN}"
  295. fi
  296. }
  297. # --- setup permissions and move binary to system directory ---
  298. setup_binary() {
  299. chmod 755 ${TMP_BIN}
  300. info "Installing k3s to ${BIN_DIR}/k3s"
  301. $SUDO chown root:root ${TMP_BIN}
  302. $SUDO mv -f ${TMP_BIN} ${BIN_DIR}/k3s
  303. if command -v getenforce > /dev/null 2>&1; then
  304. if [ "Disabled" != `getenforce` ]; then
  305. info "SeLinux is enabled, setting permissions"
  306. if ! $SUDO semanage fcontext -l | grep "${BIN_DIR}/k3s" > /dev/null 2>&1; then
  307. $SUDO semanage fcontext -a -t bin_t "${BIN_DIR}/k3s"
  308. fi
  309. $SUDO restorecon -v ${BIN_DIR}/k3s > /dev/null
  310. fi
  311. fi
  312. }
  313. # --- download and verify k3s ---
  314. download_and_verify() {
  315. if can_skip_download; then
  316. info "Skipping k3s download and verify"
  317. verify_k3s_is_executable
  318. return
  319. fi
  320. setup_verify_arch
  321. verify_curl
  322. setup_tmp
  323. get_release_version
  324. download_hash
  325. if installed_hash_matches; then
  326. info "Skipping binary downloaded, installed k3s matches hash"
  327. return
  328. fi
  329. download_binary
  330. verify_binary
  331. setup_binary
  332. }
  333. # --- add additional utility links ---
  334. create_symlinks() {
  335. [ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = "true" ] && return
  336. if [ ! -e ${BIN_DIR}/kubectl ]; then
  337. info "Creating ${BIN_DIR}/kubectl symlink to k3s"
  338. $SUDO ln -s k3s ${BIN_DIR}/kubectl
  339. fi
  340. if [ ! -e ${BIN_DIR}/crictl ]; then
  341. info "Creating ${BIN_DIR}/crictl symlink to k3s"
  342. $SUDO ln -s k3s ${BIN_DIR}/crictl
  343. fi
  344. }
  345. # --- create killall script ---
  346. create_killall() {
  347. [ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = "true" ] && return
  348. info "Creating killall script ${BIN_DIR}/${KILLALL_K3S_SH}"
  349. $SUDO tee ${BIN_DIR}/${KILLALL_K3S_SH} >/dev/null << \EOF
  350. #!/bin/sh
  351. set -x
  352. [ `id -u` = 0 ] || exec sudo $0 $@
  353. for bin in /var/lib/rancher/k3s/data/**/bin/; do
  354. [ -d $bin ] && export PATH=$bin:$PATH
  355. done
  356. for service in /etc/systemd/system/k3s*.service; do
  357. [ -s $service ] && systemctl stop $(basename $service)
  358. done
  359. for service in /etc/init.d/k3s*; do
  360. [ -x $service ] && $service stop
  361. done
  362. pstree() {
  363. for pid in $@; do
  364. echo $pid
  365. pstree $(ps -o ppid= -o pid= | awk "\$1==$pid {print \$2}")
  366. done
  367. }
  368. killtree() {
  369. [ $# -ne 0 ] && kill $(set +x; pstree $@; set -x)
  370. }
  371. killtree $(lsof | sed -e 's/^[^0-9]*//g; s/ */\t/g' | grep -w 'k3s/data/[^/]*/bin/containerd-shim' | cut -f1 | sort -n -u)
  372. do_unmount() {
  373. MOUNTS=`cat /proc/self/mounts | awk '{print $2}' | grep "^$1" | sort -r`
  374. if [ -n "${MOUNTS}" ]; then
  375. umount ${MOUNTS}
  376. fi
  377. }
  378. do_unmount '/run/k3s'
  379. do_unmount '/var/lib/rancher/k3s'
  380. nets=$(ip link show | grep 'master cni0' | awk -F': ' '{print $2}' | sed -e 's|@.*||')
  381. for iface in $nets; do
  382. ip link delete $iface;
  383. done
  384. ip link delete cni0
  385. ip link delete flannel.1
  386. rm -rf /var/lib/cni/
  387. EOF
  388. $SUDO chmod 755 ${BIN_DIR}/${KILLALL_K3S_SH}
  389. $SUDO chown root:root ${BIN_DIR}/${KILLALL_K3S_SH}
  390. }
  391. # --- create uninstall script ---
  392. create_uninstall() {
  393. [ "${INSTALL_K3S_BIN_DIR_READ_ONLY}" = "true" ] && return
  394. info "Creating uninstall script ${BIN_DIR}/${UNINSTALL_K3S_SH}"
  395. $SUDO tee ${BIN_DIR}/${UNINSTALL_K3S_SH} >/dev/null << EOF
  396. #!/bin/sh
  397. set -x
  398. [ \`id -u\` = 0 ] || exec sudo \$0 \$@
  399. ${BIN_DIR}/${KILLALL_K3S_SH}
  400. if which systemctl; then
  401. systemctl disable ${SYSTEM_NAME}
  402. systemctl reset-failed ${SYSTEM_NAME}
  403. systemctl daemon-reload
  404. fi
  405. if which rc-update; then
  406. rc-update delete ${SYSTEM_NAME} default
  407. fi
  408. rm -f ${FILE_K3S_SERVICE}
  409. rm -f ${FILE_K3S_ENV}
  410. remove_uninstall() {
  411. rm -f ${BIN_DIR}/${UNINSTALL_K3S_SH}
  412. }
  413. trap remove_uninstall EXIT
  414. if (ls ${SYSTEMD_DIR}/k3s*.service || ls /etc/init.d/k3s*) >/dev/null 2>&1; then
  415. set +x; echo "Additional k3s services installed, skipping uninstall of k3s"; set -x
  416. exit
  417. fi
  418. if [ -L ${BIN_DIR}/kubectl ]; then
  419. rm -f ${BIN_DIR}/kubectl
  420. fi
  421. if [ -L ${BIN_DIR}/crictl ]; then
  422. rm -f ${BIN_DIR}/crictl
  423. fi
  424. rm -rf /etc/rancher/k3s
  425. rm -rf /var/lib/rancher/k3s
  426. rm -f ${BIN_DIR}/k3s
  427. rm -f ${BIN_DIR}/${KILLALL_K3S_SH}
  428. EOF
  429. $SUDO chmod 755 ${BIN_DIR}/${UNINSTALL_K3S_SH}
  430. $SUDO chown root:root ${BIN_DIR}/${UNINSTALL_K3S_SH}
  431. }
  432. # --- disable current service if loaded --
  433. systemd_disable() {
  434. $SUDO rm -f /etc/systemd/system/${SERVICE_K3S} || true
  435. $SUDO rm -f /etc/systemd/system/${SERVICE_K3S}.env || true
  436. $SUDO systemctl disable ${SYSTEM_NAME} >/dev/null 2>&1 || true
  437. }
  438. # --- capture current env and create file containing k3s_ variables ---
  439. create_env_file() {
  440. info "env: Creating environment file ${FILE_K3S_ENV}"
  441. UMASK=`umask`
  442. umask 0377
  443. env | grep '^K3S_' | $SUDO tee ${FILE_K3S_ENV} >/dev/null
  444. umask $UMASK
  445. }
  446. # --- write systemd service file ---
  447. create_systemd_service_file() {
  448. info "systemd: Creating service file ${FILE_K3S_SERVICE}"
  449. $SUDO tee ${FILE_K3S_SERVICE} >/dev/null << EOF
  450. [Unit]
  451. Description=Lightweight Kubernetes
  452. Documentation=https://k3s.io
  453. After=network-online.target
  454. [Service]
  455. Type=${SYSTEMD_TYPE}
  456. EnvironmentFile=${FILE_K3S_ENV}
  457. ExecStartPre=-/sbin/modprobe br_netfilter
  458. ExecStartPre=-/sbin/modprobe overlay
  459. ExecStart=${BIN_DIR}/k3s \\
  460. ${CMD_K3S_EXEC}
  461. KillMode=process
  462. Delegate=yes
  463. LimitNOFILE=infinity
  464. LimitNPROC=infinity
  465. LimitCORE=infinity
  466. TasksMax=infinity
  467. TimeoutStartSec=0
  468. Restart=always
  469. [Install]
  470. WantedBy=multi-user.target
  471. EOF
  472. }
  473. # --- write openrc service file ---
  474. create_openrc_service_file() {
  475. LOG_FILE=/var/log/${SYSTEM_NAME}.log
  476. info "openrc: Creating service file ${FILE_K3S_SERVICE}"
  477. $SUDO tee ${FILE_K3S_SERVICE} >/dev/null << EOF
  478. #!/sbin/openrc-run
  479. depend() {
  480. after net-online
  481. need net
  482. }
  483. start_pre() {
  484. rm -f /tmp/k3s.*
  485. }
  486. supervisor=supervise-daemon
  487. name="${SYSTEM_NAME}"
  488. command="${BIN_DIR}/k3s"
  489. command_args="$(escape_dq "${CMD_K3S_EXEC}")
  490. >>${LOG_FILE} 2>&1"
  491. pidfile="/var/run/${SYSTEM_NAME}.pid"
  492. respawn_delay=5
  493. set -o allexport
  494. if [ -f /etc/environment ]; then source /etc/environment; fi
  495. if [ -f ${FILE_K3S_ENV} ]; then source ${FILE_K3S_ENV}; fi
  496. set +o allexport
  497. EOF
  498. $SUDO chmod 0755 ${FILE_K3S_SERVICE}
  499. $SUDO tee /etc/logrotate.d/${SYSTEM_NAME} >/dev/null << EOF
  500. ${LOG_FILE} {
  501. missingok
  502. notifempty
  503. copytruncate
  504. }
  505. EOF
  506. }
  507. # --- write systemd or openrc service file ---
  508. create_service_file() {
  509. [ "${HAS_SYSTEMD}" = "true" ] && create_systemd_service_file
  510. [ "${HAS_OPENRC}" = "true" ] && create_openrc_service_file
  511. return 0
  512. }
  513. # --- get hashes of the current k3s bin and service files
  514. get_installed_hashes() {
  515. $SUDO sha256sum ${BIN_DIR}/k3s ${FILE_K3S_SERVICE} ${FILE_K3S_ENV} 2>&1 || true
  516. }
  517. # --- enable and start systemd service ---
  518. systemd_enable() {
  519. info "systemd: Enabling ${SYSTEM_NAME} unit"
  520. $SUDO systemctl enable ${FILE_K3S_SERVICE} >/dev/null
  521. $SUDO systemctl daemon-reload >/dev/null
  522. }
  523. systemd_start() {
  524. info "systemd: Starting ${SYSTEM_NAME}"
  525. $SUDO systemctl restart ${SYSTEM_NAME}
  526. }
  527. # --- enable and start openrc service ---
  528. openrc_enable() {
  529. info "openrc: Enabling ${SYSTEM_NAME} service for default runlevel"
  530. $SUDO rc-update add ${SYSTEM_NAME} default >/dev/null
  531. }
  532. openrc_start() {
  533. info "openrc: Starting ${SYSTEM_NAME}"
  534. $SUDO ${FILE_K3S_SERVICE} restart
  535. }
  536. # --- startup systemd or openrc service ---
  537. service_enable_and_start() {
  538. [ "${HAS_SYSTEMD}" = "true" ] && systemd_enable
  539. [ "${HAS_OPENRC}" = "true" ] && openrc_enable
  540. [ "${INSTALL_K3S_SKIP_START}" = "true" ] && return
  541. POST_INSTALL_HASHES=`get_installed_hashes`
  542. if [ "${PRE_INSTALL_HASHES}" = "${POST_INSTALL_HASHES}" ]; then
  543. info "No change detected so skipping service start"
  544. return
  545. fi
  546. [ "${HAS_SYSTEMD}" = "true" ] && systemd_start
  547. [ "${HAS_OPENRC}" = "true" ] && openrc_start
  548. return 0
  549. }
  550. # --- re-evaluate args to include env command ---
  551. eval set -- $(escape "${INSTALL_K3S_EXEC}") $(quote "$@")
  552. # --- run the install process --
  553. {
  554. verify_system
  555. setup_env "$@"
  556. download_and_verify
  557. create_symlinks
  558. create_killall
  559. create_uninstall
  560. systemd_disable
  561. create_env_file
  562. create_service_file
  563. service_enable_and_start
  564. }