appengine 0.4.6 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,6 @@
1
- # Copyright 2016 Google Inc. All rights reserved.
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 Google LLC
2
4
  #
3
5
  # Licensed under the Apache License, Version 2.0 (the "License");
4
6
  # you may not use this file except in compliance with the License.
@@ -11,11 +13,9 @@
11
13
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
14
  # See the License for the specific language governing permissions and
13
15
  # limitations under the License.
14
- ;
15
16
 
16
17
 
17
18
  module AppEngine
18
-
19
19
  ##
20
20
  # # AppEngine Rails integration
21
21
  #
@@ -74,16 +74,11 @@ module AppEngine
74
74
  # disable debugging.
75
75
  #
76
76
  class Railtie < ::Rails::Railtie
77
-
78
77
  config.appengine = ::ActiveSupport::OrderedOptions.new
79
78
  config.appengine.define_tasks = true
80
79
 
81
80
  rake_tasks do |app|
82
- if app.config.appengine.define_tasks
83
- require "appengine/tasks"
84
- end
81
+ require "appengine/tasks" if app.config.appengine.define_tasks
85
82
  end
86
-
87
83
  end
88
-
89
84
  end
@@ -1,4 +1,6 @@
1
- # Copyright 2017 Google Inc. All rights reserved.
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 Google LLC
2
4
  #
3
5
  # Licensed under the Apache License, Version 2.0 (the "License");
4
6
  # you may not use this file except in compliance with the License.
@@ -11,19 +13,14 @@
11
13
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
14
  # See the License for the specific language governing permissions and
13
15
  # limitations under the License.
14
- ;
15
16
 
16
- # This file should be loaded or required from a Rakefile to define AppEngine
17
- # related tasks.
18
17
 
19
18
  require "shellwords"
20
19
 
21
20
  require "appengine/util/gcloud"
22
21
  require "appengine/exec"
23
22
 
24
-
25
23
  module AppEngine
26
-
27
24
  ##
28
25
  # # App Engine Rake Tasks.
29
26
  #
@@ -66,6 +63,16 @@ module AppEngine
66
63
  #
67
64
  # The following environment variable parameters are supported:
68
65
  #
66
+ # #### GAE_TIMEOUT
67
+ #
68
+ # Amount of time to wait before appengine:exec terminates the command.
69
+ # Expressed as a string formatted like: "2h15m10s". Default is "10m".
70
+ #
71
+ # #### GAE_PROJECT
72
+ #
73
+ # The ID of your Google Cloud project. If not specified, uses the current
74
+ # project from gcloud.
75
+ #
69
76
  # #### GAE_CONFIG
70
77
  #
71
78
  # Path to the App Engine config file, used when your app has multiple
@@ -74,22 +81,43 @@ module AppEngine
74
81
  #
75
82
  # #### GAE_SERVICE
76
83
  #
77
- # Name of the service to be used. If both `GAE_CONFIG` and `GAE_SERVICE` are
78
- # provided and imply different service names, an error will be raised.
84
+ # Name of the service to be used. Overrides any service name specified in
85
+ # your config file.
86
+ #
87
+ # #### GAE_EXEC_STRATEGY
88
+ #
89
+ # The execution strategy to use. Valid values are "deployment" (which is the
90
+ # default for App Engine Standard apps) and "cloud_build" (which is the
91
+ # default for App Engine Flexible apps).
92
+ #
93
+ # Normally you should leave the strategy set to the default. The main reason
94
+ # to change it is if your app runs on the Flexible Environment and talks to
95
+ # a database over a VPC (using a private IP address). The "cloud_build"
96
+ # strategy used by default for Flexible apps cannot connect to a VPC, so you
97
+ # should use "deployment" in this case. (But note that, otherwise, the
98
+ # "deployment" strategy is significantly slower for apps on the Flexible
99
+ # environment.)
79
100
  #
80
101
  # #### GAE_VERSION
81
102
  #
82
103
  # The version of the service, used to identify which application image to
83
104
  # use to run your command. If not specified, uses the most recently created
84
105
  # version, regardless of whether that version is actually serving traffic.
106
+ # Applies only to the "cloud_build" strategy. (The "deployment" strategy
107
+ # deploys its own temporary version of your app.)
85
108
  #
86
- # #### GAE_TIMEOUT
109
+ # #### GAE_EXEC_WRAPPER_IMAGE
87
110
  #
