aboutsummaryrefslogtreecommitdiff
blob: 839e41c3481bb04389ba6361dcf2e997045e9a73 (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/bin/bash

source /etc/init.d/functions.sh
#[ -f /etc/empi.conf ] && source /etc/empi.conf
MPI_IMPS="openmpi lam-mpi"
MPI_ALL_IMPS="mpich mpich2 openmpi lam-mpi"
VERBOSE=0

[[ -z ${MPI_PORTDIR} ]] \
    && MPI_PORTDIR="$(portageq portdir_overlay)" \
    && MPI_PORTDIR="${MPI_PORTDIR%% *}" \

[[ -z ${COMPILER} ]] && COMPILER=gcc
# imp (category)        Implementation we're installing for.  mpi-*
# targets               Packages to run action on.  When creating, the implementation to use.
# action                create, add, update, remove.

die(){
    if [ -n "${1}" ]; then
        echo; eerror $1; echo
    fi
    exit 1
}

usage(){
    local rc=${1:-0}
    shift
cat <<-EOF
Usage: ${HILITE}empi${NORMAL} ${GOOD}[options]${NORMAL} ${BRACKET}--implementation IMPLEMENTATION pkgspec${NORMAL} 

Options:
    ${GOOD}-c, --create${NORMAL}                (Re)Initialize setup for implementation.
    ${GOOD}-a, --add${NORMAL} pkgspec(s)        Add packages using specified implementation.
    ${GOOD}-i, --implementation${NORMAL} imp    Implementation to use.
    ${GOOD}-l, --list${NORMAL}                  List installed implementations.
    ${GOOD}-t, --tree${NORMAL} path             Path to portage tree to use ebuilds from.
    ${GOOD}-d, --delete${NORMAL} imp            Remove everything related to specified implementation.

pkgspec is specified by a package string.  Without a version, the best_visible is used.
    openmpi, sys-cluster/openmpi, =sys-cluster/openmpi-1.2.5
implementation (-i) is user defined but must be prefixed with "mpi-"

EOF
    [[ -n $* ]] && echo && eerror "Error: $*"
    exit ${rc}
}

imp_is_valid() {
    [[ -z ${imp} ]] && usage 1 "No implementation defined."
    [[ ${imp} != mpi-* ]] && usage 1 "Implementations must be prefixed with mpi-"
    [[ ${imp//./} != ${imp} ]] && usage 1 "Implementations cannot contain . (period)"
}

is_imp_category() {
    local i
    for i in $(eselect mpi list -p); do
        [[ ${1} == ${i} ]] && return 0
    done
    return 1
}

split_atom() {
    local cpv c pf pn pv
    cpv=$(portageq best_visible / ${1})
    if [[ -z ${cpv} || ${rc} -ne 0 ]]; then
        cpv=$(portageq best_visible / =${1})
        [[ -z ${cpv} || ${rc} -ne 0 ]] && return 1
    fi
    c=${cpv%/*}; pf=${cpv#${c}/}; pn=${pf%%-[0-9]*}; pv=${pf#${pn}-}
    echo "${c} ${pn} ${pv}"
}

parse_pkgspecs() {
    local atom i
    for ((i=0; i<${#targets[@]}; i++)); do
        atom=($(split_atom ${targets[i]}))
        if [[ $? -ne 0 ]]; then
            eerror "Unable to find a unique package or valid version for ${targets[i]}"
            eerror "Is the package unmasked and unblocked normally?"
            die ""
        fi
        targets[i]=${atom[0]}/${atom[1]}-${atom[2]}
    done
}

# handle_etc_portage package_spec
# parses /etc/portage/package.{keywords,use}.  If ${imp}/${pn} is seen, we don't
# do a thing.  Otherwise copy any lines that have ${cat}/${pn} inserting them again
# with the new category.
handle_etc_portage() {
    local atom=( $(split_atom ${1}) )
    local ext line gfiles f
    
    for ext in "keywords" "use"; do
        if [ -d /etc/portage/package.${ext} ]; then
            gfiles="/etc/portage/package.${ext}/*"
            f=/etc/portage/package.${ext}/${imp}
        else
            gfiles="/etc/portage/package.${ext}"
            f=/etc/portage/package.${ext}
        fi

        if ! grep "[>=<]*${imp}/${atom[1]}" ${gfiles} &>/dev/null; then
            grep "[>=<]*${atom[0]}/${atom[1]}" ${gfiles} \
                | sed "s,${atom[0]},${imp}," \
            | while read line; do
               echo "${line}" >> ${f}
                [[ ${VERBOSE} -ne 0 ]] \
                    && einfo "Addition to ${f}: ${line}"
            done
        elif [[ ${VERBOSE} -ne 0 ]]; then
            ewarn "Keys for ${imp}/${atom[1]} already exist in ${f}.  Will not replicate them."
        fi
    done
}
            


get_ebuild_dir() {
    local d a
    local want_uses_mpi=${2:-0}
    local found=0

    a=($(split_atom ${1}))
    [[ $? -ne 0 ]] && die "Unable to find a unique package or valid version for ${1}."
    is_imp_category ${a[0]} && die "It makes no sense to build a new mpi-implementation from a current one."

    if [[ -z ${portage_tree} ]]; then
        for d in $(portageq portdir_overlay) $(portageq portdir); do
            if [[ ${want_uses_mpi} -ne 0 ]]; then
                [[ -f "${d}/${a[0]}/${a[1]}/${a[1]}-${a[2]}.ebuild" ]] \
                    && ebuild_uses_mpi ${d}/${a[0]}/${a[1]} ${a[1]}-${a[2]} \
                    && found=1
            else
                [[ -f "${d}/${a[0]}/${a[1]}/${a[1]}-${a[2]}.ebuild" ]] && found=1
            fi
            [[ ${found} -ne 0 ]] && break
        done
        if [[ ${found} -ne 0 ]]; then
            portage_tree=${d}
        else
            die "Could not find an ebuild for ${a[0]}/${a[1]}-${a[2]}."
        fi
    fi
    
    ebuild_dir="${portage_tree}/${a[0]}/${a[1]}"
}

ebuild_uses_mpi() {
    grep 'inherit .*mpi' "${1}/${2##*/}.ebuild" &>/dev/null
}

setup_portage_vars() {
    export PORTDIR_OVERLAY="${MPI_PORTDIR} $(portageq portdir_overlay)"
    export PKGDIR="$(portageq envvar PKGDIR)/mpi/${imp}"
}

link_ebuild_dir() {
    ln -snf "${ebuild_dir}" "${MPI_PORTDIR}"/${imp}/${ebuild_dir##*/} \
        || die "Failed to link ${ebuild_dir} to ${MPI_PORTDIR}/${imp}/${ebuild_dir##*/}"
}

do_emerge() {
    ebegin "Emerging $*"
    setup_portage_vars
    emerge ${emerge_opts} $* || die "emerge failed!"
    eend 
}

# We should have only one target here.
create_implementation() {
    local mpi_imp_pkg d mpi_imp_pn

    [[ ${#targets[@]} -ne 1 ]] && die "Can only create one implementation at a time."
    
    # Prevent laziness
    [[ ${targets[0]} == ${targets[0]##*/} ]] \
        && targets[0]="sys-cluster/${targets[0]}"
 
    parse_pkgspecs 
    get_ebuild_dir ${targets[0]} 1
    mpi_imp_pn=${ebuild_dir##*/}
    mpi_imp_pkg=${targets[0]}
    handle_etc_portage ${targets[0]}
    targets[0]="=${imp}/${targets[0]##*/}"

    # Refuse to break systems.  If there is already an implementation
    # installed in that directory, we're not going to add another one as
    # the eclass doesn't fix one problem just to introduce a bigger one.
    for d in $(find ${MPI_PORTDIR}/${imp} -maxdepth 1 -mindepth 1 -type l);do
        d=${d##*/}
        [[ ${d} == ${mpi_imp_pn} ]] && continue
        for i in ${MPI_ALL_IMPS}; do
            [[ ${i} == ${d} ]] \
                && die "${imp} already has MPI implementation ${d}, refusing to add ${mpi_imp_pn}"
        done
    done

    if [[ -d "${MPI_PORTDIR}"/${imp} ]]; then
        [[ ${VERBOSE} -ne 0 ]] && ewarn "${imp} has already been created."
    else
        mkdir -p ${MPI_PORTDIR}/${imp}
        link_ebuild_dir
    fi
    if ! grep "^${imp}$" /etc/portage/categories &>/dev/null; then
        echo "${imp}" >> /etc/portage/categories
    fi


cat << EOF
Creating ${HILITE}${imp}${NORMAL}
    Implementation:     ${GOOD}${imp}${NORMAL}
    MPI Package:        ${GOOD}${mpi_imp_pkg}${NORMAL}
    Source:             ${GOOD}${ebuild_dir}${NORMAL}
    Destination:        ${GOOD}${MPI_PORTDIR}/${imp}${NORMAL}
EOF
    do_emerge ${targets[0]}
}


add_packages(){
    local i j deps

    [[ -d "${MPI_PORTDIR}"/${imp} ]] || die "Implementation ${imp} has not been created yet."
    [[ ${#targets[@]} -lt 1 ]] && die "You need to specify at least one package"
    
    parse_pkgspecs
    for ((i=0;i<${#targets[@]};i++)); do
        get_ebuild_dir ${targets[i]}
        if ebuild_uses_mpi ${ebuild_dir} ${targets[i]}; then
            link_ebuild_dir
            handle_etc_portage ${targets[i]}
            targets[i]="=${imp}/${targets[i]##*/}"
        else
            targets[i]="=${targets[i]}"
        fi

        # I don't know about this, but do you have a better idea?
        deps="$(emerge --color=n --onlydeps -p --quiet ${targets[i]})"
        if [[ $? -ne 0 ]]; then
            emerge --onlydeps -p ${targets[i]}
            die "Unable to calculate deps for ${targets[i]}"
        fi
        deps=( $(echo ${deps} | sed -e 's:\[[a-z]* [A-Z] \] :=:g') )
        for ((j=0;j<${#deps[@]};j++)); do
            get_ebuild_dir ${deps[j]}
            if ebuild_uses_mpi ${deps[i]}; then
                link_ebuild_dir
            fi
        done
    done
cat << EOF
Adding packages to ${HILIGHT}${imp}${NORMAL}
    Packages:       ${GOOD}${targets[@]}${NORMAL}
EOF
    do_emerge ${targets[@]}
}


delete_implementation() {
    local pkgs=( $(ls /var/db/pkg/${imp}/) )
    local ext d i
    [[ -d "${MPI_PORTDIR}"/${imp} ]] || die "Implementation ${imp} has not been created yet."
   
    for (( i=0; i<${#pkgs[@]}; i++)); do
        pkgs[i]="=${imp}/${pkgs[i]}"
    done

    if ! emerge -C ${emerge_opts/-u/} ${pkgs[@]}; then
        die "Failed to unmerge ${pkgs[@]}"
    fi

    for ext in "keywords" "use"; do
        if [ -d /etc/portage/package.${ext} ]; then
            rm /etc/portage/package.${ext}/${imp} &>/dev/null
        else
            sed -i -e "/${imp}\//d" /etc/portage/package.${ext}
        fi
    done
    sed -i -e "/^${imp}$/d" /etc/portage/categories

    for d in $(ls "${MPI_PORTDIR}"/${imp}/); do
        rm "${MPI_PORTDIR}"/${imp}/${d}
    done
    rmdir "${MPI_PORTDIR}"/${imp}
    rmdir /var/db/pkg/${imp}
}


[[ ${UID} -ne 0 ]] && die "You must be root."
targets=""
emerge_opts="-u -a -v"
portage_tree=""
action=""
while [[ $# -gt 0 ]]; do
    case $1 in
        -h|--help)
            usage;;
        -c|--create)
            action="${action}create";;
        -a|--add)
            action="${action}add";;
        -d|--delete)
            action="${action}delete"
            shift; imp=${1};;
        -i|--implementation)
            shift; imp=${1};;
        -t|--tree)
            shift; portage_tree=${1};;
        -v|--verbose)
            VERBOSE=1;;
        -*)
            emerge_opts="${emerge_opts} ${1}";;
        *)
            targets=( $(echo ${targets[@]}) ${1} );;
    esac
    shift
done

[[ -z ${action} ]] && usage 1 "No action defined."
[[ -z ${imp} ]] && usage 1 "No implementation defined."
imp_is_valid

[[ ${action} == *create* ]] && create_implementation
[[ ${action} == *add* ]]    && add_packages
[[ ${action} == *delete* ]] && delete_implementation