spurious-server 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7b00c1454c5618c7808f7b7a374f1b4b4f020a04
4
- data.tar.gz: 4b6b8c18d0dcae10060988c3950b5260208e2bbf
3
+ metadata.gz: fc578dfb5d5cbd126d50837e83c4af755ec0f55b
4
+ data.tar.gz: 25293d3e128a347e2d70db1156a27b78c94391fd
5
5
  SHA512:
6
- metadata.gz: 815c667179bd03cff5d31b54a30d63ace256741d403542e691096ce8ee862e6b970eb45b6ab4e6851304756cecb97ae468b88e66a295647ed6e8b9a8614e2d61
7
- data.tar.gz: 179d4b5eeac8d8872242ea9f50e6339a0c1cdb9a689d623cbcb94e6593087aea050ab1f3392a46133884efbe11fdc55f5068c799becd972a5b834978f417e9ef
6
+ metadata.gz: a98b2092ce91a88dac21b93efb9ad047cb0b2ee1e2910f0a76bd8cd899e98469101c4e4de4bab0d59c576253346af1218f1ffee7620fe0ffc5cc429fcd0cdc44
7
+ data.tar.gz: 1c56e932cd9a63050b6a9dc40299b1e2ff8a69f65a9ac8dfeb33065857c647ec4a3a6b335299412cdfd2994f1178af08681e718d58ff3a18f492eb4afa646eb5
data/bin/spurious-server CHANGED
@@ -1,16 +1,19 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  $: << File.join(File.dirname(__FILE__), '..', 'lib')
4
+
4
5
  require 'spurious/server'
6
+ require "spurious/server/version"
5
7
  require 'daemons'
8
+ require "eventmachine"
9
+
10
+ Docker.url = ENV.fetch('DOCKER_HOST', 'tcp://0.0.0.0:2375')
11
+ ip = ENV.fetch('SPURIOUS_SERVER_IP', '0.0.0.0')
12
+ port = ENV.fetch('SPURIOUS_SERVER_PORT', 4590)
13
+
14
+ Excon.defaults[:write_timeout] = 20000
15
+ Excon.defaults[:read_timeout] = 20000
6
16
 
7
- Daemons.run_proc(
8
- 'Spurious Server', # name of daemon
9
- :dir => '/tmp', # directory where pid file will be stored
10
- :log_output => true
11
- ) do
12
- Docker.url = 'http://0.0.0.0:2375'
13
- EventMachine.run {
14
- Spurious::Server.run!
15
- }
17
+ Daemons.run_proc('Spurious Server', :dir => '/tmp', :log_output => true) do
18
+ EventMachine.run Spurious::Server.handle(ip, port)
16
19
  end
data/config/images.yaml CHANGED
@@ -7,6 +7,9 @@
7
7
  'spurious-dynamo':
8
8
  :image: 'smaj/local-dynamo'
9
9
 
10
+ 'spurious-memcached':
11
+ :image: 'smaj/memcached'
12
+
10
13
  'spurious-elasticache':
11
14
  :image: 'smaj/fake-elasticache'
12
15
  :link:
@@ -18,6 +21,3 @@
18
21
  :image: 'smaj/fake-elasticache'
19
22
  :link:
20
23
  - 'spurious-memcached:memcached01'
21
-
22
- 'spurious-memcached':
23
- :image: 'smaj/memcached'
@@ -5,18 +5,10 @@ require "spurious/server/app"
5
5
  module Spurious
6
6
  module Server
7
7
 
8
- @@containers = []
9
-
10
- def self.add_container(name, meta)
11
- @@containers[name] = meta
12
- end
13
-
14
- def self.get_container(name)
15
- @@containers[name]
16
- end
17
-
18
- def self.run!
19
- EventMachine.start_server '0.0.0.0', 4590, Spurious::Server::App
8
+ def self.handle(ip, port)
9
+ Proc.new do
10
+ EventMachine.start_server ip, port, Spurious::Server::App
11
+ end
20
12
  end
21
13
 
22
14
  end
@@ -9,8 +9,8 @@ module Spurious
9
9
  class App < EventMachine::Connection
10
10
 
11
11
  def receive_data data
