turbot 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +15 -0
  2. data/README.md +36 -0
  3. data/bin/turbot +17 -0
  4. data/data/cacert.pem +3988 -0
  5. data/lib/turbot/auth.rb +315 -0
  6. data/lib/turbot/cli.rb +38 -0
  7. data/lib/turbot/client/cisaurus.rb +25 -0
  8. data/lib/turbot/client/pgbackups.rb +113 -0
  9. data/lib/turbot/client/rendezvous.rb +111 -0
  10. data/lib/turbot/client/ssl_endpoint.rb +25 -0
  11. data/lib/turbot/client/turbot_postgresql.rb +148 -0
  12. data/lib/turbot/client.rb +757 -0
  13. data/lib/turbot/command/auth.rb +85 -0
  14. data/lib/turbot/command/base.rb +192 -0
  15. data/lib/turbot/command/bots.rb +326 -0
  16. data/lib/turbot/command/config.rb +123 -0
  17. data/lib/turbot/command/help.rb +179 -0
  18. data/lib/turbot/command/keys.rb +115 -0
  19. data/lib/turbot/command/logs.rb +34 -0
  20. data/lib/turbot/command/ssl.rb +43 -0
  21. data/lib/turbot/command/status.rb +51 -0
  22. data/lib/turbot/command/update.rb +47 -0
  23. data/lib/turbot/command/version.rb +23 -0
  24. data/lib/turbot/command.rb +304 -0
  25. data/lib/turbot/deprecated/help.rb +38 -0
  26. data/lib/turbot/deprecated.rb +5 -0
  27. data/lib/turbot/distribution.rb +9 -0
  28. data/lib/turbot/errors.rb +28 -0
  29. data/lib/turbot/excon.rb +11 -0
  30. data/lib/turbot/helpers/log_displayer.rb +70 -0
  31. data/lib/turbot/helpers/pg_dump_restore.rb +115 -0
  32. data/lib/turbot/helpers/turbot_postgresql.rb +213 -0
  33. data/lib/turbot/helpers.rb +521 -0
  34. data/lib/turbot/plugin.rb +165 -0
  35. data/lib/turbot/updater.rb +171 -0
  36. data/lib/turbot/version.rb +3 -0
  37. data/lib/turbot.rb +19 -0
  38. data/lib/vendor/turbot/okjson.rb +598 -0
  39. data/spec/helper/legacy_help.rb +16 -0
  40. data/spec/helper/pg_dump_restore_spec.rb +67 -0
  41. data/spec/schemas/dummy_schema.json +12 -0
  42. data/spec/spec.opts +1 -0
  43. data/spec/spec_helper.rb +220 -0
  44. data/spec/support/display_message_matcher.rb +49 -0
  45. data/spec/support/dummy_api.rb +120 -0
  46. data/spec/support/openssl_mock_helper.rb +8 -0
  47. data/spec/support/organizations_mock_helper.rb +11 -0
  48. data/spec/turbot/auth_spec.rb +214 -0
  49. data/spec/turbot/client/pgbackups_spec.rb +43 -0
  50. data/spec/turbot/client/rendezvous_spec.rb +62 -0
  51. data/spec/turbot/client/ssl_endpoint_spec.rb +48 -0
  52. data/spec/turbot/client/turbot_postgresql_spec.rb +71 -0
  53. data/spec/turbot/client_spec.rb +548 -0
  54. data/spec/turbot/command/auth_spec.rb +38 -0
  55. data/spec/turbot/command/base_spec.rb +66 -0
  56. data/spec/turbot/command/bots_spec.rb +54 -0
  57. data/spec/turbot/command/config_spec.rb +143 -0
  58. data/spec/turbot/command/help_spec.rb +90 -0
  59. data/spec/turbot/command/keys_spec.rb +117 -0
  60. data/spec/turbot/command/logs_spec.rb +60 -0
  61. data/spec/turbot/command/status_spec.rb +48 -0
  62. data/spec/turbot/command/version_spec.rb +16 -0
  63. data/spec/turbot/command_spec.rb +131 -0
  64. data/spec/turbot/helpers/turbot_postgresql_spec.rb +181 -0
  65. data/spec/turbot/helpers_spec.rb +48 -0
  66. data/spec/turbot/plugin_spec.rb +172 -0
  67. data/spec/turbot/updater_spec.rb +44 -0
  68. data/templates/manifest.json +7 -0
  69. data/templates/scraper.py +5 -0
  70. data/templates/scraper.rb +6 -0
  71. metadata +199 -0
