Exemplo 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 (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
Exemplo 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 (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
Exemplo n.º 3
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
Exemplo n.º 4
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
Exemplo n.º 5
0
    def clone_container(self):
        """
        Verify all forms to clone a container.
        """
        if not self.request.user.is_superuser:
            return HTTPForbidden()

        if self.request.method == 'POST':
            orig = self.request.POST['orig']
            name = self.request.POST['name']
            snapshot = asbool(self.request.POST.get(['snapshot'], 'false'))

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

                try:
                    out = lxc.clone(orig=orig, new=name, snapshot=snapshot)
                except lxc.ContainerAlreadyExists:
                    self.session.flash('The Container {} already exists'.format(name), queue='error')
                except subprocess.CalledProcessError:
                    self.session.flash("Can't snapshot a directory", queue='error')

                # TODO: this condition is never gonna happen! because 0 evaluates to False in Python
                if out and out == 0:
                    self.session.flash('Container {} cloned into {} successfully'.format(orig, name), queue='success')
                elif out and out != 0:
                    self.session.flash('Failed to clone {} into {}'.format(orig, name), 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'))
Exemplo n.º 6
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"}
Exemplo n.º 7
0
def clone_container():
    """
    verify all forms to clone a container
    """
    if session['su'] != 'Yes':
        return abort(403)
    if request.method == 'POST':
        orig = request.form['orig']
        name = request.form['name']

        try:
            snapshot = request.form['snapshot']
            if snapshot == 'True':
                snapshot = True
        except KeyError:
            snapshot = False

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

            try:
                out = lxc.clone(orig=orig, new=name, snapshot=snapshot)
            except lxc.ContainerAlreadyExists:
                flash(u'The Container %s already exists!' % name, 'error')
            except subprocess.CalledProcessError:
                flash(u'Can\'t snapshot a directory', 'error')

            if out and out == 0:
                flash(
                    u'Container %s cloned into %s successfully!' %
                    (orig, name), 'success')
            elif out and out != 0:
                flash(u'Failed to clone %s into %s!' % (orig, name), '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'))
Exemplo n.º 8
0
def clone_container():
    """
    verify all forms to clone a container
    """
    if session['su'] != 'Yes':
        return abort(403)
    if request.method == 'POST':
        orig = request.form['orig']
        name = request.form['name']

        try:
            snapshot = request.form['snapshot']
            if snapshot == 'True':
                snapshot = True
        except KeyError:
            snapshot = False

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

            try:
                out = lxc.clone(orig=orig, new=name, snapshot=snapshot)
            except lxc.ContainerAlreadyExists:
                flash(u'The Container %s already exists!' % name, 'error')
            except subprocess.CalledProcessError:
                flash(u'Can\'t snapshot a directory', 'error')

            if out and out == 0:
                flash(u'Container %s cloned into %s successfully!' % (orig, name), 'success')
            elif out and out != 0:
                flash(u'Failed to clone %s into %s!' % (orig, name), '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'))
Exemplo n.º 9
0
def clone_container():
    """
    verify all forms to clone a container
    """
    if session["su"] != "Yes":
        return abort(403)
    if request.method == "POST":
        orig = request.form["orig"]
        name = request.form["name"]

        try:
            snapshot = request.form["snapshot"]
            if snapshot == "True":
                snapshot = True
        except KeyError:
            snapshot = False

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

            try:
                out = lxc.clone(orig=orig, new=name, snapshot=snapshot)
            except lxc.ContainerAlreadyExists:
                flash(u"The Container %s already exists!" % name, "error")
            except subprocess.CalledProcessError:
                flash(u"Can't snapshot a directory", "error")

            if out and out == 0:
                flash(u"Container %s cloned into %s successfully!" % (orig, name), "success")
            elif out and out != 0:
                flash(u"Failed to clone %s into %s!" % (orig, name), "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"))
Exemplo n.º 10
0
 def test_01_clone(self):
     lxc.clone('test00', 'testclone')
     assert os.path.exists('/tmp/lxc/test00')
     assert os.path.exists('/tmp/lxc/testclone')
Exemplo n.º 11
0
 def test_01_clone(self):
     lxc.clone('test00', 'testclone')
     assert os.path.exists('/tmp/lxc/test00')
     assert os.path.exists('/tmp/lxc/testclone')