4 # Copyright (c) 2018 Vojtech Horky
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
11 # - Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # - Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 # - The name of the author may not be used to endorse or promote products
17 # derived from this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 Keeps track of running virtual machines.
39 def __init__(self
, controller
, architecture
, boot_image
, memory_amount
):
40 self
.controller_class
= controller
41 self
.architecture
= architecture
42 self
.boot_image
= boot_image
43 self
.memory_amount
= memory_amount
47 def create(self
, name
):
48 if name
in self
.instances
:
49 raise Exception("Duplicate machine name {}.".format(name
))
50 self
.instances
[name
] = self
.controller_class(self
.architecture
, name
, self
.boot_image
)
51 self
.instances
[name
].memory
= self
.memory_amount
53 return self
.instances
[name
]
55 def get(self
, name
=None):
60 if name
in self
.instances
:
62 return self
.instances
[name
]
66 def terminate(self
, vterm_dump_filename
, last_screenshot_filename
):
67 for i
in self
.instances
:
68 self
.instances
[i
].terminate()
69 if vterm_dump_filename
is not None:
70 with
open(vterm_dump_filename
, 'w') as f
:
71 for i
in self
.instances
:
72 lines
= '\n'.join(self
.instances
[i
].full_vterm
)
74 if last_screenshot_filename
is not None:
75 for i
in self
.instances
:
76 filename
= self
.instances
[i
].screenshot_filename
77 if filename
is not None:
78 proc
= subprocess
.Popen(['convert', filename
, last_screenshot_filename
])
80 if proc
.returncode
!= 0:
81 raise Exception("Saving screenshot failed.")
85 Base class for controllers of specific virtual machine emulators.
88 def __init__(self
, provider
):
89 self
.provider_name
= provider
90 # Patched by VMManager
92 self
.screenshot_filename
= None
93 # All lines seen in the terminal
94 # (do not reset unless you know what you are doing).
96 # Used to keep track of new-lines
98 # Amount of memory (MB) (patched by VMM manager)
102 def is_supported(self
, arch
):
104 Tells whether this controller supports given architecture.
108 def boot(self
, **kwargs
):
110 Bring the machine up.
120 def type(self
, what
):
122 Type given text into vterm.
124 print("type('{}') @ {}".format(what
, self
.provider_name
))
127 def same_vterm_tail(self
, lines
):
128 lines_count
= len(lines
)
129 for i
in range(-1, -lines_count
- 1, -1):
131 if lines
[i
] != self
.full_vterm
[i
]:
134 a
= lines
[-1].replace("_", " ").strip()
135 b
= self
.full_vterm
[-1].replace("_", " ").strip()
136 if not a
.startswith(b
):
140 def capture_vterm(self
):
142 Capture contents of current terminal window and updates self.vterm
145 # Read everything from the terminal and get rid of completely empty
146 # lines (for first commands when the screen is empty).
147 lines
= self
.capture_vterm_impl()
148 lines
= [l
.strip() for l
in lines
]
149 while (len(lines
) > 0) and (lines
[-1].strip() == ""):
151 if (len(lines
) == 0):
154 # When this is the very first screen, we simply copy it.
155 if len(self
.full_vterm
) == 0:
157 self
.full_vterm
.append(l
)
160 # Otherwise, we find whether there is some overlap, i.e. whether
161 # we are capturing a rolling screen.
162 lines_count
= len(lines
)
164 for i
in range(lines_count
, 0, -1):
165 if self
.same_vterm_tail(lines
[0:i
]):
168 # If there is no overlap, we might have missed some lines.
170 self
.full_vterm
.append("!!!!!! WARNING: probably missed some lines here !!!!!")
172 # Otherwise, update the last line (last capture might have
173 # missed some characters).
174 if len(self
.full_vterm
) > 0:
175 self
.full_vterm
= self
.full_vterm
[0:-1]
176 if len(self
.vterm
) > 0:
177 self
.vterm
= self
.vterm
[0:-1]
178 same_lines
= same_lines
- 1
180 for i
in range(same_lines
, lines_count
):
181 self
.full_vterm
.append(lines
[i
])
182 self
.vterm
.append(lines
[i
])
184 def capture_vterm_impl(self
):
186 Do not call but reimplement in subclass.
190 def get_temp(self
, id):
192 Get temporary file name.
194 os
.makedirs('tmp-vm-python', exist_ok
=True)
195 return 'tmp-vm-python/tmp-' + self
.name
+ '-' + id