-
Notifications
You must be signed in to change notification settings - Fork 438
/
Copy pathdistrobox-export
executable file
·380 lines (333 loc) · 11.5 KB
/
distrobox-export
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/bin/sh
# POSIX
# Expected env variables:
# HOME
# USER
# DISTROBOX_ENTER_PATH
trap '[ "$?" -ne 0 ] && printf "An error occurred\n"' EXIT
# Defaults
exported_app=""
exported_bin=""
exported_delete=0
exported_service=""
extra_flags=""
verbose=0
version="distrobox_version_placeholder"
# We depend on some commands, let's be sure we have them
base_dependencies="basename grep sed find"
for dep in ${base_dependencies}; do
if ! command -v "${dep}" >/dev/null; then
printf >&2 "Missing dependency: %s\n" "${dep}"
exit 127
fi
done
# Print usage to stdout.
# Arguments:
# None
# Outputs:
# print usage with examples.
show_help() {
cat <<EOF
distrobox version: ${version}
distrobox-export takes care of exporting an app a binary or a service from the container
to the host.
Exported app will be easily available in your normal launcher and it will
automatically be launched from the container it is exported from.
Exported services will be available in the host's user's systemd session, so
systemctl --user status exported_service_name
will show the status of the service exported.
Exported binaries will be exported in the "--export-path" of choice as a wrapper
script that acts naturally both on the host and in the container.
Note that "--export-path" is NOT OPTIONAL, you have to explicitly set it.
You can specify additional flags to add to the command, for example if you want
to export an electron app, you could add the "--foreground" flag to the command:
distrobox-export --app atom --extra-flags "--foreground"
distrobox-export --bin /usr/bin/vim --export-path ~/.local/bin --extra-flags "-p"
distrobox-export --service syncthing --extra-flags "-allow-newer-config"
This works for services, binaries and apps.
Extra flags are only used then the exported app, binary or service is used from
the host, using them inside the container will not include them.
The option "--delete" will un-export an app, binary or service.
distrobox-export --app atom --delete
distrobox-export --bin /usr/bin/vim --export-path ~/.local/bin --delete
distrobox-export --service syncthing --delete
Note you can use --app OR --bin OR --service but not together.
Usage:
distrobox-export --app mpv [--extra-flags "flags"] [--delete]
distrobox-export --service syncthing [--extra-flags "flags"] [--delete]
distrobox-export --bin /path/to/bin --export-path ~/.local/bin [--extra-flags "flags"] [--delete]
Options:
--app/-a: name of the application to export
--bin/-b: absolute path of the binary to export
--service/-s: name of the service to export
--delete/-d: delete exported application or service
--export-path/-ep: path where to export the binary
--extra-flags/-ef: extra flags to add to the command
--help/-h: show this message
--verbose/-v: show more verbosity
--version/-V: show version
EOF
}
# Print generated script from template
# Arguments:
# distrobox name
# binary path
# extra flags
# Outputs:
# print generated script.
generate_script() {
cat <<EOF
#!/bin/sh
# distrobox_binary
# name: ${container_name}
if [ ! -f /run/.containerenv ]; then
${DISTROBOX_ENTER_PATH:-"distrobox-enter"} --name ${container_name} -- ${exported_bin} ${extra_flags} \$@
else
${exported_bin} \$@
fi
EOF
return $?
}
# Parse arguments
while :; do
case $1 in
-h | --help)
# Call a "show_help" function to display a synopsis, then exit.
show_help
exit 0
;;
-v | --verbose)
shift
verbose=1
;;
-V | --version)
printf "distrobox: %s\n" "${version}"
exit 0
;;
-a | --app)
if [ -n "$2" ]; then
exported_app="$2"
shift
shift
fi
;;
-b | --bin)
if [ -n "$2" ]; then
exported_bin="$2"
shift
shift
fi
;;
-s | --service)
if [ -n "$2" ]; then
exported_service="$2"
shift
shift
fi
;;
-ep | --export-path)
if [ -n "$2" ]; then
dest_path="$2"
shift
shift
fi
;;
-ef | --extra-flags)
if [ -n "$2" ]; then
extra_flags="$2"
shift
shift
fi
;;
-d | --delete)
exported_delete=1
shift
;;
*) # Default case: If no more options then break out of the loop.
break ;;
esac
done
# Also check we're running inside a container and not on the host
if [ ! -f /run/.containerenv ]; then
printf >&2 "You must run %s inside a container!\n" " $(basename "$0")"
exit 126
fi
# Ensure the foundamental variables are set and not empty, we will not proceed if
# they are not all set.
if [ -z "${exported_app}" ] &&
[ -z "${exported_bin}" ] &&
[ -z "${exported_service}" ]; then
printf >&2 "Invalid arguments, choose an action below.\n"
show_help
exit 2
fi
if [ -n "${exported_app}" ] &&
[ -n "${exported_bin}" ] &&
[ -n "${exported_service}" ]; then
printf >&2 "Invalid arguments, choose only one action below.\n"
show_help
exit 2
fi
if [ -n "${exported_bin}" ] && [ -z "${dest_path}" ]; then
printf >&2 "Missing argument export-path.\n"
exit 2
fi
set -o errexit
set -o nounset
# set verbosity
if [ "${verbose}" -ne 0 ]; then
set -o xtrace
fi
# We can assume this as we set it the same as container name during creation.
container_name=$(cat /etc/hostname)
# Prefix to add to an existing command to work throught the container
container_command_prefix="${DISTROBOX_ENTER_PATH:-"distrobox-enter"} --name ${container_name} -- "
if [ -n "${exported_bin}" ]; then
# Work on a binary export
# Ensure the binary we're exporting is installed
if [ ! -f "${exported_bin}" ]; then
printf >&2 "Error: cannot find %s.\n" "${exported_bin}"
exit 127
fi
# generate dest_file path
dest_file="${dest_path}/$(basename "${exported_bin}")"
# If we're deleting it, just do it and exit
if [ "${exported_delete}" -ne 0 ]; then
if [ ! -f "${dest_file}" ]; then
printf >&2 "Error: cannot find %s in %s.\nWas it exported?.\n" "${dest_file}" "${dest_path}"
exit 1
fi
if grep -q "distrobox_binary" "${dest_file}"; then
if rm -f "${dest_file}"; then
printf "OK!\n%s exported from %s removed successfully from %s.\n" "${exported_bin}" "${container_name}" "${dest_path}"
exit 0
fi
else
printf >&2 "Error: %s exists but it's not a distrobox exported file.\n" "${dest_file}"
printf >&2 "Error: cannot delete: %s.\n" "${dest_file}"
exit 2
fi
fi
# test if we have writing rights on the file
if ! touch "${dest_file}"; then
printf >&2 "Error: cannot create destination file %s.\n" "${dest_file}"
exit 1
fi
# create the script from template and write to file
if generate_script "${container_name}" "${exported_bin}" >"${dest_file}"; then
chmod +x "${dest_file}"
printf "OK!\n%s from %s exported successfully in %s.\n" "${exported_bin}" "${container_name}" "${dest_path}"
exit 0
fi
printf >&2 "A problem occurred.\n"
exit 3
elif [ -n "${exported_app}" ]; then
# Work on a desktop app export
# Ensure the app we're exporting is installed
if ! command -v "${exported_app}" >/dev/null; then
printf >&2 "Error: trying to export a non-installed application.\n"
exit 127
fi
# Find desktop file for the application to export
desktop_files=$(grep -ril "${exported_app}" /usr/share/applications/* || :)
icon_files=$(find /usr/share/icons -iname "*${exported_app}*")
# Check that we found some desktop files first.
if [ -z "${desktop_files}" ]; then
printf >&2 "Error: cannot find any desktop files.\n"
printf >&2 "Error: trying to export a non-installed application.\n"
exit 127
fi
# copy icons in home directory
for icon_file in ${icon_files}; do
icon_home_directory="$(dirname "${icon_file}" | sed "s|/usr/share/|\/run\/host\/${HOME}/.local/share/|g")"
# check if we're exporting or deleting
if [ "${exported_delete}" -ne 0 ]; then
# we need to remove, not export
rm -f "${icon_home_directory}"/"$(basename "${icon_file}")"
else
# we wanto to export the application's icons
mkdir -p "${icon_home_directory}"
cp "${icon_file}" "${icon_home_directory}"
fi
done
# create desktop files for the distrobox
for desktop_file in ${desktop_files}; do
desktop_home_file="$(basename "${desktop_file}")"
# create applications dir if not existing
if [ ! -d "/run/host/${HOME}/.local/share/applications" ]; then
mkdir -p "/run/host/${HOME}/.local/share/applications"
fi
# check if we're exporting or deleting
if [ "${exported_delete}" -ne 0 ]; then
if [ ! -f "/run/host/${HOME}/.local/share/applications/${desktop_home_file}" ]; then
printf >&2 "Error: trying to remove a non-exported application.\n"
exit 1
fi
rm -f "/run/host/${HOME}/.local/share/applications/${desktop_home_file}"
else
# If a TryExec is present, we have to fake it as it will not work throught the
# container separation
sed "s|^Exec=|Exec=${container_command_prefix} |g" "${desktop_file}" |
sed "s|\(%.*\)|${extra_flags} \1|g" |
sed "s|^TryExec=.*|TryExec=true|g" \
>"/run/host/${HOME}/.local/share/applications/${desktop_home_file}"
fi
done
printf "OK!\nApplication %s successfully exported.\n" "${exported_app}"
printf "%s will appear in your applications list in a few seconds.\n" "${exported_app}"
elif [ -n "${exported_service}" ]; then
# Work on a service export
# If we're managing services, let's be sure we have systemctl
if ! command -v systemctl >/dev/null; then
printf >&2 "Missing dependency: systemd\n"
exit 127
fi
# Ensure we're working with fresh data
systemctl --user daemon-reload
# Fetch original service file
service_file="/run/host/${HOME}/.config/systemd/user/${exported_service}-${container_name}.service"
# If we're deleting it, just do it and exit
if [ "${exported_delete}" -ne 0 ]; then
if [ ! -f "${service_file}" ]; then
printf >&2 "Error: cannot find service %s.\nWas it exported?.\n" "${exported_service}-${container_name}"
exit 1
fi
rm -f "${service_file}"
systemctl --user daemon-reload
printf "OK!\nService %s successfully removed.\n" "${exported_service}-${container_name}"
exit 0
fi
# Check if it is already exported
if [ -f "${service_file}" ] &&
grep -q "${container_command_prefix}" "${service_file}"; then
printf "Service %s is already exported.\n\n" "${exported_service}-${container_name}"
printf "To check the status, run:\n\tsystemctl --user status %s \n" "${exported_service}-${container_name}.service"
printf "To start it, run:\n\tsystemctl --user start %s \n" "${exported_service}-${container_name}.service"
printf "To start it at login, run:\n\tsystemctl --user enable %s \n" "${exported_service}-${container_name}.service"
exit 0
fi
# Create temp file with random name
temp_file="$(mktemp -u)"
# Replace all Exec occurrencies
systemctl --user cat "${exported_service}.service" >"${service_file}" 2>/dev/null
for exec_cmd in ExecStart ExecStartPre ExecStartPost ExecReload ExecStop ExecStopPost; do
# Save to temp file
systemctl --user cat "${exported_service}-${container_name}.service" >"${temp_file}" 2>/dev/null
# Add prefix only if not present
if ! grep "${exec_cmd}" "${temp_file}" | grep -q "${container_command_prefix}"; then
tail -n+2 "${temp_file}" |
sed "s|^${exec_cmd}=|${exec_cmd}=${container_command_prefix} |g" |
sed "s|^${exec_cmd}=.*|& ${extra_flags}|g" >"${service_file}"
fi
done
# Cleanup
rm -f "${temp_file}"
# Reload
systemctl --user daemon-reload
printf "OK!\nService %s successfully exported.\n" "${exported_service}-${container_name}"
printf "%s will appear in your services list in a few seconds.\n\n" "${exported_service}-${container_name}"
printf "To check the status, run:\n\tsystemctl --user status %s \n" "${exported_service}-${container_name}.service"
printf "To start it, run:\n\tsystemctl --user start %s \n" "${exported_service}-${container_name}.service"
printf "To start it at login, run:\n\tsystemctl --user enable %s \n" "${exported_service}-${container_name}.service"
exit 0
fi