veronic 0.0.8 → 0.0.9

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.
data/bin/veronic CHANGED
@@ -25,6 +25,7 @@ opt_parser = OptionParser.new do |opt|
25
25
  opt.separator " stop: stop box"
26
26
  opt.separator " start: start box"
27
27
  opt.separator " list: list boxes"
28
+ opt.separator " status: status boxes"
28
29
  opt.separator ""
29
30
  opt.separator "Options"
30
31
 
@@ -116,6 +117,8 @@ when "start"
116
117
  Veronic::Deployer.new(options).start
117
118
  when "list"
118
119
  Veronic::Deployer.new(options).instances_list
120
+ when "status"
121
+ puts Veronic::Deployer.new(options).status
119
122
  else
120
123
  puts opt_parser
121
124
  end
data/lib/config/config.rb CHANGED
@@ -26,10 +26,10 @@ module Veronic
26
26
  @ssl_version = options[:ssl_version] || config_from_file['ssl_version']
27
27
  @identity_file = options[:identity_file] || config_from_file['identity_file']
28
28
  @branch = options[:branch] || config_from_file['branch']
29
- @environment = options[:environment] || config_from_file['environment'] || 'beta'
29
+ @environment = options[:environment] || config_from_file['environment']
30
30
  @ssh_user = options[:ssh_user] || config_from_file['ssh_user'] || 'ubuntu'
31
31
  @ssh_port = options[:ssh_port] || config_from_file['ssh_port'] || 22
32
- @role = options[:role] || config_from_file['role'] || 'default'
32
+ @role = options[:role] || config_from_file['role']
33
33
  @flavor = options[:flavor] || config_from_file['flavor'] || 'm1.medium'
34
34
  @security_groups = [options[:security_groups]] || [config_from_file['security_groups']]
35
35
  @deploy_cmd = options[:deploy_cmd] || config_from_file['deploy_cmd'] || 'sudo chef-client'
@@ -22,7 +22,7 @@ module Provider
22
22
  Chef::Config[:validation_key] = config[:validation_key]
23
23
  Chef::Config[:chef_server_url] = config[:chef_server_url]
24
24
  Chef::Config[:ssl_version] = config[:ssl_version]
25
- Chef::Config[:log_level] = :debug
25
+ Chef::Config[:log_level] = config[:verbose]
26
26
  @knife = knife
27
27
  end
28
28
 
@@ -6,6 +6,8 @@ require 'chef/knife/ssh'
6
6
  require 'chef/knife/node_run_list_add'
7
7
  require 'chef/knife/core/bootstrap_context'
8
8
  require 'chef/knife/ec2_base'
9
+ require 'chef/node'
10
+
9
11
  require_relative 'rest_request'
10
12
 
11
13
  module Provider
@@ -27,6 +29,7 @@ module Provider
27
29
  @flavor = config[:flavor]
28
30
  @region = config[:region]
29
31
  @availability_zone = config[:availability_zone]
32
+ @verbose = config[:verbose]
30
33
  Chef::Config[:knife][:image] = @image
31
34
  Chef::Config[:knife][:aws_ssh_key_id] = @aws_ssh_key_id
32
35
  Chef::Config[:knife][:aws_access_key_id] = @access_key_id
@@ -39,56 +42,61 @@ module Provider
39
42
  def create
40
43
  puts "Creating ec2 server #{@name} ..."
41
44
 
42
- create = Chef::Knife::Ec2ServerCreate.new()
43
-
44
- create.config[:run_list] = [@roles]
45
- create.config[:image] = @image
46
- create.config[:flavor] = @flavor
47
- create.config[:security_groups] = @security_groups
48
- create.config[:ssh_user] = @ssh_user
49
- create.config[:ssh_port] = @ssh_port
50
- create.config[:chef_node_name] = @name
51
- create.config[:identity_file] = @identity_file
52
- create.config[:environment] = @environment
53
- create.config[:log_level] = :debug
54
-
55
- puts create.config
56
- create.run
45
+ node = Chef::Knife::Ec2ServerCreate.new()
46
+
47
+ node.config[:run_list] = [@roles]
48
+ node.config[:image] = @image
49
+ node.config[:flavor] = @flavor
50
+ node.config[:security_groups] = @security_groups
51
+ node.config[:ssh_user] = @ssh_user
52
+ node.config[:ssh_port] = @ssh_port
53
+ node.config[:chef_node_name] = @name
54
+ node.config[:identity_file] = @identity_file
55
+ node.config[:environment] = @environment
56
+ node.config[:log_level] = @verbose
57
+
58
+ puts node.config
59
+ node.run
57
60
  end
58
61
 
59
- def bootstrap
62
+ def bootstrap(recursive_count=0)
60
63
  puts "Bootstrapping ec2 server #{@name} ..."
61
64
 
62
- bootstrap = Chef::Knife::Ec2ServerCreate.new()
63
-
64
- bootstrap.config[:image] = @image
65
- bootstrap.config[:flavor] = @flavor
66
- bootstrap.config[:security_groups] = @security_groups
67
- bootstrap.config[:ssh_user] = @ssh_user
68
- bootstrap.config[:ssh_port] = @ssh_port
69
- bootstrap.config[:chef_node_name] = @name
70
- bootstrap.config[:identity_file] = @identity_file
71
- bootstrap.config[:environment] = @environment
72
- bootstrap.config[:log_level] = :debug
73
-
74
- puts bootstrap.config
75
- bootstrap.run
65
+ node = Chef::Knife::Ec2ServerCreate.new()
66
+
67
+ node.config[:image] = @image
68
+ node.config[:flavor] = @flavor
69
+ node.config[:security_groups] = @security_groups
70
+ node.config[:ssh_user] = @ssh_user
71
+ node.config[:ssh_port] = @ssh_port
72
+ node.config[:chef_node_name] = @name
73
+ node.config[:identity_file] = @identity_file
74
+ node.config[:environment] = @environment
75
+ node.config[:log_level] = @verbose
76
+
77
+ puts node.config
78
+ begin
79
+ node.run
80
+ rescue
81
+ self.destroy([node.server.id])
82
+ self.bootstrap(recursive_count+=1) if recursive_count < 3
83
+ end
76
84
  end
77
85
 
78
86
  def destroy(instance_ids = [])
79
87
  puts "Deleting ec2 server #{@name} ..."
80
88
 
81
- destroy = Chef::Knife::Ec2ServerDelete.new()
89
+ node = Chef::Knife::Ec2ServerDelete.new()
82
90
 
83
- destroy.config[:purge] = true
84
- destroy.config[:chef_node_name] = @name
85
- destroy.config[:yes] = true
86
- destroy.name_args = instance_ids
91
+ node.config[:purge] = true
92
+ node.config[:chef_node_name] = @name
93
+ node.config[:yes] = true
94
+ node.name_args = instance_ids
87
95
 
88
- puts destroy.config
89
- destroy.run
90
- destroy.destroy_item(Chef::Node, @name, "node")
91
- destroy.destroy_item(Chef::ApiClient, @name, "client")
96
+ puts node.config
97
+ node.run
98
+ node.destroy_item(Chef::Node, @name, "node")
99
+ node.destroy_item(Chef::ApiClient, @name, "client")
92
100
  end
93
101
 
94
102
  def set_role
@@ -97,13 +105,22 @@ module Provider
97
105
  node.run
98
106
  end
99
107
 
108
+ def set_environment
109
+ node = Chef::Node.new.tap do |n|
110
+ n.name( @name )
111
+ n.chef_environment( @environment )
112
+ end
113
+ node.save
114
+ puts "Environment: #{@environment}"
115
+ end
116
+
100
117
  def ssh(query, cmd_line, manual)
101
118
  knife_ssh = Chef::Knife::Ssh.new()
102
119
 
103
120
  knife_ssh.config[:manual] = manual
104
121
  knife_ssh.config[:ssh_user] = @ssh_user
105
122
  knife_ssh.config[:identity_file] = @identity_file
106
- knife_ssh.config[:log_level] = :debug
123
+ knife_ssh.config[:log_level] = @verbose
107
124
 
108
125
  knife_ssh.name_args = [query, cmd_line]
109
126
  sys_status = knife_ssh.run
