summaryrefslogtreecommitdiffstats
path: root/src/workflow/workflow_factory.py
blob: 1f4a28abf5227f83b0cce87f518b388733a16de9 (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
##############################################################################
# Copyright (c) 2018 Sawyer Bergeron, Parker Berberian, and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################


from workflow.booking_workflow import Booking_Resource_Select, SWConfig_Select, Booking_Meta
from workflow.resource_bundle_workflow import Define_Hardware, Define_Nets, Resource_Meta_Info
from workflow.sw_bundle_workflow import Config_Software, Define_Software, SWConf_Resource_Select
from workflow.snapshot_workflow import Select_Host_Step, Image_Meta_Step
from workflow.models import Repository, Confirmation_Step

import uuid

import logging
logger = logging.getLogger(__name__)


class BookingMetaWorkflow(object):
    workflow_type = 0
    color = "#0099ff"
    is_child = False


class ResourceMetaWorkflow(object):
    workflow_type = 1
    color = "#ff6600"


class ConfigMetaWorkflow(object):
    workflow_type = 2
    color = "#00ffcc"

class MetaStep(object):

    UNTOUCHED = 0
    INVALID = 100
    VALID = 200

    def set_invalid(self, message, code=100):
        self.valid = code
        self.message = message

    def set_valid(self, message, code=200):
        self.valid = code
        self.message = message

    def __init__(self, *args, **kwargs):
        self.short_title = "error"
        self.skip_step = 0
        self.valid = 0
        self.message = ""
        self.id = uuid.uuid4()

    def to_json(self):
        return {
            'title': self.short_title,
            'skip': self.skip_step,
            'valid': self.valid,
            'message': self.message,
        }

    def __str__(self):
        return "metastep: " + str(self.short_title)

    def __hash__(self):
        return hash(self.id)

    def __eq__(self, other):
        return self.id.int == other.id.int

    def __ne__(self, other):
        return self.id.int != other.id.int

class Workflow(object):
    def __init__(self, steps, metasteps, repository):
        self.repository = repository
        self.steps = steps
        self.metasteps = metasteps
        self.active_index = 0

class WorkflowFactory():
    booking_steps = [
        Booking_Resource_Select,
        SWConfig_Select,
        Booking_Meta,
    ]

    resource_steps = [
        Define_Hardware,
        Define_Nets,
        Resource_Meta_Info,
    ]

    config_steps = [
        SWConf_Resource_Select,
        Define_Software,
        Config_Software,
    ]

    snapshot_steps = [
        Select_Host_Step,
        Image_Meta_Step,
    ]

    def conjure(self, workflow_type=None, repo=None):
        workflow_types = [
            self.booking_steps,
            self.resource_steps,
            self.config_steps,
            self.snapshot_steps,
        ]

        steps = self.make_steps(workflow_types[workflow_type], repository=repo)
        meta_steps = self.metaize(steps=steps, wf_type=workflow_type)
        return steps, meta_steps

    def create_workflow(self, workflow_type=None, repo=None):
        steps, meta_steps = self.conjure(workflow_type, repo)
        c_step = self.make_step(Confirmation_Step, repo)
        metaconfirm = MetaStep()
        metaconfirm.short_title = "confirm"
        metaconfirm.index = len(steps)
        steps.append(c_step)
        meta_steps.append(metaconfirm)
        return Workflow(steps, meta_steps, repo)

    def make_steps(self, step_types, repository):
        steps = []
        for step_type in step_types:
            steps.append(self.make_step(step_type, repository))

        return steps

    def make_step(self, step_type, repository):
        iden = step_type.description + step_type.title + step_type.template
        return step_type(iden, repository)

    def metaize(self, steps, wf_type):
        meta_dict = []
        for step in steps:
            meta_step = MetaStep()
            meta_step.short_title = step.short_title
            meta_dict.append(meta_step)
            step.metastep = meta_step

        return meta_dict