Display image size when creating vcluster.

Compare image size and disk size when creating vcluster.
This commit is contained in:
zhongyehong 2018-11-15 18:10:42 +08:00
parent 613c5cee4a
commit ad84d416bb
4 changed files with 55 additions and 4 deletions

View File

@ -116,6 +116,8 @@ class VclusterMgr(object):
def create_cluster(self, clustername, username, image, user_info, setting):
if self.is_cluster(clustername, username):
return [False, "cluster:%s already exists" % clustername]
if self.imgmgr.get_image_size(image) + 100 > int(setting["disk"]):
return [False, "the size of disk is not big enough for the image"]
clustersize = int(self.defaultsize)
logger.info ("starting cluster %s with %d containers for %s" % (clustername, int(clustersize), username))
workers = self.nodemgr.get_nodeips()
@ -202,6 +204,8 @@ class VclusterMgr(object):
def scale_out_cluster(self,clustername,username, image,user_info, setting):
if not self.is_cluster(clustername,username):
return [False, "cluster:%s not found" % clustername]
if self.imgmgr.get_image_size(image) + 100 > int(setting["disk"]):
return [False, "the size of disk is not big enough for the image"]
workers = self.nodemgr.get_nodeips()
if (len(workers) == 0):
logger.warning("no workers to start containers, scale out failed")

View File

