-
Notifications
You must be signed in to change notification settings - Fork 0
/
beakerclient.py
164 lines (129 loc) · 5.77 KB
/
beakerclient.py
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
import logging
import os
import time
import fabric
import warnings
from fabric.api import env, run
from wait_for import wait_for
from propertyreader import Properties
properties = Properties()
if properties.logslevel.level == 'INFO':
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(name)s - %(levelname)s - %(message)s')
if properties.logslevel.level == 'DEBUG':
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s %(name)s - %(levelname)s - %(message)s')
warnings.filterwarnings('ignore')
class Client(object):
def __init__(self, host=None):
self.host = host
self.username = properties.beaker.username
self.password = properties.beaker.password
self.hub = properties.beaker.hub
self.logger = logging.getLogger('system')
env.host_string = properties.beaker.env_host_string
env.user = properties.beaker.env_username
env.password = properties.beaker.env_password
env.output_prefix = True
fabric.state.output['running'] = True if properties.logslevel.level == 'DEBUG' else False
fabric.state.output['output'] = True if properties.logslevel.level == 'DEBUG' else False
def setupBkrClient(self):
command = "/usr/bin/bkr whoami;date"
job = run(command, shell=False)
if job.count("username") == 1:
logging.info("Beaker client already configured!")
return job.count("username")
else:
myid = run("id -u", shell=False)
if int(myid) == 0:
run("echo -e '"
"[beaker]\n"
"name=Beaker\n"
"baseurl='http://download.eng.bos.redhat.com/beakerrepos/client"
"/RedHatEnterpriseLinux7Client'\n"
"enabled=1\n"
"gpgcheck=0'>/etc/yum.repos.d/beaker_client.repo",
shell=False)
config_auth = 'AUTH_METHOD="password"'
config_username = 'USERNAME="{}"'.format(self.username)
config_password = 'PASSWORD="{}"'.format(self.password)
config_hub = 'HUB_URL="{}"'.format(self.hub)
run("mkdir ~/.beaker_client; echo -e '{}\n{}\n{}\n{}\n'> ~/.beaker_client/config"
.format(config_hub, config_auth, config_username, config_password),
shell=False)
run("subscription-manager repos --enable=rhel-7-server-optional-rpms &&"
"yum clean all && yum install -y rhts-devel beaker{,lib}-redhat nmap-ncat",
shell=False)
job = run(command, shell=False)
if job.count("username") == 1:
logging.info("Beaker client has been configured!")
return job.count("username")
else:
logging.error(
"Beaker client setup failed!, please check manually or try again.")
else:
return int(myid)
def randomSystem(self):
filepath = os.getcwd()+'/files/system.xml'
job = run("/usr/bin/bkr job-submit --xml {}".format(filepath), shell=False)
location = job.find('Submitted')
if job.succeeded:
logging.info("Job {} has been submitted".format(job[location::].rsplit()[1][2:-2]))
return job[location::].rsplit()[1][2:-2]
def theSystem(self, hostname, distrotree):
job = run("/usr/bin/bkr system-provision --distro-tree={} {} "
.format(distrotree, hostname), shell=False)
if job.succeeded:
logging.info("Provision job has been submitted to beaker.")
return "Submitted"
def _checkSSH(self, hostname):
job = run("echo 'QUIT'| nc -v {} 22 -w 5;date".format(hostname), shell=False)
count = job.count('Connected to')
logging.info("Still awaiting to reachable on SSH Port.")
return count
def systemConnect(self, hostname):
logging.info("Awaiting {} to reachable on SSH port!".format(hostname))
time.sleep(60)
wait_for(
lambda: self._checkSSH(hostname=hostname) == 1,
timeout=1000,
delay=60,
logger=self.logger
)
if self._checkSSH(hostname=hostname) == 1:
logging.info("{} is reachable on SSH port".format(hostname))
return "Up"
else:
logging.error("{} is not reachable on SSH port, Please check.".format(hostname))
def _cstatus(self, jobID):
rstatus = None
hostname = None
jobcheck = run("/usr/bin/bkr job-results {}".format(jobID), shell=False)
logging.info("This may take sometime, awaiting system to build ..")
if 'system value=' in jobcheck:
istart = jobcheck.find('system value=')
iend = jobcheck.find('/></role></roles><repos/><distroRequires>')
hostname = jobcheck[istart:iend].split('=')[1].split('"')[1]
for status in ['Cancelled', 'Running']:
if status in jobcheck:
rstatus = status
return [rstatus, hostname]
def jobStatus(self, jobID):
wait_for(
lambda: self._cstatus(jobID=jobID)[0] == 'Running',
timeout=6000,
delay=60,
logger=self.logger
)
status = self._cstatus(jobID=jobID)
logging.info(status)
return status
def contentHost(self):
filepath = os.getcwd()+'/files/contenthost.xml'
job = run("/usr/bin/bkr job-submit --xml {}".format(filepath), shell=False)
location = job.find('Submitted')
if job.succeeded:
logging.info("Job {} has been submitted".format(job[location::].rsplit()[1][2:-2]))
return job[location::].rsplit()[1][2:-2]