OLD | NEW |
(Empty) | |
| 1 # Copyright 2012 Google Inc. |
| 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 xml.dom.minidom import parseString |
| 16 |
| 17 from gslib.command import Command |
| 18 from gslib.command import COMMAND_NAME |
| 19 from gslib.command import COMMAND_NAME_ALIASES |
| 20 from gslib.command import CONFIG_REQUIRED |
| 21 from gslib.command import FILE_URIS_OK |
| 22 from gslib.command import MAX_ARGS |
| 23 from gslib.command import MIN_ARGS |
| 24 from gslib.command import PROVIDER_URIS_OK |
| 25 from gslib.command import SUPPORTED_SUB_ARGS |
| 26 from gslib.command import URIS_START_ARG |
| 27 from gslib.exception import CommandException |
| 28 from gslib.help_provider import HELP_NAME |
| 29 from gslib.help_provider import HELP_NAME_ALIASES |
| 30 from gslib.help_provider import HELP_ONE_LINE_SUMMARY |
| 31 from gslib.help_provider import HELP_TEXT |
| 32 from gslib.help_provider import HelpType |
| 33 from gslib.help_provider import HELP_TYPE |
| 34 from gslib.util import NO_MAX |
| 35 |
| 36 |
| 37 _detailed_help_text = (""" |
| 38 <B>SYNOPSIS</B> |
| 39 gsutil setwebcfg [-m main_page_suffix] [-e error_page] bucket_uri... |
| 40 |
| 41 |
| 42 <B>DESCRIPTION</B> |
| 43 The Website Configuration feature enables you to configure a Google Cloud |
| 44 Storage bucket to simulate the behavior of a static website. You can define |
| 45 main pages or directory indices (for example, index.html) for buckets and |
| 46 "directories". Also, you can define a custom error page in case a requested |
| 47 resource does not exist. |
| 48 |
| 49 The gstuil setwebcfg command allows you to configure use of web semantics |
| 50 on one or more buckets. The main page suffix and error page parameters are |
| 51 specified as arguments to the -m and -e flags respectively. If either or |
| 52 both parameters are excluded, the corresponding behavior will be disabled |
| 53 on the target bucket. |
| 54 """) |
| 55 |
| 56 def BuildGSWebConfig(main_page_suffix=None, not_found_page=None): |
| 57 config_body_l = ['<WebsiteConfiguration>'] |
| 58 if main_page_suffix: |
| 59 config_body_l.append('<MainPageSuffix>%s</MainPageSuffix>' % |
| 60 main_page_suffix) |
| 61 if not_found_page: |
| 62 config_body_l.append('<NotFoundPage>%s</NotFoundPage>' % |
| 63 not_found_page) |
| 64 config_body_l.append('</WebsiteConfiguration>') |
| 65 return "".join(config_body_l) |
| 66 |
| 67 def BuildS3WebConfig(main_page_suffix=None, error_page=None): |
| 68 config_body_l = ['<WebsiteConfiguration xmlns="http://s3.amazonaws.com/doc/200
6-03-01/">'] |
| 69 if not main_page_suffix: |
| 70 raise CommandException('S3 requires main page / index document') |
| 71 config_body_l.append('<IndexDocument><Suffix>%s</Suffix></IndexDocument>' % |
| 72 main_page_suffix) |
| 73 if error_page: |
| 74 config_body_l.append('<ErrorDocument><Key>%s</Key></ErrorDocument>' % |
| 75 error_page) |
| 76 config_body_l.append('</WebsiteConfiguration>') |
| 77 return "".join(config_body_l) |
| 78 |
| 79 class SetWebcfgCommand(Command): |
| 80 """Implementation of gsutil setwebcfg command.""" |
| 81 |
| 82 # Command specification (processed by parent class). |
| 83 command_spec = { |
| 84 # Name of command. |
| 85 COMMAND_NAME : 'setwebcfg', |
| 86 # List of command name aliases. |
| 87 COMMAND_NAME_ALIASES : [], |
| 88 # Min number of args required by this command. |
| 89 MIN_ARGS : 1, |
| 90 # Max number of args required by this command, or NO_MAX. |
| 91 MAX_ARGS : NO_MAX, |
| 92 # Getopt-style string specifying acceptable sub args. |
| 93 SUPPORTED_SUB_ARGS : 'm:e:', |
| 94 # True if file URIs acceptable for this command. |
| 95 FILE_URIS_OK : False, |
| 96 # True if provider-only URIs acceptable for this command. |
| 97 PROVIDER_URIS_OK : False, |
| 98 # Index in args of first URI arg. |
| 99 URIS_START_ARG : 1, |
| 100 # True if must configure gsutil before running command. |
| 101 CONFIG_REQUIRED : True, |
| 102 } |
| 103 help_spec = { |
| 104 # Name of command or auxiliary help info for which this help applies. |
| 105 HELP_NAME : 'setwebcfg', |
| 106 # List of help name aliases. |
| 107 HELP_NAME_ALIASES : [], |
| 108 # Type of help) |
| 109 HELP_TYPE : HelpType.COMMAND_HELP, |
| 110 # One line summary of this help. |
| 111 HELP_ONE_LINE_SUMMARY : 'Set a main page and/or error page for one or more b
uckets', |
| 112 # The full help text. |
| 113 HELP_TEXT : _detailed_help_text, |
| 114 } |
| 115 |
| 116 |
| 117 # Command entry point. |
| 118 def RunCommand(self): |
| 119 main_page_suffix = None |
| 120 error_page = None |
| 121 if self.sub_opts: |
| 122 for o, a in self.sub_opts: |
| 123 if o == '-m': |
| 124 main_page_suffix = a |
| 125 elif o == '-e': |
| 126 error_page = a |
| 127 |
| 128 uri_args = self.args |
| 129 |
| 130 # Iterate over URIs, expanding wildcards, and setting the website |
| 131 # configuration on each. |
| 132 some_matched = False |
| 133 for uri_str in uri_args: |
| 134 for blr in self.WildcardIterator(uri_str): |
| 135 uri = blr.GetUri() |
| 136 if not uri.names_bucket(): |
| 137 raise CommandException('URI %s must name a bucket for the %s command' |
| 138 % (str(uri), self.command_name)) |
| 139 some_matched = True |
| 140 print 'Setting website config on %s...' % uri |
| 141 uri.set_website_config(main_page_suffix, error_page) |
| 142 if not some_matched: |
| 143 raise CommandException('No URIs matched') |
| 144 |
| 145 return 0 |
| 146 |
| 147 webcfg_full = parseString('<WebsiteConfiguration><MainPageSuffix>main' |
| 148 '</MainPageSuffix><NotFoundPage>404</NotFoundPage>' |
| 149 '</WebsiteConfiguration>').toprettyxml() |
| 150 |
| 151 webcfg_main = parseString('<WebsiteConfiguration>' |
| 152 '<MainPageSuffix>main</MainPageSuffix>' |
| 153 '</WebsiteConfiguration>').toprettyxml() |
| 154 |
| 155 webcfg_error = parseString('<WebsiteConfiguration><NotFoundPage>' |
| 156 '404</NotFoundPage></WebsiteConfiguration>').toprettyxml() |
| 157 |
| 158 webcfg_empty = parseString('<WebsiteConfiguration/>').toprettyxml() |
| 159 |
| 160 num_test_buckets = 3 |
| 161 test_steps = [ |
| 162 ('1. setup webcfg_full', 'echo \'%s\' > $F0' % webcfg_full, 0, None), |
| 163 ('2. apply full config', 'gsutil setwebcfg -m main -e 404 gs://$B0', 0, |
| 164 None), |
| 165 ('3. check full config', 'gsutil getwebcfg gs://$B0 ' |
| 166 '| grep -v \'^Getting website config on\' ' |
| 167 ' > $F1', 0, ('$F0', '$F1')), |
| 168 ('4. setup webcfg_main', 'echo \'%s\' > $F0' % webcfg_main, 0, None), |
| 169 ('5. apply config_main', 'gsutil setwebcfg -m main gs://$B0', 0, None), |
| 170 ('6. check config_main', 'gsutil getwebcfg gs://$B0 ' |
| 171 '| grep -v \'^Getting website config on\' ' |
| 172 ' > $F1', 0, ('$F0', '$F1')), |
| 173 ('7. setup webcfg_error', 'echo \'%s\' > $F0' % webcfg_error, 0, None), |
| 174 ('8. apply config_error', 'gsutil setwebcfg -e 404 gs://$B0', 0, None), |
| 175 ('9. check config_error', 'gsutil getwebcfg gs://$B0 ' |
| 176 '| grep -v \'^Getting website config on\' ' |
| 177 ' > $F1', 0, ('$F0', '$F1')), |
| 178 ('10. setup webcfg_empty', 'echo \'%s\' > $F0' % webcfg_empty, 0, None), |
| 179 ('11. remove config', 'gsutil setwebcfg gs://$B0', 0, None), |
| 180 ('12. check empty config', 'gsutil getwebcfg gs://$B0 ' |
| 181 '| grep -v \'^Getting website config on\' ' |
| 182 ' > $F1', 0, ('$F0', '$F1')), |
| 183 ] |
OLD | NEW |