-
Notifications
You must be signed in to change notification settings - Fork 593
/
docker_upgrader.py
232 lines (202 loc) · 7.84 KB
/
docker_upgrader.py
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
"""
Copyright 2020 The Magma Authors.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import asyncio
import logging
import pathlib
from magma.common.service import MagmaService
from magma.configuration.service_configs import load_service_config
from magma.magmad.upgrade.upgrader import UpgraderFactory
from magma.magmad.upgrade.upgrader2 import (
UpgradeIntent,
Upgrader2,
VersionInfo,
VersionT,
run_command,
)
MAGMA_GITHUB_PATH = "/tmp/magma_upgrade"
MAGMA_GITHUB_URL = "https://github.com/magma/magma.git"
class DockerUpgrader(Upgrader2):
"""
Downloads and installs images
"""
def perform_upgrade_if_necessary(self, target_version: str) -> None:
"""
Target version comes from tier configuration currently.
"""
if self.upgrade_task and not self.upgrade_task.done():
logging.info("Not starting another upgrade, upgrade in progress")
return
self.upgrade_task = self.loop.create_task(self.do_docker_upgrade())
async def get_upgrade_intent(self) -> UpgradeIntent:
"""
Returns the desired version tag for the gateway.
"""
version_info = await asyncio.gather(self.get_versions())
current_version = version_info[0].current_version
tgt_version = self.service.mconfig.package_version
if tgt_version is None or tgt_version == "":
logging.warning(
'magmad package_version not found, '
'using current tag: %s as target tag.',
current_version,
)
return UpgradeIntent(
stable=VersionT(current_version),
canary=VersionT(""),
)
return UpgradeIntent(stable=VersionT(tgt_version), canary=VersionT(""))
async def get_versions(self) -> VersionInfo:
""" Returns the current version by parsing the IMAGE_VERSION in the
.env file
"""
with open('/var/opt/magma/docker/.env', 'r', encoding='utf-8') as env:
for line in env:
if line.startswith("IMAGE_VERSION="):
current_version = line.split("=")[1].strip()
break
return VersionInfo(
current_version=current_version,
available_versions=set(),
)
async def prepare_upgrade(
self, version: VersionT, path_to_image: pathlib.Path,
) -> None:
"""Install the new docker-compose file"""
gw_module = self.service.config["upgrader_factory"]\
.get("gateway_module")
# Update any mounted static configs
await run_command(
"cp -TR {}/magma/{}/gateway/configs /etc/magma".
format(MAGMA_GITHUB_PATH, gw_module),
shell=True, check=True,
)
# Update any mounted template configs
await run_command(
"cp -TR {}/magma/orc8r/gateway/configs/templates "
"/etc/magma/templates".format(MAGMA_GITHUB_PATH),
shell=True, check=True,
)
# Copy updated docker-compose
await run_command(
"cp {}/magma/{}/gateway/docker/docker-compose.yml "
"/var/opt/magma/docker".format(
MAGMA_GITHUB_PATH,
gw_module,
),
shell=True, check=True,
)
async def upgrade(
self, version: VersionT, path_to_image: pathlib.Path,
) -> None:
"""Upgrade is a no-op as an external process (e.g. cron) must
trigger it
"""
pass
async def do_docker_upgrade(self) -> None:
"""
Do a single loop of the upgrade process, using the given upgrader.
"""
try:
await self._do_docker_upgrade()
except Exception as exp: # pylint: disable=broad-except
logging.error("Upgrade loop failed! Error: %s", exp)
async def _do_docker_upgrade(self) -> None:
upgrade_intent, version_info = await asyncio.gather(
self.get_upgrade_intent(), self.get_versions(),
)
current_version = version_info.current_version
target_image = upgrade_intent.stable
if target_image != current_version:
logging.info(
"There is work to be done:\n"
" current: %s\n"
" to_upgrade: %s",
current_version,
target_image,
)
use_proxy = self.service.config["upgrader_factory"]\
.get("use_proxy", True)
await download_update(target_image, use_proxy)
await self.prepare_upgrade(
current_version,
pathlib.Path(MAGMA_GITHUB_PATH, "magma"),
)
# As a last step, update the IMAGE_VERSION in .env
sed_args = "sed -i s/IMAGE_VERSION={}/IMAGE_VERSION={}/g " \
"var/opt/magma/docker/.env".format(
current_version,
target_image,
)
logging.info(
"Successfully downloaded version %s! Awaiting docker "
"container recreation...", target_image,
)
await run_command(sed_args, shell=True, check=True)
else:
logging.info(
'Service is currently on image tag %s, '
'ignoring upgrade to tag %s, since they\'re equal.',
current_version, target_image,
)
async def download_update(
target_version: str,
use_proxy: bool,
) -> None:
"""
Download the images for the given tag and clones the github repo.
"""
await run_command(
"rm -rf {}".format(MAGMA_GITHUB_PATH), shell=True,
check=True,
)
await run_command(
"mkdir -p {}".format(MAGMA_GITHUB_PATH), shell=True,
check=True,
)
control_proxy_config = load_service_config('control_proxy')
await run_command(
"cp {} /usr/local/share/ca-certificates/rootCA.crt".
format(control_proxy_config['rootca_cert']), shell=True,
check=True,
)
await run_command("update-ca-certificates", shell=True, check=True)
if use_proxy:
git_clone_cmd = "git -c http.proxy=https://{}:{} -C {} clone {}".format(
control_proxy_config['bootstrap_address'],
control_proxy_config['bootstrap_port'], MAGMA_GITHUB_PATH,
MAGMA_GITHUB_URL,
)
else:
git_clone_cmd = "git -C {} clone {}".format(
MAGMA_GITHUB_PATH,
MAGMA_GITHUB_URL,
)
await run_command(git_clone_cmd, shell=True, check=True)
git_checkout_cmd = "git -C {}/magma checkout {}".format(
MAGMA_GITHUB_PATH, target_version,
)
await run_command(git_checkout_cmd, shell=True, check=True)
docker_login_cmd = "docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD " \
"$DOCKER_REGISTRY"
await run_command(docker_login_cmd, shell=True, check=True)
docker_pull_cmd = "IMAGE_VERSION={} docker-compose --project-directory " \
"/var/opt/magma/docker -f " \
"/var/opt/magma/docker/docker-compose.yml pull -q".\
format(target_version)
await run_command(docker_pull_cmd, shell=True, check=True)
class DockerUpgraderFactory(UpgraderFactory):
""" Returns an instance of the DockerUpgrader """
def create_upgrader(
self,
magmad_service: MagmaService,
loop: asyncio.AbstractEventLoop,
) -> DockerUpgrader:
return DockerUpgrader(magmad_service)