Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/scripts/new_license_image.py
Views: 687
#!/usr/bin/env python1###############################################################################2#3# CoCalc: Collaborative Calculation4#5# Copyright (C) 2016, Sagemath Inc.6#7# This program is free software: you can redistribute it and/or modify8# it under the terms of the GNU General Public License as published by9# the Free Software Foundation, either version 3 of the License, or10# (at your option) any later version.11#12# This program is distributed in the hope that it will be useful,13# but WITHOUT ANY WARRANTY; without even the implied warranty of14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15# GNU General Public License for more details.16#17# You should have received a copy of the GNU General Public License18# along with this program. If not, see <http://www.gnu.org/licenses/>.19#20###############################################################################2122import argparse, os, sys, time23VM_PATH = os.path.join(os.environ['HOME'], 'vm/images/license/')242526def cmd(s):27print(s)28if os.system(s):29raise RuntimeError("error executing '%s'" % s)303132cmd("ls -lth %s|head -5" % VM_PATH)3334parser = argparse.ArgumentParser(description="Create a new VM image.")35parser.add_argument(36"--prev", dest="prev", type=str, help="previous vm image name", default="")37parser.add_argument(38"--next", dest="next", type=str, help="new vm image name", default="")39args = parser.parse_args()4041prev = args.prev42next = args.next4344if prev == "":45# make it the most recent image46prev = os.popen(47"ls -1t %s/*.qcow2|head -1" % VM_PATH).read().strip().rstrip('.qcow2')4849if next == "":50# make image name sagemathcloud-date51next = time.strftime("sagemathcloud-%Y-%m-%d-%H%M")5253defined_machines = os.popen("virsh_list").read()5455for machine in [prev, next]:56if machine in defined_machines:57print("%s is currently defined. Please undefine it before proceeding further, in order to avoid any possible corruption." % machine)58sys.exit(1)5960prev_path = os.path.join(VM_PATH, prev + '.qcow2')61next_path = os.path.join(VM_PATH, next + '.qcow2')6263if not os.path.exists(prev_path):64raise ValueError("previous vm image doesn't exist -- " + prev_path)6566if os.path.exists(next_path):67raise ValueError("next vm image already exists -- " + next_path)6869cmd("qemu-img create -b %s -f qcow2 %s" % (prev_path, next_path))70cmd("chgrp kvm %s; chmod g+rw %s" % (next_path, next_path))7172cmd("virt-install --connect qemu:///system --cpu host --network network:default,model=virtio --name %s --vcpus=4 --ram 4000 --import --disk %s,device=disk,bus=virtio,format=qcow2,cache=writeback --noautoconsole --graphics vnc,port=12505"73% (next, next_path))7475print("Booting...")7677while True:78ip = os.popen("kvm_addresses.py %s" % next).read().strip()79if not ip:80print("waiting for ip address...")81time.sleep(2)82else:83print("The ip address is: '%s'" % ip)84break8586print("""87You probably want to do something like this:8889sshvm %s90sudo su91./update_salvus92apt-get update; apt-get upgrade9394reboot -h now95sshvm %s96sudo shutdown -h now9798Then99100virsh undefine %s101cd vm/images/base/102./push103104""" % (next, next, next))105106107