Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 4 more methods #10

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "resin-release-tool"
version = "0.1.0"
version = "0.1.1"
readme = "README.md"
description = "Release tool to have canary in resin.io"
homepage = "https://github.com/mobilityhouse/resin-release-tool"
Expand Down
31 changes: 31 additions & 0 deletions resin_release_tool/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,36 @@ def releases(releaser, count):
click.echo(f'{release["end_timestamp"]} {release["commit"]}')


@cli.command()
@pass_releaser
def show_tags_per_device(releaser):
""" Shows device's tags"""
tag = releaser.get_tags_per_device()
click.echo(tag)

@cli.command()
@pass_releaser
def show_device_env_vars(releaser):
"""Shows overriden ENV VARS in each device"""
tag = releaser.get_tags_per_device()
dequis marked this conversation as resolved.
Show resolved Hide resolved
env_vars = releaser.get_device_env_vars()
click.echo(env_vars)

@cli.command()
@pass_releaser
def show_app_env_vars(releaser):
"""Shows application ENV VARS with default values"""
env_vars = releaser.get_app_env_vars()
click.echo(env_vars)

@cli.command()
@pass_releaser
def set_device_env_vars(releaser):
"""Override ENV VARS with with values of tags"""
tags_per_device = releaser.get_tags_per_device()
device_env_vars = releaser.get_device_env_vars()
releaser.set_device_env_vars_from_tags(tags_per_device, device_env_vars)


if __name__ == '__main__':
cli()
65 changes: 65 additions & 0 deletions resin_release_tool/releaser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def __init__(self, token, app_id):

self.app_id = app_id

self.uuid_list = [] #list of all the UUIDs in the app

def get_info(self):
return self.models.application.get_by_id(self.app_id)

Expand All @@ -21,6 +23,69 @@ def get_canaries(self):
if tag['tag_key'] == 'CANARY']
return canaries

def get_tags_per_device(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it makes it clearer to have a short docstring here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe

all_apps = self.models.application.get_all()
if len(next((app for app in all_apps if app['id'] == int(self.app_id)),{})) != 0: #checks if the app exists
me-sosa marked this conversation as resolved.
Show resolved Hide resolved
print("Found app with app_id ", self.app_id, "in Balena")
app_info = next((app for app in all_apps if app['id'] == int(self.app_id)),{})
all_devices = self.models.device.get_all_by_application_id(app_info['id'])
me-sosa marked this conversation as resolved.
Show resolved Hide resolved
tags_per_device = [{'uuid':device['uuid'],
'tags':self.models.tag.device.get_all_by_device(device['uuid'])}
for device in all_devices]
entries_to_remove = ('id', 'device', '__metadata')
for device in tags_per_device: #removes all the key/values except, "tag_key" and "value"
for tag in device['tags']:
for k in entries_to_remove:
tag.pop(k)
dequis marked this conversation as resolved.
Show resolved Hide resolved
for device in tags_per_device:
dict_of_vars = {}
for tag in device['tags']:
dict_of_vars[tag['tag_key']] = tag['value']
del(device['tags'])
me-sosa marked this conversation as resolved.
Show resolved Hide resolved
device['tags'] = dict_of_vars
self.uuid_list = [uuid['uuid'] for uuid in tags_per_device]
dequis marked this conversation as resolved.
Show resolved Hide resolved
return tags_per_device
else:
print("Could not find the app with app_id ", self.app_id)
me-sosa marked this conversation as resolved.
Show resolved Hide resolved

def get_app_env_vars(self):
allvars = self.models.environment_variables.application.get_all(int(self.app_id))
me-sosa marked this conversation as resolved.
Show resolved Hide resolved
list_of_app_env_vars = [{'id':var['id'], var['name']:var['value']} for var in allvars]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason this format was chosen?

If I'm understanding things right, it's:

[{"id": 1234, "something": "its value"}, {"id": 2345, "foobar": "baz"}]

Which seems odd to me, using var['name'] as dictionary key makes it hard to access stuff programmatically (you'd have to iterate over the dictionary's keys and exclude the one named "id" to access the actual key/value pair)

It would make things simpler to either keep the original format returned by get_all() (since the name is reliably in the dictionary key named 'name') or rearrange it into a dictionary like this:

{"something": {"id": 1234, "value": "its value"}, "foobar": {"id": 2345, "value": "baz"}}

So you can just access dict["something"]["value"] without for loops.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been refactored now, thanks for your help!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function looks the same to me though.

return list_of_app_env_vars
me-sosa marked this conversation as resolved.
Show resolved Hide resolved

def get_device_env_vars(self):
list_of_env_vars_per_device = []
for device in self.uuid_list:
allvars = self.models.environment_variables.device.get_all(device)
list_of_vars = [{'id':var['id'], var['name']:var['value']} for var in allvars]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems very similar to the code in get_app_env_vars, consider putting that in a separate function.

list_of_env_vars_per_device.append({device:list_of_vars})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty much the comment on structure as above: Any particular reason to choose this...

[{"some_device_id": [...list...]}, {"other_device_id": [...list...]}]

over just this?

{"some_device_id": [...list...], "other_device_id": [...list...]}

return list_of_env_vars_per_device

def set_device_env_vars_from_tags(self, tags_per_device, device_env_vars):
for device in tags_per_device:
uuid = device['uuid']
print("----------------------------------------------------------")
print("Device: ", uuid)
for tag,value in device['tags'].items():
print('--')
try:
self.models.environment_variables.device.create(device['uuid'], tag, value)
print("creating/overriding var: ", device['uuid'], tag, value)
except:
print("var", tag, "already exists!")
for elem in device_env_vars:
if uuid in elem:
list_of_env_vars = elem[uuid]
for var in list_of_env_vars:
if tag in var:
var_id = var['id']
old_val = var[tag]
if old_val != value:
print("Updating var ", tag, "with value", value)
self.models.environment_variables.device.update(var_id, value)
else:
print("Not updating var ", tag, "since the value did not change!")

@lru_cache()
def get_releases(self):
releases = self.models.release.get_all_by_application(self.app_id)
Expand Down