88
- # Amount of time to wait before appengine:exec terminates the command.
89
- # Expressed as a string formatted like: "2h15m10s". Default is "10m".
111
+ # The fully-qualified name of the wrapper image to use. (This is a Docker
112
+ # image that emulates the App Engine environment in Google Cloud Build for
113
+ # the "cloud_build" strategy, and applies only to that strategy.) Normally,
114
+ # you should not override this unless you are testing a new wrapper.
90
115
  #
91
116
  module Tasks
92
-
117
+ ## @private
118
+ PROJECT_ENV = "GAE_PROJECT"
119
+ ## @private
120
+ STRATEGY_ENV = "GAE_EXEC_STRATEGY"
93
121
  ## @private
94
122
  CONFIG_ENV = "GAE_CONFIG"
95
123
  ## @private
@@ -104,7 +132,6 @@ module AppEngine
104
132
  @defined = false
105
133
 
106
134
  class << self
107
-
108
135
  ##
109
136
  # @private
110
137
  # Define rake tasks.
@@ -123,189 +150,224 @@ module AppEngine
123
150
 
124
151
  def setup_exec_task
125
152
  ::Rake.application.last_description =
126
- "Execute the given command in Google App Engine."
127
- ::Rake::Task.define_task "appengine:exec", [:cmd] do |t, args|
153
+ "Execute the given command in Google App Engine."
154
+ ::Rake::Task.define_task "appengine:exec", [:cmd] do |_t, args|
128
155
  verify_gcloud_and_report_errors
129
- if args[:cmd]
130
- command = ::Shellwords.split args[:cmd]
131
- else
132
- i = (::ARGV.index{ |a| a.to_s == "--" } || -1) + 1
133
- if i == 0
134
- report_error <<-MESSAGE
135
- No command provided for appengine:exec.
136
- Did you remember to delimit it with two dashes? e.g.
137
- bundle exec rake appengine:exec -- bundle exec ruby myscript.rb
138
- For detailed usage instructions, provide two dashes but no command:
139
- bundle exec rake appengine:exec --
140
- MESSAGE
141
- end
142
- command = ::ARGV[i..-1]
143
- if command.empty?
144
- show_usage
145
- exit
146
- end
147
- end
148
- app_exec = Exec.new \
149
- command,
150
- service: ::ENV[SERVICE_ENV],
151
- config_path: ::ENV[CONFIG_ENV],
152
- version: ::ENV[VERSION_ENV],
153
- timeout: ::ENV[TIMEOUT_ENV],
154
- wrapper_image: ::ENV[WRAPPER_IMAGE_ENV]
156
+ command = extract_command args[:cmd], ::ARGV
157
+ app_exec = Exec.new command,
158
+ project: ::ENV[PROJECT_ENV],
159
+ service: ::ENV[SERVICE_ENV],
160
+ config_path: ::ENV[CONFIG_ENV],
161
+ version: ::ENV[VERSION_ENV],
162
+ timeout: ::ENV[TIMEOUT_ENV],
163
+ wrapper_image: ::ENV[WRAPPER_IMAGE_ENV],
164
+ strategy: ::ENV[STRATEGY_ENV]
155
165
  start_and_report_errors app_exec
156
166
  exit
157
167
  end
158
168
  end
159
169
 
170
+ def extract_command cmd, argv
171
+ if cmd
172
+ ::Shellwords.split cmd
173
+ else
174
+ i = (argv.index { |a| a.to_s == "--" } || -1) + 1
175
+ if i.zero?
176
+ report_error <<~MESSAGE
177
+ No command provided for appengine:exec.
178
+ Did you remember to delimit it with two dashes? e.g.
179
+ bundle exec rake appengine:exec -- bundle exec ruby myscript.rb
180
+ For detailed usage instructions, provide two dashes but no command:
181
+ bundle exec rake appengine:exec --
182
+ MESSAGE
183
+ end
184
+ command = ::ARGV[i..-1]
185
+ if command.empty?
186
+ show_usage
187
+ exit
188
+ end
189
+ command
190
+ end
191
+ end
192
+
160
193
  def show_usage
161
- puts <<-USAGE
162
- rake appengine:exec
194
+ puts <<~USAGE
195
+ rake appengine:exec
196
+
197
+ This Rake task executes a given command in the context of an App Engine
198
+ application, using App Engine remote execution. For more information,
199
+ on this capability, see the AppEngine::Exec documentation at
200
+ http://www.rubydoc.info/gems/appengine/AppEngine/Exec
201
+
202
+ The command to be run may either be provided as a rake argument, or as
203
+ command line arguments delimited by two dashes `--`. (The dashes are
204
+ needed to separate your command from rake arguments and flags.)
205
+ For example, to run a production database migration, you can run either
206
+ of the following equivalent commands:
207
+
208
+ bundle exec rake "appengine:exec[bundle exec bin/rails db:migrate]"
209
+ bundle exec rake appengine:exec -- bundle exec bin/rails db:migrate
163
210
 
