2017-09-30 15:54:22 +00:00
|
|
|
class ClusterCreationWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
include DedicatedSidekiqQueue
|
|
|
|
|
|
|
|
def perform(cluster_id)
|
2017-10-02 12:58:50 +00:00
|
|
|
cluster = Gcp::Cluster.find_by_id(cluster_id)
|
2017-09-30 15:54:22 +00:00
|
|
|
|
|
|
|
unless cluster
|
|
|
|
return Rails.logger.error "Cluster object is not found; #{cluster_id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
api_client =
|
|
|
|
GoogleApi::CloudPlatform::Client.new(cluster.gcp_token, nil)
|
|
|
|
|
|
|
|
operation = api_client.projects_zones_clusters_create(
|
|
|
|
cluster.gcp_project_id,
|
|
|
|
cluster.cluster_zone,
|
|
|
|
cluster.cluster_name,
|
|
|
|
cluster.cluster_size,
|
|
|
|
machine_type: cluster.machine_type
|
|
|
|
)
|
|
|
|
|
|
|
|
if operation.is_a?(StandardError)
|
2017-10-01 11:30:32 +00:00
|
|
|
return cluster.errored!("Failed to request to CloudPlatform; #{operation.message}")
|
2017-09-30 15:54:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
unless operation.status == 'RUNNING' || operation.status == 'PENDING'
|
2017-10-01 11:30:32 +00:00
|
|
|
return cluster.errored!("Operation status is unexpected; #{operation.status_message}")
|
2017-09-30 15:54:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
operation_id = api_client.parse_operation_id(operation.self_link)
|
|
|
|
|
|
|
|
unless operation_id
|
2017-10-01 11:30:32 +00:00
|
|
|
return cluster.errored!('Can not find operation_id from self_link')
|
2017-09-30 15:54:22 +00:00
|
|
|
end
|
|
|
|
|
2017-10-01 11:30:32 +00:00
|
|
|
if cluster.creating!(operation_id)
|
2017-09-30 15:54:22 +00:00
|
|
|
WaitForClusterCreationWorker.perform_in(
|
|
|
|
WaitForClusterCreationWorker::INITIAL_INTERVAL,
|
|
|
|
cluster.id
|
|
|
|
)
|
|
|
|
else
|
2017-10-01 11:30:32 +00:00
|
|
|
return cluster.errored!("Failed to update cluster record; #{cluster.errors}")
|
2017-09-30 15:54:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|