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.

329 lines
7.9 KiB

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|WSL)* ]]; 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. install.linux() {
  183. depends docker curl git
  184. if [ "$UID" != 0 ]; then
  185. BIN_PATH=~/bin
  186. CHARM_PATH=~/.charm-store
  187. DEFAULT_COMPOSE_YML=~/.compose/etc/compose.yml
  188. COMPOSE_OPTION_FILE=~/.compose/etc/local.conf
  189. else
  190. BIN_PATH=/usr/local/bin
  191. CHARM_PATH=/srv/charm-store
  192. DEFAULT_COMPOSE_YML=/etc/compose/compose.yml
  193. COMPOSE_OPTION_FILE=/etc/compose/local.conf
  194. fi
  195. ## COMPOSE
  196. if compose=$(type -p "compose"); then
  197. if [ "$compose" != "$BIN_PATH/compose" ]; then
  198. warn "Found a 'compose' in \$PATH at '$compose' that doesn't match expected compose location '$BIN_PATH/compose'." \
  199. "Not doing anything."
  200. elif [ -L "$compose" ]; then
  201. warn "Found a 'compose' at '$compose' that is a link. " \
  202. "Not doing anything."
  203. else
  204. compose="" ## force overwrite
  205. fi
  206. fi
  207. if [ -z "$compose" ]; then
  208. mkdir -p "$BIN_PATH" || exit 1
  209. info "Downloading 'compose'..."
  210. (
  211. set +x
  212. curl -sS https://git.0k.io/0k-compose.git/plain/bin/compose > /tmp/compose
  213. chmod -v +x /tmp/compose
  214. mv -v /tmp/compose "$BIN_PATH/"
  215. ) || exit 1
  216. echo " .. Done !"
  217. fi
  218. if [[ ":$PATH:" != *":$BIN_PATH:"* ]]; then
  219. warn "Please ensure that '$BIN_PATH' is in your \$PATH to ensure" \
  220. "the simple usage of 'compose' command."
  221. fi
  222. ## CHARMS
  223. get_charm_store || exit 1
  224. ##
  225. ## YYYvlab: following needs to be discussed further with njeudy
  226. ##
  227. ## DEFAULT COMPOSE
  228. # cat <<EOF | install_file "$DEFAULT_COMPOSE_YML"
  229. # odoo:
  230. # charm: odoo-tecnativa
  231. # # docker-compose:
  232. # # ## Important to keep as a list: otherwise it'll overwrite charm's arguments.
  233. # # command:
  234. # # - "--log-level=debug"
  235. # # environment:
  236. # # TOTO: TATA
  237. # # image: mynewimage
  238. # # options:
  239. # # workers: 1
  240. # # modules: ## 'base' is automatically added
  241. # # - l10n_fr
  242. # # - mymodule
  243. # # database: mybase ## defaults to database in relation
  244. # frontend:
  245. # charm: apache
  246. # EOF
  247. # cat <<EOF | install_file "$COMPOSE_OPTION_FILE"
  248. # #CHARM_STORE=$CHARM_STORE
  249. # #COMPOSE_DOCKER_IMAGE=docker.0k.io/compose:1.3.0-rc5
  250. # #if [ "${docker_run_opts+x}" ]; then
  251. # # docker_run_opts+=("-v" "/home/vaab/dev/sh/0k-compose/bin/compose-core:/usr/local/bin/compose-core")
  252. # #fi
  253. # #DEFAULT_COMPOSE_YML=$DEFAULT_COMPOSE_YML
  254. # EOF
  255. }
  256. install.mac() {
  257. depends docker
  258. die "Not implemented yet."
  259. }
  260. install.wsl() {
  261. CHARM_PATH=~/.charm-store
  262. get_charm_store || return 1
  263. }
  264. run() {
  265. OS="$(get_os)"
  266. if fn.exists "install.$OS"; then
  267. "install.$OS"
  268. else
  269. echo "System '$OS' not supported yet." >&2
  270. fi
  271. }
  272. ansi_color tty
  273. run