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.

338 lines
8.1 KiB

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. else
  130. e linux
  131. fi
  132. ;;
  133. Darwin*) e mac;;
  134. CYGWIN*) e cygwin;;
  135. MINGW*) e mingw;;
  136. *) e "UNKNOWN:${uname_output}";;
  137. esac
  138. }
  139. OS=$(get_os) || {
  140. err "Couldn't figure what OS you are running."
  141. exit 1
  142. }
  143. fn.exists() { declare -F "$1" >/dev/null; }
  144. ## copy stdin to file, archive previous existing file if any
  145. install_file() {
  146. local path="$1" tmpfile
  147. tmpfile="$(mktemp)"
  148. cat > "${tmpfile}" || return 1
  149. mkdir -vp "${path%/*}" || exit 1
  150. if [[ -e "$path" ]]; then
  151. if ! diff "${tmpfile}" "${path}";then
  152. info "File '$path' exists but is different, archiving current content and replacing it."
  153. candidate="${path}.bak"
  154. n=1
  155. while [ -e "$candidate" ]; do
  156. candidate="${path}.bak.$((n++))"
  157. done
  158. echo "Archiving previous version of '$path'"
  159. mv -v "${path}" "$candidate" || return 1
  160. else
  161. info "File '$path' exists and is already with the right content."
  162. fi
  163. else
  164. echo "Creating '${path}'."
  165. mv "${tmpfile}" "$path" || return 1
  166. fi
  167. rm -f "$tmpfile"
  168. }
  169. get_charm_store() {
  170. if ! [ -d "$CHARM_PATH" ]; then
  171. echo "Creating charm-store in '$CHARM_PATH'."
  172. git clone https://git.0k.io/0k-charms.git "$CHARM_PATH" || exit 1
  173. else
  174. echo "Updating existing charm-store in '$CHARM_PATH'."
  175. cd "$CHARM_PATH" || exit 1
  176. git checkout master &&
  177. git pull -r || {
  178. warn "Could not update charm-store, do you have devs ingoing ?"
  179. }
  180. fi
  181. }
  182. fetch_binary() {
  183. local url="$1" name="$2" bin_path tmpfile
  184. mkdir -p "$BIN_PATH" || return 1
  185. info "Downloading '$name'..."
  186. (
  187. tmpfile=$(mktemp) && trap "rm -f '$tmpfile'" EXIT &&
  188. curl -sS "$url" > "$tmpfile" &&
  189. chmod +x "$tmpfile"
  190. if [ -e "$BIN_PATH/$name" ] && diff "$tmpfile" "$BIN_PATH/$name"; then
  191. echo " .. Done (File '$BIN_PATH/$name' was already up to date.)"
  192. else
  193. mv "$tmpfile" "$BIN_PATH/$name"
  194. echo " .. Done !"
  195. fi
  196. ) || return 1
  197. hash -r
  198. if bin_path=$(type -p "$name"); then
  199. if [ "$bin_path" != "$BIN_PATH/$name" ]; then
  200. warn "Found a '$name' in \$PATH at '$bin_path'." \
  201. $'\n'" That doesn't match expected location '$BIN_PATH/$name'." \
  202. $'\n'" You might need to change you \$PATH to include '$BIN_PATH' before '${bin_path%/*}'."
  203. fi
  204. fi
  205. }
  206. install.linux() {
  207. depends docker curl git
  208. if [ "$UID" != 0 ]; then
  209. BIN_PATH=~/bin
  210. CHARM_PATH=~/.charm-store
  211. DEFAULT_COMPOSE_YML=~/.compose/etc/compose.yml
  212. COMPOSE_OPTION_FILE=~/.compose/etc/local.conf
  213. else
  214. BIN_PATH=/usr/local/bin
  215. CHARM_PATH=/srv/charm-store
  216. DEFAULT_COMPOSE_YML=/etc/compose/compose.yml
  217. COMPOSE_OPTION_FILE=/etc/compose/local.conf
  218. fi
  219. fetch_binary https://git.0k.io/0k-compose.git/plain/bin/compose compose
  220. if [[ ":$PATH:" != *":$BIN_PATH:"* ]]; then
  221. warn "Please ensure that '$BIN_PATH' is in your \$PATH to ensure" \
  222. "the simple usage of 'compose' command."
  223. fi
  224. ## CHARMS
  225. get_charm_store || exit 1
  226. ##
  227. ## YYYvlab: following needs to be discussed further with njeudy
  228. ##
  229. ## DEFAULT COMPOSE
  230. # cat <<EOF | install_file "$DEFAULT_COMPOSE_YML"
  231. # odoo:
  232. # charm: odoo-tecnativa
  233. # # docker-compose:
  234. # # ## Important to keep as a list: otherwise it'll overwrite charm's arguments.
  235. # # command:
  236. # # - "--log-level=debug"
  237. # # environment:
  238. # # TOTO: TATA
  239. # # image: mynewimage
  240. # # options:
  241. # # workers: 1
  242. # # modules: ## 'base' is automatically added
  243. # # - l10n_fr
  244. # # - mymodule
  245. # # database: mybase ## defaults to database in relation
  246. # frontend:
  247. # charm: apache
  248. # EOF
  249. # cat <<EOF | install_file "$COMPOSE_OPTION_FILE"
  250. # #CHARM_STORE=$CHARM_STORE
  251. # #COMPOSE_DOCKER_IMAGE=docker.0k.io/compose:1.3.0-rc5
  252. # #if [ "${docker_run_opts+x}" ]; then
  253. # # docker_run_opts+=("-v" "/home/vaab/dev/sh/0k-compose/bin/compose-core:/usr/local/bin/compose-core")
  254. # #fi
  255. # #DEFAULT_COMPOSE_YML=$DEFAULT_COMPOSE_YML
  256. # EOF
  257. }
  258. install.mac() {
  259. depends docker
  260. die "Not implemented yet."
  261. }
  262. install.wsl() {
  263. CHARM_PATH=~/.charm-store
  264. get_charm_store || return 1
  265. }
  266. install.wsl2() {
  267. install.linux
  268. }
  269. run() {
  270. OS="$(get_os)"
  271. if fn.exists "install.$OS"; then
  272. "install.$OS"
  273. else
  274. echo "System '$OS' not supported yet." >&2
  275. fi
  276. }
  277. ansi_color tty
  278. run