platform_sh 0.2.5 → 0.2.6.pre

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: 38ac07493dd59becce0aee62387dd18b2b069ff4
4
- data.tar.gz: 8d832102efcb46fd3c7d56b86651c5304d9563f4
3
+ metadata.gz: 78c66403efc02ad1eb05b048c1f667cf7f5b2b99
4
+ data.tar.gz: 59ab8cc66bfc0408f1566a5e1a72bf96899f2f20
5
5
  SHA512:
6
- metadata.gz: 7997e052884f4d86e55674429638bd1fb6f2abe8fdddf08a3630cbcd1eeee2c2237a2ac025eb3a91d952f4d966d3b5ed6b9e80b507420021644a28b66fae1ea3
7
- data.tar.gz: 8fe54482f72f4a8df0f51ae36b2a38c5ba0dfdd62f983bc9c89a57896b71a27e20c55878c9577a86af5c85b11b6c733ae713d4f9fbd5afa93b175175dd614001
6
+ metadata.gz: d970fd9ccb319decd295dc086f5f62006516aefe20a9c350f86028b732dc6815e4d5f18a2e5a915d4bea96cb7096da10a2ad3c8cef2f88b7e73b4adfe115067c
7
+ data.tar.gz: 9e6af8337ed65aad6b0a8d87c5f93f08c598fc774e041887db103f7c5019a1883022c1f375c8fa74b76abb0196c758db867d0355b7c6259f49ee9d336d673f78
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "platform_sh"
5
+
6
+ PlatformSH::local_tunnel_env
data/lib/platform_sh.rb CHANGED
@@ -1,13 +1,18 @@
1
1
  require "platform_sh/version"
2
2
  require "base64"
3
3
  require 'json'
4
+ require 'open3'
4
5
 
5
6
  class PlatformSH
6
7
  'use strict'
7
8
  # Reads Platform.sh configuration from environment and returns a single object
8
9
  def self.config
10
+ conf = {}
11
+ if platform_local_development?
12
+ conf["relationships"] = JSON.parse(Base64.decode64(tunnel_info))
13
+ return conf
14
+ end
9
15
  if on_platform?
10
- conf = {}
11
16
  conf["application"] = read_base64_json('PLATFORM_APPLICATION')
12
17
  conf["application_name"] =ENV["PLATFORM_APPLICATION_NAME"] || nil
13
18
  conf["app_dir"] =ENV["PLATFORM_APP_DIR"] || nil
@@ -27,7 +32,7 @@ class PlatformSH
27
32
  end
28
33
  conf
29
34
  end
30
-
35
+
31
36
  def self.on_platform?
32
37
  ENV.has_key? 'PLATFORM_PROJECT'
33
38
  end
@@ -35,7 +40,7 @@ class PlatformSH
35
40
  def self.is_build_environment?
36
41
  (!ENV.has_key?('PLATFORM_ENVIRONMENT') && ENV.has_key?('PLATFORM_PROJECT'))
37
42
  end
38
-
43
+
39
44
  def self.relationship rel_name, attr
40
45
  on_platform? ? config["relationships"][rel_name].first[attr] : nil
41
46
  end
@@ -54,8 +59,8 @@ class PlatformSH
54
59
  def self.read_app_config
55
60
  JSON.parse(File.read('/run/config.json'))
56
61
  end
57
-
58
-
62
+
63
+
59
64
  #Tries to guess relational database url
60
65
  def self.guess_database_url
61
66
  postgresql_url = self.guess_postgresql_url
@@ -72,36 +77,36 @@ class PlatformSH
72
77
  end
73
78
 
74
79
  def self.guess_url(service_type, platform_scheme, url_template)
