20171013104327_migrate_gcp_clusters_to_new_clusters_architectures.rb 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 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
class MigrateGcpClustersToNewClustersArchitectures < ActiveRecord::Migration
  DOWNTIME = false

  class GcpCluster < ActiveRecord::Base
    self.table_name = 'gcp_clusters'

    belongs_to :project, class_name: 'Project'

    include EachBatch
  end

  class Cluster < ActiveRecord::Base
    self.table_name = 'clusters'

    has_many :cluster_projects, class_name: 'ClustersProject'
    has_many :projects, through: :cluster_projects, class_name: 'Project'
    has_one :provider_gcp, class_name: 'ProvidersGcp'
    has_one :platform_kubernetes, class_name: 'PlatformsKubernetes'

    accepts_nested_attributes_for :provider_gcp
    accepts_nested_attributes_for :platform_kubernetes

    enum platform_type: {
      kubernetes: 1
    }

    enum provider_type: {
      user: 0,
      gcp: 1
    }
  end

  class Project < ActiveRecord::Base
    self.table_name = 'projects'

    has_one :cluster_project, class_name: 'ClustersProject'
    has_one :cluster, through: :cluster_project, class_name: 'Cluster'
  end

  class ClustersProject < ActiveRecord::Base
    self.table_name = 'cluster_projects'

    belongs_to :cluster, class_name: 'Cluster'
    belongs_to :project, class_name: 'Project'
  end

  class ProvidersGcp < ActiveRecord::Base
    self.table_name = 'cluster_providers_gcp'
  end

  class PlatformsKubernetes < ActiveRecord::Base
    self.table_name = 'cluster_platforms_kubernetes'
  end

  def up
    GcpCluster.all.find_each(batch_size: 1) do |gcp_cluster|
      Cluster.create(
        enabled: gcp_cluster.enabled,
        user_id: gcp_cluster.user_id,
        name: gcp_cluster.gcp_cluster_name,
        provider_type: Cluster.provider_types[:gcp],
        platform_type: Cluster.platform_types[:kubernetes],
        projects: [gcp_cluster.project],
        provider_gcp_attributes: {
          status: gcp_cluster.status,
          status_reason: gcp_cluster.status_reason,
          gcp_project_id: gcp_cluster.gcp_project_id,
          zone: gcp_cluster.gcp_cluster_zone,
          num_nodes: gcp_cluster.gcp_cluster_size,
          machine_type: gcp_cluster.gcp_machine_type,
          operation_id: gcp_cluster.gcp_operation_id,
          endpoint: gcp_cluster.endpoint,
          encrypted_access_token: gcp_cluster.encrypted_gcp_token,
S
Shinya Maeda 已提交
74
          encrypted_access_token_iv: gcp_cluster.encrypted_gcp_token_iv
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        },
        platform_kubernetes_attributes: {
          api_url: api_url(gcp_cluster.endpoint),
          ca_cert: gcp_cluster.ca_cert,
          namespace: gcp_cluster.project_namespace,
          username: gcp_cluster.username,
          encrypted_password: gcp_cluster.encrypted_password,
          encrypted_password_iv: gcp_cluster.encrypted_password_iv,
          encrypted_token: gcp_cluster.encrypted_kubernetes_token,
          encrypted_token_iv: gcp_cluster.encrypted_kubernetes_token_iv
        } )
    end
  end

  def down
    execute('DELETE FROM clusters')
  end

  private

  def api_url(endpoint)
    endpoint ? 'https://' + endpoint : nil
  end
end