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