forked from nicolargo/ubuntupostinstall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ubuntu-12.04-postinstall.py
executable file
·369 lines (317 loc) · 12.2 KB
/
ubuntu-12.04-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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Mon script de post installation Ubuntu 12.04 LTS
#
# Syntax: # sudo ./ubuntupostinstall-12.04.sh
#
# Nicolargo (aka) Nicolas Hennion
# http://www.nicolargo.com
# Distributed under the GPL version 3 license
#
"""
Post installation script for Ubuntu 12.04 LTS
"""
import os, sys, platform, getopt, shutil, logging, getpass, ConfigParser
# Global variables
#-----------------------------------------------------------------------------
_VERSION="0.6b"
_DEBUG = 1
_LOG_FILE = "/tmp/ubuntu-12.04-postinstall.log"
_CONF_FILE = "https://raw.github.com/nicolargo/ubuntupostinstall/master/ubuntu-12.04-unity-postinstall.cfg"
# System commands
#-----------------------------------------------------------------------------
_APT_ADD = "add-apt-repository -y"
_APT_INSTALL = "DEBIAN_FRONTEND=noninteractive apt-get -y -f install"
_APT_REMOVE = "DEBIAN_FRONTEND=noninteractive apt-get -y -f remove"
_APT_UPDATE = "DEBIAN_FRONTEND=noninteractive apt-get -y update"
_APT_UPGRADE = "DEBIAN_FRONTEND=noninteractive apt-get -y upgrade"
_APT_KEY = "apt-key adv --keyserver keyserver.ubuntu.com --recv-keys"
_WGET = "wget"
# 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 init():
"""
Init the script
"""
# Globals variables
global _VERSION
global _DEBUG
# Set the log configuration
logging.basicConfig(
filename=_LOG_FILE,
level=logging.DEBUG,
format='%(asctime)s %(levelname)s - %(message)s',
datefmt='%d/%m/%Y %H:%M:%S',
)
def syntax():
"""
Print the script syntax
"""
print "Ubuntu 12.04 post installation script version %s" % _VERSION
print ""
print "Syntax: ubuntu-12.04-postinstall.py [-c cfgfile] [-h] [-v]"
print " -c cfgfile: Use the cfgfile instead of the default one"
print " -h : Print the syntax and exit"
print " -v : Print the version and exit"
print ""
print "Exemples:"
print ""
print " # ubuntu-12.04-postinstall.py"
print " > Run the script with the default configuration file"
print " %s" % _CONF_FILE
print ""
print " # ubuntu-12.04-postinstall.py -c ./myconf.cfg"
print " > Run the script with the ./myconf.cfg file"
print ""
print " # ubuntu-12.04-postinstall.py -c http://mysite.com/myconf.cfg"
print " > Run the script with the http://mysite.com/myconf.cfg configuration file"
print ""
def version():
"""
Print the script version
"""
sys.stdout.write ("Script version %s" % _VERSION)
sys.stdout.write (" (running on %s %s)\n" % (platform.system() , platform.machine()))
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 = ""):
"""
Exec 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 (optionnal)
if (presskey == 1):
try:
input = raw_input
except:
pass
raw_input()
# Run the command
returncode = os.system ("/bin/sh -c \"%s\" >> /dev/null 2>&1" % 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" % returncode)
# Stop the program if returncode and exitonerror != 0
if ((returncode != 0) & (exitonerror != 0)):
if _DEBUG:
logging.debug ("Forced to quit")
exit(exitonerror)
def getpassword(description = ""):
"""
Read password (with confirmation)
"""
if (description != ""):
sys.stdout.write ("%s\n" % description)
password1 = getpass.getpass("Password: ");
password2 = getpass.getpass("Password (confirm): ");
if (password1 == password2):
return password1
else:
sys.stdout.write (colors.ORANGE + "[Warning] Password did not match, please try again" + colors.NO + "\n")
return getpassword()
def getstring(message = "Enter a value: "):
"""
Ask user to enter a value
"""
try:
input = raw_input
except:
pass
return raw_input(message)
def waitenterpressed(message = "Press ENTER to continue..."):
"""
Wait until ENTER is pressed
"""
try:
input = raw_input
except:
pass
raw_input(message)
return 0
def main(argv):
"""
Main function
"""
try:
opts, args = getopt.getopt(argv, "c:hv", ["config", "help", "version"])
except getopt.GetoptError:
syntax()
exit(2)
config_file = ""
config_url = _CONF_FILE
for opt, arg in opts:
if opt in ("-c", "--config"):
if arg.startswith("http://") or \
arg.startswith("https://") or \
arg.startswith("ftp://"):
config_url = arg
else:
config_file = arg
elif opt in ("-h", "--help"):
syntax()
exit()
elif opt in ('-v', "--version"):
version()
exit()
# Are your root ?
if (not isroot()):
showexec ("Script should be run as root", "tpastroot", exitonerror = 1)
# Is it Precise Pangolin ?
_UBUNTU_VERSION = platform.linux_distribution()[2]
if (_UBUNTU_VERSION != "precise"):
showexec ("Script only for Ubuntu 12.04", "tpassousprecise", exitonerror = 1)
# Read the configuration file
if (config_file == ""):
config_file = "/tmp/ubuntu-12.04-postinstall.cfg"
showexec ("Download the configuration file", "rm -f "+config_file+" ; "+_WGET+" -O "+config_file+" "+config_url)
config = ConfigParser.RawConfigParser()
config.read(config_file)
if (config.has_section("gnome3") and config.has_section("unity")):
showexec ("Can not use both Gnome 3 and Unity, please change your .cfg file", "gnome3etunitygrosboulet", exitonerror = 1)
# Parse and exec pre-actions
for action_name, action_cmd in config.items("preactions"):
showexec ("Execute preaction "+action_name.lstrip("action_"), action_cmd)
# Parse and install repositories
pkg_list_others = {}
for item_type, item_value in config.items("repos"):
if (item_type.startswith("ppa_")):
showexec ("Install repository "+item_type.lstrip("ppa_"), _APT_ADD+" "+item_value)
elif (item_type.startswith("url_")):
showexec ("Install repository "+item_type.lstrip("url_"), _APT_ADD+" \\\"deb "+item_value+"\\\"")
elif (item_type.startswith("key_")):
showexec ("Install key for the "+item_type.lstrip("key_")+" repository", _APT_KEY+" "+item_value)
elif (item_type.startswith("pkg_")):
pkg_list_others[item_type] = item_value
# Update repos
showexec ("Update repositories", _APT_UPDATE)
# Upgrade system
showexec ("System upgrade (~20 mins, please be patient...)", _APT_UPGRADE)
# Parse and install packages
for pkg_type, pkg_list in config.items("packages"):
if (pkg_type.startswith("remove_")):
showexec ("Remove packages "+pkg_type.lstrip("remove_"), _APT_REMOVE+" "+pkg_list)
else:
showexec ("Install packages "+pkg_type, _APT_INSTALL+" "+pkg_list)
# Install packages related to repositories
#~ print pkg_list_others
for pkg in pkg_list_others.keys():
showexec ("Install packages "+pkg, _APT_INSTALL+" "+pkg_list_others[pkg])
# Allow user to read DVD (CSS)
showexec ("DVDs CSS encryption reader", "sh /usr/share/doc/libdvdread4/install-css.sh")
# Download and install dotfiles: vimrc, prompt...
if (config.has_section("dotfiles")):
# Create the bashrc.d subfolder
showexec ("Create the ~/.bashrc.d subfolder", "mkdir -p $HOME/.bashrc.d")
if (config.has_option("dotfiles", "bashrc")):
showexec ("Install bash main configuration file", _WGET+" -O $HOME/.bashrc "+config.get("dotfiles", "bashrc"))
if (config.has_option("dotfiles", "bashrc_prompt")):
showexec ("Install bash prompt configuration file", _WGET+" -O $HOME/.bashrc.d/bashrc_prompt "+config.get("dotfiles", "bashrc_prompt"))
if (config.has_option("dotfiles", "bashrc_aliases")):
showexec ("Install bash aliases configuration file", _WGET+" -O $HOME/.bashrc.d/bashrc_aliases "+config.get("dotfiles", "bashrc_aliases"))
# Vim
if (config.has_option("dotfiles", "vimrc")):
showexec ("Install the Vim configuration file", _WGET+" -O $HOME/.vimrc "+config.get("dotfiles", "vimrc"))
# Htop
if (config.has_option("dotfiles", "htoprc")):
showexec ("Install the Htop configuration file", _WGET+" -O $HOME/.htoprc "+config.get("dotfiles", "htoprc"))
# Gnome 3 configuration
if (config.has_section("gnome3")):
# Set the default theme
if (config.has_option("gnome3", "theme")):
showexec ("Set the default Gnome Shell theme to "+config.get("gnome3", "theme"), "gsettings set org.gnome.desktop.interface gtk-theme "+config.get("gnome3", "theme"))
# Set the default icons
if (config.has_option("gnome3", "icons")):
showexec ("Set the default Gnome Shell icons to "+config.get("gnome3", "icons"), "gsettings set org.gnome.desktop.interface icon-theme "+config.get("gnome3", "icons"))
# Set the default cursors
if (config.has_option("gnome3", "cursors")):
showexec ("Set the default Gnome Shell cursors to "+config.get("gnome3", "cursors"), "gsettings set org.gnome.desktop.interface cursor-theme "+config.get("gnome3", "cursors"))
# Download and install the default Conky configuration
if (config.has_option("gnome3", "conky")):
showexec ("Install the Conky configuration file", _WGET+" -O $HOME/.conkyrc "+config.get("gnome3", "conky"))
# Get the minimize/maximize button and ALT-F2 shortcut back
showexec ("Get the minimize and maximize button back in Gnome Shell", "gconftool-2 -s -t string /desktop/gnome/shell/windows/button_layout \":minimize,maximize,close\"")
showexec ("Get ALT-F2 back to me", "gconftool-2 --recursive-unset /apps/metacity/global_keybindings")
# Gnome Shell is the default UI
showexec ("Gnome Shell is now the default shell", "/usr/lib/lightdm/lightdm-set-defaults -s gnome-shell")
# Unity configuration
if (config.has_section("unity")):
# Set the default theme
if (config.has_option("unity", "theme")):
showexec ("Set the default Unity theme to "+config.get("unity", "theme"), "gsettings set org.gnome.desktop.interface gtk-theme "+config.get("unity", "theme"))
# Set the default icons
if (config.has_option("unity", "icons")):
showexec ("Set the default Unity icons to "+config.get("unity", "icons"), "gsettings set org.gnome.desktop.interface icon-theme "+config.get("unity", "icons"))
# Set the default cursors
if (config.has_option("unity", "cursors")):
showexec ("Set the default Unity cursors to "+config.get("unity", "cursors"), "gsettings set org.gnome.desktop.interface cursor-theme "+config.get("unity", "cursors"))
# Download and install the default Conky configuration
if (config.has_option("unity", "conky")):
showexec ("Install the Conky configuration file", _WGET+" -O $HOME/.conkyrc "+config.get("unity", "conky"))
# Unity is the default UI
showexec ("Unity is now the default shell", "/usr/lib/lightdm/lightdm-set-defaults -s unity-3d")
# Parse and exec post-actions
for action_name, action_cmd in config.items("postactions"):
showexec ("Execute postaction "+action_name.lstrip("action_"), action_cmd)
# End of the script
print("---")
print("End of the script.")
print(" - Cfg file: "+config_file)
print(" - Log file: "+_LOG_FILE)
print("")
print("Please restart your session to complete.")
print("---")
# Main program
#-----------------------------------------------------------------------------
if __name__ == "__main__":
init()
main(sys.argv[1:])
exit()