Skip to content

Instantly share code, notes, and snippets.

@scott-kloud
Last active November 18, 2016 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scott-kloud/359ab77d17ea4b723006559edc5373e3 to your computer and use it in GitHub Desktop.
Save scott-kloud/359ab77d17ea4b723006559edc5373e3 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:batch="http://www.mulesoft.org/schema/mule/batch" xmlns:s3="http://www.mulesoft.org/schema/mule/s3" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:cloudhub="http://www.mulesoft.org/schema/mule/cloudhub" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/cloudhub http://www.mulesoft.org/schema/mule/cloudhub/current/mule-cloudhub.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/s3 http://www.mulesoft.org/schema/mule/s3/current/mule-s3.xsd
http://www.mulesoft.org/schema/mule/batch http://www.mulesoft.org/schema/mule/batch/current/mule-batch.xsd">
<custom-transformer name="customZipTransformer" class="kloud.cloudhub.logarchiver.transformers.ZipTransformer" doc:name="Java"/>
<http:request-config name="Access_Management_Config" protocol="HTTPS" host="anypoint.mulesoft.com" port="443" basePath="/accounts" doc:name="HTTP Request Configuration">
<http:raml-api-configuration location="access_management/api.raml"/>
</http:request-config>
<http:request-config name="CloudHub_Config" protocol="HTTPS" host="anypoint.mulesoft.com" port="443" basePath="/cloudhub/api" doc:name="HTTP Request Configuration">
<http:raml-api-configuration location="cloudhub/api.raml"/>
</http:request-config>
<s3:config name="Amazon_S3_Configuration" accessKey="${amazon.s3.access_key}" secretKey="${amazon.s3.secret_key}" doc:name="Amazon S3 Configuration"/>
<flow name="logArchiverFlow">
<poll doc:name="Poll">
<fixed-frequency-scheduler frequency="${polling.frequency.hours}" timeUnit="HOURS"/>
<set-payload value="#['${log.achiver.config}']" mimeType="application/json" doc:name="Read config"/>
</poll>
<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
<set-variable variableName="configCollection" value="#[payload.config]" doc:name="Set configCollection flowVar"/>
<foreach collection="#[flowVars.configCollection]" counterVariableName="configCounter" doc:name="For Each item in Config">
<set-variable variableName="config" value="#[flowVars.configCollection[configCounter-1]]" doc:name="Set config flowVar"/>
<logger message="#['Archiving log files for CloudHub application: &quot;' + flowVars.config.anypointApplication + '&quot; to Amazon S3 bucket: &quot;' + flowVars.config.amazonS3Bucket + '&quot;...']" level="INFO" doc:name="Logger"/>
<flow-ref name="archiveLogFile" doc:name="archiveLogFile"/>
</foreach>
<catch-exception-strategy doc:name="Catch Exception Strategy">
<logger level="ERROR" doc:name="Logger"/>
</catch-exception-strategy>
</flow>
<sub-flow name="archiveLogFile">
<flow-ref name="cloudhubLogin" doc:name="cloudhubLogin"/>
<flow-ref name="cloudhubDeployments" doc:name="cloudhubDeployments"/>
<foreach collection="#[flowVars.instances]" counterVariableName="instanceCounter" doc:name="For Each deployed instance">
<set-variable variableName="instanceId" value="#[flowVars.instances[flowVars.instanceCounter-1].instanceId]" doc:name="Set InstanceId flowVar"/>
<flow-ref name="cloudhubLogFiles" doc:name="cloudhubLogFiles"/>
</foreach>
</sub-flow>
<sub-flow name="cloudhubLogin">
<set-payload value="#['{ &quot;username&quot;: &quot;${anypoint.login.username}&quot;, &quot;password&quot;: &quot;${anypoint.login.password}&quot;}']" mimeType="application/json" doc:name="Set Payload"/>
<http:request config-ref="Access_Management_Config" path="/login" method="POST" doc:name="HTTP"/>
<json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.util.HashMap"/>
<set-variable variableName="access_token" value="#[payload.access_token]" doc:name="Set Access_Token FlowVar"/>
<logger level="DEBUG" doc:name="Logger"/>
</sub-flow>
<sub-flow name="cloudhubDeployments">
<set-payload value="{}" mimeType="application/json" doc:name="Set Payload"/>
<http:request config-ref="CloudHub_Config" path="/v2/applications/{domain}/deployments" method="GET" doc:name="HTTP">
<http:request-builder>
<http:uri-param paramName="domain" value="#[flowVars.config.anypointApplication]"/>
<http:header headerName="X-ANYPNT-ENV-ID" value="#[flowVars.config.anypointEnvironmentId]"/>
<http:header headerName="Authorization" value="#['Bearer ' + flowVars.access_token]"/>
</http:request-builder>
</http:request>
<json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.util.HashMap"/>
<set-variable variableName="instances" value="#[payload.data[0].instances]" doc:name="Set Instances FlowVar"/>
<logger level="DEBUG" doc:name="Logger"/>
</sub-flow>
<sub-flow name="cloudhubLogFiles">
<set-payload value="{}" mimeType="application/json" doc:name="Set Payload"/>
<http:request config-ref="CloudHub_Config" path="/v2/applications/{domain}/instances/{instanceId}/log-file" method="GET" doc:name="HTTP">
<http:request-builder>
<http:uri-param paramName="domain" value="#[flowVars.config.anypointApplication]"/>
<http:uri-param paramName="instanceId" value="#[flowVars.instanceId]"/>
<http:header headerName="X-ANYPNT-ENV-ID" value="#[flowVars.config.anypointEnvironmentId]"/>
<http:header headerName="Authorization" value="#['Bearer ' + flowVars.access_token]"/>
</http:request-builder>
</http:request>
<transformer ref="customZipTransformer" doc:name="ZIP before sending"/>
<s3:create-object config-ref="Amazon_S3_Configuration" bucketName="#[flowVars.config.amazonS3Bucket]" key="#[flowVars.config.anypointApplication + '-' + flowVars.instanceId + '-' + server.dateTime + '.zip']" doc:name="Amazon S3"/>
</sub-flow>
</mule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment