-
Notifications
You must be signed in to change notification settings - Fork 50
/
ubuntu-12.10-postinstall.py
executable file
·403 lines (339 loc) · 14.8 KB
/
ubuntu-12.10-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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Mon script de post installation Ubuntu 12.10
#
# Syntax: # sudo ./ubuntupostinstall-12.10.sh
#
# Nicolargo (aka) Nicolas Hennion
# http://www.nicolargo.com
# Distributed under the GPL version 3 license
#
__appname__ = 'ubuntu-12.10-postinstall'
__version__ = "0.7.1"
__author__ = "Nicolas Hennion <nicolas@nicolargo.com>"
__licence__ = "LGPL"
"""
Post installation script for Ubuntu 12.10
"""
import os
import sys
import platform
import getopt
import shutil
import logging
import getpass
import ConfigParser
import gettext
gettext.install(__appname__)
# Global variables
#-----------------------------------------------------------------------------
_FOR_UBUNTU = "quantal"
_DEBUG = 1
_LOG_FILE = "/tmp/%s.log" % __appname__
_CONF_FILE = "https://raw.github.com/nicolargo/ubuntupostinstall/master/ubuntu-12.10-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.10 post installation script version %s for %s")
% (__version__, _FOR_UBUNTU))
print("")
print(_("Syntax: %s.py [-c cfgfile] [-h] [-v]") % __appname__)
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(_(" # %s.py") % __appname__)
print(_(" > Run the script with the default configuration file"))
print(_(" %s") % _CONF_FILE)
print("")
print(_(" # %s.py -c ./myconf.cfg") % __appname__)
print(_(" > Run the script with the ./myconf.cfg file"))
print("")
print(_(" # %s.py -c http://mysite.com/myconf.cfg") % __appname__)
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 != _FOR_UBUNTU):
showexec (_("Script only for Ubuntu %s") % _FOR_UBUNTU, "tpassousprecise", exitonerror = 1)
# Read the configuration file
if (config_file == ""):
config_file = "/tmp/%s.cfg" % __appname__
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 (_("Download bash main configuration file"), _WGET+" -O $HOME/.bashrc "+config.get("dotfiles", "bashrc"))
if (config.has_option("dotfiles", "bashrc_prompt")):
showexec (_("Download bash prompt configuration file"), _WGET+" -O $HOME/.bashrc.d/bashrc_prompt "+config.get("dotfiles", "bashrc_prompt"))
if (config.has_option("dotfiles", "bashrc_aliases")):
showexec (_("Download bash aliases configuration file"), _WGET+" -O $HOME/.bashrc.d/bashrc_aliases "+config.get("dotfiles", "bashrc_aliases"))
showexec (_("Install the bash configuration file"), "chown -R $USERNAME:$USERNAME $HOME/.bashrc*")
# Vim
if (config.has_option("dotfiles", "vimrc")):
showexec (_("Donwload the Vim configuration file"), _WGET+" -O $HOME/.vimrc "+config.get("dotfiles", "vimrc"))
showexec (_("Install the Vim configuration file"), "chown -R $USERNAME:$USERNAME $HOME/.vimrc")
# Htop
if (config.has_option("dotfiles", "htoprc")):
showexec (_("Download the Htop configuration file"), _WGET+" -O $HOME/.htoprc "+config.get("dotfiles", "htoprc"))
showexec (_("Install the Htop configuration file"), "chown -R $USERNAME:$USERNAME $HOME/.htoprc")
# Pythonrc
if (config.has_option("dotfiles", "pythonrc")):
showexec (_("Download the Pythonrc configuration file"), _WGET+" -O $HOME/.pythonrc "+config.get("dotfiles", "pythonrc"))
showexec (_("Install the Pythonrc configuration file"), "chown -R $USERNAME:$USERNAME $HOME/.pythonrc")
# 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"), "sudo -u $USERNAME 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"), "sudo -u $USERNAME 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"), "sudo -u $USERNAME 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 (_("Download the Conky configuration file"), _WGET+" -O $HOME/.conkyrc "+config.get("gnome3", "conky"))
showexec (_("Install the Conky configuration file"), "chown -R $USERNAME:$USERNAME $HOME/.conkyrc")
# Get the minimize/maximize button and ALT-F2 shortcut back
showexec (_("Get the minimize and maximize button back in Gnome Shell"), "sudo -u $USERNAME gconftool-2 -s -t string /desktop/gnome/shell/windows/button_layout \":minimize,maximize,close\"")
showexec (_("Get ALT-F2 back to me"), "sudo -u $USERNAME 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()