Class: Management::RunScript
Instance Method Summary
collapse
Methods included from Helper
#cloud, #config, #get_address, #get_env, #get_script, #get_server, #get_type, #live_servers, #system_verbose
Methods inherited from Command
all, #call_with, #command_name, #fn, #help_string, inherited, #true_arity
Instance Method Details
#copy_file(server, local_path, remote_path, opts = nil) ⇒ Object
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
|
# File 'lib/management/commands/run_script.rb', line 46
def copy_file(server, local_path, remote_path, opts = nil)
should_template = opts && opts[:template]
custom_chown = opts && opts[:chown]
custom_chmod = opts && opts[:chmod]
Dir.mktmpdir('management-file-dir') do |file_tmpdir|
remote_looking_path = File.join(file_tmpdir, remote_path)
FileUtils.mkdir_p File.dirname(remote_looking_path)
FileUtils.cp_r local_path, remote_looking_path, preserve: true
if should_template
new_contents = ERB.new(File.read(remote_looking_path)).result(binding)
File.write(remote_looking_path, new_contents)
end
Dir.mktmpdir('management-tar-dir') do |tar_tmpdir|
local_tar_path = File.join(tar_tmpdir, "__management__.tar.gz")
zip_relevant_files(file_tmpdir, local_tar_path)
remote_tar_path = "/tmp/__management__.tar.gz"
server.copy_file(local_tar_path, remote_tar_path)
server.(remote_tar_path)
server.chown_r(remote_path, custom_chown) if custom_chown
server.chmod(remote_path, custom_chmod) if custom_chmod
end
end
end
|
#missing_local_files(script) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/management/commands/run_script.rb', line 86
def missing_local_files(script)
script.find_all do |tuple|
type, data = *tuple.first
if type == :copy
local, remote = *data
! File.exists?(local)
end
end.map do |tuple|
type, data = *tuple.first
local, remote = *data
local
end
end
|
#relevant_files(at_dir) ⇒ Object
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/management/commands/run_script.rb', line 100
def relevant_files(at_dir)
abort unless at_dir.start_with? "/"
Dir[File.join(at_dir, "**/*")].select do |path|
File.file?(path) || (File.directory?(path) && Dir.entries(path) == [".", ".."])
end.map do |path|
path.slice! at_dir.end_with?("/") ? at_dir : "#{at_dir}/"
"./#{path}"
end
end
|
#run(server_name, script_name) ⇒ Object
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
|
# File 'lib/management/commands/run_script.rb', line 9
def run(server_name, script_name)
require 'tmpdir'
require 'fileutils'
require 'erb'
require 'shellwords'
server = get_server(server_name)
script = get_script(script_name)
server.private_key_path = config[:ssh_key_path]
missing = missing_local_files(script)
abort "The following files are missing:" + (["\n"] + missing).join("\n - ") if !missing.empty?
script.each do |tuple|
type, data = *tuple.first
case type.to_sym
when :copy
local_path, remote_path, opts = *data
puts "############ Copying #{local_path} -> #{remote_path}"
copy_file(server, local_path, remote_path, opts)
when :run
cmd = data
puts "############ Running #{cmd}"
code = run_remote_command(server, cmd)
if code != 0
abort "############ Failed. Exit code: #{code}"
end
end
end
puts "############ Success!"
end
|
#run_remote_command(server, cmd) ⇒ Object
81
82
83
84
|
# File 'lib/management/commands/run_script.rb', line 81
def run_remote_command(server, cmd)
result = server.ssh("#{cmd}").first
return result.status
end
|