Skip to content

Commit a92c7d4

Browse files
committed
standardize ServiceMissingError messages
1 parent 35926bb commit a92c7d4

File tree

2 files changed

+165
-120
lines changed

2 files changed

+165
-120
lines changed

pants-plugins/uses_services/exceptions.py

Lines changed: 129 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,30 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from dataclasses import dataclass
15+
1416
from uses_services.platform_rules import Platform
1517

1618

19+
@dataclass(frozen=True)
20+
class ServiceSpecificMessages:
21+
service: str
22+
23+
service_start_cmd_el_7: str
24+
service_start_cmd_el: str
25+
not_installed_clause_el: str
26+
install_instructions_el: str
27+
28+
service_start_cmd_deb: str
29+
not_installed_clause_deb: str
30+
install_instructions_deb: str
31+
32+
service_start_cmd_generic: str
33+
34+
1735
class ServiceMissingError(Exception):
1836
"""Error raised when a test uses a service but that service is missing."""
1937

20-
# TODO add special platform handling to DRY instructions across services
21-
2238
def __init__(self, service, platform: Platform, instructions="", msg=None):
2339
if msg is None:
2440
msg = f"The {service} service does not seem to be running or is not accessible!"
@@ -28,3 +44,114 @@ def __init__(self, service, platform: Platform, instructions="", msg=None):
2844
self.service = service
2945
self.platform = platform
3046
self.instructions = instructions
47+
48+
@classmethod
49+
def generate_instructions(
50+
cls, platform: Platform, messages: ServiceSpecificMessages
51+
):
52+
service = messages.service
53+
54+
supported = False
55+
if platform.distro in ["centos", "rhel"] or "rhel" in platform.distro_like:
56+
supported = True
57+
if platform.distro_major_version == "7":
58+
service_start_cmd = messages.service_start_cmd_el_7
59+
else:
60+
service_start_cmd = messages.service_start_cmd_el
61+
not_installed_clause = messages.not_installed_clause_el
62+
install_instructions = messages.install_instructions_el
63+
64+
elif (
65+
platform.distro in ["ubuntu", "debian"] or "debian" in platform.distro_like
66+
):
67+
supported = True
68+
service_start_cmd = messages.service_start_cmd_deb
69+
not_installed_clause = messages.not_installed_clause_deb
70+
install_instructions = messages.install_instructions_deb
71+
72+
if supported:
73+
instructions = dedent(
74+
f"""\
75+
If {service} is installed, but not running try:
76+
77+
{service_start_cmd}
78+
79+
If {service} is not installed, {not_installed_clause}:
80+
81+
"""
82+
).format(
83+
service=service,
84+
service_start_cmd=service_start_cmd,
85+
not_installed_clause=not_installed_clause,
86+
)
87+
instructions += install_instructions
88+
elif platform.os == "Linux":
89+
instructions = dedent(
90+
f"""\
91+
You are on Linux using {platform.distro_name}, which is not
92+
one of our generally supported distributions. We recommend
93+
you use vagrant for local development with something like:
94+
95+
vagrant init stackstorm/st2
96+
vagrant up
97+
vagrant ssh
98+
99+
Please see: https://docs.stackstorm.com/install/vagrant.html
100+
101+
For anyone who wants to attempt local development without vagrant,
102+
you are pretty much on your own. At a minimum you need to install
103+
and start {service} with something like:
104+
105+
{messages.service_start_cmd_generic}
106+
107+
We would be interested to hear about alternative distros people
108+
are using for development. If you are able, please let us know
109+
on slack which distro you are using:
110+
111+
Arch: {platform.arch}
112+
Distro: {platform.distro}
113+
Distro Name: {platform.distro_name}
114+
Distro Codename: {platform.distro_codename}
115+
Distro Family: {platform.distro_like}
116+
Distro Major Version: {platform.distro_major_version}
117+
Distro Version: {platform.distro_version}
118+
119+
Thanks and Good Luck!
120+
"""
121+
)
122+
elif platform.os == "Darwin": # MacOS
123+
instructions = dedent(
124+
f"""\
125+
You are on Mac OS. Generally we recommend using vagrant for local
126+
development on Mac OS with something like:
127+
128+
vagrant init stackstorm/st2
129+
vagrant up
130+
vagrant ssh
131+
132+
Please see: https://docs.stackstorm.com/install/vagrant.html
133+
134+
For anyone who wants to attempt local development without vagrant,
135+
you may run into some speed bumps. Others StackStorm developers have
136+
been known to use Mac OS for development, so feel free to ask for
137+
help in slack. At a minimum you need to install and start {service}.
138+
"""
139+
)
140+
else:
141+
instructions = dedent(
142+
f"""\
143+
You are not on Linux. In this case we recommend using vagrant
144+
for local development with something like:
145+
146+
vagrant init stackstorm/st2
147+
vagrant up
148+
vagrant ssh
149+
150+
Please see: https://docs.stackstorm.com/install/vagrant.html
151+
152+
For anyone who wants to attempt local development without vagrant,
153+
you are pretty much on your own. At a minimum you need to install
154+
and start {service}. Good luck!
155+
"""
156+
)
157+
return instructions

pants-plugins/uses_services/mongo_rules.py

Lines changed: 36 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from pants.engine.unions import UnionRule
3535
from pants.util.logging import LogLevel
3636

