|
| 1 | +# |
| 2 | +# Copyright (C) 2020-2021 Rodrigo A. Melo |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +# |
| 17 | + |
| 18 | +"""openflow.configure |
| 19 | +
|
| 20 | +A Class to configure the OCI engine, the containers and the name of the |
| 21 | +underlying FOSS tools. |
| 22 | +""" |
| 23 | + |
| 24 | + |
| 25 | +import os |
| 26 | +from yaml import safe_load, dump |
| 27 | + |
| 28 | + |
| 29 | +class ConfigureTools: |
| 30 | + """Configure Tools.""" |
| 31 | + |
| 32 | + def __init__(self, filename='.openflow.yml'): |
| 33 | + """Class constructor.""" |
| 34 | + userfile = os.getenv('OPENFLOW_FILE') |
| 35 | + homefile = os.path.join(os.path.expanduser('~'), filename) |
| 36 | + projfile = os.path.join(os.path.dirname(__file__), 'configure.yml') |
| 37 | + if userfile is not None and os.path.exists(userfile): |
| 38 | + filepath = userfile |
| 39 | + elif os.path.exists(filename): |
| 40 | + filepath = filename |
| 41 | + elif os.path.exists(homefile): |
| 42 | + filepath = homefile |
| 43 | + else: |
| 44 | + filepath = projfile |
| 45 | + self.configs = {} |
| 46 | + with open(filepath, 'r') as file: |
| 47 | + self.configs = safe_load(file) |
| 48 | + |
| 49 | + def get_command(self, tool): |
| 50 | + """Get the command-line needed to run a tool.""" |
| 51 | + engine = self.configs['engine']['name'] |
| 52 | + name = self.configs['tools'][tool]['name'] |
| 53 | + if engine is not None and os.getenv('OPENFLOW_OFF') is None: |
| 54 | + oci = [ |
| 55 | + engine, |
| 56 | + 'run --rm', |
| 57 | + '-v ' + (' -v ').join(self.configs['engine']['volumes']), |
| 58 | + '-w ' + self.configs['engine']['work'], |
| 59 | + self.configs['engine'].get('options', None), |
| 60 | + self.configs['tools'][tool].get('options', None), |
| 61 | + self.configs['tools'][tool]['container'], |
| 62 | + name |
| 63 | + ] |
| 64 | + return ' '.join(list(filter(None, oci))) |
| 65 | + return name |
| 66 | + |
| 67 | + def get_tools(self): |
| 68 | + """Returns the list of configured tools.""" |
| 69 | + return sorted(list(self.configs['tools'].keys())) |
| 70 | + |
| 71 | + def dump(self): |
| 72 | + """Dumps the configuration in YAML format (debug purpouses).""" |
| 73 | + return dump(self.configs) |
| 74 | + |
| 75 | + def set_engine(self, engine): |
| 76 | + """Set the OCI engine.""" |
| 77 | + self.configs['engine']['name'] = engine |
| 78 | + |
| 79 | + def unset_engine(self): |
| 80 | + """Unset the OCI engine. """ |
| 81 | + self.configs['engine']['name'] = None |
| 82 | + |
| 83 | + def set_volumes(self, volumes): |
| 84 | + """Set the volumes of the OCI engine.""" |
| 85 | + self.configs['engine']['volumes'] = volumes |
| 86 | + |
| 87 | + def set_work(self, work): |
| 88 | + """Set the working directory inside the container.""" |
| 89 | + self.configs['engine']['work'] = work |
| 90 | + |
| 91 | + def set_global_options(self, options): |
| 92 | + """Set options shared by all the containers.""" |
| 93 | + self.configs['engine']['options'] = options |
| 94 | + |
| 95 | + def set_container(self, tool, container): |
| 96 | + """Set the container of the specified tool.""" |
| 97 | + self.configs['tools'][tool]['container'] = container |
| 98 | + |
| 99 | + def set_name(self, tool, name): |
| 100 | + """Set the name of the specified tool.""" |
| 101 | + self.configs['tools'][tool]['name'] = name |
| 102 | + |
| 103 | + def set_local_options(self, tool, options): |
| 104 | + """Set options for a particular container.""" |
| 105 | + self.configs['tools'][tool]['options'] = options |
0 commit comments