mantra 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1e17d43a33f2dfb75f61a53d8284f4a81e48eb94
4
- data.tar.gz: 3ac128bc7cecbd4f04137c224d729b8204b910e4
3
+ metadata.gz: f4c83562bb8a27496087aa37e7585c7b8732a2a4
4
+ data.tar.gz: 359689e95b93eddb73263abd1d9d95839a3353c3
5
5
  SHA512:
6
- metadata.gz: b290777d30ccb5c13e64c7fb64aead9588802a74464fb4276747ca0022e23a7815d25df308ee3cd26b5a4d8b296b9912c08f43641ea88febda50ad8b42a808e5
7
- data.tar.gz: 3fffbdda8395fd0382ac44f8c38244fc6da301af70e3881cdbf22696f59676afbd596b5b20ef2bb97624dca98ea3d76851e48bf4ea155ea5deb3270f875cfc27
6
+ metadata.gz: aefe2b31df347529fbb9fcc28e53f8046f87587aea529f5b4fb9d790e22f52cadd33c185ba319eb1bb7404de0f14d6a89ec3c6e9c7bb9453979ab13f84750330
7
+ data.tar.gz: 34ed3e362bb454af43997aec1e06fd5de3b226687cc90d1187adde4a6e10326ba01cb03e63474ebbb9d7310fedea0e82d859e6dc9f779975eb9661a3d4a57834
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
  /tmp/
10
10
  *.gem
11
11
  .ruby-*
12
+ tmp
data/LICENSE.md ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md CHANGED
@@ -4,20 +4,21 @@
4
4
 
5
5
  ### Description
6
6
 
7
- Mantra (/ˈmæntrə/, abr. **Man**ifest **Tra**nsformation) is a tool to ease work with manifests.
7
+ Mantra (/ˈmæntrə/, abr. **Man**ifest **Tra**nsformation for BOSH) is a tool to ease work with BOSH manifests.
8
8
 
9
- It allows to do following:
9
+ It allows to perform such routines as:
10
10
 
11
11
  * find manifest parts
12
- * extract cloud config from you BOSH v1 manifest
13
- * convert your manifest into spiff templates with
12
+ * extract cloud config for BOSH v2 from your BOSH v1 manifest
13
+ * convert you BOSH v1 manifest to BOSH v2 manifest
14
+ * convert your manifest into bosh-cli templates with
14
15
  * extracted passwords
15
- * extracted certificates to different files
16
- * templatized network configuration
17
- * extracted or changed properties
16
+ <!-- * extracted certificates to different files -->
17
+ <!-- * templatized network configuration -->
18
+ <!-- * extracted or changed properties -->
18
19
  * and much more
19
20
 