37-
from uses_services.exceptions import ServiceMissingError
37+
from uses_services.exceptions import ServiceMissingError, ServiceSpecificMessages
3838
from uses_services.platform_rules import Platform
3939
from uses_services.scripts.is_mongo_running import (
4040
__file__ as is_mongo_running_full_path,
@@ -159,123 +159,41 @@ async def mongo_is_running(
159159
return MongoIsRunning()
160160

161161
# mongo is not running, so raise an error with instructions.
162-
163-
if platform.distro in ["centos", "rhel"] or "rhel" in platform.distro_like:
164-
instructions = dedent(
165-
"""\
166-
If mongo is installed, but not running try:
167-
168-
"""
169-
)
170-
171-
if platform.distro_major_version == "7":
172-
instructions += "\nservice mongo start\n"
173-
else:
174-
instructions += "\nsystemctl start mongod\n"
175-
176-
instructions += dedent(
177-
"""
178-
If mongo is not installed, this is one way to install it:
179-
180-
# Add key and repo for the latest stable MongoDB (4.0)
181-
rpm --import https://www.mongodb.org/static/pgp/server-4.0.asc
182-
sh -c "cat <<EOT > /etc/yum.repos.d/mongodb-org-4.repo
183-
[mongodb-org-4]
184-
name=MongoDB Repository
185-
baseurl=https://repo.mongodb.org/yum/redhat/${OSRELEASE_VERSION}/mongodb-org/4.0/x86_64/
186-
gpgcheck=1
187-
enabled=1
188-
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
189-
EOT"
190-
# Install mongo
191-
yum install mongodb-org
192-
# Don't forget to start mongo.
193-
"""
194-
)
195-
elif platform.distro in ["ubuntu", "debian"] or "debian" in platform.distro_like:
196-
instructions = dedent(
197-
"""\
198-
If mongo is installed, but not running try:
199-
200-
systemctl start mongod
201-
202-
If mongo is not installed, this is one way to install it:
203-
204-
apt-get install mongodb mongodb-server
205-
# Don't forget to start mongo.
206-
"""
207-
)
208-
elif platform.os == "Linux":
209-
instructions = dedent(
210-
f"""\
211-
You are on Linux using {platform.distro_name}, which is not
212-
one of our generally supported distributions. We recommend
213-
you use vagrant for local development with something like:
214-
215-
vagrant init stackstorm/st2
216-
vagrant up
217-
vagrant ssh
218-
219-
Please see: https://docs.stackstorm.com/install/vagrant.html
220-
221-
For anyone who wants to attempt local development without vagrant,
222-
you are pretty much on your own. At a minimum you need to install
223-
and start mongo with something like:
224-
225-
systemctl start mongod
226-
227-
We would be interested to hear about alternative distros people
228-
are using for development. If you are able, please let us know
229-
on slack which distro you are using:
230-
231-
Arch: {platform.arch}
232-
Distro: {platform.distro}
233-
Distro Name: {platform.distro_name}
234-
Distro Codename: {platform.distro_codename}
235-
Distro Family: {platform.distro_like}
236-
Distro Major Version: {platform.distro_major_version}
237-
Distro Version: {platform.distro_version}
238-
239-
Thanks and Good Luck!
240-
"""
241-
)
242-
elif platform.os == "Darwin": # MacOS
243-
instructions = dedent(
244-
"""\
245-
You are on Mac OS. Generally we recommend using vagrant for local
246-
development on Mac OS with something like:
247-
248-
vagrant init stackstorm/st2
249-
vagrant up
250-
vagrant ssh
251-
252-
Please see: https://docs.stackstorm.com/install/vagrant.html
253-
254-
For anyone who wants to attempt local development without vagrant,
255-
you may run into some speed bumps. Others StackStorm developers have
256-
been known to use Mac OS for development, so feel free to ask for
257-
help in slack. At a minimum you need to install and start mongo.
258-
"""
259-
)
260-
else:
261-
instructions = dedent(
262-
"""\
263-
You are not on Linux. In this case we recommend using vagrant
264-
for local development with something like:
265-
266-
vagrant init stackstorm/st2
267-
vagrant up
268-
vagrant ssh
269-
270-
Please see: https://docs.stackstorm.com/install/vagrant.html
271-
272-
For anyone who wants to attempt local development without vagrant,
273-
you are pretty much on your own. At a minimum you need to install
274-
and start mongo. Good luck!
275-
"""
276-
)
277-
278-
raise ServiceMissingError("mongo", platform, instructions)
162+
raise ServiceMissingError(
163+
platform,
164+
ServiceSpecificMessages(
165+
service="mongo",
166+
service_start_cmd_el_7="service mongo start",
167+
service_start_cmd_el="systemctl start mongod",
168+
not_installed_clause_el="this is one way to install it:",
169+
install_instructions_el=dedent(
170+
"""\
171+
# Add key and repo for the latest stable MongoDB (4.0)
172+
rpm --import https://www.mongodb.org/static/pgp/server-4.0.asc
173+
sh -c "cat <<EOT > /etc/yum.repos.d/mongodb-org-4.repo
174+
[mongodb-org-4]
175+
name=MongoDB Repository
176+
baseurl=https://repo.mongodb.org/yum/redhat/${OSRELEASE_VERSION}/mongodb-org/4.0/x86_64/
177+
gpgcheck=1
178+
enabled=1
179+
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
180+
EOT"
181+
# Install mongo
182+
yum install mongodb-org
183+
# Don't forget to start mongo.
184+
"""
185+
),
186+
service_start_cmd_deb="systemctl start mongod",
187+
not_installed_clause_deb="this is one way to install it:",
188+
install_instructions_deb=dedent(
189+
"""\
190+
apt-get install mongodb mongodb-server
191+
# Don't forget to start mongo.
192+
"""
193+
),
194+
service_start_cmd_generic="systemctl start mongod",
195+
),
196+
)
279197

280198

281199
def rules():

0 commit comments

Comments
 (0)