Example #1
0
def opencenter_endpoint(chef, name='test', os='ubuntu'):
    validate_environment(chef, name=name, os=os)
    env = env_format % (name, os)
    query = "in_use:\"server\" AND chef_environment:%s" % env
    server = next(Node(node['name']) for node in Search('node').query(query))
    ep_url = "https://%s:8443" % server['ipaddress']
    return OpenCenterEndpoint(ep_url, user="******", password="******")
Example #2
0
 def destroy_node(self, node):
     """
     Destroys a node provisioned by razor
     :param node: Node to destroy
     :type node: ChefNode
     """
     cnode = Node(node.name, node.environment.local_api)
     in_use = node['in_use']
     if in_use == "provisioning" or in_use == 0:
         # Return to pool if the node is clean
         cnode['in_use'] = 0
         cnode['archive'] = {}
         cnode.chef_environment = "_default"
         cnode.save()
     else:
         # Remove active model if the node is dirty
         active_model = cnode['razor_metadata']['razor_active_model_uuid']
         try:
             if node.feature_in('controller'):
                 # rabbit can cause the node to not actually reboot
                 kill = ("for i in `ps -U rabbitmq | tail -n +2 | "
                         "awk '{print $1}' `; do kill -9 $i; done")
                 node.run_cmd(kill)
             node.run_cmd("shutdown -r now")
             self.api.remove_active_model(active_model)
             Client(node.name).delete()
             cnode.delete()
             sleep(15)
         except:
             util.logger.error("Node unreachable. "
                               "Manual restart required:{0}".format(
                                   str(node)))
Example #3
0
class MockSearchTestCase(ChefTestCase):
    @mockSearch({('node', '*:*'): [Node('fake_1', skip_load=True).to_dict()]})
    def test_single_node(self, MockSearch):
        import chef.search
        s = chef.search.Search('node')
        self.assertEqual(len(s), 1)
        self.assertIn('fake_1', s)
Example #4
0
 def chef_instance(self, deployment, name, flavor="2GBP"):
     """
     Builds an instance with desired specs and inits it with chef
     :param client: compute client object
     :type client: novaclient.client.Client
     :param deployment: deployement to add to
     :type deployment: ChefDeployment
     :param name: name for instance
     :type name: string
     :param flavor: desired flavor for node
     :type flavor: string
     :rtype: ChefNode
     """
     image = deployment.os_name
     server, password = self.build_instance(name=name, image=image,
                                            flavor=flavor)
     run_list = ",".join(util.config[str(self)]['run_list'])
     run_list_arg = ""
     if run_list:
         run_list_arg = "-r {0}".format(run_list)
     command = 'knife bootstrap {0} -u root -P {1} -N {2} {3}'.format(
         server.accessIPv4, password, name, run_list_arg)
     run_cmd(command)
     node = Node(name, api=deployment.environment.local_api)
     node.chef_environment = deployment.environment.name
     node['in_use'] = "provisioning"
     node['ipaddress'] = server.accessIPv4
     node['password'] = password
     node['uuid'] = server.id
     node['current_user'] = "******"
     node.save()
     return node
Example #5
0
 def computer_deleted(self, user, obj, computers=None):
     node_chef_id = obj.get('node_chef_id', None)
     if node_chef_id:
         api = get_chef_api(self.app.conf, user)
         node = Node(node_chef_id, api)
         node.delete()
         client = Client(node_chef_id, api=api)
         client.delete()
     self.log_action('deleted', 'Computer', obj)
    def test_create(self):
        name = self.random()
        node = Node.create(name, run_list=['recipe[foo]'])
        self.register(node)
        self.assertEqual(node.run_list, ['recipe[foo]'])

        node2 = Node(name)
        self.assertTrue(node2.exists)
        self.assertEqual(node2.run_list, ['recipe[foo]'])
