Skip to content

Commit dd4d33b

Browse files
wimnatwhiter
authored andcommitted
New module - s3_logging
1 parent d1baa5e commit dd4d33b

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed

cloud/amazon/s3_logging.py

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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+
default: null
30+
region:
31+
description:
32+
- "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."
33+
required: false
34+
default: null
35+
state:
36+
description:
37+
- "Enable or disable logging."
38+
required: false
39+
default: present
40+
choices: [ 'present', 'absent' ]
41+
target_bucket:
42+
description:
43+
- "The bucket to log to."
44+
required: true
45+
default: null
46+
target_prefix:
47+
description:
48+
- "The prefix that should be prepended to the generated log files written to the target_bucket."
49+
required: false
50+
default: no
51+
52+
extends_documentation_fragment: aws
53+
'''
54+
55+
EXAMPLES = '''
56+
# Note: These examples do not set authentication details, see the AWS Guide for details.
57+
58+
- name: Enable logging of s3 bucket mywebsite.com to s3 bucket mylogs
59+
s3_logging:
60+
name: mywebsite.com
61+
target_bucket: mylogs
62+
target_prefix: logs/mywebsite.com
63+
state: present
64+
65+
- name: Remove logging on an s3 bucket
66+
s3_logging:
67+
name: mywebsite.com
68+
state: absent
69+
70+
'''
71+
72+
try:
73+
import boto.ec2
74+
from boto.s3.connection import OrdinaryCallingFormat, Location
75+
from boto.exception import BotoServerError, S3CreateError, S3ResponseError
76+
HAS_BOTO = True
77+
except ImportError:
78+
HAS_BOTO = False
79+
80+
81+
def compare_bucket_logging(bucket, target_bucket, target_prefix):
82+
83+
bucket_log_obj = bucket.get_logging_status()
84+
if bucket_log_obj.target != target_bucket or bucket_log_obj.prefix != target_prefix:
85+
return False
86+
else:
87+
return True
88+
89+
90+
def enable_bucket_logging(connection, module):
91+
92+
bucket_name = module.params.get("name")
93+
target_bucket = module.params.get("target_bucket")
94+
target_prefix = module.params.get("target_prefix")
95+
changed = False
96+
97+
try:
98+
bucket = connection.get_bucket(bucket_name)
99+
except S3ResponseError as e:
100+
module.fail_json(msg=e.message)
101+
102+
try:
103+
if not compare_bucket_logging(bucket, target_bucket, target_prefix):
104+
# Before we can enable logging we must give the log-delivery group WRITE and READ_ACP permissions to the target bucket
105+
try:
106+
target_bucket_obj = connection.get_bucket(target_bucket)
107+
except S3ResponseError as e:
108+
if e.status == 301:
109+
module.fail_json(msg="the logging target bucket must be in the same region as the bucket being logged")
110+
else:
111+
module.fail_json(msg=e.message)
112+
target_bucket_obj.set_as_logging_target()
113+
114+
bucket.enable_logging(target_bucket, target_prefix)
115+
changed = True
116+
117+
except S3ResponseError as e:
118+
module.fail_json(msg=e.message)
119+
120+
module.exit_json(changed=changed)
121+
122+
123+
def disable_bucket_logging(connection, module):
124+
125+
bucket_name = module.params.get("name")
126+
changed = False
127+
128+
try:
129+
bucket = connection.get_bucket(bucket_name)
130+
if not compare_bucket_logging(bucket, None, None):
131+
bucket.disable_logging()
132+
changed = True
133+
except S3ResponseError as e:
134+
module.fail_json(msg=e.message)
135+
136+
module.exit_json(changed=changed)
137+
138+
139+
def main():
140+
141+
argument_spec = ec2_argument_spec()
142+
argument_spec.update(
143+
dict(
144+
name = dict(required=True, default=None),
145+
target_bucket = dict(required=True, default=None),
146+
target_prefix = dict(required=False, default=""),
147+
state = dict(required=False, default='present', choices=['present', 'absent'])
148+
)
149+
)
150+
151+
module = AnsibleModule(argument_spec=argument_spec)
152+
153+
if not HAS_BOTO:
154+
module.fail_json(msg='boto required for this module')
155+
156+
region, ec2_url, aws_connect_params = get_aws_connection_info(module)
157+
158+
if region in ('us-east-1', '', None):
159+
# S3ism for the US Standard region
160+
location = Location.DEFAULT
161+
else:
162+
# Boto uses symbolic names for locations but region strings will
163+
# actually work fine for everything except us-east-1 (US Standard)
164+
location = region
165+
try:
166+
connection = boto.s3.connect_to_region(location, is_secure=True, calling_format=OrdinaryCallingFormat(), **aws_connect_params)
167+
# use this as fallback because connect_to_region seems to fail in boto + non 'classic' aws accounts in some cases
168+
if connection is None:
169+
connection = boto.connect_s3(**aws_connect_params)
170+
except (boto.exception.NoAuthHandlerFound, StandardError), e:
171+
module.fail_json(msg=str(e))
172+
173+
174+
state = module.params.get("state")
175+
176+
if state == 'present':
177+
enable_bucket_logging(connection, module)
178+
elif state == 'absent':
179+
disable_bucket_logging(connection, module)
180+
181+
from ansible.module_utils.basic import *
182+
from ansible.module_utils.ec2 import *
183+
184+
if __name__ == '__main__':
185+
main()

0 commit comments

Comments
 (0)