75
- services = PlatformSH::config["relationships"].select {|k,v| v[0]["scheme"]==platform_scheme}
76
- case services.length
77
- when 0
78
- $stderr.puts "Could not find an #{service_type}"
79
- return nil
80
- when 1
81
- service = services.first[1][0]
82
- service = service.each_with_object({}){|(k,v), h| h[k.to_sym] = v} #keys need to be symbols
83
- return url_template % service
84
- else
85
- $stderr.puts "More than one #{service_type}, giving up, set configuration by hand"
86
- end
80
+ services = PlatformSH::config["relationships"].select {|k,v| v[0]["scheme"]==platform_scheme}
81
+ case services.length
82
+ when 0
83
+ $stderr.puts "Could not find an #{service_type}"
84
+ return nil
85
+ when 1
86
+ service = services.first[1][0]
87
+ service = service.each_with_object({}){|(k,v), h| h[k.to_sym] = v} #keys need to be symbols
88
+ return url_template % service
89
+ else
90
+ $stderr.puts "More than one #{service_type}, giving up, set configuration by hand"
91
+ end
87
92
  end
88
-
93
+
89
94
  def self.guess_elasticsearch_url
90
95
  self.guess_url("elasticsearch", "http", "http://%{host}:%{port}")
91
96
  end
92
-
97
+
93
98
  def self.guess_redis_url
94
99
  self.guess_url("redis", "redis", "redis://%{host}:%{port}")
95
100
  end
96
-
101
+
97
102
  def self.guess_mongodb_url
98
103
  self.guess_url("mongodb", "mongodb", "mongodb://%{username}:%{password}@%{host}:%{port}/%{path}")
99
104
  end
100
-
105
+
101
106
  def self.guess_solr_url
102
107
  self.guess_url("solr", "solr", "http://%{host}:%{port}/%{path}")
103
108
  end
104
-
109
+
105
110
  def self.guess_mysql_url
106
111
  #fallback to mysql url if mysql2 gem is not loaded
107
112
  if Gem::Specification::find_all_by_name("mysql2").empty?
@@ -111,22 +116,94 @@ class PlatformSH
111
116
  end
112
117
  self.guess_url("mysql", "mysql",template)
113
118
  end
114
-
119
+
115
120
  def self.guess_rabbitmq_url
116
121
  self.guess_url("rabbitmq", "amqp","amqp://%{username}:%{password}@%{host}:%{port}")
117
122
  end
118
-
123
+
119
124
  def self.guess_postgresql_url
120
125
  self.guess_url("postgresql", "pgsql","postgresql://%{username}:%{password}@%{host}:%{port}")
121
126
  end
122
-
127
+
123
128
  def self.export_services_urls
124
- ENV['DATABASE_URL']=PlatformSH::guess_database_url
125
- ENV['MONGODB_URL']=PlatformSH::guess_mongodb_url
126
- ENV['REDIS_URL']=PlatformSH::guess_redis_url
127
- ENV['ELASTICSEARCH_URL']=PlatformSH::guess_elasticsearch_url
128
- ENV['RABBITMQ_URL']=PlatformSH::guess_rabbitmq_url
129
- ENV['SOLR_URL']=PlatformSH::guess_solr_url
129
+ if (on_platform? || platform_local_development?) && !is_build_environment?
130
+ ENV['DATABASE_URL']=PlatformSH::guess_database_url
131
+ ENV['MONGODB_URL']=PlatformSH::guess_mongodb_url
132
+ ENV['REDIS_URL']=PlatformSH::guess_redis_url
133
+ ENV['ELASTICSEARCH_URL']=PlatformSH::guess_elasticsearch_url
134
+ ENV['RABBITMQ_URL']=PlatformSH::guess_rabbitmq_url
135
+ ENV['SOLR_URL']=PlatformSH::guess_solr_url
136
+ else
137
+ $stderr.puts "Can not guess URLS when not on platform"
138
+ end
139
+ end
140
+
141
+ def self.platform_local_development?
142
+ dev_environment = ENV["RACK_ENV"] != "production" && ENV["RAILS_ENV"] != "production"
143
+ dev_environment && cli_installed? && !on_platform?
144
+ end
145
+
146
+ def self.cli_installed?
147
+ begin
148
+ Open3.popen3("platform --version --yes") do |stdin, stdout, stderr, wait_thr|
149
+ err = stdout.gets
150
+ return (err.nil? && err.start_with?("Platform.sh CLI"))
151
+ end
152
+ rescue
153
+ $stderr.puts "WHATTT"
154
+ end
155
+ end
156
+
157
+ def self.tunnel_open?
158
+ Open3.popen3("platform --yes tunnel:info") do |stdin, stdout, stderr, wait_thr|
159
+ err = stderr.gets
160
+ return !(!err.nil? && err.start_with?("No tunnels found"))
161
+ end
162
+ end
163
+
164
+ def self.tunnel_info
165
+ %x(platform tunnel:info -c --yes)
166
+ end
167
+
168
+ def self.tunnel_open
169
+ %x(platform tunnel:open --yes)
170
+ end
171
+
172
+ def self.tunnel_close
173
+ %x(platform tunnel:close --yes)
130
174
  end
