Class: Veronic::Deployer

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Deployer

Returns a new instance of Deployer.



11
12
13
14
# File 'lib/veronic.rb', line 11

def initialize(options={})
	@config = config(options)
	@logger = logger
end

Instance Method Details

#bootstrapObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/veronic.rb', line 153

def bootstrap
	status = false
	if cloudprovider.instance.status == :running
		@logger.info "#{@config.name} is running"	
	elsif cloudprovider.instance.status == :stopped
		start
	elsif cloudprovider.instance.exists? == false
		@config.availability_zone = get_availability_zone
		get_image
		configprovider.instance.client.destroy
		configprovider.instance.bootstrap 
		status = true
	else
		abort('Error during connecting instance')  
	end
	set_node
	return status
end

#cloudproviderObject



41
42
43
# File 'lib/veronic.rb', line 41

def cloudprovider
	CloudProvider.new(config_hash)
end

#config(options = {}) ⇒ Object



21
22
23
# File 'lib/veronic.rb', line 21

def config
	@config || Veronic::Config.new
end

#config_hashObject



29
30
31
# File 'lib/veronic.rb', line 29

def config_hash
	@config.to_hash
end

#configproviderObject



33
34
35
# File 'lib/veronic.rb', line 33

def configprovider
	ConfigProvider.new(config_hash) 
end

#createObject



45
46
47
48
49
# File 'lib/veronic.rb', line 45

def create
	unless bootstrap
		configprovider.instance.create
	end
end

#create_imageObject



172
173
174
175
176
177
# File 'lib/veronic.rb', line 172

def create_image
	configprovider.instance.delete_client_key(cloudprovider.instance.dns_name)
	configprovider.instance.client.destroy
	cloudprovider.image.detroy
	cloudprovider.instance.create_image
end

#deployObject



97
98
99
100
101
102
103
104
105
# File 'lib/veronic.rb', line 97

def deploy
	unless @config.deploy_cmd
		abort('Arguments --deploy_cmd is missing')
	end 
	bootstrap
	query = cloudprovider.instance.dns_name
	manual = true
	configprovider.ssh(query, @config.deploy_cmd, manual)
end

#deploy_appsObject



59
60
61
62
63
64
65
66
67
# File 'lib/veronic.rb', line 59

def deploy_apps
	if bootstrap
		deploy_stacks
	end
	query = cloudprovider.instance.dns_name
	deploy_cmd = "sudo chef-client -o 'recipe[lift_envs::app_deploy]' #{@config.verbose ? '-l ' + @config.verbose : ''}"
	manual = true
	configprovider.ssh(query, deploy_cmd, manual)
end

#deploy_stacksObject



51
52
53
54
55
56
57
# File 'lib/veronic.rb', line 51

def deploy_stacks
	bootstrap
	query = cloudprovider.instance.dns_name
	deploy_cmd = "sudo chef-client #{@config.verbose ? '-l ' + @config.verbose : ''}"
	manual = true
	configprovider.ssh(query, deploy_cmd, manual)
end

#destroyObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/veronic.rb', line 80

def destroy
	@config.dnsprovider_zones.each do |z|
		@config.zone_name 	= z['zone_name']
		@config.zone_url 	= z['zone_url']
		dns 				= "#{@config.name}.#{z['zone_name']}"
		@logger.info 				"Setting DNS #{dns} ..."
		record 				= dnsprovider.zone.record(dns, [], "A", "1").delete
		@logger.info 				"DNS #{dns} deleted"
	end
	if cloudprovider.instance.exists?
		configprovider.instance.destroy([cloudprovider.instance.id])
	else
		configprovider.instance.destroy([])
	end

end

#dnsproviderObject



37
38
39
# File 'lib/veronic.rb', line 37

def dnsprovider
	DnsProvider.new(config_hash)
end

#get_availability_zoneObject



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/veronic.rb', line 198

