Doc, tools for lokavaluto development
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
6.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #!/bin/bash
  2. e() { printf "%s" "$*"; }
  3. warn() { e "${YELLOW}Warning:$NORMAL" "$*"$'\n' >&2 ; }
  4. info() { e "${BLUE}II$NORMAL" "$*"$'\n' >&2 ; }
  5. verb() { [ -z "$VERBOSE" ] || e "$*"$'\n' >&2; }
  6. debug() { [ -z "$DEBUG" ] || e "$*"$'\n' >&2; }
  7. err() { e "${RED}Error:$NORMAL $*"$'\n' >&2 ; }
  8. die() { err "$@" ; exit 1; }
  9. get_path() { (
  10. IFS=:
  11. for d in $PATH; do
  12. filename="$d/$1"
  13. [ -f "$filename" -a -x "$filename" ] && {
  14. echo "$d/$1"
  15. return 0
  16. }
  17. done
  18. return 1
  19. ) }
  20. ansi_color() {
  21. local choice="$1"
  22. if [ "$choice" == "tty" ]; then
  23. if [ -t 1 ]; then
  24. choice="yes"
  25. else
  26. choice="no"
  27. fi
  28. fi
  29. ANSI_ESC=$'\e['
  30. if [ "$choice" != "no" ]; then
  31. NORMAL="${ANSI_ESC}0m"
  32. GRAY="${ANSI_ESC}1;30m"
  33. RED="${ANSI_ESC}1;31m"
  34. GREEN="${ANSI_ESC}1;32m"
  35. YELLOW="${ANSI_ESC}1;33m"
  36. BLUE="${ANSI_ESC}1;34m"
  37. PINK="${ANSI_ESC}1;35m"
  38. CYAN="${ANSI_ESC}1;36m"
  39. WHITE="${ANSI_ESC}1;37m"
  40. DARKGRAY="${ANSI_ESC}0;30m"
  41. DARKRED="${ANSI_ESC}0;31m"
  42. DARKGREEN="${ANSI_ESC}0;32m"
  43. DARKYELLOW="${ANSI_ESC}0;33m"
  44. DARKBLUE="${ANSI_ESC}0;34m"
  45. DARKPINK="${ANSI_ESC}0;35m"
  46. DARKCYAN="${ANSI_ESC}0;36m"
  47. DARKWHITE="${ANSI_ESC}0;37m"
  48. fi
  49. ansi_color="$choice"
  50. export NORMAL \
  51. GRAY RED GREEN YELLOW BLUE PINK CYAN WHITE DARKGRAY \
  52. DARKRED DARKGREEN DARKYELLOW DARKBLUE DARKPINK DARKCYAN \
  53. ansi_color
  54. }
  55. depends() {
  56. ## Avoid colliding with variables that are created with depends.
  57. local __i __path __new_name
  58. for __i in "$@"; do
  59. if ! __path=$(get_path "$__i"); then
  60. __new_name="$(echo "${__i//-/_}")"
  61. if [ "$__new_name" != "$__i" ]; then
  62. depends "$__new_name"
  63. else
  64. err "dependency check: couldn't find '$__i' required command."
  65. exit 1
  66. fi
  67. else
  68. if ! test -z "$__path" ; then
  69. export "$(echo "${__i//- /__}")"="$__path"
  70. fi
  71. fi
  72. done
  73. }
  74. get_os() {
  75. local uname_output
  76. uname_output="$(uname -s)"
  77. case "${uname_output}" in
  78. Linux*)
  79. if [[ "$(< /proc/version)" =~ ^.*@(Microsoft|WSL).*$ ]]; then
  80. e wsl
  81. elif [[ "$(< /proc/version)" =~ ^.*-microsoft-.*$ ]]; then
  82. e wsl2
  83. elif [[ "$(< /proc/version)" == *-boot2docker* ]] && [[ -d /Users ]]; then
  84. e docker-toolbox-for-mac
  85. else
  86. e linux
  87. fi
  88. ;;
  89. Darwin*) e mac;;
  90. CYGWIN*) e cygwin;;
  91. MINGW*) e mingw;;
  92. *) e "UNKNOWN:${uname_output}";;
  93. esac
  94. }
  95. OS=$(get_os) || {
  96. err "Couldn't figure what OS you are running."
  97. exit 1
  98. }
  99. fn.exists() { declare -F "$1" >/dev/null; }
  100. ## copy stdin to file, archive previous existing file if any
  101. install_file() {
  102. local path="$1" tmpfile
  103. tmpfile="$(mktemp)"
  104. cat > "${tmpfile}" || return 1
  105. mkdir -vp "${path%/*}" || exit 1
  106. if [[ -e "$path" ]]; then
  107. if ! diff "${tmpfile}" "${path}";then
  108. info "File '$path' exists but is different, archiving current content and replacing it."
  109. candidate="${path}.bak"
  110. n=1
  111. while [ -e "$candidate" ]; do
  112. candidate="${path}.bak.$((n++))"
  113. done
  114. echo "Archiving previous version of '$path'"
  115. mv -v "${path}" "$candidate" || return 1
  116. else
  117. info "File '$path' exists and is already with the right content."
  118. fi
  119. else
  120. echo "Creating '${path}'."
  121. mv "${tmpfile}" "$path" || return 1
  122. fi
  123. rm -f "$tmpfile"
  124. }
  125. get_charm_store() {
  126. local branch="${1:master}"
  127. if ! [ -d "$CHARM_PATH" ]; then
  128. echo "Creating charm-store in '$CHARM_PATH'."
  129. git clone https://git.0k.io/0k-charms.git -b "$branch" "$CHARM_PATH" || exit 1
  130. else
  131. echo "Updating existing charm-store in '$CHARM_PATH'."
  132. cd "$CHARM_PATH" || exit 1
  133. git fetch &&
  134. git checkout "$branch" &&
  135. git pull -r || {
  136. warn "Could not update charm-store, do you have devs ingoing ?"
  137. }
  138. fi
  139. }
  140. fetch_binary() {
  141. local url="$1" name="$2" bin_path tmpfile
  142. mkdir -p "$BIN_PATH" || return 1
  143. info "Downloading '$name'..."
  144. (
  145. tmpfile=$(mktemp) && trap "rm -f '$tmpfile'" EXIT &&
  146. curl -sS "$url" > "$tmpfile" &&
  147. chmod +x "$tmpfile"
  148. if [ -e "$BIN_PATH/$name" ] && diff "$tmpfile" "$BIN_PATH/$name"; then
  149. echo " .. Done (File '$BIN_PATH/$name' was already up to date.)"
  150. else
  151. mv "$tmpfile" "$BIN_PATH/$name"
  152. echo " .. Done !"
  153. fi
  154. ) || return 1
  155. hash -r
  156. if bin_path=$(type -p "$name"); then
  157. if [ "$bin_path" != "$BIN_PATH/$name" ]; then
  158. warn "Found a '$name' in \$PATH at '$bin_path'." \
  159. $'\n'" That doesn't match expected location '$BIN_PATH/$name'." \
  160. $'\n'" You might need to change you \$PATH to include '$BIN_PATH' before '${bin_path%/*}'."
  161. fi
  162. fi
  163. }
  164. get_docker_ip() {
  165. fetch_binary https://git.0k.io/0k-docker.git/plain/src/bin/docker-ip docker-ip
  166. }
  167. install.linux() {
  168. depends docker curl git
  169. if [ "$UID" != 0 ]; then
  170. BIN_PATH=~/bin
  171. CHARM_PATH=~/.charm-store
  172. DEFAULT_COMPOSE_YML=~/.compose/etc/compose.yml
  173. COMPOSE_OPTION_FILE=~/.compose/etc/local.conf
  174. else
  175. BIN_PATH=/usr/local/bin
  176. CHARM_PATH=/srv/charm-store
  177. DEFAULT_COMPOSE_YML=/etc/compose/compose.yml
  178. COMPOSE_OPTION_FILE=/etc/compose/local.conf
  179. fi
  180. fetch_binary https://git.0k.io/0k-compose.git/plain/bin/compose?h="${DEPLOY_REF}" compose
  181. get_docker_ip
  182. if [[ ":$PATH:" != *":$BIN_PATH:"* ]]; then
  183. warn "Please ensure that '$BIN_PATH' is in your \$PATH to ensure" \
  184. "the simple usage of 'compose' command."
  185. fi
  186. get_charm_store "${DEPLOY_REF}" || exit 1
  187. cat <<EOF | install_file "$COMPOSE_OPTION_FILE"
  188. #CHARM_STORE=$CHARM_STORE
  189. COMPOSE_DOCKER_IMAGE=docker.0k.io/compose:${DEPLOY_REF//\//-}
  190. #if [ "${docker_run_opts+x}" ]; then
  191. # docker_run_opts+=("-v" "/home/vaab/dev/sh/0k-compose/bin/compose-core:/usr/local/bin/compose-core")
  192. #fi
  193. #DEFAULT_COMPOSE_YML=$DEFAULT_COMPOSE_YML
  194. EOF
  195. }
  196. install.docker-toolbox-for-mac() {
  197. install.linux
  198. }
  199. install.wsl() {
  200. CHARM_PATH=~/.charm-store
  201. get_charm_store "${DEPLOY_REF}" || return 1
  202. get_docker_ip
  203. }
  204. install.wsl2() {
  205. install.linux
  206. }
  207. run() {
  208. OS="$(get_os)"
  209. if fn.exists "install.$OS"; then
  210. "install.$OS"
  211. else
  212. echo "System '$OS' not supported yet." >&2
  213. fi
  214. }
  215. ##
  216. ## Code
  217. ##
  218. ansi_color tty
  219. DEPLOY_REF="${DEPLOY_REF:-lokavaluto/dev/master}"
  220. run