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