Module: Libroute
- Defined in:
- lib/libroute.rb,
lib/libroute/version.rb,
lib/libroute/librouteexec.rb,
lib/libroute/librouteparse.rb
Constant Summary collapse
- VERSION =
"0.1.8"
- CODES =
%w[iso-2022-jp shift_jis euc-jp utf8 binary]
- CODE_ALIASES =
{ "jis" => "iso-2022-jp", "sjis" => "shift_jis" }
Class Method Summary collapse
- .build(options) ⇒ Object
- .exec(library, params, opts = {}) ⇒ Object
-
.parse(args) ⇒ Object
Return a structure describing the options.
- .upload(options, params) ⇒ Object
Class Method Details
.build(options) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/libroute/librouteexec.rb', line 27 def build() puts "Building #{.library} from directory #{.build}" begin im = Docker::Image.build_from_dir(.build) do |v| if (log = JSON.parse(v)) && log.has_key?("stream") puts log["stream"] end end im.tag('repo' => 'libroute_image-'+.library) rescue puts "Libroute: An error occurred during the build process.\n\n" end end |
.exec(library, params, opts = {}) ⇒ Object
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 |
# File 'lib/libroute/librouteexec.rb', line 41 def exec(library, params, opts={}) # Check if existing container running c = Docker::Container.all(all: true).select{|c| c.info['Names'][0].eql?('/libroute_instance-' + library)} if c.length == 1 # Check status if c[0].info['State'].eql?('exited') # Kill and request start c[0].delete c = [] end end if c.length == 0 # Launch container from image imagemap = Docker::Image.all.flat_map{|x| x.info['RepoTags'].count == 1 ? [[x.info['RepoTags'][0],x]] : x.info['RepoTags'].map{|y| [y,x]} } # This command splits multiple tags into a flat vector # -> imagemap is a vector of length equal to the number of images # => each elements is a vector of length 2: [tag, image] imagesel = imagemap.select{|x| x[0].split(':')[0].include?('libroute_image-' + library)} if imagesel.count == 0 h = Hash.new h['stderr'] = 'Image not found' return h end image = imagesel[0][1] c = Docker::Container.create({'Image' => image.id, 'name' => 'libroute_instance-' + library, 'Tty' => true}.merge(opts)) c.start # Wait for container to start sleep 1 else c = c[0] end ip_address = c.json['NetworkSettings']['IPAddress'] # Send and retrieve data from container #s = TCPSocket.new(ip_address, 2000) #s.write(Marshal.dump(params)) #s.close_write #outp = Marshal.load(s.read) #s.close s = TCPSocket.new(ip_address, 2000) bb = params.to_bson s.write(bb.get_bytes(bb.length)) s.close_write data = s.read bb = BSON::ByteBuffer.new(data) outp = Hash.from_bson(bb) s.close # Return response return outp end |
.parse(args) ⇒ Object
Return a structure describing the options.
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 |
# File 'lib/libroute/librouteparse.rb', line 11 def parse(args) # The options specified on the command line will be collected in *options*. # We set default values here. = OpenStruct.new .library = [] .build = [] .upload = false .param = [] .file = [] .showhelp = false opt_parser = OptionParser.new do |opts| opts. = "Usage: libroute.rb [options]" opts.separator "" opts.separator "Required:" # Mandatory argument. #opts.on("-r", "--require LIBRARY", # "Require the LIBRARY before executing your script") do |lib| # options.library << lib #end opts.on("-l", "--lib LIBRARY","Name of the LIBRARY") do |lib| .library = lib end opts.separator "" opts.separator "Options:" opts.on("-b","--build DIR","Build the library from the specified directory") do |o| .build = o end opts.on("-u","--upload","Uploads the specified tarfile. Specify the location using the 'tarfile' parameter.") do |o| .upload = true end opts.on("-p","--param PARAMETER","Specify parameter in the form -p param=value") do |p| .param.push p end opts.on("-f","--file FILE","Specify parameter in the form -f param=file"," Use - for file to read from stdin") do |f| .file.push f end opts.on_tail("-h", "--help", "Show command options","Show library options (-l LIBRARY)") do |h| if .library.empty? puts opts exit else .showhelp = h end end # Another typical switch to print the version. opts.on_tail("--version", "Show version") do puts Libroute::VERSION exit end end opt_parser.parse!(args) end |
.upload(options, params) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/libroute/librouteexec.rb', line 5 def upload(, params) if params['tarfile'].nil? puts "Parameter 'tarfile' not specified" exit 1 end user = ENV['LIBROUTE_USER'] host = ENV['LIBROUTE_HOST'] port = ENV['LIBROUTE_PORT'] if user.nil? then puts "Environment variable LIBROUTE_USER not defined" ; exit 1 end if host.nil? then host = "libroute.io" end if port.nil? then port = 80 end uri = URI.parse("http://#{host}:#{port}/#{user}/libraries/#{.library}/upload") header = {'Content-Type': 'application/octet-stream'} http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Post.new(uri.request_uri, header) request.body = params['tarfile'].data response = http.request(request) end |