Class: Tunl::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tunl.rb

Instance Method Summary collapse

Instance Method Details

#authorize_security_group(name) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/tunl.rb', line 99

def authorize_security_group(name)
	[ 22, 80 ].each do |port|
		begin
			ec2.authorize_security_group_IP_ingress("tunl_#{name}", port, port)
		rescue Aws::AwsError => e
			if e.message =~ /already been authorized/
				# do nothing
			else
				raise e
			end
		end
	end
end

#configObject



43
44
45
# File 'lib/tunl.rb', line 43

def config
	@config ||= YAML.load(File.read(root.join('tunl.yml')))
end

#create_instance(name) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/tunl.rb', line 135

def create_instance(name)
	# launch instance with:
	#  os - ubuntu hardy 32 bit
	#  security_group: tunl_name
	#  key_pair: tunl_name
	log "launch", "instance ..."
	instances = ec2.launch_instances('ami-ed46a784',
		:key_name => "tunl_#{name}",
		:group_ids => ["tunl_#{name}"],
		:instance_type => 'm1.small',
		:availability_zone => 'us-east-1a',
		:user_data => %Q{#!/bin/sh
			echo "starting" > /tmp/user_data_status.txt
			echo "GatewayPorts clientspecified" >> /etc/ssh/sshd_config
			/etc/init.d/ssh restart
			echo "finished" > /tmp/user_data_status.txt
		})
	log "", "#{instances.first[:aws_instance_id]}"	
end

#create_key_pair(name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tunl.rb', line 55

def create_key_pair(name)
	log "create", "key pair tunl_#{name}"
	filepath = key_pair_filepath(name)
	key_pair = ec2.create_key_pair("tunl_#{name}")
	File.open(filepath, 'w') { |f| f.write(key_pair[:aws_material]) }
	FileUtils.chmod 0400, filepath
rescue Aws::AwsError => e
   if e.message =~ /already exists/
     # WARNING : this could be dangerous.
     delete_key_pair(name)
     retry
   else
     raise e
   end
end

#create_security_group(name) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/tunl.rb', line 88

def create_security_group(name)
	log "create", "security group tunl_#{name}"
	ec2.create_security_group("tunl_#{name}", 'created by tunl gem')
rescue Aws::AwsError => e
	if e.message =~ /already exists/
		return true
	else
		raise e
	end
end

#delete_instance(name) ⇒ Object



179
180
181
182
183
# File 'lib/tunl.rb', line 179

def delete_instance(name)
	instance = find_instance(name)
 	log "delete", "instance tunl_#{name} with id #{instance[:aws_instance_id]}"
	ec2.terminate_instances([instance[:aws_instance_id]])
end

#delete_key_pair(name) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/tunl.rb', line 71

def delete_key_pair(name)
	log "delete", "key Pair tunl_#{name}"
	# delete key pair from amazon
	ec2.delete_key_pair("tunl_#{name}")
	# delete local file
	filepath = key_pair_filepath(name)
	if File.exists?(filepath)
		FileUtils.rm_f(filepath)
	end
end

#delete_security_group(name) ⇒ Object



113
114
115
116
# File 'lib/tunl.rb', line 113

def delete_security_group(name)
	log "delete", "security group tunl_#{name}"
	ec2.delete_security_group("tunl_#{name}")
end

#ec2Object



47
48
49
# File 'lib/tunl.rb', line 47

def ec2
	@ec2 ||= Aws::Ec2.new(config['aws_access_key'],config['aws_secret_key'], :logger => SilentLogger)
end

#find_all_instancesObject



195
196
197
# File 'lib/tunl.rb', line 195

def find_all_instances
	ec2.describe_instances.find_all { |item| item[:aws_groups].first.include?('tunl_') and ['running', 'pending'].include?(item[:aws_state]) }
end

#find_instance(name) ⇒ Object



131
132
133
# File 'lib/tunl.rb', line 131

def find_instance(name)
	ec2.describe_instances.find { |item| item[:aws_groups].include?("tunl_#{name}") and (item[:aws_state].include?("pending") or item[:aws_state].include?("running")) }
end

#find_security_group(name) ⇒ Object



118
119
120
# File 'lib/tunl.rb', line 118

def find_security_group(name)
	ec2.describe_security_groups.find { |item| item[:aws_group_name]=="tunl_#{name}" }
end

#key_pair_filepath(name) ⇒ Object



51
52
53
# File 'lib/tunl.rb', line 51

def key_pair_filepath(name)
	root.join("tunl_key_pair_#{name}")
end

#log(action, message = '') ⇒ Object



199
200
201
# File 'lib/tunl.rb', line 199

def log(action,message='')
	$stdout.printf("%-3s %-8s %-12s\n", '', action, message)
end

#rootObject



39
40
41
# File 'lib/tunl.rb', line 39

def root
	@root ||= Pathname.new(File.expand_path("~/.tunl"))
end

#setup_instance(name) ⇒ Object



185
186
187
188
189
190
191
192
193
# File 'lib/tunl.rb', line 185

def setup_instance(name)
	instance = find_instance(name)
	if instance.nil?
		create_instance(name)
	else
		log "connect", "instance #{instance[:aws_instance_id]}"
	end
	wait_for_instance(name)
end

#setup_key_pair(name) ⇒ Object



82
83
84
85
86
# File 'lib/tunl.rb', line 82

def setup_key_pair(name)
	if ! File.exists?(key_pair_filepath(name))
		create_key_pair(name)
	end
end

#setup_security_group(name) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/tunl.rb', line 122

def setup_security_group(name)
	# returns the names of all existing security groups prefixed with tunl_
	group = find_security_group(name)
	if group.nil?
		create_security_group(name)
		authorize_security_group(name)
	end
end

#wait_for_instance(name) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/tunl.rb', line 155

def wait_for_instance(name)
	log "", "waiting for dns ..."
	while true
		break if ( find_instance(name) and find_instance(name)[:dns_name].size>0 )
		sleep(2)
	end
	log "", "done: #{find_instance(name)[:dns_name]}"
	require 'socket'     
	dns_name = find_instance(name)[:dns_name]
	log "", "waiting for ssh"
	loop do
		begin
			Timeout::timeout(4) do
				TCPSocket.new(dns_name, 22)
				log "", "done!"
				log ""
				return
			end
		rescue SocketError, Timeout::Error, Errno::ECONNREFUSED, Errno::EHOSTUNREACH
			sleep(2)
		end
	end
end