iravat 0.1.0 → 0.1.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.
@@ -10,14 +10,28 @@
10
10
  <Port>443</Port>
11
11
  <Protocol>https</Protocol>
12
12
  <MultiThread>false</MultiThread>
13
- <NamePrefix>ChipMonk</NamePrefix>
14
- <NameSuffix>S3</NameSuffix>
13
+ <NamePrefix>chipmonk</NamePrefix>
14
+ <NameSuffix>s3</NameSuffix>
15
15
  <Delimeter>-</Delimeter>
16
16
  </S3>
17
17
 
18
18
  <EC2>
19
19
  <NamePrefix>ChipMonk></NamePrefix>
20
- <NameSuffix>S3</NameSuffix>
20
+ <NameSuffix>S3</NameSuffix>
21
+
22
+ <!--TODO: These can be made per region configurations -->
23
+ <KeyNameToUse>iravat_test_key</KeyNameToUse>
24
+ <SecurityGroupToUse>quick-start-1</SecurityGroupToUse>
25
+
26
+ <!--Initializer related configuration of EC2 -->>
27
+ <MinWaitToRetryToCheckRunningInSeconds>20</MinWaitToRetryToCheckRunningInSeconds>
28
+ <MaxRetriesToCheckRunning>30</MaxRetriesToCheckRunning>
29
+ <DefaultUserIDToLogIn>ubuntu</DefaultUserIDToLogIn>
30
+ <SSHKeyPathToLogin>/etc/conf/chipmonk/keys/ec2/iravat_test_key.pem</SSHKeyPathToLogin>
31
+ <BundlePathToCopy>/etc/conf/chipmonk/bundle</BundlePathToCopy>
32
+ <BundleInitScriptToExecute>./ec2_initializer_script.sh</BundleInitScriptToExecute>
33
+
34
+ <S3ToolInstanceName>chipmonk-tools-s3</S3ToolInstanceName>
21
35
  </EC2>
22
36
 
23
37
  <LogFile>
@@ -10,6 +10,8 @@ class IrEC2
10
10
  awsConfig.loadConfigurationFile()
11
11
  @awsKeyID=awsConfig.getPathFromXML("/ChipMonkAWSConfigs/AWSAccessKey/AWSAccessKey")
12
12
  @awsSecretKey=awsConfig.getPathFromXML("/ChipMonkAWSConfigs/AWSAccessKey/AWSAccessSecretKey")
13
+ @awsKeyToUse=awsConfig.getPathFromXML("/ChipMonkAWSConfigs/EC2/KeyNameToUse")
14
+ @awsSecurityGroupToUse=awsConfig.getPathFromXML("/ChipMonkAWSConfigs/EC2/SecurityGroupToUse")
13
15
  end
14
16
 
15
17
 
@@ -38,25 +40,29 @@ class IrEC2
38
40
 
39
41
  def getKey()
40
42
  begin
41
- key = @ec2.describe_key_pairs()[0][:aws_key_name]
42
- if(key.nil?)
43
- @logger.error("No Key pair existing")
43
+ keyPairs= @ec2.describe_key_pairs([@awsKeyToUse])
44
+ if(keyPairs.nil? or keyPairs.length <= 0)
45
+ @logger.error("No Key pair existing for mentioned key name " + @awsKeyToUse)
46
+ return nil
44
47
  end
45
- return key
46
- rescue RightAws::AwsError=>err
48
+
49
+ return @awsKeyToUse
50
+ rescue RightAws::AwsError=>err
47
51
  self.error_ec2(err)
48
52
  return nil
49
- end
53
+ end
50
54
  end
51
55
 
52
56
 
53
57
  def getSecurityGroup()
54
58
  begin
55
- securityGroup = @ec2.describe_key_pairs()[0]
56
- if(securityGroup.nil?)
59
+ securityGroups = @ec2.describe_security_groups([@awsSecurityGroupToUse])
60
+ if(securityGroups.nil? or securityGroups.length <=0 )
57
61
  @logger.error("No security group created")
62
+ return nil
58
63
  end
59
- return securityGroup
64
+
65
+ return [@awsSecurityGroupToUse]
60
66
  rescue RightAws::AwsError=>err
