forked from aws/aws-codedeploy-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux_util.rb
147 lines (121 loc) · 3.88 KB
/
linux_util.rb
1
2
3
4
5
6
7
8
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
module InstanceAgent
class LinuxUtil
def self.supported_versions()
[0.0]
end
def self.supported_oses()
['linux']
end
def self.prepare_script_command(script, absolute_cmd_path)
runas = !!script.runas
sudo = !!script.sudo
if runas && sudo
return 'sudo su ' + script.runas + ' -c ' + absolute_cmd_path
end
if runas && !sudo
return 'su ' + script.runas + ' -c ' + absolute_cmd_path
end
if !runas && sudo
return 'sudo ' + absolute_cmd_path
end
# If neither sudo or runas is specified, execute the
# command as the code deploy agent user
absolute_cmd_path
end
def self.quit()
# Send kill signal to parent and exit
Process.kill('TERM', Process.ppid)
end
def self.script_executable?(path)
File.executable?(path)
end
def self.extract_tar(bundle_file, dst)
log(:debug, "extract_tar - dst : #{dst}")
FileUtils.mkdir_p(dst)
working_dir = FileUtils.pwd()
absolute_bundle_path = File.expand_path(bundle_file)
FileUtils.cd(dst)
execute_tar_command("/bin/tar -xpsf #{absolute_bundle_path}")
FileUtils.cd(working_dir)
end
def self.extract_zip(bundle_file, dst)
log(:debug, "extract_zip - dst : #{dst}")
FileUtils.mkdir_p(dst)
absolute_bundle_path = File.expand_path(bundle_file)
execute_zip_command("unzip -qo #{absolute_bundle_path} -d #{dst}")
end
def self.extract_tgz(bundle_file, dst)
log(:debug, "extract_tgz - dst : #{dst}")
FileUtils.mkdir_p(dst)
working_dir = FileUtils.pwd()
absolute_bundle_path = File.expand_path(bundle_file)
FileUtils.cd(dst)
execute_tar_command("/bin/tar -zxpsf #{absolute_bundle_path}")
FileUtils.cd(working_dir)
end
def self.supports_process_groups?()
true
end
def self.codedeploy_version_file
File.join(ProcessManager::Config.config[:root_dir], '..')
end
def self.fallback_version_file
"/opt/codedeploy-agent"
end
# shelling out the rm folder command to native os in this case linux.
def self.delete_dirs_command(dirs_to_delete)
log(:debug,"Dirs to delete: #{dirs_to_delete}");
for dir in dirs_to_delete do
log(:debug,"Deleting dir: #{dir}");
delete_folder(dir);
end
end
private
def self.delete_folder (dir)
if dir != nil && dir != "/"
output = `rm -rf #{dir} 2>&1`
exit_status = $?.exitstatus
log(:debug, "Command status: #{$?}")
log(:debug, "Command output: #{output}")
unless exit_status == 0
msg = "Error deleting directories: #{exit_status}"
log(:error, msg)
raise msg
end
else
log(:debug, "Empty directory or a wrong directory passed,#{dir}");
end
end
private
def self.execute_tar_command(cmd)
log(:debug, "Executing #{cmd}")
output = `#{cmd} 2>&1`
exit_status = $?.exitstatus
log(:debug, "Command status: #{$?}")
log(:debug, "Command output: #{output}")
if exit_status != 0
msg = "Error extracting tar archive: #{exit_status}"
log(:error, msg)
raise msg
end
end
private
def self.execute_zip_command(cmd)
log(:debug, "Executing #{cmd}")
output = `#{cmd} 2>&1`
exit_status = $?.exitstatus
log(:debug, "Command status: #{$?}")
log(:debug, "Command output: #{output}")
if exit_status != 0
msg = "Error extracting zip archive: #{exit_status}"
log(:error, msg)
raise msg
end
end
private
def self.log(severity, message)
raise ArgumentError, "Unknown severity #{severity.inspect}" unless InstanceAgent::Log::SEVERITIES.include?(severity.to_s)
InstanceAgent::Log.send(severity.to_sym, "#{self.to_s}: #{message}")
end
end
end