Top Level Namespace

Defined Under Namespace

Modules: OpsWorks, OpzWorks Classes: String

Constant Summary collapse

SSH_PREFIX =
'# --- OpzWorks ---'
SSH_POSTFIX =
'# --- End of OpzWorks ---'

Instance Method Summary collapse

Instance Method Details

#es_enable_allocation(ip, type) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/opzworks/commands/include/elastic.rb', line 56

def es_enable_allocation(ip, type)
  puts "Cluster routing.allocation is being set to #{type}".foreground(:blue)
  conn = Faraday.new(url: "http://#{ip}:9200") do |f|
    f.adapter :net_http
  end

  count = 0
  loop do
    begin
      conn.put do |req|
        req.url '/_cluster/settings'
        req.body = "{\"transient\": {\"cluster.routing.allocation.enable\": \"#{type}\"}}"
        req.options[:timeout]       = 5
        req.options[:open_timeout]  = 2
      end
      break
    rescue StandardError => e
      puts 'Caught exception while trying to change allocation state: '.foreground(:yellow) + "#{e}".foreground(:red) + ', looping around...'.foreground(:yellow) if count == 0
      count += 1
      sleep 1
    end
  end
end

#es_get_input(input, data = {}, *cmd) ⇒ Object



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
# File 'lib/opzworks/commands/include/elastic.rb', line 9

def es_get_input(input, data = {}, *cmd)
  match = {}
  count = 0

  data[:stacks].each do |stack|
    next unless stack[:name].chomp =~ /#{input}/
    count = count += 1
    match = stack.to_hash
  end

  # break?
  if count < 1
    puts 'No matching stacks found for input '.foreground(:yellow) + input.foreground(:green) + ', skipping.'.foreground(:yellow)
    @get_data_failure = true
  elsif count > 1
    puts 'Found more than one stack matching input '.foreground(:yellow) + input.foreground(:green) + ', skipping.'.foreground(:yellow)
    @get_data_failure = true
  else
    puts 'Operating on stack '.foreground(:blue) + "#{match[:name]}".foreground(:green)
    layers = @client.describe_layers(stack_id: match[:stack_id])
    layers[:layers].each { |layer| printf("%-30s %-50s\n", layer[:name], layer[:layer_id]) }

    STDOUT.print 'Specify a layer: '.foreground(:blue)
    layer = STDIN.gets.chomp

    unless cmd.include? 'start'
      STDOUT.print 'Disable shard allocation before starting? (true/false, default is true): '.foreground(:blue)
      disable_allocation = STDIN.gets.chomp
      case disable_allocation
      when 'false'
        @disable_shard_allocation = false
      else
        @disable_shard_allocation = true
      end
    end

    options = {}
    if layer == ''
      puts 'Must specify a layer.'.foreground(:red)
      abort
    else
      options[:layer_id] = layer
    end
    opsworks_list_ips(options)
  end
end

#es_service(command, ips = []) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/opzworks/commands/include/elastic.rb', line 80

def es_service(command, ips = [])
  puts "Operating on ES with command #{command}".foreground(:yellow)
  user = ENV['USER']

  Net::SSH::Multi.start do |session|
    ips.each do |ip|
      session.use "#{user}@#{ip}"
    end

    session.exec "sudo service elasticsearch #{command}"
    session.loop
  end
end

#es_wait_for_status(ip, color) ⇒ Object



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
# File 'lib/opzworks/commands/include/elastic.rb', line 94

def es_wait_for_status(ip, color)
  puts 'Waiting for cluster to go '.foreground(:blue) + "#{color}".foreground(:"#{color}")
  conn = Faraday.new(url: "http://#{ip}:9200") do |f|
    f.adapter :net_http
  end

  count = 0
  rescue_count = 0
  loop do
    begin
      response = conn.get do |req|
        req.url '/_cluster/health'
        req.options[:timeout]       = 5
        req.options[:open_timeout]  = 2
      end
      json = JSON.parse response.body
    rescue StandardError => e
      puts 'Caught exception while trying to check cluster status: '.foreground(:yellow) + "#{e}".foreground(:red) + ', looping around...'.foreground(:yellow) if rescue_count == 0
      rescue_count += 1
      printf '.'
      sleep 1
    else
      case json['status']
      when color
        puts "\nCluster is now ".foreground(:blue) + "#{color}".foreground(:"#{color}")
        break
      when 'green'
        puts "\nCluster is green, proceeding without waiting for requested status of #{color}".foreground(:green)
        break
      else
        count += 1
        if count == 10
          puts "\nStill waiting, cluster is currently ".foreground(:blue) + "#{json['status']}".foreground(:"#{json['status']}")
          count = 0
        end
        printf '.'
        sleep 1
      end
    end
  end