164
- This Rake task executes a given command in the context of an App Engine
165
- application, using App Engine remote execution. For more information,
166
- on this capability, see the AppEngine::Exec documentation at
167
- http://www.rubydoc.info/gems/appengine/AppEngine/Exec
211
+ To display these usage instructions, provide two dashes but no command:
168
212
 
169
- The command to be run may either be provided as a rake argument, or as
170
- command line arguments delimited by two dashes `--`. (The dashes are
171
- needed to separate your command from rake arguments and flags.)
172
- For example, to run a production database migration, you can run either
173
- of the following equivalent commands:
213
+ bundle exec rake appengine:exec --
174
214
 
175
- bundle exec rake "appengine:exec[bundle exec bin/rails db:migrate]"
176
- bundle exec rake appengine:exec -- bundle exec bin/rails db:migrate
215
+ You may customize the behavior of App Engine execution through a few
216
+ enviroment variable parameters. These are set via the normal mechanism at
217
+ the end of a rake command line but before the double dash. For example, to
218
+ set GAE_CONFIG:
177
219
 
178
- To display these usage instructions, provide two dashes but no command:
220
+ bundle exec rake appengine:exec GAE_CONFIG=myservice.yaml -- bundle exec bin/rails db:migrate
179
221
 
180
- bundle exec rake appengine:exec --
222
+ Be sure to set these parameters before the double dash. Any arguments
223
+ following the double dash are interpreted as part of the command itself.
181
224
 
182
- You may customize the behavior of App Engine execution through a few
183
- enviroment variable parameters. These are set via the normal mechanism at
184
- the end of a rake command line. For example, to set GAE_CONFIG:
225
+ The following environment variable parameters are supported:
185
226
 
186
- bundle exec rake appengine:exec GAE_CONFIG=myservice.yaml -- bundle exec bin/rails db:migrate
227
+ GAE_TIMEOUT
187
228
 
188
- Be sure to set these parameters before the double dash. Any arguments
189
- following the double dash are interpreted as part of the command itself.
229
+ Amount of time to wait before appengine:exec terminates the command.
230
+ Expressed as a string formatted like: "2h15m10s". Default is "10m".
190
231
 
191
- The following environment variable parameters are supported:
232
+ GAE_PROJECT
192
233
 
193
- GAE_CONFIG
234
+ The ID of your Google Cloud project. If not specified, uses the current
235
+ project from gcloud.
194
236
 
195
- Path to the App Engine config file, used when your app has multiple
196
- services, or the config file is otherwise not called `./app.yaml`. The
197
- config file is used to determine the name of the App Engine service.
237
+ GAE_CONFIG
198
238
 
199
- GAE_SERVICE
239
+ Path to the App Engine config file, used when your app has multiple
240
+ services, or the config file is otherwise not called `./app.yaml`. The
241
+ config file is used to determine the name of the App Engine service.
200
242
 
201
- Name of the service to be used. If both `GAE_CONFIG` and `GAE_SERVICE`
202
- are provided and imply different service names, an error will be raised.
243
+ GAE_SERVICE
203
244
 
204
- GAE_VERSION
245
+ Name of the service to be used. Overrides any service name specified in
246
+ your config file.
205
247
 
206
- The version of the service, used to identify which application image to
207
- use to run your command. If not specified, uses the most recently created
208
- version, regardless of whether that version is actually serving traffic.
248
+ GAE_EXEC_STRATEGY
209
249
 
210
- GAE_TIMEOUT
250
+ The execution strategy to use. Valid values are "deployment" (which is the
251
+ default for App Engine Standard apps) and "cloud_build" (which is the
252
+ default for App Engine Flexible apps).
211
253
 
212
- Amount of time to wait before appengine:exec terminates the command.
213
- Expressed as a string formatted like: "2h15m10s". Default is "10m".
254
+ Normally you should leave the strategy set to the default. The main reason
255
+ to change it is if your app runs on the Flexible Environment and talks to
256
+ a database over a VPC (using a private IP address). The "cloud_build"
257
+ strategy used by default for Flexible apps cannot connect to a VPC, so you
258
+ should use "deployment" in this case. (But note that, otherwise, the
259
+ "deployment" strategy is significantly slower for apps on the Flexible
260
+ environment.)
214
261
 
