Beispiel #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
        except:
            abort(500)
    return jsonify(status="ok"), 200
Beispiel #2
0
def clone_container():
    '''
    verify all forms to clone a container
    '''
    if 'logged_in' in session:
        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('home'))
    return render_template('login.html')
Beispiel #3
0
def clone_container():
    '''
    verify all forms to clone a container
    '''
    if 'logged_in' in session:
        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('home'))
    return render_template('login.html')