Class: Mist::GceHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/mist/handlers/gce.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ GceHandler

Returns a new instance of GceHandler.



20
21
22
23
# File 'lib/mist/handlers/gce.rb', line 20

def initialize(config)
  @api = Fog::Compute.new(provider: 'Google')
  @config = config
end

Instance Method Details

#create(args) ⇒ Object



25
26
27
28
29
30
31
32
33
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mist/handlers/gce.rb', line 25

def create(args)
  Mist.logger.debug "create: args=#{args}"

  hostname = Socket.gethostname

  distro = args['distro'] || @config.default_distro
  release = args['release'] || @config.default_release
  name = args['name'] || create_name

  begin
    # Map the distro & release to a source image
    Mist.logger.debug 'attempting to find source image'

    source_image = nil
    disk_size = 0

    @api.images.all.each do |image|
      next if image.deprecated

      match = image.name.match(/^#{distro}-((\d*)-(.*)-v.*$|(\d*)-v.*$)/)
      next unless match
      next unless match[2] == release or
                  match[3] == release or
                  match[4] == release

      # Found one
      Mist.logger.info "found image #{image.name} for #{distro}:#{release}"
      source_image = image.name
      disk_size = image.disk_size_gb

      break
    end

    # If we didn't find a disk, we have to stop now
    raise "could not find suitable source image for #{distro}:#{release}" \
      unless source_image

    # Create a disk, create an instance with the disk attached, set the disk
    # to be auto-deleted when the instance is destroyed
    Mist.logger.info "creating disk #{name}"
    disk = @api.disks.create(name: name,
                             size_gb: disk_size,
                             zone_name: @config.zone,
                             source_image: source_image)

    Mist.logger.info 'waiting for disk...'
    disk.wait_for { disk.ready? }

    Mist.logger.info "creating instance #{name}"

    startup_script = File.join(@config.startup_script_path, 'gce', distro)

     = { 'startup-script' => File.read(startup_script),
                 'mist-user' => @config.username,
                 'mist-key' => File.read(@config.ssh_public_key) }

    instance = @api.servers.create(name: name,
                                   disks: [disk],
                                   machine_type: @config.machine_type,
                                   zone_name: @config.zone,
                                   network: @config.network,
                                   subnet: @config.subnet,
                                   public_key_path: @config.ssh_public_key,
                                   private_key_path: @config.ssh_private_key,
                                   metadata: ,
                                   tags: ['build', 'build-host'])

    device_name = instance.disks[0]['deviceName']
    instance.set_disk_auto_delete(true, device_name)

    instance.wait_for { instance.sshable? }

    ip = if @config.use_public_ip
           instance.public_ip_address
         else
           instance.private_ip_address
         end

    # Give the instance a grace period while the startup script runs
    sleep(5)
  rescue StandardError => ex
    Mist.logger.error "Create request failed: #{ex}"
    return { status: false, server: hostname, message: "create request failed: #{ex}" }
  end

  return { status: true,
           server: hostname,
           message: 'created new instance',
           name: name,
           ip: ip,
           username: @config.username }
end

#destroy(args) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/mist/handlers/gce.rb', line 118

def destroy(args)
  Mist.logger.debug "destroy: args=#{args}"

  begin
    name = args['name']

    instance = @api.servers.get(name)
    raise "instance #{name} does not exist" unless instance

    Mist.logger.info "destroying #{name}"
    raise 'failed to destroy instance' unless instance.destroy
  rescue StandardError => ex
    Mist.logger.error "Destroy request failed: #{ex}"
    return { status: false, message: "destroy request failed: #{ex}" }
  end

  return { status: true, message: 'destroyed instance', name: name }
end