215
- This rake task is provided by the "appengine" gem. To make these tasks
216
- available, add the following line to your Rakefile:
262
+ GAE_VERSION
217
263
 
218
- require "appengine/tasks"
264
+ The version of the service, used to identify which application image to
265
+ use to run your command. If not specified, uses the most recently created
266
+ version, regardless of whether that version is actually serving traffic.
267
+ Applies only to the "cloud_build" strategy. (The "deployment" strategy
268
+ deploys its own temporary version of your app.)
219
269
 
220
- If your app uses Ruby on Rails, the gem provides a railtie that adds its
221
- tasks automatically, so you don't have to do anything beyond adding the
222
- gem to your Gemfile.
270
+ GAE_EXEC_WRAPPER_IMAGE
223
271
 
224
- For more information or to report issues, visit the Github page:
225
- https://github.com/GoogleCloudPlatform/appengine-ruby
272
+ The fully-qualified name of the wrapper image to use. (This is a Docker
273
+ image that emulates the App Engine environment in Google Cloud Build for
274
+ the "cloud_build" strategy, and applies only to that strategy.) Normally,
275
+ you should not override this unless you are testing a new wrapper.
276
+
277
+ This rake task is provided by the "appengine" gem. To make these tasks
278
+ available, add the following line to your Rakefile:
279
+
280
+ require "appengine/tasks"
281
+
282
+ If your app uses Ruby on Rails, the gem provides a railtie that adds its
283
+ tasks automatically, so you don't have to do anything beyond adding the
284
+ gem to your Gemfile.
285
+
286
+ For more information or to report issues, visit the Github page:
287
+ https://github.com/GoogleCloudPlatform/appengine-ruby
226
288
  USAGE
227
289
  end
228
290
 
229
291
  def verify_gcloud_and_report_errors
230
292
  Util::Gcloud.verify!
231
293
  rescue Util::Gcloud::BinaryNotFound
232
- report_error <<-MESSAGE
233
- Could not find the `gcloud` binary in your system path.
234
- This tool requires the Google Cloud SDK. To download and install it,
235
- visit https://cloud.google.com/sdk/downloads
294
+ report_error <<~MESSAGE
295
+ Could not find the `gcloud` binary in your system path.
296
+ This tool requires the Google Cloud SDK. To download and install it,
297
+ visit https://cloud.google.com/sdk/downloads
236
298
  MESSAGE
237
299
  rescue Util::Gcloud::GcloudNotAuthenticated
238
- report_error <<-MESSAGE
239
- The gcloud authorization has not been completed. If you have not yet
240
- initialized the Google Cloud SDK, we recommend running the `gcloud init`
241
- command as described at https://cloud.google.com/sdk/docs/initializing
242
- Alternately, you may log in directly by running `gcloud auth login`.
300
+ report_error <<~MESSAGE
301
+ The gcloud authorization has not been completed. If you have not yet
302
+ initialized the Google Cloud SDK, we recommend running the `gcloud init`
303
+ command as described at https://cloud.google.com/sdk/docs/initializing
304
+ Alternately, you may log in directly by running `gcloud auth login`.
243
305
  MESSAGE
244
306
  rescue Util::Gcloud::ProjectNotSet
245
- report_error <<-MESSAGE
246
- The gcloud project configuration has not been set. If you have not yet
247
- initialized the Google Cloud SDK, we recommend running the `gcloud init`
248
- command as described at https://cloud.google.com/sdk/docs/initializing
249
- Alternately, you may set the default project configuration directly by
250
- running `gcloud config set project <project-name>`.
307
+ report_error <<~MESSAGE
308
+ The gcloud project configuration has not been set. If you have not yet
309
+ initialized the Google Cloud SDK, we recommend running the `gcloud init`
310
+ command as described at https://cloud.google.com/sdk/docs/initializing
311
+ Alternately, you may set the default project configuration directly by
312
+ running `gcloud config set project <project-name>`.
251
313
  MESSAGE
252
314
  end
253
315
 
254
316
  def start_and_report_errors app_exec
255
317
  app_exec.start
