| OLD | NEW |
| (Empty) |
| 1 # Copyright 2011 Google Inc. All Rights Reserved. | |
| 2 # | |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 # you may not use this file except in compliance with the License. | |
| 5 # You may obtain a copy of the License at | |
| 6 # | |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 # | |
| 9 # Unless required by applicable law or agreed to in writing, software | |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 # See the License for the specific language governing permissions and | |
| 13 # limitations under the License. | |
| 14 | |
| 15 from gslib.command import Command | |
| 16 from gslib.command import COMMAND_NAME | |
| 17 from gslib.command import COMMAND_NAME_ALIASES | |
| 18 from gslib.command import CONFIG_REQUIRED | |
| 19 from gslib.command import FILE_URIS_OK | |
| 20 from gslib.command import MAX_ARGS | |
| 21 from gslib.command import MIN_ARGS | |
| 22 from gslib.command import PROVIDER_URIS_OK | |
| 23 from gslib.command import SUPPORTED_SUB_ARGS | |
| 24 from gslib.command import URIS_START_ARG | |
| 25 from gslib.exception import CommandException | |
| 26 from gslib.help_provider import HELP_NAME | |
| 27 from gslib.help_provider import HELP_NAME_ALIASES | |
| 28 from gslib.help_provider import HELP_ONE_LINE_SUMMARY | |
| 29 from gslib.help_provider import HELP_TEXT | |
| 30 from gslib.help_provider import HelpType | |
| 31 from gslib.help_provider import HELP_TYPE | |
| 32 from gslib.util import NO_MAX | |
| 33 | |
| 34 _detailed_help_text = (""" | |
| 35 <B>SYNOPSIS</B> | |
| 36 gsutil enablelogging -b logging_bucket [-o log_object_prefix] uri... | |
| 37 | |
| 38 | |
| 39 <B>DESCRIPTION</B> | |
| 40 Google Cloud Storage offers access logs and storage data in the form of | |
| 41 CSV files that you can download and view. Access logs provide information | |
| 42 for all of the requests made on a specified bucket in the last 24 hours, | |
| 43 while the storage logs provide information about the storage consumption of | |
| 44 that bucket for the last 24 hour period. The logs and storage data files | |
| 45 are automatically created as new objects in a bucket that you specify, in | |
| 46 24 hour intervals. | |
| 47 | |
| 48 The gsutil enablelogging command will enable access logging of the | |
| 49 buckets named by the specified uris, outputting log files in the specified | |
| 50 logging_bucket. logging_bucket must already exist, and all URIs must name | |
| 51 buckets (e.g., gs://bucket). For example, the command: | |
| 52 | |
| 53 gsutil enablelogging -b gs://my_logging_bucket -o AccessLog \\ | |
| 54 gs://my_bucket1 gs://my_bucket2 | |
| 55 | |
| 56 will cause all read and write activity to objects in gs://mybucket1 and | |
| 57 gs://mybucket2 to be logged to objects prefixed with the name "AccessLog", | |
| 58 with those log objects written to the bucket gs://my_logging_bucket. | |
| 59 | |
| 60 Note that log data may contain sensitive information, so you should make | |
| 61 sure to set an appropriate default bucket ACL to protect that data. (See | |
| 62 "gsutil help setdefacl".) | |
| 63 | |
| 64 You can check logging status using the gsutil getlogging command. For log | |
| 65 format details see "gsutil help getlogging". | |
| 66 | |
| 67 | |
| 68 <B>OPTIONS</B> | |
| 69 -b bucket Specifies the log bucket. | |
| 70 | |
| 71 -o prefix Specifies the prefix for log object names. Default value | |
| 72 is the bucket name. | |
| 73 """) | |
| 74 | |
| 75 | |
| 76 class EnableLoggingCommand(Command): | |
| 77 """Implementation of gsutil enablelogging command.""" | |
| 78 | |
| 79 # Command specification (processed by parent class). | |
| 80 command_spec = { | |
| 81 # Name of command. | |
| 82 COMMAND_NAME : 'enablelogging', | |
| 83 # List of command name aliases. | |
| 84 COMMAND_NAME_ALIASES : [], | |
| 85 # Min number of args required by this command. | |
| 86 MIN_ARGS : 1, | |
| 87 # Max number of args required by this command, or NO_MAX. | |
| 88 MAX_ARGS : NO_MAX, | |
| 89 # Getopt-style string specifying acceptable sub args. | |
| 90 SUPPORTED_SUB_ARGS : 'b:o:', | |
| 91 # True if file URIs acceptable for this command. | |
| 92 FILE_URIS_OK : False, | |
| 93 # True if provider-only URIs acceptable for this command. | |
| 94 PROVIDER_URIS_OK : False, | |
| 95 # Index in args of first URI arg. | |
| 96 URIS_START_ARG : 0, | |
| 97 # True if must configure gsutil before running command. | |
| 98 CONFIG_REQUIRED : True, | |
| 99 } | |
| 100 help_spec = { | |
| 101 # Name of command or auxiliary help info for which this help applies. | |
| 102 HELP_NAME : 'enablelogging', | |
| 103 # List of help name aliases. | |
| 104 HELP_NAME_ALIASES : ['logging', 'logs', 'log'], | |
| 105 # Type of help: | |
| 106 HELP_TYPE : HelpType.COMMAND_HELP, | |
| 107 # One line summary of this help. | |
| 108 HELP_ONE_LINE_SUMMARY : 'Enable logging on buckets', | |
| 109 # The full help text. | |
| 110 HELP_TEXT : _detailed_help_text, | |
| 111 } | |
| 112 | |
| 113 # Command entry point. | |
| 114 def RunCommand(self): | |
| 115 # Disallow multi-provider enablelogging calls, because the schemas | |
| 116 # differ. | |
| 117 storage_uri = self.UrisAreForSingleProvider(self.args) | |
| 118 if not storage_uri: | |
| 119 raise CommandException('enablelogging command spanning providers not ' | |
| 120 'allowed.') | |
| 121 target_bucket_uri = None | |
| 122 target_prefix = None | |
| 123 for opt, opt_arg in self.sub_opts: | |
| 124 if opt == '-b': | |
| 125 target_bucket_uri = self.suri_builder.StorageUri(opt_arg) | |
| 126 if opt == '-o': | |
| 127 target_prefix = opt_arg | |
| 128 | |
| 129 if not target_bucket_uri: | |
| 130 raise CommandException('enablelogging requires \'-b <log_bucket>\' ' | |
| 131 'option') | |
| 132 if not target_bucket_uri.names_bucket(): | |
| 133 raise CommandException('-b option must specify a bucket uri') | |
| 134 | |
| 135 did_some_work = False | |
| 136 for uri_str in self.args: | |
| 137 for uri in self.WildcardIterator(uri_str).IterUris(): | |
| 138 if uri.names_object(): | |
| 139 raise CommandException('enablelogging cannot be applied to objects') | |
| 140 did_some_work = True | |
| 141 print 'Enabling logging on %s...' % uri | |
| 142 self.proj_id_handler.FillInProjectHeaderIfNeeded( | |
| 143 'enablelogging', storage_uri, self.headers) | |
| 144 uri.enable_logging(target_bucket_uri.bucket_name, target_prefix, False, | |
| 145 self.headers) | |
| 146 if not did_some_work: | |
| 147 raise CommandException('No URIs matched') | |
| 148 | |
| 149 return 0 | |
| OLD | NEW |