Class: GoogleCloudPlatform

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

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
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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

    # assume we already logged in (GCE instance or GCloud SDK)
    puts project_name

    @storage = Gcloud.storage project_name, credentials
    buckets = @storage.buckets
    buckets.each do |bucket|
        if bucket.name == @app_name
            @bucket = bucket
        end
    end

    unless @bucket
        @bucket = @storage.create_bucket @app_name, retries: 3
    end
end

Instance Method Details

#generate_startup_scriptObject



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
65
66
67
# File 'lib/infrastructure/google_cloud_platform/google_cloud_platform.rb', line 35

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

    dist = <<-EOF
#!/bin/bash

gsutil cp gs://<bucket_name>/docker-compose/<primary_yaml> .
gsutil cp gs://<bucket_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
EOF

    dist = dist.gsub('<bucket_name>', @app_name)
    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"
    @bucket.create_file filename, "scripts/#{filename}"
end

#get_yaml_filename(duty, latest = false) ⇒ Object



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

def 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

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

#make_latest_production_yaml(duty) ⇒ Object



75
76
77
78
79
80
# File 'lib/infrastructure/google_cloud_platform/google_cloud_platform.rb', line 75

def 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



69
70
71
72
73
# File 'lib/infrastructure/google_cloud_platform/google_cloud_platform.rb', line 69

def put_production_yaml(filename)
    puts "Putting #{filename} on Google Cloud Storage"

    @bucket.create_file filename, "docker-compose/#{filename}"
end