2 # -*- coding: utf-8 -*-
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
16 # @author: David Blaisonneau <david.blaisonneau@orange.com>
17 # @author: Arnaud Morin <arnaud1.morin@orange.com>
28 virsh = "/usr/bin/virsh"
29 genisoimage = "/usr/bin/genisoimage"
36 self.environment = dict(os.environ) # Copy current environment
37 self.environment['LANG'] = 'en_US.UTF-8'
43 def volumeList(self, pool="default"):
45 Return all volumes from a pool
47 p = subprocess.Popen([self.virsh, "-q", "vol-list", pool], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, env=self.environment)
48 stdout, stderr = p.communicate()
51 lines = stdout.splitlines()
53 # Foreach line, split with space and construct a dictionnary
56 name, path = line.split(maxsplit=1)
57 newLines[name.strip()] = path.strip()
61 def volumeDelete(self, path):
65 p = subprocess.Popen([self.virsh, "-q", "vol-delete", path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, env=self.environment)
66 stdout, stderr = p.communicate()
68 return {"stdout":stdout, "stderr":stderr}
70 def volumeClone(self, origin, name, pool="default"):
74 p = subprocess.Popen([self.virsh, "-q", "vol-clone", "--pool", pool, origin, name], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, env=self.environment)
75 stdout, stderr = p.communicate()
77 return {"stdout":stdout, "stderr":stderr}
79 def volumeResize(self, name, size, pool="default"):
83 p = subprocess.Popen([self.virsh, "-q", "vol-resize", "--pool", pool, name, size], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, env=self.environment)
84 stdout, stderr = p.communicate()
86 return {"stdout":stdout, "stderr":stderr}
91 def poolRefresh(self, pool="default"):
95 p = subprocess.Popen([self.virsh, "-q", "pool-refresh", pool], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, env=self.environment)
96 stdout, stderr = p.communicate()
98 return {"stdout":stdout, "stderr":stderr}
103 def domainList(self):
105 Return all domains (VM)
107 p = subprocess.Popen([self.virsh, "-q", "list", "--all"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, env=self.environment)
108 stdout, stderr = p.communicate()
111 lines = stdout.splitlines()
113 # Foreach line, split with space and construct a dictionnary
116 id, name, status = line.split(maxsplit=2)
117 newLines[name.strip()] = status.strip()
121 def domainDefine(self, xml):
123 Define a domain (create a VM)
125 p = subprocess.Popen([self.virsh, "-q", "define", xml], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, env=self.environment)
126 stdout, stderr = p.communicate()
128 return {"stdout":stdout, "stderr":stderr}
130 def domainUndefine(self, name):
132 Undefine a domain (delete a VM)
134 p = subprocess.Popen([self.virsh, "-q", "undefine", name], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, env=self.environment)
135 stdout, stderr = p.communicate()
137 return {"stdout":stdout, "stderr":stderr}
139 def domainStart(self, name):
141 Define a domain (create a VM)
143 p = subprocess.Popen([self.virsh, "-q", "start", name], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, env=self.environment)
144 stdout, stderr = p.communicate()
146 return {"stdout":stdout, "stderr":stderr}
148 def domainDestroy(self, name):
150 Destroy a domain (stop a VM)
152 p = subprocess.Popen([self.virsh, "-q", "destroy", name], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, env=self.environment)
153 stdout, stderr = p.communicate()
155 return {"stdout":stdout, "stderr":stderr}
160 def generateConfiguration(self, name, files):
165 commandArray = [self.genisoimage, "-quiet", "-o", "/var/lib/libvirt/images/{0}-configuration.iso".format(name), "-volid", "cidata", "-joliet", "-rock"]
166 for k, f in files.items():
167 commandArray.append(f)
169 # Generate the iso file
170 p = subprocess.Popen(commandArray, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, env=self.environment)
171 stdout, stderr = p.communicate()
173 return {"stdout":stdout, "stderr":stderr}