Class: GTBase
- Inherits:
-
Object
- Object
- GTBase
- Defined in:
- lib/app.rb
Direct Known Subclasses
Instance Method Summary collapse
- #cgen ⇒ Object
- #getCluster ⇒ Object
-
#initialize(args) ⇒ GTBase
constructor
A new instance of GTBase.
- #isCluster? ⇒ Boolean
- #putkey(host) ⇒ Object
- #recipeRun(host, run_list, settings = {}) ⇒ Object
- #singleHost ⇒ Object
- #sshcmd(host, commands, options = {}) ⇒ Object
- #sshReachable? ⇒ Boolean
Constructor Details
#initialize(args) ⇒ GTBase
Returns a new instance of GTBase.
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 |
# File 'lib/app.rb', line 150 def initialize(args) @args = args if ENV['GT_ENV_SETTINGS'] @env_settings = YAML::load(File.open(File.(ENV['GT_ENV_CONFIG']))) else @env_settings = YAML::load(File.open(File.("#{ENV['HOME']}/.gaptool-ma/env.yml"))) end if ENV['GT_USER_SETTINGS'] @user_settings = YAML::load(File.open(File.(ENV['GT_USER_CONFIG']))) else @user_settings = YAML::load(File.open(File.("#{ENV['HOME']}/.gaptool-ma/user.yml"))) end chef_extra = { 'rails_env' => @args[:environment], } @chefsettings = @args @chefsettings.merge!(chef_extra) if @args[:zone].to_s == '' zone = @env_settings['default_zone'] else zone = @args[:zone] end az = zone.chop AWS.config(:access_key_id => @user_settings['aws_id'], :secret_access_key => @user_settings['aws_secret'], :ec2_endpoint => "ec2.#{az}.amazonaws.com") @ec2 = AWS::EC2.new def cgen c = Array.new = @ec2.instances.inject({}) { |m, i| m[i.id] = i..to_h; m } .keys.each do |key| if [key]['gaptool'] != nil = eval([key]['gaptool']) hostname = "#{gaptags[:role]}-#{gaptags[:environment]}-#{gaptags[:number]}.#{@env_settings['domain']}" c += [{ :hostname => hostname, :recipe => [:recipe], :deploy => [:deploy], :number => [:number], :role => [:role], :environment => [:environment], :apps => eval([:apps]) }] end end return c end if File.exists?("#{ENV['HOME']}/.gaptool-ma/aws.yml") $c = YAML::load(File.open(File.("#{ENV['HOME']}/.gaptool-ma/aws.yml"))) else $c = cgen() end cwrite = fork do File.open(File.("#{ENV['HOME']}/.gaptool-ma/aws.yml"), 'w') {|f| f.write(cgen().to_yaml)} end Process.detach(cwrite) end |
Instance Method Details
#cgen ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/app.rb', line 175 def cgen c = Array.new = @ec2.instances.inject({}) { |m, i| m[i.id] = i..to_h; m } .keys.each do |key| if [key]['gaptool'] != nil = eval([key]['gaptool']) hostname = "#{gaptags[:role]}-#{gaptags[:environment]}-#{gaptags[:number]}.#{@env_settings['domain']}" c += [{ :hostname => hostname, :recipe => [:recipe], :deploy => [:deploy], :number => [:number], :role => [:role], :environment => [:environment], :apps => eval([:apps]) }] end end return c end |
#getCluster ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/app.rb', line 77 def getCluster hosts = Array.new if @args[:role] nodes = $c.select {|f| f[:role] == @args[:role] }.select {|i| i[:environment] == @args[:environment]} elsif @args[:app] nodes = $c.select {|i| i[:environment] == @args[:environment]}.select {|f| f[:apps].include? @args[:app]} end nodes.each {|f| hosts += [f[:hostname]]} return hosts end |
#isCluster? ⇒ Boolean
74 75 76 |
# File 'lib/app.rb', line 74 def isCluster? return false end |
#putkey(host) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/app.rb', line 113 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 |
#recipeRun(host, run_list, settings = {}) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/app.rb', line 205 def recipeRun(host, run_list, settings={}) host_settings = { 'this_server' => host, 'run_list' => [ "recipe[#{run_list}]" ], 'app_user' => @env_settings['user'], } @chefsettings.merge!(host_settings) @chefsettings.merge!(settings) @chefsettings.merge!($c.select {|f| f[:hostname] == host}.first) run = [ "cd ~admin/ops; git pull", "echo '#{@chefsettings.to_json}' > ~admin/solo.json", "sudo chef-solo -c ~admin/ops/cookbooks/solo.rb -j ~admin/solo.json" ] putkey(host) sshcmd(host, run) end |
#singleHost ⇒ Object
147 148 149 |
# File 'lib/app.rb', line 147 def singleHost return "#{@args[:role]}-#{@args[:environment]}-#{@args[:number]}.#{@env_settings['domain']}" end |
#sshcmd(host, commands, options = {}) ⇒ Object
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 |
# File 'lib/app.rb', line 87 def sshcmd(host, commands, = {}) if [:user] user = [:user] else user = 'admin' end if [:key] key = [:key] else key = @user_settings['mykey'] end Net::SSH.start(host, user, :key_data => [key], :config => false, :keys_only => true, :paranoid => false) do |ssh| ENV['SSH_AUTH_SOCK'] = '' commands.each do |command| if ![:quiet] puts command.color(:cyan) end ssh.exec! command do |ch, stream, line| if ![:quiet] puts "#{host.color(:yellow)}:#{line}" end end end end end |
#sshReachable? ⇒ Boolean
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/app.rb', line 133 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 |