shoestring-assembler 0.0.2__tar.gz → 0.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/PKG-INFO +1 -1
  2. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/pyproject.toml +1 -1
  3. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/src/shoestring_assembler/assembler.py +270 -90
  4. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/src/shoestring_assembler/cli.py +24 -5
  5. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/src/shoestring_assembler/display.py +37 -5
  6. shoestring_assembler-0.0.3/src/shoestring_assembler/schemas/meta.schema.json +85 -0
  7. {shoestring_assembler-0.0.2/src/shoestring_assembler → shoestring_assembler-0.0.3/src/shoestring_assembler/schemas}/recipe.schema.json +101 -18
  8. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/src/shoestring_assembler.egg-info/PKG-INFO +1 -1
  9. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/src/shoestring_assembler.egg-info/SOURCES.txt +2 -1
  10. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/AUTHORS.rst +0 -0
  11. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/CONTRIBUTING.rst +0 -0
  12. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/HISTORY.rst +0 -0
  13. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/LICENSE +0 -0
  14. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/MANIFEST.in +0 -0
  15. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/README.rst +0 -0
  16. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/docs/Makefile +0 -0
  17. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/docs/authors.rst +0 -0
  18. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/docs/conf.py +0 -0
  19. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/docs/contributing.rst +0 -0
  20. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/docs/history.rst +0 -0
  21. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/docs/index.rst +0 -0
  22. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/docs/installation.rst +0 -0
  23. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/docs/make.bat +0 -0
  24. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/docs/readme.rst +0 -0
  25. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/docs/usage.rst +0 -0
  26. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/setup.cfg +0 -0
  27. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/src/shoestring_assembler/__init__.py +0 -0
  28. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/src/shoestring_assembler/minimal_mustache.py +0 -0
  29. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/src/shoestring_assembler.egg-info/dependency_links.txt +0 -0
  30. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/src/shoestring_assembler.egg-info/entry_points.txt +0 -0
  31. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/src/shoestring_assembler.egg-info/requires.txt +0 -0
  32. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/src/shoestring_assembler.egg-info/top_level.txt +0 -0
  33. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/tests/__init__.py +0 -0
  34. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/tests/console.log +0 -0
  35. {shoestring_assembler-0.0.2 → shoestring_assembler-0.0.3}/tests/test_shoestring_assembler.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: shoestring_assembler
3
- Version: 0.0.2
3
+ Version: 0.0.3
4
4
  Summary: Assembles Shoestring Solutions from a solution recipe
5
5
  Author-email: Greg Hawkridge <gth24@cam.ac.uk>
6
6
  Maintainer-email: Greg Hawkridge <gth24@cam.ac.uk>
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
6
6
 
7
7
  [project]
8
8
  name = "shoestring_assembler"
9
- version = "0.0.2"
9
+ version = "0.0.3"
10
10
  description = "Assembles Shoestring Solutions from a solution recipe"
11
11
  readme = "README.rst"
