summaryrefslogtreecommitdiffstats
path: root/func/minion/modules/netapp/common.py
blob: 9b318119bf11df552c6dfc36e835ac0676600b24 (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
import re
import subprocess 

SSH = '/usr/bin/ssh'
SSH_USER = 'root'
SSH_OPTS = '-o forwardagent=no'
class GenericSSHError(Exception): pass
class NetappCommandError(Exception): pass
class NetappMissingParam(Exception): pass

def ssh(host, cmdargs, input=None, user=SSH_USER):
    cmdline = [SSH, SSH_OPTS, "%s@%s" % (user, host)]
    cmdline.extend(cmdargs)

    cmd = subprocess.Popen(cmdline,
                           executable=SSH,
                           stdin=subprocess.PIPE,
                           stdout=subprocess.PIPE, 
                           stderr=subprocess.PIPE,
                           shell=False)

    (out, err) = cmd.communicate(input)
    
    if cmd.wait() != 0:
        raise GenericSSHError, err
    else:
        return out + err

def param_check(args, required):
    for r in required:
        if not args.has_key(r):
            raise NetappMissingParam, r

def check_output(regex, output):
    #strip newlines
    output = output.replace('\n', ' ')
    if re.search(regex, output):
        return True
    else:
        raise NetappCommandError, output