forked from truenas/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion-release.py
More file actions
executable file
·151 lines (117 loc) · 3.6 KB
/
version-release.py
File metadata and controls
executable file
·151 lines (117 loc) · 3.6 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import subprocess
import git
import sys
# WEB_SERVER_HOST = 'docs.ixsystems.com'
WEB_SERVER_DIR = '/var/www/html/docs1/archive'
WEB_SERVER_USER = 'docs'
REPO_CLONE_URL = 'https://github.com/freenas/documentation.git'
USER_HOME_DIR = '/home/docs'
LOCAL_DIR_FOR_REPO = f'{USER_HOME_DIR}/documentation-repo'
DEFAULT_REPO_BRANCH = 'master'
def verify_argv():
"""
Make sure sys.argv[1] is provided
"""
branch = None
try:
if sys.argv[1]:
branch = sys.argv[1]
except IndexError:
# no arg specified
pass
return branch
def check_existing_repo():
"""
This checks to see if repo has already been downloaded.
"""
repo = None
try:
print(f'Cloning {REPO_CLONE_URL} into {LOCAL_DIR_FOR_REPO} ...')
repo = git.Repo.clone_from(REPO_CLONE_URL, LOCAL_DIR_FOR_REPO)
except git.exc.GitCommandError:
print(f'{LOCAL_DIR_FOR_REPO} already exists. Skipping clone.')
repo = git.Repo(LOCAL_DIR_FOR_REPO)
return repo
def check_out_release_branch(repo, branch):
"""
pull master branch
checkout branch if it exists
pull origin master
"""
try:
print('Checking out branch...')
repo.git.checkout(DEFAULT_REPO_BRANCH)
repo.git.pull()
repo.git.checkout(branch)
repo.git.pull('origin', 'master')
except git.exc.GitCommandError:
print(f'"{branch}" does not exist.')
print(
'Please create the release branch at',
'https://github.com/freenas/documentation first.'
)
sys.exit(1)
def hugo_build(branch):
"""
Build site with Hugo
"""
print('Building docs with hugo...')
# Build docs with Hugo.
hugo_cmd = ['hugo', '-d', f'/tmp/{branch}']
hugo_proc = subprocess.run(
hugo_cmd,
cwd=LOCAL_DIR_FOR_REPO,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
# Check for hugo errors.
if hugo_proc.stdout:
msg = hugo_proc.stdout.decode()
if 'POSTCSS: failed to transform' in msg:
print(
f'Build failed with error: {msg}',
'The correct npm packages may not be installed.\n',
f'cd into {LOCAL_DIR_FOR_REPO},',
'ensure npm is installed, and run:\n',
'\tsudo npm install -D --save autoprefixer\n',
'\tsudo npm install -D --save postcss-cli'
)
sys.exit(1)
elif 'pages' or 'paginator pages' in msg:
print(msg)
print(
'Success.'
f' The built files can be found in /tmp/{branch}'
)
else:
print(msg)
sys.exit(1)
def copy_built_files(branch):
"""
This function copies the built files to the
NGINX configured directory.
"""
print(f'Copying /tmp/{branch} to {WEB_SERVER_DIR}.')
# Copy files from hugo output to the hosting dir on server.
cp_cmd = ['cp', '-r', '-u', f'/tmp/{branch}', f'{WEB_SERVER_DIR}/']
cp_proc = subprocess.run(
cp_cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
if cp_proc.stdout:
print(cp_proc.stdout.decode())
elif cp_proc.stderr:
print(cp_proc.stderr.decode())
if __name__ == '__main__':
branch = verify_argv()
if branch is None:
print('FATAL: Branch not provided!')
sys.exit(1)
repo = check_existing_repo()
if repo is None:
print('FATAL: Repository not found!')
sys.exit(1)
check_out_release_branch(repo, branch)
hugo_build(branch)
copy_built_files(branch)