capawsext 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 762e84774e300c85a73a55b0c6aea0c31fcad919
4
+ data.tar.gz: 148f79e31294a8ad2d0fae088761a8aa6614ab3e
5
+ SHA512:
6
+ metadata.gz: b694827848eae4a164ce673874358f4b2c4eef31c35ad50bb2532f0ec76b6c81153e4570a632696d1c545765332f29f7c6f8a6b967ceb5a0790d7a82ed175956
7
+ data.tar.gz: 859bfeccca344579b30fd280f7603c3e1d576c793829dfde0a5b9498621cde1b24c7c9309ff3fe6aca56ad213083e32095baab9d021aa3c7d284c49c9312c70d
data/README ADDED
@@ -0,0 +1,5 @@
1
+ #TODOs
2
+ * Load group name specific configuration
3
+ * Load dependent gems like fog, colored from the dependencies
4
+ * Push to github or git repo.
5
+
data/capawsext.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "capawsext/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "capawsext"
7
+ s.version = CapAwsExt::VERSION
8
+ s.authors = ["Sumit vij"]
9
+ s.email = ["sumit@whodini.com"]
10
+ s.homepage = "http://github.com/thedebugger/capawsext"
11
+ s.summary = %q{Import the server ips, roles, groups from AWS server tags}
12
+
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+
20
+ s.add_dependency "capistrano", "~> 2.0"
21
+ s.add_dependency "fog"
22
+ s.add_dependency "colored"
23
+ end
data/lib/capawsext.rb ADDED
@@ -0,0 +1,2 @@
1
+ module CapAwsExt
2
+ end
@@ -0,0 +1,46 @@
1
+ require 'capistrano'
2
+
3
+ unless Capistrano::Configuration.respond_to?(:instance)
4
+ abort "cap_git_tools requires Capistrano 2"
5
+ end
6
+
7
+ require 'capawsext/whoec2helper'
8
+
9
+ Capistrano::Configuration.instance(:must_exist).load do
10
+ def add_groups
11
+ ec2_helper = Ec2Helper.new
12
+ groups = ec2_helper.get_groups
13
+ groups.each { |group|
14
+ task group.to_sym, :desc => "Run the task in all instances of the #{group}" do
15
+ instances = ec2_helper.get_instances(group)
16
+ #Initialize the global variables
17
+ sec_groups = Set.new
18
+ instances.each {|instance|
19
+ instance.groups.each { |sec_group|
20
+ sec_groups << sec_group
21
+ }
22
+
23
+ roles = Array.new
24
+ instance.tags['role'].split(',').each {|role|
25
+ roles << role.to_sym
26
+ }
27
+ server instance.dns_name, *roles
28
+ }
29
+ init_group_global_variables(group,sec_groups)
30
+ end
31
+ }
32
+ end
33
+
34
+ def init_group_global_variables(group, sec_groups)
35
+ if sec_groups.count != 1
36
+ raise "#{sec_groups} security defined in this group"
37
+ end
38
+ set :security_group, sec_groups.to_a[0]
39
+ set :group, group
40
+ #Load the file is exists
41
+ load "config/#{group}" if File.exists?(File.expand_path(Dir.pwd + "/config/#{group}.rb"))
42
+ #TODO: call the group name configuration file to load group configuration.
43
+ #Like the capmultiext
44
+
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module CapAwsExt
2
+ VERSION="0.1"
3
+ end
@@ -0,0 +1,201 @@
1
+ require 'rubygems'
2
+ require 'fog'
3
+ require 'colored'
4
+ #require File.expand_path(File.dirname(__FILE__) + '/capify-cloud/server')
5
+
6
+ class Ec2Helper
7
+
8
+ def initialize(cloud_config = "config/cloud.yml")
9
+ @cloud_config = YAML.load_file cloud_config
10
+ @instances =[]
11
+ @cloud_providers = @cloud_config[:cloud_providers]
12
+ @cloud_providers.each do |cloud_provider|
13
+ config = @cloud_config[cloud_provider.to_sym]
14
+ config[:provider] = cloud_provider
15
+ regions = determine_regions(cloud_provider)
16
+ config.delete(:regions)
17
+ if regions
18
+ regions.each do |region|
19
+ config.delete(:region)
20
+ config[:region] = region
21
+ populate_instances(config)
22
+ end
23
+ else populate_instances(config)
24
+ end
25
+ end
26
+
27
+ end
28
+ def determine_regions(cloud_provider = 'AWS')
29
+ regions = @cloud_config[cloud_provider.to_sym][:regions]
30
+ end
31
+
32
+
33
+ def populate_instances(config)
34
+ @fog = Fog::Compute.new(config)
35
+ @fog.servers.each {|server| @instances << server }
36
+ end
37
+
38
+ def get_zoo_srvs_private_dns(group)
39
+ zoosrvs = Array.new
40
+ instances = get_instances_byrole(group, 'zoo-srv')
41
+ instances.each {|instance|
42
+ zoosrvs << instance.private_dns_name
43
+ }
44
+ return zoosrvs
45
+ end
46
+
47
+ def get_instances_byrole(group, role)
48
+ ret_instances = Array.new
49
+ @instances.each {|instance|
50
+ if not instance.tags['role'].nil? and instance.ready?
51
+ unless instance.tags['role'].match(role).nil? or instance.tags['group'] != group
52
+ ret_instances << instance
53
+ end
54
+ end
55
+ }
56
+ return ret_instances
57
+ end
58
+
59
+ def get_solr_private_dns(group)
60
+ instances = get_instances_byrole(group, 'solr')
61
+ return instances[0].private_dns_name if instances.count > 0
62
+ end
63
+
64
+
65
+ def get_facade_srv_public_dns(group)
66
+ instance = get_instances_byrole(group, 'api-srv')
67
+ return instance[0].dns_name if instance.count > 0
68
+ end
69
+
70
+ def get_db_srv_private_dns(group)
71
+ instance = get_instances_byrole(group, 'db-srv')
72
+ return instance[0].private_dns_name if instance.count > 0
73
+ end
74
+
75
+ def printInstanceDetails()
76
+ @instances.each_with_index do |instance, i|
77
+ puts sprintf "%02d: %-20s %-20s %-20s %-20s %-25s %-20s (%s) (%s) (%s)",
78
+ i, (instance.tags["Name"] || "").green,instance.private_dns_name ,instance.id.red, instance.flavor_id.cyan,
79
+ instance.dns_name.blue, instance.availability_zone.magenta, (instance.tags["role"] || "").yellow,
80
+ (instance.tags["group"] || "").yellow, (instance.tags["app"] || "").green if instance.ready?
81
+ end
82
+
83
+ end
84
+
85
+ def get_server_details_by_pri_pub_dns(dns)
86
+ details={}
87
+ @instances.each { |instance|
88
+ if instance.private_dns_name == dns or instance.dns_name == dns
89
+ details['name']=instance.tags['name_s']
90
+ details['orgid']=instance.tags['orgid']
91
+ details['group']=instance.tags['group']
92
+ details['private_dns_name'] = instance.private_dns_name
93
+ details['role'] = instance.tags['role']
94
+ details['public_dns_name'] = instance.dns_name
95
+ details['id'] = instance.id
96
+ details['zone'] = instance.availability_zone
97
+ return details
98
+ end
99
+ }
100
+ return details
101
+ end
102
+
103
+ def get_server_details_map(isrunning)
104
+ servers={}
105
+ @instances.each { |instance|
106
+ if not isrunning or (isrunning and instance.ready?)
107
+ details={}
108
+ details['name']=instance.tags['name_s']
109
+ details['orgid']=instance.tags['orgid']
110
+ details['group']=instance.tags['group']
111
+ details['private_dns_name'] = instance.private_dns_name
112
+ details['role'] = instance.tags['role']
113
+ details['public_dns_name'] = instance.dns_name
114
+ details['id'] = instance.id
115
+ details['zone'] = instance.availability_zone
116
+ details['device_mappings'] = Array.new
117
+ instance.block_device_mapping.each { |mapping|
118
+ details['device_mappings'] << mapping['deviceName']
119
+ }
120
+ servers[instance.private_dns_name] = details
121
+ end
122
+ }
123
+ return servers
124
+ end
125
+
126
+ def get_all_stacks
127
+ stacks = {}
128
+ @instances.each {|instance|
129
+ instances = stacks[instance.tags['group']]
130
+ if instances.nil?
131
+ instances = Array.new
132
+ stacks[instance.tags['group']] = instances
133
+ end
134
+ instances << instance
135
+ }
136
+ return stacks
137
+ end
138
+
139
+
140
+ def get_instance_on_dns(dns)
141
+ @instances.each { |instance|
142
+ if instance.private_dns_name == dns or instance.dns_name == dns
143
+ return instance
144
+ end
145
+ }
146
+ end
147
+
148
+ def get_groups
149
+ groups = Set.new
150
+ @instances.each { |instance|
151
+ group = instance.tags['group']
152
+ groups.add(group) if not group.nil?
153
+ }
154
+ return groups
155
+ end
156
+
157
+ def get_instances(group)
158
+ ret_instances = Array.new
159
+ @instances.each { |instance|
160
+ ret_instances << instance if instance.tags['group'] == group and instance.ready?
161
+ }
162
+ return ret_instances
163
+ end
164
+
165
+ def get_instance_by_name(name)
166
+ @instances.each {|instance|
167
+ return instance if (instance.tags['name_s'] || "").casecmp(name) == 0
168
+ return instance if (instance.tags['Name'] || "").casecmp(name) == 0
169
+ }
170
+ return nil
171
+ end
172
+
173
+ def get_zone(roles, sec_group)
174
+ zones = Hash["us-west-1b" =>0,"us-west-1c" =>0]
175
+ @instances.each { |instance|
176
+ if instance.tags['role'] == roles and instance.security_group_ids.include?(sec_group)
177
+ zones[instance.availability_zone] = zones[instance.availability_zone] + 1
178
+ end
179
+ }
180
+ min_zone = "us-west-1b"
181
+ zones.keys.each{|key|
182
+ if zones[min_zone] > zones[key]
183
+ min_zone = key
184
+ end
185
+ }
186
+ return min_zone
187
+ end
188
+
189
+
190
+ def add_tags(resource_id, tags = {})
191
+ tags.each { |key, value|
192
+ puts "#{resource_id}, #{key}, #{value}"
193
+ @fog.tags.create(:resource_id => resource_id, :key => key, :value => value)
194
+ }
195
+ end
196
+
197
+ def get_all_instances
198
+ return @instances
199
+ end
200
+ end
201
+
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capawsext
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Sumit vij
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: capistrano
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fog
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: colored
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - sumit@whodini.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README
63
+ - capawsext.gemspec
64
+ - lib/capawsext.rb
65
+ - lib/capawsext/tasks.rb
66
+ - lib/capawsext/version.rb
67
+ - lib/capawsext/whoec2helper.rb
68
+ homepage: http://github.com/thedebugger/capawsext
69
+ licenses: []
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.0.0.rc.2
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Import the server ips, roles, groups from AWS server tags
91
+ test_files: []