Advertisement
Guest User

Untitled

a guest
Apr 6th, 2017
1,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. #!/usr/bin/python
  2. #
  3. # Usage: packer-config my-template.yaml | packer build -
  4. #
  5. # Constructs a Packer JSON configuration file from the specified YAML
  6. # template file and writes it to STDOUT.
  7. #
  8. # The YAML template format adds some flexibility and readability by
  9. # adding comments and an !include directive, allowing for the
  10. # following template syntax:
  11. #
  12. # variables:
  13. # - ssh_user: ubuntu
  14. # - ssh_pass: ubuntu
  15. # - hostname: host
  16. # builders:
  17. # - !include build/ubuntu-12.04.kvm.yaml
  18. # - !include build/ubuntu-12.04.ami.yaml
  19. # provisioners:
  20. # - !include provision/ubuntu-12.04-base.yaml
  21. # - !include provision/ops-deploy.yaml
  22. # - !include provision/java7.yaml
  23. # - type: shell
  24. # environment_vars: [ 'HOST_NAME={{user `hostname`}}' ]
  25. # script: host/setup.sh
  26. #
  27. # In addition to the !include directive, the resulting YAML is
  28. # post-processed to flatten lists-of-lists in the top level sections,
  29. # allowing includes to contain multiple entries (i.e. running two shell
  30. # scripts in a common provisioner.)
  31. #
  32. # Author: jeremy@jongsma.org
  33. # License: MPLv2
  34.  
  35. import sys
  36. import os
  37. import yaml
  38. import json
  39.  
  40.  
  41. class IncludeLoader(yaml.Loader):
  42. """
  43. Custom YAML loader that adds an !include constructor for including
  44. other YAML files relative to the current file being parsed.
  45.  
  46. Example:
  47.  
  48. section: !include other/file.yaml
  49.  
  50. """
  51.  
  52. def __init__(self, stream):
  53. self._root = os.path.abspath(os.path.split(stream.name)[0])
  54. super(IncludeLoader, self).__init__(stream)
  55.  
  56. def include(self, node):
  57. path = self.construct_scalar(node)
  58. filename = path if path[0] == '/' else os.path.join(self._root, path)
  59. with open(filename, 'r') as f:
  60. return yaml.load(f, IncludeLoader)
  61.  
  62. IncludeLoader.add_constructor('!include', IncludeLoader.include)
  63.  
  64.  
  65. def parse_yaml(template):
  66. """
  67. Constructs a Packer JSON configuration file from the specified YAML
  68. template and returns it as a string.
  69.  
  70. The YAML template format adds some flexibility and readability by
  71. adding comments and an !include directive, allowing for the
  72. following template syntax:
  73.  
  74. builders:
  75. - !include build/ubuntu-12.04.kvm.yaml
  76. - !include build/ubuntu-12.04.ami.yaml
  77. provisioners:
  78. - !include provision/ubuntu-12.04-base.yaml
  79. - !include provision/ops-deploy.yaml
  80. - !include provision/java7.yaml
  81. - type: shell
  82. script: app/setup.sh
  83.  
  84. In addition to the !include directive, the resulting YAML is
  85. post-processed to flatten lists-of-lists in the top level sections,
  86. allowing includes to contain lists of multiple entries (i.e. running
  87. two shell scripts in a common provisioner.)
  88. """
  89.  
  90. with open(template, 'r') as infile:
  91. parsed = yaml.load(infile, IncludeLoader)
  92. # Flatten sections to allow including lists of steps in each include
  93. for section in ('builders', 'provisioners', 'post-processors'):
  94. if section in parsed:
  95. parsed[section] = [f for l in [e if isinstance(e, list) else [e]
  96. for e in parsed[section]] for f in l]
  97. return json.dumps(parsed)
  98.  
  99.  
  100. if __name__ == "__main__":
  101.  
  102. if len(sys.argv) < 2:
  103. sys.stderr.write("ERROR: No template specified")
  104. sys.exit(1)
  105.  
  106. if not os.path.exists(sys.argv[1]):
  107. sys.stderr.write("ERROR: Template not found: %s" % sys.argv[1])
  108. sys.exit(2)
  109.  
  110. sys.stdout.write(parse_yaml(sys.argv[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement