Skip to content

Commit

Permalink
Append to existing apps.json preload state instead of overwriting
Browse files Browse the repository at this point in the history
If the image we are preloading to contains an existing `apps.json` append
the preloaded app to it instead of overwriting it.

Fixes balena-io/balena-cli#2262

Change-type: patch
Signed-off-by: Alex Gonzalez <[email protected]>
  • Loading branch information
alexgg committed May 25, 2021
1 parent 3706403 commit e3a74c6
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion src/preload.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,59 @@ def write_resin_device_pinning(app_data, output):
)


# Remove once supervisor is part of the hostapp
def isSupervisor(app, images):
if len(app['services']) != 1:
# Supervisor is single service
return False

# Fetch image name from state app
for service in app['services']:
image_name = app['services'][service]['image']

for image in images:
if re.match(image, image_name):
return True
return False


def write_apps_json(data, output):
"""Writes data dict to output as json"""
"""Updates or creates preloaded state"""
if os.path.isfile(output):
with open(output, "r") as f:
current_state = json.load(f)

# Check state version consistency
for uuid in data.keys():
if len(uuid) != 32 and len(uuid) != 62:
raise Exception("Refusing to preload v2 target state app")

# Fetch container image details from image
images, supervisor_version, balena_os_version = (
get_images_and_supervisor_version()
)

for uuid in list(current_state['apps'].keys()):
if len(uuid) != 32 and len(uuid) != 62:
raise Exception("Invalid current state")

# Remove all user apps from current state
app = current_state['apps'][uuid]
# Skip the hostOS app
if 'isHost' in app:
if app['isHost'] == 'true':
continue
# Skip the supervisor
if isSupervisor(app, images):
continue
del current_state['apps'][uuid]

# Add provided apps
for uuid in data['apps'].keys():
current_state['apps'][uuid] = data['apps'][uuid]
current_state['pinDevice'] = data['pinDevice']
current_state['config'] = data['config']
data = current_state
with open(output, "w") as f:
json.dump(data, f, indent=4, sort_keys=True)

Expand Down

0 comments on commit e3a74c6

Please sign in to comment.