hugo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,112 @@
1
+ module Hugo
2
+ module Aws
3
+ class Ec2
4
+
5
+ ACCESS_KEY = ENV['AMAZON_ACCESS_KEY_ID']
6
+ SECRET_KEY = ENV['AMAZON_SECRET_ACCESS_KEY']
7
+ KEY_NAME = ENV['KEY_NAME']
8
+
9
+ AMI = ENV['EC2_AMI_ID'] || 'ami-1515f67c'
10
+ ZONE = "us-east-1c"
11
+ TYPE = "m1.small"
12
+
13
+ attr_accessor :name, :uri, :type, :zone, :image_id, :key_name, :create_time, :status, :security_group
14
+
15
+ def initialize(options = {})
16
+ set_attributes(options)
17
+ end
18
+
19
+ def set_attributes(options = {})
20
+ @name = options["instanceId"]
21
+
22
+ if options["placement"] and options["placement"]["availabilityZone"]
23
+ @zone = options["placement"]["availabilityZone"]
24
+ else
25
+ @zone = ZONE
26
+ end
27
+
28
+ @uri = options["dnsName"] || ""
29
+ @type = options["instanceType"] || TYPE
30
+ @image_id = options["imageId"] || AMI
31
+ @create_time = options["launchTime"] || nil
32
+
33
+ @key_name = options[:key_name] || options["keyName"] || KEY_NAME
34
+ if options["instanceState"] and options["instanceState"]["name"]
35
+ @status = options["instanceState"]["name"]
36
+ else
37
+ @status = "unknown"
38
+ end
39
+
40
+ # @security_group = options[:security_group] || nil
41
+ # if options["groupSet"] and options["groupSet"]["item"] and options["groupSet"]["item"][0]
42
+ # @security_group = options["groupSet"]["item"][0]["groupId"]
43
+ # end
44
+
45
+ end
46
+
47
+
48
+ def create
49
+ @ec2 = AWS::EC2::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
50
+ result = @ec2.run_instances(:image_id => self.image_id, :key_name => self.key_name,
51
+ :max_count => 1,
52
+ :availability_zone => self.zone) unless self.create_time
53
+ set_attributes(result.instancesSet.item[0]) if result.instancesSet.item[0]
54
+ self
55
+ end
56
+
57
+ def destroy
58
+ @ec2 = AWS::EC2::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
59
+ @ec2.terminate_instances(:instance_id => self.name)
60
+ end
61
+
62
+ def save
63
+ self.create
64
+ end
65
+
66
+ def ssh(commands, dna=nil, key_pair_file=nil)
67
+ raise ArgumentError.new("Key Pair File is required") if key_pair_file.nil?
68
+ Net::SSH.start(self.uri, "ubuntu", :keys => key_pair_file) do |ssh|
69
+ if dna
70
+ ssh.exec!("echo \"#{dna.to_json.gsub('"','\"')}\" > ~/dna.json")
71
+ end
72
+ commands.each do |cmd|
73
+ puts ssh.exec!(cmd)
74
+ end
75
+ end
76
+ end
77
+
78
+ def self.find_or_create_security_group(name, description)
79
+ @ec2 = AWS::EC2::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
80
+ begin
81
+ @security_groups = @ec2.describe_security_groups(:group_name => name)
82
+ rescue
83
+ @security_groups = @ec2.create_security_group(:group_name => name, :group_description => description)
84
+ end
85
+ @security_groups
86
+ end
87
+
88
+ def self.destroy_security_group(name)
89
+ @ec2 = AWS::EC2::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
90
+ @ec2.delete_security_group(:group_name => name)
91
+ end
92
+
93
+ def self.all
94
+ @ec2 = AWS::EC2::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
95
+ @ec2.describe_instances().reservationSet.item[0].instancesSet.item.map { |i| self.new(i) }
96
+ end
97
+
98
+ def self.find(instance)
99
+ @ec2 = AWS::EC2::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
100
+ self.new(@ec2.describe_instances(:instance_id => instance).reservationSet.item[0].instancesSet.item[0])
101
+ end
102
+
103
+ def self.find_or_create(options)
104
+ if options[:name]
105
+ self.find(options[:name])
106
+ else
107
+ self.new(options).create
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,101 @@
1
+ module Hugo
2
+ module Aws
3
+ class Elb
4
+
5
+ ACCESS_KEY = ENV['AMAZON_ACCESS_KEY_ID']
6
+ SECRET_KEY = ENV['AMAZON_SECRET_ACCESS_KEY']
7
+ ZONES = ["us-east-1c"]
8
+ LISTENERS = [{"InstancePort"=>"8080", "Protocol"=>"HTTP", "LoadBalancerPort"=>"80"},
9
+ {"InstancePort"=>"8443", "Protocol"=>"TCP", "LoadBalancerPort"=>"443"}]
10
+
11
+ attr_accessor :name, :uri, :listeners, :instances, :zones, :create_time
12
+
13
+ def initialize(options = {} )
14
+ @name = options[:name] || options["LoadBalancerName"]
15
+
16
+ @uri = options["DNSName"] if options["DNSName"]
17
+
18
+ if options["Instances"] and options["Instances"]["member"]
19
+ @instances = options["Instances"]["member"].map { |i| i.InstanceId }
20
+ else
21
+ @instances = []
22
+ end
23
+ if options["AvailabilityZones"] and options["AvailabilityZones"]["member"]
24
+ @zones = options["AvailabilityZones"]["member"]
25
+ else
26
+ @zones = options[:zones] || ZONES
27
+ end
28
+ if options["Listeners"] and options["Listeners"]["member"]
29
+ @listeners = options["Listeners"]["member"]
30
+ else
31
+ @listeners = options[:listeners] || LISTENERS
32
+ end
33
+ if options["CreatedTime"]
34
+ @create_time = options["CreatedTime"]
35
+ end
36
+ end
37
+
38
+ def create
39
+ @elb = AWS::ELB::Base.new(:access_key_id => Hugo::Aws::Elb::ACCESS_KEY, :secret_access_key => Hugo::Aws::Elb::SECRET_KEY)
40
+ @elb.create_load_balancer(
41
+ :load_balancer_name => self.name,
42
+ :listeners => self.listeners,
43
+ :availability_zones => self.zones
44
+ ) unless self.create_time
45
+ self
46
+ end
47
+
48
+ def destroy
49
+ @elb = AWS::ELB::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
50
+ @elb.delete_load_balancer(:load_balancer_name => self.name)
51
+ end
52
+
53
+ def save
54
+ self.create
55
+ end
56
+
57
+ def add(instance)
58
+ @elb = AWS::ELB::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
59
+ @elb.register_instances_with_load_balancer(
60
+ :instances => [instance],
61
+ :load_balancer_name => @name)
62
+ @instances << instance
63
+ self
64
+ end
65
+
66
+ def remove(instance)
67
+
68
+ @elb = AWS::ELB::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
69
+ @elb.deregister_instances_from_load_balancer(
70
+ :instances => [instance],
71
+ :load_balancer_name => @name)
72
+ @instances = @instances - [instance]
73
+ self
74
+ end
75
+
76
+ def self.all
77
+ @elb = AWS::ELB::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
78
+ instances = @elb.describe_load_balancers().DescribeLoadBalancersResult.LoadBalancerDescriptions.member
79
+ instances.map { |i| self.new(i) }
80
+ end
81
+
82
+ def self.find(balancer)
83
+ @elb = AWS::ELB::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
84
+
85
+ result = nil
86
+
87
+ @elb.describe_load_balancers().DescribeLoadBalancersResult.LoadBalancerDescriptions.member.each do |m|
88
+ result = self.new(m) if m.LoadBalancerName == balancer
89
+ end
90
+
91
+ result
92
+ rescue
93
+ nil
94
+ end
95
+
96
+ def self.find_or_create(options)
97
+ self.find(options[:name]) || self.new(options).create
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,120 @@
1
+ module Hugo
2
+ module Aws
3
+ class Rds
4
+
5
+ ACCESS_KEY = ENV['AMAZON_ACCESS_KEY_ID']
6
+ SECRET_KEY = ENV['AMAZON_SECRET_ACCESS_KEY']
7
+ DEFAULT_SIZE = 5
8
+ INSTANCE_CLASS = "db.m1.small"
9
+ ZONE = "us-east-1c"
10
+
11
+ attr_accessor :db, :uri, :server, :user, :password, :instance_class,
12
+ :zone, :size, :status, :create_time, :db_security_group
13
+
14
+ def initialize(options={})
15
+ # create instance
16
+
17
+ @server = options[:server] || options["DBInstanceIdentifier"]
18
+ @db = options[:name] || options["DBName"]
19
+ @user = options[:user] || options["MasterUsername"]
20
+ @password = options[:password] if options[:password]
21
+ @size = options[:size] || options["AllocatedStorage"] || DEFAULT_SIZE
22
+ @instance_class = options[:instance_class] || options["DBInstanceClass"] || INSTANCE_CLASS
23
+ @zone = options[:zone] || options["AvailabilityZone"] || ZONE
24
+ @status = options["DBInstanceStatus"] || "pending"
25
+ @create_time = options["InstanceCreateTime"] || nil
26
+ if options["Endpoint"] and options["Endpoint"]["Address"]
27
+ @uri = options["Endpoint"]["Address"] || nil
28
+ end
29
+ @db_security_group = options[:db_security_group] || "default"
30
+ if options["DBSecurityGroups"] and options["DBSecurityGroups"]["DBSecurityGroup"]
31
+ @db_security_group = options["DBSecurityGroups"]["DBSecurityGroup"]["DBSecurityGroupName"]
32
+ end
33
+
34
+ end
35
+
36
+ def create
37
+ @rds = AWS::RDS::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
38
+
39
+ # TODO Create DB Security Group wheh Amazon Bug Fixed! -tnw
40
+ # if @db_security_group
41
+ # find_or_create_db_security_group(@db_security_group, @db_security_group)
42
+ # end
43
+
44
+ @rds.create_db_instance(
45
+ :db_instance_identifier => self.server,
46
+ :allocated_storage => self.size,
47
+ :db_instance_class => self.instance_class,
48
+ :engine => "MySQL5.1",
49
+ :master_username => self.user,
50
+ :master_user_password => self.password,
51
+ :db_name => self.db,
52
+ :availability_zone => self.zone
53
+ #, :db_security_groups => @db_security_group
54
+ ) unless self.create_time
55
+ self
56
+ end
57
+
58
+ def save
59
+ create
60
+ end
61
+
62
+ def destroy
63
+ @rds = AWS::RDS::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
64
+ @rds.delete_db_instance(:db_instance_identifier => self.server, :skip_final_snapshot => true)
65
+ end
66
+
67
+ def self.all
68
+ @rds = AWS::RDS::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
69
+ instances = @rds.describe_db_instances.DescribeDBInstancesResult.DBInstances.DBInstance
70
+
71
+ if instances.kind_of?(Array)
72
+ #instances.map { |i| self.get_from_aws(i) }
73
+ instances.map { |i| self.new(i) }
74
+ else
75
+ #self.get_from_aws(instances)
76
+ [ self.new(instances) ]
77
+ end
78
+ end
79
+
80
+ def self.find(instance)
81
+ # find instance
82
+ @rds = AWS::RDS::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
83
+ @rds_instance = @rds.describe_db_instances(:db_instance_identifier => instance)
84
+ instance_desc = @rds_instance.DescribeDBInstancesResult.DBInstances.DBInstance
85
+ # initialize Hugo::Rds Object with instance hash
86
+ self.new(instance_desc)
87
+ rescue
88
+ # AWS Can't find db instance called ????
89
+ nil
90
+ end
91
+
92
+ def self.find_or_create(options)
93
+ rds = self.find(options[:server]) || self.new(options).create
94
+ rds.password = options[:password]
95
+ rds
96
+ end
97
+
98
+ def authorize_security_group(db_sec, ec2_sec, owner_id)
99
+ @rds = AWS::RDS::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
100
+ @rds.authorize_db_security_group(:db_security_group_name => db_sec, :ec2_security_group_name => ec2_sec, :ec2_security_group_owner_id => owner_id)
101
+ end
102
+
103
+ def find_or_create_db_security_group(name, description)
104
+ @rds = AWS::RDS::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
105
+ begin
106
+ @security_groups = @rds.describe_db_security_groups(:db_security_group_name => name)
107
+ rescue
108
+ @security_groups = @rds.create_db_security_group(:db_security_group_name => name, :db_security_group_description => description)
109
+ end
110
+ puts @security_groups
111
+ @security_groups
112
+ end
113
+
114
+ def destroy_db_security_group(name)
115
+ @rds = AWS::RDS::Base.new(:access_key_id => ACCESS_KEY, :secret_access_key => SECRET_KEY)
116
+ @rds.delete_db_security_group(:db_security_group_name => name)
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,59 @@
1
+ module Hugo; end
2
+
3
+ class Hugo::Balancer
4
+ include Singleton
5
+ include Hugo::Mixin::ParamsValidate
6
+
7
+ DEFAULT_ZONE = 'us-east-1c'
8
+ DEFAULT_PORT = '8080'
9
+ DEFAULT_WEB = '80'
10
+ DEFAULT_TYPE = 'http'
11
+ DEFAULT_SSL_PORT = '8443'
12
+ DEFAULT_SSL_WEB = '443'
13
+
14
+ def initialize
15
+ zone DEFAULT_ZONE
16
+ port DEFAULT_PORT
17
+ web DEFAULT_WEB
18
+ ssl_port DEFAULT_SSL_PORT
19
+ ssl_web DEFAULT_SSL_WEB
20
+ type DEFAULT_TYPE
21
+ end
22
+
23
+ def deploy
24
+ Hugo::Aws::Elb.find_or_create(:name => name,
25
+ :zones => zone,
26
+ :listeners => [{ :instance_port => port, :protocol =>"HTTP", :load_balancer_port => web},
27
+ { :instance_port => ssl_port, :protocol =>"TCP", :load_balancer_port => ssl_web}]
28
+ )
29
+ end
30
+
31
+ def name(arg=nil)
32
+ set_or_return(:name, arg, :kind_of => [String])
33
+ end
34
+
35
+ def zone(arg=nil)
36
+ set_or_return(:zone, arg, :kind_of => [String])
37
+ end
38
+
39
+ def port(arg=nil)
40
+ set_or_return(:port, arg, :kind_of => [String])
41
+ end
42
+
43
+ def web(arg=nil)
44
+ set_or_return(:web, arg, :kind_of => [String])
45
+ end
46
+
47
+ def type(arg=nil)
48
+ set_or_return(:type, arg, :kind_of => [String])
49
+ end
50
+
51
+ def ssl_port(arg=nil)
52
+ set_or_return(:ssl_port, arg, :kind_of => [String])
53
+ end
54
+
55
+ def ssl_web(arg=nil)
56
+ set_or_return(:ssl_web, arg, :kind_of => [String])
57
+ end
58
+
59
+ end
@@ -0,0 +1,113 @@
1
+ module Hugo; end
2
+
3
+ class Hugo::Cloud
4
+ include Singleton
5
+ include Hugo::Mixin::ParamsValidate
6
+
7
+
8
+ def initialize
9
+
10
+ end
11
+
12
+ def database(db_name, &block)
13
+ database = Hugo::Database.instance
14
+
15
+ database.db_security_group name
16
+ database.server name
17
+ database.name db_name
18
+
19
+ database.instance_eval(&block) if block_given?
20
+ db database.deploy
21
+
22
+
23
+ end
24
+
25
+ def balancer(&block)
26
+ balancer = Hugo::Balancer.instance
27
+ balancer.name name
28
+ balancer.instance_eval(&block) if block_given?
29
+ lb balancer.deploy
30
+ end
31
+
32
+ def app(name, &block)
33
+ app_info = Hugo::App.instance
34
+ app_info.name name
35
+ app_info.lb lb
36
+ app_info.db db
37
+ app_info.cloud_name name
38
+ app_info.instance_eval(&block) if block_given?
39
+ cloud_app app_info
40
+
41
+ end
42
+
43
+ def deploy
44
+ if cloud_app
45
+ cloud_app.setup
46
+ cloud_app.deploy
47
+ end
48
+
49
+ end
50
+
51
+ def delete
52
+ [db, cloud_app, lb].each do |s|
53
+ s.destroy if s
54
+ end
55
+ end
56
+
57
+
58
+
59
+ def print
60
+ if db
61
+ puts <<REPORT
62
+ ------------------------
63
+ DATABASE: #{db.db}
64
+ User: #{db.user}
65
+ Password: #{db.password}
66
+ Uri: #{db.uri}
67
+ REPORT
68
+ end
69
+
70
+ if lb
71
+ puts <<REPORT
72
+ -----------------------
73
+ Balancer: #{lb.name}
74
+ Uri: #{lb.uri}
75
+ Servers: #{lb.instances.length}
76
+ REPORT
77
+
78
+ lb.instances.each do |i|
79
+ ec2 = Hugo::Aws::Ec2.find(i)
80
+ puts <<REPORT
81
+ -----------------------
82
+ Id: #{ec2.name}
83
+ Uri: #{ec2.uri}
84
+ Type: #{ec2.type}
85
+ Zone: #{ec2.zone}
86
+
87
+ REPORT
88
+ end
89
+ end
90
+
91
+ end
92
+
93
+ def name(arg=nil)
94
+ set_or_return(:name, arg, :kind_of => [String])
95
+ end
96
+
97
+ def db(arg=nil)
98
+ set_or_return(:db, arg, :kind_of => [Hugo::Aws::Rds])
99
+ end
100
+
101
+ def lb(arg=nil)
102
+ set_or_return(:lb, arg, :kind_of => [Hugo::Aws::Elb])
103
+ end
104
+
105
+ def cloud_app(arg=nil)
106
+ set_or_return(:cloud_app, arg, :kind_of => [Hugo::App])
107
+ end
108
+
109
+
110
+
111
+
112
+
113
+ end