def test_set_dotted2(self):
     data = {'a': {'b': 1}}
     attrs = NodeAttributes([data], write=data)
     attrs.set_dotted('a.c.d', 2)
     self.assertEqual(attrs['a']['c']['d'], 2)
     self.assertEqual(attrs.get_dotted('a.c.d'), 2)
     self.assertEqual(data['a']['c']['d'], 2)
 def test_get_dotted_keyerror(self):
     attrs = NodeAttributes([{'a': {'b': 1}}])
     with self.assertRaises(KeyError):
         attrs.get_dotted('a.b.c')
 def test_get_dotted(self):
     attrs = NodeAttributes([{'a': {'b': 1}}])
     self.assertEqual(attrs.get_dotted('a.b'), 1)
 def test_len2(self):
     attrs = NodeAttributes([{'a': {'b': 1, 'c': 2}}])
     self.assertEqual(len(attrs), 1)
     self.assertEqual(len(attrs['a']), 2)
 def test_len(self):
     attrs = NodeAttributes([{'a': 1, 'b': 2}])
     self.assertEqual(len(attrs), 2)
 def test_iter2(self):
     attrs = NodeAttributes([{'a': {'b': 1, 'c': 2}}])
     self.assertEqual(set(attrs['a']), set(['b', 'c']))
 def test_iter(self):
     attrs = NodeAttributes([{'a': 1, 'b': 2}])
     self.assertEqual(set(attrs), set(['a', 'b']))
 def test_setitem(self):
     data = {'a': 1}
     attrs = NodeAttributes([data], write=data)
     attrs['a'] = 2
     self.assertEqual(attrs['a'], 2)
     self.assertEqual(data['a'], 2)
 def test_get_default(self):
     attrs = NodeAttributes([{'a': 1}])
     self.assertEqual(attrs.get('b'), None)
 def test_get(self):
     attrs = NodeAttributes([{'a': 1}])
     self.assertEqual(attrs.get('a'), 1)
 def test_read_only(self):
     attrs = NodeAttributes([{'a': 1}])
     with self.assertRaises(ChefError):
         attrs['a'] = 2
 def test_search_path_nested(self):
     data1 = {'a': {'b': 1}}
     data2 = {'a': {'b': 2}}
     attrs = NodeAttributes([data1, data2])
     self.assertEqual(attrs['a']['b'], 1)
 def test_search_path(self):
     attrs = NodeAttributes([{'a': 1}, {'a': 2}])
     self.assertEqual(attrs['a'], 1)
 def test_set_nested(self):
     data = {'a': {'b': 1}}
     attrs = NodeAttributes([data], write=data)
     attrs['a']['b'] = 2
     self.assertEqual(attrs['a']['b'], 2)
     self.assertEqual(data['a']['b'], 2)
 def test_getitem_nested(self):
      attrs = NodeAttributes([{'a': {'b': 1}}])
      self.assertEqual(attrs['a']['b'], 1)
 def test_getitem(self):
     attrs = NodeAttributes([{'a': 1}])
     self.assertEqual(attrs['a'], 1)
 def test_getitem_keyerror(self):
     attrs = NodeAttributes([{'a': 1}])
     with self.assertRaises(KeyError):
         attrs['b']
Exemplo n.º 18
0
    def command(self):
        db = self.pyramid.db
        ou = db.nodes.find_one({'_id': ObjectId(self.options.ou_id)})
        if not ou:
            print 'Error OU does not exists'
            return
        comp = db.nodes.find_one({'_id': ObjectId(self.options.comp_id)})
        if not comp:
            print 'Error computer does not exists'
            return
        node_id = comp.get('node_chef_id', None)
        if not comp:
            print 'Error this computer has not node_chef_id'
            return
        policies = comp.get('policies', None)
        if policies != {}:
            print 'Error this computer should not have any policies'
            return
        admin = db.adminusers.find_one({'username': self.options.gcc_username})
        if not admin:
            print 'Error this admin does not exists'
            return
        elif not admin.get('is_superuser', None):
            print 'You need a super admin'
            return
        number_nodes = int(self.options.number)
        api = get_chef_api(self.settings,
                           admin)
        node = ChefNode(node_id, api)
        for i in range(number_nodes):
            new_node_name = '%s-%s' % (self.options.prefix, i)
            new_node = ChefNode(new_node_name, api)
            for attr in node.to_dict().keys():
                if hasattr(node, attr) and attr != 'name':
                    if attr == 'automatic':
                        automatic_dict = node.automatic.to_dict()
                        automatic_dict['ohai_gecos']['pclabel'] = new_node_name
                        user1 = 'user.name-%s-1' % new_node_name
                        user2 = 'user.name-%s-2' % new_node_name
                        automatic_dict['ohai_gecos']['users'] = [{'username': user1,
                                                                  'home': '/home/%s' % user1,
                                                                  'gid': 1000,
                                                                  'sudo': False,
                                                                  'uid': 1000},
                                                                 {'username': user2,
                                                                  'home': '/home/%s' % user2,
                                                                  'gid': 1000,
                                                                  'sudo': False,
                                                                  'uid': 1001}]

                        automatic = NodeAttributes(automatic_dict)
                        setattr(new_node, attr, automatic)
                    elif attr == 'normal':
                        node.normal.set_dotted('ohai_gecos', {})
                    else:
                        setattr(new_node, attr, getattr(node, attr))
            new_node.save()
            print 'Created %s at chef' % new_node_name
            res = requests.post('%s/register/computer/' % self.options.gcc_url,
                                {'ou_id': self.options.ou_id, 'node_id': new_node_name},
                                auth=(self.options.gcc_username, self.options.gcc_password))
            if res.ok and res.json()['ok']:
                print 'Created %s at gcc' % new_node_name
            elif res.ok and not res.json()['ok']:
                print 'Error %s at gcc' % new_node_name
                print '\t %s' % res.json()['message']
            else:
                print 'Unknow error %s at gcc' % new_node_name

            res = requests.put('%s/chef/status/' % self.options.gcc_url,
                               {'node_id': new_node_name,
                                'gcc_username': self.options.gcc_username})

            if res.ok and res.json()['ok']:
                print 'Chef client %s' % new_node_name
            elif res.ok and not res.json()['ok']:
                print 'Error %s at chef client' % new_node_name
                print '\t %s' % res.json()['message']
            else:
                print 'Unknow error %s at chef client' % new_node_name

        waiting_to_celery(db)