Skip to content

Commit

Permalink
devopsAsCode.Template allows to reuse existing ansible module without…
Browse files Browse the repository at this point in the history
… editing them to integrate devops as code template. The template file is attached to the CI and add to the ansbile role
  • Loading branch information
Benoit Moussaud committed Jun 18, 2020
1 parent 3483a9f commit 221e5f3
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ supervisord.*

.DS_Store
xl-deploy-9.6.0-server/
xl-deploy-9.6.1-server/
7 changes: 6 additions & 1 deletion src/main/resources/ansible_step/ansible-playbook.sh.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
export PYTHONUNBUFFERED=1
export ANSIBLE_CONFIG=ansible.cfg

echo "${devopsAsCodeTemplate}" > ${deployed.name}.j2

<#assign verbose=""/>
<#if ansibleController.debug>
<#assign verbose="-v"/>
echo "= xldeploy_playbook.yml ="
cat xldeploy_playbook.yml
cat -n xldeploy_playbook.yml

echo "= inventory = "
cat ansible_step/inventory/xldeploy_ansible_inventory
Expand All @@ -27,6 +29,9 @@ cat ansible_step/xldeploy_extravars.json
echo "= ansible.cfg = "
cat ansible.cfg

echo "= ${deployed.name}.j2 = "
cat ${deployed.name}.j2

echo "------ "
find . -ls
echo "------ "
Expand Down
34 changes: 34 additions & 0 deletions src/main/resources/planning/joiner.devopsascode.templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#
# Copyright 2020 XEBIALABS
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#


from org.apache.commons.io import IOUtils


def to_string(from_file, charset):
return IOUtils.toString(from_file.getInputStream(), charset)


def roles():
result = []
for _delta in specification.deltas:
deployed = _delta.deployedOrPrevious
if (_delta.operation == "CREATE" or _delta.operation == "MODIFY") and deployed.type == "ansible.Roles":
result.append(deployed)
return set(result)


for role in roles():
data = ""
for template in role.devopsAsCodeTemplates:
content = to_string(template.file, "UTF-8")
data = data + content
#print(data)
role.computedDevopsAsCodeTemplate = data
10 changes: 9 additions & 1 deletion src/main/resources/synthetic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<property name="devopsAsCodeUrl" required="false" default="http://localhost:4537" category="devops-as-code"/>
<property name="devopsAsCodeUsername" required="false" default="admin" category="devops-as-code"/>
<property name="devopsAsCodePassword" required="false" default="admin" category="devops-as-code" password="false"/>
<property name="xlPath" required="false" default="/usr/local/bin/xl" category="devops-as-code" description="C:\xl-deploy-9.6.0-server\xl-cli\xl.exe or /usr/local/bin/xl"/>
<property name="xlPath" required="false" default="/usr/local/bin/xl" category="devops-as-code" description="C:\xl-deploy-9.6.0-server\xl-cli\xl.exe or /usr/local/bin/xl"/>
<property name="debug" category="Advanced" kind="boolean" default="false"/>
</type>

Expand All @@ -43,9 +43,17 @@
<property name="includeDevOpsAsCodeTask" label="Include DevOpsAsCode task" kind="boolean" default="false" required="false" description="does the deployable contain a YAML definition and upload it to working directory"/>
<property name="boundConfigurationItems" hidden="false" kind="set_of_ci" as-containment="false" required="false" referenced-type="udm.BaseConfigurationItem"/>
<property name="applyRulesOnNOOP" label="apply Rules On NOOP" kind="boolean" default="false" required="true" category="Advanced"/>
<property name="devopsAsCodeTemplates" kind="set_of_ci" as-containment="true" required="false" referenced-type="devopsAsCode.Template" category="devops as code"/>
<property name="computedDevopsAsCodeTemplate" default="---" hidden="true" description="property used to transport all the templates in one property to the ansible controller, used by the `ansible.Roles.joiner.devopsascode.templates` rule"/>
</type>

<type type="devopsAsCode.Template" extends="udm.EmbeddedDeployedArtifact"
container-type="ansible.Roles" deployable-type="devopsAsCode.TemplateFile">
<generate-deployable type="devopsAsCode.TemplateFile" extends="udm.EmbeddedDeployableArtifact"/>
</type>

<type-modification type="ansible.RolesSpec">
<property name="devopsAsCodeTemplates" kind="set_of_ci" as-containment="true" required="false" referenced-type="devopsAsCode.TemplateFile" category="devops as code"/>
<property name="boundTemplates" hidden="false" required="false"
candidate-values-filter="filterByProvisioningPackage" description="The set of CI templates."
as-containment="false"
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/xl-rules.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<parameter name="galaxyRoles" type="list_of_string" description="Galaxy Roles"/>
<parameter name="variables" type="map_string_string" description="variables..."/>
<parameter name="variableFile" type="map_string_string" description="variableFile..."/>
<parameter name="devopsAsCodeTemplate" type="string" description="the devops as code template to process on the host side by ansible"/>
</parameters>
<steps>
<os-script>
Expand All @@ -71,6 +72,7 @@
<ansibleController expression="true">deployed.container.ansibleController</ansibleController>
<deployedApplication expression="true">deployedApplication</deployedApplication>
<become>yes</become>
<devopsAsCodeTemplate expression="true">deployed.computedDevopsAsCodeTemplate</devopsAsCodeTemplate>
</freemarker-context>
<classpath-resources>
<value>ansible_step/inventory/xldeploy_ansible_inventory.ftl</value>
Expand All @@ -86,6 +88,10 @@
<planning-script-path>planning/ssh-keys-remote-loader.py</planning-script-path>
</rule>

<rule name="ansible.Roles.joiner.devopsascode.templates" scope="pre-plan">
<planning-script-path>planning/joiner.devopsascode.templates.py</planning-script-path>
</rule>

<rule name="ansible.Roles.apply" scope="deployed">
<conditions>
<type>ansible.Roles</type>
Expand All @@ -100,6 +106,7 @@
<galaxyRoles expression="true">deployed.galaxyRoles</galaxyRoles>
<variables expression="true">deployed.variables</variables>
<variableFile expression="true">deployed.variableFile</variableFile>
<devopsAsCodeTemplate expression="true">context.getAttribute("{0}_computedDevopsAsCodeTemplate".format(deployed.id))</devopsAsCodeTemplate>
</ansible-playbook>
</steps>
</rule>
Expand Down
14 changes: 14 additions & 0 deletions src/main/resources/xldeploy_playbook.yml.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,17 @@
<#list roles as role>
- { role: ${role} }
</#list>
tasks:
<#list deployed.devopsAsCodeTemplates as template>
- name: Generate the Devops-As-Code
template:
src: ${deployed.name}.j2
dest: /tmp/digital.ai_xldeploy.yaml
mode: 0755
- name: Fetch the Devops-As-Code yaml file on the master
fetch:
src: /tmp/digital.ai_xldeploy.yaml
dest: "{{ devops_as_code_directory }}"
flat: no
</#list>

0 comments on commit 221e5f3

Please sign in to comment.