Esempio n. 1
0
    def test_dump_yaml_to_file_without_name(self):
        Playbook().insert({
            'name': 'cool-playbook',
            'playbook': {
                'hosts': 'test',
                'remote_user': '******',
                'tasks': [{
                    'name': 'test debug',
                    'debug': {
                        'msg': 'Debug msg'
                    }
                }]
            }
        })

        playbook = Playbook().find()
        self.assertNotEqual(playbook, None, 'Playbook obj not found')
        yaml_playbook = playbook.parse_yaml()

        path = yaml_to_file(data=yaml_playbook,
                            domain=self.domain,
                            root=self.root,
                            base_path=self.base_path,
                            sub_path=self.sub_path_playbooks)

        self.assertEqual(os.path.exists(path), True, 'File does not exists')
        file = open(path, 'r')

        content = '---\n- hosts: test\n  remote_user: test1\n  tasks:' \
                  '\n  - debug:\n      msg: Debug msg\n    name: test debug\n'

        self.assertEqual(file.read(), content, 'File content is not correct')
        shutil.rmtree('./files-generated', ignore_errors=False, onerror=None)
Esempio n. 2
0
    def test_find_playbook(self):
        h = {
            'name': 'cool-playbook',
            'playbook': [
                {
                    'hosts': 'test',
                    'remote_user': '******',
                    'tasks': [
                        {
                            'name': 'test debug',
                            'debug': {
                                'msg': 'Debug msg'
                            }
                        }
                    ]
                }
            ]
        }
        keys = ['_id', 'name', 'playbook', 'deleted']

        Playbook().insert(h)
        playbook = Playbook().find()

        self.assertNotEqual(playbook, None, 'Playbook obj not found')
        self.assertIsInstance(playbook.data, dict,
                              'Playbook data is not a dict')
        self.assertListEqual(list(playbook.data.keys()), keys,
                             'Keys are not equal')
        self.assertEqual(playbook.data['name'], h['name'], 'name not equal')
        self.assertEqual(playbook.data['playbook'], h['playbook'],
                         'playbook not equal')
Esempio n. 3
0
    def test_run_playbook(self):
        self.playbook = 'test-alpine-ssh'
        self.hosts = ['alpine']
        ips = '172.17.0.'

        response_list = DockerEngine().run_container_operation(
            operation='list', data={'all': False})

        if len(response_list) > 0:
            ips = ips + str(2 + len(response_list))
        else:
            ips = ips + '2'

        h = Host().insert({'name': self.hosts[0], 'ips': [ips]})

        self.assertEqual(h, True, 'Host not added')

        p = Playbook().insert({
            'name': self.playbook,
            'playbook': {
                'hosts':
                self.hosts[0],
                'remote_user':
                '******',
                'tasks': [{
                    'name': 'Test debug msg',
                    'debug': {
                        'msg': 'This works!'
                    }
                }]
            }
        })

        self.assertEqual(p, True, 'Playbook not added')

        container = DockerEngine().run_container_operation(
            operation='run',
            data={
                'image': 'sickp/alpine-sshd',
                'detach': True
            })

        self.assertNotEqual(container, False, 'Cointainer not running')
        self.container_id = container.short_id

        response = AnsibleEngine().run_playbook(hosts=self.hosts,
                                                playbook=self.playbook,
                                                passwords={
                                                    'conn_pass': '******',
                                                    'become_pass': '******'
                                                })

        self.assertNotEqual(response, False, 'Playbook did not run')
        self.assertNotEqual(response.find('PLAY [alpine]'), -1)

        c = DockerEngine().get_container_by_id(self.container_id)
        DockerEngine().run_operation_in_object(object=c,
                                               operation='stop',
                                               data={})
