-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rewrite apt-post to manage situations where the environment is …
…incompatible with distrobox
- Loading branch information
1 parent
310c5ed
commit a27dd36
Showing
2 changed files
with
101 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,88 @@ | ||
#!/bin/bash | ||
#!/usr/bin/python3 | ||
import os | ||
import subprocess | ||
from typing import Text, Dict, Union | ||
|
||
package="$(cat /tmp/vso-transaction)" | ||
log_file="/tmp/vso-export-log.log" | ||
|
||
if [ -n "$SUDO_USER" ]; then | ||
username=$SUDO_USER | ||
else | ||
echo "VSO does not work with root privileges, no programs will be exported. Use 'vso export -a $package' to manually export it." | ||
fi | ||
def read_file(file_path: Text) -> Text: | ||
""" | ||
Read a file and return its contents. | ||
""" | ||
with open(file_path, "r") as f: | ||
return f.read().strip() | ||
|
||
su -c "/usr/bin/vso export -a $package" $username >> "$log_file" 2>&1 | ||
|
||
if [ $? -ne 0 ]; then | ||
echo "Error occurred while exporting. Check $log_file for details." | ||
echo "No programs will be exported. Use 'vso export -a $package' to manually export it." | ||
fi | ||
def get_user_id(username: Text) -> Text: | ||
""" | ||
Get the user id of the user who started the transaction. | ||
""" | ||
return subprocess.check_output( | ||
"su -m -c 'id -u {}' {}".format(username, username), shell=True, text=True | ||
).strip() | ||
|
||
|
||
def prepare_environment(username: Text, user_id: Text) -> Dict[Text, Union[Text, int]]: | ||
""" | ||
Prepare the environment distrobox expects to run in. | ||
This is needed because the environment gets reset when | ||
running apt. | ||
""" | ||
env = os.environ.copy() | ||
env["CONTAINER_ID"] = "apx-vso-pico" | ||
env["XDG_DATA_DIRS"] = f"/home/{username}/.local/share" | ||
env["DBUS_SESSION_BUS_ADDRESS"] = f"unix:path=/run/user/{user_id}/bus" | ||
return env | ||
|
||
|
||
def run_vso_export( | ||
command: Text, env: Dict[Text, Union[Text, int]] | ||
) -> subprocess.CompletedProcess[Text]: | ||
""" | ||
Start the vso export command as the user who started the transaction. | ||
""" | ||
try: | ||
result = subprocess.run( | ||
command, shell=True, check=True, capture_output=True, text=True, env=env | ||
) | ||
return result | ||
except subprocess.CalledProcessError as e: | ||
with open(log_file, "a") as log: | ||
log.write(e.stderr) | ||
|
||
print("Error occurred while exporting. Check {} for details.".format(log_file)) | ||
print("Exit code was: {}".format(e.returncode)) | ||
print( | ||
"No programs will be exported. Use 'vso export -a {}' to manually export it.".format( | ||
package | ||
) | ||
) | ||
raise | ||
|
||
|
||
if __name__ == "__main__": | ||
log_file: Text = "/tmp/vso-export-log.log" | ||
package_file: Text = "/tmp/vso-transaction" | ||
|
||
package: Text = read_file(package_file) | ||
|
||
# Apt hook is run as root, but we need to run vso as the user | ||
# who started the transaction | ||
sudo_user: Text = os.getenv("SUDO_USER") | ||
if not sudo_user and os.geteuid() == 0: | ||
print( | ||
"VSO does not work with root privileges. Use 'vso export -a {}' to manually export it.".format( | ||
package | ||
) | ||
) | ||
os.exit(1) | ||
|
||
sudo_user_uid: Text = get_user_id(sudo_user) | ||
env: Dict[Text, Union[Text, int]] = prepare_environment(sudo_user, sudo_user_uid) | ||
|
||
command: Text = ( | ||
'su -m -c "/usr/bin/distrobox-host-exec vso export -a {}" {}'.format( | ||
package, sudo_user | ||
) | ||
) | ||
|
||
result: subprocess.CompletedProcess[Text] = run_vso_export(command, env) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/bin/bash | ||
|
||
package="$(cat /tmp/vso-transaction)" | ||
log_file="/tmp/vso-export-log.log" | ||
|
||
if [ -n "$SUDO_USER" ]; then | ||
username=$SUDO_USER | ||
else | ||
echo "VSO does not work with root privileges, no programs will be exported. Use 'vso export -a $package' to manually export it." | ||
fi | ||
|
||
su -c "/usr/bin/vso export -a $package" $username >> "$log_file" 2>&1 | ||
|
||
if [ $? -ne 0 ]; then | ||
echo "Error occurred while exporting. Check $log_file for details." | ||
echo "No programs will be exported. Use 'vso export -a $package' to manually export it." | ||
fi |