summaryrefslogtreecommitdiffstats
path: root/ipmi.sh
blob: 37b711c1d7e4f6fdd90b9fc9b2d77121ca35a92b (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
function configure {
    hosts=$1
    shift
    
    if [ -n "$1" -a -f "$itm/$1.yml" ]; then
        play="$1.yml"
        shift
    else
        play="install.yml"
    fi
    
    (
        cd $itm || { echo "ITM (Ansible scripts) are not found"; exit 1; }
#        ansible-playbook -i inventories/ipe.erb -l localhost,$hosts ssh.yml
        ansible-playbook --vault-password-file=.vault-pass -i inventories/ipe.erb -l $hosts $play "$@"
    )
}

function update {
    hosts=$1
    shift

    (
        cd $itm || { echo "ITM (Ansible scripts) are not found"; exit 1; }
        ANSIBLE_GATHER_SUBSET="!hardware" ansible-playbook --vault-password-file=.vault-pass -i inventories/ipe.erb -l $hosts "update.yml" "$@"
    )
}

function run {
    hosts=$1
    shift

    (
        cd $itm || { echo "ITM (Ansible scripts) are not found"; exit 1; }
        eval ansible all -b -u root --vault-password-file=.vault-pass -i inventories/ipe.erb -l $hosts --args="'$@'"
    )
}

function smipmi_cmd {
    echo "- Running: SMCIPMITool "
    echo "$@"
    /opt/smcipmi/SMCIPMITool "$@"
}

function smipmi {
    host=$1
    shift
    smipmi_cmd $host ADMIN '$ipepdv$' "$@"
}


function ipmi_cmd {
    echo -n "- Running: ipmitool "
    echo "$@"
    /usr/sbin/ipmitool "$@"
}

function ipmi {
    host=$1
    shift
    ipmi_cmd -H $host -U ADMIN -P $pass "$@"

}


function set_bootdev {
    host=$1
    device=${2:-disk}
    persistent=${3:-}

    ipmi $host chassis bootdev $device $persistent cons_redirect=enable verbose=default
    sleep $sleep

#    ipmi $host chassis bootparam set bootflag force_$device
#    sleep $sleep
}


    
function install {
    host=$1

#    Requires license
#    smipmi $host wsiso mount 192.168.26.172 /images/centos74-ands.iso

    # off is soft-off on compute2, so we need reset first.
    #ipmi $host power reset
    ipmi $host power off
    sleep 10
    set_bootdev $host cdrom 
    sleep $sleep
    ipmi $host power on
}

function boot {
    host=$1

    set_bootdev $host disk persistent
    ipmi $host power on
    sleep $sleep
}

function reboot {
    host=$1
    
    ipmi $host power off
    sleep 10

    set_bootdev $host disk persistent
    sleep $sleep

    ipmi $host power on
    sleep $sleep
}

function bios {
    host=$1
    
    ipmi $host power off
    sleep 10

    set_bootdev $host bios
    sleep $sleep

    ipmi $host power on
}



function status {
    host=$1
    
    ipmi $host power status | grep "off" &> /dev/null
    if [ $? -ne 0 ]; then echo 1; else echo 0; fi
}

function wait_off {
    host=$1
    
    on=1
    while [ 1 ]; do
        on=$(status $host)
        [ "$on" -eq 0 ] && break
        echo " - $host still running..."
        sleep 5
    done
}

function cmd {
    ipmi "$@"
}


if [[ "$1" =~ ^[0-9\-]+$ ]]; then
    IFS='-' read -ra range <<< "$1"

    if [ -n "${range[1]}" ]; then
        servers=$(seq ${range[0]} ${range[1]})
    else
        servers=$(seq ${range[0]} ${range[0]})
    fi
    shift
fi

source config.sh
pass=$(pass $passpath | head -n 1)


shift=1
if [ -z "$1" ]; then
    echo "$0 [#-#] <config|install|reboot|boot|wait>"
    echo "$0 [#] <cmd>"
    exit
elif [[ "$1" =~ set_bootdev ]]; then
    action="set_bootdev"
elif [[ "$1" =~ install ]]; then
    action="install"
elif [[ "$1" =~ reboot ]]; then
    action="reboot"
elif [[ "$1" =~ boot ]]; then
    action="boot"
elif [[ "$1" =~ bios ]]; then
    action="bios"
elif [[ "$1" =~ status ]]; then
    action="status"
elif [[ "$1" =~ wait ]]; then
    action="wait_off"
elif [[ "$1" =~ config ]]; then
    action="configure"
elif [[ "$1" =~ update ]]; then
    action="update"
elif [[ "$1" =~ run ]]; then
    action="run"
else
    shift=0
    action="cmd"
fi

if [ $shift -eq 1 ]; then
    shift
fi


if [ $action = "configure" -o $action = "update" -o $action = "run" ]; then
    list=$(echo $hip | sed -re 's/\s+/ /g' | tr ' ' ',')
    eval "$action" "$list" "$@"
    exit
fi

for ip in $iip; do
    eval "$action" "$ip" "$@"
done

if [ $action = "install" ]; then
    sleep 30
    for ip in $iip; do
        wait_off "$ip" "$@"
        set_bootdev "$ip" "$@"
#        boot "$iip" "$@"
    done
fi