61
67
  self.error_ec2(err)
62
68
  return nil
@@ -110,7 +116,7 @@ class IrEC2
110
116
  responseDetails=@ec2.run_instances(imageID,
111
117
  1,
112
118
  1,
113
- self.getSecurityGroup()[:aws_group_name],
119
+ self.getSecurityGroup(),
114
120
  self.getKey(),
115
121
  userCookieData,
116
122
  nil,
@@ -178,10 +184,10 @@ if __FILE__ == $0
178
184
  #@logger=$ir_logger.getLog4rLogger()
179
185
  ec2Manager=IrEC2.new
180
186
  if(ec2Manager.connectToAWS() == true)
181
- ec2Manager.printAllActiveImages()
182
- instanceID=ec2Manager.createNewInstance("ami-00d62369", "t1.micro","us-east-1b","Sudhanshu-Started")
183
- ec2Manager.terminateInstance(instanceID)
184
- ec2Manager.printAllActiveImages()
187
+ #ec2Manager.printAllActiveImages()
188
+ instanceID=ec2Manager.createNewInstance('ami-3ef00d57', 't1.micro','us-east-1b','Sudhanshu-Started')
189
+ #ec2Manager.terminateInstance(instanceID)
190
+ #ec2Manager.printAllActiveImages()
185
191
  end
186
192
 
187
193
 
@@ -0,0 +1,189 @@
1
+ module Iravat
2
+ #require '../iravat.rb'
3
+ #require './ir_module'
4
+
5
+ class IrEC2InstanceInitializer
6
+
7
+ def initialize(instanceIDToCheck)
8
+ @logger = IrLogger.new(self.class.name).getLog4rLogger()
9
+ awsConfig=IrAwsConfigManager.new(IRAVAT_AWS_CONFIG_XML_PATH)
10
+ awsConfig.loadConfigurationFile()
11
+ @awsKeyID=awsConfig.getPathFromXML("/ChipMonkAWSConfigs/AWSAccessKey/AWSAccessKey")
12
+ @awsSecretKey=awsConfig.getPathFromXML("/ChipMonkAWSConfigs/AWSAccessKey/AWSAccessSecretKey")
13
+ @maxRetries=Integer(awsConfig.getPathFromXML("/ChipMonkAWSConfigs/EC2/MaxRetriesToCheckRunning"))
14
+ @minRetryInterval=Integer(awsConfig.getPathFromXML("/ChipMonkAWSConfigs/EC2/MaxRetriesToCheckRunning"))
15
+ @sshKeyPath=awsConfig.getPathFromXML("/ChipMonkAWSConfigs/EC2/SSHKeyPathToLogin")
16
+ @dirCopyPath=awsConfig.getPathFromXML("/ChipMonkAWSConfigs/EC2/BundlePathToCopy")
17
+ @scriptToExecute=awsConfig.getPathFromXML("/ChipMonkAWSConfigs/EC2/BundleInitScriptToExecute")
18
+ @s3ToolMountS3=awsConfig.getPathFromXML("/ChipMonkAWSConfigs/EC2/S3ToolInstanceName")
19
+ @instanceID=instanceIDToCheck
20
+ @logger.info("Checking for instance id" + instanceIDToCheck)
21
+ end
22
+
23
+ #common error handler for RightScale Lib
24
+ def error_ec2 (err)
25
+ err.errors.each do |e|
26
+ @logger.error(err.errors)
27
+ end
28
+ end
29
+
30
+ def waitForEC2InstanceToGetRunning()
31
+ #return true when successful
32
+ #return false when unsuccessful and log the error
33
+ #errors can be
34
+ # No instance found for the instance ID
35
+ # Unable to connect to Amazon with the mentioned accesskey
36
+ # Able to connect to AWS but instance state is not running
37
+ begin
38
+ ec2 = RightAws::Ec2.new(@awsKeyID,@awsSecretKey)
39
+ if(ec2.nil?)
40
+ @logger.error("Failed to create an access to EC2")
41
+ return false
42
+ end
43
+ i = 0
44
+ while i < @maxRetries
45
+ awsInstances=ec2.describe_instances([@instanceID])
46
+ if awsInstances.nil?
47
+ @logger.error("Failed to initialize the instance " + @instanceID +
48
+ " instance details could not be fetched ")
49
+ return false
50
+ end
51
+ if awsInstances.length <= 0
52
+ @logger.error("Failed to initialize the instance " + @instanceID +
53
+ " instance details could not be correctly fetched ")
54
+ return false
55
+ end
56
+
57
+
58
+ awsInstances.each do |awsInstance|
59
+ if( awsInstance[:aws_state] == "running" and awsInstance[:aws_instance_id] == @instanceID)
60
+ @publicDomainName=awsInstance[:dns_name]
61
+ #we are done as we have got the state as running from AWS
62
+ return true
63
+ end
64
+ end
65
+ @logger.debug ("put to retry" )
66
+ sleep @minRetryInterval
67
+ end
68
+ @logger.error("Failed to get the instance " + @instanceID + " running!!!")
69
+ return false
70
+
71
+ rescue RightAws::AwsError => err
72
+ self.error_ec2(err)
73
+ return false
74
+ end
75
+ end
76
+
77
+ def initializationSetupOnEC2(userName, password, s3ToolStorageName)
78
+ end
79
+
80
+
81
+ def initializationSetupOnEC2(userName, password, s3UserStorageName)
82
+ @logger.debug("Copying to server " + @publicDomainName)
83
+ @logger.debug("Local Path " + @dirCopyPath)
84
+ @logger.debug("Key Path " + @sshKeyPath)
85
+ retries=0
86
+ max_retries=5
87
+ defaults= {:keys => [@sshKeyPath], :timeout =>10 }
88
+ defaults_passwd= {:password => password, :timeout =>10 }
89
+
90
+ while retries < max_retries
91
+ begin
92
+ Net::SCP.start(@publicDomainName, 'ubuntu', defaults) do |scp|
93
+ # upload a file to a remote server
94
+ scp.upload! @dirCopyPath, '/home/ubuntu/', :recursive => true
95
+ end
96
+ @logger.debug("Successful copy of bundle to server " + @publicDomainName)
97
+ break
98
+ rescue
99
+ retries = retries + 1
100
+ sleep 15
101
+ @logger.debug("Failed on copy of bundle to server " + @publicDomainName + ". Going for retry")
102
+ if retries == max_retries
103
+ @logger.error("All attempts to copy files to server failed" + @publicDomainName)
104
+ end
105
+ end
106
+ end
107
+ retries=0
108
+ @logger.debug @scriptToExecute + " " + userName + " " + password
109
+
110
+ while retries < max_retries
111
+ begin
112
+ Net::SSH.start(@publicDomainName,'ubuntu', defaults)do |ssh|
113
+ result = ssh.exec!('cd bundle;' + @scriptToExecute + ' ' + userName + ' ' + password + ';touch /home/ubuntu/patched_server' )
114
+ end
115
+ @logger.debug("Successful running init script on server " + @publicDomainName)
116
+ break
117
+ rescue
118
+ retries = retries + 1
119
+ sleep 15
120
+ @logger.debug("Failed to run init script on server " + @publicDomainName + ". Going for retry")
121
+ if retries == max_retries
122
+ @logger.error("All attempts run init script on server failed." + @publicDomainName)
123
+ end
124
+ end
125
+ end
126
+
127
+ retries=0
128
+ while retries < max_retries
129
+ begin
130
+ Net::SSH.start(@publicDomainName, userName, defaults_passwd)do |ssh|
131
+ command = 'export AWSACCESSKEYID=' + @awsKeyID + ';export AWSSECRETACCESSKEY=' + @awsSecretKey + ';s3fs -o allow_other ' + s3UserStorageName + ' ' + '/home/' + userName + '/IravatSharedData' + ';s3fs -o allow_other ' + @s3ToolMountS3 + ' ' + '/iravat'
132
+ @logger.debug(command)
133
+
134
+ result = ssh.exec!( command )
135
+ end
136
+ @logger.debug("Successful running init script as user server " + @publicDomainName)
137
+ break
138
+ rescue
139
+ retries = retries + 1
140
+ sleep 15
141
+ @logger.debug("Failed to run init script as user on server " + @publicDomainName + ". Going for retry")
142
+ if retries == max_retries
143
+ @logger.error("All attempts run init script as user server failed." + @publicDomainName)
144
+ end
145
+ end
146
+ end
147
+
148
+
149
+
150
+
151
+ return @publicDomainName
152
+ end
153
+
154
+
155
+ end
156
+
157
+ if __FILE__ == $0
158
+ username='iravat_test'
159
+ password='iravat'
160
+ instancename='ami-7632ce1f'
161
+ instancesize='t1.micro'
162
+ instancelocation='us-east-1b'
163
+ instance_user_info='Sudhanshu-Started'
164
+ ir = IrS3.new
165
+ ir.create_s3_bucket(username)
166
+ s3name = ir.get_bucket_name(username)
167
+
168
+ ec2Manager=IrEC2.new
169
+ if(ec2Manager.connectToAWS() == true)
170
+
171
+ #ec2Manager.printAllActiveImages()
172
+ instanceID=ec2Manager.createNewInstance(instancename,
173
+ instancesize,
174
+ instancelocation,
175
+ instance_user_info)
176
+ #ec2Manager.terminateInstance(instanceID)
177
+ #ec2Manager.printAllActiveImages()
178
+ ec2Init=IrEC2InstanceInitializer.new(instanceID)
179
+ if(ec2Init.waitForEC2InstanceToGetRunning())
180
+ ec2Init.initializationSetupOnEC2(username, password, s3name)
181
+ else
182
+ puts("Failed to get instance running")
183
+ end
184
+ end
185
+
186
+
187
+ end
188
+
189
+ end
@@ -12,6 +12,8 @@ require 'logger/ir_log_initializer'
12
12
  require 'awsconf/ir_aws_config_manager'
13
13
  require 's3/ir_s3'
14
14
  require 'ec2/ir_ec2'
15
+ require 'ec2/ir_ec2_instance_initializer'
16
+
15
17
 
16
18
  module Iravat
17
19
 
@@ -48,7 +48,6 @@
48
48
  <outputter>file_outputter</outputter>
49
49
  </logger>
50
50
  </log4r_config>
51
- W, [2011-04-20T15:23:41.665759 #9565] WARN -- : Unable to initiate logging for module MasterLogger
52
51
 
53
52
  W, [2011-04-20T15:23:41.665927 #9565] WARN -- : Unable to initiate logging for module MasterLogger
54
53
 
@@ -53,11 +53,14 @@
53
53
  self.error_aws(s3,err)
54
54
  end
55
55
 
56
+ def get_bucket_name(username)
57
+ @name_prefix+@delimeter+username+@delimeter+@name_suffix
58
+ end
56
59
 
57
60
  def create_s3_bucket(username)
58
61
  s3 = RightAws::S3.new(@aws_access_key_id, @aws_secret_access_key,@s3config)
59
62
 
60
- bucketname = @name_prefix+@delimeter+username+@delimeter+@name_suffix
63
+ bucketname = self.get_bucket_name(username)
61
64
 
62
65
  @logger.info(bucketname)
63
66
 
@@ -92,7 +95,7 @@
92
95
  if __FILE__ == $0
93
96
  ir = IrS3.new
94
97
 
95
- ir.create_s3_bucket("alok")
98
+ ir.create_s3_bucket("IravatInstances")
96
99
  ir.list_s3_bucket()
97
100
  #ir.delete_s3_bucket("iravattest")
98
101
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iravat
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - alok
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-23 00:00:00 +05:30
18
+ date: 2011-05-09 00:00:00 +05:30
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -91,6 +91,7 @@ files:
91
91
  - lib/awsconf/aws_config.xml
92
92
  - lib/awsconf/ir_aws_config_manager.rb
93
93
  - lib/ec2/ir_ec2.rb
94
+ - lib/ec2/ir_ec2_instance_initializer.rb
94
95
  - lib/iravat.rb
95
96
  - lib/logger/ir_log_initializer.rb
96
97
  - lib/logger/ir_logger.rb