rubypitaya 2.14.1 → 2.19.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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rubypitaya/app-template/Gemfile +6 -6
  3. data/lib/rubypitaya/app-template/Gemfile.lock +94 -68
  4. data/lib/rubypitaya/app-template/Makefile +1 -1
  5. data/lib/rubypitaya/app-template/app/bll/player_bll.rb +2 -2
  6. data/lib/rubypitaya/app-template/app/handlers/player_handler.rb +1 -1
  7. data/lib/rubypitaya/app-template/docker-compose.yml +4 -0
  8. data/lib/rubypitaya/app-template/docker/dev/Dockerfile +14 -7
  9. data/lib/rubypitaya/app-template/docker/entrypoint.sh +2 -4
  10. data/lib/rubypitaya/app-template/docker/prod/Dockerfile +27 -18
  11. data/lib/rubypitaya/app-template/docker/wait_for.sh +184 -0
  12. data/lib/rubypitaya/app-template/helm/Chart.yaml +4 -0
  13. data/lib/rubypitaya/app-template/helm/templates/config-maps/nginx-config.yaml +9 -0
  14. data/lib/rubypitaya/app-template/helm/templates/deployments/connector.yaml +38 -0
  15. data/lib/rubypitaya/app-template/helm/templates/deployments/gameserver-nginx.yaml +40 -0
  16. data/lib/rubypitaya/app-template/helm/templates/deployments/rubypitaya.yaml +71 -0
  17. data/lib/rubypitaya/app-template/helm/templates/role-bindings/rubypitaya.yaml +12 -0
  18. data/lib/rubypitaya/app-template/helm/templates/roles/rubypitaya.yaml +31 -0
  19. data/lib/rubypitaya/app-template/helm/templates/service-accounts/rubypitaya.yaml +4 -0
  20. data/lib/rubypitaya/app-template/helm/templates/services/connector.yaml +13 -0
  21. data/lib/rubypitaya/app-template/helm/templates/services/etcd.yaml +18 -0
  22. data/lib/rubypitaya/app-template/helm/templates/services/gameserver-nginx.yaml +20 -0
  23. data/lib/rubypitaya/app-template/helm/templates/services/gameserver.yaml +20 -0
  24. data/lib/rubypitaya/app-template/helm/templates/services/nats.yaml +13 -0
  25. data/lib/rubypitaya/app-template/helm/templates/services/redis.yaml +14 -0
  26. data/lib/rubypitaya/app-template/helm/templates/services/rubypitaya.yaml +13 -0
  27. data/lib/rubypitaya/app-template/helm/templates/statefulsets/etcd.yaml +28 -0
  28. data/lib/rubypitaya/app-template/helm/templates/statefulsets/nats.yaml +26 -0
  29. data/lib/rubypitaya/app-template/helm/templates/statefulsets/redis.yaml +26 -0
  30. data/lib/rubypitaya/app-template/helm/values.yaml +32 -0
  31. data/lib/rubypitaya/core/config.rb +27 -4
  32. data/lib/rubypitaya/core/config_core.rb +4 -21
  33. data/lib/rubypitaya/core/db/migration/0000000001_create_user_migration.rb +1 -1
  34. data/lib/rubypitaya/core/handler_base.rb +11 -0
  35. data/lib/rubypitaya/core/handler_router.rb +3 -11
  36. data/lib/rubypitaya/core/http_routes.rb +30 -0
  37. data/lib/rubypitaya/core/main.rb +11 -8
  38. data/lib/rubypitaya/core/parameters.rb +449 -138
  39. data/lib/rubypitaya/core/spec-helpers/config_spec_helper.rb +2 -2
  40. data/lib/rubypitaya/core/templates/template_migration.rb.erb +1 -1
  41. data/lib/rubypitaya/version.rb +1 -1
  42. metadata +43 -37
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d6e77cbe864f72313b664b1bc09412eb1a4e72e585783c9926cb81f904e4fa1b
4
- data.tar.gz: 1e1aa371b0ef4c57f19612b30706f154783ff4a3ad9c20d45571aedc03041e20
3
+ metadata.gz: 29e44f8dac6ea62a79303ae177f79515dc38309175c0e30b54fd95c0a7a7237e
4
+ data.tar.gz: cd1a3663b60d5dbd62e521d7590560dad861f6eb857033001cef259ada067149
5
5
  SHA512:
6
- metadata.gz: b930dcb7634a04e77424b0452580651af68127c63e326cd1917f6bed4ad39140bc7ae484f50a13e223bb994f23ef33a559fa6b2a6907d8031d589b7918ab4016
7
- data.tar.gz: 711ae4f8291d2916111abaa5c82fe99e31901d731e38eece1a50627e2769054f06c63a44ca24c5b433bfdd3df5419629a2d4bfa3ceb101a8b9a9d435eb874034
6
+ metadata.gz: baee0fc660cb49a6f68ef705ea371b29e1f6dbb7da0db714d88d25ffc593fd8b94155a296ae8341454a9b79e33a3251a471b19256dd9c1bcf49ee84182692ea4
7
+ data.tar.gz: adf400ab74394136cfdfe99688368996f39ddc4cbd9f57671d386ea2fa7bbf1074927fdb4a51da9239b8998e546a8db2aacb73a16aea78ee682f474b3072039b
@@ -1,11 +1,11 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem 'rubypitaya', '2.14.1'
3
+ gem 'rubypitaya', '2.19.0'
4
4
 
5
5
  group :development do
6
- gem 'pry', '0.12.2'
7
- gem 'bundler', '1.17.2'
8
- gem 'rspec', '3.8.0'
9
- gem 'listen', '3.2.1'
10
- gem 'cucumber', '2.4.0'
6
+ gem 'pry', '0.14.0'
7
+ gem 'bundler', '2.1.4'
8
+ gem 'rspec', '3.10.0'
9
+ gem 'listen', '3.4.1'
10
+ gem 'cucumber', '5.3.0'
11
11
  end
@@ -1,99 +1,123 @@
1
1
  GEM
2
2
  remote: https://rubygems.org/
3
3
  specs:
4
- activemodel (6.0.2)
5
- activesupport (= 6.0.2)
6
- activerecord (6.0.2)
7
- activemodel (= 6.0.2)
8
- activesupport (= 6.0.2)
9
- activesupport (6.0.2)
4
+ activemodel (6.1.3)
5
+ activesupport (= 6.1.3)
6
+ activerecord (6.1.3)
7
+ activemodel (= 6.1.3)
8
+ activesupport (= 6.1.3)
9
+ activesupport (6.1.3)
10
10
  concurrent-ruby (~> 1.0, >= 1.0.2)
11
- i18n (>= 0.7, < 2)
12
- minitest (~> 5.1)
13
- tzinfo (~> 1.1)
14
- zeitwerk (~> 2.2)
11
+ i18n (>= 1.6, < 2)
12
+ minitest (>= 5.1)
13
+ tzinfo (~> 2.0)
14
+ zeitwerk (~> 2.3)
15
15
  builder (3.2.4)
16
16
  coderay (1.1.3)
