Sha256: 016077ccffdeb24cac7562ccd3e62a164747be57d80f419884c14e435936f556

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

module OdaniaStaticPages
	module Deploy
		class Rsync
			include ::NginxHelper

			def initialize
				@config = OdaniaStaticPages.config
				@deploy_config = @config.current_environment.deploy_module
				@local_state = '_local_state.yml'
			end

			def prepare
				puts 'Preparing rsync state'
				load_state
				save_state

				puts 'Copying Dockerfile'
				FileUtils.cp File.join(@config.base_dir, 'templates', 'live', 'Dockerfile'), File.join(@config.output_path, 'Dockerfile')

				generate_nginx_config(false)
			end

			def publish(color, do_rebuild)
				puts 'Rsync website'
				load_state
				color = color.nil? ? @state[:color] : color
				new_color = 'green'.eql?(color) ? 'blue' : 'green'
				puts " -> Current color: #{color}"
				@site_path = @config.output_site_path
				puts " -> Deploying to color: #{new_color} [Path: #{@site_path}]"

				@deploy_config.targets[new_color].each do |target|
					puts
					puts
					puts "Syncing target #{target} " + '-' * 50

					cmd = "cd #{@site_path} && rsync #{@deploy_config.rsync_options} . #{target}"
					puts "Executing: #{cmd}"
					puts `#{cmd}`.split("\n").join("\n    ")

					unless $?.success?
						puts 'Error during rsync!!'
						exit 1
					end
				end

				@state[:color] = new_color
				save_state

				@config.current_environment.do_notify new_color, color
				puts
				puts "Finished deploying color #{new_color}"
			end

			private

			def load_state
				@state = {color: 'blue'}
				cmd = "rsync #{@deploy_config.state_file} #{@local_state}"
				puts "Syncing state: #{cmd}"
				puts `#{cmd}`

				@state = YAML.load_file(@local_state).symbolize_keys! if $?.success?
				@state
			end

			def save_state
				File.write @local_state, YAML.dump(@state)
				cmd = "rsync #{@local_state} #{@deploy_config.state_file}"
				puts "Syncing state: #{cmd}"
				puts `#{cmd}`

				unless $?.success?
					puts 'Error saving state!!'
					exit 1
				end
			end
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
odania-static-pages-0.1.5 lib/odania_static_pages/deploy/rsync.rb