-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathubuntu-postinstall.py
executable file
·167 lines (132 loc) · 4.34 KB
/
ubuntu-postinstall.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# My script for post installation of Ubuntu
#
# Syntax: # sudo ./ubuntu-postinstall.py
#
# Shiquan Wang <[email protected]>
# http://shiquanwang.github.io
# Distributed under the LGPL version 3 license
__appname__ = "ubuntu-postinstall"
__version__ = "0.1"
__author__ = "Shiquan Wang <[email protected]>"
__license__ = "LGPL v3"
"""
Post installation script for Ubuntu
"""
import argparse
import logging
import os
import sys
# Global variables
_FOR_UBUNTU = "saucy"
_DEBUG = 1
_LOG_FILE = "/tmp/%s.log" % __appname__
_CONF_FILE = "https://raw.github.com/shiquanwang/ubuntu-postinstall/master/ubuntu-13.10-unity-postinstall.cfg"
# Classes
class colors:
RED = '\033[91m'
GREEN = '\033[92m'
BLUE = '\033[94m'
ORANGE = '\033[93m'
NO = '\033[0m'
def disable(self):
self.RED = ''
self.GREEN = ''
self.BLUE = ''
self.ORANGE = ''
self.NO = ''
# Functions
def isroot():
"""
Check if the user is root.
Return TRUE if user is root.
"""
return (os.geteuid() == 0)
def showexec(description, command, exitonerror = 0, presskey = 0, waitmessage = "", user = "root"):
"""
Execute a system command with a pretty status display (Running / OK / Warning / Error)
By default (exitcode=0), the function did not exit if the command failed
"""
if _DEBUG:
logging.debug("%s" % description)
logging.debug("%s" % command)
# wait message
if (waitmessage == ""):
waitmessage = description
# manage very long description
if (len(waitmessage) > 65):
waitmessage = waitmessage[0:65] + "..."
if (len(description) > 65):
description = description[0:65] + "..."
# display the command
if (presskey == 1):
status = _("[ ENTER ]")
else:
status = _("[Running]")
statuscolor = colors.BLUE
sys.stdout.write(colors.NO + "%s" % waitmessage + statuscolor + "%s" % status.rjust(79-len(waitmessage)) + colors.NO)
sys.stdout.flush()
# wait keypressed (optional)
if (presskey == 1):
try:
tmp_input = input()
except:
pass
input()
# run the command
if (user == "root"):
returncode = os.system("/bin/sh -c \"%s\" >> /dev/null 2>&1" % command)
else:
returncode = os.system("/bin/sh -c \"sudo -u {0} -H {1}\" >> /dev/null 2>&1".format(user, command))
# display the result
if ((returncode == 0) or (returncode == 25600)):
status = "[ OK ]"
statuscolor = colors.GREEN
else:
if exitonerror == 0:
status = "[Warning]"
statuscolor = colors.ORANGE
else:
status = "[ Error ]"
statuscolor = colors.RED
sys.stdout.write(colors.NO + "\r%s" % description + statuscolor + "%s\n" % status.rjust(79-len(description)) + colors.NO)
if _DEBUG:
logging.debug(_("Returncode = %d") % retruncode)
# stop the program if returncode and exitonerror != 0
if ((returncode != 0) & (exitonerror != 0)):
if _DEBUG:
logging.debug(_("Forced to quit"))
exit(exitonerror)
def main():
"""
Utility for Ubuntu post installation.
See more using ubuntu-postinstall.py --help
"""
# create parser
parser = argparse.ArgumentParser()
# argument for configuration file
parser.add_argument('-c', '--conf', type=str, required=True,
help='Ubuntu post installation configuration file.')
# argument group for scene/module selection
group = parser.add_mutually_exclusive_group()
group.add_argument('-s', '--scene', type=str,
help='The scene to be installed.')
group.add_argument('-m', '--module', type=str, action='append',
help='The module to be installed.')
# argument for version
parser.add_argument('--version', action='version',
version='{0} Version {1}'.format(
__appname__, __version__))
# parse args
args = parser.parse_args()
# are you root
if (not isroot()):
showexec(_("Script should be run as root"), "tpastroot", exitonerror = 1)
# read the configuration file
config = configparser.ConfigParser()
config.read(config_file)
# Main program
if __name__ == "__main__":
sys.exit(main())