1
- import boto3
2
- import click
3
- from botocore .exceptions import ClientError
1
+ #!/usr/bin/python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """Webotron: Deploy websites with aws.
5
+
6
+ Webotron automates the process of deploying static websites
7
+ -Configure AWS S3 buckets
8
+ - Create them
9
+ - Set them up for static website hosting
10
+ - Deploy local files to them
11
+ - Configure DNS with Route 53
12
+ - COnfigure a Content Delivery Network and SSL with AWS Cloudfront
13
+ """
14
+
4
15
from pathlib import Path
5
16
import mimetypes
6
17
18
+ import boto3
19
+ from botocore .exceptions import ClientError
20
+ import click
21
+
22
+
7
23
session = boto3 .Session (profile_name = 'pythonAutomation' )
8
24
s3 = session .resource ('s3' )
9
25
26
+
10
27
@click .group ()
11
28
def cli ():
12
- "Webotron deploys websites to AWS"
29
+ """ Webotron deploys websites to AWS."" "
13
30
pass
14
31
32
+
15
33
@cli .command ('list-buckets' )
16
34
def list_buckets ():
17
- ' List all s3 buckets'
35
+ """ List all s3 buckets."""
18
36
for bucket in s3 .buckets .all ():
19
37
print (bucket )
20
38
39
+
21
40
@cli .command ('list-bucket-objects' )
22
41
@click .argument ('bucket' )
23
42
def list_bucket_objects (bucket ):
24
- ' List objects in an s3 bucket'
43
+ """ List objects in an s3 bucket."""
25
44
for obj in s3 .Bucket (bucket ).objects .all ():
26
45
print (obj )
27
46
47
+
28
48
@cli .command ('Setup-bucket' )
29
49
@click .argument ('bucket' )
30
50
def setup_bucket (bucket ):
31
- ' Create and configure S3 bucket'
51
+ """ Create and configure S3 bucket."""
32
52
s3_bucket = None
33
53
34
54
try :
35
55
s3_bucket = s3 .create_bucket (
36
56
Bucket = bucket ,
37
57
CreateBucketConfiguration = {'LocationConstraint' : session .region_name })
38
- except ClientError as e :
39
- if e .response ['Error' ]['Code' ] == 'BucketAlreadyOwnedByYou' :
58
+
59
+ except ClientError as error :
60
+ if error .response ['Error' ]['Code' ] == 'BucketAlreadyOwnedByYou' :
40
61
s3_bucket = s3 .Bucket (bucket )
41
62
else :
42
- raise e
63
+ raise error
43
64
44
65
policy = """
45
66
{
@@ -60,8 +81,7 @@ def setup_bucket(bucket):
60
81
pol = s3_bucket .Policy ()
61
82
pol .put (Policy = policy )
62
83
63
- ws = s3_bucket .Website ()
64
- ws .put (WebsiteConfiguration =
84
+ s3_bucket .Website ().put (WebsiteConfiguration =
65
85
{'ErrorDocument' :{
66
86
'Key' : 'error.html'
67
87
},
@@ -72,28 +92,35 @@ def setup_bucket(bucket):
72
92
73
93
return
74
94
95
+
75
96
def upload_file (s3_bucket , path , key ):
97
+ """Upload path to s3_bucket at key."""
76
98
content_type = mimetypes .guess_type (key )[0 ] or 'text/plain'
77
99
s3_bucket .upload_file (
78
100
path ,
79
101
key ,
80
- ExtraArgs = {'ContentType' : 'text/html' })
102
+ ExtraArgs = {'ContentType' : content_type })
103
+
81
104
82
105
@cli .command ('Sync' )
83
106
@click .argument ('pathname' , type = click .Path (exists = True ))
84
107
@click .argument ('bucket' )
85
108
def sync (pathname , bucket ):
86
- "Sync contents of PATHNAME to BUCKET"
109
+ """ Sync contents of PATHNAME to BUCKET."" "
87
110
s3_bucket = s3 .Bucket (bucket )
88
111
89
112
root = Path (pathname ).expanduser ().resolve ()
90
113
114
+
91
115
def handle_directory (target ):
92
116
for p in target .iterdir ():
93
- if p .is_dir (): handle_directory (p )
94
- if p .is_file (): upload_file (s3_bucket , str (p ), str (p .relative_to (root )))
117
+ if p .is_dir ():\
118
+ handle_directory (p )
119
+ if p .is_file (): upload_file \
120
+ (s3_bucket , str (p ), str (p .relative_to (root )))
95
121
96
122
handle_directory (root )
97
123
124
+
98
125
if __name__ == '__main__' :
99
126
cli ()
0 commit comments