summaryrefslogtreecommitdiff
path: root/app/services/ci/provision_cluster_service.rb
blob: 52d80b018134698374e4a732c6ce4e4c454807ba (plain)
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
module Ci
  class ProvisionClusterService
    def execute(cluster)
      api_client =
        GoogleApi::CloudPlatform::Client.new(cluster.gcp_token, nil)

      begin
        operation = api_client.projects_zones_clusters_create(
          cluster.gcp_project_id,
          cluster.gcp_cluster_zone,
          cluster.gcp_cluster_name,
          cluster.gcp_cluster_size,
          machine_type: cluster.gcp_machine_type)
      rescue Google::Apis::ServerError, Google::Apis::ClientError, Google::Apis::AuthorizationError => e
        return cluster.make_errored!("Failed to request to CloudPlatform; #{e.message}")
      end

      unless operation.status == 'RUNNING' || operation.status == 'PENDING'
        return cluster.make_errored!("Operation status is unexpected; #{operation.status_message}")
      end

      cluster.gcp_operation_id = api_client.parse_operation_id(operation.self_link)

      unless cluster.gcp_operation_id
        return cluster.make_errored!('Can not find operation_id from self_link')
      end

      if cluster.make_creating
        WaitForClusterCreationWorker.perform_in(
          WaitForClusterCreationWorker::INITIAL_INTERVAL, cluster.id)
      else
        return cluster.make_errored!("Failed to update cluster record; #{cluster.errors}")
      end
    end
  end
end