<commit_before>require "yaml"

module Bosh::Gen::Models
  class DeploymentManifest
    attr_reader :manifest
    
    def initialize(name, director_uuid, release_properties, cloud_properties)
      @manifest = {}
      @cloud_properties = cloud_properties
      @security_groups = ["default"]
      @stemcell_version = "0.5.1"
      @stemcell = { "name" => "bosh-stemcell", "version" => @stemcell_version }
      @persistent_disk = cloud_properties.delete(:persistent_disk)
      
      manifest["name"] = name
      manifest["director_uuid"] = director_uuid
      manifest["release"] = release_properties.dup
      manifest["compilation"] = {
        "workers" => 10,
        "network" => "default",
        "cloud_properties" => cloud_properties.dup
      }
      manifest["update"] = {
        "canaries" => 1,
        "canary_watch_time" => 30000,
        "update_watch_time" => 30000,
        "max_in_flight" => 4,
        "max_errors" => 1
      }
      manifest["networks"] = [
        {
          "name" => "default",
          "type" => "dynamic",
          "cloud_properties" => { "security_groups" => @security_groups.dup }
        },
        {
          "name" => "vip_network",
          "type" => "vip",
          "cloud_properties" => { "security_groups" => @security_groups.dup }
        }
      ]
      manifest["resource_pools"] = [
        {
          "name" => "common",
          "network" => "default",
          "size" => 0,
          "stemcell" => @stemcell,
          "cloud_properties" => cloud_properties.dup
        }
      ]
      manifest["resource_pools"].first["persistent_disk"] = @persistent_disk if @persistent_disk
      manifest["jobs"] = []
      manifest["properties"] = {}
    end
    
    # Each item of +jobs+ is a hash. 
    # The minimum hash is:
    # { "name" => "jobname" }
    # This is the equivalent to:
    # { "name" => "jobname", "template" => "jobname", "instances" => 1}
    #
    # A +jobs+ item can also include a +"static_ips" item, which is an array of strings:
    # { "name" => "jobname", "static_ips" => ['192.168.127.12', '172.16.17.32']}
    def jobs=(jobs)
      total_instances = 0
      manifest["jobs"] = []
      jobs.each do |job|
        manifest_job = {
          "name" => job["name"],
          "template" => job["template"] || job["name"],
          "instances" => job["instances"] || 1,
          "resource_pool" => "common",
          "networks" => [
            {
              "name" => "default",
              "default" => %w[dns gateway]
            }
          ]
        }
        if job["static_ips"]
          manifest_job["networks"] << {
            "name" => "vip_network",
            "static_ips" => job["static_ips"]
          }
        end
        manifest["jobs"] << manifest_job
      end
      manifest["resource_pools"].first["size"] = manifest["jobs"].inject(0) {|total, job| total + job["instances"]}
    end
    
    def to_yaml
      manifest.to_yaml
    end
  end
end<commit_msg>when wanting a persistent disk, also add property to job<commit_after>require "yaml"

module Bosh::Gen::Models
  class DeploymentManifest
    attr_reader :manifest
    
    def initialize(name, director_uuid, release_properties, cloud_properties)
      @manifest = {}
      @cloud_properties = cloud_properties
      @security_groups = ["default"]
      @stemcell_version = "0.5.1"
      @stemcell = { "name" => "bosh-stemcell", "version" => @stemcell_version }
      @persistent_disk = cloud_properties.delete(:persistent_disk)
      
      manifest["name"] = name
      manifest["director_uuid"] = director_uuid
      manifest["release"] = release_properties.dup
      manifest["compilation"] = {
        "workers" => 10,
        "network" => "default",
        "cloud_properties" => cloud_properties.dup
      }
      manifest["update"] = {
        "canaries" => 1,
        "canary_watch_time" => 30000,
        "update_watch_time" => 30000,
        "max_in_flight" => 4,
        "max_errors" => 1
      }
      manifest["networks"] = [
        {
          "name" => "default",
          "type" => "dynamic",
          "cloud_properties" => { "security_groups" => @security_groups.dup }
        },
        {
          "name" => "vip_network",
          "type" => "vip",
          "cloud_properties" => { "security_groups" => @security_groups.dup }
        }
      ]
      manifest["resource_pools"] = [
        {
          "name" => "common",
          "network" => "default",
          "size" => 0,
          "stemcell" => @stemcell,
          "cloud_properties" => cloud_properties.dup
        }
      ]
      manifest["resource_pools"].first["persistent_disk"] = @persistent_disk if @persistent_disk
      manifest["jobs"] = []
      manifest["properties"] = {}
    end
    
    # Each item of +jobs+ is a hash. 
    # The minimum hash is:
    # { "name" => "jobname" }
    # This is the equivalent to:
    # { "name" => "jobname", "template" => "jobname", "instances" => 1}
    #
    # A +jobs+ item can also include a +"static_ips" item, which is an array of strings:
    # { "name" => "jobname", "static_ips" => ['1.2.3.4', '9.8.7.6']}
    def jobs=(jobs)
      total_instances = 0
      manifest["jobs"] = []
      jobs.each do |job|
        manifest_job = {
          "name" => job["name"],
          "template" => job["template"] || job["name"],
          "instances" => job["instances"] || 1,
          "resource_pool" => "common",
          "networks" => [
            {
              "name" => "default",
              "default" => %w[dns gateway]
            }
          ]
        }
        if job["static_ips"]
          manifest_job["networks"] << {
            "name" => "vip_network",
            "static_ips" => job["static_ips"]
          }
        end
        manifest_job["persistent_disk"] = @persistent_disk if @persistent_disk
        manifest["jobs"] << manifest_job
      end
      manifest["resource_pools"].first["size"] = manifest["jobs"].inject(0) {|total, job| total + job["instances"]}
    end
    
    def to_yaml
      manifest.to_yaml
    end
  end
end
{'fraction_non_alphanumeric': 0.15683690280065898, 'fraction_numerical': 0.012191103789126854, 'mean_word_length': 2.10537084398977, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7577876', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1855, 'n_words': 366}