export: remove service export, now we have initful container for that.

Signed-off-by: Luca Di Maio <luca.dimaio1@gmail.com>
This commit is contained in:
Luca Di Maio
2023-03-26 16:56:04 +02:00
parent 8b31dc46cd
commit 4bc28f1a62
2 changed files with 11 additions and 132 deletions
+3 -99
View File
@@ -32,7 +32,6 @@ exported_app=""
exported_app_label=""
exported_bin=""
exported_delete=0
exported_service=""
extra_flags=""
# Use DBX_HOST_HOME if defined, else fallback to HOME
# DBX_HOST_HOME is set in case container is created
@@ -66,15 +65,13 @@ distrobox version: ${version}
Usage:
distrobox-export --app mpv [--extra-flags "flags"] [--delete] [--sudo]
distrobox-export --service syncthing.service [--extra-flags "flags"] [--delete] [--sudo]
distrobox-export --bin /path/to/bin --export-path ~/.local/bin [--extra-flags "flags"] [--delete] [--sudo]
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
--delete/-d: delete exported application or binary
--export-label/-el: label to add to exported application name.
Defaults to (on \$container_name)
--export-path/-ep: path where to export the binary
@@ -119,14 +116,6 @@ while :; do
shift
fi
;;
-s | --service)
if [ -n "$2" ]; then
export_action="service"
exported_service="$2"
shift
shift
fi
;;
-l | --login)
is_login=1
shift
@@ -197,8 +186,7 @@ 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
[ -z "${exported_bin}" ]; then
printf >&2 "Error: Invalid arguments.\n"
printf >&2 "Error: missing export target. Run\n"
printf >&2 "\tdistrobox-export --help\n"
@@ -206,9 +194,7 @@ if [ -z "${exported_app}" ] &&
exit 2
fi
# Ensure we're not receiving more than one action at time.
if [ -n "${exported_app}" ] && [ -n "${exported_bin}" ] ||
[ -n "${exported_app}" ] && [ -n "${exported_service}" ] ||
[ -n "${exported_bin}" ] && [ -n "${exported_service}" ]; then
if [ -n "${exported_app}" ] && [ -n "${exported_bin}" ]; then
printf >&2 "Error: Invalid arguments, choose only one action below.\n"
printf >&2 "Error: You can only export one thing at time.\n"
exit 2
@@ -486,85 +472,6 @@ export_application() {
}
# Export systemd service to the host.
# the following function will export a selected systemd unit from the distrobox
# to the host. It will modify the original unit to include the container_command_prefix.
#
# Arguments:
# none it will use the ones set up globally
# Outputs:
# new systemd unit in /run/host/$host_home/.config/systemd/user/
# or error code.
export_service() {
# find the service file in the common
service_file=$(find \
/etc/systemd/system/ /lib/systemd/system/ /usr/lib/systemd/system/ \
/lib/systemd/user/ /usr/lib/systemd/user/ \
"${host_home}"/.config/systemd/user/ \
-type f -name "${exported_service}*" 2> /dev/null | tail -1)
# Check that we found some service files first.
if [ -z "${service_file}" ]; then
printf >&2 "Error: cannot find any service file for %s.\n" "${exported_service}"
printf >&2 "Error: trying to export a non-installed service.\n"
return 127
fi
# this is the output file we will produce.
exported_service_file="${container_name}-$(basename "${service_file}")"
exported_service_fullpath="/run/host${host_home}/.config/systemd/user/${exported_service_file}"
# If we're deleting it, just do it and exit
if [ "${exported_delete}" -ne 0 ]; then
rm -f "${exported_service_fullpath}"
printf "Service %s successfully removed.\nOK!\n" "${exported_service_file}"
return 0
fi
# Check if it is already exported
if [ -f "${exported_service_fullpath}" ] &&
grep -q "${container_command_prefix}" "${exported_service_fullpath}"; then
printf "Service %s is already exported.\n\n" "${exported_service_file}"
printf "\nTo check the status, run:\n\tsystemctl --user status %s \n" "${exported_service_file}"
printf "\nTo start it, run:\n\tsystemctl --user start %s \n" "${exported_service_file}"
printf "\nTo start it at login, run:\n\tsystemctl --user enable %s \n" "${exported_service_file}"
return 0
fi
# Create temp file with random name
temp_file="$(mktemp -u)"
mkdir -p "/run/host${host_home}/.config/systemd/user/"
# Replace all Exec occurrences
cp -f "${service_file}" "${exported_service_fullpath}"
chmod 0644 "${exported_service_fullpath}"
for exec_cmd in ExecStart ExecStartPre ExecStartPost ExecReload ExecStop ExecStopPost; do
# Save to temp file
cp -f "${exported_service_fullpath}" "${temp_file}"
# Add prefix only if not present
if ! grep "${exec_cmd}" "${temp_file}" | grep -q "${container_command_prefix}"; then
# Add command_prefix
# Add extra flags
# Add closing quote
sed "s|^${exec_cmd}=|${exec_cmd}=${container_command_prefix}|g" "${temp_file}" |
sed "s|^${exec_cmd}=.*|& ${extra_flags}|g" > "${exported_service_fullpath}"
# in the end we add the final quote we've opened in the "container_command_prefix"
fi
done
# Cleanup
rm -f "${temp_file}"
printf "Service %s successfully exported.\nOK\n" "${exported_service_file}"
printf "%s will appear in your services list in a few seconds.\n\n" "${exported_service_file}"
printf "\nTo check the status, run:\n\tsystemctl --user status %s \n" "${exported_service_file}"
printf "\nTo start it, run:\n\tsystemctl --user start %s \n" "${exported_service_file}"
printf "\nTo start it at login, run:\n\tsystemctl --user enable %s \n" "${exported_service_file}"
return 0
}
# Main routine
case "${export_action}" in
app)
@@ -573,9 +480,6 @@ case "${export_action}" in
bin)
export_binary
;;
service)
export_service
;;
*)
printf >&2 "Invalid arguments, choose an action below.\n"
show_help
+8 -33
View File
@@ -6,9 +6,9 @@
# DESCRIPTION
**Application and service exporting**
**Application and binary exporting**
distrobox-export takes care of exporting an app a binary or a service from the container
distrobox-export takes care of exporting an app or a binary from the container
to the host.
The exported app will be easily available in your normal launcher and it will
@@ -20,8 +20,7 @@ automatically be launched from the container it is exported from.
--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
--delete/-d: delete exported application or binary
--export-label/-el: label to add to exported application name.
Defaults to (on \$container_name)
--export-path/-ep: path where to export the binary
@@ -32,13 +31,12 @@ automatically be launched from the container it is exported from.
--verbose/-v: show more verbosity
--version/-V: show version
You may want to install graphical applications or user services in your distrobox.
You may want to install graphical applications or CLI tools in your distrobox.
Using `distrobox-export` from **inside** the container will let you use them from the host itself.
# EXAMPLES
distrobox-export --app mpv [--extra-flags "flags"] [--delete] [--sudo]
distrobox-export --service syncthing [--extra-flags "flags"] [--delete] [--sudo]
distrobox-export --bin /path/to/bin --export-path ~/.local/bin [--extra-flags "flags"] [--delete] [--sudo]
**App export example**
@@ -49,22 +47,6 @@ This tool will simply copy the original `.desktop` files along with needed icons
add the prefix `/usr/local/bin/distrobox-enter -n distrobox_name -e ...` to the commands to run, and
save them in your home to be used directly from the host as a normal app.
**Service export example**
distrobox-export --service syncthing --extra-flags "--allow-newer-config"
distrobox-export --service nginx --sudo
For services, it will similarly export the systemd unit inside the container to a
`systemctl --user` service, prefixing the various
`ExecStart ExecStartPre ExecStartPost ExecReload ExecStop ExecStopPost` with the
`distrobox-enter` command prefix.
The 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.
**Binary export example**
distrobox-export --bin /usr/bin/code --extra-flags "--foreground" --export-path $HOME/.local/bin
@@ -86,20 +68,17 @@ 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
This works for binaries and apps.
Extra flags are only used then the exported app or binary is used from
the host, using them inside the container will not include them.
**Unexport**
The option "--delete" will un-export an app, binary, or service.
The option "--delete" will un-export an app or binary
distrobox-export --app atom --delete
distrobox-export --bin /usr/bin/vim --export-path ~/.local/bin --delete
distrobox-export --service syncthing --delete
distrobox-export --service nginx --delete
**Run as root in the container**
@@ -159,14 +138,10 @@ was the case before).
**Notes**
Note you can use --app OR --bin OR --service but not together.
distrobox-export --service nginx --sudo
Note you can use --app OR --bin but not together.
![app-export](https://user-images.githubusercontent.com/598882/144294795-c7785620-bf68-4d1b-b251-1e1f0a32a08d.png)
![service-export](https://user-images.githubusercontent.com/598882/144294314-29a8921f-4511-453d-bf8e-d0d1e336db91.png)
NOTE: some electron apps such as vscode and atom need additional flags to work from inside the
container, use the `--extra-flags` option to provide a series of flags, for example: