Ejemplo n.º 1
0
def add_container():
    data = request.get_json(force=True)
    if data is None:
        return jsonify(status="error", error="Bad request"), 400

    if (bool('template' not in data) != bool('clone' not in data)) | bool('name' not in data):
        return jsonify(status="error", error="Bad request"), 400

    print(data)
    if 'template' in data:
        # we want a new container
        if 'store' not in data:
            data.update(store=None)
        if 'xargs' not in data:
            data.update(xargs=None)

        try:
            lxc.create(data.name, data.template, data.store, data.xargs)
        except lxc.ContainerAlreadyExists:
            return jsonify(status="error", error="Container yet exists"), 409
    else:
        #we want to clone a container
        try:
            lxc.clone(data.clone, data.name)
        except lxc.ContainerAlreadyExists:
            return jsonify(status="error", error="Container yet exists"), 409
        finally:
            abort(500)
    return jsonify(status="ok"), 200
Ejemplo n.º 2
0
def add_container():
    data = request.get_json(force=True)
    if data is None:
        return jsonify(status="error", error="Bad request"), 400

    if (not(('template' in data) or ('clone' in data)) or ('name' not in data)):
        return jsonify(status="error", error="Bad request"), 402

    if 'template' in data:
        # we want a new container
        if 'store' not in data:
            data['store'] = ""
        if 'xargs' not in data:
            data['xargs'] = ""

        try:
            lxc.create(data['name'], data['template'], data['store'], data['xargs'])
        except lxc.ContainerAlreadyExists:
            return jsonify(status="error", error="Container yet exists"), 409
    else:
        # we want to clone a container
        try:
            lxc.clone(data['clone'], data['name'])
        except lxc.ContainerAlreadyExists:
            return jsonify(status="error", error="Container yet exists"), 409
    return jsonify(status="ok"), 200
Ejemplo n.º 3
0
    def container_add(self):
        """
        Create a new container.
        """
        try:
            data = self.request.json_body
        except ValueError:
            self.request.response.status = 400
            return {"status": "error", "error": "Bad Request"}

        if data is None:
            self.request.response.status = 400
            return {"status": "error", "error": "Bad Request"}

        if (bool("template" not in data) != bool("clone" not in data)) or bool("name" not in data):
            self.request.response.status = 400
            return {"status": "error", "error": "Bad Request"}

        if "template" in data:
            # we want a new container
            if "store" not in data:
                data.update(store=None)
            if "xargs" not in data:
                data.update(xargs=None)

            try:
                lxc.create(data.name, data.template, data.store, data.xargs)
            except lxc.ContainerAlreadyExists:
                self.request.response.status = 409
                return {"status": "error", "error": "Container already exists"}
        else:
            # we want to clone a container
            try:
                lxc.clone(data.clone, data.name)
            except lxc.ContainerAlreadyExists:
                self.request.response.status = 409
                return {"status": "error", "error": "Container already exists"}
            finally:
                raise HTTPServerError()

        return {"status": "ok"}
Ejemplo n.º 4
0
 def test_00_create(self):
     lxc.create('test00')
     assert os.path.exists('/tmp/lxc/test00')
Ejemplo n.º 5
0
def create_container():
    """
    verify all forms to create a container
    """
    if session['su'] != 'Yes':
        return abort(403)
    if request.method == 'POST':
        name = request.form['name']
        template = request.form['template']
        command = request.form['command']

        if re.match('^(?!^containers$)|[a-zA-Z0-9_-]+$', name):
            storage_method = request.form['backingstore']

            if storage_method == 'default':
                try:
                    if lxc.create(name, template=template, xargs=command) == 0:
                        flash(u'Container %s created successfully!' % name, 'success')
                    else:
                        flash(u'Failed to create %s!' % name, 'error')
                except lxc.ContainerAlreadyExists:
                    flash(u'The Container %s is already created!' % name, 'error')
                except subprocess.CalledProcessError:
                    flash(u'Error! %s' % name, 'error')

            elif storage_method == 'directory':
                directory = request.form['dir']

                if re.match('^/[a-zA-Z0-9_/-]+$', directory) and directory != '':
                    try:
                        if lxc.create(name, template=template, storage='dir --dir %s' % directory, xargs=command) == 0:
                            flash(u'Container %s created successfully!' % name, 'success')
                        else:
                            flash(u'Failed to create %s!' % name, 'error')
                    except lxc.ContainerAlreadyExists:
                        flash(u'The Container %s is already created!' % name, 'error')
                    except subprocess.CalledProcessError:
                        flash(u'Error! %s' % name, 'error')

            elif storage_method == 'btrfs':
                try:
                    if lxc.create(name, template=template, storage='btrfs', xargs=command) == 0:
                        flash(u'Container %s created successfully!' % name, 'success')
                    else:
                        flash(u'Failed to create %s!' % name, 'error')
                except lxc.ContainerAlreadyExists:
                    flash(u'The Container %s is already created!' % name, 'error')
                except subprocess.CalledProcessError:
                    flash(u'Error! %s' % name, 'error')

            elif storage_method == 'zfs':
                zfs = request.form['zpoolname']

                if re.match('^[a-zA-Z0-9_-]+$', zfs) and zfs != '':
                    try:
                        if lxc.create(name, template=template, storage='zfs --zfsroot %s' % zfs, xargs=command) == 0:
                            flash(u'Container %s created successfully!' % name, 'success')
                        else:
                            flash(u'Failed to create %s!' % name, 'error')
                    except lxc.ContainerAlreadyExists:
                        flash(u'The Container %s is already created!' % name, 'error')
                    except subprocess.CalledProcessError:
                        flash(u'Error! %s' % name, 'error')

            elif storage_method == 'lvm':
                lvname = request.form['lvname']
                vgname = request.form['vgname']
                fstype = request.form['fstype']
                fssize = request.form['fssize']
                storage_options = 'lvm'

                if re.match('^[a-zA-Z0-9_-]+$', lvname) and lvname != '':
                    storage_options += ' --lvname %s' % lvname
                if re.match('^[a-zA-Z0-9_-]+$', vgname) and vgname != '':
                    storage_options += ' --vgname %s' % vgname
                if re.match('^[a-z0-9]+$', fstype) and fstype != '':
                    storage_options += ' --fstype %s' % fstype
                if re.match('^[0-9][G|M]$', fssize) and fssize != '':
                    storage_options += ' --fssize %s' % fssize

                try:
                    if lxc.create(name, template=template, storage=storage_options, xargs=command) == 0:
                        flash(u'Container %s created successfully!' % name, 'success')
                    else:
                        flash(u'Failed to create %s!' % name, 'error')
                except lxc.ContainerAlreadyExists:
                    flash(u'The container/logical volume %s is already created!' % name, 'error')
                except subprocess.CalledProcessError:
                    flash(u'Error! %s' % name, 'error')

            else:
                flash(u'Missing parameters to create container!', 'error')

        else:
            if name == '':
                flash(u'Please enter a container name!', 'error')
            else:
                flash(u'Invalid name for \"%s\"!' % name, 'error')

    return redirect(url_for('main.home'))
Ejemplo n.º 6
0
    def create_container(self):
        """
        Verify all forms to create a container.
        """
        if not self.request.user.is_superuser:
            return HTTPForbidden()

        if self.request.method == 'POST':
            name = self.request.POST['name']
            template = self.request.POST['template']
            command = self.request.POST['command']

            if re.match('^(?!^containers$)|[a-zA-Z0-9_-]+$', name):
                storage_method = self.request.POST['backingstore']

                if storage_method == 'default':
                    try:
                        if lxc.create(name, template=template, xargs=command) == 0:
                            self.session.flash('Container {} created successfully'.format(name), queue='success')
                        else:
                            self.session.flash('Failed to create {}'.format(name), queue='error')
                    except lxc.ContainerAlreadyExists:
                        self.session.flash('The container {} already exists'.format(name), queue='error')
                    except subprocess.CalledProcessError:
                        self.session.flash('Error {}'.format(name), queue='error')

                elif storage_method == 'directory':
                    directory = self.request.POST['dir']

                    if re.match('^/[a-zA-Z0-9_/-]+$', directory) and directory != '':
                        try:
                            if lxc.create(name, template=template, storage='dir --dir ' + directory, xargs=command) == 0:
                                self.session.flash('Container {} created successfully'.format(name), queue='success')
                            else:
                                self.session.flash('Failed to create {}'.format(name), 'error')
                        except lxc.ContainerAlreadyExists:
                            self.session.flash('The container {} already exists'.format(name), queue='error')
                        except subprocess.CalledProcessError:
                            self.session.flash('Error {}'.format(name), queue='error')

                elif storage_method == 'zfs':
                    zfs = self.request.POST['zpoolname']

                    if re.match('^[a-zA-Z0-9_-]+$', zfs) and zfs != '':
                        try:
                            if lxc.create(name, template=template, storage='zfs --zfsroot %s' % zfs, xargs=command) == 0:
                                self.session.flash('Container {} created successfully'.format(name), queue='success')
                            else:
                                self.session.flash('Failed to create {}'.format(name), queue='error')
                        except lxc.ContainerAlreadyExists:
                            self.session.flash('The container {} already exists'.format(name), queue='error')
                        except subprocess.CalledProcessError:
                            self.session.flash('Error {}'.format(name), queue='error')

                elif storage_method == 'lvm':
                    lvname = self.request.POST['lvname']
                    vgname = self.request.POST['vgname']
                    fstype = self.request.POST['fstype']
                    fssize = self.request.POST['fssize']
                    storage_options = 'lvm'

                    if re.match('^[a-zA-Z0-9_-]+$', lvname) and lvname != '':
                        storage_options += ' --lvname %s' % lvname
                    if re.match('^[a-zA-Z0-9_-]+$', vgname) and vgname != '':
                        storage_options += ' --vgname %s' % vgname
                    if re.match('^[a-z0-9]+$', fstype) and fstype != '':
                        storage_options += ' --fstype %s' % fstype
                    if re.match('^[0-9][G|M]$', fssize) and fssize != '':
                        storage_options += ' --fssize %s' % fssize

                    try:
                        if lxc.create(name, template=template, storage=storage_options, xargs=command) == 0:
                            self.session.flash('Container {} created successfully'.format(name), queue='success')
                        else:
                            self.session.flash('Failed to create {}'.format(name), queue='error')
                    except lxc.ContainerAlreadyExists:
                        self.session.flash('The container/logical volume {} already exists'.format(name), queue='error')
                    except subprocess.CalledProcessError:
                        self.session.flash('Error {}'.format(name), queue='error')

                else:
                    self.session.flash('Missing parameters to create container', queue='error')

            else:
                if name == '':
                    self.session.flash('Please enter a container name', queue='error')
                else:
                    self.session.flash('Invalid name for "{}"'.format(name), queue='error')

        return HTTPFound(location=self.request.route_path('home'))
Ejemplo n.º 7
0
def create_container():
    """
    verify all forms to create a container
    """
    if session["su"] != "Yes":
        return abort(403)
    if request.method == "POST":
        name = request.form["name"]
        template = request.form["template"]
        command = request.form["command"]

        if re.match("^(?!^containers$)|[a-zA-Z0-9_-]+$", name):
            storage_method = request.form["backingstore"]

            if storage_method == "default":
                try:
                    if lxc.create(name, template=template, xargs=command) == 0:
                        flash(u"Container %s created successfully!" % name, "success")
                    else:
                        flash(u"Failed to create %s!" % name, "error")
                except lxc.ContainerAlreadyExists:
                    flash(u"The Container %s is already created!" % name, "error")
                except subprocess.CalledProcessError:
                    flash(u"Error! %s" % name, "error")

            elif storage_method == "directory":
                directory = request.form["dir"]

                if re.match("^/[a-zA-Z0-9_/-]+$", directory) and directory != "":
                    try:
                        if lxc.create(name, template=template, storage="dir --dir %s" % directory, xargs=command) == 0:
                            flash(u"Container %s created successfully!" % name, "success")
                        else:
                            flash(u"Failed to create %s!" % name, "error")
                    except lxc.ContainerAlreadyExists:
                        flash(u"The Container %s is already created!" % name, "error")
                    except subprocess.CalledProcessError:
                        flash(u"Error! %s" % name, "error")

            elif storage_method == "btrfs":
                try:
                    if lxc.create(name, template=template, storage="btrfs", xargs=command) == 0:
                        flash(u"Container %s created successfully!" % name, "success")
                    else:
                        flash(u"Failed to create %s!" % name, "error")
                except lxc.ContainerAlreadyExists:
                    flash(u"The Container %s is already created!" % name, "error")
                except subprocess.CalledProcessError:
                    flash(u"Error! %s" % name, "error")

            elif storage_method == "zfs":
                zfs = request.form["zpoolname"]

                if re.match("^[a-zA-Z0-9_-]+$", zfs) and zfs != "":
                    try:
                        if lxc.create(name, template=template, storage="zfs --zfsroot %s" % zfs, xargs=command) == 0:
                            flash(u"Container %s created successfully!" % name, "success")
                        else:
                            flash(u"Failed to create %s!" % name, "error")
                    except lxc.ContainerAlreadyExists:
                        flash(u"The Container %s is already created!" % name, "error")
                    except subprocess.CalledProcessError:
                        flash(u"Error! %s" % name, "error")

            elif storage_method == "lvm":
                lvname = request.form["lvname"]
                vgname = request.form["vgname"]
                fstype = request.form["fstype"]
                fssize = request.form["fssize"]
                storage_options = "lvm"

                if re.match("^[a-zA-Z0-9_-]+$", lvname) and lvname != "":
                    storage_options += " --lvname %s" % lvname
                if re.match("^[a-zA-Z0-9_-]+$", vgname) and vgname != "":
                    storage_options += " --vgname %s" % vgname
                if re.match("^[a-z0-9]+$", fstype) and fstype != "":
                    storage_options += " --fstype %s" % fstype
                if re.match("^[0-9][G|M]$", fssize) and fssize != "":
                    storage_options += " --fssize %s" % fssize

                try:
                    if lxc.create(name, template=template, storage=storage_options, xargs=command) == 0:
                        flash(u"Container %s created successfully!" % name, "success")
                    else:
                        flash(u"Failed to create %s!" % name, "error")
                except lxc.ContainerAlreadyExists:
                    flash(u"The container/logical volume %s is already created!" % name, "error")
                except subprocess.CalledProcessError:
                    flash(u"Error! %s" % name, "error")

            else:
                flash(u"Missing parameters to create container!", "error")

        else:
            if name == "":
                flash(u"Please enter a container name!", "error")
            else:
                flash(u'Invalid name for "%s"!' % name, "error")

    return redirect(url_for("main.home"))