-
Notifications
You must be signed in to change notification settings - Fork 19
/
host_os.py
executable file
·83 lines (74 loc) · 2.79 KB
/
host_os.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
#!/usr/bin/python2
# Copyright (C) IBM Corp. 2016.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import grp
import logging
import os
import sys
from lib import config
from lib import exception
from lib.utils import create_directory
from lib.utils import is_package_installed
from lib.subcommands import build_images
from lib.subcommands import build_packages
from lib.subcommands import build_release_notes
from lib.subcommands import update_metapackage
from lib.subcommands import update_versions
INSUFFICIENT_PRIVILEGE_ERROR = 3
TOO_MUCH_PRIVILEGE_ERROR = 4
MISSING_PACKAGES_ERROR = 5
REQUIRED_PACKAGES_FILE_PATH = "rpm_requirements.txt"
LOG = logging.getLogger(__name__)
SUBCOMMANDS = {
'build-packages': build_packages,
'build-release-notes': build_release_notes,
'update-metapackage': update_metapackage,
'update-versions': update_versions,
'build-images': build_images,
}
MOCK_REQUIRED_SUBCOMANDS = [
'build-package',
'build-images',
]
if __name__ == '__main__':
CONF = config.setup_default_config()
subcommand = CONF.get('subcommand')
# validate if all required packages are installed
with open(REQUIRED_PACKAGES_FILE_PATH) as f:
required_packages = f.read().splitlines()
missing_packages = [p for p in required_packages
if not is_package_installed(p)]
if missing_packages:
print("Following packages should be installed before running this "
"script: %s" % ", ".join(missing_packages))
sys.exit(MISSING_PACKAGES_ERROR)
if os.getuid() is 0:
print("Please, do not run this command as root.")
sys.exit(TOO_MUCH_PRIVILEGE_ERROR)
if subcommand in MOCK_REQUIRED_SUBCOMANDS:
mock_users = grp.getgrnam('mock').gr_mem
user = os.environ['USER']
if user not in mock_users:
print("User must be in mock group, please run "
"'sudo usermod -a -G mock $(whoami)'")
sys.exit(INSUFFICIENT_PRIVILEGE_ERROR)
create_directory(CONF.get('work_dir'))
return_code = 0
try:
SUBCOMMANDS[subcommand].run(CONF)
except exception.BaseException as exc:
LOG.exception("Command %s failed." % subcommand)
return_code = exc.error_code
sys.exit(return_code)