Exemple #1
0
 def __init__(self):
     self.backend = VagrantBackend()
Exemple #2
0
class InstancesApi(Resource):
    backend = None

    def __init__(self):
        self.backend = VagrantBackend()

    def get(self):
        instances = self.backend.get_all_instances()

        return {
            'instances': map(lambda t: marshal(t, instance_fields), instances),
        }

    def delete(self):
        instanceId = int(request.json['id'])
        self.backend.delete(instanceId)

    def post(self):
        query = request.get_json()
        instanceId = query.get('id')
        if instanceId:
            instanceId = int(instanceId)
            instance = self.backend.get(instanceId)

            auditlog(current_user,
                     '{} instance'.format(query.get('state', 'unknown')),
                     instance,
                     request_details=request.get_json())
            if 'start' in query.get('state', ''):
                provider = query['state'].replace('start-', '')
                instance.start(provider)

            elif query.get('state') == 'stop':
                instance.stop()
            elif query.get('state') == 'sync':
                instance.sync()

        elif query.get('state') == 'create':
            instance = self.backend.create(query)
        else:
            return self.get()

        return {
            'instance': marshal(instance, instance_fields),
        }

    def _sync(self, id):
        self.backend.sync(id)

    def _stop(self, id):
        self.backend.stop(id)

    def _start(self, id):
        self.backend.start(id)

    def _getInstance(self, id):
        instances = self.backend.get_all_instances()
        for instance in instances:
            if instance.id == id:
                return instance
Exemple #3
0
class InstanceApi(Resource):
    backend = None

    def __init__(self):
        self.backend = VagrantBackend()

    def get(self, id, machineName=None):
        instance = self._getInstance(id)
        jeto_infos = None
        if machineName is None:
            instance.status, jeto_infos, scripts, date_commit = instance._status(
            )
            # instance.jeto_infos = json.dumps(instance.jeto_infos)
        else:
            # app.logger.debug(instance._ip(machineName))
            return {'ip': instance._ip(machineName)}

        instance_json = marshal(instance, instance_fields)
        instance_json['date_commit'] = date_commit
        instance_json['jeto_infos'] = jeto_infos
        instance_json['scripts'] = scripts
        return instance_json

    def post(self, id):
        instance = self._getInstance(id) or abort(404)

        changed = False
        query = request.get_json()
        if 'name' in query:
            if query['name'] != instance.name:
                instance.name = query['name']
                changed = True

        if changed:
            instance.save()

        if 'machine' in query:
            machineName = query['machine']

        state = query.get('state')
        permission = states.get(state)
        if permission:
            if current_user.has_permission(permission, id):
                if state == 'runScript':
                    instance.runScript(query.get('script'), machineName)
                elif state == 'rsync':
                    instance.rsync()
                elif state == 'sync':
                    instance.sync()
                else:
                    getattr(self, state)(id, machineName)
            else:
                abort(403)

    def provision(self, id, machineName):
        return self.backend.provision(id, machineName)

    def stop(self, id, machineName):
        return self.backend.stop(id, machineName)

    def start(self, id, machineName):
        return self.backend.start(id, machineName)

    def sync(self, id):
        return self.backend.sync(id)

    def delete(self, id):
        instanceId = int(id)
        return self.backend.delete(instanceId)

    def _getInstance(self, id):
        instances = self.backend.get_all_instances()
        for instance in instances:
            if instance.id == id:
                return instance
Exemple #4
0
 def __init__(self):
     self.backend = VagrantBackend()
Exemple #5
0
class InstancesApi(Resource):
    backend = None

    def __init__(self):
        self.backend = VagrantBackend()

    def get(self):
        instances = self.backend.get_all_instances()

        return {
            'instances': map(lambda t: marshal(t, instance_fields), instances),
        }

    def delete(self):
        instanceId = int(request.json['id'])
        self.backend.delete(instanceId)

    def post(self):
        query = request.get_json()
        instanceId = query.get('id')
        if instanceId:
            instanceId = int(instanceId)
            instance = self.backend.get(instanceId)

            auditlog(
                current_user,
                '{} instance'.format(query.get('state', 'unknown')),
                instance,
                request_details=request.get_json())
            if 'start' in query.get('state', ''):
                provider = query['state'].replace('start-', '')
                instance.start(provider)

            elif query.get('state') == 'stop':
                instance.stop()
            elif query.get('state') == 'sync':
                instance.sync()

        elif query.get('state') == 'create':
            instance = self.backend.create(query)
        else:
            return self.get()

        return {
            'instance': marshal(instance, instance_fields),
        }

    def _sync(self, id):
        self.backend.sync(id)

    def _stop(self, id):
        self.backend.stop(id)

    def _start(self, id):
        self.backend.start(id)

    def _getInstance(self, id):
        instances = self.backend.get_all_instances()
        for instance in instances:
            if instance.id == id:
                return instance
Exemple #6
0
class InstanceApi(Resource):
    backend = None

    def __init__(self):
        self.backend = VagrantBackend()

    def get(self, id, machineName=None):
        instance = self._getInstance(id)
        jeto_infos = None
        if machineName is None:
            instance.status, jeto_infos, scripts, date_commit = instance._status()
            # instance.jeto_infos = json.dumps(instance.jeto_infos)
        else:
            # app.logger.debug(instance._ip(machineName))
            return {'ip': instance._ip(machineName)}

        instance_json = marshal(instance, instance_fields)
        instance_json['date_commit'] = date_commit
        instance_json['jeto_infos'] = jeto_infos
        instance_json['scripts'] = scripts
        return instance_json

    def post(self, id):
        instance = self._getInstance(id) or abort(404)

        changed = False
        query = request.get_json()
        if 'name' in query:
            if query['name'] != instance.name:
                instance.name = query['name']
                changed = True

        if changed:
            instance.save()

        if 'machine' in query:
            machineName = query['machine']

        state = query.get('state')
        permission = states.get(state)
        if permission:
            if current_user.has_permission(permission, id):
                if state == 'runScript':
                    instance.runScript(query.get('script'), machineName)
                elif state == 'rsync':
                    instance.rsync()
                elif state == 'sync':
                    instance.sync()
                else:
                    getattr(self, state)(id, machineName)
            else:
                abort(403)

    def provision(self, id, machineName):
        return self.backend.provision(id, machineName)

    def stop(self, id, machineName):
        return self.backend.stop(id, machineName)

    def start(self, id, machineName):
        return self.backend.start(id, machineName)

    def sync(self, id):
        return self.backend.sync(id)

    def delete(self, id):
        instanceId = int(id)
        return self.backend.delete(instanceId)

    def _getInstance(self, id):
        instances = self.backend.get_all_instances()
        for instance in instances:
            if instance.id == id:
                return instance