Module: Base

Defined in:
lib/plugins/Base/plugin.rb

Instance Method Summary collapse

Instance Method Details

#chefrunObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/plugins/Base/plugin.rb', line 30

def chefrun
  if sshReachable?
    hosts = getCluster()
    hosts.peach do |host|
      host_settings = {
        'this_server' => host,
        'run_list'    => [ "recipe[#{@chefsettings['normal_recipe']}]" ],
        'app_user'    => @env_settings['user'],
        'app_name'    => @args[:app]
      }
      json = @chefsettings.merge!(host_settings).to_json
      run = [
        "cd ~admin/ops; git pull",
        "echo '#{json}' > ~admin/solo.json",
        "sudo chef-solo -c ~admin/ops/cookbooks/solo.rb -j ~admin/solo.json"
      ]
      putkey(host)
      sshcmd(host, run)
    end
  end
end

#deployObject



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
# File 'lib/plugins/Base/plugin.rb', line 69

def deploy
  if sshReachable?
    if @args[:branch] == "nil" || @args[:branch].nil?
      branch = @env_settings['applications'][@args[:app]][@args[:environment]]['default_branch']
    else
      branch = @args[:branch]
    end
    hosts = getCluster()
    hosts.peach do |host|
      host_settings = {
        'this_server' => host,
        'run_list'    => [ "recipe[#{@chefsettings['deploy_recipe']}]" ],
        'do_migrate'  => @args[:migrate],
        'branch'      => branch,
        'app_user'    => @env_settings['user'],
        'app_name'    => @args[:app],
        'rollback'    => false
      }
      json = @chefsettings.merge!(host_settings).to_json
      run = [
        "cd ~admin/ops; git pull",
        "echo '#{json}' > ~admin/solo.json",
        "sudo chef-solo -c ~admin/ops/cookbooks/solo.rb -j ~admin/solo.json"
      ]
      putkey(host)
      sshcmd(host, run)
    end
  end
end

#infoObject



51
52
53
54
55
56
57
# File 'lib/plugins/Base/plugin.rb', line 51

def info
  hosts = getCluster()
  hosts.peach do |host|
    run = [ "~admin/ops/scripts/gtinfo.rb" ]
    sshcmd(host, run)
  end
end

#initObject



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
# File 'lib/plugins/Base/plugin.rb', line 144

def init
  if @args[:zone] == 'nil'
    zone = @env_settings['default_zone']
  else
    zone = @args[:zone]
  end
  az = zone.chop
  if @args[:arch] == 'nil'
    arch = @env_settings['default_arch']
  else
    arch = @args[:arch]
  end
  ami = @env_settings['amis'][az][arch]['id']
  user = @env_settings['amis'][az][arch]['user']
  if @args[:node] == 'solo' && isCluster?
    hosts = getCluster()
  end
  if @args[:node] != 'solo' || !isCluster?
    hosts = [ singleHost() ]
  end
  begin
    initscript = File.read(File.expand_path(File.dirname(__FILE__) + "/init/#{@env_settings['amis'][az][arch]['os']}.erb"))
  rescue
    puts "There is no init file for your OS, aborting."
    exit 100
  end
  itype = @env_settings['applications'][@args[:app]][@args[:environment]]['itype']
  keyname = @user_settings['awskeys'][az]
  key = @user_settings['initkeys'][keyname]
  sg = @env_settings['applications'][@args[:app]][@args[:environment]]['sg']
  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
  hosts.peach do |host|
    host_settings = {
      'this_server' => host,
      'run_list'    => [ "recipe[#{@chefsettings['init_recipe']}]" ],
      'do_migrate'  => @args[:migrate],
      'branch'      => @args[:branch],
      'app_user'    => @env_settings['user'],
      'app_name'    => @args[:app]
    }
    json = @chefsettings.merge!(host_settings).to_json
    instance = ec2.instances.create(:image_id => ami, :availability_zone => zone, :instance_type => itype, :key_name => keyname, :security_group_ids => sg)
    print "Waiting for instanace to start".color(:cyan)
    while instance.status != :running do
      sleep 5
      print ".".color(:yellow)
    end
    puts ""
    print "Waiting for SSH to respoond".color(:cyan)
    loop do
      begin
        ip = instance.ip_address
        sshcmd(ip, ["exit 0"], :user => user, :key => key)
        break
      rescue
        print ".".color(:yellow)
      end
      sleep 2
    end
    puts "Sleeping another 5s for disks to settle".color(:cyan)
    sleep 5
    ip = instance.ip_address
    puts ""
    instance.add_tag('dns', :value => host)
    instance.add_tag('app', :value => @args[:app])
    instance.add_tag('environment', :value => @args[:environment])
    instance.add_tag('Name', :value => host.sub(".#{@env_settings['domain']}", ""))
    instance.add_tag('node', :value => @args[:node])
    render = ERB.new(initscript)
    run = render.result(binding).split(/\n/)
    run += [
        "echo '#{json}' > /tmp/solo.json",
        "sudo su -c 'chef-solo -c ~admin/ops/cookbooks/solo.rb -j /tmp/solo.json'",
        "sudo rm /tmp/solo.json"
      ]
    sshcmd(ip, run, :user => user, :key => key)
  end
