-
Notifications
You must be signed in to change notification settings - Fork 13
/
entrypoint.py
196 lines (145 loc) · 6.24 KB
/
entrypoint.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import os
import sys
import logging
import gnupg
import git
import shutil
import re
import json
from debian.debfile import DebFile
from key import detectPublicKey, importPrivateKey
debug = os.environ.get('INPUT_DEBUG', False)
if debug:
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
else:
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
if __name__ == '__main__':
logging.info('-- Parsing input --')
github_token = os.environ.get('INPUT_GITHUB_TOKEN')
supported_arch = os.environ.get('INPUT_REPO_SUPPORTED_ARCH')
supported_version = os.environ.get('INPUT_REPO_SUPPORTED_VERSION')
deb_file_path = os.environ.get('INPUT_FILE')
deb_file_target_version = os.environ.get('INPUT_FILE_TARGET_VERSION')
github_repo = os.environ.get('GITHUB_REPOSITORY')
gh_branch = os.environ.get('INPUT_PAGE_BRANCH', 'gh-pages')
apt_folder = os.environ.get('INPUT_REPO_FOLDER', 'repo')
if None in (github_token, supported_arch, supported_version, deb_file_path):
logging.error('Required key is missing')
sys.exit(1)
supported_arch_list = supported_arch.strip().split('\n')
supported_version_list = supported_version.strip().split('\n')
deb_file_path = deb_file_path.strip()
deb_file_version = deb_file_target_version.strip()
logging.debug(supported_arch_list)
logging.debug(supported_version_list)
logging.debug(deb_file_path)
logging.debug(deb_file_version)
if deb_file_version not in supported_version_list:
logging.error('File version target is not listed in repo supported version list')
sys.exit(1)
key_public = os.environ.get('INPUT_PUBLIC_KEY')
key_private = os.environ.get('INPUT_PRIVATE_KEY')
key_passphrase = os.environ.get('INPUT_KEY_PASSPHRASE')
logging.debug(github_token)
logging.debug(supported_arch_list)
logging.debug(supported_version_list)
logging.info('-- Done parsing input --')
# Clone repo
logging.info('-- Cloning current Github page --')
github_user = github_repo.split('/')[0]
github_slug = github_repo.split('/')[1]
if os.path.exists(github_slug):
shutil.rmtree(github_slug)
git_repo = git.Repo.clone_from(
'https://{}@github.com/{}.git'.format(github_token, github_repo),
github_slug,
)
git_refs = git_repo.remotes.origin.refs
git_refs_name = list(map(lambda x: str(x).split('/')[-1], git_refs))
logging.debug(git_refs_name)
if gh_branch not in git_refs_name:
git_repo.git.checkout(b=gh_branch)
else:
git_repo.git.checkout(gh_branch)
# Generate metadata
logging.debug("cwd : {}".format(os.getcwd()))
logging.debug(os.listdir())
deb_file_handle = DebFile(filename=deb_file_path)
deb_file_control = deb_file_handle.debcontrol()
current_metadata = {
'format_version': 1,
'sw_version': deb_file_control['Version'],
'sw_architecture': deb_file_control['Architecture'],
'linux_version': deb_file_version
}
current_metadata_str = json.dumps(current_metadata)
logging.debug('Metadata {}'.format(current_metadata_str))
# Get metadata
all_commit = git_repo.iter_commits(gh_branch)
all_apt_action_commit = list(filter(lambda x: (x.message[:12] == '[apt-action]'), all_commit))
apt_action_metadata_str = list(
map(
lambda x: re.findall('apt-action-metadata({.+})$', x.message),
all_apt_action_commit,
)
)
apt_action_valid_metadata_str = list(filter(lambda x: len(x) > 0, apt_action_metadata_str))
apt_action_metadata = list(map(lambda x: json.loads(x[0]), apt_action_valid_metadata_str))
logging.debug(all_apt_action_commit)
logging.debug(apt_action_valid_metadata_str)
for check_metadata in apt_action_metadata:
if (check_metadata == current_metadata):
logging.info('Loop detected, exiting')
sys.exit(0)
logging.info('-- Done cloning current Github page --')
# Prepare key
logging.info('-- Importing key --')
key_dir = os.path.join(github_slug, 'public.key')
gpg = gnupg.GPG()
detectPublicKey(gpg, key_dir, key_public)
private_key_id = importPrivateKey(gpg, key_private)
logging.info('-- Done importing key --')
# Prepare repo
logging.info('-- Preparing repo directory --')
apt_dir = os.path.join(github_slug, apt_folder)
apt_conf_dir = os.path.join(apt_dir, 'conf')
if not os.path.isdir(apt_dir):
logging.info('Existing repo not detected, creating new repo')
os.mkdir(apt_dir)
os.mkdir(apt_conf_dir)
logging.debug('Creating repo config')
with open(os.path.join(apt_conf_dir, 'distributions'), 'w') as distributions_file:
for codename in supported_version_list:
distributions_file.write('Description: {}\n'.format(github_repo))
distributions_file.write('Codename: {}\n'.format(codename))
distributions_file.write('Architectures: {}\n'.format(' '.join(supported_arch_list)))
distributions_file.write('Components: main\n')
distributions_file.write('SignWith: {}\n'.format(private_key_id))
distributions_file.write('\n\n')
logging.info('-- Done preparing repo directory --')
# Fill repo
logging.info('-- Adding package to repo --')
logging.info('Adding {}'.format(deb_file_path))
os.system(
'reprepro -b {} --export=silent-never includedeb {} {}'.format(
apt_dir,
deb_file_version,
deb_file_path,
)
)
logging.debug('Signing to unlock key on gpg agent')
gpg.sign('test', keyid=private_key_id, passphrase=key_passphrase)
logging.debug('Export and sign repo')
os.system('reprepro -b {} export'.format(apt_dir))
logging.info('-- Done adding package to repo --')
# Commiting and push changes
logging.info('-- Saving changes --')
git_repo.config_writer().set_value(
'user', 'email', '{}@users.noreply.github.com'.format(github_user)
)
git_repo.git.add('*')
git_repo.index.commit(
'[apt-action] Update apt repo\n\n\napt-action-metadata{}'.format(current_metadata_str)
)
git_repo.git.push('--set-upstream', 'origin', gh_branch)
logging.info('-- Done saving changes --')