lib/kameleon/persistent_cache.rb in kameleon-builder-2.4.0 vs lib/kameleon/persistent_cache.rb in kameleon-builder-2.5.0
- old
+ new
@@ -1,8 +1,9 @@
require 'childprocess'
require 'singleton'
require 'socket'
+require 'net/http'
module Kameleon
#This ruby class will control the execution of Polipo web proxy
class Persistent_cache
@@ -15,11 +16,11 @@
attr_writer :polipo_path
attr_reader :name
attr_writer :cache_path
attr_accessor :mode
attr_accessor :name
- attr_accessor :recipe_files # have to check those.
+ attr_accessor :recipe_files # FIXME have to check those.
attr_accessor :recipe_path
def initialize()
## we must configure Polipo to be execute for the in and out context
## we have to start polipo in the out context for debootstrap step
@@ -61,11 +62,11 @@
port = 0
tmp = nil
ports.each do |p|
begin
port = p
- tmp = TCPServer.new('localhost', port)
+ tmp = TCPServer.new('127.0.0.1', port)
rescue
port =0
end
break if(port>0)
end
@@ -97,27 +98,46 @@
directory_name = File.join(@cache_dir,"DATA","#{step_name}")
FileUtils.mkdir_p directory_name
directory_name
end
+ def proxy_is_running?()
+ begin
+ res = Net::HTTP.get_response(URI("http://127.0.0.1:#{@polipo_port}/polipo/status"))
+ if not res.body.include? "is on line"
+ Kameleon.ui.debug("The proxy is running but not responding. Server response: #{res.inspect}")
+ else
+ Kameleon.ui.debug("The proxy is responding")
+ return true
+ end
+ return false
+ rescue Exception => e
+ Kameleon.ui.debug("The proxy is not responding. Server response: #{e.message}")
+ return false
+ end
+ end
+
def start_web_proxy_in(directory)
- sleep 2 # this is for assuring that the cache is correctly created
- ## setting current step dir
- @current_step_dir = directory
## This function assumes that the cache directory has already been created by the engine
- ## Stopping first the previous proxy
- ## have to check if polipo is running
+
+ # setting current step dir
+ @current_step_dir = directory
Kameleon.ui.debug("Starting web proxy Polipo in directory #{directory} using port: #{@polipo_port}")
@polipo_process.stop(0) unless @polipo_process.nil?
command = ["#{@polipo_path}/polipo", "-c", "/dev/null"]
@polipo_cmd_options[:diskCacheRoot] = directory
@polipo_cmd_options.each{ |v,k| command.push("#{v}=#{k}") }
ChildProcess.posix_spawn = true
Kameleon.ui.debug("Starting process '#{command}'")
@polipo_process = ChildProcess.build(*command)
@polipo_process.start
- return true
+ timeout = 0
+ while ( not(proxy_is_running?) and timeout < 5 )
+ sleep 1
+ timeout = timeout + 1
+ end
+ return (@polipo_process.alive? and proxy_is_running?)
end
def stop_web_proxy
@polipo_process.stop
@@ -180,11 +200,16 @@
end
## Saving metadata information
Kameleon.ui.info("Caching recipe")
File.open("#{cache_metadata_dir}/header",'w+') do |f|
- f.puts({:recipe_path => @recipe_path.basename.to_s}.to_yaml)
+ if recipe_dir.nil?
+ recipe_path = @recipe_path.basename
+ else
+ recipe_path = @recipe_path.relative_path_from(recipe_dir)
+ end
+ f.puts({:recipe_path => recipe_path.to_s}.to_yaml)
f.puts({:date => Time.now.to_i}.to_yaml)
end
#Removing empty directories
cache_data_dir = File.join(@cache_dir,"DATA")
@@ -217,14 +242,15 @@
FileUtils.mkdir_p File.join(@cache_dir,"DATA")
FileUtils.mkdir_p File.join(@cache_dir,"metadata")
end
def get_recipe()
- cached_recipe=Dir.mktmpdir("cache")
- execute("tar","-xf #{@cache_path} -C #{cached_recipe} ./recipe ./metadata")
+ extract_path = File.join(Kameleon.env.build_path, File.basename(@cache_path, ".*"))
+ FileUtils.mkdir_p extract_path
+ execute("tar","-xf #{@cache_path} -C #{extract_path} ./recipe ./metadata")
Kameleon.ui.info("Getting cached recipe")
- recipe_header = YAML::load(File.read(File.join(cached_recipe,"metadata","header")))
- recipe_file = File.join(cached_recipe,"recipe",recipe_header[:recipe_path])
+ recipe_header = YAML::load(File.read(File.join(extract_path,"metadata","header")))
+ recipe_file = File.join(extract_path,"recipe",recipe_header[:recipe_path])
return recipe_file
end
def execute(cmd,args,dir=nil)
command = [cmd ] + args.split(" ")