131
175
 
176
+ def self.local_tunnel_env
177
+ puts "This is an experimental feature\n"
178
+ %w(INT TERM).each do |sig|
179
+ begin
180
+ trap sig do
181
+ shut_down
182
+ end
183
+ rescue ArgumentError
184
+ puts "Signal #{sig} not supported"
185
+ end
186
+ end
187
+ def self.shut_down
188
+ puts "\nShutting down gracefully..."
189
+ puts tunnel_close
190
+ exit
191
+ end
192
+
193
+ command = ARGV[1]
194
+ if command.nil?
195
+ $stderr.puts "You must supply an executable to run"
196
+ return nil
197
+ end
198
+ if !tunnel_open?
199
+ puts tunnel_open
200
+ else
201
+ puts tunnel_info
202
+ end
203
+ export_services_urls
204
+ Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
205
+ puts "stdout is:" + stdout.read
206
+ $stderr.puts "stderr is:" + stderr.read
207
+ end
208
+ end
132
209
  end
@@ -1,3 +1,3 @@
1
1
  class PlatformSH
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6.pre"
3
3
  end
@@ -0,0 +1,11 @@
1
+ require 'rake'
2
+ require "platform_sh"
3
+
4
+ task :default => [:local_tunnel_env]
5
+
6
+ namespace :platform_sh do
7
+ desc 'Open Tunnels, set services env variables to remote Platform.sh services and runs a command with the env'
8
+ task :local_tunnel_env do
9
+ PlatformSH::local_tunnel_env
10
+ end
11
+ end
data/platform.gemspec CHANGED
@@ -15,8 +15,8 @@ Gem::Specification.new do |spec|
15
15
  spec.license = "MIT"
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
- spec.bindir = "exe"
19
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.bindir = "bin"
19
+ spec.executables = spec.files.grep(%r{^bin/platform.*}) { |f| File.basename(f) }
20
20
  spec.require_paths = ["lib"]
21
21
 
22
22
  spec.add_development_dependency "bundler", "~> 1.12"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: platform_sh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ori Pekelman
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-08 00:00:00.000000000 Z
11
+ date: 2016-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,7 +69,8 @@ dependencies:
69
69
  description: Platform.sh helper gem to ease interacting with the environment
70
70
  email:
71
71
  - ori@platform.sh
72
- executables: []
72
+ executables:
73
+ - platform_local_tunnel_env
73
74
  extensions: []
74
75
  extra_rdoc_files: []
75
76
  files:
@@ -81,9 +82,11 @@ files:
81
82
  - README.md
82
83
  - Rakefile
83
84
  - bin/console
85
+ - bin/platform_local_tunnel_env
84
86
  - bin/setup
85
87
  - lib/platform_sh.rb
86
88
  - lib/platform_sh/version.rb
89
+ - lib/tasks/local_development.rake
87
90
  - platform.gemspec
88
91
  homepage: https://platform.sh
89
92
  licenses:
@@ -100,9 +103,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
103
  version: '0'
101
104
  required_rubygems_version: !ruby/object:Gem::Requirement
102
105
  requirements:
103
- - - ">="
106
+ - - ">"
104
107
  - !ruby/object:Gem::Version
105
- version: '0'
108
+ version: 1.3.1
106
109
  requirements: []
107
110
  rubyforge_project:
108
111
  rubygems_version: 2.5.1