17
- concurrent-ruby (1.1.7)
18
- cucumber (2.4.0)
19
- builder (>= 2.1.2)
20
- cucumber-core (~> 1.5.0)
21
- cucumber-wire (~> 0.0.1)
22
- diff-lcs (>= 1.1.3)
23
- gherkin (~> 4.0)
24
- multi_json (>= 1.7.5, < 2.0)
25
- multi_test (>= 0.1.2)
26
- cucumber-core (1.5.0)
27
- gherkin (~> 4.0)
28
- cucumber-wire (0.0.1)
17
+ concurrent-ruby (1.1.8)
18
+ cucumber (5.3.0)
19
+ builder (~> 3.2, >= 3.2.4)
20
+ cucumber-core (~> 8.0, >= 8.0.1)
21
+ cucumber-create-meta (~> 2.0, >= 2.0.2)
22
+ cucumber-cucumber-expressions (~> 10.3, >= 10.3.0)
23
+ cucumber-gherkin (~> 15.0, >= 15.0.2)
24
+ cucumber-html-formatter (~> 9.0, >= 9.0.0)
25
+ cucumber-messages (~> 13.1, >= 13.1.0)
26
+ cucumber-wire (~> 4.0, >= 4.0.1)
27
+ diff-lcs (~> 1.4, >= 1.4.4)
28
+ multi_test (~> 0.1, >= 0.1.2)
29
+ sys-uname (~> 1.2, >= 1.2.1)
30
+ cucumber-core (8.0.1)
31
+ cucumber-gherkin (~> 15.0, >= 15.0.2)
32
+ cucumber-messages (~> 13.0, >= 13.0.1)
33
+ cucumber-tag-expressions (~> 2.0, >= 2.0.4)
34
+ cucumber-create-meta (2.0.4)
35
+ cucumber-messages (~> 13.1, >= 13.1.0)
36
+ sys-uname (~> 1.2, >= 1.2.1)
37
+ cucumber-cucumber-expressions (10.3.0)
38
+ cucumber-gherkin (15.0.2)
39
+ cucumber-messages (~> 13.0, >= 13.0.1)
40
+ cucumber-html-formatter (9.0.0)
41
+ cucumber-messages (~> 13.0, >= 13.0.1)
42
+ cucumber-messages (13.2.1)
43
+ protobuf-cucumber (~> 3.10, >= 3.10.8)
44
+ cucumber-tag-expressions (2.0.4)
45
+ cucumber-wire (4.0.1)
46
+ cucumber-core (~> 8.0, >= 8.0.1)
47
+ cucumber-cucumber-expressions (~> 10.3, >= 10.3.0)
48
+ cucumber-messages (~> 13.0, >= 13.0.1)
29
49
  diff-lcs (1.4.4)
30
50
  etcdv3 (0.10.2)
31
51
  grpc (~> 1.17)
32
52
  eventmachine (1.2.7)
33
- ffi (1.14.2)
34
- gherkin (4.1.3)
35
- google-protobuf (3.14.0)
36
- googleapis-common-protos-types (1.0.5)
37
- google-protobuf (~> 3.11)
38
- grpc (1.34.0)
39
- google-protobuf (~> 3.13)
53
+ ffi (1.15.0)
54
+ google-protobuf (3.15.5)
55
+ googleapis-common-protos-types (1.0.6)
56
+ google-protobuf (~> 3.14)
57
+ grpc (1.36.0)
58
+ google-protobuf (~> 3.14)
40
59
  googleapis-common-protos-types (~> 1.0)
41
- i18n (1.8.6)
60
+ i18n (1.8.9)
42
61
  concurrent-ruby (~> 1.0)
43
- listen (3.2.1)
62
+ listen (3.4.1)
44
63
  rb-fsevent (~> 0.10, >= 0.10.3)
45
64
  rb-inotify (~> 0.9, >= 0.9.10)
46
- method_source (0.9.2)
65
+ method_source (1.0.0)
47
66
  middleware (0.1.0)
48
- minitest (5.14.2)
67
+ minitest (5.14.4)
49
68
  multi_json (1.15.0)
50
69
  multi_test (0.1.2)
51
70
  mustermann (1.1.1)
52
71
  ruby2_keywords (~> 0.0.1)
53
72
  nats (0.11.0)
54
73
  eventmachine (~> 1.2, >= 1.2)
55
- ostruct (0.1.0)
56
- pg (0.21.0)
74
+ ostruct (0.3.3)
75
+ pg (1.2.3)
57
76
  protobuf (3.10.0)
58
77
  activesupport (>= 3.2)
59
78
  middleware
60
79
  thor
61
80
  thread_safe
