Skip to content

Commit

Permalink
Merge pull request #67 from NabuCasa/dev
Browse files Browse the repository at this point in the history
0.14
  • Loading branch information
balloob authored Jun 11, 2019
2 parents efd9909 + 44f0bb6 commit b58090e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
13 changes: 11 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,18 @@ jobs:
displayName: 'Use Python 3.7'
inputs:
versionSpec: '3.7'
- script: pip install twine
- script: |
setup_version="$(python setup.py -V)"
branch_version="$(Build.SourceBranchName)"
if [ "${setup_version}" != "${branch_version}" ]; then
echo "Version of tag ${branch_version} don't match with ${setup_version}!"
exit 1
fi
displayName: 'Check version of branch/tag'
- script: pip install twine wheel
displayName: 'Install twine'
- script: python setup.py sdist
- script: python setup.py sdist bdist_wheel
displayName: 'Build package'
- script: |
export TWINE_USERNAME="$(twineUser)"
Expand Down
26 changes: 21 additions & 5 deletions hass_nabucasa/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

_LOGGER = logging.getLogger(__name__)

RENEW_IF_EXPIRES_DAYS = 25
WARN_RENEW_FAILED_DAYS = 18


class RemoteError(Exception):
"""General remote error."""
Expand Down Expand Up @@ -307,26 +310,39 @@ async def _certificate_handler(self) -> None:
"""Handle certification ACME Tasks."""
try:
while True:
await asyncio.sleep(utils.next_midnight())
await asyncio.sleep(utils.next_midnight() + random.randint(1, 3600))

# Backend not initialize / No certificate issue now
if not self._snitun:
await self.load_backend()
continue

# Renew certificate?
if self._acme.expire_date > utils.utcnow() + timedelta(days=25):
if self._acme.expire_date > utils.utcnow() + timedelta(
days=RENEW_IF_EXPIRES_DAYS
):
continue

# Renew certificate
try:
await self._acme.issue_certificate()
await self.close_backend()

# Wait until backend is cleaned
await asyncio.sleep(5)
await self.load_backend()
except AcmeClientError:
_LOGGER.warning(
"Renew of ACME certificate fails. Try it lager again"
)
# Only log as warning if we have a certain amount of days left
if (
self._acme.expire_date
> utils.utcnow()
< timedelta(days=WARN_RENEW_FAILED_DAYS)
):
meth = _LOGGER.warning
else:
meth = _LOGGER.debug

meth("Renewal of ACME certificate failed. Please try again later.")

except asyncio.CancelledError:
pass
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup

VERSION = "0.13"
VERSION = "0.14"

setup(
name="hass-nabucasa",
Expand Down
8 changes: 6 additions & 2 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,9 @@ async def test_certificate_task_no_backend(

acme_mock.expire_date = valid

with patch("hass_nabucasa.utils.next_midnight", return_value=0) as mock_midnight:
with patch(
"hass_nabucasa.utils.next_midnight", return_value=0
) as mock_midnight, patch("random.randint", return_value=0):
remote._acme_task = loop.create_task(remote._certificate_handler())

await asyncio.sleep(0.1)
Expand Down Expand Up @@ -457,7 +459,9 @@ async def test_certificate_task_renew_cert(

acme_mock.expire_date = utcnow() + timedelta(days=-40)

with patch("hass_nabucasa.utils.next_midnight", return_value=0) as mock_midnight:
with patch("hass_nabucasa.utils.next_midnight", return_value=0), patch(
"random.randint", return_value=0
):
remote._acme_task = loop.create_task(remote._certificate_handler())

await remote.load_backend()
Expand Down

0 comments on commit b58090e

Please sign in to comment.