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
|
# 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
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
Mist.logger.info "found image #{image.name} for #{distro}:#{release}"
source_image = image.name
disk_size = image.disk_size_gb
break
end
raise "could not find suitable source image for #{distro}:#{release}" \
unless source_image
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)
metadata = { '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: 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
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
|