-
Notifications
You must be signed in to change notification settings - Fork 2
/
application.py
290 lines (259 loc) · 9.5 KB
/
application.py
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python3
# This file is part of xjs a tool used to disply offline juju status
# Copyright 2019 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License version 3, as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
# SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
import re
from colors import Color
import pendulum
from unit import Unit
class Application:
column_names = [
"App",
"Version",
"Status",
"Scale",
"Charm",
"Store",
"Rev",
"OS",
"Series",
"Message",
"Notes",
]
def __init__(self, appname, appinfo="", model=""):
"""
Create an Application object with basic information from an application
object from a juju status output
"""
# Default Values
self.notes = []
self.units = {}
self.subordinates = {}
self.version = ""
self.message = ""
self.endpointbindings = {}
self.charmlatestrev = -1
self.exposed = ""
# Required Variables
self.name = appname
self.model = model
if "charm" in appinfo:
self.charm = appinfo["charm"]
if "series" in appinfo:
self.series = appinfo["series"]
else:
self.series = "NA"
if "os" in appinfo:
self.os = appinfo["os"]
else:
self.os = "NA"
isv1 = False
if "charm-origin" in appinfo:
self.charmorigin = appinfo["charm-origin"]
else:
self.charmorigin = "NA"
isv1 = True
if "charm-name" in appinfo:
self.charmname = appinfo["charm-name"]
else:
self.charmname = "NA"
isv1 = True
if "charm-rev" in appinfo:
self.charmrev = int(appinfo["charm-rev"])
else:
self.charmrev = -1
isv1 = True
# Handle v1 Charm info:
if isv1 and "charm" in appinfo:
match = re.match(r"(.*):(.*)-(\d+)", self.charm)
if match:
charmsource = match.group(1)
if charmsource == "cs":
self.charmorigin = "jujucharms"
else:
self.charmorigin = match.group(1)
self.charmname = match.group(2)
self.charmrev = int(match.group(3))
seriesmatch = re.match(
r"^(~[^/]+/)*([^/]+)/[^/]+", match.group(2)
)
if seriesmatch:
self.series = seriesmatch.group(2)
if "exposed" in appinfo:
self.exposed = appinfo["exposed"]
if "application-status" in appinfo:
statuskey = "application-status"
elif "service-status" in appinfo:
statuskey = "service-status"
else:
statuskey = "none"
if statuskey in appinfo and "current" in appinfo[statuskey]:
self.status = appinfo[statuskey]["current"]
else:
self.status = "NA"
# Required Dates
if statuskey in appinfo and "since" in appinfo[statuskey]:
if re.match(r".*Z$", appinfo[statuskey]["since"]):
appinfo[statuskey]["since"] = re.sub(r"Z$", "", appinfo[statuskey]["since"])
self.since = pendulum.from_format(
appinfo[statuskey]["since"],
"DD MMM YYYY HH:mm:ss",
tz="UTC",
)
else:
self.since = pendulum.from_format(
appinfo[statuskey]["since"], "DD MMM YYYY HH:mm:ssZ"
)
model.controller.update_timestamp(self.since)
# Optional Variables
if statuskey in appinfo:
if "message" in appinfo[statuskey]:
self.message = appinfo[statuskey]["message"]
if "version" in appinfo:
self.version = appinfo["version"]
if "endpoint-bindings" in appinfo:
self.endpointbindings = appinfo["endpoint-bindings"]
if "can-upgrade-to" in appinfo:
match = re.match(r"\D+(\d+)$", appinfo["can-upgrade-to"])
if match:
self.charmlatestrev = int(match.group(1))
self.canupgradeto = appinfo["can-upgrade-to"]
# Calculated Values
if self.exposed:
self.notes.append("exposed")
self.charmid = ""
if "charm" in appinfo:
match = re.match(r"(cs:~[^/]+)\/([^/]+/)*([^/]+)-\d+$", self.charm)
if match:
self.charmid = (
match.group(1) + "/" + self.series + "/" + match.group(3)
)
else:
match = re.match(r"cs:(.*)-\d+$", self.charm)
if match:
self.charmid = "cs:" + self.series + "/" + match.group(1)
if self.charmorigin != "jujucharms":
self.notes.append("Not from Charm Store")
# Handle Units
if "units" in appinfo:
for unitname, unitinfo in appinfo["units"].items():
unit = Unit(unitname, unitinfo, self)
self.units[unitname] = unit
def __dict__(self):
return {self.name: self}
def add_subordinate(self, subunit):
"""Add a subordinate relationship"""
self.subordinates[subunit.name] = subunit
def get_scale(self):
"""
Return the scale of an application which is the number of units and/or
subordinate units
"""
return len(self.units) + len(self.subordinates)
# TODO These colors should return a color not a string
def get_status_color(self):
"""Return a status string with correct colors based on status"""
if self.status == "active":
return Color.Fg.Green + self.status + Color.Reset
elif self.status in ("error", "blocked"):
return Color.Fg.Red + self.status + Color.Reset
elif self.status == "waiting":
return self.status
elif self.status == "maintenance":
return Color.Fg.Orange + self.status + Color.Reset
else:
return Color.Fg.Yellow + self.status + Color.Reset
def get_scale_color(self):
"""Return a scale string with correct colors based on scale"""
scale = self.get_scale()
if scale == 0:
return Color.Fg.Red + str(scale) + Color.Reset
else:
return str(scale)
def get_charmrev_color(self):
"""
Return a charm revision string with correct colors based on the
revision
"""
if self.charmlatestrev == -1:
return str(self.charmrev)
if self.charmrev < self.charmlatestrev:
return Color.Fg.Yellow + str(self.charmrev) + Color.Reset
elif self.charmrev == self.charmlatestrev:
return Color.Fg.Green + str(self.charmrev) + Color.Reset
else:
return Color.Fg.Red + str(self.charmrev) + Color.Reset
def get_charmorigin_color(self):
"""
Return a charm origin string with correct colors based on the origin
"""
if self.charmorigin != "jujucharms":
return Color.Fg.Yellow + self.charmorigin + Color.Reset
else:
return self.charmorigin
def get_row(
self, color, include_controller_name=False, include_model_name=False
):
"""Return a list which can be used for a row in a table."""
row = []
if color:
row = [
self.name,
self.version,
self.get_status_color(),
self.get_scale_color(),
self.charm,
self.get_charmorigin_color(),
self.get_charmrev_color(),
self.os,
self.series,
self.message,
", ".join(self.notes),
]
else:
row = [
self.name,
self.version,
self.status,
str(self.get_scale()),
self.charm,
self.charmorigin,
str(self.charmrev),
self.os,
self.series,
self.message,
", ".join(self.notes),
]
if include_model_name:
row.insert(0, self.model.name)
if include_controller_name:
row.insert(0, self.model.controller.name)
return row
def get_column_names(
self, include_controller_name=False, include_model_name=False
):
"""Append the controller name and/or model name as necessary"""
if include_model_name:
self.column_names.insert(0, "Model")
if include_controller_name:
self.column_names.insert(0, "Controller")
return self.column_names
def filter_dictionary(self, dictionary, key_filter):
return {
key: value
for (key, value) in dictionary.items()
if key_filter in key
}
def filter_units(self, unit_filter):
self.units = self.filter_dictionary(self.units, unit_filter)