Skip to content

Commit

Permalink
Porting to iron_core and version bump.
Browse files Browse the repository at this point in the history
  • Loading branch information
iced committed Apr 27, 2012
1 parent 671387d commit 74c7241
Show file tree
Hide file tree
Showing 21 changed files with 47 additions and 221 deletions.
3 changes: 1 addition & 2 deletions Gemfile
@@ -1,8 +1,7 @@
source 'http://rubygems.org'

gem 'iron_core'
gem 'zip'
gem 'rest-client'
gem 'rest'
gem 'json'
gem 'bundler', '> 1.0.0'

Expand Down
8 changes: 6 additions & 2 deletions Gemfile.lock
Expand Up @@ -2,6 +2,11 @@ GEM
remote: http://rubygems.org/
specs:
git (1.2.5)
iron_core (0.1.0)
bundler (> 1.0.0)
json
rest
rest-client
jeweler (1.8.3)
bundler (~> 1.0)
git (>= 1.2.5)
Expand All @@ -24,8 +29,7 @@ PLATFORMS

DEPENDENCIES
bundler (> 1.0.0)
iron_core
jeweler (~> 1.8.3)
json
rest
rest-client
zip
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.3.9
0.4.0
2 changes: 1 addition & 1 deletion examples/ruby/simple/simple.rb
@@ -1,6 +1,6 @@
require 'iron_worker_ng'

IronWorkerNG::Logger.logger.level = ::Logger::DEBUG
IronCore::Logger.logger.level = ::Logger::DEBUG

