gaptool 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .AppleDouble
2
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source :rubygems
2
+ gem 'net-ssh'
3
+ gem 'trollop'
4
+ gem 'aws-sdk'
5
+ gem 'json'
6
+ gem 'peach'
7
+ gem 'rainbow'
data/Gemfile.lock ADDED
@@ -0,0 +1,31 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ aws-sdk (1.5.2)
5
+ httparty (~> 0.7)
6
+ json (~> 1.4)
7
+ nokogiri (>= 1.4.4)
8
+ uuidtools (~> 2.1)
9
+ httparty (0.8.3)
10
+ multi_json (~> 1.0)
11
+ multi_xml
12
+ json (1.6.1)
13
+ multi_json (1.3.6)
14
+ multi_xml (0.5.1)
15
+ net-ssh (2.1.4)
16
+ nokogiri (1.5.2)
17
+ peach (0.4)
18
+ rainbow (1.1.4)
19
+ trollop (1.16.2)
20
+ uuidtools (2.1.2)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ aws-sdk
27
+ json
28
+ net-ssh
29
+ peach
30
+ rainbow
31
+ trollop
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,17 @@
1
+ See LICENSE for license (Apache-2.0)
2
+
3
+ About
4
+ =====
5
+
6
+ gaptool will create environments in AWS EC2, RDS, and Route 53 based on your chef recipes and make them manageable much in the same way that EngineYard and Heroku are.
7
+
8
+ Essentially, turning your IaaS into a PaaS.
9
+
10
+ Setup
11
+ =====
12
+
13
+ Lots of documentation will be fourthcoming, stay tuned.
14
+
15
+ Examples of configuration for the tool are in common/
16
+
17
+ Example of chef repo to use this properly will be available soon
data/bin/gt ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'net/ssh'
5
+ require 'trollop'
6
+ require 'yaml'
7
+ require 'aws'
8
+ require 'json'
9
+ require 'peach'
10
+ require 'tinder'
11
+ require 'erb'
12
+ require 'rainbow'
13
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/app.rb")
14
+
15
+ # Make sure lang isn't fucked
16
+ ENV['LANG'] = "en_US.UTF-8"
17
+
18
+
19
+ # Nuke your ssh-agent
20
+ ENV['SSH_AUTH_SOCK'] = ''
data/common/README ADDED
@@ -0,0 +1 @@
1
+ Put all these files in ~/.gaptool
data/common/env.yml ADDED
@@ -0,0 +1,61 @@
1
+ # This is the user that your application will run under
2
+ user: appuser
3
+
4
+ # This is the domain that is associated with your Route 53 account
5
+ domain: foo.com
6
+
7
+ # AMI IDs. These are ubuntu 12.04, distributed by Canonical
8
+ # The user is the user that the AMI associates with the AMI key
9
+ # Ubuntu AMIs use 'ubuntu' as this user
10
+ amis:
11
+ us-east-1:
12
+ i686:
13
+ id: ami-ac9943c5
14
+ user: ubuntu
15
+ x86_64:
16
+ id: ami-a29943cb
17
+ user: ubuntu
18
+
19
+ # if you do not set an AZ, this one will be used on init
20
+ default_zone: us-east-1d
21
+
22
+ # Default architecture, can be overrridden
23
+ default_arch: x86_64
24
+
25
+ # This is the git repo that will contain your chef recipes and other ops things
26
+ ops_repo: git@github.com:Gild/ops.git
27
+
28
+ # This is where you define your applications
29
+ # First is an example of a minimal set
30
+ # Then an example of settings not required, but will be set, with their defaults
31
+ # Third, and example with RDS
32
+ # Any variable you set here will be passed on to chef
33
+ applications:
34
+ minimalapp:
35
+ production:
36
+ scmsource: git@github.com:Group/myapp.git
37
+ cluster:
38
+ - app1 # this will create 2 cluster nodes
39
+ - app2 # you can name them however you want
40
+ staging:
41
+ scmsource: git@github.com:Group/myapp.git
42
+ minimalappdefaults:
43
+ production:
44
+ scmsource: git@github.com:Group/myapp.git
45
+ scmtype: git # git is default, it will not autodetect (yet...)
46
+ itype: m1.small # m1.small is the default node size, just because
47
+ default_branch: master # master is the default branch
48
+ init_recipe: init # the default recipe that is run on 'init' is 'init'
49
+ normal_recipe: main # the default recipe that is run on 'chefrun' is 'main'
50
+ deploy_recipe: deploy # the default recipe that is run on 'deploy' is 'deploy'
51
+ cluster: no # the default setting for clustering is 'no'
52
+ tcplocal: 22 # the security group will be created with port 22 available to nodes in the security group
53
+ tcpexternal: 22,80 # by default, the security group will be created with port 22 and 80 available externally
54
+ udplocal: nil # by default no UDP ports are opened to the SG
55
+ udpexternal: nil # by default no UDP ports are open to the outside
56
+ appwithrds:
57
+ production:
58
+ url: bar.foo.com
59
+ scmsource: git@github.com:Group/myapp.git
60
+ db_host: arkham-staging.cbrp4yfcp7ys.us-east-1.rds.amazonaws.com
61
+ db_pass: MA4RruJJK8
@@ -0,0 +1,10 @@
1
+ test:
2
+ app:
3
+ short: -a
4
+ type: string
5
+ help: Which appliction, e.g. twitter
6
+ default: nil
7
+ environment:
8
+ short: -e
9
+ type: string
10
+ default: nil
@@ -0,0 +1,13 @@
1
+ module SamplePlugin
2
+ def reboot
3
+ if sshReachable?
4
+ hosts.peach do |host|
5
+ run = [ "sudo reboot" ]
6
+ sshcmd(host, run)
7
+ end
8
+ end
9
+ end
10
+ def test
11
+ puts "things worked!"
12
+ end
13
+ end
@@ -0,0 +1 @@
1
+ - SamplePlugin
data/common/user.yml ADDED
@@ -0,0 +1,28 @@
1
+ # How many lines the 'log' facility will display
2
+ taillines: 50
3
+
4
+ # This is your AWS authentication credentials
5
+ aws_id:
6
+ aws_secret:
7
+
8
+ # Keys you have set up within AWS
9
+ awskeys:
10
+ us-east-1: "east1key"
11
+ us-west-2: "west2key"
12
+
13
+ # This is your personal private key
14
+ mykey: |
15
+ -----BEGIN RSA PRIVATE KEY-----
16
+ ...
17
+ -----END RSA PRIVATE KEY-----
18
+
19
+ # These are your keys you share with AWS that are used for the initial user
20
+ initkeys:
21
+ east1key: |
22
+ -----BEGIN RSA PRIVATE KEY-----
23
+ ...
24
+ -----END RSA PRIVATE KEY-----
25
+ west2key: |
26
+ -----BEGIN RSA PRIVATE KEY-----
27
+ ...
28
+ -----END RSA PRIVATE KEY-----
data/gaptool.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "gaptool"
3
+ s.version = "0.10.0"
4
+ s.authors = ['Matt Bailey']
5
+ s.email = ['m@mdb.io']
6
+ s.homepage = 'http://mdb.io'
7
+ s.summary = 'EC2 application deployment, initialization and configuration tool'
8
+ s.description = 'This command line tool will service a chef repo for building out, configuring, and deploying environments in EC2 and EC2 compatable infrastructure clouds'
9
+ s.files = `git ls-files`.split("\n")
10
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
11
+
12
+ s.add_dependency "net-ssh"
13
+ s.add_dependency "trollop"
14
+ s.add_dependency "aws-sdk"
15
+ s.add_dependency "json"
16
+ s.add_dependency "peach"
17
+ s.add_dependency "rainbow"
18
+ end
19
+
data/lib/app.rb ADDED
@@ -0,0 +1,171 @@
1
+ def setup
2
+ yaml = Hash.new
3
+ unless File.directory?("#{ENV['HOME']}/.gaptool") || File.exists?("#{ENV['HOME']}/.gaptool")
4
+ puts "Welcome to gaptool setup\nThis will set up your ~/.gaptool configuration\nYou will need very little info here if you are NOT creating new nodes (e.g. just configuring and deploying)\nIf you ARE using the the 'init' facility, you will need your AWS ID, Secret, and EC2 PEM keys for relevant Availability Zones.".color(:red)
5
+ puts "Starting with your AWS ID/Secret.\nIf you don't have these, just press enter.".color(:cyan)
6
+ print "Enter your AWS ID: "
7
+ yaml['aws_id'] = gets.chomp
8
+ print "enter your AWS Secret: "
9
+ yaml['aws_secret'] = gets.chomp
10
+ puts "Now we will go through each AWS zone\nEnter a key NAME you have in each zone that you want associated with gaptool nodes.\nIf you don't have one, press enter.\nAfter each key, paste the path to the downloaded key.".color(:cyan)
11
+ zones = [ 'us-east-1', 'us-west-1', 'us-west-2', 'eu-west-1', 'ap-southeast-1', 'ap-northeast-1', 'sa-east-1' ]
12
+ yaml['awskeys'] = Hash.new
13
+ yaml['initkeys'] = Hash.new
14
+ zones.each do |zone|
15
+ print "#{zone}: "
16
+ yaml['awskeys'][zone] = gets.chomp
17
+ if yaml['awskeys'][zone] != ''
18
+ print "Path to #{zone}.pem: "
19
+ yaml['initkeys'][yaml['awskeys'][zone]] = File.read(File.expand_path(gets.chomp))
20
+ end
21
+ end
22
+ key = OpenSSL::PKey::RSA.new 2048
23
+ type = key.ssh_type
24
+ data = [ key.to_blob ].pack('m0')
25
+ yaml['mykey'] = key.to_pem
26
+ yaml['mypub'] = "#{type} #{data}"
27
+ Dir.mkdir("#{ENV['HOME']}/.gaptool")
28
+ Dir.mkdir("#{ENV['HOME']}/.gaptool/plugins")
29
+ File.open("#{ENV['HOME']}/.gaptool/plugins.yml", "w") {}
30
+ File.open(File.expand_path("~/.gaptool/user.yml"), 'w') {|f| f.write(yaml.to_yaml) }
31
+ puts "Your ~/.gaptool directory and user.yml have been configured\nPlease ask your administrator to provide you with a env.yml\nAdd the following public key to your github profile (or your git repo server)\nas well as in the authorized_keys file in your chef recipe for the admin user.".color(:cyan)
32
+ puts yaml['mypub']
33
+ exit 0
34
+ end
35
+ end
36
+ setup()
37
+
38
+ $dist_plugins = [ 'Base' ]
39
+ $plugins = $dist_plugins + YAML::load(File.open("#{ENV['HOME']}/.gaptool/plugins.yml"))
40
+
41
+ $commands = Hash.new
42
+ $plugins.each do |plugin|
43
+ if $dist_plugins.include? plugin
44
+ $commands.merge!(YAML::load(File.open(File.expand_path(File.dirname(__FILE__) + "/plugins/#{plugin}/config.yml"))))
45
+ else
46
+ $commands.merge!(YAML::load(File.open("#{ENV['HOME']}/.gaptool/plugins/#{plugin.to_s}/config.yml")))
47
+ end
48
+ end
49
+
50
+ cmd = ARGV.shift
51
+ if !$commands.keys.include?(cmd) || cmd.nil?
52
+ puts "Invalid option, please select one of the following commands:"
53
+ $commands.keys.each do |command|
54
+ puts " #{command}"
55
+ end
56
+ exit 1
57
+ else
58
+ cmd_opts = Trollop::options do
59
+ $commands[cmd].each_key do |key|
60
+ if $commands[cmd][key]['type'].nil?
61
+ opt key.to_sym, $commands[cmd][key]['help'], :short => $commands[cmd][key]['short'], :default => $commands[cmd][key]['default']
62
+ else
63
+ opt key.to_sym, $commands[cmd][key]['help'], :short => $commands[cmd][key]['short'], :default => $commands[cmd][key]['default'], :type => $commands[cmd][key]['type'].to_sym
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ class GTBase
70
+ def isCluster?
71
+ if @env_settings['applications'][@args[:app]][@args[:environment]]['cluster']
72
+ return true
73
+ else
74
+ return false
75
+ end
76
+ end
77
+ def getCluster
78
+ hosts = Array.new
79
+ if isCluster?
80
+ @env_settings['applications'][@args[:app]][@args[:environment]]['cluster'].each do |node|
81
+ hosts << "#{@args[:app]}-#{@args[:environment]}-#{node}.#{@env_settings['domain']}"
82
+ end
83
+ else
84
+ hosts << "#{@args[:app]}-#{@args[:environment]}.#{@env_settings['domain']}"
85
+ end
86
+ return hosts
87
+ end
88
+ def sshcmd(host, commands, user='admin', key=@user_settings['mykey'])
89
+ Net::SSH.start(host, user, :key_data => [key], :config => false, :keys_only => true, :paranoid => false) do |ssh|
90
+ commands.each do |command|
91
+ puts command.color(:cyan)
92
+ ssh.exec! command do
93
+ |ch, stream, line|
94
+ puts "#{host.color(:yellow)}:#{line}"
95
+ end
96
+ end
97
+ end
98
+ end
99
+ def putkey(host)
100
+ breakkey = @user_settings['mykey'].gsub(/\n/,'###')
101
+ run = [
102
+ "rm ~admin/.ssh/mykey* 2> /dev/null",
103
+ "echo '#{breakkey}' > /tmp/key",
104
+ "key=`mktemp -u -p ~admin/.ssh/ -t mykey.XXX`; cat /tmp/key|perl -pe 's/###/\\n$1/g' > ${key}; echo \"Host *\\n StrictHostKeyChecking no\\n IdentityFile ${key}\" > ~admin/.ssh/config;chmod 600 ${key}",
105
+ "chmod 600 ~admin/.ssh/config",
106
+ "rm /tmp/key"
107
+ ]
108
+ sshcmd(host, run)
109
+ end
110
+ def sshReachable?
111
+ hosts = getCluster()
112
+ hosts.each do |host|
113
+ begin
114
+ puts "Checking SSH to: #{host}"
115
+ sshcmd(host, ["exit 0"])
116
+ rescue
117
+ puts "ERROR: Could not ssh to #{host}\nEither your connection is failing or AWS is having routing issues.\nAborting."
118
+ exit 255
119
+ return false
120
+ end
121
+ end
122
+ return true
123
+ end
124
+ def singleHost
125
+ if @args[:node] == 'solo' && isCluster?
126
+ 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."
127
+ exit 100
128
+ end
129
+ if isCluster?
130
+ return "#{@args[:app]}-#{@args[:environment]}-#{@args[:node]}.#{@env_settings['domain']}"
131
+ else
132
+ return "#{@args[:app]}-#{@args[:environment]}.#{@env_settings['domain']}"
133
+ end
134
+ end
135
+ def initialize(args)
136
+ @args = args
137
+ if ENV['GT_ENV_SETTINGS']
138
+ @env_settings = YAML::load(File.open(File.expand_path(ENV['GT_ENV_CONFIG'])))
139
+ else
140
+ @env_settings = YAML::load(File.open(File.expand_path("#{ENV['HOME']}/.gaptool/env.yml")))
141
+ end
142
+ if ENV['GT_USER_SETTINGS']
143
+ @user_settings = YAML::load(File.open(File.expand_path(ENV['GT_USER_CONFIG'])))
144
+ else
145
+ @user_settings = YAML::load(File.open(File.expand_path("#{ENV['HOME']}/.gaptool/user.yml")))
146
+ end
147
+ chef_extra = {
148
+ 'rails_env' => @args[:environment],
149
+ 'server_names' => getCluster()
150
+ }
151
+ @chefsettings = @env_settings['applications'][@args[:app]][@args[:environment]]
152
+ @chefsettings.merge!(@args)
153
+ @chefsettings.merge!(chef_extra)
154
+ end
155
+ end
156
+
157
+ class GTCluster < GTBase
158
+ $plugins.each do |plugin|
159
+ if $dist_plugins.include?(plugin)
160
+ require File.expand_path(File.dirname(__FILE__) + "/plugins/#{plugin}/plugin.rb")
161
+ else
162
+ require File.expand_path(ENV['HOME'] + "/.gaptool/plugins/#{plugin}/plugin.rb")
163
+ end
164
+ include Object.const_get(plugin)
165
+ end
166
+ end
167
+
168
+
169
+
170
+ cluster = GTCluster.new(cmd_opts)
171
+ cluster.send cmd
@@ -0,0 +1,65 @@
1
+ chefrun: &withoutnode
2
+ app:
3
+ short: -a
4
+ type: string
5
+ help: Which appliction, e.g. twitter
6
+ default: nil
7
+ environment:
8
+ short: -e
9
+ type: string
10
+ help: Environment, e.g. staging
11
+ default: nil
12
+ scpto: &node
13
+ << : *withoutnode
14
+ node:
15
+ short: -n
16
+ type: string
17
+ help: leave blank if solo, otherwise, app2, util, etc...
18
+ default: solo
19
+ scpfrom:
20
+ << : *node
21
+ ssh:
22
+ << : *node
23
+ deploy:
24
+ << : *withoutnode
25
+ migrate:
26
+ short: -m
27
+ help: "Toggle migrations, default off"
28
+ defaut: nil
29
+ branch:
30
+ short: -r
31
+ help: "Choose git branch, default defined in your gt_env.yml"
32
+ type: string
33
+ default: nil
34
+ init:
35
+ << : *node
36
+ zone:
37
+ short: -z
38
+ help: "availability zone to deploy in, default in your gt_env.yml"
39
+ type: string
40
+ default: nil
41
+ arch:
42
+ short: -r
43
+ help: "CPU architecture, currently i686 or x86_64"
44
+ type: string
45
+ default: nil
46
+ web:
47
+ << : *withoutnode
48
+ enable:
49
+ short: -n
50
+ help: "Remove maint. page"
51
+ default: nil
52
+ disable:
53
+ short: -d
54
+ help: "Put up maint. page"
55
+ default: nil
56
+ info:
57
+ << : *node
58
+ log:
59
+ << : *node
60
+ logtype:
61
+ short: -l
62
+ help: "Log type, as defined in the plugin's log.yml"
63
+ type: string
64
+ default: nil
65
+
@@ -0,0 +1,24 @@
1
+ sudo apt-get install -y zsh git libssl-dev ruby1.9.1-full build-essential
2
+ sudo REALLY_GEM_UPDATE_SYSTEM=true gem update --system
3
+ sudo gem install --bindir /usr/local/bin --no-ri --no-rdoc chef
4
+ sudo mkdir /data
5
+ sudo useradd -m -d /data/<%= @args[:app] %> -s /bin/zsh <%= @env_settings['user'] %> -g admin
6
+ sudo useradd -m -d /data/admin -s /bin/zsh admin -g admin
7
+ mkdir ~/.ssh 2> /dev/null
8
+ chmod 700 ~/.ssh/
9
+ echo '<%= @user_settings['mykey'].gsub(/\n/,'###') %>' > ~/.ssh/id_rsa.tmp
10
+ cat ~/.ssh/id_rsa.tmp | perl -pe 's/###/\n$1/g' > ~/.ssh/id_rsa
11
+ rm ~/.ssh/id_rsa.tmp
12
+ chmod 600 ~/.ssh/id_rsa
13
+ echo 'StrictHostKeyChecking no' > ~/.ssh/config
14
+ git clone <%= @env_settings['ops_repo'] %> ./ops
15
+ sudo mv ~/ops /data/admin; sudo chown -R admin:admin /data/admin
16
+ sudo sh -c "echo '%admin ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers"
17
+ sudo groupadd <%= @env_settings['user'] %>
18
+ sudo usermod -a -G <%= @env_settings['user'] %> <%= @env_settings['user'] %>
19
+ sudo mkdir ~admin/.ssh
20
+ sudo chmod 700 ~admin/.ssh
21
+ sudo chown admin:admin ~admin/.ssh
22
+ sudo sh -c "echo '<%= @user_settings['mypub'] %>' > ~admin/.ssh/authorized_keys"
23
+ sudo chown admin:admin ~admin/.ssh/authorized_keys
24
+ sudo chmod 600 ~admin/.ssh/authorized_keys
@@ -0,0 +1,2 @@
1
+ rails:
2
+ - /var/log/rails.log
@@ -0,0 +1,173 @@
1
+ module Base
2
+ def ssh
3
+ host = singleHost()
4
+ system "echo '#{@user_settings['mykey']}' > /tmp/mykey;chmod 600 /tmp/mykey"
5
+ system "ssh -i /tmp/mykey admin@#{host}"
6
+ system "rm /tmp/mykey 2> /dev/null"
7
+ end
8
+ def scpfrom
9
+ end
10
+ def scpto
11
+ end
12
+ def chefrun
13
+ if sshReachable?
14
+ hosts = getCluster()
15
+ hosts.peach do |host|
16
+ host_settings = {
17
+ 'this_server' => host,
18
+ 'run_list' => [ "recipe[#{@chefsettings['normal_recipe']}]" ],
19
+ 'app_user' => @env_settings['user'],
20
+ 'app_name' => @args[:app]
21
+ }
22
+ json = @chefsettings.merge!(host_settings).to_json
23
+ run = [
24
+ "cd ~admin/ops; git pull",
25
+ "echo '#{json}' > ~admin/solo.json",
26
+ "sudo chef-solo -c ~admin/ops/cookbooks/solo.rb -j ~admin/solo.json"
27
+ ]
28
+ putkey(host)
29
+ sshcmd(host, run)
30
+ end
31
+ end
32
+ end
33
+ def info
34
+ hosts = getCluster()
35
+ hosts.peach do |host|
36
+ run = [ "~admin/ops/scripts/gtinfo.rb" ]
37
+ sshcmd(host, run)
38
+ end
39
+ end
40
+ def log
41
+ logs = YAML::load(File.open('./log.yml'))[@args[:logtype]]
42
+ run = Array.new
43
+ logs.each do |log|
44
+ run += "tail -f -n#{@user_settings['taillines']} #{log}"
45
+ end
46
+ hosts = getCluster()
47
+ hosts.peach do |host|
48
+ sshcmd(host, run)
49
+ end
50
+ end
51
+ def deploy
52
+ if sshReachable?
53
+ if @args[:branch] == "nil" || @args[:branch].nil?
54
+ branch = @env_settings['applications'][@args[:app]][@args[:environment]]['default_branch']
55
+ end
56
+ hosts = getCluster()
57
+ hosts.peach do |host|
58
+ host_settings = {
59
+ 'this_server' => host,
60
+ 'run_list' => [ "recipe[#{@chefsettings['deploy_recipe']}]" ],
61
+ 'do_migrate' => @args[:migrate],
62
+ 'branch' => @args[:branch],
63
+ 'app_user' => @env_settings['user'],
64
+ 'app_name' => @args[:app]
65
+ }
66
+ json = @chefsettings.merge!(host_settings).to_json
67
+ run = [
68
+ "cd ~admin/ops; git pull",
69
+ "echo '#{json}' > ~admin/solo.json",
70
+ "sudo chef-solo -c ~admin/ops/cookbooks/solo.rb -j ~admin/solo.json"
71
+ ]
72
+ putkey(host)
73
+ sshcmd(host, run)
74
+ end
75
+ end
76
+ end
77
+ def web
78
+ if sshRachable?
79
+ hosts = getCluster()
80
+ if @args[:enable]
81
+ hosts.peach do |host|
82
+ sshcmd(host, "sudo -u #{@env_settings['user']} rm /data/#{@args[:app]}/shared/system/maintenance.html 2> /dev/null")
83
+ puts "#{host} : web enabled"
84
+ end
85
+ end
86
+ if @args[:disable]
87
+ hosts.peach do |host|
88
+ sshcmd(host, "sudo -u #{@env_settings['user']} ln -sf /data/#{@args[:app]}/current/public/maintenance.html /data/#{@args[:app]}/shared/system/maintenance.html")
89
+ puts "#{host} : web disabled"
90
+ end
91
+ end
92
+ end
93
+ end
94
+ def init
95
+ if @args[:zone] == 'nil'
96
+ zone = @env_settings['default_zone']
97
+ else
98
+ zone = @args[:zone]
99
+ end
100
+ az = zone.chop
101
+ if @args[:arch] == 'nil'
102
+ arch = @env_settings['default_arch']
103
+ else
104
+ arch = @args[:arch]
105
+ end
106
+ ami = @env_settings['amis'][az][arch]['id']
107
+ user = @env_settings['amis'][az][arch]['user']
108
+ if @args[:node] == 'solo' && isCluster?
109
+ hosts = getCluster()
110
+ end
111
+ if @args[:node] != 'solo' || !isCluster?
112
+ hosts = [ singleHost() ]
113
+ end
114
+ begin
115
+ initscript = File.read(File.expand_path(File.dirname(__FILE__) + "/init/#{@env_settings['amis'][az][arch]['os']}.erb"))
116
+ rescue
117
+ puts "There is no init file for your OS, aborting."
118
+ exit 100
119
+ end
120
+ itype = @env_settings['applications'][@args[:app]][@args[:environment]]['itype']
121
+ keyname = @user_settings['awskeys'][az]
122
+ key = @user_settings['initkeys'][keyname]
123
+ sg = @env_settings['applications'][@args[:app]][@args[:environment]]['sg']
124
+ AWS.config(:access_key_id => @user_settings['aws_id'], :secret_access_key => @user_settings['aws_secret'], :ec2_endpoint => "ec2.#{az}.amazonaws.com")
125
+ ec2 = AWS::EC2.new
126
+ hosts.peach do |host|
127
+ host_settings = {
128
+ 'this_server' => host,
129
+ 'run_list' => [ "recipe[#{@chefsettings['init_recipe']}]" ],
130
+ 'do_migrate' => @args[:migrate],
131
+ 'branch' => @args[:branch],
132
+ 'app_user' => @env_settings['user'],
133
+ 'app_name' => @args[:app]
134
+ }
135
+ json = @chefsettings.merge!(host_settings).to_json
136
+ instance = ec2.instances.create(:image_id => ami, :availability_zone => zone, :instance_type => itype, :key_name => keyname, :security_group_ids => sg)
137
+ print "Waiting for instanace to start".color(:cyan)
138
+ while instance.status != :running do
139
+ sleep 5
140
+ print ".".color(:yellow)
141
+ end
142
+ puts ""
143
+ print "Waiting for SSH to respoond".color(:cyan)
144
+ loop do
145
+ begin
146
+ ip = instance.ip_address
147
+ sshcmd(ip, ["exit 0"], user, key)
148
+ break
149
+ rescue
150
+ print ".".color(:yellow)
151
+ end
152
+ sleep 2
153
+ end
154
+ puts "Sleeping another 5s for disks to settle".color(:cyan)
155
+ sleep 5
156
+ ip = instance.ip_address
157
+ puts ""
158
+ instance.add_tag('dns', :value => host)
159
+ instance.add_tag('app', :value => @args[:app])
160
+ instance.add_tag('environment', :value => @args[:environment])
161
+ instance.add_tag('Name', :value => host.sub(".#{@env_settings['domain']}", ""))
162
+ instance.add_tag('node', :value => @args[:node])
163
+ render = ERB.new(initscript)
164
+ run = render.result(binding).split(/\n/)
165
+ run += [
166
+ "echo '#{json}' > /tmp/solo.json",
167
+ "sudo su -c 'chef-solo -c ~admin/ops/cookbooks/solo.rb -j /tmp/solo.json'",
168
+ "sudo rm /tmp/solo.json"
169
+ ]
170
+ sshcmd(ip, run, user, key)
171
+ end
172
+ end
173
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gaptool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.10.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matt Bailey
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-21 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: net-ssh
16
+ requirement: &70263218578860 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70263218578860
25
+ - !ruby/object:Gem::Dependency
26
+ name: trollop
27
+ requirement: &70263218578420 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70263218578420
36
+ - !ruby/object:Gem::Dependency
37
+ name: aws-sdk
38
+ requirement: &70263218578000 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70263218578000
47
+ - !ruby/object:Gem::Dependency
48
+ name: json
49
+ requirement: &70263218577580 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70263218577580
58
+ - !ruby/object:Gem::Dependency
59
+ name: peach
60
+ requirement: &70263218577160 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *70263218577160
69
+ - !ruby/object:Gem::Dependency
70
+ name: rainbow
71
+ requirement: &70263218576740 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: *70263218576740
80
+ description: This command line tool will service a chef repo for building out, configuring,
81
+ and deploying environments in EC2 and EC2 compatable infrastructure clouds
82
+ email:
83
+ - m@mdb.io
84
+ executables:
85
+ - gt
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - .gitignore
90
+ - Gemfile
91
+ - Gemfile.lock
92
+ - LICENSE
93
+ - README.md
94
+ - bin/gt
95
+ - common/README
96
+ - common/env.yml
97
+ - common/plugins.yml
98
+ - common/plugins/SamplePlugin/config.yml
99
+ - common/plugins/SamplePlugin/plugin.rb
100
+ - common/user.yml
101
+ - gaptool.gemspec
102
+ - lib/app.rb
103
+ - lib/plugins/Base/config.yml
104
+ - lib/plugins/Base/init/ubuntu-12.04.erb
105
+ - lib/plugins/Base/log.yml
106
+ - lib/plugins/Base/plugin.rb
107
+ homepage: http://mdb.io
108
+ licenses: []
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 1.8.10
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: EC2 application deployment, initialization and configuration tool
131
+ test_files: []