62
- pry (0.12.2)
63
- coderay (~> 1.1.0)
64
- method_source (~> 0.9.0)
81
+ protobuf-cucumber (3.10.8)
82
+ activesupport (>= 3.2)
83
+ middleware
84
+ thor
85
+ thread_safe
86
+ pry (0.14.0)
87
+ coderay (~> 1.1)
88
+ method_source (~> 1.0)
65
89
  rack (2.2.3)
66
90
  rack-protection (2.1.0)
67
91
  rack
68
- rake (10.0.0)
92
+ rake (13.0.3)
69
93
  rb-fsevent (0.10.4)
70
94
  rb-inotify (0.10.1)
71
95
  ffi (~> 1.0)
72
- redis (4.1.3)
73
- rspec (3.8.0)
74
- rspec-core (~> 3.8.0)
75
- rspec-expectations (~> 3.8.0)
76
- rspec-mocks (~> 3.8.0)
77
- rspec-core (3.8.2)
78
- rspec-support (~> 3.8.0)
79
- rspec-expectations (3.8.6)
96
+ redis (4.2.5)
97
+ rspec (3.10.0)
98
+ rspec-core (~> 3.10.0)
99
+ rspec-expectations (~> 3.10.0)
100
+ rspec-mocks (~> 3.10.0)
101
+ rspec-core (3.10.1)
102
+ rspec-support (~> 3.10.0)
103
+ rspec-expectations (3.10.1)
80
104
  diff-lcs (>= 1.2.0, < 2.0)
81
- rspec-support (~> 3.8.0)
82
- rspec-mocks (3.8.2)
105
+ rspec-support (~> 3.10.0)
106
+ rspec-mocks (3.10.2)
83
107
  diff-lcs (>= 1.2.0, < 2.0)
84
- rspec-support (~> 3.8.0)
85
- rspec-support (3.8.3)
86
- ruby2_keywords (0.0.2)
87
- rubypitaya (2.14.1)
88
- activerecord (= 6.0.2)
108
+ rspec-support (~> 3.10.0)
109
+ rspec-support (3.10.2)
110
+ ruby2_keywords (0.0.4)
111
+ rubypitaya (2.19.0)
112
+ activerecord (= 6.1.3)
89
113
  etcdv3 (= 0.10.2)
90
114
  eventmachine (= 1.2.7)
91
115
  nats (= 0.11.0)
92
- ostruct (= 0.1.0)
93
- pg (= 0.21.0)
116
+ ostruct (= 0.3.3)
117
+ pg (= 1.2.3)
94
118
  protobuf (= 3.10.0)
95
- rake (= 10.0)
96
- redis (= 4.1.3)
119
+ rake (= 13.0.3)
120
+ redis (= 4.2.5)
97
121
  sinatra (= 2.1.0)
98
122
  sinatra-contrib (= 2.1.0)
99
123
  sinatra (2.1.0)
@@ -107,23 +131,25 @@ GEM
107
131
  rack-protection (= 2.1.0)
108
132
  sinatra (= 2.1.0)
109
133
  tilt (~> 2.0)
110
- thor (1.0.1)
134
+ sys-uname (1.2.2)
135
+ ffi (~> 1.1)
136
+ thor (1.1.0)
111
137
  thread_safe (0.3.6)
112
138
  tilt (2.0.10)
113
- tzinfo (1.2.9)
114
- thread_safe (~> 0.1)
139
+ tzinfo (2.0.4)
140
+ concurrent-ruby (~> 1.0)
115
141
  zeitwerk (2.4.2)
116
142
 
117
143
  PLATFORMS
118
144
  ruby
119
145
 
120
146
  DEPENDENCIES
121
- bundler (= 1.17.2)
122
- cucumber (= 2.4.0)
123
- listen (= 3.2.1)
124
- pry (= 0.12.2)
125
- rspec (= 3.8.0)
126
- rubypitaya (= 2.14.1)
147
+ bundler (= 2.1.4)
148
+ cucumber (= 5.3.0)
149
+ listen (= 3.4.1)
150
+ pry (= 0.14.0)
151
+ rspec (= 3.10.0)
152
+ rubypitaya (= 2.19.0)
127
153
 
