]> glassweightruler.freedombox.rocks Git - waydroid.git/blob - tools/__init__.py
app_manager: Allow install/remove/list while frozen
[waydroid.git] / tools / __init__.py
1 # Copyright 2021 Oliver Smith
2 # SPDX-License-Identifier: GPL-3.0-or-later
3 # PYTHON_ARGCOMPLETE_OK
4 import sys
5 import logging
6 import os
7 import traceback
8 import dbus.mainloop.glib
9 import dbus
10 import dbus.exceptions
11
12 from . import actions
13 from . import config
14 from . import helpers
15 from .helpers import logging as tools_logging
16
17
18 def main():
19 def actionNeedRoot(action):
20 if os.geteuid() != 0:
21 raise RuntimeError(
22 "Action \"{}\" needs root access".format(action))
23
24 # Wrap everything to display nice error messages
25 args = None
26 try:
27 os.umask(0o000)
28 # Parse arguments, set up logging
29 args = helpers.arguments()
30 args.cache = {}
31 args.work = config.defaults["work"]
32 args.config = args.work + "/waydroid.cfg"
33 args.log = args.work + "/waydroid.log"
34 args.sudo_timer = True
35 args.timeout = 1800
36
37 if os.geteuid() == 0:
38 if not os.path.exists(args.work):
39 os.mkdir(args.work)
40 elif not os.path.exists(args.log):
41 args.log = "/tmp/tools.log"
42
43 tools_logging.init(args)
44
45 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
46 dbus.mainloop.glib.threads_init()
47 dbus_name_scope = None
48
49 if not actions.initializer.is_initialized(args) and \
50 args.action and args.action not in ("init", "first-launch", "log"):
51 if args.wait_for_init:
52 try:
53 dbus_name_scope = dbus.service.BusName("id.waydro.Container", dbus.SystemBus(), do_not_queue=True)
54 actions.wait_for_init(args)
55 except dbus.exceptions.NameExistsException:
56 print('ERROR: WayDroid service is already awaiting initialization')
57 return 1
58 else:
59 print('ERROR: WayDroid is not initialized, run "waydroid init"')
60 return 0
61
62 # Initialize or require config
63 if args.action == "init":
64 actionNeedRoot(args.action)
65 actions.init(args)
66 elif args.action == "upgrade":
67 actionNeedRoot(args.action)
68 actions.upgrade(args)
69 elif args.action == "session":
70 if args.subaction == "start":
71 actions.session_manager.start(args)
72 elif args.subaction == "stop":
73 actions.session_manager.stop(args)
74 else:
75 logging.info(
76 "Run waydroid {} -h for usage information.".format(args.action))
77 elif args.action == "container":
78 actionNeedRoot(args.action)
79 if args.subaction == "start":
80 if dbus_name_scope is None:
81 try:
82 dbus_name_scope = dbus.service.BusName("id.waydro.Container", dbus.SystemBus(), do_not_queue=True)
83 except dbus.exceptions.NameExistsException:
84 print('ERROR: WayDroid container service is already running')
85 return 1
86 actions.container_manager.start(args)
87 elif args.subaction == "stop":
88 actions.container_manager.stop(args)
89 elif args.subaction == "restart":
90 actions.container_manager.restart(args)
91 elif args.subaction == "freeze":
92 actions.container_manager.freeze(args)
93 elif args.subaction == "unfreeze":
94 actions.container_manager.unfreeze(args)
95 else:
96 logging.info(
97 "Run waydroid {} -h for usage information.".format(args.action))
98 elif args.action == "app":
99 if args.subaction == "install":
100 actions.app_manager.install(args)
101 elif args.subaction == "remove":
102 actions.app_manager.remove(args)
103 elif args.subaction == "launch":
104 actions.app_manager.launch(args)
105 elif args.subaction == "intent":
106 actions.app_manager.intent(args)
107 elif args.subaction == "list":
108 actions.app_manager.list(args)
109 else:
110 logging.info(
111 "Run waydroid {} -h for usage information.".format(args.action))
112 elif args.action == "prop":
113 if args.subaction == "get":
114 ret = helpers.props.get(args, args.key)
115 if ret:
116 print(ret)
117 elif args.subaction == "set":
118 helpers.props.set(args, args.key, args.value)
119 else:
120 logging.info(
121 "Run waydroid {} -h for usage information.".format(args.action))
122 elif args.action == "shell":
123 actionNeedRoot(args.action)
124 helpers.lxc.shell(args)
125 elif args.action == "logcat":
126 actionNeedRoot(args.action)
127 helpers.lxc.logcat(args)
128 elif args.action == "show-full-ui":
129 actions.app_manager.showFullUI(args)
130 elif args.action == "first-launch":
131 actions.remote_init_client(args)
132 if actions.initializer.is_initialized(args):
133 actions.app_manager.showFullUI(args)
134 elif args.action == "status":
135 actions.status.print_status(args)
136 elif args.action == "log":
137 if args.clear_log:
138 helpers.run.user(args, ["truncate", "-s", "0", args.log])
139 try:
140 helpers.run.user(
141 args, ["tail", "-n", args.lines, "-F", args.log], output="tui")
142 except KeyboardInterrupt:
143 pass
144 else:
145 logging.info("Run waydroid -h for usage information.")
146
147 #logging.info("Done")
148
149 except Exception as e:
150 # Dump log to stdout when args (and therefore logging) init failed
151 if not args:
152 logging.getLogger().setLevel(logging.DEBUG)
153
154 logging.info("ERROR: " + str(e))
155 logging.info("See also: <https://github.com/waydroid>")
156 logging.debug(traceback.format_exc())
157
158 # Hints about the log file (print to stdout only)
159 log_hint = "Run 'waydroid log' for details."
160 if not args or not os.path.exists(args.log):
161 log_hint += (" Alternatively you can use '--details-to-stdout' to"
162 " get more output, e.g. 'waydroid"
163 " --details-to-stdout init'.")
164 print(log_hint)
165 return 1
166
167
168 if __name__ == "__main__":
169 sys.exit(main())