2017-10-03 10:44:06 -04:00
|
|
|
module Ci
|
2017-10-03 17:24:32 -04:00
|
|
|
class ProvisionClusterService
|
2017-10-03 10:44:06 -04:00
|
|
|
def execute(cluster)
|
|
|
|
api_client =
|
|
|
|
GoogleApi::CloudPlatform::Client.new(cluster.gcp_token, nil)
|
|
|
|
|
|
|
|
begin
|
|
|
|
operation = api_client.projects_zones_clusters_create(
|
2017-10-04 03:04:45 -04:00
|
|
|
cluster.gcp_project_id,
|
|
|
|
cluster.gcp_cluster_zone,
|
|
|
|
cluster.gcp_cluster_name,
|
|
|
|
cluster.gcp_cluster_size,
|
|
|
|
machine_type: cluster.gcp_machine_type)
|
2017-10-03 10:44:06 -04:00
|
|
|
rescue Google::Apis::ServerError, Google::Apis::ClientError, Google::Apis::AuthorizationError => e
|
2017-10-04 09:14:01 -04:00
|
|
|
return cluster.make_errored!("Failed to request to CloudPlatform; #{e.message}")
|
2017-10-03 10:44:06 -04:00
|
|
|
end
|
2017-10-03 17:24:32 -04:00
|
|
|
|
2017-10-03 10:44:06 -04:00
|
|
|
unless operation.status == 'RUNNING' || operation.status == 'PENDING'
|
2017-10-04 09:14:01 -04:00
|
|
|
return cluster.make_errored!("Operation status is unexpected; #{operation.status_message}")
|
2017-10-03 10:44:06 -04:00
|
|
|
end
|
|
|
|
|
2017-10-03 17:24:32 -04:00
|
|
|
cluster.gcp_operation_id = api_client.parse_operation_id(operation.self_link)
|
2017-10-03 10:44:06 -04:00
|
|
|
|
2017-10-03 17:24:32 -04:00
|
|
|
unless cluster.gcp_operation_id
|
2017-10-04 09:14:01 -04:00
|
|
|
return cluster.make_errored!('Can not find operation_id from self_link')
|
2017-10-03 10:44:06 -04:00
|
|
|
end
|
|
|
|
|
2017-10-04 09:14:01 -04:00
|
|
|
if cluster.make_creating
|
2017-10-03 10:44:06 -04:00
|
|
|
WaitForClusterCreationWorker.perform_in(
|
|
|
|
WaitForClusterCreationWorker::INITIAL_INTERVAL, cluster.id)
|
|
|
|
else
|
2017-10-04 09:14:01 -04:00
|
|
|
return cluster.make_errored!("Failed to update cluster record; #{cluster.errors}")
|
2017-10-03 10:44:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|