def from_chef_environment(cls, environment): """ Rebuilds a Deployment given a chef environment :param environment: name of environment :type environment: string :rtype: Chef """ local_api = autoconfigure() env = ChefEnvironment(environment, api=local_api) override = env.override_attributes default = env.default_attributes chef_auth = override.get('remote_chef', None) remote_api = None if chef_auth and chef_auth["key"]: remote_api = ChefServer._remote_chef_api(chef_auth) renv = ChefEnvironment(environment, api=remote_api) override = renv.override_attributes default = renv.default_attributes environment = MonsterChefEnvironment(env.name, local_api, description=env.name, default=default, override=override, remote_api=remote_api) name = env.name deployment_args = override.get('deployment', {}) features = deployment_args.get('features', {}) os_name = deployment_args.get('os_name', None) branch = deployment_args.get('branch', None) status = deployment_args.get('status', "provisioning") product = deployment_args.get('product', None) provisioner_name = deployment_args.get('provisioner', "razor") provisioner = get_provisioner(provisioner_name) deployment = cls.deployment_config(features, name, os_name, branch, environment, provisioner, status, product=product) nodes = deployment_args.get('nodes', []) for node in (ChefNode(n, local_api) for n in nodes): if not node.exists: util.logger.error("Non existant chef node:{0}".format( node.name)) continue cnode = MonsterChefNode.from_chef_node(node, deployment_args['os_name'], product, environment, deployment, provisioner, deployment_args['branch']) deployment.nodes.append(cnode) return deployment
def fromfile(cls, name, template_name, branch, provisioner, template_file, template_path=None): """ Returns a new deployment given a deployment template at path :param name: name for the deployment :type name: string :param name: name of template to use :type name: string :param branch: branch of the RCBOPS chef cookbook repo to use :type branch:: string :param provisioner: provisioner to use for nodes :type provisioner: Provisioner :param path: path to template :type path: string :rtype: Chef """ local_api = autoconfigure() if ChefEnvironment(name, api=local_api).exists: # Use previous dry build if exists util.logger.info("Using previous deployment:{0}".format(name)) return cls.from_chef_environment(name) if not template_path: path = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, 'templates/{0}.yaml'.format( template_file)) else: path = template_path template = Config(path)[template_name] environment = MonsterChefEnvironment(name, local_api, description=name) os_name = template['os'] product = template['product'] deployment = cls.deployment_config(template['features'], name, os_name, branch, environment, provisioner, product=product) # provision nodes chef_nodes = provisioner.provision(template, deployment) for node in chef_nodes: cnode = MonsterChefNode.from_chef_node(node, os_name, product, environment, deployment, provisioner, branch) provisioner.post_provision(cnode) deployment.nodes.append(cnode) # add features for node, features in zip(deployment.nodes, template['nodes']): node.add_features(features) return deployment
def destroy(self): """ Destroys Chef Deployment """ self.status = "Destroying" # Nullify remote api so attributes are not sent remotely self.environment.remote_api = None super(Chef, self).destroy() # Destroy rogue nodes if not self.nodes: nodes = Razor.node_search("chef_environment:{0}".format(self.name), tries=1) for n in nodes: MonsterChefNode.from_chef_node(n, environment=self.environment).\ destroy() # Destroy Chef environment self.environment.destroy() self.status = "Destroyed"
def from_chef_environment(cls, environment): """ Rebuilds a Deployment given a chef environment :param environment: name of environment :type environment: string :rtype: Chef """ local_api = autoconfigure() environ = ChefEnvironment(environment, api=local_api) if not environ.exists: util.logger.error("The specified environment, {0}, does not" "exist.".format(environment)) exit(1) override = environ.override_attributes default = environ.default_attributes chef_auth = override.get('remote_chef', None) remote_api = None if chef_auth and chef_auth["key"]: remote_api = ChefServer._remote_chef_api(chef_auth) renv = ChefEnvironment(environment, api=remote_api) override = renv.override_attributes default = renv.default_attributes environment = MonsterChefEnvironment( environ.name, local_api, description=environ.name, default=default, override=override, remote_api=remote_api) name = environ.name deployment_args = override.get('deployment', {}) features = deployment_args.get('features', {}) os_name = deployment_args.get('os_name', None) branch = deployment_args.get('branch', None) status = deployment_args.get('status', "provisioning") product = deployment_args.get('product', None) provisioner_name = deployment_args.get('provisioner', "razor2") provisioner = get_provisioner(provisioner_name) deployment = cls.deployment_config(features, name, os_name, branch, environment, provisioner, status, product=product) nodes = deployment_args.get('nodes', []) for node in (ChefNode(n, local_api) for n in nodes): if not node.exists: util.logger.error("Non existant chef node:{0}". format(node.name)) continue cnode = MonsterChefNode.from_chef_node(node, product, environment, deployment, provisioner, deployment_args['branch']) deployment.nodes.append(cnode) return deployment
def destroy(self): """ Destroys Chef Deployment """ self.status = "Destroying" # Nullify remote api so attributes are not sent remotely self.environment.remote_api = None super(Chef, self).destroy() # Destroy rogue nodes if not self.nodes: nodes = Razor.node_search("chef_environment:{0}". format(self.name), tries=1) for n in nodes: MonsterChefNode.from_chef_node(n, environment=self.environment).\ destroy() # Destroy Chef environment self.environment.destroy() self.status = "Destroyed"
def fromfile(cls, name, template_name, branch, provisioner, template_file, template_path=None): """ Returns a new deployment given a deployment template at path :param name: name for the deployment :type name: string :param name: name of template to use :type name: string :param branch: branch of the RCBOPS chef cookbook repo to use :type branch:: string :param provisioner: provisioner to use for nodes :type provisioner: Provisioner :param path: path to template :type path: string :rtype: Chef """ local_api = autoconfigure() if ChefEnvironment(name, api=local_api).exists: # Use previous dry build if exists util.logger.info("Using previous deployment:{0}".format(name)) return cls.from_chef_environment(name) if not template_path: path = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, 'templates/{0}.yaml'.format(template_file)) else: path = template_path template = Config(path)[template_name] environment = MonsterChefEnvironment(name, local_api, description=name) os_name = template['os'] product = template['product'] deployment = cls.deployment_config(template['features'], name, os_name, branch, environment, provisioner, product=product) # provision nodes chef_nodes = provisioner.provision(template, deployment) for node in chef_nodes: cnode = MonsterChefNode.from_chef_node(node, os_name, product, environment, deployment, provisioner, branch) provisioner.post_provision(cnode) deployment.nodes.append(cnode) # add features for node, features in zip(deployment.nodes, template['nodes']): node.add_features(features) return deployment
def fromfile(cls, name, template, branch, provisioner, template_path): """ Returns a new deployment given a deployment template at path :param name: name for the deployment :type name: string :param name: name of template to use :type name: string :param branch: branch of the RCBOPS chef cookbook repo to use :type branch:: string :param provisioner: provisioner to use for nodes :type provisioner: Provisioner :param path: path to template :type path: string :rtype: Chef """ local_api = autoconfigure() template_file = "" if branch == "master": template_file = "default" else: template_file = branch.lstrip('v') if "rc" in template_file: template_file = template_file.rstrip("rc") template_file = template_file.replace('.', '_') if ChefEnvironment(name, api=local_api).exists: # Use previous dry build if exists util.logger.info("Using previous deployment:{0}".format(name)) return cls.from_chef_environment(name) path = "" if not template_path: path = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, 'templates/{0}.yaml'.format( template_file)) else: path = template_path try: template = Config(path)[template] except KeyError: util.logger.critical("Looking for the template {0} in the file: " "\n{1}\n The key was not found!" .format(template, path)) sys.exit(1) environment = MonsterChefEnvironment(name, local_api, description=name) os_name = template['os'] product = template['product'] deployment = cls.deployment_config(template['features'], name, os_name, branch, environment, provisioner, product=product) # provision nodes chef_nodes = provisioner.provision(template, deployment) # threads = [] # from time import sleep for node in chef_nodes: # cnode = MonsterChefNode.from_chef_node(node, product, environment, # deployment, provisioner, # branch) # deployment.nodes.append(cnode) # tx = Thread(target=cls.provision_nodes, # args=(provisioner, cnode, )) # threads.append(tx) # tx.start() # sleep(2) cnode = MonsterChefNode.from_chef_node(node, product, environment, deployment, provisioner, branch) provisioner.post_provision(cnode) deployment.nodes.append(cnode) # for tx in threads: # tx.join() # add features for node, features in zip(deployment.nodes, template['nodes']): node.add_features(features) return deployment