Advertisement
Guest User

Untitled

a guest
Nov 28th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. #
  2. # Blog as a Service Pipeline
  3. # - Author: Anthony Hawkins
  4. # - Purpose: Automate the testing and deployment of my personal blog
  5. #
  6.  
  7. #
  8. # Name of the pipeline used to lookup pipeline configuration in pillar
  9. #
  10. {% set pipeline_name = pillar['pipeline_name'] %}
  11.  
  12. #
  13. # Build Space - Where the work will be done
  14. #
  15. {% set build_space = pillar['pipelines'][pipeline_name]['build_env']['build_dir'] %}
  16.  
  17. #
  18. # Lookup the Repo we are building
  19. #
  20. {% set repo_source = pillar['pipelines'][pipeline_name]['git']['provider'] %}
  21. {% set repo_user = pillar['pipelines'][pipeline_name]['git']['username'] %}
  22. {% set repo_name = pillar['pipelines'][pipeline_name]['git']['repo'] %}
  23.  
  24. #
  25. # Container Info
  26. #
  27.  
  28. # Image base name to append tags to for builds
  29. {% set image_base = pillar['pipelines'][pipeline_name]['container']['image'] %}
  30.  
  31. # A volumn will be created from this dir - this allows salt to import the output
  32. # and process testing results
  33. {% set log_dir = pillar['pipelines'][pipeline_name]['container']['log_dir'] %}
  34.  
  35.  
  36. ##
  37. ## PIPELINE TASKS
  38. ##
  39.  
  40. # Set Build ID
  41. {% set build_id = "ID-" + salt.cmd.run('date +%s') %}
  42. {% set slack_key = pillar['pipelines'][pipeline_name]['slack']['api_key'] %}
  43. {% set slack_channel = pillar['pipelines'][pipeline_name]['slack']['channel'] %}
  44.  
  45. #
  46. # STEP 1 - Target the Build Box and tell the minion to clone the repo
  47. #
  48. clone:
  49. salt.state:
  50. - tgt: {{pillar['pipelines'][pipeline_name]['build_env']['minion']}}
  51. - sls:
  52. - pipelines.{{pipeline_name}}.clone
  53. - pillar:
  54. build_id: {{ build_id }}
  55. build_space: {{ build_space }}
  56. repo_user: {{ repo_user }}
  57. repo_name: {{ repo_name }}
  58. repo_source: {{ repo_source }}
  59. slack_key: {{ slack_key }}
  60. slack_channel: {{ slack_channel }}
  61.  
  62. #
  63. # STEP 2 - Ensure the Docker Image has been built
  64. #
  65. build:
  66. salt.state:
  67. - tgt: {{pillar['pipelines'][pipeline_name]['build_env']['minion']}}
  68. - sls:
  69. - pipelines.{{pipeline_name}}.build
  70. - pillar:
  71. build_id: {{ build_id }}
  72. build_space: {{ build_space }}
  73. repo_user: {{ repo_user }}
  74. repo_name: {{ repo_name }}
  75. image_base: {{ image_base }}
  76. slack_key: {{ slack_key}}
  77. slack_channel: {{ slack_channel }}
  78. - require:
  79. - clone
  80.  
  81. #
  82. # STEP 3 - Run the container in test mode this will do the following within the container
  83. # - start all processes
  84. # - start the salt minion
  85. # - run the salt-call --local state.apply blog.test.run command localy within the container
  86. # - output the result to a file bound to a volumn, this is now accesible by the next STEP
  87. #
  88. test:
  89. salt.state:
  90. - tgt: {{pillar['pipelines'][pipeline_name]['build_env']['minion']}}
  91. - sls:
  92. - pipelines.{{pipeline_name}}.test
  93. - pillar:
  94. build_id: {{ build_id }}
  95. build_space: {{ build_space }}
  96. log_dir: {{ log_dir }}
  97. repo_user: {{ repo_user }}
  98. repo_name: {{ repo_name }}
  99. image_base: {{ image_base }}
  100. slack_key: {{ slack_key }}
  101. slack_channel: {{ slack_channel }}
  102. - require:
  103. - build
  104.  
  105. #
  106. # STEP 4 - Process the Test Results, This state loads in the yaml which was outputed by the local
  107. # minion within the contaienr, from there it is run through conditionals to see what needs to be done next:
  108. # - PASS: Push the Image to Dockerhub
  109. # - FAIL: Terminate Pipeline
  110. #
  111. push:
  112. salt.state:
  113. - tgt: {{pillar['pipelines'][pipeline_name]['build_env']['minion']}}
  114. - sls:
  115. - pipelines.{{pipeline_name}}.push
  116. - pillar:
  117. build_id: {{ build_id }}
  118. build_space: {{ build_space }}
  119. log_dir: {{ log_dir }}
  120. repo_user: {{ repo_user }}
  121. repo_name: {{ repo_name }}
  122. image_base: {{ image_base }}
  123. slack_key: {{ slack_key}}
  124. slack_channel: {{ slack_channel}}
  125. - require:
  126. - test
  127.  
  128. #
  129. # STEP 5 - Deploy to Production
  130. #
  131.  
  132. {% set kuber_host = pillar['pipelines'][pipeline_name]['kubernetes']['api_endpoint']['host'] %}
  133. {% set kuber_username = pillar['pipelines'][pipeline_name]['kubernetes']['api_endpoint']['username'] %}
  134. {% set kuber_password = pillar['pipelines'][pipeline_name]['kubernetes']['api_endpoint']['password'] %}
  135. {% set kuber_namespace = pillar['pipelines'][pipeline_name]['kubernetes']['api_endpoint']['namespace'] %}
  136.  
  137. # where the kubernetes source files are located in relation to the root of the git repo
  138. {% set deployment_file = pillar['pipelines'][pipeline_name]['kubernetes']['deployment']['source'] %}
  139. {% set service_file = pillar['pipelines'][pipeline_name]['kubernetes']['service']['source'] %}
  140.  
  141. # tells the build minion where to find the kubernetes source files AFTER being cloned from step 1
  142. {% set deploy_source = build_space + "/" + repo_user + "/" + repo_name + "/" + deployment_file %}
  143. {% set service_source = build_space + "/" + repo_user + "/" + repo_name + "/" + service_file %}
  144.  
  145. # entities use which api versions
  146. {% set deployment_api = pillar['pipelines'][pipeline_name]['kubernetes']['deployment']['api_version'] %}
  147. {% set service_api = pillar['pipelines'][pipeline_name]['kubernetes']['service']['api_version'] %}
  148. deploy:
  149. salt.state:
  150. - tgt: {{pillar['pipelines'][pipeline_name]['build_env']['minion']}}
  151. - sls:
  152. - pipelines.{{pipeline_name}}.deploy
  153. - pillar:
  154. build_id: {{ build_id }}
  155. repo_user: {{ repo_user }}
  156. repo_name: {{ repo_name }}
  157. image_base: {{ image_base }}
  158. slack_key: {{ slack_key}}
  159. slack_channel: {{ slack_channel}}
  160. kubernetes:
  161. api_endpoint:
  162. host: {{ kuber_host }}
  163. username: {{ kuber_username }}
  164. password: {{ kuber_password }}
  165. namespace: {{ kuber_namespace }}
  166. deployment:
  167. api_version: {{ deployment_api }}
  168. source: {{ deploy_source }}
  169. service:
  170. api_version: {{ service_api }}
  171. source: {{ service_source }}
  172. - require:
  173. - push
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement