Newer
Older
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
#!/usr/bin/env python3
# Copyright 2020 David Garcia
# See LICENSE file for licensing details.
from apt.progress.base import InstallProgress
import logging
import os
import shutil
from jinja2 import Template
from ops.charm import CharmBase
from ops.framework import StoredState
from ops.main import main
from ops.model import (
MaintenanceStatus,
ActiveStatus,
# BlockedStatus,
)
from utils import (
service_stop,
service_restart,
install_apt,
shell,
)
# from typing import Dict, Any
logger = logging.getLogger(__name__)
APT_REQUIREMENTS = [
"firefox",
"mate-desktop", # 469 packages
"mate-applets",
"mate-applet-brisk-menu",
"mate-indicator-applet",
"mate-session-manager",
"mate-terminal",
"xrdp",
]
SNAP_INSTALLS = [
"code --classic",
]
POLKIT_TEMPLATE = "./templates/color.pkla"
POLKIT_PATH = "/etc/polkit-1/localauthority/50-local.d/color.pkla"
STARTWM_TEMPLATE = "./templates/startwm.sh"
STARTWM_PATH = "/etc/xrdp/startwm.sh"
# WM_COMMAND = "startxfce4" # xubuntu-desktop
# WM_COMMAND = "budgie-desktop" # budgie-desktop-environment
WM_COMMAND = "mate-session" # mate-desktop
class VirtualPCCharm(CharmBase, InstallProgress):
_stored = StoredState()
def __init__(self, *args):
super().__init__(*args)
InstallProgress.__init__(self)
self._stored.set_default()
# Basic hooks
self.framework.observe(self.on.install, self._on_install)
self.framework.observe(self.on.start, self._on_start)
self.framework.observe(self.on.stop, self._on_stop)
self.framework.observe(self.on.config_changed, self._on_config_changed)
self.framework.observe(self.on.update_status, self._on_update_status)
# Actions hooks
# Relations hooks
# Override InstallProgress to update our status
def status_change(self, pkg, percent, status):
message = str(int(percent)) + "% " + status
self.unit.status = MaintenanceStatus(message)
# Basic hooks
def _on_install(self, _):
self.unit.status = MaintenanceStatus("Installing apt packages")
install_apt(packages=APT_REQUIREMENTS, update=True, progress=self)
service_stop('xrdp')
self.unit.status = MaintenanceStatus("Installing snaps")
for snap in SNAP_INSTALLS:
shell("sudo snap install " + snap)
self.unit.status = MaintenanceStatus("Setting default display manager")
shell("echo /usr/sbin/lightdm | sudo tee /etc/X11/default-display-manager")
self.unit.status = MaintenanceStatus("Adding XRDP to ssl-cert group")
shell("sudo adduser xrdp ssl-cert")
self.unit.status = MaintenanceStatus("Generating Window Manager startup script")
with open(STARTWM_TEMPLATE, "r") as template:
content = Template(template.read()).render(command=WM_COMMAND)
with open(STARTWM_PATH, "w") as startwm:
startwm.write(content)
self.unit.status = MaintenanceStatus("Generating Polkit files")
with open(POLKIT_TEMPLATE, "r") as template:
content = Template(template.read()).render()
with open(POLKIT_PATH, "w") as polkit:
polkit.write(content)
self._stored.installed = True
def _on_start(self, _):
self.unit.status = MaintenanceStatus("Starting XRDP server")
service_restart('xrdp')
self._stored.started = True
self.unit.status = self._get_current_status()
def _on_stop(self, _):
service_stop('xrdp')
self._stored.started = False
self.unit.status = self._get_current_status()
def _on_config_changed(self, _):
self.unit.status = self._get_current_status()
def _on_update_status(self, _):
self.unit.status = self._get_current_status()
# Action hooks
# Relation hooks
# Private functions
def _get_current_status(self):
status_type = ActiveStatus
status_msg = ""
if self._stored.installed:
status_msg = "Ready"
return status_type(status_msg)
if __name__ == "__main__":
main(VirtualPCCharm)