-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathhypervisor.py
More file actions
56 lines (47 loc) · 1.6 KB
/
hypervisor.py
File metadata and controls
56 lines (47 loc) · 1.6 KB
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
#!/usr/bin/python
import os
import sys
import time
from fabric import api as fab
from fabric.api import *
# Set user/passwd for fabric ssh
env.user = 'root'
env.password = 'password'
env.forward_agent = True
env.disable_known_hosts = True
env.parallel = False
env.pool_size = 1
# Supress Fabric output by default, we will enable when needed
output['debug'] = False
output['running'] = False
output['stdout'] = False
output['stdin'] = False
output['output'] = False
output['warnings'] = False
# Class to talk to hypervisors
class hypervisor(object):
def __init__(self, ssh_user='root', threads=5):
self.ssh_user = ssh_user
self.threads = threads
self.DEBUG = 0
# Check if we are really offline
def check_offline(self, host):
print "Note: Waiting for " + host.name + " to go offline"
while os.system("ping -c 1 " + host.ipaddress + " 2>&1 >/dev/null") == 0:
# Progress indication
sys.stdout.write(".")
sys.stdout.flush()
time.sleep(5)
# Remove progress indication
sys.stdout.write("\033[F")
print "Note: Host " + host.name + " is now offline! "
time.sleep(120)
# Execute script on hypervisor
def exec_script_on_hypervisor(self, host, script):
script = script.split('/')[-1]
print "Note: Executing script '%s' on host %s.." % (script, host.name)
try:
with settings(show('output'), host_string=self.ssh_user + "@" + host.ipaddress):
return fab.run("bash /tmp/" + script)
except:
return False