@ -380,6 +380,29 @@ class ImageMgr():
return ""
return image.description
def get_image_size(self, image):
imagename = image['name']
imagetype = image['type']
imageowner = image['owner']
if imagename == "base" and imagetype == "base":
return 0
if imagetype == "private":
imgpath = self.imgpath + "private/" + user + "/"
else:
imgpath = self.imgpath + "public/" + imageowner + "/"
return os.stat(os.path.join(imgpath, imagename)).st_size // (1024*1024)
def format_size(self, size_in_byte):
if size_in_byte < 1024:
return str(size_in_byte) + "B"
elif size_in_byte < 1024*1024:
return str(size_in_byte//1024) + "KB"
elif size_in_byte < 1024*1024*1024:
return str(size_in_byte//(1024*1024)) + "MB"
else:
return str(size_in_byte//(1024*1024*1024)) + "GB"
def list_images(self,user):
images = {}
images["private"] = []
@ -398,6 +421,9 @@ class ImageMgr():
[time, description] = self.get_image_info(user, imagename, "private")
fimage["time"] = time
fimage["description"] = description
fimage["size"] = os.stat(os.path.join(imgpath, image)).st_size
fimage["size_format"] = self.format_size(fimage["size"])
fimage["size_in_mb"] = fimage["size"] // (1024*1024)
images["private"].append(fimage)
except Exception as e:
logger.error(e)
@ -423,6 +449,9 @@ class ImageMgr():
[time, description] = self.get_image_info(public_user, imagename, "public")
fimage["time"] = time
fimage["description"] = description
fimage["size"] = os.stat(os.path.join(imgpath, image)).st_size
fimage["size_format"] = self.format_size(fimage["size"])
fimage["size_in_mb"] = fimage["size"] // (1024*1024)
images["public"][public_user].append(fimage)
except Exception as e:
logger.error(e)

View File

@ -64,6 +64,7 @@
<th>ImageName</th>
<th>Type</th>
<th>Owner</th>
<th>Size</th>
<th>Description</th>
<th>Choose</th>
</tr>
@ -73,16 +74,18 @@
<td>base</td>
<td>public</td>
<td>docklet</td>
<td>--</td>
<td>A base image for you</td>
<td><div class="i-checks"><label><input type="radio" name="image" value="base_base_base" checked="checked"></label></div></td>
<td><div class="i-checks"><label><input type="radio" name="image" value="base_base_base" checked="checked" onchange="setMinDiskSize(0)"></label></div></td>
</tr>
{% for image in images['private'] %}
<tr>
<td>{{image['name']}}</td>
<td>private</td>
<td>{{user}}</td>
<td>{{image['size_format']}}</td>
<td><a href="/image/{{masterips[0].split("@")[0]}}/description/{{image['name']}}_{{user}}_private/" target="_blank">{{image['description']}}</a></td>
<td><div class="i-checks"><label><input type="radio" name="image" value="{{image['name']}}_{{user}}_private"></label></div></td>
<td><div class="i-checks"><label><input type="radio" name="image" value="{{image['name']}}_{{user}}_private" onchange="setMinDiskSize({{image['size_in_mb']}})"></label></div></td>
</tr>
{% endfor %}
{% for p_user,p_images in images['public'].items() %}
@ -91,8 +94,9 @@
<td>{{image['name']}}</td>
<td>public</td>
<td>{{p_user}}</td>
<td>{{image['size_format']}}</td>
<td><a href="/image/{{masterips[0].split("@")[0]}}/description/{{image['name']}}_{{p_user}}_public/" target="_blank">{{image['description']}}</a></td>
<td><div class="i-checks"><label><input type="radio" name="image" value="{{image['name']}}_{{p_user}}_public"></label></div></td>
<td><div class="i-checks"><label><input type="radio" name="image" value="{{image['name']}}_{{p_user}}_public" onchange="setMinDiskSize({{image['size_in_mb']}})"></label></div></td>
</tr>
{% endfor %}
{% endfor %}
@ -125,7 +129,7 @@
</div>
<div class="form-group">
<label class="col-sm-2 control-label">DISK</label>
<div class="col-sm-3"><input type="number" class="form-control" name="diskSetting" id="diskSetting" value= {{defaultsetting['disk']}} /> {{usage['disk']}} MB/{{quota['disk']}}MB
<div class="col-sm-3"><input type="number" class="form-control" name="diskSetting" id="diskSetting" value= {{defaultsetting['disk']}} /> {{usage['disk']}} MB/{{quota['disk']}}MB (min value is the size of image + 100)
</div>
</div>
</div>
@ -167,6 +171,12 @@
<script src="http://cdn.bootcss.com/datatables/1.10.11/js/dataTables.bootstrap.js"></script>
<script src="http://cdn.bootcss.com/datatables-tabletools/2.1.5/js/TableTools.min.js"></script>
<script type="text/javascript">
function setMinDiskSize(minSize) {
var disk = document.getElementById('diskSetting');
disk.setAttribute('min', minSize+100);
}
</script>
<script type="text/javascript">
$("select#masterselector").change(function() {

View File

@ -80,6 +80,7 @@
<th>ImageName</th>
<th>Type</th>
<th>Owner</th>
<th>Size</th>
<th>Choose</th>
</tr>
</thead>
@ -88,6 +89,7 @@
<td>base</td>
<td>public</td>
<td>docklet</td>
<td>--</td>
<td><input type="radio" name="image" value="base_base_base" checked="checked"></td>
</tr>
{% for image in allimages[master]['private'] %}
@ -95,6 +97,7 @@
<td>{{image['name']}}</td>
<td>private</td>
<td>{{mysession['username']}}</td>
<td>{{image['size_format']}}</td>
<td><input type="radio" name="image" value="{{image['name']}}_{{mysession['username']}}_private"></td>
</tr>
{% endfor %}
@ -104,6 +107,7 @@
<td>{{image['name']}}</td>
<td>public</td>
<td>{{p_user}}</td>
<td>{{image['size_format']}}</td>
<td><input type="radio" name="image" value="{{image['name']}}_{{p_user}}_public"></td>
</tr>
{% endfor %}
@ -327,6 +331,7 @@
<th>Type</th>
<th>Owner</th>
<th>CreateTime</th>
<th>Size</th>
<th>Description</th>
<th>Location</th>
<th>Status</th>
@ -339,6 +344,7 @@
<td>public</td>
<td>docklet</td>
<td>2015-01-01 00:00:00</td>
<td>--</td>
<td>A Base Image For You</td>
<td>--</td>
<td></td>
@ -351,6 +357,7 @@
<td>private</td>
<td>{{mysession['username']}}</td>
<td>{{image['time']}}</td>
<td>{{image['size_format']}}</td>
<td><a href="/image/{{master.split("@")[0]}}/description/{{image['name']}}_{{mysession['username']}}_private/" target="_blank">{{image['description']}}</a></td>
<td>{{master.split("@")[1]}}</td>
{% if image['isshared'] == 'false' %}
@ -414,6 +421,7 @@
<td>public</td>
<td>{{p_user}}</td>
<td>{{image['time']}}</td>
<td>{{image['size_format']}}</td>
<td><a href="/image/{{master.split("@")[0]}}/description/{{image['name']}}_{{p_user}}_public/" target="_blank">{{image['description']}}</a></td>
<td>{{master.split("@")[1]}}</td>
<td></td>