Esempio n. 4
0
    def test_find_all_hosts(self):
        Playbook().insert({
            'name': 'cool-playbook-1',
            'playbook': [
                {
                    'hosts': 'test',
                    'remote_user': '******',
                    'tasks': [
                        {
                            'name': 'test debug',
                            'debug': {
                                'msg': 'Debug msg'
                            }
                        }
                    ]
                }
            ]
        })

        Playbook().insert({
            'name': 'cool-playbook-2',
            'playbook': [
                {
                    'hosts': 'test',
                    'remote_user': '******',
                    'tasks': [
                        {
                            'name': 'test debug',
                            'debug': {
                                'msg': 'Debug msg'
                            }
                        }
                    ]
                }
            ]
        })

        playbooks = Playbook().find()

        self.assertIsInstance(playbooks.data, list,
                              'Hosts data is not a list')
        self.assertEqual(len(playbooks.data), 2,
                         'There are more than 2 playbooks')
Esempio n. 5
0
    def test_create_duplicated_host(self):
        pb = {
            'name': 'cool-playbook',
            'playbook': [
                {
                    'hosts': 'test',
                    'remote_user': '******',
                    'tasks': [
                        {
                            'name': 'test debug',
                            'debug': {
                                'msg': 'Debug msg'
                            }
                        }
                    ]
                }
            ]
        }

        Playbook().insert(pb)
        status = Playbook().insert(pb)

        self.assertEqual(status, False, 'Added duplicated playbook')
Esempio n. 6
0
    def test_create_playbook(self):
        status = Playbook().insert({
            'name': 'cool-playbook',
            'playbook': [
                {
                    'hosts': 'test',
                    'remote_user': '******',
                    'tasks': [
                        {
                            'name': 'test debug',
                            'debug': {
                                'msg': 'Debug msg'
                            }
                        }
                    ]
                }
            ]
        })

        self.assertEqual(status, True, 'Playbook not added')
    def test_run_playbook(self):
        ips = '172.17.0.'

        response_list = DockerEngine().run_container_operation(
            operation='list',
            data={
                'all': False
            }
        )

        if len(response_list) > 0:
            ips = ips + str(2 + len(response_list))
        else:
            ips = ips + '2'

        h = Host().insert({
            'name': self.hosts[0],
            'ips': ips
        })

        self.assertEqual(h, True, 'Host not added')

        p = Playbook().insert({
            'name': self.playbook,
            'playbook': {
                'hosts': self.hosts[0],
                'remote_user': '******',
                'tasks': [
                    {
                        'name': 'Test debug msg',
                        'debug': {
                            'msg': 'This works!'
                        }
                    }
                ]
            }
        })

        self.assertEqual(p, True, 'Playbook not added')

        container = DockerEngine().run_container_operation(
            operation='run',
            data={
                'image': 'sickp/alpine-sshd',
                'detach': True
            }
        )

        self.assertNotEqual(container, False, 'Cointainer not running')
        self.container_id = container.short_id

        # Run the playbook
        response = self.app.post(
            self.path,
            headers=self.headers,
            data=json.dumps({
                'hosts': self.hosts,
                'playbook': self.playbook,
                'passwords': {
                    'conn_pass': '******',
                    'become_pass': '******'
                }
            })
        )

        self.assertEqual(response.status_code, 200, 'Playbook failed running')
        self.assertNotEqual(json.loads(response.data)['result']
                            .find('PLAY [alpine]'), -1)

        # Stop the container
        c = DockerEngine().get_container_by_id(self.container_id)
        DockerEngine().run_operation_in_object(
            object=c,
            operation='stop',
            data={}
        )
Esempio n. 8
0
 def post():
     data = validate_or_abort(BasePlaybook, request.get_json())
     return response_by_success(Playbook().insert(data))
Esempio n. 9
0
 def post():
     data = validate_or_abort(QuerySchema, request.get_json())
     playbook = Playbook().find(
         criteria=data['query'],
         projection=data['filter'] if 'filter' in data.keys() else {})
     return parse_data(BasePlaybook, playbook.data)
Esempio n. 10
0
 def get(name):
     user = Playbook().find(criteria={'name': name})
     return parse_data(BasePlaybook, user.data)
Esempio n. 11
0
 def delete():
     data = validate_or_abort(ProvisionSchemaDelete, request.get_json())
     return response_by_success(
         Playbook().remove(criteria={'name': data['name']}), is_remove=True)
Esempio n. 12
0
 def put():
     data = validate_or_abort(ProvisionPlaybookPut, request.get_json())
     return response_by_success(Playbook().update(
         criteria={'name': data['name']}, data=data['data']))