util.sh 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. #!/bin/bash
  2. # Copyright 2014 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. kratos::util::sortable_date() {
  16. date "+%Y%m%d-%H%M%S"
  17. }
  18. kratos::util::wait_for_url() {
  19. local url=$1
  20. local prefix=${2:-}
  21. local wait=${3:-1}
  22. local times=${4:-30}
  23. which curl >/dev/null || {
  24. kratos::log::usage "curl must be installed"
  25. exit 1
  26. }
  27. local i
  28. for i in $(seq 1 "$times"); do
  29. local out
  30. if out=$(curl --max-time 1 -gkfs "$url" 2>/dev/null); then
  31. kratos::log::status "On try ${i}, ${prefix}: ${out}"
  32. return 0
  33. fi
  34. sleep "${wait}"
  35. done
  36. kratos::log::error "Timed out waiting for ${prefix} to answer at ${url}; tried ${times} waiting ${wait} between each"
  37. return 1
  38. }
  39. # Example: kratos::util::trap_add 'echo "in trap DEBUG"' DEBUG
  40. # See: http://stackoverflow.com/questions/3338030/multiple-bash-traps-for-the-same-signal
  41. kratos::util::trap_add() {
  42. local trap_add_cmd
  43. trap_add_cmd=$1
  44. shift
  45. for trap_add_name in "$@"; do
  46. local existing_cmd
  47. local new_cmd
  48. # Grab the currently defined trap commands for this trap
  49. existing_cmd=`trap -p "${trap_add_name}" | awk -F"'" '{print $2}'`
  50. if [[ -z "${existing_cmd}" ]]; then
  51. new_cmd="${trap_add_cmd}"
  52. else
  53. new_cmd="${trap_add_cmd};${existing_cmd}"
  54. fi
  55. # Assign the test
  56. trap "${new_cmd}" "${trap_add_name}"
  57. done
  58. }
  59. # Opposite of kratos::util::ensure-temp-dir()
  60. kratos::util::cleanup-temp-dir() {
  61. rm -rf "${KRATOS_TEMP}"
  62. }
  63. # Create a temp dir that'll be deleted at the end of this bash session.
  64. #
  65. # Vars set:
  66. # KRATOS_TEMP
  67. kratos::util::ensure-temp-dir() {
  68. if [[ -z ${KRATOS_TEMP-} ]]; then
  69. KRATOS_TEMP=$(mktemp -d 2>/dev/null || mktemp -d -t kratosrnetes.XXXXXX)
  70. kratos::util::trap_add kratos::util::cleanup-temp-dir EXIT
  71. fi
  72. }
  73. # This figures out the host platform without relying on golang. We need this as
  74. # we don't want a golang install to be a prerequisite to building yet we need
  75. # this info to figure out where the final binaries are placed.
  76. kratos::util::host_platform() {
  77. local host_os
  78. local host_arch
  79. case "$(uname -s)" in
  80. Darwin)
  81. host_os=darwin
  82. ;;
  83. Linux)
  84. host_os=linux
  85. ;;
  86. *)
  87. kratos::log::error "Unsupported host OS. Must be Linux or Mac OS X."
  88. exit 1
  89. ;;
  90. esac
  91. case "$(uname -m)" in
  92. x86_64*)
  93. host_arch=amd64
  94. ;;
  95. i?86_64*)
  96. host_arch=amd64
  97. ;;
  98. amd64*)
  99. host_arch=amd64
  100. ;;
  101. aarch64*)
  102. host_arch=arm64
  103. ;;
  104. arm64*)
  105. host_arch=arm64
  106. ;;
  107. arm*)
  108. host_arch=arm
  109. ;;
  110. i?86*)
  111. host_arch=x86
  112. ;;
  113. s390x*)
  114. host_arch=s390x
  115. ;;
  116. ppc64le*)
  117. host_arch=ppc64le
  118. ;;
  119. *)
  120. kratos::log::error "Unsupported host arch. Must be x86_64, 386, arm, arm64, s390x or ppc64le."
  121. exit 1
  122. ;;
  123. esac
  124. echo "${host_os}/${host_arch}"
  125. }
  126. kratos::util::find-binary-for-platform() {
  127. local -r lookfor="$1"
  128. local -r platform="$2"
  129. local locations=(
  130. "${KRATOS_ROOT}/_output/bin/${lookfor}"
  131. "${KRATOS_ROOT}/_output/dockerized/bin/${platform}/${lookfor}"
  132. "${KRATOS_ROOT}/_output/local/bin/${platform}/${lookfor}"
  133. "${KRATOS_ROOT}/platforms/${platform}/${lookfor}"
  134. )
  135. # Also search for binary in bazel build tree.
  136. # The bazel go rules place binaries in subtrees like
  137. # "bazel-bin/source/path/linux_amd64_pure_stripped/binaryname", so make sure
  138. # the platform name is matched in the path.
  139. locations+=($(find "${KRATOS_ROOT}/bazel-bin/" -type f -executable \
  140. -path "*/${platform/\//_}*/${lookfor}" 2>/dev/null || true) )
  141. # List most recently-updated location.
  142. local -r bin=$( (ls -t "${locations[@]}" 2>/dev/null || true) | head -1 )
  143. echo -n "${bin}"
  144. }
  145. kratos::util::find-binary() {
  146. kratos::util::find-binary-for-platform "$1" "$(kratos::util::host_platform)"
  147. }
  148. # Run all known doc generators (today gendocs and genman for kratosctl)
  149. # $1 is the directory to put those generated documents
  150. kratos::util::gen-docs() {
  151. local dest="$1"
  152. # Find binary
  153. gendocs=$(kratos::util::find-binary "gendocs")
  154. genkratosdocs=$(kratos::util::find-binary "genkratosdocs")
  155. genman=$(kratos::util::find-binary "genman")
  156. genyaml=$(kratos::util::find-binary "genyaml")
  157. genfeddocs=$(kratos::util::find-binary "genfeddocs")
  158. mkdir -p "${dest}/docs/user-guide/kratosctl/"
  159. "${gendocs}" "${dest}/docs/user-guide/kratosctl/"
  160. mkdir -p "${dest}/docs/admin/"
  161. "${genkratosdocs}" "${dest}/docs/admin/" "kratos-apiserver"
  162. "${genkratosdocs}" "${dest}/docs/admin/" "kratos-controller-manager"
  163. "${genkratosdocs}" "${dest}/docs/admin/" "cloud-controller-manager"
  164. "${genkratosdocs}" "${dest}/docs/admin/" "kratos-proxy"
  165. "${genkratosdocs}" "${dest}/docs/admin/" "kratos-scheduler"
  166. "${genkratosdocs}" "${dest}/docs/admin/" "kratoslet"
  167. "${genkratosdocs}" "${dest}/docs/admin/" "kratosadm"
  168. mkdir -p "${dest}/docs/man/man1/"
  169. "${genman}" "${dest}/docs/man/man1/" "kratos-apiserver"
  170. "${genman}" "${dest}/docs/man/man1/" "kratos-controller-manager"
  171. "${genman}" "${dest}/docs/man/man1/" "cloud-controller-manager"
  172. "${genman}" "${dest}/docs/man/man1/" "kratos-proxy"
  173. "${genman}" "${dest}/docs/man/man1/" "kratos-scheduler"
  174. "${genman}" "${dest}/docs/man/man1/" "kratoslet"
  175. "${genman}" "${dest}/docs/man/man1/" "kratosctl"
  176. "${genman}" "${dest}/docs/man/man1/" "kratosadm"
  177. mkdir -p "${dest}/docs/yaml/kratosctl/"
  178. "${genyaml}" "${dest}/docs/yaml/kratosctl/"
  179. # create the list of generated files
  180. pushd "${dest}" > /dev/null
  181. touch docs/.generated_docs
  182. find . -type f | cut -sd / -f 2- | LC_ALL=C sort > docs/.generated_docs
  183. popd > /dev/null
  184. }
  185. # Puts a placeholder for every generated doc. This makes the link checker work.
  186. kratos::util::set-placeholder-gen-docs() {
  187. local list_file="${KRATOS_ROOT}/docs/.generated_docs"
  188. if [[ -e "${list_file}" ]]; then
  189. # remove all of the old docs; we don't want to check them in.
  190. while read file; do
  191. if [[ "${list_file}" != "${KRATOS_ROOT}/${file}" ]]; then
  192. cp "${KRATOS_ROOT}/build/autogenerated_placeholder.txt" "${KRATOS_ROOT}/${file}"
  193. fi
  194. done <"${list_file}"
  195. # The docs/.generated_docs file lists itself, so we don't need to explicitly
  196. # delete it.
  197. fi
  198. }
  199. # Removes previously generated docs-- we don't want to check them in. $KRATOS_ROOT
  200. # must be set.
  201. kratos::util::remove-gen-docs() {
  202. if [ -e "${KRATOS_ROOT}/docs/.generated_docs" ]; then
  203. # remove all of the old docs; we don't want to check them in.
  204. while read file; do
  205. rm "${KRATOS_ROOT}/${file}" 2>/dev/null || true
  206. done <"${KRATOS_ROOT}/docs/.generated_docs"
  207. # The docs/.generated_docs file lists itself, so we don't need to explicitly
  208. # delete it.
  209. fi
  210. }
  211. # Takes a group/version and returns the path to its location on disk, sans
  212. # "pkg". E.g.:
  213. # * default behavior: extensions/v1beta1 -> apis/extensions/v1beta1
  214. # * default behavior for only a group: experimental -> apis/experimental
  215. # * Special handling for empty group: v1 -> api/v1, unversioned -> api/unversioned
  216. # * Special handling for groups suffixed with ".k8s.io": foo.k8s.io/v1 -> apis/foo/v1
  217. # * Very special handling for when both group and version are "": / -> api
  218. kratos::util::group-version-to-pkg-path() {
  219. staging_apis=(
  220. $(
  221. cd "${KRATOS_ROOT}/staging/src/k8s.io/api" &&
  222. find . -name types.go -exec dirname {} \; | sed "s|\./||g" | sort
  223. ))
  224. local group_version="$1"
  225. if [[ " ${staging_apis[@]} " =~ " ${group_version/.*k8s.io/} " ]]; then
  226. echo "vendor/k8s.io/api/${group_version/.*k8s.io/}"
  227. return
  228. fi
  229. # "v1" is the API GroupVersion
  230. if [[ "${group_version}" == "v1" ]]; then
  231. echo "vendor/k8s.io/api/core/v1"
  232. return
  233. fi
  234. # Special cases first.
  235. # TODO(lavalamp): Simplify this by moving pkg/api/v1 and splitting pkg/api,
  236. # moving the results to pkg/apis/api.
  237. case "${group_version}" in
  238. # both group and version are "", this occurs when we generate deep copies for internal objects of the legacy v1 API.
  239. __internal)
  240. echo "pkg/apis/core"
  241. ;;
  242. meta/v1)
  243. echo "vendor/k8s.io/apimachinery/pkg/apis/meta/v1"
  244. ;;
  245. meta/v1beta1)
  246. echo "vendor/k8s.io/apimachinery/pkg/apis/meta/v1beta1"
  247. ;;
  248. unversioned)
  249. echo "pkg/api/unversioned"
  250. ;;
  251. *.k8s.io)
  252. echo "pkg/apis/${group_version%.*k8s.io}"
  253. ;;
  254. *.k8s.io/*)
  255. echo "pkg/apis/${group_version/.*k8s.io/}"
  256. ;;
  257. *)
  258. echo "pkg/apis/${group_version%__internal}"
  259. ;;
  260. esac
  261. }
  262. # Takes a group/version and returns the swagger-spec file name.
  263. # default behavior: extensions/v1beta1 -> extensions_v1beta1
  264. # special case for v1: v1 -> v1
  265. kratos::util::gv-to-swagger-name() {
  266. local group_version="$1"
  267. case "${group_version}" in
  268. v1)
  269. echo "v1"
  270. ;;
  271. *)
  272. echo "${group_version%/*}_${group_version#*/}"
  273. ;;
  274. esac
  275. }
  276. # Fetches swagger spec from apiserver.
  277. # Assumed vars:
  278. # SWAGGER_API_PATH: Base path for swaggerapi on apiserver. Ex:
  279. # http://localhost:8080/swaggerapi.
  280. # SWAGGER_ROOT_DIR: Root dir where we want to to save the fetched spec.
  281. # VERSIONS: Array of group versions to include in swagger spec.
  282. kratos::util::fetch-swagger-spec() {
  283. for ver in ${VERSIONS}; do
  284. if [[ " ${KRATOS_NONSERVER_GROUP_VERSIONS} " == *" ${ver} "* ]]; then
  285. continue
  286. fi
  287. # fetch the swagger spec for each group version.
  288. if [[ ${ver} == "v1" ]]; then
  289. SUBPATH="api"
  290. else
  291. SUBPATH="apis"
  292. fi
  293. SUBPATH="${SUBPATH}/${ver}"
  294. SWAGGER_JSON_NAME="$(kratos::util::gv-to-swagger-name ${ver}).json"
  295. curl -w "\n" -fs "${SWAGGER_API_PATH}${SUBPATH}" > "${SWAGGER_ROOT_DIR}/${SWAGGER_JSON_NAME}"
  296. # fetch the swagger spec for the discovery mechanism at group level.
  297. if [[ ${ver} == "v1" ]]; then
  298. continue
  299. fi
  300. SUBPATH="apis/"${ver%/*}
  301. SWAGGER_JSON_NAME="${ver%/*}.json"
  302. curl -w "\n" -fs "${SWAGGER_API_PATH}${SUBPATH}" > "${SWAGGER_ROOT_DIR}/${SWAGGER_JSON_NAME}"
  303. done
  304. # fetch swagger specs for other discovery mechanism.
  305. curl -w "\n" -fs "${SWAGGER_API_PATH}" > "${SWAGGER_ROOT_DIR}/resourceListing.json"
  306. curl -w "\n" -fs "${SWAGGER_API_PATH}version" > "${SWAGGER_ROOT_DIR}/version.json"
  307. curl -w "\n" -fs "${SWAGGER_API_PATH}api" > "${SWAGGER_ROOT_DIR}/api.json"
  308. curl -w "\n" -fs "${SWAGGER_API_PATH}apis" > "${SWAGGER_ROOT_DIR}/apis.json"
  309. curl -w "\n" -fs "${SWAGGER_API_PATH}logs" > "${SWAGGER_ROOT_DIR}/logs.json"
  310. }
  311. # Returns the name of the upstream remote repository name for the local git
  312. # repo, e.g. "upstream" or "origin".
  313. kratos::util::git_upstream_remote_name() {
  314. git remote -v | grep fetch |\
  315. grep -E 'github.com[/:]kratosrnetes/kratosrnetes|k8s.io/kratosrnetes' |\
  316. head -n 1 | awk '{print $1}'
  317. }
  318. # Ensures the current directory is a git tree for doing things like restoring or
  319. # validating godeps
  320. kratos::util::create-fake-git-tree() {
  321. local -r target_dir=${1:-$(pwd)}
  322. pushd "${target_dir}" >/dev/null
  323. git init >/dev/null
  324. git config --local user.email "nobody@k8s.io"
  325. git config --local user.name "$0"
  326. git add . >/dev/null
  327. git commit -q -m "Snapshot" >/dev/null
  328. if (( ${KRATOS_VERBOSE:-5} >= 6 )); then
  329. kratos::log::status "${target_dir} is now a git tree."
  330. fi
  331. popd >/dev/null
  332. }
  333. # Checks whether godep restore was run in the current GOPATH, i.e. that all referenced repos exist
  334. # and are checked out to the referenced rev.
  335. kratos::util::godep_restored() {
  336. local -r godeps_json=${1:-Godeps/Godeps.json}
  337. local -r gopath=${2:-${GOPATH%:*}}
  338. if ! which jq &>/dev/null; then
  339. echo "jq not found. Please install." 1>&2
  340. return 1
  341. fi
  342. local root
  343. local old_rev=""
  344. while read path rev; do
  345. rev=$(echo "${rev}" | sed "s/['\"]//g") # remove quotes which are around revs sometimes
  346. if [[ "${rev}" == "${old_rev}" ]] && [[ "${path}" == "${root}"* ]]; then
  347. # avoid checking the same git/hg root again
  348. continue
  349. fi
  350. root="${path}"
  351. while [ "${root}" != "." -a ! -d "${gopath}/src/${root}/.git" -a ! -d "${gopath}/src/${root}/.hg" ]; do
  352. root=$(dirname "${root}")
  353. done
  354. if [ "${root}" == "." ]; then
  355. echo "No checkout of ${path} found in GOPATH \"${gopath}\"." 1>&2
  356. return 1
  357. fi
  358. local head
  359. if [ -d "${gopath}/src/${root}/.git" ]; then
  360. head="$(cd "${gopath}/src/${root}" && git rev-parse HEAD)"
  361. else
  362. head="$(cd "${gopath}/src/${root}" && hg parent --template '{node}')"
  363. fi
  364. if [ "${head}" != "${rev}" ]; then
  365. echo "Unexpected HEAD '${head}' at ${gopath}/src/${root}, expected '${rev}'." 1>&2
  366. return 1
  367. fi
  368. old_rev="${rev}"
  369. done < <(jq '.Deps|.[]|.ImportPath + " " + .Rev' -r < "${godeps_json}")
  370. return 0
  371. }
  372. # Exits script if working directory is dirty. If it's run interactively in the terminal
  373. # the user can commit changes in a second terminal. This script will wait.
  374. kratos::util::ensure_clean_working_dir() {
  375. while ! git diff HEAD --exit-code &>/dev/null; do
  376. echo -e "\nUnexpected dirty working directory:\n"
  377. if tty -s; then
  378. git status -s
  379. else
  380. git diff -a # be more verbose in log files without tty
  381. exit 1
  382. fi | sed 's/^/ /'
  383. echo -e "\nCommit your changes in another terminal and then continue here by pressing enter."
  384. read
  385. done 1>&2
  386. }
  387. # Ensure that the given godep version is installed and in the path. Almost
  388. # nobody should use any version but the default.
  389. kratos::util::ensure_godep_version() {
  390. GODEP_VERSION=${1:-"v79"} # this version is known to work
  391. if [[ "$(godep version 2>/dev/null)" == *"godep ${GODEP_VERSION}"* ]]; then
  392. return
  393. fi
  394. kratos::log::status "Installing godep version ${GODEP_VERSION}"
  395. go install ./vendor/github.com/tools/godep/
  396. GP="$(echo $GOPATH | cut -f1 -d:)"
  397. hash -r # force bash to clear PATH cache
  398. PATH="${GP}/bin:${PATH}"
  399. if [[ "$(godep version 2>/dev/null)" != *"godep ${GODEP_VERSION}"* ]]; then
  400. kratos::log::error "Expected godep ${GODEP_VERSION}, got $(godep version)"
  401. return 1
  402. fi
  403. }
  404. # Ensure that none of the staging repos is checked out in the GOPATH because this
  405. # easily confused godep.
  406. kratos::util::ensure_no_staging_repos_in_gopath() {
  407. kratos::util::ensure_single_dir_gopath
  408. local error=0
  409. for repo_file in "${KRATOS_ROOT}"/staging/src/k8s.io/*; do
  410. if [[ ! -d "$repo_file" ]]; then
  411. # not a directory or there were no files
  412. continue;
  413. fi
  414. repo="$(basename "$repo_file")"
  415. if [ -e "${GOPATH}/src/k8s.io/${repo}" ]; then
  416. echo "k8s.io/${repo} exists in GOPATH. Remove before running godep-save.sh." 1>&2
  417. error=1
  418. fi
  419. done
  420. if [ "${error}" = "1" ]; then
  421. exit 1
  422. fi
  423. }
  424. # Installs the specified go package at a particular commit.
  425. kratos::util::go_install_from_commit() {
  426. local -r pkg=$1
  427. local -r commit=$2
  428. kratos::util::ensure-temp-dir
  429. mkdir -p "${KRATOS_TEMP}/go/src"
  430. GOPATH="${KRATOS_TEMP}/go" go get -d -u "${pkg}"
  431. (
  432. cd "${KRATOS_TEMP}/go/src/${pkg}"
  433. git checkout -q "${commit}"
  434. GOPATH="${KRATOS_TEMP}/go" go install "${pkg}"
  435. )
  436. PATH="${KRATOS_TEMP}/go/bin:${PATH}"
  437. hash -r # force bash to clear PATH cache
  438. }
  439. # Checks that the GOPATH is simple, i.e. consists only of one directory, not multiple.
  440. kratos::util::ensure_single_dir_gopath() {
  441. if [[ "${GOPATH}" == *:* ]]; then
  442. echo "GOPATH must consist of a single directory." 1>&2
  443. exit 1
  444. fi
  445. }
  446. # Checks whether there are any files matching pattern $2 changed between the
  447. # current branch and upstream branch named by $1.
  448. # Returns 1 (false) if there are no changes, 0 (true) if there are changes
  449. # detected.
  450. kratos::util::has_changes_against_upstream_branch() {
  451. local -r git_branch=$1
  452. local -r pattern=$2
  453. local -r not_pattern=${3:-totallyimpossiblepattern}
  454. local full_branch
  455. full_branch="$(kratos::util::git_upstream_remote_name)/${git_branch}"
  456. echo "Checking for '${pattern}' changes against '${full_branch}'"
  457. # make sure the branch is valid, otherwise the check will pass erroneously.
  458. if ! git describe "${full_branch}" >/dev/null; then
  459. # abort!
  460. exit 1
  461. fi
  462. # notice this uses ... to find the first shared ancestor
  463. if git diff --name-only "${full_branch}...HEAD" | grep -v -E "${not_pattern}" | grep "${pattern}" > /dev/null; then
  464. return 0
  465. fi
  466. # also check for pending changes
  467. if git status --porcelain | grep -v -E "${not_pattern}" | grep "${pattern}" > /dev/null; then
  468. echo "Detected '${pattern}' uncommitted changes."
  469. return 0
  470. fi
  471. echo "No '${pattern}' changes detected."
  472. return 1
  473. }
  474. kratos::util::download_file() {
  475. local -r url=$1
  476. local -r destination_file=$2
  477. rm ${destination_file} 2&> /dev/null || true
  478. for i in $(seq 5)
  479. do
  480. if ! curl -fsSL --retry 3 --keepalive-time 2 ${url} -o ${destination_file}; then
  481. echo "Downloading ${url} failed. $((5-i)) retries left."
  482. sleep 1
  483. else
  484. echo "Downloading ${url} succeed"
  485. return 0
  486. fi
  487. done
  488. return 1
  489. }
  490. # Test whether openssl is installed.
  491. # Sets:
  492. # OPENSSL_BIN: The path to the openssl binary to use
  493. function kratos::util::test_openssl_installed {
  494. openssl version >& /dev/null
  495. if [ "$?" != "0" ]; then
  496. echo "Failed to run openssl. Please ensure openssl is installed"
  497. exit 1
  498. fi
  499. OPENSSL_BIN=$(command -v openssl)
  500. }
  501. # creates a client CA, args are sudo, dest-dir, ca-id, purpose
  502. # purpose is dropped in after "key encipherment", you usually want
  503. # '"client auth"'
  504. # '"server auth"'
  505. # '"client auth","server auth"'
  506. function kratos::util::create_signing_certkey {
  507. local sudo=$1
  508. local dest_dir=$2
  509. local id=$3
  510. local purpose=$4
  511. # Create client ca
  512. ${sudo} /bin/bash -e <<EOF
  513. rm -f "${dest_dir}/${id}-ca.crt" "${dest_dir}/${id}-ca.key"
  514. ${OPENSSL_BIN} req -x509 -sha256 -new -nodes -days 365 -newkey rsa:2048 -keyout "${dest_dir}/${id}-ca.key" -out "${dest_dir}/${id}-ca.crt" -subj "/C=xx/ST=x/L=x/O=x/OU=x/CN=ca/emailAddress=x/"
  515. echo '{"signing":{"default":{"expiry":"43800h","usages":["signing","key encipherment",${purpose}]}}}' > "${dest_dir}/${id}-ca-config.json"
  516. EOF
  517. }
  518. # signs a client certificate: args are sudo, dest-dir, CA, filename (roughly), username, groups...
  519. function kratos::util::create_client_certkey {
  520. local sudo=$1
  521. local dest_dir=$2
  522. local ca=$3
  523. local id=$4
  524. local cn=${5:-$4}
  525. local groups=""
  526. local SEP=""
  527. shift 5
  528. while [ -n "${1:-}" ]; do
  529. groups+="${SEP}{\"O\":\"$1\"}"
  530. SEP=","
  531. shift 1
  532. done
  533. ${sudo} /bin/bash -e <<EOF
  534. cd ${dest_dir}
  535. echo '{"CN":"${cn}","names":[${groups}],"hosts":[""],"key":{"algo":"rsa","size":2048}}' | ${CFSSL_BIN} gencert -ca=${ca}.crt -ca-key=${ca}.key -config=${ca}-config.json - | ${CFSSLJSON_BIN} -bare client-${id}
  536. mv "client-${id}-key.pem" "client-${id}.key"
  537. mv "client-${id}.pem" "client-${id}.crt"
  538. rm -f "client-${id}.csr"
  539. EOF
  540. }
  541. # signs a serving certificate: args are sudo, dest-dir, ca, filename (roughly), subject, hosts...
  542. function kratos::util::create_serving_certkey {
  543. local sudo=$1
  544. local dest_dir=$2
  545. local ca=$3
  546. local id=$4
  547. local cn=${5:-$4}
  548. local hosts=""
  549. local SEP=""
  550. shift 5
  551. while [ -n "${1:-}" ]; do
  552. hosts+="${SEP}\"$1\""
  553. SEP=","
  554. shift 1
  555. done
  556. ${sudo} /bin/bash -e <<EOF
  557. cd ${dest_dir}
  558. echo '{"CN":"${cn}","hosts":[${hosts}],"key":{"algo":"rsa","size":2048}}' | ${CFSSL_BIN} gencert -ca=${ca}.crt -ca-key=${ca}.key -config=${ca}-config.json - | ${CFSSLJSON_BIN} -bare serving-${id}
  559. mv "serving-${id}-key.pem" "serving-${id}.key"
  560. mv "serving-${id}.pem" "serving-${id}.crt"
  561. rm -f "serving-${id}.csr"
  562. EOF
  563. }
  564. # creates a self-contained kratosconfig: args are sudo, dest-dir, ca file, host, port, client id, token(optional)
  565. function kratos::util::write_client_kratosconfig {
  566. local sudo=$1
  567. local dest_dir=$2
  568. local ca_file=$3
  569. local api_host=$4
  570. local api_port=$5
  571. local client_id=$6
  572. local token=${7:-}
  573. cat <<EOF | ${sudo} tee "${dest_dir}"/${client_id}.kratosconfig > /dev/null
  574. apiVersion: v1
  575. kind: Config
  576. clusters:
  577. - cluster:
  578. certificate-authority: ${ca_file}
  579. server: https://${api_host}:${api_port}/
  580. name: local-up-cluster
  581. users:
  582. - user:
  583. token: ${token}
  584. client-certificate: ${dest_dir}/client-${client_id}.crt
  585. client-key: ${dest_dir}/client-${client_id}.key
  586. name: local-up-cluster
  587. contexts:
  588. - context:
  589. cluster: local-up-cluster
  590. user: local-up-cluster
  591. name: local-up-cluster
  592. current-context: local-up-cluster
  593. EOF
  594. # flatten the kratosconfig files to make them self contained
  595. username=$(whoami)
  596. ${sudo} /bin/bash -e <<EOF
  597. $(kratos::util::find-binary kratosctl) --kratosconfig="${dest_dir}/${client_id}.kratosconfig" config view --minify --flatten > "/tmp/${client_id}.kratosconfig"
  598. mv -f "/tmp/${client_id}.kratosconfig" "${dest_dir}/${client_id}.kratosconfig"
  599. chown ${username} "${dest_dir}/${client_id}.kratosconfig"
  600. EOF
  601. }
  602. # Determines if docker can be run, failures may simply require that the user be added to the docker group.
  603. function kratos::util::ensure_docker_daemon_connectivity {
  604. DOCKER=(docker ${DOCKER_OPTS})
  605. if ! "${DOCKER[@]}" info > /dev/null 2>&1 ; then
  606. cat <<'EOF' >&2
  607. Can't connect to 'docker' daemon. please fix and retry.
  608. Possible causes:
  609. - Docker Daemon not started
  610. - Linux: confirm via your init system
  611. - macOS w/ docker-machine: run `docker-machine ls` and `docker-machine start <name>`
  612. - macOS w/ Docker for Mac: Check the menu bar and start the Docker application
  613. - DOCKER_HOST hasn't been set or is set incorrectly
  614. - Linux: domain socket is used, DOCKER_* should be unset. In Bash run `unset ${!DOCKER_*}`
  615. - macOS w/ docker-machine: run `eval "$(docker-machine env <name>)"`
  616. - macOS w/ Docker for Mac: domain socket is used, DOCKER_* should be unset. In Bash run `unset ${!DOCKER_*}`
  617. - Other things to check:
  618. - Linux: User isn't in 'docker' group. Add and relogin.
  619. - Something like 'sudo usermod -a -G docker ${USER}'
  620. - RHEL7 bug and workaround: https://bugzilla.redhat.com/show_bug.cgi?id=1119282#c8
  621. EOF
  622. return 1
  623. fi
  624. }
  625. # Wait for background jobs to finish. Return with
  626. # an error status if any of the jobs failed.
  627. kratos::util::wait-for-jobs() {
  628. local fail=0
  629. local job
  630. for job in $(jobs -p); do
  631. wait "${job}" || fail=$((fail + 1))
  632. done
  633. return ${fail}
  634. }
  635. # kratos::util::join <delim> <list...>
  636. # Concatenates the list elements with the delimiter passed as first parameter
  637. #
  638. # Ex: kratos::util::join , a b c
  639. # -> a,b,c
  640. function kratos::util::join {
  641. local IFS="$1"
  642. shift
  643. echo "$*"
  644. }
  645. # Downloads cfssl/cfssljson into $1 directory if they do not already exist in PATH
  646. #
  647. # Assumed vars:
  648. # $1 (cfssl directory) (optional)
  649. #
  650. # Sets:
  651. # CFSSL_BIN: The path of the installed cfssl binary
  652. # CFSSLJSON_BIN: The path of the installed cfssljson binary
  653. #
  654. function kratos::util::ensure-cfssl {
  655. if command -v cfssl &>/dev/null && command -v cfssljson &>/dev/null; then
  656. CFSSL_BIN=$(command -v cfssl)
  657. CFSSLJSON_BIN=$(command -v cfssljson)
  658. return 0
  659. fi
  660. # Create a temp dir for cfssl if no directory was given
  661. local cfssldir=${1:-}
  662. if [[ -z "${cfssldir}" ]]; then
  663. kratos::util::ensure-temp-dir
  664. cfssldir="${KRATOS_TEMP}/cfssl"
  665. fi
  666. mkdir -p "${cfssldir}"
  667. pushd "${cfssldir}" > /dev/null
  668. echo "Unable to successfully run 'cfssl' from $PATH; downloading instead..."
  669. kernel=$(uname -s)
  670. case "${kernel}" in
  671. Linux)
  672. curl --retry 10 -L -o cfssl https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
  673. curl --retry 10 -L -o cfssljson https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
  674. ;;
  675. Darwin)
  676. curl --retry 10 -L -o cfssl https://pkg.cfssl.org/R1.2/cfssl_darwin-amd64
  677. curl --retry 10 -L -o cfssljson https://pkg.cfssl.org/R1.2/cfssljson_darwin-amd64
  678. ;;
  679. *)
  680. echo "Unknown, unsupported platform: ${kernel}." >&2
  681. echo "Supported platforms: Linux, Darwin." >&2
  682. exit 2
  683. esac
  684. chmod +x cfssl || true
  685. chmod +x cfssljson || true
  686. CFSSL_BIN="${cfssldir}/cfssl"
  687. CFSSLJSON_BIN="${cfssldir}/cfssljson"
  688. if [[ ! -x ${CFSSL_BIN} || ! -x ${CFSSLJSON_BIN} ]]; then
  689. echo "Failed to download 'cfssl'. Please install cfssl and cfssljson and verify they are in \$PATH."
  690. echo "Hint: export PATH=\$PATH:\$GOPATH/bin; go get -u github.com/cloudflare/cfssl/cmd/..."
  691. exit 1
  692. fi
  693. popd > /dev/null
  694. }
  695. # kratos::util::ensure_dockerized
  696. # Confirms that the script is being run inside a kratos-build image
  697. #
  698. function kratos::util::ensure_dockerized {
  699. if [[ -f /kratos-build-image ]]; then
  700. return 0
  701. else
  702. echo "ERROR: This script is designed to be run inside a kratos-build container"
  703. exit 1
  704. fi
  705. }
  706. # kratos::util::ensure-gnu-sed
  707. # Determines which sed binary is gnu-sed on linux/darwin
  708. #
  709. # Sets:
  710. # SED: The name of the gnu-sed binary
  711. #
  712. function kratos::util::ensure-gnu-sed {
  713. if LANG=C sed --help 2>&1 | grep -q GNU; then
  714. SED="sed"
  715. elif which gsed &>/dev/null; then
  716. SED="gsed"
  717. else
  718. kratos::log::error "Failed to find GNU sed as sed or gsed. If you are on Mac: brew install gnu-sed." >&2
  719. return 1
  720. fi
  721. }
  722. function kratos::util::ensure-homebrew {
  723. if ! brew --version > /dev/null ; then
  724. echo "install homebrew..."
  725. /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  726. fi
  727. }
  728. function kratos::util::ensure-homebrew-bazel {
  729. if ! brew ls --versions bazel > /dev/null ; then
  730. echo "install bazel..."
  731. brew install bazel
  732. fi
  733. }
  734. function kratos::util::ensure-bazel {
  735. if ! bazel version > /dev/null ; then
  736. echo "Please install bazel by being compiled from code."
  737. fi
  738. }
  739. # Some useful colors.
  740. if [[ -z "${color_start-}" ]]; then
  741. declare -r color_start="\033["
  742. declare -r color_red="${color_start}0;31m"
  743. declare -r color_yellow="${color_start}0;33m"
  744. declare -r color_green="${color_start}0;32m"
  745. declare -r color_norm="${color_start}0m"
  746. fi
  747. # ex: ts=2 sw=2 et filetype=sh