How to use cmd_config_validate method in localstack

Best Python code snippet using localstack_python

nodepoolcmd.py

Source:nodepoolcmd.py Github

copy

Full Screen

1#!/usr/bin/env python2#3# Copyright 2013 OpenStack Foundation4#5# Licensed under the Apache License, Version 2.0 (the "License"); you may6# not use this file except in compliance with the License. You may obtain7# a copy of the License at8#9# http://www.apache.org/licenses/LICENSE-2.010#11# Unless required by applicable law or agreed to in writing, software12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the14# License for the specific language governing permissions and limitations15# under the License.16import logging.config17import sys18from prettytable import PrettyTable19from nodepool import launcher20from nodepool import provider_manager21from nodepool import status22from nodepool import zk23from nodepool.cmd import NodepoolApp24from nodepool.cmd.config_validator import ConfigValidator25log = logging.getLogger(__name__)26class NodePoolCmd(NodepoolApp):27 def create_parser(self):28 parser = super(NodePoolCmd, self).create_parser()29 parser.add_argument('-c', dest='config',30 default='/etc/nodepool/nodepool.yaml',31 help='path to config file')32 parser.add_argument('-s', dest='secure',33 help='path to secure file')34 parser.add_argument('--debug', dest='debug', action='store_true',35 help='show DEBUG level logging')36 subparsers = parser.add_subparsers(title='commands',37 description='valid commands',38 dest='command',39 help='additional help')40 cmd_list = subparsers.add_parser('list', help='list nodes')41 cmd_list.set_defaults(func=self.list)42 cmd_list.add_argument('--detail', action='store_true',43 help='Output detailed node info')44 cmd_image_list = subparsers.add_parser(45 'image-list', help='list images from providers')46 cmd_image_list.set_defaults(func=self.image_list)47 cmd_dib_image_list = subparsers.add_parser(48 'dib-image-list',49 help='list images built with diskimage-builder')50 cmd_dib_image_list.set_defaults(func=self.dib_image_list)51 cmd_image_build = subparsers.add_parser(52 'image-build',53 help='build image using diskimage-builder')54 cmd_image_build.add_argument('image', help='image name')55 cmd_image_build.set_defaults(func=self.image_build)56 cmd_alien_image_list = subparsers.add_parser(57 'alien-image-list',58 help='list images not accounted for by nodepool')59 cmd_alien_image_list.set_defaults(func=self.alien_image_list)60 cmd_alien_image_list.add_argument('provider', help='provider name',61 nargs='?')62 cmd_delete = subparsers.add_parser(63 'delete',64 help='place a node in the DELETE state')65 cmd_delete.set_defaults(func=self.delete)66 cmd_delete.add_argument('id', help='node id')67 cmd_delete.add_argument('--now',68 action='store_true',69 help='delete the node in the foreground')70 cmd_image_delete = subparsers.add_parser(71 'image-delete',72 help='delete an image')73 cmd_image_delete.set_defaults(func=self.image_delete)74 cmd_image_delete.add_argument('--provider', help='provider name',75 required=True)76 cmd_image_delete.add_argument('--image', help='image name',77 required=True)78 cmd_image_delete.add_argument('--upload-id', help='image upload id',79 required=True)80 cmd_image_delete.add_argument('--build-id', help='image build id',81 required=True)82 cmd_dib_image_delete = subparsers.add_parser(83 'dib-image-delete',84 help='Delete a dib built image from disk along with all cloud '85 'uploads of this image')86 cmd_dib_image_delete.set_defaults(func=self.dib_image_delete)87 cmd_dib_image_delete.add_argument('id', help='dib image id')88 cmd_config_validate = subparsers.add_parser(89 'config-validate',90 help='Validate configuration file')91 cmd_config_validate.set_defaults(func=self.config_validate)92 cmd_request_list = subparsers.add_parser(93 'request-list',94 help='list the current node requests')95 cmd_request_list.set_defaults(func=self.request_list)96 cmd_info = subparsers.add_parser(97 'info',98 help='Show provider data from zookeeper')99 cmd_info.add_argument(100 'provider',101 help='Provider name',102 metavar='PROVIDER')103 cmd_info.set_defaults(func=self.info)104 cmd_erase = subparsers.add_parser(105 'erase',106 help='Erase provider data from zookeeper')107 cmd_erase.add_argument(108 'provider',109 help='Provider name',110 metavar='PROVIDER')111 cmd_erase.add_argument(112 '--force',113 help='Bypass the warning prompt',114 action='store_true')115 cmd_erase.set_defaults(func=self.erase)116 return parser117 def setup_logging(self):118 # NOTE(jamielennox): This should just be the same as other apps119 if self.args.debug:120 m = '%(asctime)s %(levelname)s %(name)s: %(message)s'121 logging.basicConfig(level=logging.DEBUG, format=m)122 elif self.args.logconfig:123 super(NodePoolCmd, self).setup_logging()124 else:125 m = '%(asctime)s %(levelname)s %(name)s: %(message)s'126 logging.basicConfig(level=logging.INFO, format=m)127 l = logging.getLogger('kazoo')128 l.setLevel(logging.WARNING)129 def list(self, node_id=None, detail=False):130 if hasattr(self.args, 'detail'):131 detail = self.args.detail132 fields = ['id', 'provider', 'label', 'server_id',133 'public_ipv4', 'ipv6', 'state', 'age', 'locked']134 if detail:135 fields.extend(['pool', 'hostname', 'private_ipv4', 'AZ',136 'connection_port', 'launcher',137 'allocated_to', 'hold_job',138 'comment'])139 results = status.node_list(self.zk, node_id)140 print(status.output(results, 'pretty', fields))141 def dib_image_list(self):142 results = status.dib_image_list(self.zk)143 print(status.output(results, 'pretty'))144 def image_list(self):145 results = status.image_list(self.zk)146 print(status.output(results, 'pretty'))147 def image_build(self, diskimage=None):148 diskimage = diskimage or self.args.image149 if diskimage not in self.pool.config.diskimages:150 # only can build disk images, not snapshots151 raise Exception("Trying to build a non disk-image-builder "152 "image: %s" % diskimage)153 if self.pool.config.diskimages[diskimage].pause:154 raise Exception(155 "Skipping build request for image %s; paused" % diskimage)156 self.zk.submitBuildRequest(diskimage)157 def alien_image_list(self):158 self.pool.updateConfig()159 t = PrettyTable(["Provider", "Name", "Image ID"])160 t.align = 'l'161 for provider in self.pool.config.providers.values():162 if (self.args.provider and163 provider.name != self.args.provider):164 continue165 manager = self.pool.getProviderManager(provider.name)166 # Build list of provider images as known by the provider167 provider_images = []168 try:169 # Only consider images marked as managed by nodepool.170 # Prevent cloud-provider images from showing171 # up in alien list since we can't do anything about them172 # anyway.173 provider_images = [174 image for image in manager.listImages()175 if 'nodepool_build_id' in image['properties']]176 except Exception as e:177 log.warning("Exception listing alien images for %s: %s"178 % (provider.name, str(e)))179 alien_ids = []180 uploads = []181 for image in provider.diskimages:182 # Build list of provider images as recorded in ZK183 for bnum in self.zk.getBuildNumbers(image):184 uploads.extend(185 self.zk.getUploads(image, bnum,186 provider.name,187 states=[zk.READY])188 )189 # Calculate image IDs present in the provider, but not in ZK190 provider_image_ids = set([img['id'] for img in provider_images])191 zk_image_ids = set([img.external_id for img in uploads])192 alien_ids = provider_image_ids - zk_image_ids193 for image in provider_images:194 if image['id'] in alien_ids:195 t.add_row([provider.name, image['name'], image['id']])196 print(t)197 def delete(self):198 node = self.zk.getNode(self.args.id)199 if not node:200 print("Node id %s not found" % self.args.id)201 return202 self.zk.lockNode(node, blocking=True, timeout=5)203 if self.args.now:204 if node.provider not in self.pool.config.providers:205 print("Provider %s for node %s not defined on this launcher" %206 (node.provider, node.id))207 return208 provider = self.pool.config.providers[node.provider]209 manager = provider_manager.get_provider(provider)210 manager.start(self.zk)211 launcher.NodeDeleter.delete(self.zk, manager, node)212 manager.stop()213 else:214 node.state = zk.DELETING215 self.zk.storeNode(node)216 self.zk.unlockNode(node)217 self.list(node_id=node.id)218 def dib_image_delete(self):219 (image, build_num) = self.args.id.rsplit('-', 1)220 build = self.zk.getBuild(image, build_num)221 if not build:222 print("Build %s not found" % self.args.id)223 return224 if build.state == zk.BUILDING:225 print("Cannot delete a build in progress")226 return227 build.state = zk.DELETING228 self.zk.storeBuild(image, build, build.id)229 def image_delete(self):230 provider_name = self.args.provider231 image_name = self.args.image232 build_id = self.args.build_id233 upload_id = self.args.upload_id234 image = self.zk.getImageUpload(image_name, build_id, provider_name,235 upload_id)236 if not image:237 print("Image upload not found")238 return239 if image.state == zk.UPLOADING:240 print("Cannot delete because image upload in progress")241 return242 image.state = zk.DELETING243 self.zk.storeImageUpload(image.image_name, image.build_id,244 image.provider_name, image, image.id)245 def erase(self):246 def do_erase(provider_name, provider_builds, provider_nodes):247 print("Erasing build data for %s..." % provider_name)248 self.zk.removeProviderBuilds(provider_name, provider_builds)249 print("Erasing node data for %s..." % provider_name)250 self.zk.removeProviderNodes(provider_name, provider_nodes)251 provider_name = self.args.provider252 provider_builds = self.zk.getProviderBuilds(provider_name)253 provider_nodes = self.zk.getProviderNodes(provider_name)254 if self.args.force:255 do_erase(provider_name, provider_builds, provider_nodes)256 else:257 print("\nWARNING! This action is not reversible!")258 answer = input("Erase ZooKeeper data for provider %s? [N/y] " %259 provider_name)260 if answer.lower() != 'y':261 print("Aborting. No data erased.")262 else:263 do_erase(provider_name, provider_builds, provider_nodes)264 def info(self):265 provider_name = self.args.provider266 provider_builds = self.zk.getProviderBuilds(provider_name)267 provider_nodes = self.zk.getProviderNodes(provider_name)268 print("ZooKeeper data for provider %s\n" % provider_name)269 print("Image builds:")270 t = PrettyTable(['Image Name', 'Build IDs'])271 t.align = 'l'272 for image, builds in provider_builds.items():273 t.add_row([image, ','.join(builds)])274 print(t)275 print("\nNodes:")276 t = PrettyTable(['ID', 'Server ID'])277 t.align = 'l'278 for node in provider_nodes:279 t.add_row([node.id, node.external_id])280 print(t)281 def config_validate(self):282 validator = ConfigValidator(self.args.config)283 validator.validate()284 log.info("Configuration validation complete")285 # TODO(asselin,yolanda): add validation of secure.conf286 def request_list(self):287 results = status.request_list(self.zk)288 print(status.output(results, 'pretty'))289 def _wait_for_threads(self, threads):290 for t in threads:291 if t:292 t.join()293 def run(self):294 self.zk = None295 # no arguments, print help messaging, then exit with error(1)296 if not self.args.command:297 self.parser.print_help()298 return 1299 # commands which do not need to start-up or parse config300 if self.args.command in ('config-validate'):301 return self.args.func()302 self.pool = launcher.NodePool(self.args.secure, self.args.config)303 config = self.pool.loadConfig()304 # commands needing ZooKeeper305 if self.args.command in ('image-build', 'dib-image-list',306 'image-list', 'dib-image-delete',307 'image-delete', 'alien-image-list',308 'list', 'delete',309 'request-list', 'info', 'erase'):310 self.zk = zk.ZooKeeper(enable_cache=False)311 self.zk.connect(list(config.zookeeper_servers.values()))312 self.pool.setConfig(config)313 self.args.func()314 if self.zk:315 self.zk.disconnect()316def main():317 return NodePoolCmd.main()318if __name__ == "__main__":...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful