Skip to content

Commit 591c22b

Browse files
committed
Merge pull request ansible#680 from wimnat/feature/s3_logging
New module - s3_logging
2 parents 40b85c8 + 9fb2eae commit 591c22b

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed

cloud/amazon/s3_logging.py

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
#!/usr/bin/python
2+
#
3+
# This is a free software: you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation, either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This Ansible library is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this library. If not, see <http://www.gnu.org/licenses/>.
15+
16+
DOCUMENTATION = '''
17+
---
18+
module: s3_logging
19+
short_description: Manage logging facility of an s3 bucket in AWS
20+
description:
21+
- Manage logging facility of an s3 bucket in AWS
22+
version_added: "2.0"
23+
author: Rob White (@wimnat)
24+
options:
25+
name:
26+
description:
27+
- "Name of the s3 bucket."
28+
required: true
29+
region:
30+
description:
31+
- "AWS region to create the bucket in. If not set then the value of the AWS_REGION and EC2_REGION environment variables are checked, followed by the aws_region and ec2_region settings in the Boto config file. If none of those are set the region defaults to the S3 Location: US Standard."
32+
required: false
33+
default: null
34+
state:
35+
description:
36+
- "Enable or disable logging."
37+
required: false
38+
default: present
39+
choices: [ 'present', 'absent' ]
40+
target_bucket:
41+
description:
42+
- "The bucket to log to. Required when state=present."
43+
required: false
44+
default: null
45+
target_prefix:
46+
description:
47+
- "The prefix that should be prepended to the generated log files written to the target_bucket."
48+
required: false
49+
default: ""
50+
51+
extends_documentation_fragment: aws
52+
'''
53+
54+
EXAMPLES = '''
55+
# Note: These examples do not set authentication details, see the AWS Guide for details.
56+
57+
- name: Enable logging of s3 bucket mywebsite.com to s3 bucket mylogs
58+
s3_logging:
59+
name: mywebsite.com
60+
target_bucket: mylogs
61+
target_prefix: logs/mywebsite.com
62+
state: present
63+
64+
- name: Remove logging on an s3 bucket
65+
s3_logging:
66+
name: mywebsite.com
67+
state: absent
68+
69+
'''
70+
71+
try:
72+
import boto.ec2
73+
from boto.s3.connection import OrdinaryCallingFormat, Location
74+
from boto.exception import BotoServerError, S3CreateError, S3ResponseError
75+
HAS_BOTO = True
76+
except ImportError:
77+
HAS_BOTO = False
78+
79+
80+
def compare_bucket_logging(bucket, target_bucket, target_prefix):
81+
82+
bucket_log_obj = bucket.get_logging_status()
83+
if bucket_log_obj.target != target_bucket or bucket_log_obj.prefix != target_prefix:
84+
return False
85+
else:
86+
return True
87+
88+
89+
def enable_bucket_logging(connection, module):
90+
91+
bucket_name = module.params.get("name")
92+
target_bucket = module.params.get("target_bucket")
93+
target_prefix = module.params.get("target_prefix")
94+
changed = False
95+
96+
try:
97+
bucket = connection.get_bucket(bucket_name)
98+
except S3ResponseError as e:
99+
module.fail_json(msg=e.message)
100+
101+
try:
102+
if not compare_bucket_logging(bucket, target_bucket, target_prefix):
103+
# Before we can enable logging we must give the log-delivery group WRITE and READ_ACP permissions to the target bucket
104+
try:
105+
target_bucket_obj = connection.get_bucket(target_bucket)
106+
except S3ResponseError as e:
107+
if e.status == 301:
108+
module.fail_json(msg="the logging target bucket must be in the same region as the bucket being logged")
109+
else:
110+
module.fail_json(msg=e.message)
111+
target_bucket_obj.set_as_logging_target()
112+
113+
bucket.enable_logging(target_bucket, target_prefix)
114+
changed = True
115+
116+
except S3ResponseError as e:
117+
module.fail_json(msg=e.message)
118+
119+
module.exit_json(changed=changed)
120+
121+
122+
def disable_bucket_logging(connection, module):
123+
124+
bucket_name = module.params.get("name")
125+
changed = False
126+
127+
try:
128+
bucket = connection.get_bucket(bucket_name)
129+
if not compare_bucket_logging(bucket, None, None):
130+
bucket.disable_logging()
131+
changed = True
132+
except S3ResponseError as e:
133+
module.fail_json(msg=e.message)
134+
135+
module.exit_json(changed=changed)
136+
137+
138+
def main():
139+
140+
argument_spec = ec2_argument_spec()
141+
argument_spec.update(
142+
dict(
143+
name = dict(required=True),
144+
target_bucket = dict(required=False, default=None),
145+
target_prefix = dict(required=False, default=""),
146+
state = dict(required=False, default='present', choices=['present', 'absent'])
147+
)
148+
)
149+
150+
module = AnsibleModule(argument_spec=argument_spec)
151+
152+
if not HAS_BOTO:
153+
module.fail_json(msg='boto required for this module')
154+
155+
region, ec2_url, aws_connect_params = get_aws_connection_info(module)
156+
157+
if region in ('us-east-1', '', None):
158+
# S3ism for the US Standard region
159+
location = Location.DEFAULT
160+
else:
161+
# Boto uses symbolic names for locations but region strings will
162+
# actually work fine for everything except us-east-1 (US Standard)
163+
location = region
164+
try:
165+
connection = boto.s3.connect_to_region(location, is_secure=True, calling_format=OrdinaryCallingFormat(), **aws_connect_params)
166+
# use this as fallback because connect_to_region seems to fail in boto + non 'classic' aws accounts in some cases
167+
if connection is None:
168+
connection = boto.connect_s3(**aws_connect_params)
169+
except (boto.exception.NoAuthHandlerFound, StandardError), e:
170+
module.fail_json(msg=str(e))
171+
172+
173+
state = module.params.get("state")
174+
175+
if state == 'present':
176+
enable_bucket_logging(connection, module)
177+
elif state == 'absent':
178+
disable_bucket_logging(connection, module)
179+
180+
from ansible.module_utils.basic import *
181+
from ansible.module_utils.ec2 import *
182+
183+
if __name__ == '__main__':
184+
main()

0 commit comments

Comments
 (0)