|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Sagittarius |
| 4 | + module Orchestrator |
| 5 | + class Operator |
| 6 | + ORCHESTRATOR_LABEL_PREFIX = 'tech.code0.sagittarius.orchestrator' |
| 7 | + VOLUME_NAME_LABEL = "#{ORCHESTRATOR_LABEL_PREFIX}.volume.name".freeze |
| 8 | + VOLUME_CONTAINER_LABEL = "#{ORCHESTRATOR_LABEL_PREFIX}.volume.container".freeze |
| 9 | + |
| 10 | + class << self |
| 11 | + def ensure_self_connected! |
| 12 | + ensure_network! |
| 13 | + |
| 14 | + self_container_id = State.self_container_id |
| 15 | + return if self_container_id.nil? |
| 16 | + |
| 17 | + network.connect(State.self_container_id) |
| 18 | + end |
| 19 | + |
| 20 | + def ensure_container_up!(container) |
| 21 | + ensure_network! |
| 22 | + create_container!(container) if State[container.name].internal_container.nil? |
| 23 | + |
| 24 | + container.internal_container.refresh! |
| 25 | + |
| 26 | + unless container.internal_container.info['NetworkSettings']['Networks'].key?(network.info['Name']) |
| 27 | + network.connect(container.internal_container.id) |
| 28 | + end |
| 29 | + container.internal_container.start |
| 30 | + end |
| 31 | + |
| 32 | + def ensure_container_down!(container) |
| 33 | + destroy_container!(container) unless State[container.name].internal_container.nil? |
| 34 | + end |
| 35 | + |
| 36 | + private |
| 37 | + |
| 38 | + def ensure_network! |
| 39 | + return unless network.nil? |
| 40 | + |
| 41 | + Docker::Network.create( |
| 42 | + unique_name('code0'), |
| 43 | + 'Labels' => { ORCHESTRATOR_LABEL_PREFIX => 'network' } |
| 44 | + ) |
| 45 | + end |
| 46 | + |
| 47 | + def network |
| 48 | + Docker::Network.all(filters: JSON.dump({ 'label' => ["#{ORCHESTRATOR_LABEL_PREFIX}=network"] })).first |
| 49 | + end |
| 50 | + |
| 51 | + def ensure_volumes!(container) |
| 52 | + container.volumes.each_key do |name| |
| 53 | + next if State.volumes[container.name]&.key?(name.to_s) |
| 54 | + |
| 55 | + Docker::Volume.create( |
| 56 | + unique_name("#{container.name}_#{name}"), |
| 57 | + 'Labels' => { VOLUME_NAME_LABEL => name, VOLUME_CONTAINER_LABEL => container.name } |
| 58 | + ) |
| 59 | + end |
| 60 | + |
| 61 | + State.build_volumes! |
| 62 | + end |
| 63 | + |
| 64 | + def create_container!(container) |
| 65 | + ensure_volumes!(container) |
| 66 | + volumes = container.volumes.map do |name, path| |
| 67 | + "#{State.volumes[container.name][name.to_s].info['Name']}:#{path}" |
| 68 | + end |
| 69 | + |
| 70 | + Docker::Image.create('fromImage' => container.image) |
| 71 | + container.internal_container = Docker::Container.create( |
| 72 | + 'name' => unique_name(container.name), |
| 73 | + 'Image' => container.image, |
| 74 | + 'Labels' => { ORCHESTRATOR_LABEL_PREFIX => container.name }, |
| 75 | + 'Cmd' => container.cmd, |
| 76 | + 'Env' => container.environment_variables, |
| 77 | + 'HostConfig' => { |
| 78 | + 'Binds' => volumes, |
| 79 | + 'PortBindings' => container.ports, |
| 80 | + } |
| 81 | + ) |
| 82 | + end |
| 83 | + |
| 84 | + def destroy_container!(container) |
| 85 | + container.internal_container.stop |
| 86 | + container.internal_container.delete |
| 87 | + container.internal_container = nil |
| 88 | + end |
| 89 | + |
| 90 | + def unique_name(component) |
| 91 | + "#{component}_#{SecureRandom.hex}" |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | +end |
0 commit comments