def handle(event, _context):
    """Lambda Handler"""
    log_event(event)
    node_name = None
    node_ip = None

    # Remove from one of the chef servers
    for URL in CHEF_SERVER_URLS:
        with ChefAPI(URL, CHEF_PEM, CHEF_USERNAME):
            instance_id = get_instance_id(event)
            try:
                search = Search('node', 'ec2_instance_id:' + instance_id)
            except ChefServerNotFoundError as err:
                LOGGER.error(err)
                return False

            if len(search) != 0:
                for instance in search:
                    node_name = instance.object.name
                    node = Node(node_name)
                    node_ip = node['ipaddress']
                    client = Client(node_name)

                    try:
                        node.delete()
                        client.delete()
                        LOGGER.info(
                            '=====SUCCESSFULLY REMOVED INSTANCE FROM CHEF SERVER===== {}'
                            .format(URL))
                        break
                    except ChefServerNotFoundError as err:
                        LOGGER.error(err)
                        return False
            else:
                LOGGER.info(
                    '===Instance does not appear to be Chef Server managed.=== {}'
                    .format(URL))

    # Remove from Spacewalk
    spacewalk_cleanup(node_ip)

    # Remove from DNS
    dns_cleanup(node_name)

    # Remove from AD
    active_directory_cleanup(node_name)

    # Remove fom Solarwinds
    solarwinds_cleanup(node_ip, node_name)

    # Remove from Chef Automate
    chef_automate_cleanup(node_name)
Example #8
0
 def destroy_node(self, node):
     """
     Destroys chef node from openstack
     :param node: node to destroy
     :type node: ChefNode
     """
     cnode = Node(node.name, node.environment.local_api)
     if cnode.exists:
         self.compute_client.servers.get(node['uuid']).delete()
         cnode.delete()
     client = Client(node.name, node.environment.local_api)
     if client.exists:
         client.delete()
Example #9
0
def openstack_endpoints(opencenter_endpoint):
    ep = opencenter_endpoint
    infrastructure_nodes = ep.nodes.filter('name = "Infrastructure"')
    for node_id in infrastructure_nodes.keys():
        ha = infrastructure_nodes[node_id].facts["ha_infra"]
        endpoint = None
        if ha:
            endpoint = infrastructure_nodes[node_id].facts["nova_api_vip"]
        else:
            name = next(node.name for node in ep.nodes
                        if "nova-controller" in node.facts["backends"])
            endpoint = Node(name)['ipaddress']
        return endpoint
Example #10
0
    def put(self):
        node_id = self.request.POST.get('node_id')
        username = self.request.POST.get('gcc_username')
        if not node_id:
            return {'ok': False,
                    'message': 'Please set a node id (node_id)'}
        if not username:
            return {'ok': False,
                    'message': 'Please set a admin username (gcc_username)'}
        self.request.user = self.request.db.adminusers.find_one({'username': username})
        if not self.request.user:
            return {'ok': False,
                    'message': 'The admin user %s does not exists' % username}
        settings = get_current_registry().settings
        api = get_chef_api(settings, self.request.user)
        node = Node(node_id, api)
        job_status = node.attributes.get('job_status')
        reserve_node = False
        if job_status:
            node = reserve_node_or_raise(node_id, api, 'gcc-chef-status-%s' % random.random(), attempts=3)
            reserve_node = True
            chef_client_error = False

            for job_id, job_status in job_status.to_dict().items():
                job = self.collection.find_one({'_id': ObjectId(job_id)})
                if not job:
                    continue
                if job_status['status'] == 0:
                    self.collection.update({'_id': job['_id']},
                                           {'$set': {'status': 'finished',
                                                     'last_update': datetime.datetime.utcnow()}})
                else:
                    chef_client_error = True
                    self.collection.update({'_id': job['_id']},
                                           {'$set': {'status': 'errors',
                                                     'message': job_status.get('message', 'Error'),
                                                     'last_update': datetime.datetime.utcnow()}})
            self.request.db.nodes.update({'node_chef_id': node_id}, {'$set': {'error_last_chef_client': chef_client_error}})
            invalidate_jobs(self.request)
            node.attributes.set_dotted('job_status', {})

        users_old = self.get_attr(node, USERS_OLD)
        users = self.get_attr(node, USERS_OHAI)
        if not users_old or users_old != users:
            if not reserve_node:
                node = reserve_node_or_raise(node_id, api, 'gcc-chef-status-%s' % random.random(), attempts=3)
            return self.check_users(node)
        if job_status:
            save_node_and_free(node)
        return {'ok': True}
 def get_node_information(self, node_name):
     """
     Get node attributes(metadata) using Node.attributes of PyChef
     Store the values of the attributes selected by the user for each node
     """
     chefUniqueId = self.organization + "_" + node_name
     node_details = Node(node_name)
     node_information = {}
     node_information['chefUniqueId'] = chefUniqueId
     node_information['chef_environment'] = node_details.chef_environment
     for attribute in self.config:
         attribute_value = self.get_attribute_value(attribute, node_details)
         if attribute_value:
             attribute = self.adjust_attribute_name(attribute)
             node_information[attribute] = attribute_value
     self.nodes_metadata.append(node_information)