end

#manage_berks_reposObject



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
# File 'lib/opzworks/commands/include/manage_berks_repos.rb', line 1

def manage_berks_repos
  config = OpzWorks.config
  @target_path = File.expand_path(config.berks_repository_path + "/opsworks-#{@project}", File.dirname(__FILE__))

  if !File.directory?(@target_path)
    if config.berks_github_org.nil?
      puts "#{@target_path} does not exist, and 'berks-github-org' is not set in ~/.aws/config, skipping.".foreground(:yellow)
      @berks_repo_failure = true
    else
      repo = "[email protected]:#{config.berks_github_org}/opsworks-#{@project}.git"
      puts "#{@target_path} does not exist!".foreground(:red)
      puts 'Attempting git clone of '.foreground(:blue) + repo.foreground(:green)
      run_local <<-BASH
        cd #{config.berks_repository_path}
        git clone #{repo}
      BASH
    end
  else
    puts "Git pull from #{@target_path}, branch: ".foreground(:blue) + @branch.foreground(:green)
    run_local <<-BASH
      cd #{@target_path}
      git checkout #{@branch} && git pull origin #{@branch}
    BASH
  end
end

#opsworks_list_ips(options = {}) ⇒ Object



1
2
3
4
5
6
7
# File 'lib/opzworks/commands/include/elastic.rb', line 1

def opsworks_list_ips(options = {})
  response = @client.describe_instances options
  @ip_addrs = []
  response[:instances].each { |instance| @ip_addrs << instance.private_ip if instance[:status] == 'online' }
rescue StandardError => e
  abort "Exception raised: #{e}".foreground(:red)
end

#populate_stack(input, data = {}) ⇒ Object



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
# File 'lib/opzworks/commands/include/populate_stack.rb', line 1

def populate_stack(input, data = {})
  # loops over inputs
  match = {}
  count = 0

  data[:stacks].each do |stack|
    next unless stack[:name].chomp =~ /#{input}/
    count = count += 1
    match = stack.to_hash
  end

  # break?
  if count < 1
    puts 'No matching stacks found for input '.foreground(:yellow) + input.foreground(:green) + ', skipping.'.foreground(:yellow)
    @populate_stack_failure = true
  elsif count > 1
    puts 'Found more than one stack matching input '.foreground(:yellow) + input.foreground(:green) + ', skipping.'.foreground(:yellow)
    @populate_stack_failure = true
  else
    @stack_json = match[:custom_json]
    @project    = match[:name].split('::').first
    @s3_path    = match[:name].gsub('::', '-')
    @stack_id   = match[:stack_id]
    @branch     = (match[:name].split('::')[1] + '-' + match[:name].split('::')[2]).gsub('::', '-')

    hash = {
      'PROJECT:'  => @project,
      'STACK ID:' => @stack_id,
      'S3 PATH:'  => @s3_path,
      'BRANCH:'   => @branch
    }
    puts "\n"
    hash.each { |k, v| printf("%-25s %-25s\n", k.foreground(:green), v.foreground(:red)) }
  end
end

#run_local(cmd) ⇒ Object

wrap run_locally so we can catch failures



2
3
4
5
6
7
8
9
# File 'lib/opzworks/commands/include/run_local.rb', line 2

def run_local(cmd)
  require 'English'

  system cmd
  return unless $CHILD_STATUS.exitstatus != 0
  puts 'exit code: ' + $CHILD_STATUS.exitstatus.to_s
  exit
end