-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_deploy.py
133 lines (110 loc) · 4.18 KB
/
app_deploy.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
124
125
126
127
128
129
130
131
132
133
'''
Deploy Laravel/Other Apps to Apache
By: Ron Bailey
'''
import json
import os
import sys
import deploylib
import datetime
import optparse
import yaml
with open('config.yaml') as file:
config = yaml.safe_load(file)
# settings
TEST = True
repo = config['repo']['source']
project_name = config['repo']['name']
keep_releases = config['keep-releases']
shared_dirs = config['shared-directories']
shared_files = config['shared-files']
link_storage_public = config['link_storage_public']
writable_dirs = config['writable_dirs']
writable_use_sudo = config['writable_use_sudo']
deploy_path = config['deploy_directory_path']
cwd = os.getcwd()
# deploylib.cleanup_releases(cwd, keep_releases)
parser = optparse.OptionParser("App Deploy")
parser.add_option('-b', '--rollback', action="store", dest="rollback", help="rollback release", default=False)
parser.add_option('-v', '--releases', action="store_true", dest="releases", help="show releases", default=False)
parser.add_option('-d', '--deploy', action="store_true", dest="deploy", help="deploy release", default=False)
# parser.add_option('-h', '--help', action="store_true", dest="help", help="help", default=False)
options, args = parser.parse_args()
if options.rollback is not False:
# rollback
deploylib.rollback_release(cwd, options.rollback)
if options.releases is not False:
deploylib.get_all_versions(cwd)
# if options.deploy is not False:
if options.deploy is not True:
print("Not deployed.")
print("Must add flag `--deploy` or `-d` flag to deploy.")
sys.exit()
dep_dir = os.path.isdir(cwd + '/.dep')
releases_dir = os.path.isdir(cwd + '/releases')
shared_dir = os.path.isdir(cwd + '/shared')
if dep_dir and releases_dir and shared_dir:
print("[+] Directories Set Up OK")
else:
sys.exit("[x] Directories missing, run `inst_php_deploy.sh` to set them up.")
if not TEST:
if os.path.isfile(cwd + '/.dep/deploy.lock'):
sys.exit("[x] Stopped: Can only have one deployment at a time.")
else:
deploylib.create_deploy_lock(cwd + '/.dep/deploy.lock')
print("[+] Lock created.")
# release step
run_first_time = False
if os.path.isfile(cwd + '/.dep/releases.json'):
release_collection = []
release_count = 0
with open(cwd + '/.dep/releases.json') as json_file:
data = json.load(json_file)
for r in data['releases']:
release_count += 1
release_collection.append(r['name'])
release_collection.sort()
latest_release = release_collection[-1]
new_release = latest_release + 1
data['releases'].append({
'name': int(new_release)
})
with open(cwd + '/.dep/releases.json', "w") as outfile:
json.dump(data, outfile)
print("latest_release: " + str(latest_release))
print("new_release: " + str(new_release))
print('[+] New release added out of {} releases'.format(release_count))
if TEST:
print('release_collection:')
print(release_collection)
else:
run_first_time = True
rel_config = {'releases': []}
rel_config['releases'].append({
'name': 0
})
with open(cwd + '/.dep/releases.json', "w") as outfile:
json.dump(rel_config, outfile)
print('New releases file created')
if run_first_time:
deploylib.create_directory(cwd, cwd + '/releases', '0')
deploylib.create_directory(cwd, cwd + '/releases', '1')
print('Release Folder created. [0]')
print('Release Folder created. [1]')
last_release = 0
new_release = 1
latest_release = new_release
print(latest_release)
print(new_release)
else:
last_release = latest_release - 1
latest_release = new_release
deploylib.create_directory(cwd, cwd + '/releases', str(new_release))
# copy to shared. shared_dirs, shared_files
shared_copy_logs = deploylib.copy_to_shared(cwd, str(last_release), shared_dirs, shared_files)
print(shared_copy_logs)
# git download code, run deploy script.
deploylib.run_scripts_deploy(config['runtime'], cwd, str(latest_release), repo, project_name, shared_dirs, shared_files, str(last_release), link_storage_public)
deploylib.cleanup_releases(cwd, keep_releases)
deploylib.run_command("rm -f " + cwd + "/.dep/deploy.lock")
print("Deploy lock file released.")