@@ -0,0 +1,213 @@
1
+ require "turbot/helpers"
2
+
3
+ module Turbot::Helpers::TurbotPostgresql
4
+
5
+ extend self
6
+ extend Turbot::Helpers
7
+
8
+ class Attachment
9
+ attr_reader :bot, :name, :config_var, :resource_name, :url, :addon, :plan
10
+ def initialize(raw)
11
+ @raw = raw
12
+ @bot = raw['bot']['name']
13
+ @name = raw['name']
14
+ @config_var = raw['config_var']
15
+ @resource_name = raw['resource']['name']
16
+ @url = raw['resource']['value']
17
+ @addon, @plan = raw['resource']['type'].split(':')
18
+ end
19
+
20
+ def starter_plan?
21
+ plan =~ /dev|basic/
22
+ end
23
+
24
+ def display_name
25
+ config_var + (primary_attachment? ? " (DATABASE_URL)" : '')
26
+ end
27
+
28
+ def primary_attachment!
29
+ @primary_attachment = true
30
+ end
31
+
32
+ def primary_attachment?
33
+ @primary_attachment
34
+ end
35
+ end
36
+
37
+ def hpg_resolve(identifier, default=nil)
38
+ $stderr.puts " ! #hpg_resolve is deprecated. Please run `turbot plugins:update` to update your plugins."
39
+ $stderr.puts " ! from: #{caller.first}"
40
+ Resolver.new(bot, api).resolve(identifier , default)
41
+ end
42
+
43
+ class Resolver
44
+ include Turbot::Helpers
45
+ attr_reader :api, :bot_name
46
+ def initialize(bot_name, api)
47
+ @bot_name = bot_name
48
+ @api = api
49
+ end
50
+
51
+ def resolve(identifier, default=nil)
52
+ if identifier =~ /::/
53
+ @bot_name, db_name = identifier.split('::')
54
+ else
55
+ db_name = identifier
56
+ end
57
+
58
+ hpg_resolve(db_name, default)
59
+ end
60
+
61
+ def all_databases
62
+ hpg_databases
63
+ end
64
+
65
+ def database_name_from_url(url)
66
+ vars = bot_config_vars.reject {|key,value| key == 'DATABASE_URL'}
67
+ if var = vars.invert[url]
68
+ var.gsub(/_URL$/, '')
69
+ else
70
+ uri = URI.parse(url)
71
+ "Database on #{uri.host}:#{uri.port || 5432}#{uri.path}"
72
+ end
73
+ end
74
+
75
+ def hpg_addon_name
76
+ if ENV['SHOGUN']
77
+ "shogun-#{ENV['SHOGUN']}"
78
+ else
79
+ ENV['TURBOT_POSTGRESQL_ADDON_NAME'] || 'turbot-postgresql'
80
+ end
81
+ end
82
+
83
+ private
84
+
85
+ def protect_missing_bot
86
+ # in the case where --bot was left out, AND bot::db shorthand was not used, AND no bot autodetect
87
+ unless bot_name
88
+ error("No bot specified.\nRun this command from an bot folder or specify which bot to use with --bot APP.")
89
+ end
90
+ end
91
+
92
+ def bot_config_vars
93
+ protect_missing_bot
94
+ @bot_config_vars ||= api.get_config_vars(bot_name).body
95
+ end
96
+
97
+ def bot_attachments
98
+ protect_missing_bot
99
+ @bot_attachments ||= api.get_attachments(bot_name).body.map { |raw| Attachment.new(raw) }
100
+ end
101
+
102
+ def hpg_databases
103
+ return @hpg_databases if @hpg_databases
104
+ pairs = bot_attachments.select {|att|
105
+ att.addon == hpg_addon_name
106
+ }.map { |att|
107
+ [att.config_var, att]
108
+ }
109
+ @hpg_databases = Hash[ pairs ]
110
+
111
+ if find_database_url_real_attachment
112
+ @hpg_databases['DATABASE_URL'] = find_database_url_real_attachment
113
+ end
114
+
115
+ return @hpg_databases
116
+ end
117
+
118
+ def resource_url(resource)
119
+ api.get_resource(resource).body['value']
120
+ end
121
+
122
+ def forget_config!
123
+ @hpg_databases = nil
124
+ @bot_config_vars = nil
125
+ @bot_attachments = nil
126
+ end
127
+
128
+ def find_database_url_real_attachment
129
+ raw_primary_db_url = bot_config_vars['DATABASE_URL']
130
+ return unless raw_primary_db_url
131
+
132
+ primary_db_url = raw_primary_db_url.split("?").first
133
+ return unless primary_db_url && !primary_db_url.empty?
134
+
135
+ real_config = bot_config_vars.detect {|k,v| k != 'DATABASE_URL' && v == primary_db_url }
136
+ if real_config
137
+ real = hpg_databases[real_config.first]
138
+ real.primary_attachment! if real
139
+ return real
140
+ else
141
+ return nil
142
+ end
143
+ end
144
+
145
+ def match_attachments_by_name(name)
146
+ return [] if name.empty?
147
+ return [name] if hpg_databases[name]
148
+ hpg_databases.keys.grep(%r{#{ name }}i)
149
+ end
150
+
151
+ def hpg_resolve(name, default=nil)
152
+ name = '' if name.nil?
153
+ name = 'DATABASE_URL' if name == 'DATABASE'
154
+
155
+ if hpg_databases.empty?
156
+ error("Your bot has no databases.")
157
+ end
158
+
159
+ found_attachment = nil
160
+ candidates = match_attachments_by_name(name)
161
+ if default && name.empty? && bot_config_vars[default]
162
+ found_attachment = hpg_databases[default]
163
+ elsif candidates.size == 1
164
+ found_attachment = hpg_databases[candidates.first]
165
+ end
166
+
167
+ if found_attachment.nil?
168
+ error("Unknown database#{': ' + name unless name.empty?}. Valid options are: #{hpg_databases.keys.sort.join(", ")}")
169
+ end
170
+
171
+ return found_attachment
172
+ end
173
+ end
174
+
175
+ def hpg_translate_fork_and_follow(addon, config)
176
+ $stderr.puts " ! #hpg_translate_fork_and_follow is deprecated. Update your plugins."
177
+ hpg_translate_db_opts_to_urls(addon, config)
178
+ end
179
+
180
+ def hpg_translate_db_opts_to_urls(addon, config)
181
+ bot_name = bot rescue nil
182
+ resolver = Resolver.new(bot_name, api)
183
+ if addon =~ /^#{resolver.hpg_addon_name}/
184
+ %w[fork follow rollback].each do |opt|
185
+ if val = config[opt]
186
+ unless val.is_a?(String)
187
+ error("--#{opt} requires a database argument.")
188
+ end
189
+
190
+ uri = URI.parse(val) rescue nil
191
+ if uri && uri.scheme && uri.scheme == 'postgres'
192
+ argument_url = uri.to_s
193
+ else
194
+ attachment = resolver.resolve(val)
195
+ if attachment.starter_plan?
196
+ error("#{opt.tr 'f', 'F'} is only available on production databases.")
197
+ end
198
+ argument_url = attachment.url
199
+ end
200
+
201
+ config[opt] = argument_url
202
+ end
203
+ end
204
+ end
205
+ end
206
+
207
+ private
208
+
209
+ def hpg_promote(url)
210
+ api.put_config_vars(bot, "DATABASE_URL" => url)
211
+ end
212
+
213
+ end