summaryrefslogtreecommitdiffstats
path: root/Server/bkr/server/test/data_setup.py
blob: 5950812998d258e7e919fe98f2894adfa8cb7395 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Beaker
#
# Copyright (C) 2010 rmancy@redhat.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import logging
import re
import time
import datetime
import sqlalchemy
import turbogears.config, turbogears.database
from bkr.server.model import LabController, User, Group, Distro, Breed, Arch, \
        OSMajor, OSVersion, SystemActivity, Task, MachineRecipe, System, \
        SystemType, SystemStatus, Recipe, RecipeTask, RecipeTaskResult, \
        Device, TaskResult, TaskStatus, Job, RecipeSet, TaskPriority, \
        LabControllerDistro, Power, PowerType, TaskExcludeArch, TaskExcludeOSMajor, \
        Permission, RetentionTag, Product, Watchdog

log = logging.getLogger(__name__)

ADMIN_USER = u'admin'
ADMIN_PASSWORD = u'testing'
ADMIN_EMAIL_ADDRESS = u'admin@example.com'

def setup_model(override=True):
    from bkr.server.tools.init import init_db
    engine = turbogears.database.get_engine()
    db_name = engine.url.database
    connection = engine.connect()
    if override:
        log.info('Dropping database %s', db_name)
        connection.execute('DROP DATABASE IF EXISTS %s' % db_name)
    log.info('Creating database %s', db_name)
    connection.execute('CREATE DATABASE %s' % db_name)
    connection.invalidate() # can't reuse this one
    del connection
    log.info('Initialising model')
    init_db(user_name=ADMIN_USER, password=ADMIN_PASSWORD,
            user_email_address=ADMIN_EMAIL_ADDRESS)
    Product(name=u'the_product')

def create_labcontroller(fqdn=None):
    if fqdn is None:
        fqdn=u'lab-devel.rhts.eng.bos.redhat.com'
    try:
        lc = LabController.by_name(fqdn)  
    except sqlalchemy.exceptions.InvalidRequestError, e: #Doesn't exist ?
        if e.args[0] == 'No rows returned for one()':
            lc = LabController.lazy_create(fqdn=fqdn)
            return lc
        else:
            raise
    log.debug('labcontroller %s already exists' % fqdn)
    return lc

def create_user(user_name=None, password=None, display_name=None,
        email_address=None):
    if user_name is None:
        user_name = u'user%d' % int(time.time() * 100000)
    if display_name is None:
        display_name = user_name
    if email_address is None:
        email_address = u'%s@example.com' % user_name
    user = User(user_name=user_name, display_name=display_name,
            email_address=email_address)
    if password:
        user.password = password
    log.debug('Created user %r', user)
    return user

def add_system_lab_controller(system,lc): 
    system.lab_controller = lc

def create_group(permissions=None):
    # tg_group.group_name column is VARCHAR(16)
    suffix = str(int(time.time() * 1000))[-11:]
    group = Group(group_name=u'group%s' % suffix, display_name=u'Group %s' % suffix)
    if permissions:
        group.permissions.extend(Permission.by_name(name) for name in permissions)
    return group

def add_user_to_group(user,group):
    user.groups.append(group)

def add_group_to_system(system,group):
    system.groups.append(group)

def create_distro(name=u'DAN6-Server-U9', breed=u'Dan',
        osmajor=u'DansAwesomeLinuxServer6', osminor=u'9',
        arch=u'i386', method=u'http', virt=False, tags=None):
    install_name = u'%s-%d_%s-%s' % (name, int(time.time() * 1000), method, arch)
    distro = Distro(install_name=install_name)
    distro.name = name
    distro.method = method
    distro.breed = Breed.lazy_create(breed=breed)
    distro.virt = virt
    if tags:
        distro.tags.extend(tags)
    osmajor = OSMajor.lazy_create(osmajor=osmajor)
    try:
        distro.osversion = OSVersion.by_name(osmajor, osminor)
    except sqlalchemy.exceptions.InvalidRequestError:
        distro.osversion = OSVersion(osmajor, osminor, arches=[])
    distro.arch = Arch.by_name(arch)
    # make it available in all lab controllers
    for lc in LabController.query():
        distro.lab_controller_assocs.append(LabControllerDistro(lab_controller=lc))
    log.debug('Created distro %r', distro)
    return distro

def create_system(arch=u'i386', type=u'Machine', status=u'Automated',
        owner=None, fqdn=None, shared=False, **kw):
    if owner is None:
        owner = create_user()
    if fqdn is None:
        fqdn = u'system%d.testdata' % int(time.time() * 1000)
    if System.query().filter(System.fqdn == fqdn).count():
        raise ValueError('Attempted to create duplicate system %s' % fqdn)
    system = System(fqdn=fqdn,type=SystemType.by_name(type), owner=owner, 
                status=SystemStatus.by_name(status), **kw)
    system.shared = shared
    system.arch.append(Arch.by_name(arch))
    configure_system_power(system)
    system.date_modified = datetime.datetime.utcnow()
    log.debug('Created system %r', system)
    return system

def configure_system_power(system, power_type=u'ilo', address=None,
        user=None, password=None, power_id=None):
    if address is None:
        address = u'%s_power_address' % system.fqdn
    if user is None:
        user = u'%s_power_user' % system.fqdn
    if password is None:
        password = u'%s_power_password' % system.fqdn
    if power_id is None:
        power_id = '%d' % int(time.time() * 1000)
    system.power = Power(power_type=PowerType.by_name(power_type),
            power_address=address, power_id=power_id,
            power_user=user, power_passwd=password)

