Class: Apcera::Stager
- Inherits:
-
Object
- Object
- Apcera::Stager
- Defined in:
- lib/apcera/stager/stager.rb
Constant Summary collapse
- PKG_NAME =
"pkg.tar.gz"- UPDATED_PKG_NAME =
"updated.tar.gz"
Instance Attribute Summary collapse
-
#app_path ⇒ Object
Returns the value of attribute app_path.
-
#pkg_path ⇒ Object
Returns the value of attribute pkg_path.
-
#root_path ⇒ Object
Returns the value of attribute root_path.
-
#stager_url ⇒ Object
Returns the value of attribute stager_url.
-
#system_options ⇒ Object
Returns the value of attribute system_options.
-
#updated_pkg_path ⇒ Object
Returns the value of attribute updated_pkg_path.
Instance Method Summary collapse
-
#complete ⇒ Object
Finish staging, compress your app dir and send to the staging coordinator.
-
#dependencies_add(type, name) ⇒ Object
Add dependencies to package.
-
#dependencies_remove(type, name) ⇒ Object
Delete dependencies from package.
-
#done ⇒ Object
Tell the staging coordinator you are done.
-
#download ⇒ Object
Download a package from the staging coordinator.
-
#environment_add(key, value) ⇒ Object
Add environment variable to package.
-
#environment_remove(key, value) ⇒ Object
Delete environment variable from package.
-
#execute(cmd) ⇒ Object
Execute a command in the shell.
-
#execute_app(cmd) ⇒ Object
Execute a command in the app dir.
-
#exit0r(code) ⇒ Object
Exit, needed for tests to not quit.
-
#extract(location) ⇒ Object
Extract the package to a given location.
-
#fail(error = nil) ⇒ Object
Fail the stager, something went wrong.
-
#initialize(options = {}) ⇒ Stager
constructor
A new instance of Stager.
-
#meta ⇒ Object
Get metadata for the package being staged.
-
#output(text) ⇒ Object
Output to stdout.
-
#output_error(text) ⇒ Object
Output to stderr.
-
#provides_add(type, name) ⇒ Object
Add provides to package.
-
#provides_remove(key, value) ⇒ Object
Delete provides from package.
-
#relaunch ⇒ Object
Tell the staging coordinator you need to relaunch.
-
#snapshot ⇒ Object
Snapshot the stager filesystem for app.
-
#start_command ⇒ Object
Returns the start command for the package.
-
#start_command=(val) ⇒ Object
Easily set the start command.
-
#start_path ⇒ Object
Returns the start path for the package.
-
#start_path=(val) ⇒ Object
Easily set the start path.
-
#templates_add(path, left_delimiter = "{{", right_delimiter = "}}") ⇒ Object
Add template to package.
-
#templates_remove(path, left_delimiter = "{{", right_delimiter = "}}") ⇒ Object
Delete template from package.
-
#upload ⇒ Object
Upload the new package to the staging coordinator.
Constructor Details
#initialize(options = {}) ⇒ Stager
Returns a new instance of Stager.
8 9 10 11 12 13 14 15 |
# File 'lib/apcera/stager/stager.rb', line 8 def initialize( = {}) # Require stager url. Needed to talk to the Staging Coordinator. @stager_url = [:stager_url] || ENV["STAGER_URL"] raise Apcera::Error::StagerURLRequired.new("stager_url required") unless @stager_url # Setup the environment, some test items here. setup_environment end |
Instance Attribute Details
#app_path ⇒ Object
Returns the value of attribute app_path.
3 4 5 |
# File 'lib/apcera/stager/stager.rb', line 3 def app_path @app_path end |
#pkg_path ⇒ Object
Returns the value of attribute pkg_path.
3 4 5 |
# File 'lib/apcera/stager/stager.rb', line 3 def pkg_path @pkg_path end |
#root_path ⇒ Object
Returns the value of attribute root_path.
3 4 5 |
# File 'lib/apcera/stager/stager.rb', line 3 def root_path @root_path end |
#stager_url ⇒ Object
Returns the value of attribute stager_url.
3 4 5 |
# File 'lib/apcera/stager/stager.rb', line 3 def stager_url @stager_url end |
#system_options ⇒ Object
Returns the value of attribute system_options.
3 4 5 |
# File 'lib/apcera/stager/stager.rb', line 3 def end |
#updated_pkg_path ⇒ Object
Returns the value of attribute updated_pkg_path.
3 4 5 |
# File 'lib/apcera/stager/stager.rb', line 3 def updated_pkg_path @updated_pkg_path end |
Instance Method Details
#complete ⇒ Object
Finish staging, compress your app dir and send to the staging coordinator. Then tell the staging coordinator we are done.
223 224 225 226 |
# File 'lib/apcera/stager/stager.rb', line 223 def complete upload done end |
#dependencies_add(type, name) ⇒ Object
Add dependencies to package.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/apcera/stager/stager.rb', line 137 def dependencies_add(type, name) exists = self.["dependencies"].detect { |dep| dep["type"] == type && dep["name"] == name } return false if exists response = RestClient.put(@stager_url+"/meta", { :resource => "dependencies", :action => "add", :type => type, :name => name }) true rescue => e fail e end |
#dependencies_remove(type, name) ⇒ Object
Delete dependencies from package.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/apcera/stager/stager.rb', line 154 def dependencies_remove(type, name) exists = self.["dependencies"].detect { |dep| dep["type"] == type && dep["name"] == name} return false if !exists response = RestClient.put(@stager_url+"/meta", { :resource => "dependencies", :action => "remove", :type => type, :name => name }) true rescue => e fail e end |
#done ⇒ Object
Tell the staging coordinator you are done.
206 207 208 209 210 211 |
# File 'lib/apcera/stager/stager.rb', line 206 def done response = RestClient.post(@stager_url+"/done", {}) exit0r 0 rescue => e fail e end |
#download ⇒ Object
Download a package from the staging coordinator.
18 19 20 21 22 23 24 25 |
# File 'lib/apcera/stager/stager.rb', line 18 def download response = RestClient.get(@stager_url + "/data") File.open(@pkg_path, "wb") do |f| f.write(response.to_str) end rescue => e fail e end |
#environment_add(key, value) ⇒ Object
Add environment variable to package.
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/apcera/stager/stager.rb', line 89 def environment_add(key, value) response = RestClient.put(@stager_url+"/meta", { :resource => "environment", :action => "add", :key => key, :value => value }) rescue => e fail e end |
#environment_remove(key, value) ⇒ Object
Delete environment variable from package.
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/apcera/stager/stager.rb', line 101 def environment_remove(key, value) response = RestClient.put(@stager_url+"/meta", { :resource => "environment", :action => "remove", :key => key, :value => value }) rescue => e fail e end |
#execute(cmd) ⇒ Object
Execute a command in the shell. We don’t want real commands in tests.
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/apcera/stager/stager.rb', line 29 def execute(cmd) Bundler.with_clean_env do result = system(cmd, ) if !result raise Apcera::Error::ExecuteError.new("failed to execute: #{cmd}.\n") end result end rescue => e fail e end |
#execute_app(cmd) ⇒ Object
Execute a command in the app dir. Useful helper.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/apcera/stager/stager.rb', line 43 def execute_app(cmd) Bundler.with_clean_env do Dir.chdir(@app_path) do |app_path| result = system(cmd, ) if !result raise Apcera::Error::ExecuteError.new("failed to execute: #{cmd}.\n") end result end end rescue => e fail e end |
#exit0r(code) ⇒ Object
Exit, needed for tests to not quit.
259 260 261 |
# File 'lib/apcera/stager/stager.rb', line 259 def exit0r(code) exit code end |
#extract(location) ⇒ Object
Extract the package to a given location.
59 60 61 62 63 64 65 66 |
# File 'lib/apcera/stager/stager.rb', line 59 def extract(location) @app_path = File.join(@root_path, location) Dir.mkdir(@app_path) unless Dir.exists?(@app_path) execute_app("tar -zxf #{@pkg_path}") rescue => e fail e end |
#fail(error = nil) ⇒ Object
Fail the stager, something went wrong.
249 250 251 252 253 254 255 256 |
# File 'lib/apcera/stager/stager.rb', line 249 def fail(error = nil) output_error "Error: #{error.message}.\n" if error RestClient.post(@stager_url+"/failed", {}) rescue => e output_error "Error: #{e.message}.\n" ensure exit0r 1 end |
#meta ⇒ Object
Get metadata for the package being staged.
197 198 199 200 201 202 203 |
# File 'lib/apcera/stager/stager.rb', line 197 def response = RestClient.get(@stager_url+"/meta") return JSON.parse(response.to_s) rescue => e output_error "Error: #{e.message}.\n" raise e end |
#output(text) ⇒ Object
Output to stdout
269 270 271 |
# File 'lib/apcera/stager/stager.rb', line 269 def output(text) $stdout.puts text end |
#output_error(text) ⇒ Object
Output to stderr
264 265 266 |
# File 'lib/apcera/stager/stager.rb', line 264 def output_error(text) $stderr.puts text end |
#provides_add(type, name) ⇒ Object
Add provides to package.
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/apcera/stager/stager.rb', line 113 def provides_add(type, name) response = RestClient.put(@stager_url+"/meta", { :resource => "provides", :action => "add", :type => type, :name => name }) rescue => e fail e end |
#provides_remove(key, value) ⇒ Object
Delete provides from package.
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/apcera/stager/stager.rb', line 125 def provides_remove(key, value) response = RestClient.put(@stager_url+"/meta", { :resource => "provides", :action => "remove", :type => type, :name => name }) rescue => e fail e end |
#relaunch ⇒ Object
Tell the staging coordinator you need to relaunch.
214 215 216 217 218 219 |
# File 'lib/apcera/stager/stager.rb', line 214 def relaunch response = RestClient.post(@stager_url+"/relaunch", {}) exit0r 0 rescue => e fail e end |
#snapshot ⇒ Object
Snapshot the stager filesystem for app
82 83 84 85 86 |
# File 'lib/apcera/stager/stager.rb', line 82 def snapshot response = RestClient.post(@stager_url+"/snapshot", {}) rescue => e fail e end |
#start_command ⇒ Object
Returns the start command for the package.
229 230 231 |
# File 'lib/apcera/stager/stager.rb', line 229 def start_command self.["environment"]["START_COMMAND"] end |
#start_command=(val) ⇒ Object
Easily set the start command
234 235 236 |
# File 'lib/apcera/stager/stager.rb', line 234 def start_command=(val) self.environment_add("START_COMMAND", val) end |
#start_path ⇒ Object
Returns the start path for the package.
239 240 241 |
# File 'lib/apcera/stager/stager.rb', line 239 def start_path self.["environment"]["START_PATH"] end |
#start_path=(val) ⇒ Object
Easily set the start path
244 245 246 |
# File 'lib/apcera/stager/stager.rb', line 244 def start_path=(val) self.environment_add("START_PATH", val) end |
#templates_add(path, left_delimiter = "{{", right_delimiter = "}}") ⇒ Object
Add template to package.
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/apcera/stager/stager.rb', line 171 def templates_add(path, left_delimiter = "{{", right_delimiter = "}}") response = RestClient.put(@stager_url+"/meta", { :resource => "templates", :action => "add", :path => path, :left_delimiter => left_delimiter, :right_delimiter => right_delimiter }) rescue => e fail e end |
#templates_remove(path, left_delimiter = "{{", right_delimiter = "}}") ⇒ Object
Delete template from package.
184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/apcera/stager/stager.rb', line 184 def templates_remove(path, left_delimiter = "{{", right_delimiter = "}}") response = RestClient.put(@stager_url+"/meta", { :resource => "templates", :action => "remove", :path => path, :left_delimiter => left_delimiter, :right_delimiter => right_delimiter }) rescue => e fail e end |
#upload ⇒ Object
Upload the new package to the staging coordinator
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/apcera/stager/stager.rb', line 69 def upload app_dir = Pathname.new(@app_path).relative_path_from(Pathname.new(@root_path)).to_s execute_app("cd #{app_path}/.. && tar czf #{@updated_pkg_path} #{app_dir}") sha1 = Digest::SHA1.file(@updated_pkg_path) File.open(@updated_pkg_path, "rb") do |f| response = RestClient.post(@stager_url+"/data?sha1=#{sha1.to_s}", f.read, { :content_type => "application/octet-stream" } ) end rescue => e fail e end |