|
| 1 | +# |
| 2 | +# Copyright 2021 Splunk Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +""" |
| 17 | +Summary |
| 18 | +======= |
| 19 | +A module which deals with a Splunk app |
| 20 | +""" |
| 21 | + |
| 22 | +import logging |
| 23 | +import os |
| 24 | +from builtins import object |
| 25 | + |
| 26 | +from pytest_splunk_addon.helmut.manager.confs import Confs |
| 27 | + |
| 28 | + |
| 29 | +class App(object): |
| 30 | + """ |
| 31 | + A representation of a Splunk Application |
| 32 | +
|
| 33 | + @ivar __logger: The logger we use |
| 34 | + @ivar _required_indexes: Sub classes can indexes to this list and they will |
| 35 | + be added as part of the setup. The indexes must be |
| 36 | + know by the filegetter.py, the format of each |
| 37 | + entry is a dictionary with the same parameters as |
| 38 | + the filegetter.get_file method. |
| 39 | + @ivar _required_configs: A list of paths to config files that should be |
| 40 | + copied to the app's local config directory as part |
| 41 | + of the setup |
| 42 | + @ivar _required_lookups: A list of paths to lookup files that should be |
| 43 | + copied to the app's lookup directory as part of |
| 44 | + the setup |
| 45 | + @ivar _name: The name of this app |
| 46 | + @ivar _splunk: The splunk instance this app belongs to |
| 47 | + @ivar _config_manager: The config manager for this app |
| 48 | + @ivar _shared_service: The shared service for this app. |
| 49 | + @ivar package: The package this app will be installed from or None if not |
| 50 | + package exists. |
| 51 | + """ |
| 52 | + |
| 53 | + DEFAULT_NAMESPACE = "nobody:{app_name}" |
| 54 | + |
| 55 | + def __init__(self, name, splunk, package=None): |
| 56 | + """ |
| 57 | + Constructs this app |
| 58 | +
|
| 59 | + @param name: The name of the app, should be all lower case. |
| 60 | + @type name: str |
| 61 | +
|
| 62 | + @param splunk: The splunk instance this app belongs to. |
| 63 | + @type splunk: Splunk |
| 64 | +
|
| 65 | + @param package: An optional path to any package this app can be |
| 66 | + installed from |
| 67 | + @type package: str |
| 68 | + """ |
| 69 | + super(App, self).__init__() |
| 70 | + |
| 71 | + self.__logger = logging.getLogger("App-{0}".format(name)) |
| 72 | + |
| 73 | + self._name = name |
| 74 | + self._splunk = splunk |
| 75 | + self._confs = None |
| 76 | + |
| 77 | + self.package = package |
| 78 | + |
| 79 | + @property |
| 80 | + def confs(self): |
| 81 | + """ |
| 82 | + The confs manager that is used for this app. |
| 83 | +
|
| 84 | + The manager will have a namespace that has the app portion set to this |
| 85 | + app. |
| 86 | +
|
| 87 | + @rtype: L{Confs} |
| 88 | + """ |
| 89 | + if self._confs is None: |
| 90 | + self._confs = self._create_confs_manager() |
| 91 | + return self._confs |
| 92 | + |
| 93 | + def _create_confs_manager(self): |
| 94 | + """ |
| 95 | + Creates a confs manager for this app. |
| 96 | +
|
| 97 | + It uses the same connector factory as the Splunk instance. |
| 98 | +
|
| 99 | + @return: The newly created confs manager. |
| 100 | + @rtype: L{Confs} |
| 101 | + """ |
| 102 | + return Confs(self.splunk.default_connector) |
| 103 | + |
| 104 | + @property |
| 105 | + def namespace(self): |
| 106 | + """ |
| 107 | + The namespace for this app. |
| 108 | +
|
| 109 | + @rtype: str |
| 110 | + """ |
| 111 | + return self.DEFAULT_NAMESPACE.format(app_name=self._name) |
| 112 | + |
| 113 | + @confs.setter |
| 114 | + def confs(self, value): |
| 115 | + """ |
| 116 | + Updates the confs manager for this app. |
| 117 | +
|
| 118 | + @param value: The new manager. |
| 119 | + @type value: L{Confs} |
| 120 | + """ |
| 121 | + self._confs = value |
| 122 | + |
| 123 | + @property |
| 124 | + def installed(self): |
| 125 | + """ |
| 126 | + Checks too see whether this app is already installed or not. |
| 127 | +
|
| 128 | + It does this by checking if the directory exists which means that there |
| 129 | + is no guarantee that it was installed this session. |
| 130 | +
|
| 131 | + @rtype: bool |
| 132 | + """ |
| 133 | + return self.splunk.has_app(self.name) |
| 134 | + |
| 135 | + @property |
| 136 | + def name(self): |
| 137 | + """ |
| 138 | + The name for this app |
| 139 | +
|
| 140 | + @rtype: str |
| 141 | + """ |
| 142 | + return self._name |
| 143 | + |
| 144 | + @property |
| 145 | + def splunk(self): |
| 146 | + """ |
| 147 | + The Splunk instance this app belongs to |
| 148 | +
|
| 149 | + @rtype: L{Splunk} |
| 150 | + """ |
| 151 | + return self._splunk |
| 152 | + |
| 153 | + @property |
| 154 | + def apps_dir(self): |
| 155 | + """ |
| 156 | + The path to the directory that splunk stores it's apps |
| 157 | +
|
| 158 | + @rtype: str |
| 159 | + """ |
| 160 | + return self.splunk.apps_dir |
| 161 | + |
| 162 | + @property |
| 163 | + def install_path(self): |
| 164 | + """ |
| 165 | + The path to the directory where this app will be/is installed |
| 166 | +
|
| 167 | + @rtype: str |
| 168 | + """ |
| 169 | + return os.path.join(self.apps_dir, self.name) |
| 170 | + |
| 171 | + def can_install(self): |
| 172 | + """ |
| 173 | + Checks if this app can be installed meaning if a package has been |
| 174 | + supplied. |
| 175 | +
|
| 176 | + @rtype: bool |
| 177 | + @return: True if this app can be installed |
| 178 | + """ |
| 179 | + return self.package is not None |
| 180 | + |
| 181 | + def install(self): |
| 182 | + """ |
| 183 | + Installs this app. |
| 184 | +
|
| 185 | + @rtype: bool |
| 186 | + @return: True if the app was installed and splunk needs to restart |
| 187 | + """ |
| 188 | + self._verify_can_install() |
| 189 | + return self.splunk.install_app(self.name, self.package) |
| 190 | + |
| 191 | + def _verify_can_install(self): |
| 192 | + """ |
| 193 | + Checks that this app can be installed and raising an exception if it |
| 194 | + can't. |
| 195 | +
|
| 196 | + @raise AppHasNoPackage: If the app can't be installed. |
| 197 | + """ |
| 198 | + if not self.package: |
| 199 | + raise AppHasNoPackage(self.name) |
| 200 | + |
| 201 | + def uninstall(self): |
| 202 | + """ |
| 203 | + Uninstalls this app |
| 204 | +
|
| 205 | + @rtype: bool |
| 206 | + @return: True if the app was installed and has now been removed |
| 207 | + """ |
| 208 | + return self.splunk.uninstall_app(self.name) |
| 209 | + |
| 210 | + |
| 211 | +class AppHasNoPackage(RuntimeError): |
| 212 | + def __init__(self, app_name): |
| 213 | + msg = "The app {0} has no package to install from".format(app_name) |
| 214 | + super(AppHasNoPackage, self).__init__(msg) |
0 commit comments