forked from AnythingLinux/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud-setup-console-proxy.in
More file actions
executable file
·220 lines (191 loc) · 6.81 KB
/
cloud-setup-console-proxy.in
File metadata and controls
executable file
·220 lines (191 loc) · 6.81 KB
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
#!/usr/bin/env python
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import sys, os, subprocess, errno, re, getopt
# ---- This snippet of code adds the sources path and the waf configured PYTHONDIR to the Python path ----
# ---- We do this so cloud_utils can be looked up in the following order:
# ---- 1) Sources directory
# ---- 2) waf configured PYTHONDIR
# ---- 3) System Python path
for pythonpath in (
"@PYTHONDIR@",
os.path.join(os.path.dirname(__file__),os.path.pardir,os.path.pardir,"python","lib"),
):
if os.path.isdir(pythonpath): sys.path.insert(0,pythonpath)
# ---- End snippet of code ----
import cloud_utils
from cloud_utils import stderr
E_GENERIC= 1
E_NOKVM = 2
E_NODEFROUTE = 3
E_DHCP = 4
E_NOPERSISTENTNET = 5
E_NETRECONFIGFAILED = 6
E_VIRTRECONFIGFAILED = 7
E_FWRECONFIGFAILED = 8
E_CPRECONFIGFAILED = 9
E_CPFAILEDTOSTART = 10
E_NOFQDN = 11
def bail(errno=E_GENERIC,message=None,*args):
if message: stderr(message,*args)
stderr("Cloud Console Proxy setup aborted")
sys.exit(errno)
#---------------- boilerplate for python 2.4 support
# CENTOS does not have this -- we have to put this here
try:
from subprocess import check_call
from subprocess import CalledProcessError
except ImportError:
def check_call(*popenargs, **kwargs):
import subprocess
retcode = subprocess.call(*popenargs, **kwargs)
cmd = kwargs.get("args")
if cmd is None: cmd = popenargs[0]
if retcode: raise CalledProcessError(retcode, cmd)
return retcode
class CalledProcessError(Exception):
def __init__(self, returncode, cmd):
self.returncode = returncode ; self.cmd = cmd
def __str__(self): return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)
# ------------ end boilerplate -------------------------
def check_hostname(): return check_call(["hostname",'--fqdn'])
class Command:
def __init__(self,name,parent=None):
self.__name = name
self.__parent = parent
def __getattr__(self,name):
if name == "_print": name = "print"
return Command(name,self)
def __call__(self,*args):
cmd = self.__get_recursive_name() + list(args)
#print " ",cmd
popen = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
m = popen.communicate()
ret = popen.wait()
if ret:
e = CalledProcessError(ret,cmd)
e.stdout,e.stderr = m
raise e
class CommandOutput:
def __init__(self,stdout,stderr):
self.stdout = stdout
self.stderr = stderr
return CommandOutput(*m)
def __lt__(self,other):
cmd = self.__get_recursive_name()
#print " ",cmd,"<",other
popen = subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
m = popen.communicate(other)
ret = popen.wait()
if ret:
e = CalledProcessError(ret,cmd)
e.stdout,e.stderr = m
raise e
class CommandOutput:
def __init__(self,stdout,stderr):
self.stdout = stdout
self.stderr = stderr
return CommandOutput(*m)
def __get_recursive_name(self,sep=None):
m = self
l = []
while m is not None:
l.append(m.__name)
m = m.__parent
l.reverse()
if sep: return sep.join(l)
else: return l
def __str__(self):
return '<Command %r>'%self.__get_recursive_name(sep=" ")
def __repr__(self): return self.__str__()
ip = Command("ip")
service = Command("service")
chkconfig = Command("chkconfig")
ufw = Command("ufw")
iptables = Command("iptables")
augtool = Command("augtool")
ifconfig = Command("ifconfig")
uuidgen = Command("uuidgen")
Fedora = os.path.exists("/etc/fedora-release")
CentOS = os.path.exists("/etc/centos-release") or ( os.path.exists("/etc/redhat-release") and not os.path.exists("/etc/fedora-release") )
#--------------- procedure starts here ------------
def main():
# parse cmd line
opts, args = getopt.getopt(sys.argv[1:], "a", ["host=", "zone=", "pod="])
host=None
zone=None
pod=None
autoMode=False
do_check_kvm = True
for opt, arg in opts:
if opt == "--host":
if arg != "":
host = arg
elif opt == "--zone":
if arg != "":
zone = arg
elif opt == "--pod":
if arg != "":
pod = arg
elif opt == "-a":
autoMode=True
servicename = "@PACKAGE@-console-proxy"
if autoMode:
cloud_utils.setLogFile("/var/log/cloud/setupConsoleProxy.log")
stderr("Welcome to the Cloud Console Proxy setup")
stderr("")
try:
check_hostname()
stderr("The hostname of this machine is properly set up")
except CalledProcessError,e:
bail(E_NOFQDN,"This machine does not have an FQDN (fully-qualified domain name) for a hostname")
stderr("Stopping the Cloud Console Proxy")
cloud_utils.stop_service(servicename)
stderr("Cloud Console Proxy stopped")
ports = "8002".split()
if Fedora or CentOS:
try:
o = chkconfig("--list","iptables")
if ":on" in o.stdout and os.path.exists("/etc/sysconfig/iptables"):
stderr("Setting up firewall rules to permit traffic to Cloud services")
service.iptables.start() ; print o.stdout + o.stderr
for p in ports: iptables("-I","INPUT","1","-p","tcp","--dport",p,'-j','ACCEPT')
o = service.iptables.save() ; print o.stdout + o.stderr
except CalledProcessError,e:
print e.stdout+e.stderr
bail(E_FWRECONFIGFAILED,"Firewall rules could not be set")
else:
stderr("Setting up firewall rules to permit traffic to Cloud services")
try:
for p in ports: ufw.allow(p)
stderr("Rules set")
except CalledProcessError,e:
print e.stdout+e.stderr
bail(E_FWRECONFIGFAILED,"Firewall rules could not be set")
stderr("We are going to enable ufw now. This may disrupt network connectivity and service availability. See the ufw documentation for information on how to manage ufw firewall policies.")
try:
o = ufw.enable < "y\n" ; print o.stdout + o.stderr
except CalledProcessError,e:
print e.stdout+e.stderr
bail(E_FWRECONFIGFAILED,"Firewall could not be enabled")
cloud_utils.setup_consoleproxy_config("@CPSYSCONFDIR@/agent.properties", host, zone, pod)
stderr("Enabling and starting the Cloud Console Proxy")
cloud_utils.enable_service(servicename)
stderr("Cloud Console Proxy restarted")
if __name__ == "__main__":
main()
# FIXMES: 1) nullify networkmanager on ubuntu (asking the user first) and enable the networking service permanently