edf-hpc / clara

Cluster Administration tools
Other
18 stars 12 forks source link

Use clustershell exec worker to run command #65

Open rezib opened 8 years ago

rezib commented 8 years ago

As explained in #63, I guess now that we're using clustershell 1.7 everywhere, we should better rely on it now to run commands such as ipmitool, using the exec worker, instead of using low-level subprocess python module directly.

Here is an example of usage of this worker:

#!/usr/bin/env python

import sys
import ConfigParser
from ClusterShell.Task import task_self
from ClusterShell.Worker.Exec import ExecWorker

conf = None

def read_conf():

    global conf

    conf = ConfigParser.ConfigParser()
    conf.read('/etc/slurm-llnl/pwmgt.conf')

def wake_nodes(hostlist):
    """Wake nodes through their BMC with IPMI"""

    ipmi_cmd = conf.get('ipmi', 'cmd')
    ipmi_prefix = conf.get('ipmi', 'prefix')
    ipmi_user = conf.get('ipmi', 'user')
    ipmi_user = conf.get('ipmi', 'password')

    cmd = "{cmd} -I lanplus -U {user} -H {prefix}%host chassis power s3wake" \
            .format(cmd=ipmi_cmd, user=ipmi_user, prefix=ipmi_prefix)
    task = task_self()
    worker = ExecWorker(hostlist,
                        handler=None,
                        command=cmd)
    task.schedule(worker)
    task.resume()

def main():
    """Main function"""
    read_conf()
    hostlist = sys.argv[1]
    wake_nodes(hostlist)

if __name__ == '__main__':
    main()