Example #12
0
 def _clean_node(self, hostid, config, target_system, **kwargs):
     """clean node"""
     from chef import Node
     try:
         node = Node(self._get_node_name(config['hostname'],
                                         config['clusterid'],
                                         target_system),
                     api=self.api_)
         node.delete()
         logging.debug(
             'node is removed for host %s '
             'config %s target_system %s', hostid, config, target_system)
     except Exception as error:
         logging.debug(
             'no node to delete for host %s '
             'config %s target_system %s', hostid, config, target_system)
Example #13
0
    def fqdn_for_node(self, node):
        self.node = node
        api = autoconfigure()
        n = Node(node)
        if n:
            try:
                self.fqdn = n.attributes['ec2']['public_hostname']
            except KeyError:
                if 'fqdn' in n:
                    self.fqdn = n['fqdn']
                else:
                    return None
        else:
            return None

        return self.fqdn
Example #14
0
 def command(self):
     api = _get_chef_api(self.settings.get('chef.url'),
                         toChefUsername(self.options.chef_username),
                         self.options.chef_pem, self.settings.get('chef.version'))
                         
     print('INFO: Update IPv4 address START!') 
     db = self.pyramid.db
     computers = db.nodes.find({'type': 'computer'})
     for comp in computers:
         node_id = comp.get('node_chef_id', None)
         node = Node(node_id, api)
         ipaddress = node.attributes.get('ipaddress')
         
         print('INFO: Update node: %s, set IP: %s'%(node_id, ipaddress)) 
         db.nodes.update({'node_chef_id':node_id},{'$set': {'ipaddress':ipaddress}})
     
     print('INFO: Update IPv4 address END!') 
Example #15
0
    def get_machine_name(self):
        """
        Get the human readable machine name using the machine type and
        searching Knife for similar servers
        """
        # This must be called to get the correct Knife Setup for Node()
        # Even if we don't use the resulting API object directly
        api = ChefAPI.from_config_file(self.KNIFE_CONFIG)
        if self.VPC:
            base_name = "yip_%s%%s" % self.machine_type
        else:
            base_name = "yipit_%s%%s" % self.machine_type

        index = 1
        while True:
            name = base_name % index
            node = Node(name)
            if node.exists:
                index += 1
            else:
                break

        return name
chef_node_names = map(lambda chef_node: chef_node.object.name, chef_nodes)

def is_ec2_node_defined_in_region(ec2_instance_id_to_be_found, ec2_instance_ids):
    is_ec2_node_defined = False
    for ec2_instance_id in ec2_instance_ids:
        if(ec2_instance_id == ec2_instance_id_to_be_found):
            is_ec2_node_defined = True
    return is_ec2_node_defined

if (len(chef_nodes) == 0):
    print("No chef nodes registered... doing nothing")