12
- payload = parse_payload data
13
- state(payload[:type]).execute!
12
+ payload = parse_payload data
13
+ state(payload[:type]).execute!
14
14
  rescue Exception => e
15
15
  puts e.message
16
16
  state(:error).tap { |s| s.message = "JSON payload malformed" }.execute!
@@ -22,6 +22,8 @@ module Spurious
22
22
  def spurious_containers
23
23
  Docker::Container.all(:all => true).select do |container|
24
24
  config.name_exists?(sanitize(container.json["Name"]))
25
+ end.sort do |e1, e2|
26
+ app_config.keys.index(sanitize(e1.json["Name"])) <=> app_config.keys.index(sanitize(e2.json["Name"]))
25
27
  end
26
28
  end
27
29
 
@@ -33,8 +35,12 @@ module Spurious
33
35
  config.for sanitize(image)
34
36
  end
35
37
 
36
- def send(data)
37
- connection.send_data JSON.generate({:type => state_identifer, :response => data}) + "\n"
38
+ def send(data, close = false)
39
+ connection.send_data "#{JSON.generate({:type => state_identifer, :response => data, :close => close})}\n"
40
+ end
41
+
42
+ def error(message, close = false)
43
+ connection.send_data "#{JSON.generate({:type => 'error', :response => message, :close => close})}\n"
38
44
  end
39
45
 
40
46
  def sanitize(name)
@@ -16,7 +16,7 @@ module Spurious
16
16
  c.delete(:force => true)
17
17
  end
18
18
  end
19
- send "#{containers} containers successfully removed"
19
+ send "#{containers} containers successfully removed", true
20
20
 
21
21
  connection.unbind
22
22
  rescue Exception => e
@@ -1,5 +1,5 @@
1
1
  require 'spurious/server/state/init'
2
- require 'spurious/server/state/up'
2
+ require 'spurious/server/state/start'
3
3
  require 'spurious/server/state/stop'
4
4
  require 'spurious/server/state/delete'
5
5
  require 'spurious/server/state/update'
@@ -15,8 +15,8 @@ module Spurious
15
15
  case type.to_sym
16
16
  when :init
17
17
  Init.new(connection, config)
18
- when :up
19
- Up.new(connection, config)
18
+ when :start
19
+ Start.new(connection, config)
20
20
  when :stop
21
21
  Stop.new(connection, config)
22
22
  when :ports
@@ -8,28 +8,58 @@ module Spurious
8
8
  module State
9
9
  class Init < Base
10
10
 
11
+ attr_accessor :completed_containers
12
+
11
13
  def execute!
14
+ this = self
15
+ completed_containers = 0
12
16
 
13
17
  app_config.each do |name, meta|
14
- begin
15
- send "Pulling #{name} from the public repo..."
16
- image_meta = { 'fromImage' => meta[:image]}
17
- image_meta['Env'] = meta[:env] unless meta[:env].nil?
18
- Docker::Image.create(image_meta)
19
-
20
- send "Creating container with name: #{name}"
21
- Docker::Container.create("name" => name, "Image" => meta[:image])
22
-
23
- rescue Exception => e
24
- case e.message
25
- when /409 Conflict/
26
- puts "Container with name: #{name} already exists"
18
+ image_meta = { 'fromImage' => meta[:image]}
19
+ image_meta['Env'] = meta[:env] unless meta[:env].nil?
20
+
21
+ container_operation = Proc.new do
22
+
23
+ begin
24
+ this.send "Creating container with name: #{name}"
25
+ Docker::Container.create("name" => name, "Image" => meta[:image])
26
+ rescue Exception => e
27
+ case e.message
28
+ when /409 Conflict/
29
+ this.error "Container with name: #{name} already exists"
30
+ else
31
+ this.error "Error creating container: #{e.message}"
32
+ end
27
33
  end
34
+
35
+ end
36
+
37
+ container_callback = Proc.new do
38
+ completed_containers = completed_containers + 1
39
+ this.send("#{config.app.length} containers successfully initialized", true) if completed_containers == app_config.length
40
+ end
41
+
42
+ image_operation = Proc.new do
43
+ begin
44
+ this.send "Pulling #{name} from the public repo..."
45
+ Docker::Image.create(image_meta)
46
+ rescue Exception => e
47
+ this.error "Error pulling down image: #{e.message}"
48
+ end
49
+
50
+ end
51
+
52
+ image_callback = Proc.new do
53
+ EM.add_timer(1) do
54
+ EM.defer(container_operation, container_callback)
55
+ end
56
+ end
57
+
58
+ EM.add_timer(1) do
59
+ EM.defer(image_operation, image_callback)
28
60
  end