256
- rescue Exec::ConfigFileNotFound => ex
257
- report_error <<-MESSAGE
258
- Could not determine which service should run this command because the App
259
- Engine config file "#{ex.config_path}" was not found.
260
- Specify the config file using the GAE_CONFIG argument. e.g.
261
- bundle exec rake appengine:exec GAE_CONFIG=myapp.yaml -- myscript.sh
262
- Alternately, you may specify a service name directly with GAE_SERVICE. e.g.
263
- bundle exec rake appengine:exec GAE_SERVICE=myservice -- myscript.sh
264
- MESSAGE
265
- rescue Exec::BadConfigFileFormat => ex
266
- report_error <<-MESSAGE
267
- Could not determine which service should run this command because the App
268
- Engine config file "#{ex.config_path}" was malformed.
269
- It must be a valid YAML file.
270
- Specify the config file using the GAE_CONFIG argument. e.g.
271
- bundle exec rake appengine:exec GAE_CONFIG=myapp.yaml -- myscript.sh
272
- Alternately, you may specify a service name directly with GAE_SERVICE. e.g.
273
- bundle exec rake appengine:exec GAE_SERVICE=myservice -- myscript.sh
274
- MESSAGE
275
- rescue Exec::NoSuchVersion => ex
276
- if ex.version
277
- report_error <<-MESSAGE
278
- Could not find version "#{ex.version}" of service "#{ex.service}".
279
- Please double-check the version exists. To use the most recent version by
280
- default, omit the GAE_VERSION argument.
281
- MESSAGE
318
+ rescue Exec::ConfigFileNotFound => e
319
+ report_error <<~MESSAGE
320
+ Could not determine which service should run this command because the App
321
+ Engine config file "#{e.config_path}" was not found.
322
+ Specify the config file using the GAE_CONFIG argument. e.g.
323
+ bundle exec rake appengine:exec GAE_CONFIG=myapp.yaml -- myscript.sh
324
+ Alternately, you may specify a service name directly with GAE_SERVICE. e.g.
325
+ bundle exec rake appengine:exec GAE_SERVICE=myservice -- myscript.sh
326
+ MESSAGE
327
+ rescue Exec::BadConfigFileFormat => e
328
+ report_error <<~MESSAGE
329
+ Could not determine which service should run this command because the App
330
+ Engine config file "#{e.config_path}" was malformed.
331
+ It must be a valid YAML file.
332
+ Specify the config file using the GAE_CONFIG argument. e.g.
333
+ bundle exec rake appengine:exec GAE_CONFIG=myapp.yaml -- myscript.sh
334
+ Alternately, you may specify a service name directly with GAE_SERVICE. e.g.
335
+ bundle exec rake appengine:exec GAE_SERVICE=myservice -- myscript.sh
336
+ MESSAGE
337
+ rescue Exec::NoSuchVersion => e
338
+ if e.version
339
+ report_error <<~MESSAGE
340
+ Could not find version "#{e.version}" of service "#{e.service}".
341
+ Please double-check the version exists. To use the most recent version by
342
+ default, omit the GAE_VERSION argument.
343
+ MESSAGE
282
344
  else
283
- report_error <<-MESSAGE
284
- Could not find any versions of service "#{ex.service}".
285
- Please double-check that you have deployed this service. If you want to run
286
- a command against a different service, you may provide a GAE_CONFIG argument
287
- pointing to your App Engine config file, or a GAE_SERVICE argument to specify
288
- a service directly.
289
- MESSAGE
345
+ report_error <<~MESSAGE
346
+ Could not find any versions of service "#{e.service}".
347
+ Please double-check that you have deployed this service. If you want to run
348
+ a command against a different service, you may provide a GAE_CONFIG argument
349
+ pointing to your App Engine config file, or a GAE_SERVICE argument to specify
350
+ a service directly.
351
+ MESSAGE
290
352
  end
291
- rescue Exec::ServiceNameConflict => ex
292
- report_error <<-MESSAGE
293
- The explicit service name "#{ex.service_name}" was requested
294
- but conflicts with the service "#{ex.config_name}" from the
295
- config file "#{ex.config_path}"
296
- You should specify either GAE_SERVICE or GAE_CONFIG but not both.
297
- MESSAGE
353
+ rescue Exec::NoDefaultProject
354
+ report_error <<~MESSAGE
355
+ Could not get the default project from gcloud.
356
+ Please either set the current project using
357
+ gcloud config set project my-project-id
358
+ or specify the project by setting the GAE_PROJECT argument. e.g.
359
+ bundle exec rake appengine:exec GAE_PROJECT=my-project-id -- myscript.sh
360
+ MESSAGE
361
+ rescue Exec::UsageError => e
362
+ report_error e.message
298
363
  end
299
364
 
300
365
  def report_error str
301
366
  ::STDERR.puts str
302
367
  exit 1
303
368
  end
304
-
305
369
  end
306
-
307
370
  end
308
371
  end
309
372
 
310
-
311
373
  ::AppEngine::Tasks.define