Skip to content

Instantly share code, notes, and snippets.

@JollyWizard
Last active September 1, 2020 06:41
Show Gist options
  • Save JollyWizard/7a735c3477fe7a5ec20d926088f59fd3 to your computer and use it in GitHub Desktop.
Save JollyWizard/7a735c3477fe7a5ec20d926088f59fd3 to your computer and use it in GitHub Desktop.
An example using `ytt`'s data annotations and a function in a separate source code file.
load("@ytt:struct", "struct")
def process(data):
r = []
#! data module returns struct.
#! struct needs to be decoded to a collection.
decoded = struct.decode(data)
r.append('TYPE ' + type(decoded))
#! decoded data is assumed to be list
#! a.k.a `data.volumes`
for d in decoded:
#! data collected before inner keys modified
r.append(d)
#! Narrow context to the mappings array.
mapping = d['mapping']
#! Read map keys
host = mapping['host']
guest = mapping['guest']
#! Create derived string.
mapping['volume'] = host + ":" + guest
end
return r
end
#! This example uses the `@ytt/data` library to declare configuration data in yml.
#! The yml data is then processed in a pythonic script.
#! Use data file mechanism to load data.
#@ load("@ytt:data", "data")
#! Use `starlark` to provide pythonic functions.
#@ load("funcs.star", "process")
output: #@ process(data.values.volumes)
#@data/values
#! This file contains volume maping information to be transformed for injection into a docker compose file.
---
volumes:
- name: host-dir
description: Maps the host working directory inside the container.
mapping:
host: ./
guest: /host/
- name: mysql-data
description: The data folder for the mysql service.
mapping:
host: ./.volumes/mysql/
guest: /var/lib/mysql
- name: wordpress-data
description: The root folder of the wordpress application.
mapping:
host: ./.volumes/wp-data
guest: /var/www/html
- name: wordpress-cli-data
description: The user folder where wp-cli packages are installed.
mapping:
host: ./.volumes/wp-data
guest: /home/www-data/.wp-cli
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment