forked from ansible-provisioning/ansible-workshop-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workshop-config
executable file
·105 lines (89 loc) · 3.71 KB
/
workshop-config
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
#!/usr/bin/env python
### 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 2
### 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, write to the Free Software
### Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
### Copyright 2014 Dag Wieers <dag@wieers.com>
### This tool is run on boot and provides a configuration window to
### display the system's IP address and set the system's name.
### This is useful when you have a basic image using DHCP which can
### be cloned at will as many times to have a group of systems to
### demo or learn a configuration system.
import ethtool, fcntl, os, sys, termios, tty
from snack import Entry, GridForm, Label, Listbox, SnackScreen
names = ( 'vm-master', 'vm-web', 'vm-db', 'vm-syslog', 'vm-kdump' )
system = os.uname()[1].split('.')[0]
domain = 'example.net'
def ipaddress():
'''Return the first acceptable IP address on the hostonly network'''
for dev in ethtool.get_active_devices():
if (ethtool.get_flags(dev) & (ethtool.IFF_LOOPBACK | ethtool.IFF_POINTOPOINT)):
continue
for addr in ethtool.get_interfaces_info(dev)[0].get_ipv4_addresses():
return addr.address
return '<unknown>'
def set_tty(nr):
'''Move application to a different console'''
fd = open('/dev/tty%d' % nr, 'a')
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[0] = new[0] & ~termios.ICRNL
new[3] = new[3] & ~(termios.ICANON | termios.ECHO)
new[3] = new[3] | termios.ISIG
termios.tcsetattr(fd, termios.TCSAFLUSH, new)
tty.setraw(fd)
os.closerange(0, 3)
os.dup2(fd.fileno(), sys.stdin.fileno())
os.dup2(fd.fileno(), sys.stdout.fileno())
os.dup2(fd.fileno(), sys.stderr.fileno())
def chvt(nr):
'''Change to a different console'''
fd = open('/proc/self/fd/0')
fcntl.ioctl(fd, 0x00005606, nr)
fcntl.ioctl(fd, 0x00005607, nr)
try:
tty_nr = int(sys.argv[1])
except IndexError:
tty_nr = None
except ValueError:
print >>sys.stderr, "ERROR: Argument %s is not a number" % sys.argv[1]
sys.exit(1)
if tty_nr != None:
set_tty(tty_nr)
screen = SnackScreen()
screen.drawRootText(17, 1, 'Welcome to the Ansible Workshop configurator !')
screen.pushHelpLine('Change or select hostname')
hostname = Entry(15, system)
listbox = Listbox(height = len(names), width = 18, returnExit = 1)
for nr, name in enumerate(names):
listbox.append(name, nr)
if name == system:
listbox.setCurrent(names.index(system))
grid = GridForm(screen, "Virtual Machine", 1, 5)
grid.add(Label('Hostname:'), 0, 2, anchorLeft=1)
grid.add(hostname, 0, 3, anchorRight=1)
grid.add(Label('IP Address:'), 0, 0, anchorLeft=1)
grid.add(Label(ipaddress()), 0, 1, anchorRight=1)
grid.add(listbox, 0, 4, padding=(0, 1, 0, 0))
if tty_nr != None:
chvt(tty_nr)
while True:
hostname.set(system)
result = grid.run()
system = names[listbox.current()]
try:
open('/proc/sys/kernel/hostname', 'w').write(system)
open('/etc/sysconfig/network', 'w').write('NETWORKING=yes\nHOSTNAME=%s.%s\n' % (system, domain))
open('/etc/hostname', 'w').write('%s.%s\n' % (system, domain))
screen.pushHelpLine('Configured!')
except:
screen.pushHelpLine('Error changing system name')