~1chb1n/+junk/jenkins-reactive-temp-build

« back to all changes in this revision

Viewing changes to hooks/relations/jenkins-extension/provides.py

  • Committer: Ryan Beisner
  • Date: 2016-08-27 05:52:07 UTC
  • Revision ID: ryan.beisner@canonical.com-20160827055207-x310qr154yorsr4r
Add temp build of jenkins charm from https://github.com/freeekanayaka/layer-jenkins

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
from charmhelpers.core.hookenv import (
 
4
    log,
 
5
    relation_ids,
 
6
    related_units,
 
7
    relation_get,
 
8
    relation_set,
 
9
    unit_get,
 
10
)
 
11
 
 
12
from charms.reactive import RelationBase
 
13
from charms.reactive import hook
 
14
from charms.reactive import scopes
 
15
 
 
16
from charms.layer.jenkins.credentials import Credentials
 
17
from charms.layer.jenkins.plugins import Plugins
 
18
from charms.layer.jenkins.api import Api
 
19
 
 
20
 
 
21
class JenkinsMaster(RelationBase):
 
22
    scope = scopes.GLOBAL
 
23
 
 
24
    @hook("{provides:jenkins-extension}-relation-{joined}")
 
25
    def joined(self):
 
26
        """Indicate the relation is connected and transmit our credentials."""
 
27
        log("Updating extension interface with up-to-date data.")
 
28
 
 
29
        # Fish out the current zuul address from any relation we have.
 
30
        zuul_address = None
 
31
        for rid in relation_ids("zuul"):
 
32
            for unit in related_units(rid):
 
33
                zuul_address = relation_get(
 
34
                    rid=rid, unit=unit, attribute="private-address")
 
35
                break
 
36
 
 
37
        credentials = Credentials()
 
38
        for rid in relation_ids("extension"):
 
39
            relation_settings = {
 
40
                "admin_username": credentials.username(),
 
41
                "admin_password": credentials.password(),
 
42
                "jenkins_url": "http://%s:8080" % unit_get("private-address"),
 
43
                "jenkins-admin-user": credentials.username(),
 
44
                "jenkins-token": credentials.token(),
 
45
            }
 
46
            relation_set(relation_id=rid, relation_settings=relation_settings)
 
47
            if zuul_address:
 
48
                relation_set(relation_id=rid, zuul_address=zuul_address)
 
49
 
 
50
        self.set_state("{relation_name}.connected")
 
51
 
 
52
    @hook("{provides:jenkins-extension}-relation-{changed}")
 
53
    def changed(self):
 
54
        """Install optional plugins."""
 
55
        # extension subordinates may request the principle service install
 
56
        # specified jenkins plugins
 
57
        log("Installing required plugins as requested by jenkins-extension "
 
58
            "subordinate.")
 
59
        plugins = Plugins()
 
60
        plugins.install(relation_get("required_plugins"))
 
61
 
 
62
        api = Api()
 
63
        api.wait()  # Wait for the service to be fully up