forked from romilgupta/openstack-icehouse-scripts
-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.py
127 lines (101 loc) · 3.62 KB
/
common.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
#!/usr/bin/python
import sys
import os
# import time
import fcntl
import struct
import socket
import subprocess
# These modules will be loaded later after downloading
iniparse = None
psutil = None
def kill_process(process_name):
for proc in psutil.process_iter():
if proc.name == process_name:
proc.kill()
def get_ip_address(ifname):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
except Exception:
print "Cannot get IP Address for Interface %s" % ifname
sys.exit(1)
def delete_file(file_path):
if os.path.isfile(file_path):
os.remove(file_path)
else:
print("Error: %s file not found" % file_path)
def write_to_file(file_path, content):
open(file_path, "a").write(content)
def add_to_conf(conf_file, section, param, val):
config = iniparse.ConfigParser()
config.readfp(open(conf_file))
if not config.has_section(section):
config.add_section(section)
val += '\n'
config.set(section, param, val)
with open(conf_file, 'w') as f:
config.write(f)
def delete_from_conf(conf_file, section, param):
config = iniparse.ConfigParser()
config.readfp(open(conf_file))
if param is None:
config.remove_section(section)
else:
config.remove_option(section, param)
with open(conf_file, 'w') as f:
config.write(f)
def get_from_conf(conf_file, section, param):
config = iniparse.ConfigParser()
config.readfp(open(conf_file))
if param is None:
raise Exception("parameter missing")
else:
return config.get(section, param)
def print_format(string):
print "+%s+" % ("-" * len(string))
print "|%s|" % string
print "+%s+" % ("-" * len(string))
def execute(command, display=False):
print_format("Executing : %s " % command)
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
if display:
while True:
nextline = process.stdout.readline()
if nextline == '' and process.poll() != None:
break
sys.stdout.write(nextline)
sys.stdout.flush()
output, stderr = process.communicate()
exitCode = process.returncode
else:
output, stderr = process.communicate()
exitCode = process.returncode
if (exitCode == 0):
return output.strip()
else:
print "Error", stderr
print "Failed to execute command %s" % command
print exitCode, output
raise Exception(output)
def initialize_system():
if not os.geteuid() == 0:
sys.exit('Please re-run the script with root user')
execute("apt-get clean", True)
execute("apt-get autoclean -y", True)
execute("apt-get update -y", True)
execute("apt-get install ubuntu-cloud-keyring python-setuptools python-iniparse python-psutil -y", True)
delete_file("/etc/apt/sources.list.d/icehouse.list")
execute("echo deb http://ubuntu-cloud.archive.canonical.com/ubuntu trusty-updates/juno main >> /etc/apt/sources.list.d/juno.list")
execute("apt-get update -y", True)
execute("apt-get install vlan bridge-utils ethtool -y", True)
execute("sed -i 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/' /etc/sysctl.conf")
global iniparse
if iniparse is None:
iniparse = __import__('iniparse')
global psutil
if psutil is None:
psutil = __import__('psutil')