summaryrefslogtreecommitdiff
path: root/gear/client.py
blob: 17202cdd2f460f63a8d8d85ce83009fc74531850 (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
import gear
import sys
import json
import distbuild


# Artifact build states. These are used to loosely track the state of the
# remote cache.
UNBUILT = 'not-built'
BUILDING = 'building'
BUILT = 'built'

import logging
logging.basicConfig()

gear.Server()

class theController():
    def __init__(self):
        self.graph_client = aGearmanClient(self)
        self.artifact = None

    def _process_build_graph(self, packet):
        print "Decoding artifact received"
        self.artifact = distbuild.decode_artifact_reference(job.data[-1])
        print "Decoding artifact received done"
        # Mark everything as unbuilt to begin with. We'll query the actua
        # state from the cache in self._query_cache_state().
        def set_initial_state(artifact):
            artifact.state = UNBUILT
        print "Setting them as unbuilt"
        self._map_build_graph(self.artifact, set_initial_state)
        print "Setting them as unbuilt done"

    def _map_build_graph(self, artifact, callback, components=[]):
        """Run callback on each artifact in the build graph and return result.

        If components is given, then only look at the components given and
        their dependencies. Also, return a list of the components after they
        have had callback called on them.

        """
        result = []
        mapped_components = []
        done = set()
        if components:
            queue = list(components)
        else:
            queue = [artifact]
        while queue:
            a = queue.pop()
            if a not in done:
                result.append(callback(a))
                queue.extend(a.dependencies)
                done.add(a)
                if a in components:
                    mapped_components.append(a)
        return result, mapped_components


class aGearmanClient(gear.Client):
    def __init__(self, controller):
        super(aGearmanClient, self).__init__()
        self.controller = controller
        self.finished = False

    def handleWorkComplete(self, packet):
        job = super(aGearmanClient, self).handleWorkComplete(packet)
        print "workcomplete"
        self.controller._process_build_graph(packet)
        return job

    def handleWorkData(self, packet):
        job = super(aGearmanClient, self).handleWorkData(packet)
        print job.data[-1]
        job.data = []
        return job

    def handleWorkFail(self, packet):
        job = super(aGearmanClient, self).handleWorkFail(packet)
        print "workfail"
        return job

    def handleWorkException(self, packet):
        job = super(aGearmanClient, self).handleWorkException(packet)
        print "workexception"
        return job

    def handleDisconnect(self, job):
        job = super(aGearmanClient, self).handleDisconnect(job)
        print "disconnect"





controller = theController()
client = controller.graph_client
client.addServer('localhost')
client.waitForServer()  # Wait for at least one server to be connected
print "server connected"
#job = gear.Job("reverse", "test string")
build_graph_request = {}
build_graph_request['repo'] = "baserock:baserock/definitions"
build_graph_request['ref'] = "master"
build_graph_request['system'] = "systems/minimal-system-x86_64-generic.morph"

s=json.dumps(build_graph_request)

print "Json produced: "
print s

job = gear.Job("build-graph", s)
client.submitJob(job)

# loop so that client doesn't die
while True:
    import time
    time.sleep(2)