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