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.

301 lines
7.3 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
  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. if ! [ -d "$CHARM_PATH" ]; then
  127. echo "Creating charm-store in '$CHARM_PATH'."
  128. git clone https://git.0k.io/0k-charms.git "$CHARM_PATH" || exit 1
  129. else
  130. echo "Updating existing charm-store in '$CHARM_PATH'."
  131. cd "$CHARM_PATH" || exit 1
  132. git checkout master &&
  133. git pull -r || {
  134. warn "Could not update charm-store, do you have devs ingoing ?"
  135. }
  136. fi
  137. }
  138. fetch_binary() {
  139. local url="$1" name="$2" bin_path tmpfile
  140. mkdir -p "$BIN_PATH" || return 1
  141. info "Downloading '$name'..."
  142. (
  143. tmpfile=$(mktemp) && trap "rm -f '$tmpfile'" EXIT &&
  144. curl -sS "$url" > "$tmpfile" &&
  145. chmod +x "$tmpfile"
  146. if [ -e "$BIN_PATH/$name" ] && diff "$tmpfile" "$BIN_PATH/$name"; then
  147. echo " .. Done (File '$BIN_PATH/$name' was already up to date.)"
  148. else
  149. mv "$tmpfile" "$BIN_PATH/$name"
  150. echo " .. Done !"
  151. fi
  152. ) || return 1
  153. hash -r
  154. if bin_path=$(type -p "$name"); then
  155. if [ "$bin_path" != "$BIN_PATH/$name" ]; then
  156. warn "Found a '$name' in \$PATH at '$bin_path'." \
  157. $'\n'" That doesn't match expected location '$BIN_PATH/$name'." \
  158. $'\n'" You might need to change you \$PATH to include '$BIN_PATH' before '${bin_path%/*}'."
  159. fi
  160. fi
  161. }
  162. get_docker_ip() {
  163. fetch_binary https://git.0k.io/0k-docker.git/plain/src/bin/docker-ip docker-ip
  164. }
  165. install.linux() {
  166. depends docker curl git
  167. if [ "$UID" != 0 ]; then
  168. BIN_PATH=~/bin
  169. CHARM_PATH=~/.charm-store
  170. DEFAULT_COMPOSE_YML=~/.compose/etc/compose.yml
  171. COMPOSE_OPTION_FILE=~/.compose/etc/local.conf
  172. else
  173. BIN_PATH=/usr/local/bin
  174. CHARM_PATH=/srv/charm-store
  175. DEFAULT_COMPOSE_YML=/etc/compose/compose.yml
  176. COMPOSE_OPTION_FILE=/etc/compose/local.conf
  177. fi
  178. fetch_binary https://git.0k.io/0k-compose.git/plain/bin/compose compose
  179. get_docker_ip
  180. if [[ ":$PATH:" != *":$BIN_PATH:"* ]]; then
  181. warn "Please ensure that '$BIN_PATH' is in your \$PATH to ensure" \
  182. "the simple usage of 'compose' command."
  183. fi
  184. ## CHARMS
  185. get_charm_store || exit 1
  186. ##
  187. ## YYYvlab: following needs to be discussed further with njeudy
  188. ##
  189. ## DEFAULT COMPOSE
  190. # cat <<EOF | install_file "$DEFAULT_COMPOSE_YML"
  191. # odoo:
  192. # charm: odoo-tecnativa
  193. # # docker-compose:
  194. # # ## Important to keep as a list: otherwise it'll overwrite charm's arguments.
  195. # # command:
  196. # # - "--log-level=debug"
  197. # # environment:
  198. # # TOTO: TATA
  199. # # image: mynewimage
  200. # # options:
  201. # # workers: 1
  202. # # modules: ## 'base' is automatically added
  203. # # - l10n_fr
  204. # # - mymodule
  205. # # database: mybase ## defaults to database in relation
  206. # frontend:
  207. # charm: apache
  208. # EOF
  209. # cat <<EOF | install_file "$COMPOSE_OPTION_FILE"
  210. # #CHARM_STORE=$CHARM_STORE
  211. # #COMPOSE_DOCKER_IMAGE=docker.0k.io/compose:1.3.0-rc5
  212. # #if [ "${docker_run_opts+x}" ]; then
  213. # # docker_run_opts+=("-v" "/home/vaab/dev/sh/0k-compose/bin/compose-core:/usr/local/bin/compose-core")
  214. # #fi
  215. # #DEFAULT_COMPOSE_YML=$DEFAULT_COMPOSE_YML
  216. # EOF
  217. }
  218. install.docker-toolbox-for-mac() {
  219. install.linux
  220. }
  221. install.wsl() {
  222. CHARM_PATH=~/.charm-store
  223. get_charm_store || return 1
  224. get_docker_ip
  225. }
  226. install.wsl2() {
  227. install.linux
  228. }
  229. run() {
  230. OS="$(get_os)"
  231. if fn.exists "install.$OS"; then
  232. "install.$OS"
  233. else
  234. echo "System '$OS' not supported yet." >&2
  235. fi
  236. }
  237. ##
  238. ## Code
  239. ##
  240. ansi_color tty
  241. run