summaryrefslogtreecommitdiffstats
path: root/cloud.rb
blob: cc23a7290569ecdb954d90abec723eca124b82f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env ruby

require 'thor'
require 'json'
require 'yaml'
require 'securerandom'
require 'fileutils'

SCRIPT_DIR = File.expand_path(File.dirname(__FILE__))

module OpenShift
  module Ops
    # WARNING: we do not currently support environments with hyphens in the name
    SUPPORTED_ENVS = ['prod','stg','int','tint','kint','test']

    class GceHelper
      def self.list_hosts()
        cmd = "#{SCRIPT_DIR}/inventory/gce/gce.py --list"
        hosts = %x[#{cmd} 2>&1]

        raise "Error: failed to list hosts\n#{hosts}" unless $?.exitstatus == 0

        return JSON.parse(hosts)
      end

      def self.get_host_details(host)
        cmd = "#{SCRIPT_DIR}/inventory/gce/gce.py --host #{host}"
        details = %x[#{cmd} 2>&1]

        raise "Error: failed to get host details\n#{details}" unless $?.exitstatus == 0

        retval = JSON.parse(details)

        # Convert OpenShift specific tags to entries
        retval['gce_tags'].each do |tag|
          if tag =~ /\Ahost-type-([\w\d-]+)\z/
            retval['host-type'] = $1
          end

          if tag =~ /\Aenv-([\w\d]+)\z/
            retval['env'] = $1
          end
        end

        return retval
      end

      def self.generate_env_tag(env)
        return "env-#{env}"
      end

      def self.generate_env_tag_name(env)
        return "tag_#{generate_env_tag(env)}"
      end

      def self.generate_host_type_tag(host_type)
        return "host-type-#{host_type}"
      end

      def self.generate_host_type_tag_name(host_type)
        return "tag_#{generate_host_type_tag(host_type)}"
      end

      def self.generate_env_host_type_tag(env, host_type)
        return "env-host-type-#{env}-#{host_type}"
      end

      def self.generate_env_host_type_tag_name(env, host_type)
        return "tag_#{generate_env_host_type_tag(env, host_type)}"
      end
    end

    class LaunchHelper
      def self.expand_name(name)
        return [name] unless name =~ /^([a-zA-Z0-9\-]+)\{(\d+)-(\d+)\}$/

        # Regex matched, so grab the values
        start_num = $2
        end_num = $3

        retval = []
        start_num.upto(end_num) do |i|
          retval << "#{$1}#{i}"
        end

        return retval
      end

      def self.get_gce_host_types()
        return Dir.glob("#{SCRIPT_DIR}/playbooks/gce/*").map { |d| File.basename(d) }
      end
    end

    class AnsibleHelper
      attr_accessor :inventory, :extra_vars, :verbosity, :pipelining

      def initialize(extra_vars={}, inventory=nil)
        @extra_vars = extra_vars
        @verbosity = '-vvvv'
        @pipelining = true
      end

      def run_playbook(playbook)
        @inventory = 'inventory/hosts' if @inventory.nil?

        # This is used instead of passing in the json on the cli to avoid quoting problems
        tmpfile = Tempfile.new('extra_vars')
        tmpfile.write(@extra_vars.to_json)
        tmpfile.sync()
        tmpfile.close()

        cmds = []

        cmds << %Q[export ANSIBLE_FILTER_PLUGINS="#{Dir.pwd}/filter_plugins"]

        # We need this for launching instances, otherwise conflicting keys and what not kill it
        cmds << %q[export ANSIBLE_TRANSPORT="ssh"]
        cmds << %Q[export ANSIBLE_SSH_ARGS="-o ForwardAgent=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"]

        # We need pipelining off so that we can do sudo to enable the root account
        cmds << %Q[export ANSIBLE_SSH_PIPELINING='#{@pipelining.to_s}']

        ssh_key_arg = "--private-key=~/.ssh/mmcgrath_libra" if File.file?(ENV['HOME']+'/.ssh/mmcgrath_libra.pem')

        cmds << %Q[time -p ansible-playbook -i #{@inventory} #{@verbosity} #{playbook} #{ssh_key_arg} --extra-vars '@#{tmpfile.path}']

        cmd = cmds.join(' ; ')

        system(cmd)
        tmpfile.unlink
      end

      def merge_extra_vars_file(file)
        vars = YAML.load_file(file)
        @extra_vars.merge!(vars)
      end

      def self.for_gce()
        ah = AnsibleHelper.new()

        # GCE specific configs
        ah.extra_vars['gce_pem_file'] = "#{ENV['HOME']}/.ssh/os302gce_priv_key.pem"
        ah.extra_vars['gce_service_account_email'] = '198287808360-f457cs26hutqeosmlje1eosfeqo0krlg@developer.gserviceaccount.com'
        ah.extra_vars['gce_project_id'] = 'corded-cable-672'

        ah.inventory = 'inventory/gce/gce.py'

        return ah
      end

    end

    class GceCommand < Thor
      option :type, :required => true, :enum => LaunchHelper.get_gce_host_types,
             :desc => 'The host type of the new instances.'
      option :env, :required => true, :aliases => '-e', :enum => OpenShift::Ops::SUPPORTED_ENVS,
             :desc => 'The environment of the new instances.'
      option :count, :default => 1, :aliases => '-c', :type => :numeric,
             :desc => 'The number of instances to create'
      option :tag, :type => :array,
             :desc => 'The tag(s) to add to the new instances. Allowed characters are letters, numbers, and hyphens.'
      desc "launch", "Launches instances."
      def launch()
        # Expand all of the instance names so that we have a complete array
        names = []
        options[:count].times { names << "#{options[:env]}-#{options[:type]}-#{SecureRandom.hex(5)}" }

        ah = AnsibleHelper.for_gce()

        # GCE specific configs
        ah.extra_vars['oo_new_inst_names'] = names
        ah.extra_vars['oo_new_inst_tags'] = options[:tag]
        ah.extra_vars['oo_env'] = options[:env]

        # Add a created by tag
        ah.extra_vars['oo_new_inst_tags'] = [] if ah.extra_vars['oo_new_inst_tags'].nil?

        ah.extra_vars['oo_new_inst_tags'] << "created-by-#{ENV['USER']}"
        ah.extra_vars['oo_new_inst_tags'] << GceHelper.generate_env_tag(options[:env])
        ah.extra_vars['oo_new_inst_tags'] << GceHelper.generate_host_type_tag(options[:type])
        ah.extra_vars['oo_new_inst_tags'] << GceHelper.generate_env_host_type_tag(options[:env], options[:type])

        puts
        puts "Creating instance(s) in GCE..."
        puts
        puts " .----               Disregard this (ansible bug 6407)                ----."
        puts " V                                                                        V"


        ah.run_playbook("playbooks/gce/#{options[:type]}/launch.yml")
      end

      option :name, :required => false, :type => :string,
             :desc => 'The name of the instance to configure.'
      option :env, :required => false, :aliases => '-e', :enum => OpenShift::Ops::SUPPORTED_ENVS,
             :desc => 'The environment of the new instances.'
      option :type, :required => false, :enum => LaunchHelper.get_gce_host_types,
             :desc => 'The type of the instances to configure.'
      desc "config", 'Configures instances.'
      def config()
        ah = AnsibleHelper.for_gce()

        abort 'Error: you can\'t specify both --name and --type' unless options[:type].nil? || options[:name].nil?

        abort 'Error: you can\'t specify both --name and --env' unless options[:env].nil? || options[:name].nil?

        host_type = nil
        if options[:name]
          details = GceHelper.get_host_details(options[:name])
          ah.extra_vars['oo_host_group_exp'] = options[:name]
          ah.extra_vars['oo_env'] = details['env']
          host_type = details['host-type']
        elsif options[:type] && options[:env]
          oo_env_host_type_tag = GceHelper.generate_env_host_type_tag_name(options[:env], options[:type])
          ah.extra_vars['oo_host_group_exp'] = "groups['#{oo_env_host_type_tag}']"
          ah.extra_vars['oo_env'] = options[:env]
          host_type = options[:type]
        else
          abort 'Error: you need to specify either --name or (--type and --env)'
        end

        puts
        puts "Configuring #{options[:type]} instance(s) in GCE..."
        puts
        puts " .----               Disregard this (ansible bug 6407)                ----."
        puts " V                                                                        V"

        ah.run_playbook("playbooks/gce/#{host_type}/config.yml")
      end

      desc "list", "Lists instances."
      def list()
        hosts = GceHelper.list_hosts()

        data = {}
        hosts.each do |key,value|
          value.each { |h| (data[h] ||= []) << key }
        end

        puts
        puts "Instances"
        puts "---------"
        data.keys.sort.each { |k| puts "  #{k}" }
        puts
      end

      option :file, :required => true, :type => :string,
             :desc => 'The name of the file to copy.'
      option :dest, :required => false, :type => :string,
             :desc => 'A relative path where files are written to.'
      desc "scp_from", "scp files from an instance"
      def scp_from(*ssh_ops, host)
        if host =~ /^([\w\d_.-]+)@([\w\d-_.]+)$/
          user = $1
          host = $2
        end

        path_to_file = options['file']
        dest = options['dest']

        details = GceHelper.get_host_details(host)
        abort "\nError: Instance [#{host}] is not RUNNING\n\n" unless details['gce_status'] == 'RUNNING'

        cmd = "scp #{ssh_ops.join(' ')}"

        if user.nil?
          cmd += " "
        else
          cmd += " #{user}@"
        end
        
        if dest.nil?
          download = File.join(Dir.pwd, 'download')
          FileUtils.mkdir_p(download) unless File.exists?(download)
          cmd += "#{details['gce_public_ip']}:#{path_to_file} download/"
        else
          cmd += "#{details['gce_public_ip']}:#{path_to_file} #{File.expand_path(dest)}"
        end

        exec(cmd)
      end

      desc "ssh", "Ssh to an instance"
      def ssh(*ssh_ops, host)
        puts host
        if host =~ /^([\w\d_.-]+)@([\w\d-_.]+)/
          user = $1
          host = $2
        end
        puts "user=#{user}"
        puts "host=#{host}"

        details = GceHelper.get_host_details(host)
        abort "\nError: Instance [#{host}] is not RUNNING\n\n" unless details['gce_status'] == 'RUNNING'

        cmd = "ssh #{ssh_ops.join(' ')}"

        if user.nil?
          cmd += " "
        else
          cmd += " #{user}@"
        end

        cmd += "#{details['gce_public_ip']}"

        exec(cmd)
      end

      option :name, :required => true, :aliases => '-n', :type => :string,
             :desc => 'The name of the instance.'
      desc 'details', 'Displays details about an instance.'
      def details()
        name = options[:name]

        details = GceHelper.get_host_details(name)

        key_size = details.keys.max_by { |k| k.size }.size

        header = "Details for #{name}"
        puts
        puts header
        header.size.times { print '-' }
        puts
        details.each { |k,v| printf("%#{key_size + 2}s: %s\n", k, v) }
        puts
      end

      desc 'types', 'Displays instance types'
      def types()
        puts
        puts "Available Host Types"
        puts "--------------------"
        LaunchHelper.get_gce_host_types.each { |t| puts "  #{t}" }
        puts
      end
    end

    class CloudCommand < Thor
      desc 'gce', 'Manages Google Compute Engine assets'
      subcommand "gce", GceCommand
    end
  end
end

if __FILE__ == $0
  Dir.chdir(SCRIPT_DIR) do
    # Kick off thor
    OpenShift::Ops::CloudCommand.start(ARGV)
  end
end