Class: GoogleCloudPlatform

Inherits:
Object
  • Object
show all
Defined in:
lib/infrastructure/google_cloud_platform/google_cloud_platform.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_name, app_name, credentials) ⇒ GoogleCloudPlatform

Returns a new instance of GoogleCloudPlatform.



5
6
7
8
9
10
# File 'lib/infrastructure/google_cloud_platform/google_cloud_platform.rb', line 5

def initialize(project_name, app_name, credentials)
    @project_name = project_name
    @app_name = app_name
    @storage = Gcloud.storage "#{project_name}" "#{credentials}"
    @bucket = self.get_bucket
end

Class Method Details

.generate_startup_scriptObject



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
# File 'lib/infrastructure/google_cloud_platform/google_cloud_platform.rb', line 34

def self.generate_startup_script
    branch = `git rev-parse --abbrev-ref HEAD`.strip

    dist = %w{#!/bin/bash

gsutil cp gs://<app_name>/docker-compose/<primary_yaml> .
gsutil cp gs://<app_name>/docker-compose/<backup_yaml> .

# Pull Primary Containers and start them (faster service availability)
docker-compose pull -p <app_name> -f <primary_yaml>
docker-compose up -p <app_name> -f <primary_yaml> -d

# Pull Backup containers and start them
docker-compose pull -p <app_name> -f <backup_yaml>
docker-compose up -p <app_name> -f <backup_yaml> -d
    }

    dist = dist.gsub('<app_name>', @app_name)
    dist = dist.gsub('<primary_yaml>', self.get_yaml_filename('primary', true))
    dist = dist.gsub('<backup_yaml>', self.get_yaml_filename('backup', true))

    filename = "#{branch}-startup.sh"
    out_file = File.new(filename, 'w')
    out_file.puts(dist)
    out_file.close

    puts "Generated: #{filename} \n Contents: #{dist}"

    puts "Putting #{filename} on Google Cloud Storage"
    return @bucket.create_file filename "scripts/#{filename}"
end

.get_bucketObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/infrastructure/google_cloud_platform/google_cloud_platform.rb', line 23

def self.get_bucket
    buckets = @storage.buckets
    buckets.each do |bucket|
        if bucket.name == @app_name
            return bucket
        end
    end

    return @storage.create_bucket @app_name, retries: 3
end

.get_yaml_filename(duty, latest = false) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/infrastructure/google_cloud_platform/google_cloud_platform.rb', line 12

def self.get_yaml_filename(duty, latest=false)
    branch = `git rev-parse --abbrev-ref HEAD`.strip
    commit = 'latest'
    unless latest
        commit = `git rev-parse --short HEAD`.strip
    end

    return "#{@app_name}-#{branch}-#{commit}_#{duty}.yaml"
end

.make_latest_production_yaml(duty) ⇒ Object



71
72
73
74
75
76
# File 'lib/infrastructure/google_cloud_platform/google_cloud_platform.rb', line 71

def self.make_latest_production_yaml(duty)
    existing_file = @bucket.find_file "docker-compose/#{self.get_yaml_filename(duty)}"

    puts "Making #{existing_file} latest"
    existing_file.copy "docker-compose/#{self.get_yaml_filename(duty, true)}"
end

.put_production_yaml(filename) ⇒ Object



66
67
68
69
# File 'lib/infrastructure/google_cloud_platform/google_cloud_platform.rb', line 66

def self.put_production_yaml(filename)
    puts "Putting #{filename} on Google Cloud Storage"
    return @bucket.create_file filename "docker-compose/#{filename}"
end