~lazypower/charms/trusty/nexentaedge-swift-gw/metadata-typo

« back to all changes in this revision

Viewing changes to charms/trusty/nedge-swift-gw/hooks/hooks.py

  • Committer: anton.skriptsov at nexenta
  • Date: 2015-11-11 22:13:28 UTC
  • Revision ID: anton.skriptsov@nexenta.com-20151111221328-78ddkdelwzq5e410
nedge-swift-gw initial

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
import glob
 
3
import os
 
4
import shutil
 
5
import sys
 
6
import tarfile
 
7
import subprocess
 
8
 
 
9
from charmhelpers.core import unitdata
 
10
from charmhelpers.core.hookenv import (
 
11
    log,
 
12
    ERROR,
 
13
    config,
 
14
    relation_ids,
 
15
    relation_id,
 
16
    related_units,
 
17
    relation_get,
 
18
    relation_set,
 
19
    remote_unit,
 
20
    remote_service_name,
 
21
    Hooks,
 
22
    is_leader,
 
23
    UnregisteredHookError,
 
24
    service_name,
 
25
    unit_public_ip,
 
26
    relation_clear,
 
27
    is_relation_made,
 
28
    local_unit
 
29
)
 
30
 
 
31
from charmhelpers.core.host import (
 
32
    umount,
 
33
    mkdir,
 
34
    cmp_pkgrevno,
 
35
    adduser, 
 
36
    add_user_to_group,
 
37
    service_restart
 
38
)
 
39
 
 
40
from charmhelpers.core.templating import (
 
41
    render
 
42
)
 
43
 
 
44
from configurationSteps.nedgeConfigurator import (
 
45
    NedgeBaseConfigurator,
 
46
    NedgeGatewayConfigurator
 
47
)
 
48
 
 
49
from configurationSteps.utils import (
 
50
    get_sid,
 
51
    configure_user,
 
52
    if_up,
 
53
    configure_replicast_interface,
 
54
    neadm_bundles_copy
 
55
)
 
56
 
 
57
hooks = Hooks()
 
58
hook_data = unitdata.HookData()
 
59
db = unitdata.kv()
 
60
 
 
61
@hooks.hook('install')
 
62
def install():
 
63
    neadm_bundles_copy()
 
64
    configure_user()
 
65
    log('Bundles has copied')
 
66
    replicast_eth = config('replicast_eth')
 
67
    configure_replicast_interface(replicast_eth)
 
68
 
 
69
    public_ip = unit_public_ip()
 
70
 
 
71
    log('replicast interface {} has been created'.format(replicast_eth))
 
72
    log('Node public ip is {}'.format(public_ip))
 
73
    environment = {  
 
74
        'node_public_ip'    :  public_ip,
 
75
        'replicast_eth'     :  replicast_eth,
 
76
        'profile'             : config('profile'),
 
77
        'nodocker'            : config('nodocker'),
 
78
        'exclude'             : config('exclude'),
 
79
        'reserved'            : config('reserved')
 
80
       }
 
81
 
 
82
    configurator = NedgeGatewayConfigurator(environment)
 
83
    if not configurator.configure():
 
84
        log('NEDGE swift gateway node configuration failed.', level=ERROR)
 
85
        sys.exit(1)
 
86
    return 
 
87
 
 
88
@hooks.hook('swift-gw-relation-joined')
 
89
def on_mgmt_joined():
 
90
 
 
91
    #send node sid to mgmt to create new service
 
92
    data = {'gw_info' : {'sid':get_sid(), 'unit_id':local_unit(), 'service_name':service_name()}}
 
93
    log("send gw_info:{} to MGMT".format(data))
 
94
    relation_set(relation_settings=data)
 
95
 
 
96
@hooks.hook('swift-gw-relation-departed')
 
97
def on_mgmt_departed():
 
98
    #mark service as detached
 
99
    context = db.get('swift-context', record=True)
 
100
    
 
101
    context['state'] = 'detached'
 
102
    log("context data is {}".format(context))
 
103
    db.set('swift-context', context)
 
104
 
 
105
@hooks.hook('swift-gw-relation-changed')
 
106
def on_swift_gateway_data_changed():
 
107
    #recive and parse message from mgmt
 
108
    context =  relation_get('swift-context')
 
109
    log("[SWIFT GW] data recived from MGMT: {}".format(context))
 
110
    if context:
 
111
        context = ast.literal_eval(context)
 
112
        if context['sid'] == get_sid() and context['state'] == 'incomplete':
 
113
 
 
114
            #include gw related data to context
 
115
            #context['nder-data']['nexenta_rest_address']  = unit_public_ip()
 
116
            #context['cinder-data']['nexenta_client_address']= unit_public_ip()
 
117
            context['state'] = 'complete'
 
118
 
 
119
            db.set('swift-context', context)
 
120
            for relid in relation_ids('cinder-nedge'):
 
121
                send_data_to_keystone(relid)
 
122
        else:
 
123
            log("WRONG data: {}".format(context))
 
124
 
 
125
 
 
126
@hooks.hook('identity-service-relation-joined')
 
127
def identity_joined(relid=None):
 
128
    log("keysone identity service joined")    
 
129
    # send data to keystone 
 
130
 
 
131
if __name__ == '__main__':
 
132
    try:
 
133
        with hook_data():
 
134
            hooks.execute(sys.argv)
 
135
    except UnregisteredHookError as e:
 
136
        log('Unknown hook {} - skipping.'.format(e))
 
137