rsyncd.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env bash
  2. # Copyright 2016 The Kubernetes Authors.
  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. # This script will set up and run rsyncd to allow data to move into and out of
  16. # our dockerized build system. This is used for syncing sources and changes of
  17. # sources into the docker-build-container. It is also used to transfer built binaries
  18. # and generated files back out.
  19. #
  20. # When run as root (rare) it'll preserve the file ids as sent from the client.
  21. # Usually it'll be run as non-dockerized UID/GID and end up translating all file
  22. # ownership to that.
  23. set -o errexit
  24. set -o nounset
  25. set -o pipefail
  26. # The directory that gets sync'd
  27. VOLUME=${HOME}
  28. # Assume that this is running in Docker on a bridge. Allow connections from
  29. # anything on the local subnet.
  30. ALLOW=$(ip route | awk '/^default via/ { reg = "^[0-9./]+ dev "$5 } ; $0 ~ reg { print $1 }')
  31. CONFDIR="/tmp/rsync.k8s"
  32. PIDFILE="${CONFDIR}/rsyncd.pid"
  33. CONFFILE="${CONFDIR}/rsyncd.conf"
  34. SECRETS="${CONFDIR}/rsyncd.secrets"
  35. mkdir -p "${CONFDIR}"
  36. if [[ -f "${PIDFILE}" ]]; then
  37. PID=$(cat "${PIDFILE}")
  38. echo "Cleaning up old PID file: ${PIDFILE}"
  39. kill "${PID}" &> /dev/null || true
  40. rm "${PIDFILE}"
  41. fi
  42. PASSWORD=$(</rsyncd.password)
  43. cat <<EOF >"${SECRETS}"
  44. k8s:${PASSWORD}
  45. EOF
  46. chmod go= "${SECRETS}"
  47. USER_CONFIG=
  48. if [[ "$(id -u)" == "0" ]]; then
  49. USER_CONFIG=" uid = 0"$'\n'" gid = 0"
  50. fi
  51. cat <<EOF >"${CONFFILE}"
  52. pid file = ${PIDFILE}
  53. use chroot = no
  54. log file = /dev/stdout
  55. reverse lookup = no
  56. munge symlinks = no
  57. port = 8730
  58. [k8s]
  59. numeric ids = true
  60. $USER_CONFIG
  61. hosts deny = *
  62. hosts allow = ${ALLOW} ${ALLOW_HOST-}
  63. auth users = k8s
  64. secrets file = ${SECRETS}
  65. read only = false
  66. path = ${VOLUME}
  67. filter = - /.make/ - /_tmp/
  68. EOF
  69. exec /usr/bin/rsync --no-detach --daemon --config="${CONFFILE}" "$@"