def fillImportTable(cur, conf): print("Filling import table...") api = ChefAPI(conf["chefURL"], conf["chefKey"], conf["chefUser"], ssl_verify=conf["chefSSLVerify"]) nodes = api.request("GET", "/organizations/aim/nodes").json() for node in nodes: r = row(api, node, conf) insertRow(cur, r, conf) print("Filling import table...Done.")
def updateUserDatabagItem(itemName, itemObject): with ChefAPI("https://chef.5gdcs.com/sestra-pcs", opts.key, opts.client, ssl_verify=False): dbi = DataBagItem('users', itemName) dbi.update(itemObject)
def post(self): node_id = self.request.POST.get('node_id') if node_id is None: return {'ok': False, 'message': 'Missing node ID'} settings = get_current_registry().settings api = get_chef_api(settings, self.request.user) # create chef client chef_client = ChefClient(node_id, api) if chef_client.exists: return {'ok': False, 'message': 'This client already exists'} chef_client = ChefClient.create(node_id, api) # Prepare the API for this client chef_url = settings.get('chef.url') chef_version = settings.get('chef.version') chef_ssl_verify = settings.get('chef.ssl.verify') if chef_ssl_verify == 'False' or chef_ssl_verify == 'True': chef_ssl_verify = bool(chef_ssl_verify) api = ChefAPI(chef_url, chef_client.private_key.encode(), node_id, chef_version, ssl_verify = False) # create chef node chef_node = ChefNode(node_id, api) if chef_node.exists: return {'ok': False, 'message': 'This node already exists'} chef_node.save() return {'ok': True, 'message': 'Node and client have been added', 'client_private_key': chef_client.private_key}
def opencenter_chef(opencenter_endpoint): ep = opencenter_endpoint filter = 'facts.chef_server_uri != None and facts.chef_server_pem != None' chef_node = ep.nodes.filter(filter).first() pem = chef_node['facts']['chef_server_client_pem'] key = rsa.Key(StringIO.StringIO(pem)) url = chef_node['facts']['chef_server_uri'] name = chef_node['facts']['chef_server_client_name'] return ChefAPI(url, key, name)
def _initialize_chef_api(self): try: return ChefAPI(self.config['chef_server_url'], self.config['client_key_file'], self.config['client_name']) except: self.logger.exception('Failed to open config file: %s', self.config['client_key_file']) return None
def init(self, edda_client, config, status, instance_enricher): self.edda_client = edda_client try: self.api = ChefAPI(config['chef_server_url'], config['client_key_file'], config['client_name']) except IOError: self.logger.exception('Failed to open config file: %s', config['client_key_file']) self.api = None self.excluded_instances = config.get('excluded_instances', []) self.initialize_status(status) self.instance_enricher = instance_enricher
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)
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
def _remote_chef_api(self, chef_api_dict): """ Builds a remote chef API object """ return ChefAPI(**chef_api_dict)
def _get_chef_api(chef_url, username, chef_pem): if not os.path.exists(chef_pem): raise ChefError('User has no pem to access chef server') api = ChefAPI(chef_url, chef_pem, username) return api
pemFile = '' if os.path.exists('platzScript0.pem'): pemFile = 'platzScript0.pem' elif os.path.exists('chefPlatz.pem'): pemFile = 'chefPlatz.pem' else: print '' print '*** ERROR ***' print 'No certificate file could be found!' print '' exit() elbs = boto.connect_elb( aws_access_key_id='AKIAIO6ZJBIIOKEYEGVQ', aws_secret_access_key='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') chef = ChefAPI('https://chef-server.lockerz.us:8080', pemFile, 'platzScript0') def get_elbs(): return elbs.get_all_load_balancers() def getNodesByRole(role): return Search("node", "roles:%s" % (role)) def getAppServerNodes(): return getNodesByRole("platz_apps") def printAppServerNodes():
required=False, help="client for chef") parser.add_argument('--chef_client_pem', action="store", dest="chef_client_pem", default="~/.chef/jenkins.pem", required=False, help="client pem for chef") # Save the parsed arguments results = parser.parse_args() results.chef_client_pem = results.chef_client_pem.replace( '~', os.getenv("HOME")) # Load chef and razor apis with ChefAPI(results.chef_url, results.chef_client_pem, results.chef_client): razor = razor_api(results.razor_ip) 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))