128
154
  BUNDLED WITH
129
- 1.17.2
155
+ 2.1.4
@@ -41,7 +41,7 @@ test-cucumber:
41
41
  ## Update gems dependencies on Gemfile.lock
42
42
  update-dependencies:
43
43
  @rm -f Gemfile.lock
44
- @docker run --rm -v "$(PWD)":/usr/src/app -w /usr/src/app ruby:2.6.6 bundle install
44
+ @docker run --rm -v "$(PWD)":/usr/src/app -w /usr/src/app ruby:2.7.2-slim bundle install
45
45
 
46
46
  ## + Improve metagame
47
47
 
@@ -1,11 +1,11 @@
1
1
  class PlayerBLL
2
2
 
3
3
  def create_new_player(setup, config)
4
- name = config['initial_player']['name']
4
+ name = config['initial_player'][:name]
5
5
  gold = setup['initial_player.wallet.gold']
6
6
 
7
7
  player = Player.new(name: name, gold: gold, user: User.new)
8
- player.save
8
+ player.save!
9
9
  player
10
10
  end
11
11
  end
@@ -17,7 +17,7 @@ class PlayerHandler < RubyPitaya::HandlerBase
17
17
  # Given you have the following file "app/config/initial_player.json"
18
18
  # And this json content is {'name': 'Guest'}
19
19
  # And you can get the initial player name
20
- # Then you can run the following code: @config['initial_player']['name']
20
+ # Then you can run the following code: @config['initial_player'][:name]
21
21
  #
22
22
  # - @params
23
23
  # - info: Special hash with the request parameters
@@ -69,9 +69,13 @@ services:
69
69
  ETCD_LEASE_SECONDS: '60'
70
70
  REDIS_URL: 'redis://redis:6379'
71
71
  DATABASE_HOST: 'db'
72
+ DATABASE_PORT: 5432
72
73
  DATABASE_USER: 'postgres'
73
74
  DATABASE_PASSWORD: 'postgres'
74
75
  DATABASE_NAME: 'ruby_pitaya'
76
+ HTTP_AUTH_ENABLED: 'false'
77
+ HTTP_AUTH_USER: 'user'
78
+ HTTP_AUTH_PASS: 'pass'
75
79
 
76
80
  rubypitaya-console:
77
81
  <<: *rubypitaya
@@ -1,19 +1,26 @@
1
- FROM ruby:2.6.6
1
+ FROM ruby:2.7.2-slim
2
2
 
