-
Notifications
You must be signed in to change notification settings - Fork 2
/
unit.py
108 lines (96 loc) · 3.47 KB
/
unit.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
#!/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 basicunit import BasicUnit
from subordinateunit import SubordinateUnit
class Unit(BasicUnit):
issubordinate = False
def __init__(self, unitname, unitinfo, application):
"""
Create a Unit object with basic information from a unit object from a
juju status output
"""
# Setup the BasicUnit
BasicUnit.__init__(
self, unitname, unitinfo, application.model.controller
)
# Required Variables
self.application = application
if "machine" in unitinfo:
match = re.match(r"(\d+)\/(lx[cd]|kvm)\/(\d+)$", unitinfo["machine"])
if match:
self.machine = application.model.get_container(unitinfo["machine"])
else:
self.machine = application.model.get_machine(unitinfo["machine"])
else:
self.machine = None
# Handle Subordinate Charms if any
if "subordinates" in unitinfo:
for subunitname, subunitinfo in unitinfo["subordinates"].items():
self.subordinates[subunitname] = SubordinateUnit(
subunitname, subunitinfo, self
)
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 = []
notesstr = ", ".join(self.notes)
namestr = self.name
portsstr = ",".join(self.openports)
if self.machine:
machinename = self.machine.name
else:
machinename = "PENDING"
if self.leader:
namestr += "*"
if color:
row = [
namestr,
self.get_workloadstatus_color(),
self.get_jujustatus_color(),
machinename,
self.publicaddress,
portsstr,
self.message,
notesstr,
]
else:
row = [
namestr,
self.workloadstatus,
self.jujustatus,
machinename,
self.publicaddress,
portsstr,
self.message,
notesstr,
]
if include_model_name:
row.insert(0, self.application.model.name)
if include_controller_name:
row.insert(0, self.application.model.controller.name)
return row
def filter_dictionary(self, dictionary, key_filter):
return {
key: value
for (key, value) in dictionary.items()
if key_filter in key
}
def filter_subordinates(self, subunit_filter):
self.subordinates = self.filter_dictionary(
self.subordinates, subunit_filter
)