@@ -23,7 +23,9 @@ module Provider
23
23
  end
24
24
 
25
25
  def instances
26
- @ec2.instances
26
+ AWS.memoize do
27
+ @instances ||= @ec2.instances
28
+ end
27
29
  end
28
30
 
29
31
  def ec2
@@ -5,32 +5,45 @@ module Provider
5
5
  def initialize(ec2, name)
6
6
  @ec2 = ec2
7
7
  @name = name
8
- @instance = @ec2.instances.select {|x| x.tags['Name'] == @name && x.status != :shutting_down && x.status != :terminated}.first
8
+ @instance = instance
9
9
  end
10
10
 
11
11
  def stop
12
12
  print "Stopping instance #{@name}..."
13
- @i = 0
14
- @instance.stop if @instance.status == :running
15
- while @instance.status != :stopped && @i < 40
16
- print "." ; sleep 3 ; @i += 1 || 1
13
+ if self.exist?
14
+ @instance.stop
15
+ @i = 0
16
+ while self.status != :stopped
17
+ if @i > 120
18
+ return false
19
+ end
20
+ print "." ; sleep 3 ; @i += 1
21
+ end
17
22
  end
18
23
  puts "\nInstance #{@name} is stopped"
19
24
  end
20
25
 
21
26
  def start
22
27
  print "Starting instance #{@name}..."
23
- @i = 0
24
- @instance.start if @instance.status == :stopped && @i < 40
25
- while @instance.status != :running
26
- print "." ; sleep 3 ; @i += 1 || 1
28
+ if self.exist?
29
+ while self.status == :stopping
30
+ sleep 2
31
+ end
32
+ @instance.start
33
+ @i = 0
34
+ while self.status != :running
35
+ if @i > 120
36
+ return false
37
+ end
38
+ print "." ; sleep 3 ; @i += 1
39
+ end
27
40
  end
28
41
  puts "\nInstance #{@name} is started"
29
42
  end
30
43
 
31
44
  def exist?
32
45
  puts "Checking for ec2 server #{@name} ..."
33
- if @ec2.instances.any? {|x| x.tags['Name'] == @name && x.status != :shutting_down && x.status != :terminated}
46
+ if AWS.memoize do @ec2.instances.any? {|x| x.tags['Name'] == @name && x.status != :shutting_down && x.status != :terminated} end
34
47
  puts "Instance #{@name} found"
35
48
  return true
36
49
  else
@@ -40,11 +53,7 @@ module Provider
40
53
  end
41
54
 
42
55
  def status
43
- begin
44
- @instance.status
45
- rescue Exception => e
46
- return :missing
47
- end
56
+ @instance.status if @instance
48
57
  end
49
58
 
50
59
  def dns_name
@@ -58,6 +67,16 @@ module Provider
58
67
  def id
59
68
  @instance.id
60
69
  end
70
+
71
+ def instance
72
+ @instance ||= get_instance
73
+ end
74
+
75
+ def get_instance
76
+ AWS.memoize do
77
+ @ec2.instances.select {|x| x.tags['Name'] == @name && x.status != :shutting_down && x.status != :terminated}.first
78
+ end
79
+ end
61
80
 
62
81
  end
63
82
  end
@@ -45,8 +45,9 @@ module Route53
45
45
 
46
46
  def delete
47
47
  if self.exist?
48
- record = Route53::DNSRecord.new(@name, @type, @ttl, @values, @zone)
49
- record.delete
48
+ record = self.get
49
+ delete_record = Route53::DNSRecord.new(record.name, record.type, record.ttl, record.values, @zone)
50
+ delete_record.delete
50
51
  end
51
52
  end
52
53
 
data/lib/veronic.rb CHANGED
@@ -54,7 +54,7 @@ module Veronic
54
54
  deploy_stacks
55
55
  end
56
56
  query = cloudprovider.instance.dns_name
57
- deploy_cmd = 'sudo chef-client -o "recipe[lift_envs::app_deploy]" -l debug'
57
+ deploy_cmd = "sudo chef-client -o 'recipe[lift_envs::app_deploy]' #{config_hash[:verbose] ? '-l ' + config_hash[:verbose] : ''}"
58
58
  manual = true
59
59
  configprovider.ssh(query, deploy_cmd, manual)
60
60
  end
@@ -65,7 +65,7 @@ module Veronic
65
65
  deploy_apps
66
66
  end
67
67
  query = cloudprovider.instance.dns_name
68
- deploy_cmd = 'sudo chef-client -o "recipe[lift_envs::app_testing]" -l debug'
68
+ deploy_cmd = "sudo chef-client -o 'recipe[lift_envs::app_testing]' #{config_hash[:verbose] ? '-l ' + config_hash[:verbose] : ''}"
69
69
  manual = true
70
70
  configprovider.ssh(query, deploy_cmd, manual)
71
71
  end
@@ -76,7 +76,7 @@ module Veronic
76
76
  @config.zone_url = z['zone_url']
77
77
  dns = "#{config_hash[:name]}.#{z['zone_name']}"
78
78
  puts "Setting DNS #{dns} ..."
79
- record = dnsprovider.zone.record(dns, [cloudprovider.instance.public_ip_address], "A", "10").delete
79
+ record = dnsprovider.zone.record(dns, [], "A", "1").delete
80
80
  puts "DNS #{dns} deleted"
81
81
  end
82
82
  if cloudprovider.instance.exist?
@@ -106,7 +106,7 @@ module Veronic
106
106
  @config.zone_url = z['zone_url']
107
107
  dns = "#{config_hash[:name]}.#{z['zone_name']}"
108
108
  puts "Setting DNS #{dns} ..."
109
- record = dnsprovider.zone.record(dns, [cloudprovider.instance.public_ip_address], "A", "10").wait_set
109
+ record = dnsprovider.zone.record(dns, [cloudprovider.instance.public_ip_address], "A", "1").wait_set
110
110
  puts "DNS #{dns} updated"
111
111
  end
112
112
  end
@@ -120,23 +120,51 @@ module Veronic
120
120
  end
121
121
 
122
122
  def start
123
- cloudprovider.instance.start
124
- update_instance_dns
123
+ unless cloudprovider.instance.start == false
124
+ update_instance_dns
125
+ end
126
+ end
127
+
128
+ def status
129
+ if config_hash[:name]
130
+ return cloudprovider.instance.status
131
+ else
132
+ return "Arguments name missing"
133
+ end
125
134
  end
126
135
 
127
136
  def bootstrap
128
- @config.image = cloudprovider.image.id
137
+ get_image
129
138
  if cloudprovider.instance.status == :running
130
- configprovider.instance.bootstrap
139
+ puts "#{config_hash[:name]} is running"
131
140
  elsif cloudprovider.instance.status == :stopped
132
141
  start
133
- configprovider.instance.bootstrap
134
142
  elsif cloudprovider.instance.exist? == false
135
- configprovider.instance.bootstrap
136
- configprovider.instance.set_role
143
+ configprovider.instance.bootstrap
144
+ set_node
145
+ update_instance_dns
137
146
  return true
138
147
  else
139
- raise ArgumentError.new('Error during connecting instance')
148
+ abort('Error during connecting instance')
149
+ end
150
+ set_node
151
+ return false
152
+ end
153
+
154
+ def set_node
155
+ if config_hash[:role] && config_hash[:environment]
156
+ configprovider.instance.set_environment
157
+ configprovider.instance.set_role
158
+ else
159
+ abort('Arguments "role" or "environment" missing')
160
+ end
161
+ end
162
+
163
+ def get_image
164
+ if config_hash[:environment]
165
+ @config.image = cloudprovider.image.id
166
+ else
167
+ abort('Arguments "environment" missing')
140
168
  end
141
169
  end
142
170
  end
data/veronic.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'veronic'
3
- s.version = '0.0.8'
3
+ s.version = '0.0.9'
4
4
  s.date = '2013-04-05'
5
5
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
6
6
  s.summary = "Veronic, deux qui la tiennent trois qui la niquent"
data/veronic.yml CHANGED
@@ -21,4 +21,5 @@ ssh_port:
21
21
  role:
22
22
  flavor:
23
23
  security_groups:
24
- deploy_cmd:
24
+ deploy_cmd:
25
+ verbose:
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: veronic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: