eventhub-command 0.2.1 → 0.2.2

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.
@@ -1,136 +1,149 @@
1
- class Deployer::BaseDeployer
2
- attr_reader :options, :stage_path, :stage
3
-
4
- def initialize(options)
5
- @options = options
6
-
7
- @stage_path = File.join(Eh::Settings.current.stages_dir, "#{options[:stage]}.yml")
8
- @stage = Deployer::Stage.load(stage_path)
9
- end
10
-
11
- private
12
-
13
- def config_source_dir(*extra_paths)
14
- File.join(base_dir, 'config', *extra_paths)
15
- end
16
-
17
- def log_deployment(executor, message)
18
- executor.execute("echo $(date): #{message} - #{ENV['USER']} >> #{deploy_log_file}")
19
- end
20
-
21
- def base_dir
22
- "/apps/compoundbank/s_cme/apps/event_hub"
23
- end
24
-
25
- def deploy_log_file
26
- File.join(base_dir, 'shared', 'logs', 'deploy.log')
27
- end
28
-
29
- def deploy_via
30
- options[:deploy_via]
31
- end
32
-
33
- def verbose?
34
- options[:verbose]
35
- end
36
-
37
- def via_scp?
38
- deploy_via == 'scp'
39
- end
40
-
41
- def cached_copy_dir(*extra_paths)
42
- dir = if via_scp?
43
- File.join(base_dir, 'shared', 'cached-copy-scp')
44
- else
45
- if options[:tag]
46
- File.join(base_dir, 'shared', 'cached-copy-svn', 'tags', options[:tag], 'releases')
47
- elsif options[:branch]
48
- File.join(base_dir, 'shared', 'cached-copy-svn', 'branches', options[:branch], 'releases')
49
- else
50
- File.join(base_dir, 'shared', 'cached-copy-svn', 'branches', 'master', 'releases')
51
- end
52
- end
53
- File.join(dir, *extra_paths)
54
- end
55
-
56
- def scm_username
57
- Eh::Settings.current.repository.deploy_username
58
- end
59
-
60
- def scm_password
61
- Eh::Settings.current.repository.deploy_password
62
- end
63
-
64
- def scm_base_url
65
- Eh::Settings.current.repository.url
66
- end
67
-
68
- def repository
69
- "#{scm_base_url}/branches/master/releases"
70
- end
71
-
72
-
73
- def create_base_dirs(executor)
74
- dirs = [
75
- File.join(base_dir, 'config'),
76
- File.join(base_dir, 'ruby'),
77
- File.join(base_dir, 'mule'),
78
- File.join(base_dir, 'rails'),
79
- File.join(base_dir, 'shared'),
80
- File.join(base_dir, 'shared', 'pids'),
81
- File.join(base_dir, 'shared', 'logs'),
82
- File.join(base_dir, 'shared', 'cached-copy-scp')
83
- ]
84
- cmds = dirs.map do |dir|
85
- "mkdir -p #{dir}"
86
- end
87
- executor.execute(cmds.join(" && "))
88
- end
89
- def update_scm(executor)
90
- dir = File.join(base_dir, 'shared/cached-copy-svn')
91
- cmd = <<-EOS
92
- if [[ -d #{dir} ]]
93
- then
94
- cd #{dir}
95
- svn up --trust-server-cert --non-interactive --username #{scm_username} --password #{scm_password}
96
- else
97
- svn co --trust-server-cert --non-interactive --username #{scm_username} --password #{scm_password} #{scm_base_url} #{dir}
98
- fi
99
- EOS
100
- executor.execute(cmd)
101
- end
102
-
103
- private
104
-
105
-
106
- # Executes an ls on all hosts and returns the combined
107
- # list of files or dirs.
108
- def remote_ls(executor, options, pattern)
109
- results = executor.execute("ls #{pattern}", options)
110
- results.map do |result|
111
- if result[:stdout]
112
- result[:stdout].split("\n")
113
- end
114
- end.flatten.compact.uniq
115
- end
116
-
117
- def verify_deployment_list!(requested, available)
118
- # remove requested that are not available
119
- puts 'Deployment List'.light_blue.on_blue
120
- abort = false
121
- requested.each do |name|
122
- if available.include?(name)
123
- puts "#{name}: AVAILABLE".green
124
- else
125
- abort = true
126
- puts "#{name}: UNAVAILABLE".red
127
- end
128
- end
129
- if abort
130
- puts 'Not all requested components are available in #{cached_copy_dir}. Will abort.'.red
131
- raise
132
- end
133
-
134
- end
135
-
136
- end
1
+ class Deployer::BaseDeployer
2
+ attr_reader :options, :stage_path, :stage
3
+
4
+ def initialize(options)
5
+ @options = options
6
+
7
+ @stage_path = File.join(Eh::Settings.current.stages_dir, "#{options[:stage]}.yml")
8
+ @stage = Deployer::Stage.load(stage_path)
9
+ end
10
+
11
+ private
12
+
13
+ def config_source_dir(*extra_paths)
14
+ File.join(base_dir, 'config', *extra_paths)
15
+ end
16
+
17
+ def log_deployment(executor, message)
18
+ executor.execute("echo $(date): #{message} - #{ENV['USER']} >> #{deploy_log_file}")
19
+ end
20
+
21
+ def base_dir
22
+ "/apps/compoundbank/s_cme/apps/event_hub"
23
+ end
24
+
25
+ def deploy_log_file
26
+ File.join(shared_dir, 'logs', 'deploy.log')
27
+ end
28
+
29
+ def deploy_via
30
+ options[:deploy_via]
31
+ end
32
+
33
+ def verbose?
34
+ options[:verbose]
35
+ end
36
+
37
+ def via_scp?
38
+ deploy_via == 'scp'
39
+ end
40
+
41
+ def shared_dir
42
+ File.join(base_dir, 'shared')
43
+ end
44
+
45
+ def cached_copy_scp_dir
46
+ File.join(shared_dir, 'cached_copy_scp')
47
+ end
48
+
49
+ def cached_copy_svn_dir
50
+ File.join(shared_dir, 'cached_copy_svn')
51
+ end
52
+
53
+ def cached_copy_dir(*extra_paths)
54
+ dir = if via_scp?
55
+ cached_copy_scp_dir
56
+ else
57
+ if options[:tag]
58
+ File.join(cached_copy_svn_dir, 'tags', options[:tag], 'releases')
59
+ elsif options[:branch]
60
+ File.join(cached_copy_svn_dir, 'branches', options[:branch], 'releases')
61
+ else
62
+ File.join(cached_copy_svn_dir, 'branches', 'master', 'releases')
63
+ end
64
+ end
65
+ File.join(dir, *extra_paths)
66
+ end
67
+
68
+ def scm_username
69
+ Eh::Settings.current.repository.deploy_username
70
+ end
71
+
72
+ def scm_password
73
+ Eh::Settings.current.repository.deploy_password
74
+ end
75
+
76
+ def scm_base_url
77
+ Eh::Settings.current.repository.url
78
+ end
79
+
80
+ def repository
81
+ "#{scm_base_url}/branches/master/releases"
82
+ end
83
+
84
+
85
+ def create_base_dirs(executor)
86
+ dirs = [
87
+ File.join(base_dir, 'config'),
88
+ File.join(base_dir, 'ruby'),
89
+ File.join(base_dir, 'mule'),
90
+ File.join(base_dir, 'rails'),
91
+ shared_dir,
92
+ File.join(shared_dir, 'pids'),
93
+ File.join(shared_dir, 'logs'),
94
+ cached_copy_scp_dir
95
+ ]
96
+ cmds = dirs.map do |dir|
97
+ "mkdir -p #{dir}"
98
+ end
99
+ executor.execute(cmds.join(" && "))
100
+ end
101
+
102
+ def update_scm(executor)
103
+ dir = cached_copy_svn_dir
104
+ cmd = <<-EOS
105
+ if [[ -d #{dir} ]]
106
+ then
107
+ cd #{dir}
108
+ svn up --trust-server-cert --non-interactive --username #{scm_username} --password #{scm_password}
109
+ else
110
+ svn co --trust-server-cert --non-interactive --username #{scm_username} --password #{scm_password} #{scm_base_url} #{dir}
111
+ fi
112
+ EOS
113
+ executor.execute(cmd)
114
+ end
115
+
116
+ private
117
+
118
+
119
+ # Executes an ls on all hosts and returns the combined
120
+ # list of files or dirs.
121
+ def remote_ls(executor, options, pattern)
122
+ results = executor.execute("ls #{pattern}", options)
123
+ results.map do |result|
124
+ if result[:stdout]
125
+ result[:stdout].split("\n")
126
+ end
127
+ end.flatten.compact.uniq
128
+ end
129
+
130
+ def verify_deployment_list!(requested, available)
131
+ # remove requested that are not available
132
+ puts 'Deployment List'.light_blue.on_blue
133
+ abort = false
134
+ requested.each do |name|
135
+ if available.include?(name)
136
+ puts "#{name}: AVAILABLE".green
137
+ else
138
+ abort = true
139
+ puts "#{name}: UNAVAILABLE".red
140
+ end
141
+ end
142
+ if abort
143
+ puts 'Not all requested components are available in #{cached_copy_dir}. Will abort.'.red
144
+ raise
145
+ end
146
+
147
+ end
148
+
149
+ end
@@ -1,113 +1,113 @@
1
- require 'net/ssh'
2
- require 'colorize'
3
-
4
- class Deployer::Executor
5
- attr_reader :stage
6
-
7
- def initialize(stage, options = {})
8
- @stage = stage
9
- @options = options
10
- yield(self) if block_given?
11
- end
12
-
13
- # execute a command on all hosts of a stage
14
- # commands are expanded with :hostname, :port and :stagename
15
- def execute(command, options = {})
16
- log_command("Execute: '#{command.strip}'", options[:comment])
17
- stage.hosts.each_with_index.map do |host, index|
18
- expand_options = {hostname: host[:host], stagename: stage.name, port: host[:port]}
19
-
20
- expanded_command = command % expand_options
21
- if expanded_command != command
22
- log_command("Expanded command to #{expanded_command}")
23
- end
24
- log_host(host, index)
25
- result = execute_on(host, expanded_command)
26
- log_result(result)
27
- result
28
- end
29
-
30
- rescue => e
31
- handle_exception(e, options)
32
- end
33
-
34
-
35
- def execute_on(host, command)
36
- Net::SSH.start(host[:host], host[:user], port: host[:port]) do |ssh|
37
- ssh.exec_sc!(command)
38
- end
39
- end
40
-
41
- def upload(source, target, options = {})
42
- log_command("Execute: scp #{source} to #{target}", options[:comment])
43
- stage.hosts.each_with_index do |host, index|
44
- log_host(host, index)
45
- result = upload_on(host, source, target)
46
- log_result(result)
47
- result
48
- end
49
-
50
- rescue => e
51
- handle_exception(e, options)
52
- end
53
-
54
- private
55
- def handle_exception(e, options)
56
- if options[:abort_on_error] == false
57
- puts "warning".black.on_yellow
58
- puts " #{e.message}".yellow
59
- else
60
- puts "failure".black.on_red
61
- puts " #{e.message}".red
62
- end
63
- raise unless options[:abort_on_error] == false
64
-
65
- end
66
-
67
- def verbose?
68
- @options[:verbose]
69
- end
70
-
71
- def log_result(result)
72
- puts "success".black.on_green
73
- if verbose? && result[:stdout] && result[:stdout].length > 0
74
- puts " -> #{result[:stdout].chomp}".light_blue
75
- end
76
- end
77
-
78
- def log_host(host, index)
79
- print " #{host[:host]} (#{index + 1}/#{stage.hosts.size}): "
80
- end
81
-
82
- def log_command(cmd, comment = nil)
83
- puts cmd.blue
84
- puts " (#{comment})" if verbose? && comment
85
- end
86
-
87
- def upload_on(host, source, target)
88
- execute_local "scp -P #{host[:port]} #{source} #{host[:user]}@#{host[:host]}:#{target}"
89
- end
90
-
91
-
92
-
93
- def execute_local(command)
94
- output = nil
95
- exit_code = nil
96
-
97
- Bundler.with_clean_env do
98
- output = `#{command}`
99
- exit_code = $?
100
- end
101
-
102
- raise "Command \"#{command}\" returned exit code #{exit_code}" unless exit_code.success?
103
-
104
- result = {
105
- stdout: output,
106
- stderr: '',
107
- exit_code: 0,
108
- }
109
-
110
- result
111
- end
112
-
113
- end
1
+ require 'net/ssh'
2
+ require 'colorize'
3
+
4
+ class Deployer::Executor
5
+ attr_reader :stage
6
+
7
+ def initialize(stage, options = {})
8
+ @stage = stage
9
+ @options = options
10
+ yield(self) if block_given?
11
+ end
12
+
13
+ # execute a command on all hosts of a stage
14
+ # commands are expanded with :hostname, :port and :stagename
15
+ def execute(command, options = {})
16
+ log_command("Execute: '#{command.strip}'", options[:comment])
17
+ stage.hosts.each_with_index.map do |host, index|
18
+ expand_options = {hostname: host[:host], stagename: stage.name, port: host[:port]}
19
+
20
+ expanded_command = command % expand_options
21
+ if expanded_command != command
22
+ log_command("Expanded command to #{expanded_command}")
23
+ end
24
+ log_host(host, index)
25
+ result = execute_on(host, expanded_command)
26
+ log_result(result)
27
+ result
28
+ end
29
+
30
+ rescue => e
31
+ handle_exception(e, options)
32
+ end
33
+
34
+
35
+ def execute_on(host, command)
36
+ Net::SSH.start(host[:host], host[:user], port: host[:port]) do |ssh|
37
+ ssh.exec_sc!(command)
38
+ end
39
+ end
40
+
41
+ def upload(source, target, options = {})
42
+ log_command("Execute: scp #{source} to #{target}", options[:comment])
43
+ stage.hosts.each_with_index do |host, index|
44
+ log_host(host, index)
45
+ result = upload_on(host, source, target)
46
+ log_result(result)
47
+ result
48
+ end
49
+
50
+ rescue => e
51
+ handle_exception(e, options)
52
+ end
53
+
54
+ private
55
+ def handle_exception(e, options)
56
+ if options[:abort_on_error] == false
57
+ puts "warning".black.on_yellow
58
+ puts " #{e.message}".yellow
59
+ else
60
+ puts "failure".black.on_red
61
+ puts " #{e.message}".red
62
+ end
63
+ raise unless options[:abort_on_error] == false
64
+
65
+ end
66
+
67
+ def verbose?
68
+ @options[:verbose]
69
+ end
70
+
71
+ def log_result(result)
72
+ puts "success".black.on_green
73
+ if verbose? && result[:stdout] && result[:stdout].length > 0
74
+ puts " -> #{result[:stdout].chomp}".light_blue
75
+ end
76
+ end
77
+
78
+ def log_host(host, index)
79
+ print " #{host[:host]} (#{index + 1}/#{stage.hosts.size}): "
80
+ end
81
+
82
+ def log_command(cmd, comment = nil)
83
+ puts cmd.blue
84
+ puts " (#{comment})" if verbose? && comment
85
+ end
86
+
87
+ def upload_on(host, source, target)
88
+ execute_local "scp -P #{host[:port]} #{source} #{host[:user]}@#{host[:host]}:#{target}"
89
+ end
90
+
91
+
92
+
93
+ def execute_local(command)
94
+ output = nil
95
+ exit_code = nil
96
+
97
+ Bundler.with_clean_env do
98
+ output = `#{command}`
99
+ exit_code = $?
100
+ end
101
+
102
+ raise "Command \"#{command}\" returned exit code #{exit_code}" unless exit_code.success?
103
+
104
+ result = {
105
+ stdout: output,
106
+ stderr: '',
107
+ exit_code: 0,
108
+ }
109
+
110
+ result
111
+ end
112
+
113
+ end