Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resin_release_tool/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def release(ctx, releaser, release_commit, canary_commit):
click.echo(f'Invalid release commit: {release_commit}')
exit(2)
if not releaser.is_valid_commit(canary_commit):
click.echo('Invalid canary commit: {canary_commit}')
click.echo(f'Invalid canary commit: {canary_commit}')
exit(2)

ctx.invoke(info)
Expand Down
37 changes: 30 additions & 7 deletions resin_release_tool/releaser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ def __init__(self, token, app_id):
self.app_id = app_id

def get_info(self):
""" Gets some information from the resin application """
return self.models.application.get_by_id(self.app_id)

def get_canaries(self):
def _get_canaries(self):
tags = self.models.tag.device.get_all_by_application(self.app_id)
canaries = [
tag['device']['__id'] for tag in tags
Expand All @@ -30,31 +31,43 @@ def get_releases(self):
if release['status'] == 'success'}

def is_valid_commit(self, commit):
""" Checks if the commit is part of a past release

commit : str
Commit hash

Returns
-------
True if it's part of it, False otherwise
"""
return commit in self.get_releases()

def disable_rolling(self):
""" Disables rolling release for the app"""
self.models.application.disable_rolling_updates(self.app_id)

def enable_rolling(self):
""" Enables rolling release for the app"""
self.models.application.enable_rolling_updates(self.app_id)

def set_app_to_release(self, release):
""" Sets app to release"""
self.models.application.set_to_release(self.app_id, release)

def set_device_to_release(self, device, release):
def _set_device_to_release(self, device, release):
uuid = device['uuid']
self.models.device.set_to_release(uuid, release)

def get_all_devices(self):
def _get_all_devices(self):
devices = self.models.device.get_all_by_application_id(self.app_id)
return {d['id']: d for d in devices}

@lru_cache()
def get_devices_by_status(self):
"""Group devices by status: canary, old_canary, rest
"""
all_devices = self.get_all_devices()
canaries = {c: all_devices[c] for c in self.get_canaries()}
all_devices = self._get_all_devices()
canaries = {c: all_devices[c] for c in self._get_canaries()}

def not_canary(device):
return device['id'] not in canaries and \
Expand All @@ -75,6 +88,16 @@ def not_canary(device):
}

def set_release(self, release_hash, canary_hash=None):
""" Sets a release for the given to the non_canary devices to given
commit id release_hash and to the canary devices to the commit id
canary_hash

release_hash : str
Commit ID to set the devices excluding the canary devices

canary_hash : str (default=None)
Commit ID to set the canary devices
"""
devices = self.get_devices_by_status()

canaries = devices['canaries']
Expand All @@ -89,14 +112,14 @@ def set_release(self, release_hash, canary_hash=None):
# Reset old canaries to app release
for old_canary in old_canaries.values():
print(old_canary['device_name'])
self.set_device_to_release(old_canary, None)
self._set_device_to_release(old_canary, None)

if canaries:
print('Setting canaries')
# Set canaries to current canary release
for canary in canaries.values():
print(canary['device_name'])
self.set_device_to_release(canary, canary_hash)
self._set_device_to_release(canary, canary_hash)

# We do this here to trigger update in all devices
print('Setting up current release to: %s' % release_hash)
Expand Down