# initializing api object with them
client = IronWorkerNG::Client.new(# optinal
Expand Down
1 change: 0 additions & 1 deletion lib/iron_worker_ng.rb
@@ -1,5 +1,4 @@
require_relative 'iron_worker_ng/version'
require_relative 'iron_worker_ng/logger'
require_relative 'iron_worker_ng/client'
require_relative 'iron_worker_ng/code/base'
require_relative 'iron_worker_ng/code/ruby'
Expand Down
143 changes: 7 additions & 136 deletions lib/iron_worker_ng/api_client.rb
@@ -1,151 +1,22 @@
require 'rest-client'
require 'rest'
require 'json'
require 'time'

require_relative 'api_client_error'
require 'iron_core'

module IronWorkerNG
class APIClient
class APIClient < IronCore::Client
AWS_US_EAST_HOST = 'worker-aws-us-east-1.iron.io'

attr_accessor :token
attr_accessor :project_id
attr_accessor :scheme
attr_accessor :host
attr_accessor :port
attr_accessor :api_version
attr_accessor :user_agent

attr_reader :options

def initialize(options = {})
load_from_hash(options)
load_from_config(options[:config_file] || options['config_file'])
load_from_config('iron.json')
load_from_env('IRON_WORKER')
load_from_env('IRON')
load_from_config('~/.iron.json')
load_from_hash(:scheme => 'https', :host => IronWorkerNG::APIClient::AWS_US_EAST_HOST, :port => 443, :api_version => 2, :user_agent => 'iron_worker_ng-' + IronWorkerNG.version)

if (not @token) || (not @project_id)
IronWorkerNG::Logger.error 'Both iron.io token and project_id must be specified'
raise 'Both iron.io token and project_id must be specified'
end

@options = {
:token => @token,
:project_id => @project_id,
super('worker', options)

:scheme => @scheme,
:host => @host,
:port => @port,
:api_version => @api_version,
:user_agent => @user_agent
}
load_from_hash(:scheme => 'https', :host => IronWorkerNG::APIClient::AWS_US_EAST_HOST, :port => 443, :api_version => 2, :user_agent => 'iron_worker_ruby_ng-' + IronWorkerNG.version + ' (iron_core_ruby-' + IronCore.version + ')')

@rest = Rest::Client.new
end

def load_from_hash(hash)
return if hash.nil?

@token ||= hash[:token] || hash['token']
@project_id ||= hash[:project_id] || hash['project_id']

@scheme ||= hash[:scheme] || hash['scheme']
@host ||= hash[:host] || hash['host']
@port ||= hash[:port] || hash['port']
@api_version ||= hash[:api_version] || hash['api_version']
@user_agent ||= hash[:user_agent] || hash['user_agent']
end

def load_from_env(prefix)
@token ||= ENV[prefix + '_TOKEN']
@project_id ||= ENV[prefix + '_PROJECT_ID']

@scheme ||= ENV[prefix + '_SCHEME']
@host ||= ENV[prefix + '_HOST']
@port ||= ENV[prefix + '_PORT']
@api_version ||= ENV[prefix + '_API_VERSION']
@user_agent ||= ENV[prefix + '_USER_AGENT']
end

def load_from_config(config_file)
return if config_file.nil?

if File.exists?(File.expand_path(config_file))
config = JSON.load(File.read(File.expand_path(config_file)))

load_from_hash(config['iron_worker'])
load_from_hash(config['iron'])
load_from_hash(config)
if (not @token) || (not @project_id)
IronCore::Logger.error 'IronWorkerNG', 'Both token and project_id must be specified'
raise IronCore::IronError.new('Both token and project_id must be specified')
end
end

def common_request_hash
{
'Content-Type' => 'application/json',
'Authorization' => "OAuth #{@token}",
'User-Agent' => @user_agent
}
end

def url
"#{scheme}://#{host}:#{port}/#{api_version}/"
end

def get(method, params = {})
request_hash = {}
request_hash[:headers] = common_request_hash
request_hash[:params] = params

IronWorkerNG::Logger.debug "GET #{url + method} with params='#{request_hash.to_s}'"

@rest.get(url + method, request_hash)
end

def post(method, params = {})
request_hash = {}
request_hash[:headers] = common_request_hash
request_hash[:body] = params.to_json

IronWorkerNG::Logger.debug "POST #{url + method} with params='#{request_hash.to_s}'"

@rest.post(url + method, request_hash)
end

def delete(method, params = {})
request_hash = {}
request_hash[:headers] = common_request_hash
request_hash[:params] = params

IronWorkerNG::Logger.debug "DELETE #{url + method} with params='#{request_hash.to_s}'"

@rest.delete(url + method, request_hash)
end

# FIXME: retries support
# FIXME: user agent support
def post_file(method, file, params = {})
request_hash = {}
request_hash[:data] = params.to_json
request_hash[:file] = file

IronWorkerNG::Logger.debug "POST #{url + method + "?oauth=" + @token} with params='#{request_hash.to_s}'"

RestClient.post(url + method + "?oauth=#{@token}", request_hash)
end

def parse_response(response, parse_json = true)
IronWorkerNG::Logger.debug "GOT #{response.code} with params='#{response.body}'"

raise IronWorkerNG::APIClientError.new(response.body) if response.code != 200

return response.body unless parse_json
JSON.parse(response.body)
end

def codes_list(options = {})
parse_response(get("projects/#{@project_id}/codes", options))
end
Expand Down
4 changes: 0 additions & 4 deletions lib/iron_worker_ng/api_client_error.rb

This file was deleted.

1 change: 1 addition & 0 deletions lib/iron_worker_ng/client.rb
@@ -1,4 +1,5 @@
require 'ostruct'
require 'json'

require_relative 'api_client'

Expand Down
6 changes: 3 additions & 3 deletions lib/iron_worker_ng/code/base.rb
Expand Up @@ -84,15 +84,15 @@ def bundle(zip)

def create_zip
unless @exec
IronWorkerNG::Logger.error 'No exec specified'
raise 'No exec specified'
IronCore::Logger.error 'IronWorkerNG', 'No exec specified'
raise IronCore::IronError.new('No exec specified')
end

fixate

zip_name = Dir.tmpdir + '/' + Dir::Tmpname.make_tmpname("iron-worker-ng-", "code.zip")

IronWorkerNG::Logger.debug "Creating code zip '#{zip_name}'"
IronCore::Logger.debug 'IronWorkerNG', "Creating code zip '#{zip_name}'"

Zip::ZipFile.open(zip_name, Zip::ZipFile::CREATE) do |zip|
bundle(zip)
Expand Down
2 changes: 1 addition & 1 deletion lib/iron_worker_ng/code/java.rb
Expand Up @@ -18,7 +18,7 @@ def create_runner(zip)

classpath = classpath_array.join(':')

IronWorkerNG::Logger.info "Collected '#{classpath}' classpath"
IronCore::Logger.info 'IronWorkerNG', "Collected '#{classpath}' classpath"

zip.get_output_stream(runner) do |runner|
runner.write <<RUNNER
Expand Down
6 changes: 3 additions & 3 deletions lib/iron_worker_ng/feature/binary/merge_exec.rb
Expand Up @@ -14,7 +14,7 @@ def hash_string
end

def bundle(zip)
IronWorkerNG::Logger.debug "Bundling binary exec with path='#{@path}'"
IronCore::Logger.debug 'IronWorkerNG', "Bundling binary exec with path='#{@path}'"

zip.add(File.basename(@path), @path)
end
Expand All @@ -25,15 +25,15 @@ def merge_exec(path)
@exec ||= nil

unless @exec.nil?
IronWorkerNG::Logger.warn "Ignoring attempt to merge binary exec with path='#{path}'"
IronCore::Logger.warn 'IronWorkerNG', "Ignoring attempt to merge binary exec with path='#{path}'"
return
end

@name ||= File.basename(path).gsub(/\..*$/, '').capitalize.gsub(/_./) { |x| x[1].upcase }

@exec = IronWorkerNG::Feature::Binary::MergeExec::Feature.new(path)

IronWorkerNG::Logger.info "Merging binary exec with path='#{path}'"
IronCore::Logger.info 'IronWorkerNG', "Merging binary exec with path='#{path}'"

@features << @exec
end
Expand Down
5 changes: 2 additions & 3 deletions lib/iron_worker_ng/feature/common/merge_dir.rb
Expand Up @@ -9,7 +9,6 @@ class Feature < IronWorkerNG::Feature::Base
attr_reader :dest

def initialize(path, dest)
raise 'No such directory - ' + path unless Dir.exist? path
@path = File.expand_path(path)
@dest = dest
@dest = Pathname.new(dest).cleanpath.to_s + '/' unless @dest.empty?
Expand All @@ -26,7 +25,7 @@ def hash_string
end

def bundle(zip)
IronWorkerNG::Logger.debug "Bundling dir with path='#{@path}' and dest='#{@dest}'"
IronCore::Logger.debug 'IronWorkerNG', "Bundling dir with path='#{@path}' and dest='#{@dest}'"

Dir.glob(@path + '/**/**') do |path|
zip.add(@dest + File.basename(@path) + path[@path.length .. -1], path)
Expand All @@ -36,7 +35,7 @@ def bundle(zip)

module InstanceMethods
def merge_dir(path, dest = '')
IronWorkerNG::Logger.info "Merging dir with path='#{path}' and dest='#{dest}'"
IronCore::Logger.info 'IronWorkerNG', "Merging dir with path='#{path}' and dest='#{dest}'"

@features << IronWorkerNG::Feature::Common::MergeDir::Feature.new(path, dest)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/iron_worker_ng/feature/common/merge_file.rb
Expand Up @@ -19,15 +19,15 @@ def hash_string
end

def bundle(zip)
IronWorkerNG::Logger.debug "Bundling file with path='#{@path}' and dest='#{@dest}'"
IronCore::Logger.debug 'IronWorkerNG', "Bundling file with path='#{@path}' and dest='#{@dest}'"

zip.add(@dest + File.basename(@path), @path)
end
end

module InstanceMethods
def merge_file(path, dest = '')
IronWorkerNG::Logger.info "Merging file with path='#{path}' and dest='#{dest}'"
IronCore::Logger.info 'IronWorkerNG', "Merging file with path='#{path}' and dest='#{dest}'"

@features << IronWorkerNG::Feature::Common::MergeFile::Feature.new(path, dest)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/iron_worker_ng/feature/java/merge_exec.rb
Expand Up @@ -16,7 +16,7 @@ def hash_string
end

def bundle(zip)
IronWorkerNG::Logger.debug "Bundling java exec with path='#{@path}' and class='#{@klass}'"
IronCore::Logger.debug 'IronWorkerNG', "Bundling java exec with path='#{@path}' and class='#{@klass}'"

zip.add(File.basename(@path), @path)
end
Expand All @@ -31,7 +31,7 @@ def merge_exec(path, klass = nil)
@exec ||= nil

unless @exec.nil?
IronWorkerNG::Logger.warn "Ignoring attempt to merge java exec with path='#{path}' and class='#{klass}'"
IronCore::Logger.warn 'IronWorkerNG', "Ignoring attempt to merge java exec with path='#{path}' and class='#{klass}'"
return
end

Expand All @@ -43,7 +43,7 @@ def merge_exec(path, klass = nil)

@exec = IronWorkerNG::Feature::Java::MergeExec::Feature.new(path, klass)

IronWorkerNG::Logger.info "Merging java exec with path='#{path}' and class='#{klass}'"
IronCore::Logger.info 'IronWorkerNG', "Merging java exec with path='#{path}' and class='#{klass}'"

@features << @exec
end
Expand Down
4 changes: 2 additions & 2 deletions lib/iron_worker_ng/feature/java/merge_jar.rb
Expand Up @@ -14,7 +14,7 @@ def hash_string
end

def bundle(zip)
IronWorkerNG::Logger.debug "Bundling java jar with path='#{@path}'"
IronCore::Logger.debug 'IronWorkerNG', "Bundling java jar with path='#{@path}'"

zip.add(File.basename(@path), @path)
end
Expand All @@ -26,7 +26,7 @@ def code_for_classpath

module InstanceMethods
def merge_jar(path)
IronWorkerNG::Logger.info "Merging java jar with path='#{path}'"
IronCore::Logger.info 'IronWorkerNG', "Merging java jar with path='#{path}'"

@features << IronWorkerNG::Feature::Java::MergeJar::Feature.new(path)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/iron_worker_ng/feature/node/merge_exec.rb
Expand Up @@ -14,7 +14,7 @@ def hash_string
end

def bundle(zip)
IronWorkerNG::Logger.debug "Bundling node exec with path='#{@path}'"
IronCore::Logger.debug 'IronWorkerNG', "Bundling node exec with path='#{@path}'"

zip.add(File.basename(@path), @path)
end
Expand All @@ -25,15 +25,15 @@ def merge_exec(path)
@exec ||= nil

unless @exec.nil?
IronWorkerNG::Logger.warn "Ignoring attempt to merge node exec with path='#{path}'"
IronCore::Logger.warn 'IronWorkerNG', "Ignoring attempt to merge node exec with path='#{path}'"
return
end

@name ||= File.basename(path).gsub(/\.js$/, '').capitalize.gsub(/_./) { |x| x[1].upcase }

@exec = IronWorkerNG::Feature::Node::MergeExec::Feature.new(path)

IronWorkerNG::Logger.info "Merging node exec with path='#{path}'"
IronCore::Logger.info 'IronWorkerNG', "Merging node exec with path='#{path}'"

@features << @exec
end
Expand Down

0 comments on commit 74c7241

Please sign in to comment.