def get_availability_zone
	@logger.info "Getting availability zone ..."
	environments = {}
	if @config.availability_zone.nil? || @config.availability_zone == 'auto'
		cloudprovider.regions.each do |region|
			region.instances.each do |instance|
				if instance.tags[:environment] && instance.tags[:role] && instance.status != :shutting_down && instance.status != :terminated
					environments[instance.tags[:environment]] = {} unless environments[instance.tags[:environment]]
					environments[instance.tags[:environment]][instance.tags[:role]] = {} unless environments[instance.tags[:environment]][instance.tags[:role]]
					environments[instance.tags[:environment]][instance.tags[:role]][region.name] = {} unless environments[instance.tags[:environment]][instance.tags[:role]][region.name]
					region.availability_zones.each do |availability_zone|
						environments[instance.tags[:environment]][instance.tags[:role]][region.name][availability_zone.name] = [] unless environments[instance.tags[:environment]][instance.tags[:role]][region.name][availability_zone.name]
					end
					environments[instance.tags[:environment]][instance.tags[:role]][region.name][instance.availability_zone] << instance.id
				end
			end
		end
	end
	if environments[@config.environment] && environments[@config.environment][@config.role] && environments[@config.environment][@config.role][@config.region]
		availability_zones = environments[@config.environment][@config.role][@config.region].sort_by { |availability_zone| availability_zone[1].count }
		@logger.info "Zones: " + availability_zones.to_s
		availability_zone = availability_zones.first
		@logger.info "Zone selected: " + availability_zone[0]
		availability_zone[0]
	end
end

#get_imageObject



190
191
192
193
194
195
196
# File 'lib/veronic.rb', line 190

def get_image
	if @config.image.nil?
		abort('Arguments --ami_image is missing') 
	else
		@config.image = cloudprovider.image.id
	end
end

#instances_listObject



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

def instances_list
	cloudprovider.instances_list
end

#loggerObject



16
17
18
19
# File 'lib/veronic.rb', line 16

def logger
	$stderr.sync = true
	Logger.new($stderr)
end

#run_testsObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/veronic.rb', line 69

def run_tests
	if bootstrap
		deploy_stacks
		deploy_apps
	end
	query = cloudprovider.instance.dns_name
	deploy_cmd = "sudo chef-client -o 'recipe[lift_envs::app_testing]' #{@config.verbose ? '-l ' + @config.verbose : ''}"
	manual = true
	configprovider.ssh(query, deploy_cmd, manual)
end

#search_and_deployObject



107
108
109
110
111
112
113
114
# File 'lib/veronic.rb', line 107

def search_and_deploy
	unless @config.deploy_cmd || @config.query
		abort('Arguments --deploy_cmd or --query is missing')
	end 
	query = @config.query
	manual = false
	configprovider.ssh(query, @config.deploy_cmd, manual)
end

#set_nodeObject



179
180
181
182
183
184
185
186
187
188
# File 'lib/veronic.rb', line 179

def set_node
	update_instance_dns
	if @config.role && @config.environment
		configprovider.instance.set_environment
		configprovider.instance.set_role
		cloudprovider.instance.tags({'role' => @config.role, 'environment' => @config.environment})
	else
		@logger.info 'Unable to set_node arguments --role or --environment is missing'
	end
end

#startObject



139
140
141
142
143
# File 'lib/veronic.rb', line 139

def start
	unless cloudprovider.instance.start == false
		exit 1
	end
end

#statusObject



145
146
147
148
149
150
151
# File 'lib/veronic.rb', line 145

def status
	if @config.name
		return cloudprovider.instance.status
	else
		return "Arguments --name is missing"
	end
end

#stopObject



135
136
137
# File 'lib/veronic.rb', line 135

def stop
	cloudprovider.instance.stop
end

#update_instance_dnsObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/veronic.rb', line 116

def update_instance_dns
	if @config.dnsprovider_zones
			@config.dnsprovider_zones.each do |z|
				@config.zone_name 	= z['zone_name']
				@config.zone_url 	= z['zone_url']
				dns 				= "#{@config.name}.#{z['zone_name']}"
				@logger.info 				"Setting DNS #{dns} ..."
				record 				= dnsprovider.zone.record(dns, [cloudprovider.instance.dns_name], "CNAME", "1").wait_set
				@logger.info 				"DNS #{dns} updated"
      end
    else
      @logger.info "Unabled to update DNS"
    end
end