|
16 | 16 | from swsssdk import SonicV2Connector
|
17 | 17 |
|
18 | 18 | from .bootloader import get_bootloader
|
19 |
| -from .common import run_command |
| 19 | +from .common import run_command, run_command_or_raise |
| 20 | +from .exception import SonicRuntimeException |
20 | 21 |
|
21 | 22 |
|
22 | 23 | # Global Config object
|
@@ -208,6 +209,54 @@ def print_deprecation_warning(deprecated_cmd_or_subcmd, new_cmd_or_subcmd):
|
208 | 209 | fg="red", err=True)
|
209 | 210 | click.secho("Please use '{}' instead".format(new_cmd_or_subcmd), fg="red", err=True)
|
210 | 211 |
|
| 212 | +def update_sonic_environment(click, binary_image_version): |
| 213 | + """Prepare sonic environment variable using incoming image template file. If incoming image template does not exist |
| 214 | + use current image template file. |
| 215 | + """ |
| 216 | + def mount_next_image_fs(squashfs_path, mount_point): |
| 217 | + run_command_or_raise(["mkdir", "-p", mount_point]) |
| 218 | + run_command_or_raise(["mount", "-t", "squashfs", squashfs_path, mount_point]) |
| 219 | + |
| 220 | + def umount_next_image_fs(mount_point): |
| 221 | + run_command_or_raise(["umount", "-rf", mount_point]) |
| 222 | + run_command_or_raise(["rm", "-rf", mount_point]) |
| 223 | + |
| 224 | + SONIC_ENV_TEMPLATE_FILE = os.path.join("usr", "share", "sonic", "templates", "sonic-environment.j2") |
| 225 | + SONIC_VERSION_YML_FILE = os.path.join("etc", "sonic", "sonic_version.yml") |
| 226 | + |
| 227 | + sonic_version = re.sub("SONiC-OS-", '', binary_image_version) |
| 228 | + new_image_dir = os.path.join('/', "host", "image-{0}".format(sonic_version)) |
| 229 | + new_image_squashfs_path = os.path.join(new_image_dir, "fs.squashfs") |
| 230 | + new_image_mount = os.path.join('/', "tmp", "image-{0}-fs".format(sonic_version)) |
| 231 | + env_dir = os.path.join(new_image_dir, "sonic-config") |
| 232 | + env_file = os.path.join(env_dir, "sonic-environment") |
| 233 | + |
| 234 | + try: |
| 235 | + mount_next_image_fs(new_image_squashfs_path, new_image_mount) |
| 236 | + |
| 237 | + next_sonic_env_template_file = os.path.join(new_image_mount, SONIC_ENV_TEMPLATE_FILE) |
| 238 | + next_sonic_version_yml_file = os.path.join(new_image_mount, SONIC_VERSION_YML_FILE) |
| 239 | + |
| 240 | + sonic_env = run_command_or_raise([ |
| 241 | + "sonic-cfggen", |
| 242 | + "-d", |
| 243 | + "-y", |
| 244 | + next_sonic_version_yml_file, |
| 245 | + "-t", |
| 246 | + next_sonic_env_template_file, |
| 247 | + ]) |
| 248 | + os.mkdir(env_dir, 0o755) |
| 249 | + with open(env_file, "w+") as ef: |
| 250 | + print >>ef, sonic_env |
| 251 | + os.chmod(env_file, 0o644) |
| 252 | + except SonicRuntimeException as ex: |
| 253 | + click.secho("Warning: SONiC environment variables are not supported for this image: {0}".format(str(ex)), |
| 254 | + fg="red", err=True) |
| 255 | + if os.path.exists(env_file): |
| 256 | + os.remove(env_file) |
| 257 | + os.rmdir(env_dir) |
| 258 | + finally: |
| 259 | + umount_next_image_fs(new_image_mount) |
211 | 260 |
|
212 | 261 | # Main entrypoint
|
213 | 262 | @click.group(cls=AliasedGroup)
|
@@ -274,6 +323,8 @@ def install(url, force, skip_migration=False):
|
274 | 323 | else:
|
275 | 324 | run_command('config-setup backup')
|
276 | 325 |
|
| 326 | + update_sonic_environment(click, binary_image_version) |
| 327 | + |
277 | 328 | # Finally, sync filesystem
|
278 | 329 | run_command("sync;sync;sync")
|
279 | 330 | run_command("sleep 3") # wait 3 seconds after sync
|
|
0 commit comments