demo.sh 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. set -euo pipefail
  16. function sql() {
  17. # TODO(knz): Why does the more idiomatic read from stdin not produce any
  18. # output?
  19. kubectl exec "cockroachdb-${1}" -- /cockroach/cockroach sql \
  20. --host "cockroachdb-${1}.cockroachdb" \
  21. -e "$(cat /dev/stdin)"
  22. }
  23. function kill() {
  24. ! kubectl exec -t "cockroachdb-${1}" -- /bin/bash -c "while true; do kill 1; done" &> /dev/null
  25. }
  26. # Create database on second node (idempotently for convenience).
  27. cat <<EOF | sql 1
  28. CREATE DATABASE IF NOT EXISTS foo;
  29. CREATE TABLE IF NOT EXISTS foo.bar (k STRING PRIMARY KEY, v STRING);
  30. UPSERT INTO foo.bar VALUES ('Kuber', 'netes'), ('Cockroach', 'DB');
  31. EOF
  32. # Kill the node we just created the table on.
  33. kill 1
  34. # Read the data from all other nodes (we could also read from the one we just
  35. # killed, but it's awkward to wait for it to respawn).
  36. for i in 0 2 3 4; do
  37. cat <<EOF | sql "${i}"
  38. SELECT CONCAT(k, v) FROM foo.bar;
  39. EOF
  40. done