12
12
  authors = [
@@ -1,6 +1,7 @@
1
1
  """Main module."""
2
2
 
3
3
  import rich.progress
4
+ from rich.prompt import Prompt, Confirm
4
5
  import json
5
6
  import time
6
7
  import sys
@@ -24,7 +25,8 @@ import yaml
24
25
  import jsonschema
25
26
  import re
26
27
  import select
27
- from . import minimal_mustache
28
+ from . import minimal_mustache
29
+ from functools import cache
28
30
 
29
31
  src_dir = os.path.dirname(os.path.realpath(__file__))
30
32
 
@@ -33,6 +35,8 @@ src_dir = os.path.dirname(os.path.realpath(__file__))
33
35
  SOLUTION_FILES_DIR = "solution_files"
34
36
  BASE_MODULE_FILES_DIR = f"{SOLUTION_FILES_DIR}/sources"
35
37
  SOLUTION_CONFIG_DIR = f"{SOLUTION_FILES_DIR}/config"
38
+ USER_CONFIG_SRC_DIR = f"{SOLUTION_FILES_DIR}/user_config_templates"
39
+ USER_CONFIG_VERSION_FILE = "__version__"
36
40
 
37
41
  DATA_DIR = "data"
38
42
  USER_CONFIG_DIR = "user_config"
@@ -49,7 +53,7 @@ class Assembler:
49
53
  self._recipe_hash = None
50
54
 
51
55
  def load_recipe(self):
52
- display.print_header("Loading Recipe")
56
+ display.print_header("Get Recipe")
53
57
  try:
54
58
  # check file type is supported and find parser
55
59
  ext = self._recipe_location.suffix
@@ -67,15 +71,17 @@ class Assembler:
67
71
  sys.exit(255)
68
72
 
69
73
  # parse file
70
- with rich.progress.open(
71
- self._recipe_location, "rb", description="Loading File..."
74
+ with display.open_file(
75
+ self._recipe_location, "rb", description="Loading Recipe..."
72
76
  ) as file:
73
77
  self._recipe = parser(file)
74
- file.seek(0) # reset file
78
+ file.seek(0) # reset file
75
79
  hash_fn = hashlib.sha256()
76
80
  hash_fn.update(file.read())
77
81
  self._recipe_hash = hash_fn.hexdigest()
78
82
 
83
+ self.validate_recipe()
84
+
79
85
  except FileNotFoundError:
80
86
  display.print_error(
81
87
  f"Unable to find recipe file. Expected to find it at: {self._recipe_location}"
@@ -94,29 +100,38 @@ class Assembler:
94
100
  return toml.load(file)
95
101
 
96
102
  def validate_recipe(self):
97
- display.print_header("Validating Recipe")
98
- schema_location = os.path.join(src_dir, "recipe.schema.json")
103
+ display.print_log("Validating Recipe")
104
+ schema_location = os.path.join(src_dir, "schemas/recipe.schema.json")
99
105
 
100
106
  try:
101
- with rich.progress.open(
107
+ with display.open_file(
102
108
  schema_location, "rb", description="Loading Recipe Schema..."
103
109
  ) as file:
104
110
  schema = json.load(file)
105
111
 
106
- do_validate(self._recipe, schema)
112
+ schema_validate(self._recipe, schema)
107
113
 
108
114
  except FileNotFoundError:
109
115
  display.print_error(
110
116
  f"Unable to find recipe schema. Expected to find it at: {schema_location}"
111
117
  )
112
118
  sys.exit(255)
119
+ except jsonschema.ValidationError as v_err:
120
+ display.print_error(
121
+ f"Recipe error at {v_err.json_path}:\n\n{v_err.message}"
122
+ )
123
+ display.print_error(
124
+ "Config File is not valid -- unable to start the solution -- please correct the issues flagged above and try again."
125
+ )
126
+ sys.exit(255)
113
127
 
114
- display.print_complete("Recipe valid")
128
+ display.print_log("Recipe valid")
115
129
 
116
- def clean(self):
130
+ def clean(self, clean_sources=True):
117
131
  display.print_header("Cleaning")
118
132
  # TODO
119
- rmtree(Path(BASE_MODULE_FILES_DIR))
133
+ if clean_sources:
134
+ rmtree(Path(BASE_MODULE_FILES_DIR))
120
135
  display.print_complete("Old files cleared")
121
136
 
122
137
  def verify_filestructure(self, check_sources=False):
@@ -164,74 +179,140 @@ class Assembler:
164
179
  else:
165
180
  display.print_complete("Filesystem structure valid")
166
181
 
167
- def gather_base_service_modules(self):
182
+ def gather_base_service_modules(self, do_gather):
168
183
  display.print_header("Gathering Service Module Sources")
169
- number_of_service_modules = len(self._recipe["source"])
170
184
 
171
- with rich.progress.Progress() as progress: # displays progress bar on console
172
- task = progress.add_task(
173
- "[cyan]Gathering Service Module Sources",
174
- total=number_of_service_modules,
185
+ if not do_gather:
186
+ display.print_complete(
187
+ "Fetching service module sources disabled by command line arguements"
175
188
  )
189
+ else:
190
+ number_of_service_modules = len(self._recipe["source"])
191
+
192
+ with (
193
+ rich.progress.Progress() as progress
194
+ ): # displays progress bar on console
195
+ task = progress.add_task(
196
+ "[cyan] Gathering Service Module Sources",
197
+ total=number_of_service_modules,
198
+ )
176
199
 
177
- listed_sources = self._recipe["source"]
178
- service_module_source_set = [
179
- details["source"]
180
- for details in self._recipe.get("service_module", {}).values()
181
- ]
182
- infrastructure_source_set = [
183
- details["source"]
184
- for details in self._recipe.get("infrastructure", {}).values()
185
- ]
186
-
187
- used_sources = {
188
- source_name: listed_sources[source_name]
189
- for source_name in service_module_source_set
190
- }
191
- used_sources.update(
192
- {
200
+ listed_sources = self._recipe["source"]
201
+ service_module_source_set = [
202
+ details["source"]
203
+ for details in self._recipe["service_module"].values()
204
+ ]
205
+ infrastructure_source_set = [
206
+ details["source"]
207
+ for details in self._recipe.get("infrastructure", {}).values()
208
+ ]
209
+
210
+ used_sources = {
193
211
  source_name: listed_sources[source_name]
194
- for source_name in infrastructure_source_set
212
+ for source_name in service_module_source_set
195
213
  }
196
- )
197
-
198
- for source_name, source_details in used_sources.items():
199
- display.print_log(f"Fetching {source_name}", console=progress.console)
214
+ used_sources.update(
215
+ {
216
+ source_name: listed_sources[source_name]
217
+ for source_name in infrastructure_source_set
218
+ }
219
+ )
200
220
 
201
- if "file" in source_details:
202
- result = handle_file_source(
203
- source_name, source_details["file"], console=progress.console
204
- )
205
- elif "git" in source_details:
206
- result = handle_git_source(
207
- source_name,
208
- source_details["git"],
209
- console=progress.console,
210
- progress=progress,
211
- )
212
- else:
213
- # this should never happen due to recipe validation
214
- display.print_error(
215
- f"Source {source_name} does not include details on where to get it",
216
- console=progress.console,
221
+ for source_name, source_details in used_sources.items():
222
+ display.print_log(
223
+ f"Fetching {source_name}", console=progress.console
217
224
  )
218
- sys.exit(255)
219
225
 
220
- if result:
221
- display.print_complete(
222
- f"Fetched '{source_name}' ", console=progress.console
223
- )
224
- progress.update(task, advance=1) # updates progress bar
225
- else:
226
- progress.update(task, visible=False)
227
- display.print_error(
228
- f"An error occured while fetching '{source_name}'",
229
- console=progress.console,
230
- )
231
- sys.exit(255)
226
+ if "file" in source_details:
227
+ result = handle_file_source(
228
+ source_name,
229
+ source_details["file"],
230
+ console=progress.console,
231
+ )
232
+ elif "git" in source_details:
233
+ result = handle_git_source(
234
+ source_name,
235
+ source_details["git"],
236
+ console=progress.console,
237
+ progress=progress,
238
+ )
239
+ else:
240
+ # this should never happen due to recipe validation
241
+ display.print_error(
242
+ f"Source {source_name} does not include details on where to get it",
243
+ console=progress.console,
244
+ )
245
+ sys.exit(255)
246
+
247
+ if result:
248
+ display.print_complete(
249
+ f"Fetched '{source_name}' ", console=progress.console
250
+ )
251
+ progress.update(task, advance=1) # updates progress bar
252
+ else:
253
+ progress.update(task, visible=False)
254
+ display.print_error(
255
+ f"An error occured while fetching '{source_name}'",
256
+ console=progress.console,
257
+ )
258
+ sys.exit(255)
259
+
260
+ display.print_complete("All Service Module Sources Gathered")
232
261
 
233
262
  def check_user_config(self):
234
263
  display.print_header("Checking User Config")
264
+ has_notifications = False
265
+ for service_module_name, service_module_details in self._recipe[
266
+ "service_module"
267
+ ].items():
268
+ # display.print_log(f"[cyan]* {service_module_name}")
269
+ source = service_module_details["source"]
270
+ try:
271
+ template_dir = f"{USER_CONFIG_SRC_DIR}/{source}"
272
+ service_module_dir = f"{USER_CONFIG_DIR}/{service_module_name}"
273
+ outcome = compare_template_versions(
274
+ template_dir,
275
+ service_module_dir,
276
+ )
277
+ display.print_log(
278
+ f"[green]\[{outcome}][/green] [white]{service_module_name}"
279
+ )
280
+ if outcome in ["new_version_major", "new_version_minor"]:
281
+ display.print_notification(
282
+ f"There is a new version of the user config files for {service_module_name}!"
283
+ + "You will need to transfer your current config to the new format otherwise the solution will not work as expected."
284
+ + f"You can find the current user config files at [purple]./{service_module_dir}[/purple] and the template files at [purple]./{template_dir}[/purple]"
285
+ )
286
+ has_notifications = True
287
+ elif outcome == "empty":
288
+ transfer_user_config_from_template(template_dir, service_module_dir)
289
+
290
+ except InvalidVersion as e:
291
+ display.print_error(
292
+ f"The {e.source} version file contained an invalid version."
293
+ )
294
+ sys.exit(255)
295
+ except FileNotFoundError:
296
+ display.print_error(
297
+ f"Couldn't find version file in template directory. Directory expected at {template_dir}."
298
+ )
299
+ sys.exit(255)
300
+ except TemplateOlderThanCurrentVersion as e:
301
+ display.print_log(f"[yellow]\[warning][/yellow] {service_module_name}")
302
+ display.print_warning(
303
+ f"Current user config version is {e.current_vsn} which is newer than the template version of {e.template_vsn}.\n"
304
+ + f"[red]This might be ok! [/red] - but it should be checked!\n"
305
+ + f"You can find the current user config files at [purple]./{service_module_dir}[/purple] and the template files at [purple]./{template_dir}[/purple]"
306
+ )
307
+ has_notifications = True
308
+ # doesn't exit as this may be ok (e.g. roll back after update)
309
+
310
+ if has_notifications:
311
+ do_continue = Confirm.ask(
312
+ "Please see the notifications above. continue?", default=True
313
+ )
314
+ if not do_continue:
315
+ sys.exit()
235
316
 
236
317
  display.print_complete(f"User config ready")
237
318
 
@@ -260,7 +341,7 @@ class Assembler:
260
341
 
261
342
  compose_definition["x-shoestring"] = {
262
343
  "filename": self._recipe_filepath_provided,
263
- "hash":self._recipe_hash
344
+ "hash": self._recipe_hash,
264
345
  }
265
346
 
266
347
  with open(Path.resolve(Path.cwd()) / Path("compose.yml"), "w") as f:
@@ -272,36 +353,49 @@ class Assembler:
272
353
  def generate_docker_services_for_module(
273
354
  self, module_name, module_details, module_type
274
355
  ):
275
- display.print_log(f"Generating compose services for {module_name}")
356
+ display.print_log(f"[cyan]* {module_name}")
276
357
  module_services = {}
277
358
  # load meta.toml
278
359
  source = module_details["source"]
279
360
  try:
280
361
  meta_file_path = f"./{BASE_MODULE_FILES_DIR}/{source}/meta.toml"
281
- with rich.progress.open(
362
+ meta_schema = load_meta_schema()
363
+ with display.open_file(
282
364
  meta_file_path,
283
365
  "rb",
284
366
  description="Loading meta...",
285
367
  ) as file:
286
368
  meta = toml.load(file)
369
+ schema_validate(meta, meta_schema)
287
370
  except FileNotFoundError:
288
371
  display.print_error(
289
372
  f"Unable to find meta file for {source}. Expected to find it at: {meta_file_path}"
290
373
  )
291
374
  sys.exit(255)
375
+ except jsonschema.ValidationError as v_err:
376
+ display.print_error(
377
+ f"Error in meta file for source '{source}' at {v_err.json_path}:\n\n{v_err.message}"
378
+ )
379
+ display.print_error(
380
+ f"Meta file for source '{source}' is not valid -- unable to start the solution"
381
+ )
382
+ sys.exit(255)
292
383
 
293
384
  container_list = module_details.get("containers", [])
294
385
  for container in container_list:
295
- container_meta = meta[container]
296
- entry_identifier = (
297
- f"{module_name}-{container}" if len(container_list) > 1 else module_name
298
- )
386
+ entry_identifier = module_name
387
+
388
+ if len(container_list) > 1:
389
+ entry_identifier = f"{module_name}-{container}"
390
+ display.print_log(f"[cyan]** {entry_identifier}")
391
+
299
392
  network_alias = (
300
393
  module_details["alias"].get(container, entry_identifier)
301
394
  if "alias" in module_details
302
395
  else entry_identifier
303
396
  )
304
397
 
398
+ container_meta = meta[container]
305
399
  # form base
306
400
  service_definition = {
307
401
  "build": {
@@ -329,13 +423,13 @@ class Assembler:
329
423
  if "compose_partial" in container_meta:
330
424
  try:
331
425
  partial_file_path = f'./{BASE_MODULE_FILES_DIR}/{source}/{container_meta["compose_partial"]}'
332
- with rich.progress.open(
426
+ with display.open_file(
333
427
  partial_file_path,
334
428
  "r",
335
429
  description="Loading compose partials...",
336
430
  ) as file:
337
431
  template_applied_string = minimal_mustache.render(
338
- file.read(), module_details.get("template",{})
432
+ file.read(), module_details.get("template", {})
339
433
  )
340
434
  partials = yaml.safe_load(template_applied_string)
341
435
  except FileNotFoundError:
@@ -467,14 +561,14 @@ def check_or_create_dir(rel_path):
467
561
 
468
562
  if sys.version_info[0] == 3 and sys.version_info[1] < 14:
469
563
 
470
- def do_copy(src_path, dest_path):
564
+ def do_copy(src_path, dest_path,create_dirs = False):
471
565
  import shutil
472
566
 
473
- shutil.copytree(src_path, dest_path)
567
+ shutil.copytree(src_path, dest_path, dirs_exist_ok=create_dirs)
474
568
 
475
569
  else:
476
570
 
477
- def do_copy(src_path, dest_path):
571
+ def do_copy(src_path, dest_path,create_dirs = False):
478
572
  src_path.copy_into(dest_path)
479
573
 
480
574
 
@@ -504,24 +598,109 @@ else:
504
598
  (root / name).rmdir()
505
599
 
506
600
 
601
+ class InvalidVersion(Exception):
602
+ def __init__(self, message="invalid version", source=""):
603
+ super().__init__(message)
604
+ self.source = source
605
+
606
+
607
+ class TemplateOlderThanCurrentVersion(Exception):
608
+
609
+ def __init__(self, message="template older", current_vsn=[], template_vsn=[]):
610
+ super().__init__(message)
611
+ self.current_vsn = ".".join(list(current_vsn))
612
+ self.template_vsn = ".".join(list(template_vsn))
613
+
614
+
615
+ def compare_template_versions(template_dir_path, dest_dir_path):
616
+ template_version_dir = Path.resolve(Path.cwd()) / Path(template_dir_path)
617
+ template_version_file = template_version_dir / USER_CONFIG_VERSION_FILE
618
+ dest_version_file = (
619
+ Path.resolve(Path.cwd()) / Path(dest_dir_path) / USER_CONFIG_VERSION_FILE
620
+ )
621
+
622
+ if not template_version_dir.exists():
623
+ # continue - there is no template to apply
624
+ return "no_template"
625
+
626
+ # will throw file not found if template doesn't have version file
627
+ with display.open_file(
628
+ template_version_file, "r", description="Getting template version..."
629
+ ) as f:
630
+ template_version = get_version_from_file(f)
631
+ if template_version is None:
632
+ raise InvalidVersion(source="template")
633
+
634
+ try:
635
+ with display.open_file(
636
+ dest_version_file, "r", description="Getting existing version..."
637
+ ) as f:
638
+ dest_version = get_version_from_file(f)
639
+ if dest_version is None:
640
+ raise InvalidVersion(source="destination")
641
+ except FileNotFoundError:
642
+ display.print_log("Destination Empty")
643
+ return "empty"
644
+
645
+ for index in range(3):
646
+ if template_version[index] == dest_version[index]:
647
+ continue
648
+ if template_version[index] > dest_version[index]:
649
+ return ["new_version_major", "new_version_minor", "new_version_patch"][
650
+ index
651
+ ] # TODO improve how this is handled
652
+ if template_version[index] < dest_version[index]:
653
+ raise TemplateOlderThanCurrentVersion(
654
+ current_vsn=dest_version, template_vsn=template_version
655
+ )
656
+
657
+ return "up-to-date"
658
+
659
+
660
+ version_regex = re.compile("^\s*(\d+)\.(\d+)\.(\d+)\s*$")
661
+
662
+
663
+ def get_version_from_file(file):
664
+ match = version_regex.match(file.read())
665
+ return match.groups() if match else None
666
+
667
+
668
+ def transfer_user_config_from_template(template_dir_path, dest_dir_path):
669
+ template_dir = Path.resolve(Path.cwd()) / Path(template_dir_path)
670
+ dest_dir = Path.resolve(Path.cwd()) / Path(dest_dir_path)
671
+ do_copy(template_dir,dest_dir, True)
672
+ display.print_log(f"Copied user config to {dest_dir_path} from template")
673
+
674
+
507
675
  # schema utilities
508
676
 
509
677
 
510
- def do_validate(config, schema):
678
+ @cache
679
+ def load_meta_schema():
680
+ schema_location = os.path.join(src_dir, "schemas/meta.schema.json")
681
+
511
682
  try:
512
- jsonschema.validate(
513
- instance=config,
514
- schema=schema,
515
- format_checker=jsonschema.Draft202012Validator.FORMAT_CHECKER,
516
- )
517
- except jsonschema.ValidationError as v_err:
518
- display.print_error(f"Recipe error at {v_err.json_path}:\n\n{v_err.message}")
683
+ with display.open_file(
684
+ schema_location, "rb", description="Loading Meta Schema..."
685
+ ) as file:
686
+ schema = json.load(file)
687
+ return schema
688
+
689
+ except FileNotFoundError:
519
690
  display.print_error(
520
- "Config File is not valid -- unable to start the solution -- please correct the issues flagged above and try again."
691
+ f"Unable to find meta schema. Expected to find it at: {schema_location}"
521
692
  )
522
693
  sys.exit(255)
523
694
 
524
695
 
696
+ def schema_validate(config, schema):
697
+ jsonschema.validate(
698
+ instance=config,
699
+ schema=schema,
700
+ format_checker=jsonschema.Draft202012Validator.FORMAT_CHECKER,
701
+ )
702
+
703
+
525
704
  # source utilities
526
705
 
527
706
 
@@ -676,7 +855,8 @@ TO DO List:
676
855
 
677
856
  Longer term
678
857
  * host side volume specification
679
- * named volumes
858
+ * named volumes - when only has container entry?
859
+ * would need warning if repeated
680
860
  * Environment variables
681
861
  * coveying port and alias mappings to services
682
862
  * external resources
@@ -11,7 +11,7 @@ import sys
11
11
  from rich.live import Live
12
12
  from rich.table import Table
13
13
 
14
- __version__ = "0.0.1"
14
+ __version__ = "0.0.3"
15
15
 
16
16
  typer_app = typer.Typer(name="Shoestring Assembler", no_args_is_help=True)
17
17
 
@@ -38,6 +38,16 @@ def pull(recipe: str = "./recipe.toml"):
38
38
  live.console.print("3")
39
39
 
40
40
 
41
+ @typer_app.command()
42
+ def check_recipe(
43
+ recipe: str = "./recipe.toml",
44
+ ):
45
+ display.print_top_header("Checking Recipe")
46
+ assembler_inst = assembler.Assembler(recipe)
47
+ assembler_inst.load_recipe()
48
+ display.print_top_header("Finished")
49
+
50
+
41
51
  @typer_app.command()
42
52
  def bootstrap(
43
53
  recipe: str = "./recipe.toml",
@@ -54,6 +64,7 @@ def bootstrap(
54
64
  @typer_app.command()
55
65
  def assemble(
56
66
  recipe: str = "./recipe.toml",
67
+ download: bool = True,
57
68
  verbose: Annotated[
58
69
  bool, typer.Option("--verbose", help="enable verbose output")
59
70
  ] = False,
@@ -68,10 +79,9 @@ def assemble(
68
79
  display.print_top_header("Assembling Solution")
69
80
  assembler_inst = assembler.Assembler(recipe)
70
81
  assembler_inst.load_recipe()
71
- assembler_inst.validate_recipe()
72
- assembler_inst.clean()
73
- assembler_inst.verify_filestructure(check_sources=False)
74
- assembler_inst.gather_base_service_modules()
82
+ assembler_inst.clean(clean_sources=download)
83
+ assembler_inst.verify_filestructure(check_sources=not download)
84
+ assembler_inst.gather_base_service_modules(do_gather=download)
75
85
  assembler_inst.check_user_config()
76
86
  assembler_inst.generate_compose_file()
77
87
  display.print_top_header("Finished")
@@ -102,3 +112,12 @@ def app():
102
112
 
103
113
  if __name__ == "__main__":
104
114
  app()
115
+
116
+
117
+ """
118
+ * shoestring
119
+ * assemble
120
+ * check-recipe
121
+ * install_docker
122
+ * check_docker
123
+ """
@@ -1,9 +1,11 @@
1
1
  from rich.console import Console
2
2
  from rich.panel import Panel
3
+ import rich.progress
3
4
 
4
5
  root_console = Console()
5
6
  debug = False
6
7
 
8
+ PREFIX = "[grey58]> [/grey58]"
7
9
  # TODO: tee to file here
8
10
 
9
11
  def _get_console(console):
@@ -16,7 +18,7 @@ def _get_console(console):
16
18
  def print_top_header(text,console=None):
17
19
  console = _get_console(console)
18
20
  console.rule(f"[bold cyan]{text}", align="center")
19
-
21
+
20
22
  def print_header(text, console=None):
21
23
  console = _get_console(console)
22
24
  # console.rule(f"[bold bright_magenta]{text}", style="bright_magenta", align="center")
@@ -31,14 +33,36 @@ def print_complete(text, console=None):
31
33
  # align="center",
32
34
  # )
33
35
  # console.print(Panel(f":white_check_mark: {text}", style="green"))
34
- console.print(f":white_check_mark:\t{text}", style="green")
36
+ console.print(f"{PREFIX}:white_check_mark: {text}", style="green")
37
+
38
+ def print_notification(text,console=None):
39
+ console = _get_console(console)
40
+ console.print(
41
+ Panel(
42
+ f"[bold bright_cyan]{text}[/bold bright_cyan]",
43
+ title="[bold bright_cyan]:information: Notification",
44
+ title_align="left",
45
+ style="bright_cyan",
46
+ )
47
+ )
48
+
49
+ def print_warning(text, console=None):
50
+ console = _get_console(console)
51
+ console.print(
52
+ Panel(
53
+ f"[bold yellow]{text}[/bold yellow]",
54
+ title="[bold yellow]:warning: Warning",
55
+ title_align="left",
56
+ style="yellow",
57
+ )
58
+ )
35
59
 
36
60
 
37
61
  def print_error(text, console=None):
38
62
  console = _get_console(console)
39
63
  console.print(
40
64
  Panel(
41
- f"[bold red]{text}",
65
+ f"[bold red]{text}[/bold red]",
42
66
  title="[bold red]:warning: Error",
43
67
  title_align="left",
44
68
  style="red",
@@ -48,7 +72,7 @@ def print_error(text, console=None):
48
72
 
49
73
  def print_log(text, console=None):
50
74
  console = _get_console(console)
51
- console.print(text)
75
+ console.print(f"{PREFIX}{text}")
52
76
 
53
77
  def print_debug(text,console = None):
54
78
  if not debug:
@@ -56,9 +80,17 @@ def print_debug(text,console = None):
56
80
  console = _get_console(console)
57
81
  console.print(
58
82
  Panel(
59
- f"[bold grey58]{text}",
83
+ f"[bold grey58]{text}[/bold grey58]",
60
84
  title="[bold grey58]Verbose log",
61
85
  title_align="left",
62
86
  style="grey58",
63
87
  )
64
88
  )
89
+
90
+ def open_file(*args, **kwargs):
91
+ if "description" in kwargs:
92
+ # add prefix and suffix padding
93
+ kwargs["description"] = f'{PREFIX}{kwargs["description"]}'.ljust(30, " ")
94
+ return rich.progress.open(
95
+ *args, **kwargs
96
+ )
@@ -0,0 +1,85 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://digitalshoestring.net/meta.schema.json",
4
+ "title": "Base Service Module Meta File Schema",
5
+ "description": "Defines key details about a service module source for the Shoestring Solution Assembler",
6
+ "type": "object",
7
+ "patternProperties": {
8
+ "^.*$": {
9
+ "$comment": "the pattern above doesn't matter as it never makes it into the final compose file",
10
+ "description": "container definition",
11
+ "type": "object",
12
+ "required": [
13
+ "dockerfile",
14
+ "volume"
15
+ ],
16
+ "properties": {
17
+ "dockerfile": {
18
+ "type": "string",
19
+ "description": "path to the Dockerfile from the source root"
20
+ },
21
+ "compose_partial": {
22
+ "type": "string",
23
+ "description": "path from the source root to a yaml snippet that include additional compose entries to add for this container (if present)"
24
+ },
25
+ "volume": {
26
+ "description": "container definition",
27
+ "type": "object",
28
+ "required": [
29
+ "data",
30
+ "user_config"
31
+ ],
32
+ "properties": {
33
+ "data": {
34
+ "$ref": "#/$defs/volume_definition_schema",
35
+ "description": "volume description for containers data volume"
36
+ },
37
+ "user_config": {
38
+ "$ref": "#/$defs/volume_definition_schema",
39
+ "description": "volume description for containers user config volume"
40
+ }
41
+ },
42
+ "patternProperties": {
43
+ "^.*$": {
44
+ "$ref": "#/$defs/volume_definition_schema",
45
+ "description": "other custom volume descriptions"
46
+ }
47
+ }
48
+ }
49
+ }
50
+ }
51
+ },
52
+ "$defs": {
53
+ "volume_definition_schema": {
54
+ "description":"sub schema for a volume definition",
55
+ "type":"object",
56
+ "properties":{
57
+ "ignore":{
58
+ "description":"If true, this volume definition isn't used in the compose file",
59
+ "enum":[true]
60
+ },
61
+ "path":{
62
+ "description":"The internal path at which the volume is mounted",
63
+ "type":"string"
64
+ },
65
+ "mode":{
66
+ "description":"The mount type",
67
+ "enum":["ro","rw"]
68
+ }
69
+ },
70
+ "$comment": "Ensures that volume description either has a path or is ignored",
71
+ "anyOf":[
72
+ {
73
+ "required":[
74
+ "path"
75
+ ]
76
+ },
77
+ {
78
+ "required":[
79
+ "ignore"
80
+ ]
81
+ }
82
+ ]
83
+ }
84
+ }
85
+ }
@@ -2,8 +2,14 @@
2
2
  "$schema": "https://json-schema.org/draft/2020-12/schema",
3
3
  "$id": "https://digitalshoestring.net/recipe.schema.json",
4
4
  "title": "Shoestring Solution Recipe Schema",
5
- "description": "A product in the catalog",
5
+ "description": "Defines a recipe for a shoestring solution",
6
6
  "type": "object",
7
+ "required": [
8
+ "recipe_vsn",
9
+ "solution",
10
+ "source",
11
+ "service_module"
12
+ ],
7
13
  "properties": {
8
14
  "recipe_vsn": {
9
15
  "description": "Version of the recipe specification that has been used",
@@ -13,6 +19,11 @@
13
19
  "solution": {
14
20
  "description": "",
15
21
  "type": "object",
22
+ "required": [
23
+ "name",
24
+ "slug",
25
+ "version"
26
+ ],
16
27
  "properties": {
17
28
  "name": {
18
29
  "description": "Name of the solution this recipe is for",
@@ -32,24 +43,22 @@
32
43
  "description": "A description of the solution",
33
44
  "type": "string"
34
45
  }
35
- },
36
- "required": [
37
- "name",
38
- "slug",
39
- "version"
40
- ]
46
+ }
41
47
  },
42
48
  "source": {
43
- "description": "Key-Value map relating source names to a source description",
49
+ "description": "Key-Value map relating source names to their source descriptions",
44
50
  "type": "object",
45
51
  "patternProperties": {
46
- "^.*$": {
52
+ "^[\\w\\.][\\w\\.-]*$": {
47
53
  "description": "Source description",
48
54
  "type": "object",
49
55
  "properties": {
50
56
  "file": {
51
57
  "description": "File source description",
52
58
  "type": "object",
59
+ "required": [
60
+ "path"
61
+ ],
53
62
  "properties": {
54
63
  "path": {
55
64
  "description": "File system path for the source",
@@ -62,10 +71,7 @@
62
71
  "link"
63
72
  ]
64
73
  }
65
- },
66
- "required": [
67
- "path"
68
- ]
74
+ }
69
75
  },
70
76
  "git": {
71
77
  "description": "Git source description",
@@ -116,11 +122,88 @@
116
122
  ]
117
123
  }
118
124
  }
125
+ },
126
+ "service_module": {
127
+ "description": "Key-Value map relating service module names to their module descriptions",
128
+ "type": "object",
129
+ "patternProperties": {
130
+ "^[\\w\\.][\\w\\.-]*$": {
131
+ "$ref": "#/$defs/module_schema",
132
+ "description": "module description"
133
+ }
134
+ }
135
+ },
136
+ "infrastructure": {
137
+ "description": "Key-Value map relating infrastructure module names to their module descriptions",
138
+ "type": "object",
139
+ "patternProperties": {
140
+ "^[\\w\\.][\\w\\.-]*$": {
141
+ "$ref": "#/$defs/module_schema",
142
+ "description": "module description"
143
+ }
144
+ }
119
145
  }
120
146
  },
121
- "required": [
122
- "recipe_vsn",
123
- "solution",
124
- "source"
125
- ]
147
+ "$defs": {
148
+ "module_schema": {
149
+ "description": "sub-schema for service module and infrastructure module definitions",
150
+ "type": "object",
151
+ "required":[
152
+ "source",
153
+ "containers"
154
+ ],
155
+ "properties": {
156
+ "source": {
157
+ "description": "Reference to the source the module uses",
158
+ "type": "string"
159
+ },
160
+ "containers": {
161
+ "description": "Array of container names to include",
162
+ "type": "array",
163
+ "items": {
164
+ "type": "string"
165
+ }
166
+ },
167
+ "ports": {
168
+ "description": "Key-value map of container name to named port",
169
+ "type": "object",
170
+ "patternProperties": {
171
+ "^[\\w\\.][\\w\\.-]*$": {
172
+ "description": "Key-value map of named port to port number",
173
+ "type": "object",
174
+ "patternProperties": {
175
+ "^[\\w\\.][\\w\\.-]*$": {
176
+ "description": "port number",
177
+ "type": "integer"
178
+ }
179
+ }
180
+ }
181
+ }
182
+ },
183
+ "alias": {
184
+ "description": "Key-value map of container name to docker network alias",
185
+ "type": "object",
186
+ "patternProperties": {
187
+ "^[\\w\\.][\\w\\.-]*$": {
188
+ "description": "network alias to use (suffix '.docker.local' always added by assembler)",
189
+ "type": "string"
190
+ }
191
+ }
192
+ },
193
+ "template": {
194
+ "description": "Key-value map of template variables that are applied prior to parsing partial compose yaml for the module",
195
+ "type": "object",
196
+ "patternProperties": {
197
+ "^[\\w\\.][\\w\\.-]*$": {
198
+ "description": "value used to replace template variable",
199
+ "type": [
200
+ "integer",
201
+ "string"
202
+ ]
203
+ }
204
+ }
205
+ }
206
+ }
207
+ }
208
+ }
126
209
  }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: shoestring_assembler
3
- Version: 0.0.2
3
+ Version: 0.0.3
4
4
  Summary: Assembles Shoestring Solutions from a solution recipe
5
5
  Author-email: Greg Hawkridge <gth24@cam.ac.uk>
6
6
  Maintainer-email: Greg Hawkridge <gth24@cam.ac.uk>
@@ -20,13 +20,14 @@ src/shoestring_assembler/assembler.py
20
20
  src/shoestring_assembler/cli.py
21
21
  src/shoestring_assembler/display.py
22
22
  src/shoestring_assembler/minimal_mustache.py
23
- src/shoestring_assembler/recipe.schema.json
24
23
  src/shoestring_assembler.egg-info/PKG-INFO
25
24
  src/shoestring_assembler.egg-info/SOURCES.txt
26
25
  src/shoestring_assembler.egg-info/dependency_links.txt
27
26
  src/shoestring_assembler.egg-info/entry_points.txt
28
27
  src/shoestring_assembler.egg-info/requires.txt
29
28
  src/shoestring_assembler.egg-info/top_level.txt
29
+ src/shoestring_assembler/schemas/meta.schema.json
30
+ src/shoestring_assembler/schemas/recipe.schema.json
30
31
  tests/__init__.py
31
32
  tests/console.log
32
33
  tests/test_shoestring_assembler.py