Merge "Remove Compass from genesis."
[genesis.git] / opensteak / tools / opensteak / virsh.py
1 #!/usr/bin/python3
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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
13 # under the License.
14 #
15 # Authors:
16 # @author: David Blaisonneau <david.blaisonneau@orange.com>
17 # @author: Arnaud Morin <arnaud1.morin@orange.com>
18
19 """
20 Virsh library
21 """
22
23 import subprocess
24 import os
25
26 class OpenSteakVirsh:
27
28     virsh = "/usr/bin/virsh"
29     genisoimage = "/usr/bin/genisoimage"
30     environment = ""
31
32     ###
33     # INIT
34     ###
35     def __init__(self):
36         self.environment = dict(os.environ)  # Copy current environment
37         self.environment['LANG'] = 'en_US.UTF-8'
38
39
40     ###
41     # VOLUMES
42     ###
43     def volumeList(self, pool="default"):
44         """
45         Return all volumes from a pool
46         """
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()
49
50         # Split lines
51         lines = stdout.splitlines()
52
53         # Foreach line, split with space and construct a dictionnary
54         newLines = {}
55         for line in lines:
56             name, path = line.split(maxsplit=1)
57             newLines[name.strip()] = path.strip()
58
59         return newLines
60
61     def volumeDelete(self, path):
62         """
63         Delete a volume
64         """
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()
67
68         return {"stdout":stdout, "stderr":stderr}
69
70     def volumeClone(self, origin, name, pool="default"):
71         """
72         Clone a volume
73         """
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()
76
77         return {"stdout":stdout, "stderr":stderr}
78
79     def volumeResize(self, name, size, pool="default"):
80         """
81         Resize a volume
82         """
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()
85
86         return {"stdout":stdout, "stderr":stderr}
87
88     ###
89     # POOLS
90     ###
91     def poolRefresh(self, pool="default"):
92         """
93         Refresh a pool
94         """
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()
97
98         return {"stdout":stdout, "stderr":stderr}
99
100     ###
101     # DOMAINS
102     ###
103     def domainList(self):
104         """
105         Return all domains (VM)
106         """
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()
109
110         # Split lines
111         lines = stdout.splitlines()
112
113         # Foreach line, split with space and construct a dictionnary
114         newLines = {}
115         for line in lines:
116             id, name, status = line.split(maxsplit=2)
117             newLines[name.strip()] = status.strip()
118
119         return newLines
120
121     def domainDefine(self, xml):
122         """
123         Define a domain (create a VM)
124         """
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()
127
128         return {"stdout":stdout, "stderr":stderr}
129
130     def domainUndefine(self, name):
131         """
132         Undefine a domain (delete a VM)
133         """
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()
136
137         return {"stdout":stdout, "stderr":stderr}
138
139     def domainStart(self, name):
140         """
141         Define a domain (create a VM)
142         """
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()
145
146         return {"stdout":stdout, "stderr":stderr}
147
148     def domainDestroy(self, name):
149         """
150         Destroy a domain (stop a VM)
151         """
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()
154
155         return {"stdout":stdout, "stderr":stderr}
156
157     ###
158     # ISO
159     ###
160     def generateConfiguration(self, name, files):
161         """
162         Generate an ISO file
163         """
164
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)
168
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()
172
173         return {"stdout":stdout, "stderr":stderr}
174