else:
    print("{0} chef nodes found".format(len(chef_nodes)))
    for chef_node_row in chef_nodes:
        chef_node_name = chef_node_row.object.name
        chef_node = Node(chef_node_name)
        if chef_node.has_key('ec2'):
            chef_node_ec2_instance_id = chef_node['ec2']['instance_id']
            if(is_ec2_node_defined_in_region(chef_node_ec2_instance_id, ec2_alive_instance_ids)):
                print "chef node: {0} {1} exists as a live EC2 node: NO DELETION IN CHEF org".format(chef_node_name,chef_node_ec2_instance_id)
            else:
                print "chef node: {0} {1} exists in chef org, but not in ec2, hence WILL BE DELETED FROM CHEF org".format(chef_node_name,chef_node_ec2_instance_id)
                if(args.dry_run):
                    print "chef node: {0} would have been deleted from {0} -- avoid --dry-run option to delete it".format(api.url)
                else:
                    chef_node.delete()
                    print "chef node: {0} HAS BEEN DELETED IN CHEF org".format(chef_node_name)



Example #17
0
        for compute in computes:
            compute_ip = rpcsqa.set_node_in_use(compute, "agent")
            rpcsqa.remove_chef(compute)
            rpcsqa.install_opencenter(compute, results.repo, 'agent',
                                      oc_server_ip)

        # Print Cluster Info
        print "************************************************************"
        print "2 VMs, 1 controller ( VM Host ), %i Agents" % len(computes)
        print "OpenCenter Server (VM) with IP: %s on Host: %s" % (oc_server_ip,
                                                                  controller)
        print "Chef Server (VM) with IP: %s on Host: %s" % (chef_server_ip,
                                                            controller)
        print "Controller Node: %s with IP: %s" % (controller, controller_ip)
        for agent in computes:
            node = Node(agent)
            print "Agent Node: %s with IP: %s" % (agent, node['ipaddress'])
        print "************************************************************"

    else:
        #Pick an opencenter server, and rest for agents
        server = opencenter_list[0]
        dashboard = []
        clients = []
        if len(opencenter_list) > 1:
            dashboard = opencenter_list[1]
        if len(opencenter_list) > 2:
            agents = opencenter_list[2:]

        #Remove chef client...install opencenter server
        print "Making %s the server node" % server
Example #18
0
    server = []
    dashboard = []
    agents = []

    # Make sure environment exists, if not create one
    env = "%s-%s-opencenter" % (results.name, results.os)
    if not Search("environment").query("name:%s" % env):
        print "Making environment: %s " % env
        Environment.create(env)

    # Gather the servers in the environment into their roles
    nodes = Search('node').query("name:qa-%s-pool* AND chef_environment:%s" %
                                 (results.os, env))
    for n in nodes:
        node = Node(n['name'])
        #print "Found: %s " % node.name
        #print node.attributes['in_use']
        if node.attributes['in_use'] == "server":
            server.append(node.name)
        elif node.attributes['in_use'] == "dashboard":
            dashboard.append(node.name)
        elif node.attributes['in_use'] == "agent":
            agents.append(node.name)
    print "Server: %s " % server
    print "Dashboard: %s " % dashboard
    print "Agents: %s " % agents

    # Make sure we have the servers we need
    if not dashboard or not server:
        print "No dashboard/server found"
 def setUp(self):
     super(NodeTestCase, self).setUp()
     self.node = Node('test_1')
Example #20
0
#!/usr/bin/env python
#
# requires
#   - pip install PyChef
#   - access to knife.rb file
#   - access to client.pem file
#
# DON'T RUN THIS - THESE ARE JUST EXAMPLES

#------------------------------
# update node info
from chef import autoconfigure, Node

api = autoconfigure()
n = Node('web1')
print n['fqdn']
n['myapp']['version'] = '1.0'
n.save()
#------------------------------

#------------------------------
# get client list
from chef import autoconfigure

api = autoconfigure()
print api.api_request('GET', '/clients')
#------------------------------