setup.sh 3.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/env bash

function checkout_commit() {
    # args:
    # PIPE_ROOT: The root path of the pipeline
    # STEP_NAME: The name of the step to setup
    # COMMIT: The commit to be used
    local PIPE_ROOT=$1
    local STEP_NAME=$2
    local COMMIT=$4

    local STEPS_DIR=${PIPE_ROOT}/pipeline
    local STEP_DIR=${STEPS_DIR}/${STEP_NAME}
    
    cd ${STEP_DIR}
    git checkout ${COMMIT} > /dev/null 2>&1

    if [[ $? != 0 ]];
    then
	echo "ERROR: could not checkout the provided commit"
	return $?
    fi;

    local CUR_COMMIT=`git rev-parse --short HEAD`
    echo "The repo is now in commit: ${CUR_COMMIT}"

    cd ${PIPE_ROOT}

    return 0

}
    
function setup_step_from_git_repo() {
    # args:
    # PIPE_ROOT: The root path of the pipeline
    # STEP_NAME: The name of the step to setup
    # REPO_URL: The https url of the repo (must be public)
    # COMMIT: The commit to be used
    local PIPE_ROOT=$1
    local STEP_NAME=$2
    local REPO_URL=$3
    local COMMIT=$4
    
    local STEPS_DIR="${PIPE_ROOT}/pipeline"
    local STEP_DIR="${STEPS_DIR}/${STEP_NAME}"

    echo ">>> Setup ${STEP_NAME} from ${REPO_URL}:${COMMIT}"

    # If the step does not exists in the pipeline
    if [ ! -d ${STEP_DIR} ];
    then
	cd ${STEPS_DIR}
	git clone ${REPO_URL}

	if [[ $? != 0 ]];
	then
	    echo "ERROR: could not git clone from the provided url"
	    return $?
	fi;
	
	cd ${PIPE_ROOT}

    fi;

    # if git repo then Checkout commit
    if [ -d ${STEP_DIR}/.git ];
    then
	checkout_commit ${PIPE_ROOT} ${STEP_NAME} ${COMMIT}

	if [[ $? != 0 ]];
	then
	    return $?
	fi;
	
    else
	echo "ERROR: the source must be under Git version control"
	return 1
	
    fi;

    return 0

}


function setup_step() {
    # args:
    # PIPE_ROOT: The root path of the pipeline
    # STEP_NAME: The name of the step to setup
    local PIPE_ROOT=$1
    local STEP_NAME=$2

    local STEPS=`./stoml ${PIPE_ROOT}/config.toml steps`
    
    if [[ ! " ${STEPS[*]} " =~ " ${STEP_NAME} " ]]; then
	echo "ERROR: the step: ${STEP_NAME} is not defined."
	return 1
    fi
    
    local TYPE=`./stoml ${PIPE_ROOT}/config.toml steps.${STEP_NAME}.type`

    case $TYPE in

	git_repo)
	    local REPO_URL=`./stoml ${PIPE_ROOT}/config.toml steps.${STEP_NAME}.repo_url`
	    local COMMIT=`./stoml ${PIPE_ROOT}/config.toml steps.${STEP_NAME}.commit`

108
	    # setup the step
109
110
111
112
113
114
	    setup_step_from_git_repo ${PIPE_ROOT} ${STEP_NAME} ${REPO_URL} ${COMMIT}

	    if [[ $? != 0 ]];
	    then
		return $?
	    fi;
115
116
117
118
119
120
121
122
123
124
125
126
127

	    # create a runtime-parameters for the step
	    if [ ! -d ${PIPE_ROOT}/${RUNTIME_PARAMS}/${STEP_NAME} ];
	    then
		mkdir ${PIPE_ROOT}/${RUNTIME_PARAMS}/${STEP_NAME}

		if [[ $? != 0 ]];
		then
		    echo "ERROR: Could not create the runtime-parameters folder. Do you have the write-rights there: ${PIPE_ROOT}/${RUNTIME_PARAM} ?"
		    return $?
		fi;
		
	    fi;
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
	    
	    ;;

	*)
	    echo "ERROR: TYPE: ${TYPE} not supported."
	    return 1 
	    ;;
    esac

    return 0
    
}

function setup_pipeline() {

    local PIPE_ROOT=`pwd`
    local STEP_NAMES=(`./stoml ${PIPE_ROOT}/config.toml pipeline`)

    echo ">>> Steps: ${STEP_NAMES[@]}"

    for STEP_NAME in "${STEP_NAMES[@]}";
    do

	setup_step ${PIPE_ROOT} ${STEP_NAME}

	if [[ $? != 0 ]];
	then
	    echo "ERROR: Could not setup step: ${STEP_NAME}"
	    return $?
	fi;

    done;

    return 0
}