|
| 1 | +# Copyright 2023 The StackStorm Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +from dataclasses import dataclass |
| 17 | +from textwrap import dedent |
| 18 | + |
| 19 | +from uses_services.platform_rules import Platform |
| 20 | + |
| 21 | + |
| 22 | +@dataclass(frozen=True) |
| 23 | +class ServiceSpecificMessages: |
| 24 | + service: str |
| 25 | + |
| 26 | + service_start_cmd_el_7: str |
| 27 | + service_start_cmd_el: str |
| 28 | + not_installed_clause_el: str |
| 29 | + install_instructions_el: str |
| 30 | + |
| 31 | + service_start_cmd_deb: str |
| 32 | + not_installed_clause_deb: str |
| 33 | + install_instructions_deb: str |
| 34 | + |
| 35 | + service_start_cmd_generic: str |
| 36 | + |
| 37 | + |
| 38 | +class ServiceMissingError(Exception): |
| 39 | + """Error raised when a test uses a service but that service is missing.""" |
| 40 | + |
| 41 | + def __init__( |
| 42 | + self, service: str, platform: Platform, instructions: str = "", msg=None |
| 43 | + ): |
| 44 | + if msg is None: |
| 45 | + msg = f"The {service} service does not seem to be running or is not accessible!" |
| 46 | + if instructions: |
| 47 | + msg += f"\n{instructions}" |
| 48 | + super().__init__(msg) |
| 49 | + self.service = service |
| 50 | + self.platform = platform |
| 51 | + self.instructions = instructions |
| 52 | + |
| 53 | + @classmethod |
| 54 | + def generate( |
| 55 | + cls, platform: Platform, messages: ServiceSpecificMessages |
| 56 | + ) -> ServiceMissingError: |
| 57 | + service = messages.service |
| 58 | + |
| 59 | + supported = False |
| 60 | + if platform.distro in ["centos", "rhel"] or "rhel" in platform.distro_like: |
| 61 | + supported = True |
| 62 | + if platform.distro_major_version == "7": |
| 63 | + service_start_cmd = messages.service_start_cmd_el_7 |
| 64 | + else: |
| 65 | + service_start_cmd = messages.service_start_cmd_el |
| 66 | + not_installed_clause = messages.not_installed_clause_el |
| 67 | + install_instructions = messages.install_instructions_el |
| 68 | + |
| 69 | + elif ( |
| 70 | + platform.distro in ["ubuntu", "debian"] or "debian" in platform.distro_like |
| 71 | + ): |
| 72 | + supported = True |
| 73 | + service_start_cmd = messages.service_start_cmd_deb |
| 74 | + not_installed_clause = messages.not_installed_clause_deb |
| 75 | + install_instructions = messages.install_instructions_deb |
| 76 | + |
| 77 | + if supported: |
| 78 | + instructions = dedent( |
| 79 | + f"""\ |
| 80 | + If {service} is installed, but not running try: |
| 81 | +
|
| 82 | + {service_start_cmd} |
| 83 | +
|
| 84 | + If {service} is not installed, {not_installed_clause}: |
| 85 | +
|
| 86 | + """ |
| 87 | + ).format( |
| 88 | + service=service, |
| 89 | + service_start_cmd=service_start_cmd, |
| 90 | + not_installed_clause=not_installed_clause, |
| 91 | + ) |
| 92 | + instructions += install_instructions |
| 93 | + elif platform.os == "Linux": |
| 94 | + instructions = dedent( |
| 95 | + f"""\ |
| 96 | + You are on Linux using {platform.distro_name}, which is not |
| 97 | + one of our generally supported distributions. We recommend |
| 98 | + you use vagrant for local development with something like: |
| 99 | +
|
| 100 | + vagrant init stackstorm/st2 |
| 101 | + vagrant up |
| 102 | + vagrant ssh |
| 103 | +
|
| 104 | + Please see: https://docs.stackstorm.com/install/vagrant.html |
| 105 | +
|
| 106 | + For anyone who wants to attempt local development without vagrant, |
| 107 | + you are pretty much on your own. At a minimum you need to install |
| 108 | + and start {service} with something like: |
| 109 | +
|
| 110 | + {messages.service_start_cmd_generic} |
| 111 | +
|
| 112 | + We would be interested to hear about alternative distros people |
| 113 | + are using for development. If you are able, please let us know |
| 114 | + on slack which distro you are using: |
| 115 | +
|
| 116 | + Arch: {platform.arch} |
| 117 | + Distro: {platform.distro} |
| 118 | + Distro Name: {platform.distro_name} |
| 119 | + Distro Codename: {platform.distro_codename} |
| 120 | + Distro Family: {platform.distro_like} |
| 121 | + Distro Major Version: {platform.distro_major_version} |
| 122 | + Distro Version: {platform.distro_version} |
| 123 | +
|
| 124 | + Thanks and Good Luck! |
| 125 | + """ |
| 126 | + ) |
| 127 | + elif platform.os == "Darwin": # MacOS |
| 128 | + instructions = dedent( |
| 129 | + f"""\ |
| 130 | + You are on Mac OS. Generally we recommend using vagrant for local |
| 131 | + development on Mac OS with something like: |
| 132 | +
|
| 133 | + vagrant init stackstorm/st2 |
| 134 | + vagrant up |
| 135 | + vagrant ssh |
| 136 | +
|
| 137 | + Please see: https://docs.stackstorm.com/install/vagrant.html |
| 138 | +
|
| 139 | + For anyone who wants to attempt local development without vagrant, |
| 140 | + you may run into some speed bumps. Other StackStorm developers have |
| 141 | + been known to use Mac OS for development, so feel free to ask for |
| 142 | + help in slack. At a minimum you need to install and start {service}. |
| 143 | + """ |
| 144 | + ) |
| 145 | + else: |
| 146 | + instructions = dedent( |
| 147 | + f"""\ |
| 148 | + You are not on Linux. In this case we recommend using vagrant |
| 149 | + for local development with something like: |
| 150 | +
|
| 151 | + vagrant init stackstorm/st2 |
| 152 | + vagrant up |
| 153 | + vagrant ssh |
| 154 | +
|
| 155 | + Please see: https://docs.stackstorm.com/install/vagrant.html |
| 156 | +
|
| 157 | + For anyone who wants to attempt local development without vagrant, |
| 158 | + you are pretty much on your own. At a minimum you need to install |
| 159 | + and start {service}. Good luck! |
| 160 | + """ |
| 161 | + ) |
| 162 | + |
| 163 | + return cls( |
| 164 | + service=service, |
| 165 | + platform=platform, |
| 166 | + instructions=instructions, |
| 167 | + ) |
0 commit comments