fbrettnich / hcloud-snapshot-as-backup

Hetzner Cloud - Automatic Snapshots as Backups for more flexibility
https://hub.docker.com/r/fbrettnich/hcloud-snapshot-as-backup
MIT License
111 stars 19 forks source link

add basic error handling #13

Closed abbbi closed 10 months ago

abbbi commented 10 months ago

hi,

pull request includes:

1) code formatting using python black code formatter 2) adds basic error handling if snapshot creation fails: changes the exit code of the script based on how many errors during snapshot creation happen, this makes it easier for callers to report errors during operation based on exit code.

Example of successful run:

(venv) abi@cefix ~/hcloud-snapshot-as-backup (error)$ python3 snapshot-as-backup.py 
Snapshot #144915461 (Server #13281368) has been created
Snapshot #144915462 (Server #28223848) has been created
Snapshot #144915463 (Server #28283699) has been created
Snapshot #144915464 (Server #41647785) has been created
Snapshot #144915465 (Server #41664956) has been created
Snapshot #144915334 (Server #13281368) was successfully deleted
Snapshot #144915335 (Server #28223848) was successfully deleted
Snapshot #144915336 (Server #28283699) was successfully deleted
Snapshot #144915337 (Server #41647785) was successfully deleted
Snapshot #144915338 (Server #41664956) was successfully deleted
(venv) abi@cefix ~/hcloud-snapshot-as-backup (error)$ echo $?
0

Example of errnous run:

(venv) abi@cefix ~/hcloud-snapshot-as-backup (error)$ python3 snapshot-as-backup.py 
Snapshot for Server #13281368 could not be created: Locked
{
  "error": {
    "message": "cannot perform operation because server is locked",
    "code": "locked",
    "details": null
  }
}
[..]
(venv) abi@cefix ~/hcloud-snapshot-as-backup (error)$ echo $?
5
fbrettnich commented 10 months ago

Script runs mainly in container, therefore not optimal with exit code. Code formatting is not desired.