summaryrefslogtreecommitdiff
path: root/hooks/pg_gw_hooks.py
blob: 976071ead7114d0fd8cc0561cca2224be105a5dc (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
#!/usr/bin/python

# Copyright (c) 2015, PLUMgrid Inc, http://plumgrid.com

# The hooks of this charm have been symlinked to functions
# in this file.

import sys
from charmhelpers.core.host import service_running
from charmhelpers.core.hookenv import (
    Hooks,
    UnregisteredHookError,
    log,
    config,
    relation_set,
    status_set
)

from charmhelpers.fetch import (
    apt_install,
    configure_sources,
)

from pg_gw_utils import (
    register_configs,
    ensure_files,
    restart_map,
    stop_pg,
    determine_packages,
    load_iovisor,
    remove_iovisor,
    ensure_mtu,
    add_lcm_key,
    fabric_interface_changed,
    load_iptables,
    restart_on_change,
    restart_on_stop,
    director_cluster_ready,
    configure_pg_sources,
    configure_analyst_opsvm
)

hooks = Hooks()
CONFIGS = register_configs()


@hooks.hook()
def install():
    '''
    Install hook is run when the charm is first deployed on a node.
    '''
    status_set('maintenance', 'Executing pre-install')
    load_iptables()
    configure_sources(update=True)
    status_set('maintenance', 'Installing apt packages')
    pkgs = determine_packages()
    for pkg in pkgs:
        apt_install(pkg, options=['--force-yes'], fatal=True)
    load_iovisor()
    ensure_mtu()
    ensure_files()
    add_lcm_key()


@hooks.hook('plumgrid-relation-changed')
@restart_on_change(restart_map())
def plumgrid_changed():
    '''
    This hook is run when relation between plumgrid-gateway and
    plumgrid-director is made.
    '''
    if director_cluster_ready():
        ensure_mtu()
        configure_analyst_opsvm()
        CONFIGS.write_all()


@hooks.hook('plumgrid-relation-joined')
def gateway_node_joined(relation_id=None):
    '''
    This hook is run when relation between plumgrid-gateway and
    plumgrid-director is made.
    '''
    rel_data = {'gateway-peer': 'gateway-peer'}
    relation_set(relation_id=relation_id, **rel_data)


@hooks.hook('config-changed')
@restart_on_stop()
@restart_on_change(restart_map())
def config_changed():
    '''
    This hook is run when a config parameter is changed.
    It also runs on node reboot.
    '''
    charm_config = config()
    if charm_config.changed('lcm-ssh-key'):
        if add_lcm_key():
            log("PLUMgrid LCM Key added")
    if charm_config.changed('fabric-interfaces'):
        if not fabric_interface_changed():
            log("Fabric interface already set")
    if (charm_config.changed('install_sources') or
        charm_config.changed('plumgrid-build') or
        charm_config.changed('install_keys') or
            charm_config.changed('iovisor-build')):
        stop_pg()
        status_set('maintenance', 'Upgrading apt packages')
        if charm_config.changed('install_sources'):
            configure_pg_sources()
        configure_sources(update=True)
        pkgs = determine_packages()
        for pkg in pkgs:
            apt_install(pkg, options=['--force-yes'], fatal=True)
            remove_iovisor()
            load_iovisor()
    ensure_mtu()
    CONFIGS.write_all()


@hooks.hook('upgrade-charm')
@restart_on_change(restart_map())
def upgrade_charm():
    ensure_mtu()
    CONFIGS.write_all()


@hooks.hook('stop')
def stop():
    '''
    This hook is run when the charm is destroyed.
    '''
    stop_pg()


@hooks.hook('update-status')
def update_status():
    if service_running('plumgrid'):
        status_set('active', 'Unit is ready')
    else:
        status_set('blocked', 'plumgrid service not running')


def main():
    try:
        hooks.execute(sys.argv)
    except UnregisteredHookError as e:
        log('Unknown hook {} - skipping.'.format(e))


if __name__ == '__main__':
    main()