lib/gooddata/models/process.rb in gooddata-0.6.10 vs lib/gooddata/models/process.rb in gooddata-0.6.11

- old
+ new

@@ -1,11 +1,16 @@ # encoding: UTF-8 require 'pry' +require 'zip' +require_relative '../helpers/global_helpers' require_relative '../rest/resource' +require_relative 'execution_detail' +require_relative 'schedule' + module GoodData class Process < GoodData::Rest::Object attr_reader :data alias_method :raw_data, :data @@ -80,63 +85,22 @@ fail ArgumentError, 'No :project specified' if p.nil? project = GoodData::Project[p, opts] fail ArgumentError, 'Wrong :project specified' if project.nil? - if !path.directory? && (path.extname == '.grf' || path.extname == '.rb') - puts 'Creating package for upload' - Tempfile.open('deploy-graph-archive') do |temp| - Zip::OutputStream.open(temp.path) do |zio| - FileUtils.cd(path.parent) do - files_to_pack = [path.basename] - files_to_pack.each do |item| - puts "including #{item}" - unless File.directory?(item) - zio.put_next_entry(item) - zio.print IO.read(item) - end - end - end - end - - client.upload_to_user_webdav(temp.path, opts) - temp.path - end - elsif !path.directory? - client.upload_to_user_webdav(path, opts) - path - else - Tempfile.open('deploy-graph-archive') do |temp| - Zip::OutputStream.open(temp.path) do |zio| - FileUtils.cd(path) do - - files_to_pack = Dir.glob('./**/*').reject { |f| files_to_exclude.include?(Pathname(path) + f) } - files_to_pack.each do |item| - puts "including #{item}" - unless File.directory?(item) - zio.put_next_entry(item) - zio.print IO.read(item) - end - end - end - end - - client.upload_to_user_webdav(temp.path, opts) - temp.path - end - end + zip_and_upload path, files_to_exclude, opts end # Deploy a new process or redeploy existing one. # # @param path [String] Path to ZIP archive or to a directory containing files that should be ZIPed # @option options [String] :files_to_exclude # @option options [String] :type ('GRAPH') Type of process - GRAPH or RUBY # @option options [String] :name Readable name of the process # @option options [String] :process_id ID of a process to be redeployed (do not set if you want to create a new process) # @option options [Boolean] :verbose (false) Switch on verbose mode for detailed logging - def deploy(path, options = {}) + def deploy(path, options = { :client => GoodData.client, :project => GoodData.project }) client = options[:client] fail ArgumentError, 'No :client specified' if client.nil? p = options[:project] fail ArgumentError, 'No :project specified' if p.nil? @@ -172,10 +136,50 @@ process = client.create(Process, res, project: p) puts HighLine.color("Deploy DONE #{path}", HighLine::GREEN) if verbose process end + + # ----------------------------- Private Stuff + + private + + def zip_and_upload(path, files_to_exclude, opts = {}) + def with_zip(opts = {}) + Tempfile.open('deploy-graph-archive') do |temp| + zip_filename = temp.path + File.open(zip_filename, 'w') do |zip| + Zip::File.open(zip.path, Zip::File::CREATE) do |zipfile| + yield zipfile + end + end + client.upload_to_user_webdav(temp.path, opts) + temp.path + end + end + + puts 'Creating package for upload' + if !path.directory? && (path.extname == '.grf' || path.extname == '.rb') + with_zip(opts) do |zipfile| + zipfile.add(File.basename(path), path) + end + + elsif !path.directory? + client.upload_to_user_webdav(path, opts) + path + else + with_zip(opts) do |zipfile| + Dir[File.join(path, '**', '**')].reject { |f| files_to_exclude.include?(Pathname(path) + f) }.each do |file| + file_pathname = Pathname.new(file) + puts "Including item #{file_pathname}" + file_relative_pathname = file_pathname.relative_path_from(Pathname.new(path)) + zipfile.add(file_relative_pathname, file) + end + end + end + end + # ----------------------------- end def initialize(data) @data = data end @@ -191,13 +195,21 @@ # @option options [String] :process_id ('nobody') From address # @option options [String] :type ('GRAPH') Type of process - GRAPH or RUBY # @option options [String] :name Readable name of the process # @option options [Boolean] :verbose (false) Switch on verbose mode for detailed logging def deploy(path, options = {}) - Process.deploy(path, options.merge(:process_id => process_id)) + Process.deploy(path, options.merge(:process_id => process_id, :client => client, :project => project)) end + # Downloads the process from S3 in a zipped form. + # + # @return [IO] The stream of data that represents a zipped deployed process. + def download + link = links['source'] + client.get(link, process: false) { |_, _, result| RestClient.get(result.to_hash['location'].first) } + end + def process data['process'] end def name @@ -243,27 +255,31 @@ def create_schedule(cron, executable, options = {}) project.create_schedule(process_id, cron, executable, options.merge(client: client, project: project)) end def execute(executable, options = {}) - params = options[:params] || {} - hidden_params = options[:hidden_params] || {} - result = client.post(executions_link, - :execution => { - :graph => executable.to_s, - :params => params, - :hiddenParams => hidden_params - }) + result = start_execution(executable, options) begin client.poll_on_code(result['executionTask']['links']['poll']) rescue RestClient::RequestFailed => e raise(e) ensure result = client.get(result['executionTask']['links']['detail']) if result['executionDetail']['status'] == 'ERROR' - fail "Runing process failed. You can look at a log here #{result["executionDetail"]["logFileName"]}" + fail "Runing process failed. You can look at a log here #{result['executionDetail']['logFileName']}" end end - result + client.create(GoodData::ExecutionDetail, result, client: client, project: project) + end + + def start_execution(executable, options = {}) + params = options[:params] || {} + hidden_params = options[:hidden_params] || {} + client.post(executions_link, + :execution => { + :graph => executable.to_s, + :params => GoodData::Helpers.encode_params(params, false), + :hiddenParams => GoodData::Helpers.encode_params(hidden_params, true) + }) end end end