Esempio n. 1
0
 def on_get(self, req, resp, tenant_id, target_id):
     target = Target.get_target(tenant_id, target_id)
     if target:
         resp.body = self.format_response_body(target.as_dict())
     else:
         msg = 'Cannot find target: {target_id}'.format(target_id=target_id)
         resp.status = falcon.HTTP_404
         resp.body = json.dumps({'description': msg})
Esempio n. 2
0
 def on_get(self, req, resp, tenant_id, target_id):
     target = Target.get_target(tenant_id, target_id)
     if target:
         resp.body = self.format_response_body(target.as_dict())
     else:
         msg = 'Cannot find target: {target_id}'.format(target_id=target_id)
         resp.status = falcon.HTTP_404
         resp.body = json.dumps({'description': msg})
Esempio n. 3
0
 def execute_action(self, job, action):
     # TODO: Replace with actual logging
     print('Executing a sample action')
     print('Tenant ID: {0}'.format(job.tenant_id))
     print('Target IDs: {0}'.format(action.targets))
     targets = [Target.get_target(job.tenant_id, target_id)
                for target_id in action.targets]
     print('Targets: {0}'.format(targets))
     print('Type: {0}'.format(action.action_type))
     print('Params: {0}'.format(action.parameters))
Esempio n. 4
0
 def execute_action(self, job, action):
     # TODO: Replace with actual logging
     print('Executing a sample action')
     print('Tenant ID: {0}'.format(job.tenant_id))
     print('Target IDs: {0}'.format(action.targets))
     targets = [
         Target.get_target(job.tenant_id, target_id)
         for target_id in action.targets
     ]
     print('Targets: {0}'.format(targets))
     print('Type: {0}'.format(action.action_type))
     print('Params: {0}'.format(action.parameters))
Esempio n. 5
0
    def execute_action(self, job, action):
        cmd = action.parameters.get("command")

        for target_id in action.targets:
            target = Target.get_target(job.tenant_id, target_id)
            ip = target.address.address_child
            ssh = target.authentication.get("ssh")

            creds = SSHKeyCredentials(username=ssh.get("username"), key_contents=ssh.get("private_key"))
            client = SSHClient(host=ip.address, port=ip.port, credentials=creds)
            client.connect()
            LOG.info("Remote command plugin executing: %s", cmd)
            resp = client.execute(command=cmd)
            client.close()
            LOG.info("Remote command plugin execution response: %s", resp)
Esempio n. 6
0
    def on_get(self, req, resp, tenant_id, target_id):
        target = Target.get_target(tenant_id, target_id)
        if target:
            address = target.address
            # Nova
            if 'nova' in address.as_dict().keys():
                nova_address = address.address_child

                auth = target.authentication
                try:
                    if 'rackspace' in auth:
                        cls = get_driver(Provider.RACKSPACE)
                        cls(auth['rackspace']['username'],
                            auth['rackspace']['api_key'],
                            region=nova_address.region.lower())
                        resp.status = falcon.HTTP_200
                    else:
                        raise Exception("No supported providers in target: {0}"
                                        .format(target.as_dict()))
                except Exception:
                    resp.status = falcon.HTTP_404
            # SSH
            else:
                ip = address.address_child
                ssh = target.authentication.get('ssh')

                creds = SSHKeyCredentials(
                    username=ssh.get('username'),
                    key_contents=ssh.get('private_key')
                )
                client = SSHClient(
                    host=ip.address,
                    port=ip.port,
                    credentials=creds
                )
                try:
                    client.connect()
                    resp.status = falcon.HTTP_200
                    client.close()
                except SSHException:
                    resp.status = falcon.HTTP_404
        else:
            msg = 'Cannot find target: {target_id}'.format(target_id=target_id)
            resp.status = falcon.HTTP_404
            resp.body = json.dumps({'description': msg})
Esempio n. 7
0
    def on_post(self, req, resp, tenant_id):
        target_id = str(uuid.uuid4())

        body = self.load_body(req, self.validator)
        body['id'] = target_id

        target = Target.build_target_from_dict(tenant_id, body)
        duplicate_target = Target.get_target(tenant_id, target_id=target.name)

        if duplicate_target:
            raise falcon.exceptions.HTTPConflict(
                'Duplicate Target Name',
                'Target names must be unique: {0}'.format(target.name))

        Target.save_target(target)

        resp.status = falcon.HTTP_201
        resp.body = self.format_response_body({'target_id': target_id})
Esempio n. 8
0
    def on_post(self, req, resp, tenant_id):
        target_id = str(uuid.uuid4())

        body = self.load_body(req, self.validator)
        body['id'] = target_id

        target = Target.build_target_from_dict(tenant_id, body)
        duplicate_target = Target.get_target(tenant_id, target_id=target.name)

        if duplicate_target:
            raise falcon.exceptions.HTTPConflict(
                'Duplicate Target Name',
                'Target names must be unique: {0}'.format(target.name))

        Target.save_target(target)

        resp.status = falcon.HTTP_201
        resp.body = self.format_response_body({'target_id': target_id})
Esempio n. 9
0
    def on_get(self, req, resp, tenant_id, target_id):
        target = Target.get_target(tenant_id, target_id)
        if target:
            address = target.address
            # Nova
            if 'nova' in address.as_dict().keys():
                nova_address = address.address_child

                auth = target.authentication
                try:
                    if 'rackspace' in auth:
                        cls = get_driver(Provider.RACKSPACE)
                        cls(auth['rackspace']['username'],
                            auth['rackspace']['api_key'],
                            region=nova_address.region.lower())
                        resp.status = falcon.HTTP_200
                    else:
                        raise Exception(
                            "No supported providers in target: {0}".format(
                                target.as_dict()))
                except Exception:
                    resp.status = falcon.HTTP_404
            # SSH
            else:
                ip = address.address_child
                ssh = target.authentication.get('ssh')

                creds = SSHKeyCredentials(username=ssh.get('username'),
                                          key_contents=ssh.get('private_key'))
                client = SSHClient(host=ip.address,
                                   port=ip.port,
                                   credentials=creds)
                try:
                    client.connect()
                    resp.status = falcon.HTTP_200
                    client.close()
                except SSHException:
                    resp.status = falcon.HTTP_404
        else:
            msg = 'Cannot find target: {target_id}'.format(target_id=target_id)
            resp.status = falcon.HTTP_404
            resp.body = json.dumps({'description': msg})
Esempio n. 10
0
 def execute_action(self, job, action):
     for target_id in action.targets:
         target = Target.get_target(job.tenant_id, target_id)
         self._reboot_target(target)
Esempio n. 11
0
 def execute_action(self, job, action):
     for target_id in action.targets:
         target = Target.get_target(job.tenant_id, target_id)
         self._reboot_target(target)