9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/simple_deployer.rb', line 9
def self.run()
trap 'SIGINT' do
exit 130
end
if ARGV.length && ARGV[0] == '-h'
puts 'Usage: deployer.rb OR deployer.rb <config_file.yml>'
end
config_file = ARGV[0] ? ARGV[0] : 'config.yml'
unless File.exists?(config_file)
puts 'Invalid config file'
exit(false)
end
config = YAML.load_file(config_file)
deployment_directory = config['deployment-directory']
applications = []
config['applications'].each_key { |app| applications << app }
app_choice = nil
while !(1..applications.length).to_a.include?(app_choice.to_i)
puts 'Invalid Selection' unless app_choice === nil
puts 'Select your desired application:'
applications.each_with_index do |application, i|
puts "#{i + 1} #{application.to_s}"
end
puts ''
$stdout.flush
app_choice = gets.chomp
end
application_name = applications[app_choice.to_i - 1]
configured_app = config['applications'][application_name]
application_directory = "#{deployment_directory}/#{application_name}"
FileUtils.makedirs(deployment_directory) unless File.directory?(deployment_directory)
if File.directory?(application_directory)
puts '-- Fetching latest --'
g = Git.open(application_directory)
g.reset_hard
g.fetch
g.checkout('master')
else
puts '-- Cloning project --'
Git.clone(configured_app['git'], application_name, path: deployment_directory)
end
servers = []
configured_app['build-commands'].each_key { |server| servers << server }
server_choice = nil
while !(1..servers.length).to_a.include?(server_choice.to_i)
puts 'Invalid Selection' unless server_choice === nil
puts "Where should #{application_name} be deployed:"
servers.each_with_index do |server, i|
puts "#{i + 1} #{server.to_s}"
end
puts ''
$stdout.flush
server_choice = gets.chomp
end
server = servers[server_choice.to_i - 1]
build_commands = configured_app['build-commands'][server]
if build_commands.length
build_commands.insert(0, "cd #{application_directory}")
status = system(build_commands.join(' && '))
unless status
puts '-- Error during build steps --'
exit(false)
end
end
remote_server = config['servers'][server]
if configured_app['file-to-deploy']
puts '-- Uploading file --'
Net::SCP.upload!(remote_server['url'],
remote_server['user'],
"#{application_directory}/#{configured_app['file-to-deploy']}",
configured_app['file-deploy-location'],
{ssh: {port: remote_server['port']}})
puts '-- File uploaded --'
end
if configured_app['ssh-commands']
puts '-- Running ssh commands --'
Net::SSH.start(remote_server['url'], remote_server['user'], port: remote_server['port']) do |ssh|
output = ssh.exec! configured_app['ssh-commands'][server].join(' && ')
if output
puts output
puts '-- Error while executing remote commands --'
exit(false)
end
end
end
puts '-- Build completed --'
end
|