20
- Mantra is tend to be an easily extendable tool, so you can add new commands and transforms.
21
+ Mantra tends to be an easily extendable tool, so you can add new commands and transforms by yourself. If you need have any questions or suggestions feel free to create an [issue on Github](https://github.com/allomov/mantra/issues) or contact [me](https://github.com/allomov) directly.
21
22
 
22
23
  ### Concepts
23
24
 
@@ -28,119 +29,21 @@ While some actions performed on manifest can be automated, anther require specia
28
29
  `Target Manifest` - manifest that will contain extracted template properties, to restore source manifest you'll need to run merge tool with resulting source manifest.
29
30
  `Transform` - a ruby class that declares how specific transformation should be done.
30
31
 
31
- ### Examples
32
-
33
- Find and output in json some manifest parts:
34
-
35
- ```
36
- mantra find jobs[name=consul].properties -m manifest.yml -f json
37
- # alias: mantra f
38
- ```
39
-
40
- Transform BOSH v1 manifest to extract cloud config:
41
-
42
- ```
43
- mantra transform -c transformation-manifest.yml -m manifest.yml
44
- # mantra t
45
- ```
46
-
47
- Here is an example of `transformation-manifest.yml`:
48
-
49
- ```yaml
50
- global:
51
- source: manifest.yml
52
- target: cloud-config.yml
53
- transforms:
54
- - filter:
55
- sections: ["networks", "compilation", "update", "resource_pools", "disk_pools"]
56
- - rename:
57
- section: "resource_pools"
58
- to: "vm_types"
59
- - rename:
60
- section: "disk_pools"
61
- to: "disk_types"
62
- - type: add
63
- section:
64
- az: z1
65
- to: "networks[].subnets[]"
66
- - add:
67
- section:
68
- az: z1
69
- to: "update"
70
- - add:
71
- section:
72
- az: z1
73
- to: "compilation"
74
- - add:
75
- section:
76
- azs:
77
- - name: z1
78
- to: ""
79
- ```
80
-
81
-
82
- ```yaml
83
- global_vars:
84
- source: manifest.yml
85
- transforms:
86
- - extract:
87
- section: jobs
88
- to: workspace/jobs.yml
89
- - extract-certs:
90
- from: workspace/properties.yml
91
- to: workspace/secrets.yml
92
- - extract-certs-to-files:
93
- from: workspace/secrets.yml
94
- to: workspace/secrets
95
- - extract:
96
- section: jobs
97
- to:
98
- file: manifest.yml
99
- with_scope: meta.jobs
100
- - extract:
101
- source: manifest.yml
102
- section: resource_pools
103
- - extract:
104
- section: resource_pools
105
- to: resource_pools.yml
106
- - extract:
107
- section: networks
108
- to: networks.yml
109
- - templatize-ip-address:
110
- from: networks.yml
111
- to: workspace/stub.yml
112
- quads:
113
- - range: "1-2"
114
- scope: meta.networks.cf.prefix
115
- with_value: "192.168"
116
- - number: 3
117
- scope: meta.networks.cf.quad1
118
- with_value: 2
119
- - number: 3
120
- scope: meta.networks.cf.quad2
121
- with_value: 3
122
- - templatize-value:
123
- source: manifest.yml
124
- to: workspace/stub.yml
125
- value: cfdomain.com
126
- in: "*.cfdomain.com"
127
- scope: meta.domain
128
- ```
32
+ One more concept: no nokogiri and native dependencies.
129
33
 
34
+ ### Examples
130
35
 
131
- ## Installation
36
+ - [Simple example of how to use a transform](https://github.com/allomov/mantra/blob/master/examples/filter-transform.md)
37
+ - [Example of `mantra merge` command to update your manifest](https://github.com/allomov/mantra/blob/master/examples/merge-command.md)
38
+ - [Convert BOSH v1 manifest to BOSH v2 manifest](https://github.com/allomov/mantra/blob/master/examples/convert-bosh-manifest-to-v2.md)
39
+ - [Extract cloud config from BOSH v1 manifest](https://github.com/allomov/mantra/blob/master/examples/extract-cloud-config.md)
132
40
 
133
- Install it as a gem:
41
+ ### Run tests
134
42
 
135
43
  ```
136
- $ gem install mantra
44
+ rake
137
45
  ```
138
46
 
139
- ## Dependencies
140
-
141
- `openssl`!
142
-
143
47
  ## Contributing
144
48
 
145
49
  Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mantra. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
146
-
@@ -0,0 +1,98 @@
1
+
2
+ # Convert BOSH manifest v1 to manifest v2
3
+
4
+ Imagine you have a following BOSH v1 manifest in following format:
5
+
6
+ ```file:manifest.yml
7
+ compilation:
8
+ cloud_properties: {cpu: 1}
9
+ network: redis_z1
10
+ director_uuid: DIRECTOR_ID
11
+ jobs:
12
+ - instances: 3
13
+ name: dedicated-node
14
+ networks:
15
+ - name: redis_z1
16
+ static_ips:
17
+ - 10.0.0.51
18
+ persistent_disk: 4096
19
+ resource_pool: redis_z1
20
+ templates:
21
+ - {name: dedicated-node, release: cf-redis}
22
+ - {name: syslog-configurator, release: cf-redis}
23
+ - instances: 1
24
+ lifecycle: errand
25
+ name: smoke-tests
26
+ networks:
27
+ - name: redis_z1
28
+ resource_pool: redis_z1
29
+ templates:
30
+ - {name: smoke-tests, release: cf-redis}
31
+ networks:
32
+ - name: redis_z1
33
+ subnets:
34
+ - cloud_properties:
35
+ name: YOUR_SUBNET_NAME
36
+ dns:
37
+ - 8.8.8.8
38
+ gateway: 10.0.0.1
39
+ range: 10.0.0.0/24
40
+ reserved:
41
+ - 10.0.0.2 - 10.0.0.49
42
+ static:
43
+ - 10.0.0.50 - 10.0.0.99
44
+ properties:
45
+ redis:
46
+ agent:
47
+ backend_port: 54321
48
+ host: 10.0.0.50
49
+ maxmemory: 262144000
50
+ syslog_aggregator:
51
+ address: REPLACE_WITH_SYSLOG_AGGREGATOR_HOST
52
+ port: REPLACE_WITH_SYSLOG_AGGREGATOR_PORT
53
+ releases:
54
+ - {name: cf-redis, version: latest}
55
+ - {name: routing, version: 0.143.0}
56
+ resource_pools:
57
+ - cloud_properties:
58
+ cpu: 1
59
+ name: redis_z1
60
+ stemcell: {name: bosh-vsphere-esxi-ubuntu-trusty-go_agent, version: latest}
61
+ update:
62
+ canaries: 1
63
+ max_in_flight: 6
64
+ ```
65
+
66
+ You should be able to define transformations in the following format:
67
+
68
+ ```file:transform.yml
69
+ transforms:
70
+ - type: filter
71
+ sections: ["jobs", "releases", "stemcells", "update"]
72
+ - type: rename
73
+ section: "jobs"
74
+ to: "instance_groups"
75
+ - type: rename
76
+ section: "instance_groups[].templates"
77
+ to: "jobs"
78
+ - type: merge
79
+ value:
80
+ azs: az1
81
+ to: "instance_groups[]"
82
+ - type: rename
83
+ section: "instance_groups[].resource_pools"
84
+ to: "vm_type"
85
+ ```
86
+
87
+ And run this command:
88
+
89
+ ```command
90
+ mantra transform -m manifest.yml -t transform.yml
91
+ ```
92
+ <!--
93
+ And get following output:
94
+
95
+ ```output
96
+
97
+ ```
98
+ -->
@@ -0,0 +1,121 @@
1
+
2
+ # How to extract Cloud Config
3
+
4
+ ```file:manifest.yml
5
+ compilation:
6
+ cloud_properties: {cpu: 1}
7
+ network: redis_z1
8
+ director_uuid: DIRECTOR_ID
9
+ jobs:
10
+ - instances: 3
11
+ name: dedicated-node
12
+ networks:
13
+ - name: redis_z1
14
+ static_ips:
15
+ - 10.0.0.51
16
+ persistent_disk: 4096
17
+ resource_pool: redis_z1
18
+ templates:
19
+ - {name: dedicated-node, release: cf-redis}
20
+ - {name: syslog-configurator, release: cf-redis}
21
+ - instances: 1
22
+ lifecycle: errand
23
+ name: smoke-tests
24
+ networks:
25
+ - name: redis_z1
26
+ resource_pool: redis_z1
27
+ templates:
28
+ - {name: smoke-tests, release: cf-redis}
29
+ networks:
30
+ - name: redis_z1
31
+ subnets:
32
+ - cloud_properties:
33
+ name: YOUR_SUBNET_NAME
34
+ dns:
35
+ - 8.8.8.8
36
+ gateway: 10.0.0.1
37
+ range: 10.0.0.0/24
38
+ reserved:
39
+ - 10.0.0.2 - 10.0.0.49
40
+ static:
41
+ - 10.0.0.50 - 10.0.0.99
42
+ properties:
43
+ redis:
44
+ agent:
45
+ backend_port: 54321
46
+ host: 10.0.0.50
47
+ maxmemory: 262144000
48
+ syslog_aggregator:
49
+ address: REPLACE_WITH_SYSLOG_AGGREGATOR_HOST
50
+ port: REPLACE_WITH_SYSLOG_AGGREGATOR_PORT
51
+ releases:
52
+ - {name: cf-redis, version: latest}
53
+ - {name: routing, version: 0.143.0}
54
+ resource_pools:
55
+ - cloud_properties:
56
+ cpu: 1
57
+ name: redis_z1
58
+ stemcell: {name: bosh-vsphere-esxi-ubuntu-trusty-go_agent, version: latest}
59
+ update:
60
+ canaries: 1
61
+ max_in_flight: 6
62
+ ```
63
+
64
+ ```file:transform.yml
65
+ transforms:
66
+ - type: "filter"
67
+ sections: ["networks", "compilation", "update", "resource_pools", "disk_pools"]
68
+ - type: "rename"
69
+ section: "resource_pools"
70
+ to: "vm_types"
71
+ - type: "rename"
72
+ section: "disk_pools"
73
+ to: "disk_types"
74
+ - type: merge
75
+ value: &az-hash
76
+ az: az1
77
+ to: "networks[].subnets[]"
78
+ - type: merge
79
+ value: *az-hash
80
+ to: "update"
81
+ - type: merge
82
+ value: *az-hash
83
+ to: "compilation"
84
+ ```
85
+
86
+ ```command
87
+ mantra transform -m manifest.yml -t transform.yml
88
+ ```
89
+
90
+ ```output
91
+ networks:
92
+ - name: redis_z1
93
+ subnets:
94
+ - cloud_properties:
95
+ name: YOUR_SUBNET_NAME
96
+ dns:
97
+ - 8.8.8.8
98
+ gateway: 10.0.0.1
99
+ range: 10.0.0.0/24
100
+ reserved:
101
+ - 10.0.0.2 - 10.0.0.49
102
+ static:
103
+ - 10.0.0.50 - 10.0.0.99
104
+ az: az1
105
+ compilation:
106
+ cloud_properties:
107
+ cpu: 1
108
+ network: redis_z1
109
+ az: az1
110
+ update:
111
+ canaries: 1
112
+ max_in_flight: 6
113
+ az: az1
114
+ vm_types:
115
+ - cloud_properties:
116
+ cpu: 1
117
+ name: redis_z1
118
+ stemcell:
119
+ name: bosh-vsphere-esxi-ubuntu-trusty-go_agent
120
+ version: latest
121
+ ```
@@ -0,0 +1,46 @@
1
+ # Filter transform
2
+
3
+ ### Description
4
+
5
+ Filter all sections of a manifest, leaving only those you mention in parameters.
6
+
7
+ ### Example
8
+
9
+ For example you may have `manifest.yml` file wit the following content:
10
+
11
+ ```file:manifest.yml
12
+ section1:
13
+ key: value
14
+ properties: {cpu: 1}
15
+ section2: "value"
16
+ section3: [a1, a2, a3]
17
+ section4: "some-value"
18
+ ```
19
+
20
+ Config for filter transform in `transform.yml` will look as following:
21
+
22
+ ```file:transform.yml
23
+ transforms:
24
+ - type: "filter"
25
+ sections: ["section1", "section3"]
26
+ ```
27
+
28
+ To perform transformation run this command:
29
+
30
+ ```command
31
+ mantra transform -m manifest.yml -t transform.yml
32
+ ```
33
+
34
+ The output that you'll get is:
35
+
36
+ ```output
37
+ section1:
38
+ key: value
39
+ properties:
40
+ cpu: 1
41
+ section3:
42
+ - a1
43
+ - a2
44
+ - a3
45
+ ```
46
+
@@ -0,0 +1,44 @@
1
+
2
+ # Example of mantra merge command
3
+
4
+ Imagine you have in your `manifest.yml` file something like this:
5
+
6
+ ```file:manifest.yml
7
+ networks:
8
+ - name: redis_z1
9
+ releases:
10
+ - name: cf-redis
11
+ version: 23
12
+ - name: cf-redis-broker
13
+ version: 13
14
+ - name: backup-and-monitoring
15
+ version: 3
16
+ update:
17
+ canaries: 1
18
+ max_in_flight: 6
19
+ ```
20
+
21
+ And you want to update all releases which names start from `cf` prefix to have `"latest"` version. You can do it using `mantra merge` command as following:
22
+
23
+ ```command
24
+ mantra merge -m manifest.yml -j '{"version": "latest"}' -s releases[name=cf-*]
25
+ ```
26
+
27
+ As an output you will have a following manifest:
28
+
29
+ ```output
30
+ networks:
31
+ - name: redis_z1
32
+ releases:
33
+ - name: cf-redis
34
+ version: latest
35
+ - name: cf-redis-broker
36
+ version: latest
37
+ - name: backup-and-monitoring
38
+ version: 3
39
+ update:
40
+ canaries: 1
41
+ max_in_flight: 6
42
+ ```
43
+
44
+ As you see all release versions that start with `cf` prefix got updated versions to the `latest`.
@@ -48,4 +48,5 @@ module Mantra
48
48
  end
49
49
 
50
50
  require "mantra/commands/find"
51
+ require "mantra/commands/merge"
51
52
  require "mantra/commands/transform"
@@ -0,0 +1,34 @@
1
+ require "yaml"
2
+ require "json"
3
+
4
+ module Mantra
5
+ class Commands
6
+ class Merge < Command
7
+ type :merge
8
+ alias_type :m
9
+ attr_accessor :manifest
10
+
11
+ option :json, "--json JSON_STRING", "-j", "json in text format"
12
+ option :file, "--file JSON_FILE", "-f", "File that should be merged to the manifest (not implemented)"
13
+ option :path, "--scope PATH", "-s", "Scope (or path) in Manifest where to merge this value"
14
+ option :manifest_path, "--manifest MANIFEST", "-m", "Manifest path"
15
+
16
+ def perform
17
+ if json.nil? && file.nil?
18
+ raise "json of file should be specified"
19
+ end
20
+
21
+ json_string = json.nil? ? File.read(file) : json
22
+ object_to_merge = JSON.parse(json_string)
23
+ element_to_merge = Mantra::Manifest::Element.create(object_to_merge)
24
+ manifest = Mantra::Manifest.new(manifest_path)
25
+ manifst_elements = manifest.select(path || "")
26
+ manifst_elements.each do |e|
27
+ e.merge(element_to_merge, force: true)
28
+ end
29
+ puts manifest.to_ruby_object.to_yaml
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -7,7 +7,7 @@ module Mantra
7
7
  alias_type :t
8
8
  attr_accessor :manifest
9
9
 
10
- option :transform_config_path, "--config CONFIG", "-c", "Transformation config path"
10
+ option :transform_config_path, "--config CONFIG", "-t", "Transformation config path"
11
11
  option :manifest_path, "--manifest MANIFEST", "-m", "Manifest path"
12
12
 
13
13
  def transform_config
@@ -15,22 +15,27 @@ module Mantra
15
15
  end
16
16
 
17
17
  def transforms
18
+ return @transforms unless @transforms.nil?
18
19
  previous_transform = self
19
- transform_config["transforms"].map do |t|
20
- t.merge!(previous_transform: previous_transform)
21
- previous_transform = Mantra::Transform.create(t)
20
+ @transforms = transform_config["transforms"].map do |options|
21
+ options.merge!(previous_transform: previous_transform)
22
+ previous_transform = Mantra::Transform.create(options)
22
23
  end
23
24
  end
24
25
 
25
- def manifest
26
- @manifest ||= Manifest.new(self.manifest_path)
26
+ def result
27
+ @manifest ||= Manifest.new(self.manifest_path).root
27
28
  end
28
29
 
29
30
  def perform
30
31
  transforms.each do |t|
31
32
  t.run
32
33
  end
33
- puts manifest.to_ruby_object.to_yaml
34
+ unless transforms.last.nil?
35
+ puts transforms.last.result.to_ruby_object.to_yaml
36
+ else
37
+ puts {}.to_yaml
38
+ end
34
39
  end
35
40
 
36
41
  end
@@ -11,16 +11,19 @@ module Mantra
11
11
  end
12
12
 
13
13
  def merge(element, options={})
14
+ element = escape_root(element)
14
15
  raise merge_conflict_error(element) unless self.can_merge?(element)
15
16
  elements_to_add = element.content.dup
17
+ elements_to_add.each { |e| e.parent = self }
16
18
  merge_by_name(elements_to_add, options)
17
19
  merge_by_value(elements_to_add, options)
20
+ # keep the element structure consistent
18
21
  self.content.concat(elements_to_add)
19
22
  self
20
23
  end
21
24
 
22
25
  def find_children_by_scope(scope)
23
- return [] unless scope.array?
26
+ return Element.create([]) unless scope.array?
24
27
  self.content.map do |element|
25
28
  if scope.match?(element)
26
29
  if scope.has_next?
@@ -90,6 +90,8 @@ module Mantra
90
90
  end
91
91
  end
92
92
 
93
+ # WARNING: merge method "obsorbs" and ruins the element from paramenters
94
+ # TOFIX: make duplicate of element structure that you merge
93
95
  def merge(element, options={})
94
96
  raise "not implemented"
95
97
  end
@@ -103,6 +105,10 @@ module Mantra
103
105
  MergeConflictError.new("merge conflicts: types: #{self.class} against #{element.class}, values: #{self.to_ruby_object} with #{object}")
104
106
  end
105
107
 
108
+ def escape_root(element)
109
+ element.type == :root ? element.content : element
110
+ end
111
+
106
112
  def can_merge?(element)
107
113
  if element.respond_to?(:type)
108
114
  self.type == element.type
@@ -143,6 +149,9 @@ module Mantra
143
149
  raise "not implemented"
144
150
  end
145
151
 
152
+ # there is a number of limitations on this method:
153
+ # you can not use wildcard characters, for instance jobs[name=*]
154
+ # you can not use selectors with array filters, such as jobs[name=sample]
146
155
  def self.element_with_selector(selector, value)
147
156
  object_to_add = {}
148
157
  parts = selector.split(".")
@@ -12,7 +12,10 @@ module Mantra
12
12
  end
13
13
 
14
14
  def merge(element, options={})
15
+ element = escape_root(element)
15
16
  raise merge_conflict_error(element) unless self.can_merge?(element)
17
+ # keep the element structure consistent
18
+ element.content.values.each { |v| v.parent = self }
16
19
  self.content.merge!(element.content) do |_, self_element, element_to_merge|
17
20
  self_element.merge(element_to_merge, options)
18
21
  end
@@ -9,10 +9,12 @@ module Mantra
9
9
  end
10
10
 
11
11
  def merge(element, options={})
12
+ element = escape_root(element)
12
13
  raise merge_conflict_error(element) unless self.can_merge?(element)
13
14
  if !options[:force]
14
15
  raise MergeConflictError.new("value conflict detected: #{self.content} != #{element.content}") if self.content != element.content
15
16
  end
17
+ self.content = element.content
16
18
  self
17
19
  end
18
20
 
@@ -49,7 +49,7 @@ module Mantra
49
49
  end
50
50
 
51
51
  def last?
52
- self.is_a?(EmptyScope)
52
+ self.next.is_a?(EmptyScope)
53
53
  end
54
54
 
55
55
  alias_method :tail, :next
@@ -0,0 +1,30 @@
1
+ module Mantra
2
+ class Transform
3
+ class Filter < Mantra::Transform
4
+ type :filter
5
+ attr_accessor :result
6
+
7
+ description "Filter"
8
+
9
+ input "sections", description: "list of paths to elements that should be left after this transform ", type: :any
10
+
11
+ def result
12
+ @result
13
+ end
14
+
15
+ def perform
16
+ @result = Mantra::Manifest::Element.create({})
17
+ manifest = previous_transform.result
18
+ sections.each_with_index do |p, i|
19
+ elements = manifest.select(p)
20
+ elements.each do |e|
21
+ direct_path = e.path
22
+ root_element_to_merge = Mantra::Manifest::Element.element_with_selector(direct_path, e.content)
23
+ @result.merge(root_element_to_merge)
24
+ end
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -1,26 +1,29 @@
1
1
  module Mantra
2
2
  class Transform
3
3
  class Merge < Mantra::Transform
4
- type :"merge"
5
- attr_accessor :manifest
4
+ type :merge
6
5
 
7
6
  description "Merge"
8
7
 
9
8
  input "value", description: "value that should be merged in", type: :any
10
9
 
11
- input "scope", description: "scope (or path) in stub file that will have current value (for instance meta.networks)",
12
- type: :string
10
+ input "to", description: "scope (or path) in stub file that will have current value (for instance meta.networks)",
11
+ type: :string
12
+ alias_method :scope, :to
13
13
 
14
14
  def perform
15
- @manifest = previous_transform.manifest
16
- value_manifest_element = Mantra::Manifest::Element.create(self.value).content
15
+ @manifest = previous_transform.result
16
+ element_to_merge = Mantra::Manifest::Element.create(self.value).content
17
17
 
18
- scope = @manifest.fetch(self.scope)
19
- scope.each do |s|
20
- s.merge(value_manifest_element)
18
+ scope_elements = @manifest.select(self.scope)
19
+ scope_elements.each do |element_from_scope|
20
+ element_from_scope.merge(element_to_merge)
21
21
  end
22
22
  end
23
23
 
24
+ def result
25
+ @manifest
26
+ end
24
27
  end
25
28
  end
26
29
  end
@@ -0,0 +1,35 @@
1
+ module Mantra
2
+ class Transform
3
+ class Rename < Mantra::Transform
4
+ type :rename
5
+ attr_accessor :manifest
6
+
7
+ description "Rename section"
8
+
9
+ input "section", description: "path to the section that should be renamed ", type: :any
10
+ input "to", description: "new name for the section", type: :any
11
+ alias_method :new_name, :to
12
+ alias_method :result, :manifest
13
+
14
+ def result
15
+ @manifest
16
+ end
17
+
18
+ def perform
19
+ @manifest = previous_transform.result
20
+ elemenents_to_rename = @manifest.select(section)
21
+ elemenents_to_rename.each do |element|
22
+ parent = element.parent
23
+ raise Mantra::Transform::ValidationError.new ("parent should be a hash") unless parent.hash?
24
+ unless parent.find(new_name).nil?
25
+ raise Mantra::Transform::ValidationError.new("Section with the name #{new_name} already exists.")
26
+ end
27
+ old_name = parent.selector_for(element)
28
+ parent.content[new_name] = element
29
+ parent.content.delete(old_name)
30
+ end
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -12,7 +12,7 @@ module Mantra
12
12
  type: :string
13
13
 
14
14
  def perform
15
- @manifest = previous_transform.manifest
15
+ @manifest = previous_transform.result
16
16
  # value_manifest_element = Mantra::Manifest::Element.create(self.value).content
17
17
 
18
18
  scope = @manifest.fetch(self.scope)
@@ -65,6 +65,10 @@ module Mantra
65
65
  raise "not implemented"
66
66
  end
67
67
 
68
+ def result
69
+ raise "not implemented"
70
+ end
71
+
68
72
  def ensure_yml_file_exist(path)
69
73
  return if File.exist?(path)
70
74
  pathname = Pathname.new(path)
@@ -110,6 +114,8 @@ require "mantra/transform/inputs/folder"
110
114
  require "mantra/transform/inputs/any"
111
115
 
112
116
  require "mantra/transform/merge"
117
+ require "mantra/transform/filter"
118
+ require "mantra/transform/rename"
113
119
  require "mantra/transform/replace"
114
120
  require "mantra/transform/extract_section"
115
121
  require "mantra/transform/extract_certificates"
@@ -1,3 +1,3 @@
1
1
  module Mantra
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
data/mantra.gemspec CHANGED
@@ -30,4 +30,5 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency "rake", "~> 10.0"
31
31
  spec.add_development_dependency "rspec", "~> 3.4"
32
32
  spec.add_development_dependency "rspec-its", "~> 1.2"
33
+ spec.add_development_dependency 'redcarpet', '~> 3.4', '>= 3.4.0'
33
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mantra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Lomov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-23 00:00:00.000000000 Z
11
+ date: 2017-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,26 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: redcarpet
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 3.4.0
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: '3.4'
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 3.4.0
69
89
  description: Manifest Transfromation for BOSH
70
90
  email:
71
91
  - lomov.as@gmail.com
@@ -78,6 +98,7 @@ files:
78
98
  - ".rspec"
79
99
  - ".travis.yml"
80
100
  - Gemfile
101
+ - LICENSE.md
81
102
  - README.md
82
103
  - Rakefile
83
104
  - bin/mantra
@@ -90,12 +111,17 @@ files:
90
111
  - ci/stub.yml
91
112
  - ci/tasks/run-tests-dev.yml
92
113
  - ci/tasks/run-tests-stable.yml
114
+ - examples/convert-bosh-manifest-to-v2.md
115
+ - examples/extract-cloud-config.md
116
+ - examples/filter-transform.md
117
+ - examples/merge-command.md
93
118
  - lib/mantra.rb
94
119
  - lib/mantra/certificate.rb
95
120
  - lib/mantra/certificates/cortege.rb
96
121
  - lib/mantra/certificates/group.rb
97
122
  - lib/mantra/command.rb
98
123
  - lib/mantra/commands/find.rb
124
+ - lib/mantra/commands/merge.rb
99
125
  - lib/mantra/commands/transform.rb
100
126
  - lib/mantra/helpers/object_with_type.rb
101
127
  - lib/mantra/helpers/regexp_helper.rb
@@ -119,6 +145,7 @@ files:
119
145
  - lib/mantra/transform/extract_certificates.rb
120
146
  - lib/mantra/transform/extract_certificates_to_files.rb
121
147
  - lib/mantra/transform/extract_section.rb
148
+ - lib/mantra/transform/filter.rb
122
149
  - lib/mantra/transform/inputs/any.rb
123
150
  - lib/mantra/transform/inputs/array.rb
124
151
  - lib/mantra/transform/inputs/file.rb
@@ -126,6 +153,7 @@ files:
126
153
  - lib/mantra/transform/inputs/hash.rb
127
154
  - lib/mantra/transform/inputs/string.rb
128
155
  - lib/mantra/transform/merge.rb
156
+ - lib/mantra/transform/rename.rb
129
157
  - lib/mantra/transform/replace.rb
130
158
  - lib/mantra/transform/templatize_ip_address.rb
131
159
  - lib/mantra/transform/templatize_value.rb