3
- RUN apt update
4
- RUN apt install -y git vim postgresql-client docker.io --no-install-recommends
3
+ ENV LANG=C.UTF-8
4
+ ENV LC_ALL=C.UTF-8
5
+
6
+ RUN apt update && \
7
+ apt install -y --no-install-recommends \
8
+ netcat \
9
+ libpq-dev \
10
+ build-essential \
11
+ && rm -rf /var/lib/apt/lists/*
5
12
 
6
13
  WORKDIR /app/rubypitaya/
7
14
 
8
15
  COPY Gemfile Gemfile.lock ./
9
16
 
10
- RUN bundle install
17
+ RUN bundle config --global jobs 4 && \
18
+ bundle config --global set clean 'true' \
19
+ bundle config --global git.allow_insecure true && \
20
+ bundle install
11
21
 
12
22
  COPY . .
13
23
 
14
- ENV LANG=C.UTF-8
15
- ENV LC_ALL=C.UTF-8
16
-
17
24
  ENTRYPOINT ["./docker/entrypoint.sh"]
18
25
 
19
26
  CMD ["bundle", "exec", "rubypitaya", "run"]
@@ -3,10 +3,8 @@
3
3
  cmd="$@"
4
4
 
5
5
  echo "=> Waiting for postgres"
6
- until PGPASSWORD=$DATABASE_PASSWORD psql -h "$DATABASE_HOST" -U "$DATABASE_USER" -c '\q' > /dev/null 2>&1; do
7
- echo "=> Waiting for Postgres..."
8
- sleep 1
9
- done
6
+
7
+ ./docker/wait_for.sh $DATABASE_HOST:$DATABASE_PORT
10
8
 
11
9
  echo "=> Postgres is ready"
12
10
 
@@ -1,35 +1,44 @@
1
- FROM ruby:2.6.6 as builder
1
+ FROM ruby:2.7.2-slim as builder
2
2
 
3
- WORKDIR /app/rubypitaya/
3
+ RUN apt update && \
4
+ apt install -y --no-install-recommends \
5
+ curl \
6
+ libpq-dev \
7
+ build-essential \
8
+ && rm -rf /var/lib/apt/lists/*
4
9
 
5
10
  COPY Gemfile Gemfile.lock ./
6
11
 
7
- RUN bundle install --without development
12
+ RUN bundle config --global jobs 4 && \
13
+ bundle config --global set clean 'true' \
14
+ bundle config --global git.allow_insecure true && \
15
+ bundle config --global without "development test" && \
16
+ bundle install
8
17
 
9
- COPY . .
18
+ # RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
10
19
 
11
- FROM ruby:2.6.6-slim
20
+ FROM ruby:2.7.2-slim
12
21
 
13
- RUN apt update && apt install -y postgresql-client curl python3 bash vim --no-install-recommends
14
- RUN rm -rf /var/lib/apt/lists/*
22
+ ENV LANG=C.UTF-8
23
+ ENV LC_ALL=C.UTF-8
15
24
 
16
- RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
17
- RUN chmod +x ./kubectl
18
- RUN mv ./kubectl /usr/local/bin
25
+ RUN apt update && \
26
+ apt install -y --no-install-recommends \
27
+ netcat \
28
+ libpq5 \
29
+ && rm -rf /var/lib/apt/lists/*
19
30
 
20
- COPY --from=builder /usr/local/etc /usr/local/etc
21
31
  COPY --from=builder /usr/local/bundle /usr/local/bundle
22
- COPY --from=builder /usr/local/bin/ruby /usr/local/bin/ruby
23
- COPY --from=builder /usr/local/lib/ruby/gems/2.6.0 /usr/local/lib/ruby/gems/2.6.0
32
+ RUN rm -rf /usr/local/bundle/cache
33
+ RUN rm -rf /usr/local/bundle/build_info
34
+ RUN rm -rf /usr/local/bundle/doc
24
35
 
25
- COPY --from=builder /app/rubypitaya /app/rubypitaya
36
+ # COPY --from=builder /kubectl /usr/local/bin/kubectl
37
+ # RUN chmod +x /usr/local/bin/kubectl
26
38
 
27
39
  WORKDIR /app/rubypitaya/
28
40
 
29
- RUN rm -rf vendor/
30
-
31
- ENV LANG=C.UTF-8
32
- ENV LC_ALL=C.UTF-8
41
+ COPY . .
33
42
 
34
43
  ENTRYPOINT ["./docker/entrypoint.sh"]
35
44
 
@@ -0,0 +1,184 @@
1
+ #!/bin/sh
2
+
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (c) 2017 Eficode Oy
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+ set -- "$@" -- "$TIMEOUT" "$QUIET" "$PROTOCOL" "$HOST" "$PORT" "$result"
26
+ TIMEOUT=15
27
+ QUIET=0
28
+ # The protocol to make the request with, either "tcp" or "http"
29
+ PROTOCOL="tcp"
30
+
31
+ echoerr() {
32
+ if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
33
+ }
34
+
35
+ usage() {
36
+ exitcode="$1"
37
+ cat << USAGE >&2
38
+ Usage:
39
+ $cmdname host:port|url [-t timeout] [-- command args]
40
+ -q | --quiet Do not output any status messages
41
+ -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
42
+ -- COMMAND ARGS Execute command with args after the test finishes
43
+ USAGE
44
+ exit "$exitcode"
45
+ }
46
+
47
+ wait_for() {
48
+ case "$PROTOCOL" in
49
+ tcp)
50
+ if ! command -v nc >/dev/null; then
51
+ echoerr 'nc command is missing!'
52
+ exit 1
53
+ fi
54
+ ;;
55
+ wget)
56
+ if ! command -v wget >/dev/null; then
57
+ echoerr 'nc command is missing!'
58
+ exit 1
59
+ fi
60
+ ;;
61
+ esac
62
+
63
+ while :; do
64
+ case "$PROTOCOL" in
65
+ tcp)
66
+ nc -z "$HOST" "$PORT" > /dev/null 2>&1
67
+ ;;
68
+ http)
69
+ wget --timeout=1 -q "$HOST" -O /dev/null > /dev/null 2>&1
70
+ ;;
71
+ *)
72
+ echoerr "Unknown protocol '$PROTOCOL'"
73
+ exit 1
74
+ ;;
75
+ esac
76
+
77
+ result=$?
78
+
79
+ if [ $result -eq 0 ] ; then
80
+ if [ $# -gt 7 ] ; then
81
+ for result in $(seq $(($# - 7))); do
82
+ result=$1
83
+ shift
84
+ set -- "$@" "$result"
85
+ done
86
+
87
+ TIMEOUT=$2 QUIET=$3 PROTOCOL=$4 HOST=$5 PORT=$6 result=$7
88
+ shift 7
89
+ exec "$@"
90
+ fi
91
+ exit 0
92
+ fi
93
+
94
+ if [ "$TIMEOUT" -le 0 ]; then
95
+ break
96
+ fi
97
+ TIMEOUT=$((TIMEOUT - 1))
98
+
99
+ sleep 1
100
+ done
101
+ echo "Operation timed out" >&2
102
+ exit 1
103
+ }
104
+
105
+ while :; do
106
+ case "$1" in
107
+ http://*|https://*)
108
+ HOST="$1"
109
+ PROTOCOL="http"
110
+ shift 1
111
+ ;;
112
+ *:* )
113
+ HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
114
+ PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
115
+ shift 1
116
+ ;;
117
+ -q | --quiet)
118
+ QUIET=1
119
+ shift 1
120
+ ;;
121
+ -q-*)
122
+ QUIET=0
123
+ echoerr "Unknown option: $1"
124
+ usage 1
125
+ ;;
126
+ -q*)
127
+ QUIET=1
128
+ result=$1
129
+ shift 1
130
+ set -- -"${result#-q}" "$@"
131
+ ;;
132
+ -t | --timeout)
133
+ TIMEOUT="$2"
134
+ shift 2
135
+ ;;
136
+ -t*)
137
+ TIMEOUT="${1#-t}"
138
+ shift 1
139
+ ;;
140
+ --timeout=*)
141
+ TIMEOUT="${1#*=}"
142
+ shift 1
143
+ ;;
144
+ --)
145
+ shift
146
+ break
147
+ ;;
148
+ --help)
149
+ usage 0
150
+ ;;
151
+ -*)
152
+ QUIET=0
153
+ echoerr "Unknown option: $1"
154
+ usage 1
155
+ ;;
156
+ *)
157
+ QUIET=0
158
+ echoerr "Unknown argument: $1"
159
+ usage 1
160
+ ;;
161
+ esac
162
+ done
163
+
164
+ if ! [ "$TIMEOUT" -ge 0 ] 2>/dev/null; then
165
+ echoerr "Error: invalid timeout '$TIMEOUT'"
166
+ usage 3
167
+ fi
168
+
169
+ case "$PROTOCOL" in
170
+ tcp)
171
+ if [ "$HOST" = "" -o "$PORT" = "" ]; then
172
+ echoerr "Error: you need to provide a host and port to test."
173
+ usage 2
174
+ fi
175
+ ;;
176
+ http)
177
+ if [ "$HOST" = "" ]; then
178
+ echoerr "Error: you need to provide a host to test."
179
+ usage 2
180
+ fi
181
+ ;;
182
+ esac
183
+
184
+ wait_for "$@"