end

#logObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/plugins/Base/plugin.rb', line 58

def log
  logs = YAML::load(File.open('./log.yml'))[@args[:logtype]]
  run = Array.new
  logs.each do |log|
    run += "tail -f -n#{@user_settings['taillines']} #{log}"
  end
  hosts = getCluster()
  hosts.peach do |host|
    sshcmd(host, run)
  end
end

#rollbackObject



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
# File 'lib/plugins/Base/plugin.rb', line 98

def rollback
  if sshReachable?
    if @args[:branch] == "nil" || @args[:branch].nil?
      branch = @env_settings['applications'][@args[:app]][@args[:environment]]['default_branch']
    else
      branch = @args[:branch]
    end
    hosts = getCluster()
    hosts.peach do |host|
      host_settings = {
        'this_server' => host,
        'run_list'    => [ "recipe[#{@chefsettings['deploy_recipe']}]" ],
        'do_migrate'  => @args[:migrate],
        'branch'      => branch,
        'app_user'    => @env_settings['user'],
        'app_name'    => @args[:app],
        'rollback'    => true
      }
      json = @chefsettings.merge!(host_settings).to_json
      run = [
        "cd ~admin/ops; git pull",
        "echo '#{json}' > ~admin/solo.json",
        "sudo chef-solo -c ~admin/ops/cookbooks/solo.rb -j ~admin/solo.json"
      ]
      putkey(host)
      sshcmd(host, run)
    end
  end
end

#scpfromObject



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/plugins/Base/plugin.rb', line 8

def scpfrom
  require 'net/scp'
  # net-scp does not handle ~ well...
  remote = ARGV[-2].gsub('~', "/data/admin")
  local = ARGV[-1].gsub('~', ENV['HOME'])
  host = singleHost()
  session = Net::SSH.start(host, 'admin', :key_data => [@user_settings['mykey']], :config => false, :keys_only => true, :paranoid => false)
  session.scp.download!(remote, local, :recursive => true) do |ch, name, sent, total|
    print "\r#{name}: #{(sent.to_f * 100 / total.to_f).to_i}%"
  end
end

#scptoObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/plugins/Base/plugin.rb', line 19

def scpto
  require 'net/scp'
  # net-scp does not handle ~ well...
  remote = ARGV[-1].gsub(/~/, "/data/admin")
  local = ARGV[-2].gsub(/~/, ENV['HOME'])
  host = singleHost()
  session = Net::SSH.start(host, 'admin', :key_data => [@user_settings['mykey']], :config => false, :keys_only => true, :paranoid => false)
  session.scp.upload!(local, remote, :recursive => true) do |ch, name, sent, total|
    print "\r#{name}: #{(sent.to_f * 100 / total.to_f).to_i}%"
  end
end

#sshObject



2
3
4
5
6
7
# File 'lib/plugins/Base/plugin.rb', line 2

def ssh
  host = singleHost()
  system "echo '#{@user_settings['mykey']}' > /tmp/mykey;chmod 600 /tmp/mykey"
  system "ssh -i /tmp/mykey admin@#{host}"
  system "rm /tmp/mykey 2> /dev/null"
end

#webObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/plugins/Base/plugin.rb', line 127

def web
  if sshRachable?
    hosts = getCluster()
    if @args[:enable]
      hosts.peach do |host|
        sshcmd(host, "sudo -u #{@env_settings['user']} rm /data/#{@args[:app]}/shared/system/maintenance.html 2> /dev/null", :quiet => true)
        puts "#{host} : web enabled"
      end
    end
    if @args[:disable]
      hosts.peach do |host|
        sshcmd(host, "sudo -u #{@env_settings['user']} ln -sf /data/#{@args[:app]}/current/public/maintenance.html /data/#{@args[:app]}/shared/system/maintenance.html", :quiet => true)
        puts "#{host} : web disabled"
      end
    end
  end
end