|
| 1 | +{ stdenv |
| 2 | +, lib |
| 3 | +, pkgs |
| 4 | +, writeShellScript |
| 5 | +, databaseUser |
| 6 | +, databaseName |
| 7 | +, getExe |
| 8 | +, teslamate |
| 9 | +, environmentFilePath |
| 10 | +, ... |
| 11 | +}: |
| 12 | +let |
| 13 | + closeDrive = writeShellScript "teslamate-close-drive" '' |
| 14 | + set -euo pipefail |
| 15 | + : ''${1?'Please provide a drive ID to close'} |
| 16 | +
|
| 17 | + if ! [[ "''${1}" =~ ^[0-9]+$ ]]; then |
| 18 | + echo "Error: Drive ID must be an integer." >&2 |
| 19 | + exit 1 |
| 20 | + fi |
| 21 | +
|
| 22 | + # load env file to have RELEASE_COOKIE set |
| 23 | + if [ -f ${environmentFilePath} ]; then |
| 24 | + source ${environmentFilePath} |
| 25 | + export RELEASE_COOKIE |
| 26 | + else |
| 27 | + echo "Environment file ${environmentFilePath} not found!" >&2 |
| 28 | + exit 1 |
| 29 | + fi |
| 30 | + : ''${RELEASE_COOKIE?'RELEASE_COOKIE must be set in the environment file'} |
| 31 | +
|
| 32 | + echo "Attempt to close the drive with ID ''${1}." |
| 33 | + ${getExe teslamate} rpc "TeslaMate.Repo.get!(TeslaMate.Log.Drive, ''${1}) |> TeslaMate.Log.close_drive()" |
| 34 | + ''; |
| 35 | + |
| 36 | + closeCharge = writeShellScript "teslamate-close-charge" '' |
| 37 | + set -euo pipefail |
| 38 | + : ''${1?'Please provide a charge ID to close'} |
| 39 | +
|
| 40 | + if ! [[ "''${1}" =~ ^[0-9]+$ ]]; then |
| 41 | + echo "Error: Charge ID must be an integer." >&2 |
| 42 | + exit 1 |
| 43 | + fi |
| 44 | +
|
| 45 | + # load env file to have RELEASE_COOKIE set |
| 46 | + if [ -f ${environmentFilePath} ]; then |
| 47 | + source ${environmentFilePath} |
| 48 | + export RELEASE_COOKIE |
| 49 | + else |
| 50 | + echo "Environment file ${environmentFilePath} not found!" >&2 |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | + : ''${RELEASE_COOKIE?'RELEASE_COOKIE must be set in the environment file'} |
| 54 | +
|
| 55 | + echo "Attempt to close the charge with ID ''${1}." |
| 56 | + ${getExe teslamate} rpc "TeslaMate.Repo.get!(TeslaMate.Log.ChargingProcess, ''${1}) |> TeslaMate.Log.complete_charging_process()" |
| 57 | + ''; |
| 58 | + |
| 59 | + deleteDrive = writeShellScript "teslamate-delete-drive" '' |
| 60 | + set -euo pipefail |
| 61 | + : ''${1?'Please provide a drive ID to delete'} |
| 62 | +
|
| 63 | + if ! [[ "''${1}" =~ ^[0-9]+$ ]]; then |
| 64 | + echo "Error: Drive ID must be an integer." >&2 |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | +
|
| 68 | + # Check if drive exists |
| 69 | + if [ "$(sudo -u teslamate psql -U ${databaseUser} -d ${databaseName} -tAc "SELECT 1 FROM drives WHERE id = ''${1};")" != "1" ]; then |
| 70 | + echo "Warning: Drive with ID ''${1} does not exist. Nothing to delete." >&2 |
| 71 | + exit 0 |
| 72 | + fi |
| 73 | +
|
| 74 | + echo "Attempt to delete the drive with ID ''${1}." |
| 75 | + sudo -u teslamate psql -U ${databaseUser} -d ${databaseName} -c "DELETE FROM drives WHERE id = ''${1};" |
| 76 | + echo "Successfully deleted drive with ID ''${1}." |
| 77 | + ''; |
| 78 | + |
| 79 | + deleteCharge = writeShellScript "teslamate-delete-charge" '' |
| 80 | + set -euo pipefail |
| 81 | + : ''${1?'Please provide a charge ID to delete'} |
| 82 | +
|
| 83 | + if ! [[ "''${1}" =~ ^[0-9]+$ ]]; then |
| 84 | + echo "Error: Charge ID must be an integer." >&2 |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | +
|
| 88 | + # Check if charging process exists |
| 89 | + if [ "$(sudo -u teslamate psql -U ${databaseUser} -d ${databaseName} -tAc "SELECT 1 FROM charging_processes WHERE id = ''${1};")" != "1" ]; then |
| 90 | + echo "Warning: Charging process with ID ''${1} does not exist. Nothing to delete." >&2 |
| 91 | + exit 0 |
| 92 | + fi |
| 93 | +
|
| 94 | + echo "Attempt to delete the charge with ID ''${1}." |
| 95 | + sudo -u teslamate psql -U ${databaseUser} -d ${databaseName} -c "DELETE FROM charging_processes WHERE id = ''${1};" |
| 96 | + echo "Successfully deleted charging process with ID ''${1}." |
| 97 | + ''; |
| 98 | +in |
| 99 | +stdenv.mkDerivation { |
| 100 | + pname = "teslamate-maintenance"; |
| 101 | + version = "0.1.0"; |
| 102 | + src = ./.; |
| 103 | + |
| 104 | + phases = [ "installPhase" ]; |
| 105 | + |
| 106 | + installPhase = '' |
| 107 | + mkdir -p $out/bin |
| 108 | + ln -s ${closeDrive} $out/bin/teslamate-close-drive |
| 109 | + ln -s ${closeCharge} $out/bin/teslamate-close-charge |
| 110 | + ln -s ${deleteDrive} $out/bin/teslamate-delete-drive |
| 111 | + ln -s ${deleteCharge} $out/bin/teslamate-delete-charge |
| 112 | + ''; |
| 113 | +} |
0 commit comments