summaryrefslogtreecommitdiff
path: root/include/conda_porcelain.sh
blob: f04ee5034cb23a5bf00a35da815e9efb09a1f58d (plain) (blame)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/sh
source /eng/ssb/auto/astroconda/include/sysinfo.sh
porcelain_continuum_url=https://repo.continuum.io/miniconda
porcelain_continuum_script=Miniconda3-latest-${sysinfo_platform}-${sysinfo_arch}.sh
porcelain_extra_config=/eng/ssb/auto/astroconda/etc/porcelain-extra
PORCELAIN_ALREADY_DEAD=0
PORCELAIN_SIGNALED=0

function porcelain_init
{
    echo "Porcelain initializing..."
    if [[ -z $HOME ]]; then
        echo "\$HOME is not set, dying."
        exit 1
    fi
    
    export TMPDIR="$HOME/bldtmp"
    
    if [[ ! -d $TMPDIR ]]; then
        mkdir -p $TMPDIR
        if [[ $? != 0 ]]; then
            echo "Cannot create temporary storage directory '$TMPDIR', dying."
            exit 1
        fi
    fi

    export PORCELAIN_PREFIX=`mktemp -d -t porcelain.XXXXXXXXXX` 
    export PORCELAIN_TMPDIR="$PORCELAIN_PREFIX/tmp"
    export PORCELAIN_DESTDIR="$PORCELAIN_PREFIX/porcelain"
    export PATH="$PORCELAIN_DESTDIR/bin:$PATH"
    echo "Prepended $PORCELAIN_DESTDIR/bin to PATH..."

    if [[ ! -d $PORCELAIN_TMPDIR ]]; then
        mkdir -p "$PORCELAIN_TMPDIR"
    fi

    export TMPDIR="$PORCELAIN_TMPDIR"
    echo "Redirected TMPDIR to $PORCELAIN_TMPDIR"
    echo "(Always use \$TMPDIR instead of /tmp for destructible storage)"
    
    echo "Activating signal handler... (will deinit on exit)"
    trap porcelain_signal SIGINT SIGKILL SIGTERM EXIT
}

function porcelain_verify
{
    local override="$1"
    /bin/echo -n "Running safety check... "

    if [[ $override == '-f' ]] || [[ $override == '--force' ]]; then
        echo "done (forced)"
        return 0
    fi

    if [[ -z $TMPDIR ]]; then
        echo "failed"
        echo "TMPDIR='$TMPDIR'; we have lost trust in our environment. Dying."
        exit 1
    fi

    if [[ ! -d $PORCELAIN_PREFIX ]]; then
        echo "failed"
        echo "PORCELAIN_PREFIX='$PORCELAIN_PREFIX'; mktemp must have failed. Dying."
        exit 1
    fi

    # Note: we don't care about PORCELAIN_DESTDIR
    echo "done"
}

function porcelain_get_installer
{
    porcelain_verify

    retries=3
    wtime=5
    success=0
    count=0

    echo "Obtaining $porcelain_continuum_script..."

    while [[ $count != $retries ]]
    do
        $sysinfo_fetch $sysinfo_fetch_args "$porcelain_continuum_url/$porcelain_continuum_script"
        if [[ $? == 0 ]]; then
            success=1
            break
        fi

        echo "Retrying in $wtime second(s)..."
        sleep $wtime
        count=$(( count + 1 ))
    done

    if [[ $success != 1 ]]; then
        echo "Dying..."
        exit 1
    fi
}

function porcelain_run_installer
{
    porcelain_verify

    bash $porcelain_continuum_script -b -p $PORCELAIN_DESTDIR
    if [[ $? > 0 ]]; then
        echo "Dying..."
        exit 1
    fi

    # do extraneous package installation
    porcelain_extra_install
}

function porcelain_extra_install
{
    if [[ -f $porcelain_extra_config ]]; then
        porcelain_verify

        while read pkg
        do
            if [[ $pkg == "" ]]; then
                continue
            elif [[ $pkg == "#"* ]]; then
                continue
            fi

            conda install -y -q $pkg
        done < $porcelain_extra_config
    fi
}

function porcelain_deinit
{
    if [[ $PORCELAIN_ALREADY_DEAD != 0 ]]; then
        return
    fi

    porcelain_verify

    echo "Deinitializing porcelain..."
    if [[ -d $PORCELAIN_PREFIX ]]; then
        echo "Removing $PORCELAIN_PREFIX"
        rm -rf "$PORCELAIN_PREFIX"
    fi
    export PORCELAIN_ALREADY_DEAD=1
}

# Given the chance this script could be killed in a number of different
# ways, resulting in files left behind... we need to be pretty damn
# thourough.
function porcelain_signal
{
    # Obtain last return value
    retval=$?

    # If error.sh recorded errors; use the count instead
    if [[ -n $_E_COUNT ]] && [[ $_E_COUNT > 0 ]]; then
        retval=$_E_COUNT
    fi

    # Already signaled, so die
    if [[ $PORCELAIN_SIGNALED != 0 ]]; then
        exit $retval
    fi

    # If error.sh has been activated; display error report
    if [[ -n $_E_FLAGS ]] && [[ $_E_FLAGS != 0 ]]; then
        echo '----'
        error_report
    fi

    export PORCELAIN_SIGNALED=1
    porcelain_deinit

    exit $retval
}