Class: GTBase

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

Direct Known Subclasses

GTCluster

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ GTBase

Returns a new instance of GTBase.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/app.rb', line 162

def initialize(args)
  @args = args
  if ENV['GT_ENV_SETTINGS']
      @env_settings = YAML::load(File.open(File.expand_path(ENV['GT_ENV_CONFIG'])))
  else
      @env_settings = YAML::load(File.open(File.expand_path("#{ENV['HOME']}/.gaptool/env.yml")))
  end
  if ENV['GT_USER_SETTINGS']
      @user_settings = YAML::load(File.open(File.expand_path(ENV['GT_USER_CONFIG'])))
  else
      @user_settings = YAML::load(File.open(File.expand_path("#{ENV['HOME']}/.gaptool/user.yml")))
  end
  chef_extra = {
    'rails_env' => @args[:environment],
    'server_names' => getCluster()
  }
  @chefsettings = @env_settings['applications'][@args[:app]][@args[:environment]]
  @chefsettings.merge!(@args)
  @chefsettings.merge!(chef_extra)
end

Instance Method Details

#getClusterObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/app.rb', line 81

def getCluster
  hosts = Array.new
  if isCluster?
    @env_settings['applications'][@args[:app]][@args[:environment]]['cluster'].each do |node|
      hosts << "#{@args[:app]}-#{@args[:environment]}-#{node}.#{@env_settings['domain']}"
    end
  else
    hosts << "#{@args[:app]}-#{@args[:environment]}.#{@env_settings['domain']}"
  end
  return hosts
end

#isCluster?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# File 'lib/app.rb', line 74

def isCluster?
  if @env_settings['applications'][@args[:app]][@args[:environment]]['cluster']
    return true
  else
    return false
  end
end

#putkey(host) ⇒ Object



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

def putkey(host)
  breakkey = @user_settings['mykey'].gsub(/\n/,'###')
  run = [
    "rm ~admin/.ssh/key 2> /dev/null",
    "echo '#{breakkey}' > /tmp/key",
    "cat /tmp/key|perl -pe 's/###/\\n$1/g' > ~admin/.ssh/key",
    "echo \"IdentitiesOnly yes\\nHost *\\n  StrictHostKeyChecking no\\n  IdentityFile ~/.ssh/key\" > ~admin/.ssh/config",
    "chmod 600 ~admin/.ssh/key",
    "chmod 600 ~admin/.ssh/config",
    "sudo rm ~#{@env_settings['user']}/.ssh/key",
    "sudo cp ~admin/.ssh/key ~#{@env_settings['user']}/.ssh/",
    "sudo cp ~admin/.ssh/config ~#{@env_settings['user']}/.ssh/",
    "sudo chown #{@env_settings['user']}:#{@env_settings['user']} ~#{@env_settings['user']}/.ssh/key",
    "sudo chown #{@env_settings['user']}:#{@env_settings['user']} ~#{@env_settings['user']}/.ssh/config",
    "sudo chmod 600 ~#{@env_settings['user']}/.ssh/key",
    "sudo chmod 600 ~#{@env_settings['user']}/.ssh/config",
    "rm /tmp/key"
  ]
  sshcmd(host, run, :quiet => true)
end

#singleHostObject



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/app.rb', line 151

def singleHost
  if @args[:node] == 'solo' && isCluster?
    puts "The environment you're accessing is a cluster.\nYou've selected an action that acts only on a single node, but have not specified a node with --node/-n\nAborting."
    exit 100
  end
  if isCluster?
    return "#{@args[:app]}-#{@args[:environment]}-#{@args[:node]}.#{@env_settings['domain']}"
  else
    return "#{@args[:app]}-#{@args[:environment]}.#{@env_settings['domain']}"
  end
end

#sshcmd(host, commands, options = {}) ⇒ Object



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/app.rb', line 92

def sshcmd(host, commands, options = {})
  if options[:user]
    user = options[:user]
  else
    user = 'admin'
  end
  if options[:key]
    key = options[:key]
  else
    key = @user_settings['mykey']
  end
  Net::SSH.start(host, user, :key_data => [key], :config => false, :keys_only => true, :paranoid => false) do |ssh|
    commands.each do |command|
      if !options[:quiet]
        puts command.color(:cyan)
      end
      ssh.exec! command do
        |ch, stream, line|
        if !options[:quiet]
          puts "#{host.color(:yellow)}:#{line}"
        end
      end
    end
  end
end

#sshReachable?Boolean

Returns:

  • (Boolean)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/app.rb', line 137

def sshReachable?
  hosts = getCluster()
  hosts.each do |host|
    begin
      puts "Checking SSH to: #{host}"
      sshcmd(host, ["exit 0"], :quiet => true)
    rescue
      puts "ERROR: Could not ssh to #{host}\nEither your connection is failing or AWS is having routing issues.\nAborting."
      exit 255
      return false
    end
  end
  return true
end