PK!resin_release_tool/__init__.pyPK!g4H resin_release_tool/cli.pyimport click from .releaser import ResinReleaser # get releaser object from the context pass_releaser = click.make_pass_decorator(ResinReleaser) @click.group() @click.option('--token', required=True, envvar='RESIN_TOKEN', metavar='TOKEN', help='Resin.io auth token') @click.option('--app', required=True, envvar='RESIN_APP', metavar='APP_ID', help='Resin App name') @click.pass_context def cli(ctx, app, token): """You can set app and token as environment variables, using RESIN_APP and RESIN_TOKEN """ ctx.obj = ResinReleaser(token, app) @cli.command() @pass_releaser def info(releaser): """Information of the application""" info = releaser.get_info() rolling = info['should_track_latest_release'] and 'Yes' or 'No' info_list = [ f"App Name: {info['app_name']}", f"Device Type: {info['device_type']}", f"In Commit: {info['commit']}", f"Rolling enabled: {rolling}"] click.echo('\n'.join(info_list)) @cli.command() @pass_releaser def disable_rolling(releaser): """Disables rolling releases in the application""" releaser.disable_rolling() click.echo("Disabled rolling") @cli.command() @pass_releaser def enable_rolling(releaser): """Enables rolling releases in the application""" releaser.enable_rolling() releases = list(releaser.get_releases().values()) if releases: commit = releases[0]['commit'] releaser.set_app_to_release(commit) click.echo("Enabled rolling") @cli.command() @pass_releaser def show_devices_status(releaser): """Show the status of the devices in the applications""" devices = releaser.get_devices_by_status() canaries = ', '.join( [c['uuid'][:6] for c in devices['canaries'].values()]) old_canaries = ', '.join( [c['uuid'][:6] for c in devices['old_canaries'].values()]) rest = list(devices['rest'].values()) rest_len = len(rest) rest_info = ', '.join([c['uuid'][:6] for c in rest[:10]]) if rest_len > 10: rest_info += f'... and {rest_len-10} more' click.echo(f'Canaries: {canaries}') click.echo(f'Old Canaries: {old_canaries}') click.echo(f'Rest of the Devices: {rest_info}') @cli.command() @click.argument('release_commit') @click.argument('canary_commit') @pass_releaser @click.pass_context def release(ctx, releaser, release_commit, canary_commit): """Sets release and canary commits""" if not releaser.is_valid_commit(release_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}') exit(2) ctx.invoke(info) click.echo('Devices:') ctx.invoke(show_devices_status) click.echo() confirm_text = 'Are you sure you want to set '\ 'release/canary to: "%s" "%s"?' % ( release_commit, canary_commit) if not click.confirm(confirm_text): click.echo('Cancelled!') exit(1) releaser.set_release(release_commit, canary_commit) @cli.command() @click.option('--count', default=10, help='How many') @pass_releaser def releases(releaser, count): """Show successful releases of the application""" releases = list(releaser.get_releases().values()) click.echo(f'Latest {count} releases:') for release in releases[:count]: click.echo(f'{release["end_timestamp"]} {release["commit"]}') if __name__ == '__main__': cli() PK!΅N N resin_release_tool/releaser.pyfrom functools import lru_cache from resin import Resin class ResinReleaser: def __init__(self, token, app_id): self.resin = Resin() self.resin.auth.login_with_token(token) self.models = self.resin.models self.app_id = app_id def get_info(self): return self.models.application.get_by_id(self.app_id) def get_canaries(self): tags = self.models.tag.device.get_all_by_application(self.app_id) canaries = [ tag['device']['__id'] for tag in tags if tag['tag_key'] == 'CANARY'] return canaries @lru_cache() def get_releases(self): releases = self.models.release.get_all_by_application(self.app_id) return { release['commit']: release for release in releases if release['status'] == 'success'} def is_valid_commit(self, commit): return commit in self.get_releases() def disable_rolling(self): self.models.application.disable_rolling_updates(self.app_id) def enable_rolling(self): self.models.application.enable_rolling_updates(self.app_id) def set_app_to_release(self, release): self.models.application.set_to_release(self.app_id, 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): 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()} def not_canary(device): return device['id'] not in canaries and \ device['should_be_running__release'] old_canaries = {device['id']: device for device in all_devices.values() if not_canary(device)} rest = { device['id']: device for device in all_devices.values() if device['id'] not in canaries and device['id'] not in old_canaries } return { 'canaries': canaries, 'old_canaries': old_canaries, 'rest': rest, } def set_release(self, release_hash, canary_hash=None): devices = self.get_devices_by_status() canaries = devices['canaries'] old_canaries = devices['old_canaries'] # Disable rolling releases print('Disabling rolling releases on the application') self.disable_rolling() if old_canaries: print('Reseting all canaries') # 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) 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) # We do this here to trigger update in all devices print('Setting up current release to: %s' % release_hash) self.set_app_to_release(release_hash) PK!Hkc7A3resin_release_tool-0.1.0.dist-info/entry_points.txtN+I/N.,()*J--JIM,N-ϱ CABz9V@PK!Қu&88*resin_release_tool-0.1.0.dist-info/LICENSEMIT License Copyright (c) 2018 The Mobility House GmbH Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HW"TT(resin_release_tool-0.1.0.dist-info/WHEEL A н#J."jm)Afb~ ڡ5 G7hiޅF4+-3ڦ/̖?XPK!H/\i+resin_release_tool-0.1.0.dist-info/METADATAT]O0}/$e "@ZUݤ )IAm}<-OoF(×y1h4BsT^Q8yU\oc p o.=BkY[8f*LTj%ra F-oEy4\>+&NeWLiTkԍ`Ei̅g3W LD".OWscģ@dq zeSgS^=ʔ"k^BuE-p::98;q֔06$|< ;a{D=6zi\9MEiI=A_ImIb#S%cGm[d`Bh;:49w+EI A{%"Urz $҈KbQ+M/&U5&ĊB%HDud3+ٖ$ \ ʛdN{OFxwُ}HRJ @Zɂ_؁8̮7:zĴGBf6F6NI=B4w@ka^n3L mh\S|E",WH' jYjnJ4&Lyk%5 Q5M]xN{kּ@Z!y}똩h kLqCϚYxme+?PK!H7/)resin_release_tool-0.1.0.dist-info/RECORD=s@> bq DSE$| 3"/DR}M9 4F/b^y]%mVA̢иYTR~}wvgzpo乙WA- ܻtpeCĎ# Ǩzf.s_YômGqFkp 8MF9@͞ |eqزȈl~6܇55-K/9I_9y1Pd ^F~%HJPӵ dX!iL5g'Z8n4zCBs*/Ʉ֏hMI2zH_ʪ}"v /fe}.z On 1 d*üPK!resin_release_tool/__init__.pyPK!g4H <resin_release_tool/cli.pyPK!΅N N *resin_release_tool/releaser.pyPK!Hkc7A3resin_release_tool-0.1.0.dist-info/entry_points.txtPK!Қu&88*<resin_release_tool-0.1.0.dist-info/LICENSEPK!HW"TT( resin_release_tool-0.1.0.dist-info/WHEELPK!H/\i+V!resin_release_tool-0.1.0.dist-info/METADATAPK!H7/)o$resin_release_tool-0.1.0.dist-info/RECORDPKM&