~bbaqar/charms/trusty/neutron-api-plumgrid/temp

« back to all changes in this revision

Viewing changes to hooks/plumgrid-plugin-relation-changed

  • Committer: bbaqar at plumgrid
  • Date: 2015-08-09 15:32:44 UTC
  • Revision ID: bbaqar@plumgrid.com-20150809153244-y8r6mf93wr0tqgyn
Support added for metadata

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
# Copyright (c) 2015, PLUMgrid Inc, http://plumgrid.com
 
4
 
 
5
# The hooks of this charm have been symlinked to functions
 
6
# in this file.
 
7
 
 
8
import sys
 
9
 
 
10
from charmhelpers.core.hookenv import (
 
11
    Hooks,
 
12
    UnregisteredHookError,
 
13
    log,
 
14
)
 
15
 
 
16
from charmhelpers.core.host import (
 
17
    restart_on_change,
 
18
)
 
19
 
 
20
from charmhelpers.fetch import (
 
21
    apt_install,
 
22
    apt_update,
 
23
    configure_sources,
 
24
    apt_purge,
 
25
)
 
26
 
 
27
from neutron_plumgrid_utils import (
 
28
    determine_packages,
 
29
    register_configs,
 
30
    restart_map,
 
31
    ensure_files,
 
32
)
 
33
 
 
34
hooks = Hooks()
 
35
CONFIGS = register_configs()
 
36
 
 
37
 
 
38
@hooks.hook()
 
39
def install():
 
40
    '''
 
41
    Install hook is run when the charm is first deployed on a node.
 
42
    '''
 
43
    configure_sources()
 
44
    apt_update()
 
45
    apt_install(determine_packages(), options=['--force-yes'], fatal=True)
 
46
    ensure_files()
 
47
 
 
48
 
 
49
@hooks.hook('config-changed')
 
50
def config_changed():
 
51
    '''
 
52
    This hook is run when a config parameter is changed.
 
53
    It also runs on node reboot.
 
54
    '''
 
55
    stop()
 
56
    configure_sources()
 
57
    apt_update()
 
58
    apt_install(determine_packages(), options=['--force-yes'], fatal=True)
 
59
    ensure_files()
 
60
    CONFIGS.write_all()
 
61
 
 
62
 
 
63
@hooks.hook('neutron-plugin-api-relation-joined')
 
64
@hooks.hook('plumgrid-plugin-relation-changed')
 
65
@hooks.hook('container-relation-changed')
 
66
@restart_on_change(restart_map())
 
67
def relation_changed():
 
68
    '''
 
69
    This hook is run when relation between neutron-api-plumgrid and
 
70
    neutron-api or plumgrid-edge is made.
 
71
    '''
 
72
    ensure_files()
 
73
    CONFIGS.write_all()
 
74
 
 
75
 
 
76
@hooks.hook('stop')
 
77
def stop():
 
78
    '''
 
79
    This hook is run when the charm is destroyed.
 
80
    '''
 
81
    pkgs = determine_packages()
 
82
    for pkg in pkgs:
 
83
        apt_purge(pkg, fatal=False)
 
84
 
 
85
 
 
86
def main():
 
87
    try:
 
88
        hooks.execute(sys.argv)
 
89
    except UnregisteredHookError as e:
 
90
        log('Unknown hook {} - skipping.'.format(e))
 
91
 
 
92
 
 
93
if __name__ == '__main__':
 
94
    main()