|
| 1 | +# -*- coding: utf-8 -*- {{{ |
| 2 | +# vim: set fenc=utf-8 ft=python sw=4 ts=4 sts=4 et: |
| 3 | +# |
| 4 | +# Copyright 2020, Battelle Memorial Institute. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +# This material was prepared as an account of work sponsored by an agency of |
| 19 | +# the United States Government. Neither the United States Government nor the |
| 20 | +# United States Department of Energy, nor Battelle, nor any of their |
| 21 | +# employees, nor any jurisdiction or organization that has cooperated in the |
| 22 | +# development of these materials, makes any warranty, express or |
| 23 | +# implied, or assumes any legal liability or responsibility for the accuracy, |
| 24 | +# completeness, or usefulness or any information, apparatus, product, |
| 25 | +# software, or process disclosed, or represents that its use would not infringe |
| 26 | +# privately owned rights. Reference herein to any specific commercial product, |
| 27 | +# process, or service by trade name, trademark, manufacturer, or otherwise |
| 28 | +# does not necessarily constitute or imply its endorsement, recommendation, or |
| 29 | +# favoring by the United States Government or any agency thereof, or |
| 30 | +# Battelle Memorial Institute. The views and opinions of authors expressed |
| 31 | +# herein do not necessarily state or reflect those of the |
| 32 | +# United States Government or any agency thereof. |
| 33 | +# |
| 34 | +# PACIFIC NORTHWEST NATIONAL LABORATORY operated by |
| 35 | +# BATTELLE for the UNITED STATES DEPARTMENT OF ENERGY |
| 36 | +# under Contract DE-AC05-76RL01830 |
| 37 | +# }}} |
| 38 | + |
| 39 | +import sys |
| 40 | +import shutil |
| 41 | +from pathlib import Path |
| 42 | +import glob |
| 43 | +import re |
| 44 | +from gevent import monkey as curious_george |
| 45 | +curious_george.patch_all(thread=False, select=False) |
| 46 | + |
| 47 | +from volttron.platform import get_home |
| 48 | +from volttron.platform.aip import AIPplatform |
| 49 | +from volttron.platform.instance_setup import fail_if_instance_running |
| 50 | + |
| 51 | + |
| 52 | +def get_aip(): |
| 53 | + """Get AIPplatform to interface with agent directories in vhome""" |
| 54 | + |
| 55 | + vhome = get_home() |
| 56 | + options = type("Options", (), dict(volttron_home=vhome)) |
| 57 | + aip = AIPplatform(options) |
| 58 | + return aip |
| 59 | + |
| 60 | + |
| 61 | +def move_historian_cache_files(aip): |
| 62 | + """ |
| 63 | + Moves any keystore.json from agent-data to dist-info. |
| 64 | + Only applies to agents in auth file. |
| 65 | + """ |
| 66 | + |
| 67 | + vhome = Path(aip.env.volttron_home) |
| 68 | + install_dir = vhome.joinpath("agents") |
| 69 | + # pattern example - (/vhome/agents/uuid/agentname-version/)(backup.sqlite) |
| 70 | + # pattern example - (vhome/agents/uuid/agentname-version/)(data/subdir/sqlitehistoriandb.sqlite) |
| 71 | + pattern = "(" + str(install_dir) + "/[^/]*/[^/]*/)(.*)" |
| 72 | + re_pattern = re.compile(pattern) |
| 73 | + # Look for all .sqlite files in installed agents |
| 74 | + print(f"Attempting to move backup.sqlite files in {install_dir} into corresponding agent-data directory") |
| 75 | + # currently this is only used for backup.sqlite |
| 76 | + # In 9.0 we could use the same code for *.sqlite files |
| 77 | + # for example ones created by sqlitetagging, topic watcher, weather etc. when we make agent-data folder default |
| 78 | + # agent write directory for all core agents |
| 79 | + glob_path = str(install_dir.joinpath("**/backup.sqlite")) |
| 80 | + for sqlite_file in glob.glob(glob_path, recursive=True): |
| 81 | + result = re_pattern.match(sqlite_file).groups() |
| 82 | + agent_dir = result[0] # <volttron home>/agents/uuid/<agent name-version> |
| 83 | + source_file = result[1].split('/', 1)[0] # file or directory name to be moved to agent-data folder |
| 84 | + source_path = str(Path(agent_dir).joinpath(source_file)) |
| 85 | + dest_dir = aip.get_agent_data_dir(agent_path=agent_dir) |
| 86 | + if source_path != dest_dir: |
| 87 | + # if file is not already in agent-data dir |
| 88 | + result = shutil.move(source_path, dest_dir) |
| 89 | + |
| 90 | + # print from uuid dir name so that it is easy to read |
| 91 | + print_src = source_path.split(str(install_dir)+"/")[1] |
| 92 | + print_dest = result.split(str(install_dir) + "/")[1] |
| 93 | + print(f"Moved {print_src} to {print_dest}") |
| 94 | + |
| 95 | + |
| 96 | +def main(): |
| 97 | + """Upgrade auth file to function with dynamic rpc authorizations""" |
| 98 | + |
| 99 | + fail_if_instance_running() |
| 100 | + aip = get_aip() |
| 101 | + move_historian_cache_files(aip) |
| 102 | + print("") |
| 103 | + print("Moving historian backup files complete. " |
| 104 | + "You can now safely upgrade historian agents other than SQLITE Historian with vctl install --force. " |
| 105 | + "If using using SQLite historian please back up and restore sqlite historian's db manually") |
| 106 | + |
| 107 | + |
| 108 | +def _main(): |
| 109 | + """ Wrapper for main function""" |
| 110 | + |
| 111 | + try: |
| 112 | + sys.exit(main()) |
| 113 | + except KeyboardInterrupt: |
| 114 | + sys.exit(1) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + _main() |
0 commit comments