-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpgradeContainer_Without_Repull_Command.RSC
More file actions
182 lines (168 loc) · 6.17 KB
/
UpgradeContainer_Without_Repull_Command.RSC
File metadata and controls
182 lines (168 loc) · 6.17 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
:local stopAndRemoveTimeout 15
:local addTimeout 180
:local errorRetryCount 3
:local doNotUpgradeContainer [:toarray ("mihomo:latest") ]
:local dnsContainer "adguardhome"
:local testUrl "https://hub.docker.com"
:local validAddArgs ("comment","remote-image","check-certificate","name","interface","env","envlists","cmd","entrypoint","stop-signal","stop-time","root-dir","layer-dir","mount","mountlists","dns","hostname","domain-name","workdir","logging","start-on-boot","auto-restart-interval","cpu-list","memory-high","devices","hosts")
:local escapeStr do={
:local strLen [:len $1]
:local escStr ""
:for i from=0 to=($strLen-1) do={
:local chr [:pick $1 $i ($i+1)]
:if ($chr="\$") do={
:set escStr "$escStr\\\$"
} else={
:if ($chr="\\") do={
:set escStr "$escStr\\\\"
} else={
:if ($chr="\"") do={
:set escStr "$escStr\\\""
} else={
:set escStr "$escStr$chr"
}
}
}
}
:return "\"$escStr\""
}
:local arrayToStr do={
:local arrLen [:len $1]
:local strArr "("
:for i from=0 to=($arrLen-1) do={
:local item [:pick $1 $i]
:if ([:len $strArr]>1) do={
:set $strArr "$strArr,"
}
:if ([:typeof $item]="str") do={
:set $strArr "$strArr$([$escapeStr $item])"
} else={
:set $strArr "$strArr$item"
}
}
:return "$strArr)"
}
:local escapeValue do={
:if ([:typeof $1]="str") do={
:return [$escapeStr $1]
}
:if ([:typeof $1]="array") do={
:return [$arrayToStr $1 escapeStr=$escapeStr]
}
:if ([:typeof $1]="bool") do={
:if ($1) do={
:return "yes"
} else={
:return "no"
}
}
:return $1
}
:local checkDoNotUpgrade do={
:for i from=0 to=([:len $2]-1) do={
:if ([:find $1 [:pick $2 $i]]>=0) do={
:return false
}
}
:return true
}
:local upgradeContainer do={
:local currentContainer [/container find remote-image=$1]
/container stop $currentContainer
:local sec 0
:while ([/container get $currentContainer stopped]=[]&&!([/container get $currentContainer about]~"error")&&$sec<$2) do={
:delay 1
:set sec ($sec+1)
}
:if ($sec<$2) do={
/container remove $currentContainer
:set sec 0
:while ([/container find remote-image=$1]!=""&&$sec<$2) do={
:delay 1
:set sec ($sec+1)
}
:if ($sec<$2) do={
:execute $3 as-string
:set currentContainer [/container find remote-image=$1]
:set sec 0
:while ([/container get $currentContainer about]=[]&&$sec<$4) do={
:delay 1
:set sec ($sec+1)
}
:while ([/container get $currentContainer about]~"extracting"||[/container get $currentContainer about]~"downloading"&&$sec<$4) do={
:delay 1
:set sec ($sec+1)
}
:if ($sec<$4) do={
:if (!([/container get $currentContainer about]~"error")) do={
:if ($5=true) do={
/container start $currentContainer
}
:return "Successful"
} else={
/log error "Container $1 upgrade failed"
}
} else={
/log error "Container $1 upgrade timed out"
}
} else={
/log error "Container $1 remove timed out"
}
} else={
/log error "Container $1 stop timed out"
}
:delay 5
:return "Failed"
}
:local currentDNS
:local upgradeResult
:local checkContainerStatus [:parse [/system/script get CheckContainerStatus source]]
:do {
:if ([:toarray [/tool/fetch output=none url=$testUrl as-value]]->"status"="finished") do={
:foreach container in=[/container print detail as-value] do={
:local containerImage ($container->"remote-image")
:if ([:len $globalDoNotUpgradeContainer]>0) do={
:set doNotUpgradeContainer $globalDoNotUpgradeContainer
}
:if ([$checkDoNotUpgrade $containerImage $doNotUpgradeContainer]&&[:find $containerImage "/"]>=0) do={
:local isContainerRunning ($container->"running")
:if ([:find $containerImage $dnsContainer]>=0) do={
:set currentDNS [/ip/dns get servers]
/ip/dns set servers=223.5.5.5
/ip/dns cache flush
:delay 3
}
:local addCmd "/container add"
:foreach k,v in=$container do={
:if ([:len $v]!=0) do={
:if ([:find $validAddArgs $k]>=0) do={
:set addCmd "$addCmd $k=$([$escapeValue $v escapeStr=$escapeStr arrayToStr=$arrayToStr])"
}
}
}
:local count
:local result
:while ($result!="Successful"&&$count<$errorRetryCount) do={
:set count ($count+1)
:set result [$upgradeContainer $containerImage $stopAndRemoveTimeout $addCmd $addTimeout $isContainerRunning]
}
:if ([:len $currentDNS]>0) do={
:if ([$checkContainerStatus $dnsContainer]=true) do={
/ip/dns set servers=$currentDNS
:delay 5
} else={
/log error "DNS container not running,please check it manually and change your DNS settings"
}
:set currentDNS ""
}
:set upgradeResult "$upgradeResult $containerImage $result\n"
}
}
:if ([:find $upgradeResult "Failed"]>=0 ) do={
:local sendMsg [:parse [/system/script get Msg2WeChat source]]
$sendMsg msgTitle="Container upgrade report" msgContent=$upgradeResult
}
}
} on-error={
/log error "Someting went wrong,ensure you can access the docker server.Upgrade failed"
}