blob: 03153d3d26b74c588d12bc2fe10b0f310e9746e8 [file] [log] [blame]
Ales Komarek1b373112017-08-08 08:48:56 +02001#!/bin/bash
2
3# Usage:
4# ./formula-fetch.sh <Formula URL> <Name> <Branch>
5#
6# Example:
7# GIT_FORMULAS_PATH=.vendor/formulas ./formula-fetch.sh https://github.com/salt-formulas/salt-formula-salt
8# --
9# GIT_FORMULAS_PATH=/usr/share/salt-formulas/env/_formulas
10# xargs -n1 ./formula-fetch.sh < dependencies.txt
11
12
13# Parse git dependencies from metadata.yml
14# $1 - path to <formula>/metadata.yml
15# sample to output:
16# https://github.com/salt-formulas/salt-formula-git git
17# https://github.com/salt-formulas/salt-formula-salt salt
18function fetchDependencies() {
19 METADATA="$1";
20 grep -E "^dependencies:" "$METADATA" >/dev/null || return 0
21 (python - "$METADATA" | while read dep; do fetchGitFormula "$dep"; done) <<-DEPS
22 import sys,yaml
23 for dep in yaml.load(open(sys.argv[1], "ro"))["dependencies"]:
24 print("{source} {name}").format(**dep)
25 DEPS
26}
27
28# Fetch formula from git repo
29# $1 - formula git repo url
30# $2 - formula name (optional)
31# $3 - branch (optional)
32function fetchGitFormula() {
33 test -n "${FETCHED}" || declare -a FETCHED=()
34 export GIT_FORMULAS_PATH=${GIT_FORMULAS_PATH:-/usr/share/salt-formulas/env/_formulas}
35 mkdir -p "$GIT_FORMULAS_PATH"
36 if [ -n "$1" ]; then
37 source="$1"
38 name="$2"
39 test -n "$name" || name="${source//*salt-formula-}"
40 test -z "$3" && branch=master || branch=$3
41 if ! [[ "${FETCHED[*]}" =~ $name ]]; then # dependency not yet fetched
42 echo "Fetching: $name"
43 if test -e "$GIT_FORMULAS_PATH/$name"; then
44 pushd "$GIT_FORMULAS_PATH/$name" &>/dev/null
45 test ! -e .git || git pull -r
46 popd &>/dev/null
47 else
48 echo "git clone $source $GIT_FORMULAS_PATH/$name -b $branch"
49 git clone "$source" "$GIT_FORMULAS_PATH/$name" -b "$branch"
50 fi
51 # install dependencies
52 FETCHED+=($name)
53 fetchDependencies "$GIT_FORMULAS_PATH/$name/metadata.yml"
54 fi
55 else
56 echo Usage: fetchGitFormula "<git repo>" "[local formula directory name]" "[branch]"
57 fi
58}
59
60function linkFormulas() {
61 # OPTIONAL: Link formulas from git/pkg
62
63 SALT_ROOT=$1
64 SALT_ENV=${2:-/usr/share/salt-formulas/env}
65
66 # form git, development versions
67 find "$SALT_ENV"/_formulas -maxdepth 1 -mindepth 1 -type d -print0| xargs -0 -n1 --no-run-if-empty basename | xargs -I{} --no-run-if-empty \
68 ln -fs "$SALT_ENV"/_formulas/{}/{} "$SALT_ROOT"/{};
69
70 # form pkgs
71 find "$SALT_ENV" -maxdepth 1 -mindepth 1 -path "*_formulas*" -prune -o -name "*" -type d -print0| xargs -0 -n1 --no-run-if-empty basename | xargs -I{} --no-run-if-empty \
72 ln -fs "$SALT_ENV"/{} "$SALT_ROOT"/{};
73
74}
75
76# detect if file is being sourced
77[[ "$0" != "$BASH_SOURCE" ]] || {
78 # if executed, run implicit function
79 fetchGitFormula "${@}"
80}
81