Class: Terrafying::Components::Ignition
- Inherits:
-
Object
- Object
- Terrafying::Components::Ignition
- Defined in:
- lib/terrafying/components/ignition.rb
Constant Summary collapse
- UNIT_REQUIRED_KEYS =
[:name].freeze
- FILE_REQUIRED_KEYS =
i[path mode contents].freeze
Class Method Summary collapse
Class Method Details
.container_unit(name, image, options = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/terrafying/components/ignition.rb', line 12 def self.container_unit(name, image, = {}) = { volumes: [], environment_variables: [], arguments: [], require_units: [], host_networking: false, privileged: false }.merge() if [:require_units].count > 0 require_units = [:require_units].join(' ') require = " After=\#{require_units}\n Requires=\#{require_units}\n EOF\n end\n\n docker_options = []\n\n if options[:environment_variables].count > 0\n docker_options += options[:environment_variables].map do |var|\n \"-e \#{var}\"\n end\n end\n\n if options[:volumes].count > 0\n docker_options += options[:volumes].map do |volume|\n \"-v \#{volume}\"\n end\n end\n\n docker_options << '--net=host' if options[:host_networking]\n\n docker_options << '--privileged' if options[:privileged]\n\n docker_options_str = \" \\\\\\n\" + docker_options.join(\" \\\\\\n\")\n\n if options[:arguments].count > 0\n arguments = \" \\\\\\n\" + options[:arguments].join(\" \\\\\\n\")\n end\n\n {\n name: \"\#{name}.service\",\n contents: <<~EOF\n [Install]\n WantedBy=multi-user.target\n\n [Unit]\n Description=\#{name}\n \#{require}\n\n [Service]\n ExecStartPre=-/usr/bin/docker rm -f \#{name}\n ExecStart=/usr/bin/docker run --name \#{name} \#{docker_options_str} \\\n \#{image} \#{arguments}\n Restart=always\n RestartSec=30\n\n EOF\n }\nend\n" |
.generate(options = {}) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/terrafying/components/ignition.rb', line 75 def self.generate( = {}) = { keypairs: [], volumes: [], files: [], units: [], networkd_units: [], ssh_group: 'cloud', disable_update_engine: false, region: ::Generator.aws.region }.merge() unless [:units].all? { |u| UNIT_REQUIRED_KEYS.all? { |key| u.key?(key) } } raise "All units require the following keys: #{UNIT_REQUIRED_KEYS}" end unless [:units].all? { |u| u.key?(:contents) || u.key?(:dropins) || u.fetch(:enabled, true) == false || u.fetch(:mask, false) == true } raise 'All enabled unmasked units have to have contents and/or dropins' end unless [:files].all? { |f| FILE_REQUIRED_KEYS.all? { |key| f.key?(key) } } raise "All files require the following keys: #{FILE_REQUIRED_KEYS}" end [:cas] = [:keypairs].map { |kp| kp[:ca] }.compact.sort.uniq erb_path = File.join(File.dirname(__FILE__), 'templates/ignition.yaml') erb = ERB.new(IO.read(erb_path)) yaml = erb.result(OpenStruct.new().instance_eval { binding }) ::Util.to_ignition(yaml) end |