aboutsummaryrefslogtreecommitdiff
path: root/bigtop-packages/src/charm/zeppelin/layer-zeppelin/reactive/zeppelin.py
blob: 6d37fdccc6fb9db5d6df6fdcb5b236876347545b (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
153
154
155
156
157
158
159
160
161
162
163
164
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import hashlib

from charms.reactive import when, when_not
from charms.reactive import is_state, set_state, remove_state
from charmhelpers.core import hookenv
from charms.layer.apache_bigtop_base import get_package_version
from charms.layer.bigtop_zeppelin import Zeppelin
from charms.reactive.helpers import data_changed


@when('zeppelin.installed')
def update_status():
    hadoop_joined = is_state('hadoop.joined')
    hadoop_ready = is_state('hadoop.ready')
    hive_joined = is_state('hive.connected')
    hive_ready = is_state('hive.available')
    spark_joined = is_state('spark.joined')
    spark_ready = is_state('spark.ready')

    waiting_apps = []
    ready_apps = []
    # Check status of the hadoop plugin
    if hadoop_joined and not hadoop_ready:
        waiting_apps.append('hadoop')
    elif hadoop_ready:
        ready_apps.append('hadoop')

    # Check status of Hive
    if hive_joined and not hive_ready:
        waiting_apps.append('hive')
    elif hive_ready:
        ready_apps.append('hive')

    # Check status of Spark
    if spark_joined and not spark_ready:
        waiting_apps.append('spark')
    elif spark_ready:
        ready_apps.append('spark')

    # Set appropriate status based on the apps we checked above
    if waiting_apps:
        hookenv.status_set('waiting',
                           'waiting for: {}'.format(' & '.join(waiting_apps)))
    elif ready_apps:
        hookenv.status_set('active',
                           'ready with: {}'.format(' & '.join(ready_apps)))
    else:
        hookenv.status_set('active', 'ready')


@when('bigtop.available')
@when_not('zeppelin.installed')
def initial_setup():
    hookenv.status_set('maintenance', 'installing zeppelin')
    zeppelin = Zeppelin()
    zeppelin.install()
    zeppelin.setup_etc_env()
    zeppelin.open_ports()
    set_state('zeppelin.installed')
    update_status()
    # set app version string for juju status output
    zeppelin_version = get_package_version('zeppelin') or 'unknown'
    hookenv.application_version_set(zeppelin_version)


@when('zeppelin.installed')
@when('hadoop.ready')
@when_not('zeppelin.hadoop.configured')
def configure_hadoop(hadoop):
    zeppelin = Zeppelin()
    zeppelin.configure_hadoop()
    zeppelin.register_hadoop_notebooks()
    set_state('zeppelin.hadoop.configured')


@when('zeppelin.installed')
@when_not('hadoop.ready')
@when('zeppelin.hadoop.configured')
def unconfigure_hadoop():
    zeppelin = Zeppelin()
    zeppelin.remove_hadoop_notebooks()
    remove_state('zeppelin.hadoop.configured')


@when('zeppelin.installed', 'hive.ready')
def configure_hive(hive):
    hive_ip = hive.get_private_ip()
    hive_port = hive.get_port()
    hive_url = 'jdbc:hive2://%s:%s' % (hive_ip, hive_port)
    if data_changed('hive.connect', hive_url):
        hookenv.status_set('maintenance', 'configuring hive')
        zeppelin = Zeppelin()
        zeppelin.configure_hive(hive_url)
        set_state('zeppelin.hive.configured')
        update_status()


@when('zeppelin.installed', 'zeppelin.hive.configured')
@when_not('hive.ready')
def unconfigure_hive():
    hookenv.status_set('maintenance', 'removing hive relation')
    zeppelin = Zeppelin()
    zeppelin.configure_hive('jdbc:hive2://:')
    remove_state('zeppelin.hive.configured')
    update_status()


@when('zeppelin.installed', 'spark.ready')
def configure_spark(spark):
    master_url = spark.get_master_url()
    if data_changed('spark.master', master_url):
        hookenv.status_set('maintenance', 'configuring spark')
        zeppelin = Zeppelin()
        zeppelin.configure_spark(master_url)
        set_state('zeppelin.spark.configured')
        update_status()


@when('zeppelin.installed', 'zeppelin.spark.configured')
@when_not('spark.ready')
def unconfigure_spark():
    hookenv.status_set('maintenance', 'removing spark relation')
    zeppelin = Zeppelin()
    # Yarn / Hadoop may not actually be available, but that is the default
    # value and nothing else would reasonably work here either without Spark.
    zeppelin.configure_spark('yarn-client')
    data_changed('spark.master', 'yarn-client')  # ensure updated if re-added
    remove_state('zeppelin.spark.configured')
    update_status()


@when('zeppelin.started', 'client.notebook.registered')
def register_notebook(client):
    zeppelin = Zeppelin()
    for notebook in client.unregistered_notebooks():
        notebook_md5 = hashlib.md5(notebook.encode('utf8')).hexdigest()
        if zeppelin.register_notebook(notebook_md5, notebook):
            client.accept_notebook(notebook)
        else:
            client.reject_notebook(notebook)


@when('zeppelin.started', 'client.notebook.removed')
def remove_notebook(client):
    zeppelin = Zeppelin()
    for notebook in client.unremoved_notebooks():
        notebook_md5 = hashlib.md5(notebook.encode('utf8')).hexdigest()
        zeppelin.remove_notebook(notebook_md5)
        client.remove_notebook(notebook)