-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtasks.py
123 lines (103 loc) · 3.8 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from invoke import task, run
from os import path
import boto3
import configparser
import webbrowser
@task
def initialize(ctx):
if path.isfile('config.ini'):
print('Project has already been initialized. Returning...')
return
app_name = input("Please enter a name for your app: ")
aws_profile = input("Please provide the AWS Profile you wish to use (press enter for default): ") or 'default'
aws_region = input("Please provide the AWS Region you wish to use: ")
ctx.run(f'npx create-react-app ui-{app_name}')
ctx.run(f'chalice new-project api-{app_name}')
createConfigFile(ctx, app_name, aws_profile, aws_region)
@task
def createConfigFile(ctx, app_name, aws_profile, aws_region):
if aws_region:
session = boto3.Session(profile_name=aws_profile, region_name=aws_region)
else:
session = boto3.Session(profile_name=aws_profile)
bucket_name = app_name.lower() + "-bucket"
static_url = 'http://' + bucket_name + '.s3-website.' + session.region_name + '.amazonaws.com'
config = configparser.ConfigParser()
config.add_section('ProjectConfiguration')
config.set('ProjectConfiguration', 'name', app_name)
config.set('ProjectConfiguration', 'bucketName', bucket_name)
config.set('ProjectConfiguration', 'uiUrl', static_url)
config.set('ProjectConfiguration', 'uiDirectory', f'ui-{app_name}')
config.set('ProjectConfiguration', 'apiDirectory', f'api-{app_name}')
config.set('ProjectConfiguration', 'awsProfile', aws_profile)
config.set('ProjectConfiguration', 'awsRegion', aws_region)
with open('config.ini', 'w') as config_file:
config.write(config_file)
@task
def createHostBucket(ctx):
config = getConfig()
bucket_name = config.get('bucketName')
aws_region = config.get('awsRegion')
print('Creating host bucket ' + bucket_name)
s3 = boto3.client('s3')
s3.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={
'LocationConstraint': aws_region
}
)
website_configuration = {
'IndexDocument': {'Suffix': 'index.html'}
}
s3.put_bucket_website(
Bucket=bucket_name,
WebsiteConfiguration=website_configuration
)
s3.get_bucket_website(
Bucket=bucket_name
)
@task
def deployUi(ctx):
config = getConfig()
bucket_name = config.get('bucketName')
ui_directory = config.get('uiDirectory')
try:
run(f'aws s3 sync {ui_directory}/build s3://' + bucket_name + '/ --acl public-read')
except Exception as e:
print('Check that you have run createHostBucket and that the build directory exists inside the ui directory')
@task
def buildUi(ctx):
ui_directory = getConfig().get('uiDirectory')
with ctx.cd(ui_directory):
ctx.run('npm run build')
@task
def cleanUi(ctx):
ui_directory = getConfig().get('uiDirectory')
with ctx.cd(ui_directory):
print('Cleaning ui\n')
ctx.run('rm -rf build')
@task
def deployApi(ctx):
config = getConfig()
api_directory = config.get('apiDirectory')
with ctx.cd(api_directory):
result = ctx.run('chalice deploy')
for line in result.stdout.splitlines():
if line.startswith(' - Rest API URL: '):
api_url = line.split(' - Rest API URL: ')[1].strip()
if api_url:
writeConfig('apiUrl', api_url)
@task
def ui(ctx):
static_url = getConfig().get('uiUrl')
print(f'Public URL: {static_url}')
def getConfig():
config = configparser.ConfigParser()
config.read('config.ini')
return config['ProjectConfiguration']
def writeConfig(key, value):
config = configparser.ConfigParser()
config.read('config.ini')
config.set('ProjectConfiguration', key, value)
with open('config.ini', 'w') as config_file:
config.write(config_file)