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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
import os
import json
import re
import sys
import subprocess
import time
import urllib.request as urllib2
import multiprocessing as mp
from charmhelpers.core import host
from charmhelpers.core.hookenv import (
open_port,
open_ports,
status_set,
config,
unit_public_ip,
unit_private_ip,
)
from charmhelpers.core.host import (
service_start,
service_stop,
log,
mkdir,
write_file,
)
from charmhelpers.fetch import (
apt_install,
apt_update,
apt_upgrade
)
from charms.reactive.helpers import (
mark_invoked,
was_invoked,
)
from charms.reactive import (
when,
when_not,
when_file_changed,
hook,
RelationBase,
scopes,
set_state,
remove_state
)
CONF_FILE = '/tmp';
#########################################################################
# Common functions
#########################################################################
def run_command(command=None):
if command is None:
return False;
log('Running Command "%s"' % command);
try:
return subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).decode('utf-8').replace('\n', '');
except subprocess.CalledProcessError as e:
log('Error running "%s" : %s' % (command, e.output));
return False;
def get_config(key):
conf = config(key);
return conf;
def retrieve(key):
try:
conf = open('/tmp/ovn_conf', 'r');
except:
return '';
plain_text = conf.read();
conf.close();
if plain_text == '':
return '';
else:
data = json.loads(plain_text);
return data[key];
def store(key, value):
conf = open('/tmp/ovn_conf', 'r');
plain_text = conf.read();
conf.close();
conf = open('/tmp/ovn_conf', 'w+');
data = {};
if plain_text != '':
data = json.loads(plain_text);
data[key] = value;
conf.truncate(0);
conf.seek(0, 0);
conf.write(json.dumps(data));
conf.close();
#########################################################################
# Relation Class
#########################################################################
class MasterConfigPeer(RelationBase):
scope = scopes.UNIT;
@hook("{peers:master-config}-relation-{joined}")
def joined(self):
conv = self.conversation();
conv.set_state("{relation_name}.connected");
@hook("{peers:master-config}-relation-{changed}")
def changed(self):
conv = self.conversation();
worker_id = conv.get_local(key='worker_id');
if worker_id != None and conv.get_remote(worker_id):
conv.set_state("{relation_name}.master.data.available");
elif conv.get_remote('cert_to_sign'):
conv.set_state("{relation_name}.worker.cert.available");
@hook("{peers:master-config}-relation-{departed}")
def departed(self):
conv = self.conversation();
conv.remove_state("{relation_name}.connected");
conv.remove_state("{relation_name}.master.data.available");
conv.remove_state("{relation_name}.worker.cert.available");
def set_worker_id(self, worker_id):
convs = self.conversations();
for conv in convs:
conv.set_local(key='worker_id', value=worker_id);
def get_worker_data(self):
convs = self.conversations();
final_data = [];
for conv in convs:
worker_unit = {};
cert = conv.get_remote('cert_to_sign');
worker_hostname = conv.get_remote('worker_hostname');
worker_unit['cert_to_sign'] = cert;
worker_unit['worker_hostname'] = worker_hostname;
final_data.append(worker_unit);
return final_data;
def send_worker_data(self, data):
convs = self.conversations();
for conv in convs:
conv.set_remote(data=data);
def send_signed_certs(self, certs):
convs = self.conversations();
for conv in convs:
for key, value in certs.items():
data_str = json.dumps(value);
conv.set_remote(key=key, value=data_str);
def get_signed_cert(self, worker_hostname):
convs = self.conversations();
final = None;
for conv in convs:
data = conv.get_remote(worker_hostname);
if data != '' and data != None:
data = json.loads(data);
final = data;
break;
return final;
|