Skip to content

Commit 1fdd243

Browse files
author
Sergio Schvezov
authored
build providers: remove tzdata workaround (#2935)
This workaround was put in place, but it is the wrong fix. The underlying problem was fixed with the addition of --preserve-env on commit: 97fdf64 Signed-off-by: Sergio Schvezov <sergio.schvezov@canonical.com>
1 parent 534ee1c commit 1fdd243

3 files changed

Lines changed: 10 additions & 49 deletions

File tree

snapcraft/internal/build_providers/_base_provider.py

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
22
#
3-
# Copyright (C) 2018-2019 Canonical Ltd
3+
# Copyright (C) 2018-2020 Canonical Ltd
44
#
55
# This program is free software: you can redistribute it and/or modify
66
# it under the terms of the GNU General Public License version 3 as
@@ -40,20 +40,7 @@ def _get_platform() -> str:
4040
return sys.platform
4141

4242

43-
def _get_tzdata(timezone_filepath=os.path.join(os.path.sep, "etc", "timezone")) -> str:
44-
"""Return the host's timezone from timezone_filepath or Etc/UTC on error."""
45-
try:
46-
with open(timezone_filepath) as timezone_file:
47-
timezone = timezone_file.read().strip()
48-
except FileNotFoundError:
49-
timezone = "Etc/UTC"
50-
51-
return timezone
52-
53-
54-
# cloud-init's timezone keyword is not used as it requires tzdata to be installed
55-
# and the images used may not have it preinstalled.
56-
_CLOUD_USER_DATA_TMPL = dedent(
43+
_CLOUD_USER_DATA = dedent(
5744
"""\
5845
#cloud-config
5946
manage_etc_hosts: true
@@ -62,8 +49,6 @@ def _get_tzdata(timezone_filepath=os.path.join(os.path.sep, "etc", "timezone"))
6249
mode: growpart
6350
devices: ["/"]
6451
ignore_growroot_disabled: false
65-
runcmd:
66-
- ["ln", "-s", "../usr/share/zoneinfo/{timezone}", "/etc/localtime"]
6752
write_files:
6853
- path: /root/.bashrc
6954
permissions: 0644
@@ -76,7 +61,7 @@ def _get_tzdata(timezone_filepath=os.path.join(os.path.sep, "etc", "timezone"))
7661
content: |
7762
#!/bin/bash
7863
if [[ "$PWD" =~ ^$HOME.* ]]; then
79-
path="${{PWD/#$HOME/\ ..}}"
64+
path="${PWD/#$HOME/\ ..}"
8065
if [[ "$path" == " .." ]]; then
8166
ps1=""
8267
else
@@ -350,20 +335,15 @@ def _setup_snapcraft(self) -> None:
350335

351336
snap_injector.apply()
352337

353-
def _get_cloud_user_data_string(self, timezone=_get_tzdata()) -> str:
354-
return _CLOUD_USER_DATA_TMPL.format(timezone=timezone)
355-
356-
def _get_cloud_user_data(self, timezone=_get_tzdata()) -> str:
338+
def _get_cloud_user_data(self) -> str:
357339
cloud_user_data_filepath = os.path.join(
358340
self.provider_project_dir, "user-data.yaml"
359341
)
360342
if os.path.exists(cloud_user_data_filepath):
361343
return cloud_user_data_filepath
362344

363-
user_data = self._get_cloud_user_data_string(timezone=timezone)
364-
365345
with open(cloud_user_data_filepath, "w") as cloud_user_data_file:
366-
print(user_data, file=cloud_user_data_file, end="")
346+
print(_CLOUD_USER_DATA, file=cloud_user_data_file, end="")
367347

368348
return cloud_user_data_filepath
369349

snapcraft/internal/build_providers/_lxd/_lxd.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import warnings
2323
from typing import Dict, Optional, Sequence
2424

25+
from .._base_provider import _CLOUD_USER_DATA
2526
from .._base_provider import Provider
2627
from .._base_provider import errors
2728
from ._images import get_image_source
@@ -199,7 +200,7 @@ def _launch(self) -> None:
199200
raise errors.ProviderLaunchError(
200201
provider_name=self._get_provider_name(), error_message=lxd_api_error
201202
) from lxd_api_error
202-
container.config["user.user-data"] = self._get_cloud_user_data_string()
203+
container.config["user.user-data"] = _CLOUD_USER_DATA
203204
# This is setup by cloud init, but set it here to be on the safer side.
204205
container.config["environment.SNAPCRAFT_BUILD_ENVIRONMENT"] = "managed-host"
205206
container.save(wait=True)

tests/unit/build_providers/test_base_provider.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
22
#
3-
# Copyright (C) 2018-2019 Canonical Ltd
3+
# Copyright (C) 2018-2020 Canonical Ltd
44
#
55
# This program is free software: you can redistribute it and/or modify
66
# it under the terms of the GNU General Public License version 3 as
@@ -23,8 +23,7 @@
2323
from testtools.matchers import Equals, EndsWith, DirExists, FileContains, Not
2424

2525
from . import BaseProviderBaseTest, MacBaseProviderWithBasesBaseTest, ProviderImpl
26-
from snapcraft.internal.build_providers import errors, _base_provider
27-
from tests import unit
26+
from snapcraft.internal.build_providers import errors
2827

2928

3029
class BaseProviderTest(BaseProviderBaseTest):
@@ -431,9 +430,7 @@ def test_get(self):
431430
provider = ProviderImpl(project=self.project, echoer=self.echoer_mock)
432431
os.makedirs(provider.provider_project_dir)
433432

434-
cloud_data_filepath = provider._get_cloud_user_data(
435-
timezone="America/Argentina/Cordoba"
436-
)
433+
cloud_data_filepath = provider._get_cloud_user_data()
437434
self.assertThat(
438435
cloud_data_filepath,
439436
FileContains(
@@ -446,8 +443,6 @@ def test_get(self):
446443
mode: growpart
447444
devices: ["/"]
448445
ignore_growroot_disabled: false
449-
runcmd:
450-
- ["ln", "-s", "../usr/share/zoneinfo/America/Argentina/Cordoba", "/etc/localtime"]
451446
write_files:
452447
- path: /root/.bashrc
453448
permissions: 0644
@@ -474,18 +469,3 @@ def test_get(self):
474469
)
475470
),
476471
)
477-
478-
479-
class GetTzDataTest(unit.TestCase):
480-
def test_path_found(self):
481-
with open("timezone", "w") as timezone_file:
482-
print("America/Argentina/Cordoba", file=timezone_file)
483-
484-
self.assertThat(
485-
_base_provider._get_tzdata("timezone"), Equals("America/Argentina/Cordoba")
486-
)
487-
488-
def test_path_not_found(self):
489-
self.assertThat(
490-
_base_provider._get_tzdata("timezone-not-found"), Equals("Etc/UTC")
491-
)

0 commit comments

Comments
 (0)