Skip to content

Catch exception for non-JSON items in Dashboard ZIP #62

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

Merged
merged 1 commit into from
Aug 13, 2018

Conversation

philrz
Copy link
Contributor

@philrz philrz commented Aug 10, 2018

@harrysx had a use case where he'd taken a ZIP of dashboards originally created from download_dashboards.py, unzipped it to trim it down to a subset, then re-zipped it. The restore_dashboards.py script ultimately choked on this with the error:

Traceback (most recent call last):
  File "restore_dashboards.py", line 42, in <module>
    j = json.loads(data)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

The contents of the repackaged ZIP:

# unzip -l harry_k8s_dashboards.zip
Archive:  harry_k8s_dashboards.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2018-08-03 11:52   k8s_dashboards/
     6148  2018-08-03 11:56   k8s_dashboards/.DS_Store
        0  2018-08-10 10:36   __MACOSX/
        0  2018-08-10 10:36   __MACOSX/k8s_dashboards/
      120  2018-08-03 11:56   __MACOSX/k8s_dashboards/._.DS_Store
     1880  2018-08-03 12:00   k8s_dashboards/7110
      474  2018-08-03 12:00   __MACOSX/k8s_dashboards/._7110
---------                     -------
     8622                     7 files

The previous dumb version of the restore script just expects to find only valid JSON files in the ZIP. That's fine if one could assume only unmodified ZIPs that came from the download script, but this repackaging is innocent enough that we should be able to handle it.

This repackaged ZIP tripped up the restore in a couple ways:

  1. The bare directory entry k8s_dashboards/. One could exclude such entries if repackaging with the -D option, but it's not default behavior.
  2. The excess Mac files like ._.DS_Store are in there regardless.

This PR gets around all these issues by simply catching the exception from any non-JSON junk found in the ZIP, then notes what it was and that it's being skipped.

While I was at it, I also added a line to print the name of each Dashboard successfully restored.

@philrz
Copy link
Contributor Author

philrz commented Aug 13, 2018

@harrysx did a test with the branch and it worked on his Mac. Merging.

@philrz philrz merged commit fb2efb5 into master Aug 13, 2018
@philrz philrz deleted the dashboard-restore-skip-non-json branch August 13, 2018 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants