-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-homeassistant.sh
executable file
·127 lines (108 loc) · 3.2 KB
/
update-homeassistant.sh
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
#!/bin/bash
### COLOR
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
NC='\033[0m'
# full path (including registry like docker.io or gcr.io) to image that should be used
image="ghcr.io/home-assistant/raspberrypi4-homeassistant:stable"
# name of container for podman commands
container_name="homeassistant"
# path to config folder for homeassistant
config_folder=$1
usage() {
echo -e "${RED}ERROR: Variable $1 not set. Usage: bash update-homeassistant <config_folder>${NC}"
}
not_a_folder() {
echo -e "${RED}ERROR: Variable $1 is not a folder${NC}"
}
check_var() {
if [[ -z ${config_folder} ]]; then
usage config_folder
exit 1
fi
if [[ ! -d $config_folder ]]; then
not_a_folder config_folder
exit 1
fi
}
check_image() {
echo -e "Checking whether image ${CYAN}${image}${NC} exists or not"
podman image exists "${image}"
if [[ "${?}" == 0 ]]; then
echo -e "Image ${CYAN}${image}${NC} already exists\n"
return 1
else
echo -e "Image ${CYAN}${image}${NC} does not exist yet\n"
return 0
fi
}
check_container() {
echo -e "Checking whether image ${CYAN}${image}${NC} is already in use or not"
podman container exists "${container_name}"
if [[ "${?}" == 0 ]]; then
echo -e "Image ${CYAN}${image}${NC} is already in use\n"
return 1
else
echo -e "Image ${CYAN}${image}${NC} is not used by any container, going to update now\n"
return 0
fi
}
update_image() {
podman pull "${image}"
}
update_container() {
echo -e "${RED}Stopping old ${CYAN}${container_name}${NC} container${NC}"
podman stop "${container_name}"
echo -e "${RED}Removing old ${CYAN}${container_name}${NC} container${NC}"
podman rm "${container_name}"
echo -e "Starting new ${CYAN}${container_name}${NC} container"
podman run \
--detach \
--privileged \
--name "${container_name}" \
--volume /etc/localtime:/etc/localtime:ro \
--volume "${config_folder}":/config \
--network=host \
"${image}"
echo -e "${GREEN}Done${NC}"
}
compare_image_digest() {
echo -e "Comparing image id of running ${CYAN}${container_name}${NC} container with image id of ${CYAN}${image}${NC}"
# first try to pull new image
update_image
# there is a container named ${container_name} running. get image id
container_image_id=$(podman inspect ${container_name} --format "{{.Image}}")
container_image_digest=$(podman inspect ${container_image_id} --format "{{.Digest}}")
image_digest=$(podman inspect ${image} --format "{{.Digest}}")
if [[ "${container_image_digest}" == "${image_digest}" ]]; then
echo -e "Container ${CYAN}${container_name}${NC} is already on newest image version\n"
return 1
else
echo -e "Container ${CYAN}${container_name}${NC} is not on newest image version, needs to be updated\n"
return 0
fi
}
main() {
date
check_var
check_image
image_exists=$?
if [[ "${image_exists}" == 0 ]]; then
update_image
fi
check_container
container_exists=$?
if [[ "${container_exists}" == 0 ]]; then
update_container
elif [[ "${container_exists}" == 1 ]]; then
compare_image_digest
newest_image_running=$?
if [[ "${newest_image_running}" == 1 ]]; then
echo -e "${GREEN}Nothing to do${NC}"
else
update_container
fi
fi
}
main