29
61
  end
30
62
 
31
- send "#{config.app.length} containers successfully initialized"
32
- connection.unbind
33
63
  end
34
64
 
35
65
  end
@@ -27,7 +27,7 @@ module Spurious
27
27
  end
28
28
 
29
29
  end
30
- send ports
30
+ send ports, true
31
31
 
32
32
  connection.unbind
33
33
  rescue Exception => e
@@ -5,7 +5,7 @@ require 'spurious/server/state/base'
5
5
  module Spurious
6
6
  module Server
7
7
  module State
8
- class Up < Base
8
+ class Start < Base
9
9
 
10
10
  def execute!
11
11
  spurious_containers.each do |container|
@@ -15,7 +15,7 @@ module Spurious
15
15
  meta["Links"] = config[:link] unless config[:link].nil?
16
16
  container.start meta
17
17
  end
18
- send "#{spurious_containers.length} containers successfully started"
18
+ send "#{spurious_containers.length} containers successfully started", true
19
19
 
20
20
  connection.unbind
21
21
  rescue Exception => e
@@ -12,7 +12,7 @@ module Spurious
12
12
  send "Stopping container #{container.json["Name"]}..."
13
13
  container.stop
14
14
  end
15
- send "#{spurious_containers.length} containers successfully stopped"
15
+ send "#{spurious_containers.length} containers successfully stopped", true
16
16
 
17
17
  connection.unbind
18
18
  rescue Exception => e
@@ -1,5 +1,5 @@
1
1
  module Spurious
2
2
  module Server
3
- VERSION = "0.0.1"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
data/spec/server_spec.rb CHANGED
@@ -1,13 +1,15 @@
1
1
  require "helper/spec"
2
2
 
3
3
  describe Spurious::Server do
4
+ let(:ip) { '127.0.0.1' }
5
+ let(:port) { 4590 }
4
6
 
5
7
  it "starts a server and receives data" do
6
8
  EventMachine.run {
7
9
 
8
- Spurious::Server.run!
10
+ Spurious::Server.handle(ip, port).call
9
11
  expect_any_instance_of(Spurious::Server::App).to receive(:receive_data)
10
- EventMachine.connect '0.0.0.0', 4590, Helper::Client
12
+ EventMachine.connect ip, port, Helper::Client
11
13
 
12
14
  event_timer
13
15
 
@@ -16,8 +16,8 @@ describe Spurious::Server::State::Init do
16
16
  allow(Docker::Image).to receive(:create).once.with('fromImage' => 'foo/bar').and_return(true)
17
17
  allow(Docker::Container).to receive(:create).once.with('name' => 'foo-bar', 'Image' => 'foo/bar').and_return(true)
18
18
 
19
- expect(connection_double).to receive(:send_data).exactly(3).times
20
- expect(connection_double).to receive(:unbind)
19
+ allow(EM).to receive(:add_timer).twice
20
+
21
21
  state.execute!
22
22
 
23
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spurious-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Jack
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-07 00:00:00.000000000 Z
11
+ date: 2014-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -119,8 +119,8 @@ files:
119
119
  - lib/spurious/server/state/factory.rb
120
120
  - lib/spurious/server/state/init.rb
121
121
  - lib/spurious/server/state/ports.rb
122
+ - lib/spurious/server/state/start.rb
122
123
  - lib/spurious/server/state/stop.rb
123
- - lib/spurious/server/state/up.rb
124
124
  - lib/spurious/server/state/update.rb
125
125
  - lib/spurious/server/version.rb
126
126
  - spec/app_spec.rb
@@ -153,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
153
  version: '0'
154
154
  requirements: []
155
155
  rubyforge_project:
156
- rubygems_version: 2.0.3
156
+ rubygems_version: 2.2.2
157
157
  signing_key:
158
158
  specification_version: 4
159
159
  summary: The server component that the spurious cli client connects to