class GoogleCloudCompute

Public Class Methods

new(project_name, app_name, credentials_path) click to toggle source
# File lib/infrastructure/google_cloud_platform/google_cloud_compute.rb, line 3
def initialize(project_name, app_name, credentials_path)
    @project_name = project_name
    @app_name = app_name

    @client = Google::APIClient.new(
       :application_name => 'docker-publish',
       :application_version => '0.0.2'
    )

    json_key = JSON.parse(File.read(credentials_path))
    gauth = {
        token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
        audience: 'https://accounts.google.com/o/oauth2/token',
        scope: 'https://www.googleapis.com/auth/compute',
        issuer: json_key['client_email'],
        signing_key: OpenSSL::PKey::RSA.new(json_key['private_key'])
    }
    @client.authorization = Signet::OAuth2::Client.new(gauth)
    @client.authorization.fetch_access_token!

    @compute = @client.discovered_api('compute')

    @replica_pool = GoogleCloudReplicaPool.new(project_name, app_name, credentials_path)
end

Public Instance Methods

create_instance_template(machine_type, startup_script_uri) click to toggle source
# File lib/infrastructure/google_cloud_platform/google_cloud_compute.rb, line 28
def create_instance_template(machine_type, startup_script_uri)
    puts 'Creating Google Cloud Compute instance template'

    dist_file_path = File.join(File.dirname(__FILE__), 'instance-template.json.dist')
    dist_request_json = File.read(dist_file_path)

    dist_request_json = dist_request_json.gsub('<project_name>', @project_name)
    dist_request_json = dist_request_json.gsub('<app_name>', @app_name)
    dist_request_json = dist_request_json.gsub('<startup_script_uri>', startup_script_uri)
    dist_request_json = dist_request_json.gsub('<machine_type>', machine_type)

    result = @client.execute(
        :api_method => @compute.instance_templates.insert,
        :parameters => {:project => @project_name},
        :body_object => JSON.parse(dist_request_json)
    )

    puts result.response.body

    # wait for instance template to be created
    while result.data['status'] != 'DONE' and result.data['progress'] < 100
        puts "#{result.data['status']} - #{result.data['progress']}%"
        result = @client.execute(
            :uri => result.data['selfLink']
        )
        sleep 0.5
    end
    puts "#{result.data['status']} - #{result.data['progress']}%"

    result.data['targetLink']
end
replica_pool() click to toggle source
# File lib/infrastructure/google_cloud_platform/google_cloud_compute.rb, line 60
def replica_pool
    @replica_pool
end