def create_system_activity(user=None, **kw):
    if not user:
        user = create_user()
    activity = SystemActivity(user, 'WEBUI', 'Changed', 'Loaned To', 'random_%d' % int(time.time() * 1000) , '%s' % user)
    return activity

def create_task(name=None, exclude_arch=[],exclude_osmajor=[]):
    if name is None:
        name = u'/distribution/test_task_%d' % int(time.time() * 1000)
    task = Task.lazy_create(name=name)
    if exclude_arch:
       [TaskExcludeArch(arch_id=Arch.by_name(arch).id, task_id=task.id) for arch in exclude_arch]
    if exclude_osmajor:
        for osmajor in exclude_osmajor:
            distro = create_distro(osmajor=osmajor) 
            TaskExcludeOSMajor(task_id=task.id, osmajor_id=distro.osversion.osmajor.id)
        
    return task

def create_tasks(xmljob):
    # Add all tasks that the xml specifies
    for recipeset in xmljob.iter_recipeSets():
        for recipe in recipeset.iter_recipes():
            for task in recipe.iter_tasks():
                create_task(name=task.name)

def create_recipe(system=None, distro=None, task_name=u'/distribution/reservesys',
        whiteboard=None):
    recipe = MachineRecipe(ttasks=1, system=system, whiteboard=whiteboard,
            distro=distro or Distro.query()[0])
    recipe._distro_requires=u'<distroRequires><and><distro_arch value="i386"  \
            op="="></distro_arch><distro_variant value="Workstation" op="="> \
            </distro_variant><distro_family value="RedHatEnterpriseLinux6" op="="> \
            </distro_family> </and><distro_virt value="" op="="></distro_virt> \
            </distroRequires>'
    recipe.tasks.append(RecipeTask(task=create_task(name=task_name)))
    return recipe

def create_job_for_recipes(recipes, owner=None, whiteboard=None, cc=None,product=None,
        retention_tag=None):
    if retention_tag is None:
        retention_tag = RetentionTag.get_default()
    if owner is None:
        owner = create_user()
    if whiteboard is None:
        whiteboard = u'job %d' % int(time.time() * 1000)
    job = Job(whiteboard=whiteboard, ttasks=1, owner=owner,retention_tag = retention_tag)
    if cc is not None:
        job.cc = cc
    recipe_set = RecipeSet(ttasks=sum(r.ttasks for r in recipes),
            priority=TaskPriority.default_priority())
    recipe_set.recipes.extend(recipes)
    job.recipesets.append(recipe_set)
    log.debug('Created %s', job.t_id)
    return job

def create_job(owner=None, cc=None, distro=None,product=None, 
        retention_tag=None, task_name=u'/distribution/reservesys', whiteboard=None,
        recipe_whiteboard=None, **kwargs):
    recipe = create_recipe(distro=distro, task_name=task_name,
            whiteboard=recipe_whiteboard)
    return create_job_for_recipes([recipe], owner=owner,
            whiteboard=whiteboard, cc=cc, product=product)

def create_completed_job(**kwargs):
    job = create_job(**kwargs)
    mark_job_complete(job, **kwargs)
    return job

def mark_job_complete(job, result=u'Pass', system=None, **kwargs):
    for recipe in job.all_recipes:
        if system is None:
            recipe.system = create_system(arch=recipe.arch)
        else:
            recipe.system = system
        for recipe_task in recipe.tasks:
            recipe_task.status = TaskStatus.by_name(u'Running')
        recipe.update_status()
        for recipe_task in recipe.tasks:
            rtr = RecipeTaskResult(recipetask=recipe_task,
                    result=TaskResult.by_name(result))
            recipe_task.status = TaskStatus.by_name(u'Completed')
            recipe_task.results.append(rtr)
        recipe.update_status()
    log.debug('Marked %s as complete with result %s', job.t_id, result)

def mark_job_waiting(job, user=None):
    if user is None:
        user = create_user()
    for recipeset in job.recipesets:
        for recipe in recipeset.recipes:
            recipe.process()
            recipe.queue()
            recipe.schedule()
            recipe.system = create_system()
            recipe.system.user = user
            recipe.watchdog = Watchdog(system=recipe.system)
            recipe.waiting()

def playback_task_results(task, xmltask):
    # Start task
    task.start()
    # Record Result
    task._result(xmltask.result,'/',0,'(%s)' % xmltask.result)
    # Stop task
    if xmltask.status == u'Aborted':
        task.abort()
    elif xmltask.status == u'Cancelled':
        task.cancel()
    else:
        task.stop()

def playback_job_results(job, xmljob):
    for i, xmlrecipeset in enumerate(xmljob.iter_recipeSets()):
        for j, xmlrecipe in enumerate(xmlrecipeset.iter_recipes()):
            for l, xmlguest in enumerate(xmlrecipe.iter_guests()):
                for k, xmltask in enumerate(xmlguest.iter_tasks()):
                    playback_task_results(job.recipesets[i].recipes[j].guests[l].tasks[k], xmltask)
            for k, xmltask in enumerate(xmlrecipe.iter_tasks()):
                playback_task_results(job.recipesets[i].recipes[j].tasks[k], xmltask)

def create_device(**kw):
    device = Device(**kw)