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.

350 lines
8.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 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_ESC=$'\e['
  21. ansi_color() {
  22. local choice="$1"
  23. if [ "$choice" == "tty" ]; then
  24. if [ -t 1 ]; then
  25. choice="yes"
  26. else
  27. choice="no"
  28. fi
  29. fi
  30. if [ "$choice" != "no" ]; then
  31. SET_COL_CHAR="${ANSI_ESC}${COL_CHAR}G"
  32. SET_COL_STATUS="${ANSI_ESC}${COL_STATUS}G"
  33. SET_COL_INFO="${ANSI_ESC}${COL_INFO}G"
  34. SET_COL_ELT="${ANSI_ESC}${COL_ELT}G"
  35. SET_BEGINCOL="${ANSI_ESC}0G"
  36. UP="${ANSI_ESC}1A"
  37. DOWN="${ANSI_ESC}1B"
  38. LEFT="${ANSI_ESC}1D"
  39. RIGHT="${ANSI_ESC}1C"
  40. SAVE="${ANSI_ESC}7"
  41. RESTORE="${ANSI_ESC}8"
  42. NORMAL="${ANSI_ESC}0m"
  43. GRAY="${ANSI_ESC}1;30m"
  44. RED="${ANSI_ESC}1;31m"
  45. GREEN="${ANSI_ESC}1;32m"
  46. YELLOW="${ANSI_ESC}1;33m"
  47. BLUE="${ANSI_ESC}1;34m"
  48. PINK="${ANSI_ESC}1;35m"
  49. CYAN="${ANSI_ESC}1;36m"
  50. WHITE="${ANSI_ESC}1;37m"
  51. DARKGRAY="${ANSI_ESC}0;30m"
  52. DARKRED="${ANSI_ESC}0;31m"
  53. DARKGREEN="${ANSI_ESC}0;32m"
  54. DARKYELLOW="${ANSI_ESC}0;33m"
  55. DARKBLUE="${ANSI_ESC}0;34m"
  56. DARKPINK="${ANSI_ESC}0;35m"
  57. DARKCYAN="${ANSI_ESC}0;36m"
  58. DARKWHITE="${ANSI_ESC}0;37m"
  59. SUCCESS=$GREEN
  60. WARNING=$YELLOW
  61. FAILURE=$RED
  62. NOOP=$BLUE
  63. ON=$SUCCESS
  64. OFF=$FAILURE
  65. ERROR=$FAILURE
  66. else
  67. SET_COL_CHAR=
  68. SET_COL_STATUS=
  69. SET_COL_INFO=
  70. SET_COL_ELT=
  71. SET_BEGINCOL=
  72. NORMAL=
  73. RED=
  74. GREEN=
  75. YELLOW=
  76. BLUE=
  77. GRAY=
  78. WHITE=
  79. DARKGRAY=
  80. DARKRED=
  81. DARKGREEN=
  82. DARKYELLOW=
  83. DARKBLUE=
  84. DARKPINK=
  85. DARKCYAN=
  86. SUCCESS=
  87. WARNING=
  88. FAILURE=
  89. NOOP=
  90. ON=
  91. OFF=
  92. ERROR=
  93. fi
  94. ansi_color="$choice"
  95. export SET_COL_CHAR SET_COL_STATUS SET_COL_INFO SET_COL_ELT \
  96. SET_BEGINCOL UP DOWN LEFT RIGHT SAVE RESTORE NORMAL \
  97. GRAY RED GREEN YELLOW BLUE PINK CYAN WHITE DARKGRAY \
  98. DARKRED DARKGREEN DARKYELLOW DARKBLUE DARKPINK DARKCYAN \
  99. SUCCESS WARNING FAILURE NOOP ON OFF ERROR ansi_color
  100. }
  101. depends() {
  102. ## Avoid colliding with variables that are created with depends.
  103. local __i __path __new_name
  104. for __i in "$@"; do
  105. if ! __path=$(get_path "$__i"); then
  106. __new_name="$(echo "${__i//-/_}")"
  107. if [ "$__new_name" != "$__i" ]; then
  108. depends "$__new_name"
  109. else
  110. err "dependency check: couldn't find '$__i' required command."
  111. exit 1
  112. fi
  113. else
  114. if ! test -z "$__path" ; then
  115. export "$(echo "${__i//- /__}")"="$__path"
  116. fi
  117. fi
  118. done
  119. }
  120. get_os() {
  121. local uname_output
  122. uname_output="$(uname -s)"
  123. case "${uname_output}" in
  124. Linux*)
  125. if [[ "$(< /proc/version)" =~ ^.*@(Microsoft|WSL).*$ ]]; then
  126. e wsl
  127. elif [[ "$(< /proc/version)" =~ ^.*-microsoft-.*$ ]]; then
  128. e wsl2
  129. elif [[ "$(< /proc/version)" == *-boot2docker* ]] && [[ -d /Users ]]; then
  130. e docker-toolbox-for-mac
  131. else
  132. e linux
  133. fi
  134. ;;
  135. Darwin*) e mac;;
  136. CYGWIN*) e cygwin;;
  137. MINGW*) e mingw;;
  138. *) e "UNKNOWN:${uname_output}";;
  139. esac
  140. }
  141. OS=$(get_os) || {
  142. err "Couldn't figure what OS you are running."
  143. exit 1
  144. }
  145. fn.exists() { declare -F "$1" >/dev/null; }
  146. ## copy stdin to file, archive previous existing file if any
  147. install_file() {
  148. local path="$1" tmpfile
  149. tmpfile="$(mktemp)"
  150. cat > "${tmpfile}" || return 1
  151. mkdir -vp "${path%/*}" || exit 1
  152. if [[ -e "$path" ]]; then
  153. if ! diff "${tmpfile}" "${path}";then
  154. info "File '$path' exists but is different, archiving current content and replacing it."
  155. candidate="${path}.bak"
  156. n=1
  157. while [ -e "$candidate" ]; do
  158. candidate="${path}.bak.$((n++))"
  159. done
  160. echo "Archiving previous version of '$path'"
  161. mv -v "${path}" "$candidate" || return 1
  162. else
  163. info "File '$path' exists and is already with the right content."
  164. fi
  165. else
  166. echo "Creating '${path}'."
  167. mv "${tmpfile}" "$path" || return 1
  168. fi
  169. rm -f "$tmpfile"
  170. }
  171. get_charm_store() {
  172. if ! [ -d "$CHARM_PATH" ]; then
  173. echo "Creating charm-store in '$CHARM_PATH'."
  174. git clone https://git.0k.io/0k-charms.git "$CHARM_PATH" || exit 1
  175. else
  176. echo "Updating existing charm-store in '$CHARM_PATH'."
  177. cd "$CHARM_PATH" || exit 1
  178. git checkout master &&
  179. git pull -r || {
  180. warn "Could not update charm-store, do you have devs ingoing ?"
  181. }
  182. fi
  183. }
  184. fetch_binary() {
  185. local url="$1" name="$2" bin_path tmpfile
  186. mkdir -p "$BIN_PATH" || return 1
  187. info "Downloading '$name'..."
  188. (
  189. tmpfile=$(mktemp) && trap "rm -f '$tmpfile'" EXIT &&
  190. curl -sS "$url" > "$tmpfile" &&
  191. chmod +x "$tmpfile"
  192. if [ -e "$BIN_PATH/$name" ] && diff "$tmpfile" "$BIN_PATH/$name"; then
  193. echo " .. Done (File '$BIN_PATH/$name' was already up to date.)"
  194. else
  195. mv "$tmpfile" "$BIN_PATH/$name"
  196. echo " .. Done !"
  197. fi
  198. ) || return 1
  199. hash -r
  200. if bin_path=$(type -p "$name"); then
  201. if [ "$bin_path" != "$BIN_PATH/$name" ]; then
  202. warn "Found a '$name' in \$PATH at '$bin_path'." \
  203. $'\n'" That doesn't match expected location '$BIN_PATH/$name'." \
  204. $'\n'" You might need to change you \$PATH to include '$BIN_PATH' before '${bin_path%/*}'."
  205. fi
  206. fi
  207. }
  208. get_docker_ip() {
  209. fetch_binary https://git.0k.io/0k-docker.git/plain/src/bin/docker-ip docker-ip
  210. }
  211. install.linux() {
  212. depends docker curl git
  213. if [ "$UID" != 0 ]; then
  214. BIN_PATH=~/bin
  215. CHARM_PATH=~/.charm-store
  216. DEFAULT_COMPOSE_YML=~/.compose/etc/compose.yml
  217. COMPOSE_OPTION_FILE=~/.compose/etc/local.conf
  218. else
  219. BIN_PATH=/usr/local/bin
  220. CHARM_PATH=/srv/charm-store
  221. DEFAULT_COMPOSE_YML=/etc/compose/compose.yml
  222. COMPOSE_OPTION_FILE=/etc/compose/local.conf
  223. fi
  224. fetch_binary https://git.0k.io/0k-compose.git/plain/bin/compose compose
  225. get_docker_ip
  226. if [[ ":$PATH:" != *":$BIN_PATH:"* ]]; then
  227. warn "Please ensure that '$BIN_PATH' is in your \$PATH to ensure" \
  228. "the simple usage of 'compose' command."
  229. fi
  230. ## CHARMS
  231. get_charm_store || exit 1
  232. ##
  233. ## YYYvlab: following needs to be discussed further with njeudy
  234. ##
  235. ## DEFAULT COMPOSE
  236. # cat <<EOF | install_file "$DEFAULT_COMPOSE_YML"
  237. # odoo:
  238. # charm: odoo-tecnativa
  239. # # docker-compose:
  240. # # ## Important to keep as a list: otherwise it'll overwrite charm's arguments.
  241. # # command:
  242. # # - "--log-level=debug"
  243. # # environment:
  244. # # TOTO: TATA
  245. # # image: mynewimage
  246. # # options:
  247. # # workers: 1
  248. # # modules: ## 'base' is automatically added
  249. # # - l10n_fr
  250. # # - mymodule
  251. # # database: mybase ## defaults to database in relation
  252. # frontend:
  253. # charm: apache
  254. # EOF
  255. # cat <<EOF | install_file "$COMPOSE_OPTION_FILE"
  256. # #CHARM_STORE=$CHARM_STORE
  257. # #COMPOSE_DOCKER_IMAGE=docker.0k.io/compose:1.3.0-rc5
  258. # #if [ "${docker_run_opts+x}" ]; then
  259. # # docker_run_opts+=("-v" "/home/vaab/dev/sh/0k-compose/bin/compose-core:/usr/local/bin/compose-core")
  260. # #fi
  261. # #DEFAULT_COMPOSE_YML=$DEFAULT_COMPOSE_YML
  262. # EOF
  263. }
  264. install.docker-toolbox-for-mac() {
  265. install.linux
  266. }
  267. install.wsl() {
  268. CHARM_PATH=~/.charm-store
  269. get_charm_store || return 1
  270. get_docker_ip
  271. }
  272. install.wsl2() {
  273. install.linux
  274. }
  275. run() {
  276. OS="$(get_os)"
  277. if fn.exists "install.$OS"; then
  278. "install.$OS"
  279. else
  280. echo "System '$OS' not supported yet." >&2
  281. fi
  282. }
  283. ansi_color tty
  284. run