├── .coveragerc ├── .gitignore ├── .travis.yml ├── LICENSE ├── MANIFEST.in ├── README.md ├── VERSION ├── examples ├── Vagrantfile ├── cumulus.yml ├── guest-data.yml ├── guest-defaults.yml └── guests.yml ├── grifter ├── __init__.py ├── api.py ├── cli.py ├── config.yml ├── constants.py ├── custom_filters.py ├── defaults.yml ├── examples │ ├── groups-example.yml │ ├── guest-defaults.yml │ ├── guests-example.yml │ ├── veos-guests.yml │ ├── vmx-guests.yml │ ├── vqfx-guests.yml │ └── vsrx-guests.yml ├── loaders.py ├── schemas │ ├── guest-config-schema.yml │ ├── guest-pairs-schema.yml │ └── guest-schema.yml ├── templates │ ├── additional-data-storage-trigger.j2 │ ├── base.j2 │ ├── blackhole-interfaces-trigger.j2 │ ├── get-mac.rb │ ├── guest.j2 │ ├── libvirt-config.j2 │ ├── libvirt-data-interface-config.j2 │ ├── libvirt-internal-interface-config.j2 │ ├── libvirt-reserved-interface-config.j2 │ ├── throttle-cpu-trigger.j2 │ └── topology.dot.j2 ├── utils.py └── validators.py ├── requirements-dev.txt ├── requirements.txt ├── setup.py └── tests ├── __init__.py ├── mock_data.py ├── mock_invalid_guest_data.yml ├── mock_json_data.json ├── mock_vagrantfile.rb ├── mock_vagrantfile_additional_storage_volumes.rb ├── test_api.py ├── test_cli.py ├── test_constants.py ├── test_custom_filters.py ├── test_defaults.py ├── test_loaders.py ├── test_render.py ├── test_utils.py └── test_validators.py /.coveragerc: -------------------------------------------------------------------------------- 1 | [run] 2 | omit= 3 | *__init__.py 4 | */tests/test_*.py -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | 103 | # editors 104 | .idea/ 105 | 106 | # Pipenv 107 | Pipfile* 108 | 109 | # Pytest 110 | .pytest_cache/ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # CI build file 2 | language: python 3 | python: 4 | - "3.6" 5 | before_install: 6 | - pip install pytest pytest-cov 7 | - pip install coveralls 8 | - pip install mock 9 | script: 10 | - pytest --cov=grifter --cov-report=term-missing 11 | after_success: 12 | - coveralls 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include grifter/templates/* 2 | include grifter/examples/* 3 | include grifter/schemas/* 4 | include grifter/defaults.yml 5 | include grifter/config.yml 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Grifter 2 | Python library to build large scale Vagrant topologies for the networking 3 | space. Can also be used the build small scale labs for networking/compute 4 | devices. 5 | 6 | [![Build Status](https://travis-ci.org/bobthebutcher/grifter.svg?branch=master)](https://travis-ci.org/bobthebutcher/grifter.svg?branch=master) 7 | [![Coverage Status](https://coveralls.io/repos/github/bobthebutcher/grifter/badge.svg?branch=master)](https://coveralls.io/github/bobthebutcher/grifter?branch=master) 8 | 9 | NOTE: Python 3.6+ is required to make use of this library. 10 | 11 | ``` 12 | ***************************************************************** 13 | This project is currently in beta and stability is not currently 14 | guaranteed. Breaking API changes can be expected. 15 | ***************************************************************** 16 | 17 | ``` 18 | ## Vagrant 19 | What is Vagrant? From the Vagrant [website](https://www.vagrantup.com/docs/index.html) 20 | ``` 21 | A command line utility for managing the lifecycle of virtual machines 22 | ``` 23 | 24 | ## Vagrant Libvirt 25 | What is Vagrant Libvirt? From the `vagrant-libvirt` github [page](https://github.com/vagrant-libvirt/vagrant-libvirt) 26 | ``` 27 | A Vagrant plugin that adds a Libvirt provider to Vagrant, allowing Vagrant to control and provision machines via Libvirt toolkit. 28 | ``` 29 | 30 | ## Why 31 | When simulating large topologies Vagrantfiles can become thousands 32 | of lines long. Getting all the configuration correct is often a 33 | frustrating, error riddled process especially for those not familiar 34 | with Vagrant. Grifter aims to help simplify that process. 35 | 36 | ##### Additional project goals 37 | - Generate topology.dot files for use with PTM :heavy_check_mark: 38 | - Generate Inventory files for tools such as Ansible, Nornir 39 | 40 | NOTE: Only a `vagrant-libvirt` compatible `Vagrantfile` for 41 | Vagrant version `>= 2.1.0` will be generated. 42 | 43 | Support for Virtualbox or any other provider type is not supported or 44 | on the road map. 45 | 46 | ## Dependencies 47 | Grifter requires the help of the following awesome projects from the Python 48 | community. 49 | - [Cerberus](http://docs.python-cerberus.org/en/stable/) - Schema validation 50 | - [Click](https://click.palletsprojects.com/) - CLI utility 51 | - [Jinja2](http://jinja.pocoo.org/docs) - Template engine 52 | - [PyYAML](https://pyyaml.org/) - YAML all the things 53 | 54 | ## Installation 55 | There is currently no PyPI release for this project. Grifter can be 56 | installed directly from source using PIP. 57 | 58 | Create and activate virtualenv. 59 | ``` 60 | mkdir ~/test && cd ~/test 61 | python3 -m venv .venv 62 | source .venv/bin/activate 63 | ``` 64 | 65 | Install `grifter` with `pip` 66 | ``` 67 | # Install the master branch. 68 | pip install https://github.com/bobthebutcher/grifter/archive/master.zip 69 | ``` 70 | 71 | Releases are distributed via Github Releases. 72 | ``` 73 | # Install the latest release. 74 | pip install https://github.com/bobthebutcher/grifter/archive/v0.2.12.zip 75 | ``` 76 | 77 | ## Quick Start 78 | Create a `guests.yml` file. 79 | ``` 80 | tee guests.yml > /dev/null << "EOF" 81 | srv01: 82 | vagrant_box: 83 | name: "centos/7" 84 | EOF 85 | ``` 86 | 87 | Generate a Vagrantfile 88 | ``` 89 | grifter create guests.yml 90 | ``` 91 | 92 | Let Vagrant do its magic 93 | ``` 94 | vagrant up 95 | ``` 96 | 97 | 98 | 99 | ## Config File 100 | A file named `config.yml` is required to define the base settings of 101 | each box managed within the grifter environment. The default `config.yml` 102 | file can be found [here](grifter/config.yml) 103 | 104 | ### Box Naming 105 | Grifter expects Vagrant boxes to be named according to the following list. 106 | 107 | ##### Custom Boxes 108 | - arista/veos 109 | - cisco/csr1000v 110 | - cisco/iosv 111 | - cisco/xrv 112 | - juniper/vmx-vcp 113 | - juniper/vmx-vfp 114 | - juniper/vqfx-pfe 115 | - juniper/vqfx-re 116 | - juniper/vsrx 117 | - juniper/vsrx-packetmode 118 | 119 | ##### Vagrant Cloud Boxes 120 | - CumulusCommunity/cumulus-vx 121 | - centos/7 122 | - generic/ubuntu1804 123 | - opensuse/openSUSE-15.0-x86_64 124 | 125 | #### guest_config 126 | The `guest_config` section defines characteristics about the Vagrant boxes 127 | used with grifter. 128 | #### Required Parameters. 129 | - data_interface_base 130 | - data_interface_offset 131 | - max_data_interfaces 132 | - management_interface 133 | 134 | Note: `data_interface_base` cannot be an empty string. If the box does not 135 | have any data interfaces the suggested value is "NA". This field will be 136 | ignored so it can be anything as long as it is not empty. 137 | 138 | ```yaml 139 | guest_config: 140 | example/box: 141 | data_interface_base: "eth" # String pattern for data interfaces. 142 | data_interface_offset: 0 # Number of first data interface ie: 0, 1, 2, etc.. 143 | internal_interfaces: 0 # Used for inter-box connections for multi-vm boxes. 144 | max_data_interfaces: 8 145 | management_interface: "ma1" 146 | reserved_interfaces: 0 # Interfaces that are required but cannot be used. 147 | 148 | arista/veos: 149 | data_interface_base: "eth" 150 | data_interface_offset: 1 151 | internal_interfaces: 0 152 | max_data_interfaces: 24 153 | management_interface: "ma1" 154 | reserved_interfaces: 0 155 | 156 | juniper/vsrx-packetmode: 157 | data_interface_base: "ge-0/0/" 158 | data_interface_offset: 0 159 | internal_interfaces: 0 160 | max_data_interfaces: 16 161 | management_interface: "fxp0.0" 162 | reserved_interfaces: 0 163 | ``` 164 | 165 | #### guest_pairs 166 | The `guest_pairs` section is used the define boxes that need two VMs to 167 | be fully functional. Some examples are the Juniper vMX and vQFX where 168 | one box is used for the control-plane and another for the forwarding-plane. 169 | 170 | NOTE: This functionality will be added in a future release. 171 | 172 | #### Custom config files 173 | A default config file ships with the grifter python package. 174 | This file can be customized with your required parameters by creating a 175 | `config.yml` file in the following locations. 176 | - `/opt/grifter/` 177 | - `~/.grifter/` 178 | - `./` 179 | 180 | Parameters in a users `config.yml` file will be merged with the default 181 | `config.yml` file with the user-defined parameters taking preference. 182 | 183 | ## Usage 184 | 185 | #### CLI Utility 186 | Grifter ships with a CLI utility. Execute `grifter -h` to 187 | discover all the CLI options. 188 | 189 | ``` 190 | grifter -h 191 | Usage: grifter [OPTIONS] COMMAND [ARGS]... 192 | 193 | Create a Vagrantfile from a YAML data input file. 194 | 195 | Options: 196 | --version Show the version and exit. 197 | -h, --help Show this message and exit. 198 | 199 | Commands: 200 | create Create a Vagrantfile. 201 | example Print example file declaration. 202 | ``` 203 | 204 | #### Create Vagrantfile 205 | ``` 206 | grifter create guests.yml 207 | ``` 208 | 209 | ### Guests Datafile 210 | Guest VMs characteristics and interface connections are defined in a YAML file. 211 | This file can be named anything, but the recommended naming convention is 212 | `guests.yml`. 213 | 214 | #### Guest Schema 215 | Jinja2 is used a the templating engine to generate the Vagrantfiles. 216 | Guests definition within a guests file must use the following 217 | schema as it is required to ensure templates render correctly and 218 | without errors. The guest data will be validated against the schema 219 | using the Cerberus project. 220 | 221 | ```yaml 222 | some-guest: # guest name 223 | vagrant_box: # vagrant_box parameters 224 | name: # string - required 225 | version: # string - optional | default: "" 226 | url: # string - optional | default: "" 227 | provider: # string - optional | default: "libvirt" 228 | guest_type: # string - optional | default: "" 229 | boot_timeout: # integer - optional | default: 0 230 | throttle_cpu: # integer - optional | default: 0 231 | 232 | ssh: # dict - optional 233 | username: # string - optional | default: "" 234 | password: # string - optional | default: "" 235 | insert_key: # boolean - optional | default: False 236 | 237 | synced_folder: # dict - optional 238 | enabled: # boolean - default: False 239 | id: # string - default: "vagrant-root" 240 | src: # string - default: "." 241 | dst: # string - default: "/vagrant" 242 | 243 | provider_config: # dict - optional 244 | random_hostname: # boolean - optional | default: False 245 | nic_adapter_count: # integer - optional | default: 0 246 | disk_bus: # string - optional | default: "" 247 | cpus: # integer - optional | default: 1 248 | memory: # integer - optional | default: 512 249 | huge_pages: # boolean - optional | default: False 250 | storage_pool: # string - optional | default: "" 251 | additional_storage_volumes: # list - optional 252 | # For each list element the following is required. 253 | - location: # string 254 | type: # string 255 | bus: # string 256 | device: # string 257 | nic_model_type: # string - optional | default: "" 258 | management_network_mac: # string - optional | default: "" 259 | 260 | internal_interfaces: # list - optional 261 | # For each list element the following is required. 262 | - local_port: # integer 263 | remote_guest: # string 264 | remote_port: # integer 265 | 266 | data_interfaces: # list - optional 267 | # For each list element the following is required. 268 | - local_port: # integer 269 | remote_guest: # string 270 | remote_port: # integer 271 | ``` 272 | 273 | #### Example Datafile 274 | The following example datafile defines two `arista/veos` switches connected 275 | together on ports 1 and 2. 276 | ```yaml 277 | sw01: 278 | vagrant_box: 279 | name: "arista/veos" 280 | version: "4.20.1F" 281 | guest_type: "tinycore" 282 | provider: "libvirt" 283 | ssh: 284 | insert_key: False 285 | synced_folder: 286 | enabled: False 287 | provider_config: 288 | nic_adapter_count: 2 289 | disk_bus: "ide" 290 | cpus: 2 291 | memory: 2048 292 | data_interfaces: 293 | - local_port: 1 294 | remote_guest: "sw02" 295 | remote_port: 1 296 | - local_port: 2 297 | remote_guest: "sw02" 298 | remote_port: 2 299 | 300 | sw02: 301 | vagrant_box: 302 | name: "arista/veos" 303 | version: "4.20.1F" 304 | guest_type: "tinycore" 305 | provider: "libvirt" 306 | ssh: 307 | insert_key: False 308 | synced_folder: 309 | enabled: False 310 | provider_config: 311 | nic_adapter_count: 2 312 | disk_bus: "ide" 313 | cpus: 2 314 | memory: 2048 315 | data_interfaces: 316 | - local_port: 1 317 | remote_guest: "sw01" 318 | remote_port: 1 319 | - local_port: 2 320 | remote_guest: "sw01" 321 | remote_port: 2 322 | ``` 323 | #### Generated Vagrantfile 324 | ```ruby 325 | # -*- mode: ruby -*- 326 | # vi: set ft=ruby : 327 | 328 | def get_mac(oui="28:b7:ad") 329 | "Generate a MAC address" 330 | nic = (1..3).map{"%0.2x"%rand(256)}.join(":") 331 | return "#{oui}:#{nic}" 332 | end 333 | 334 | cwd = Dir.pwd.split("/").last 335 | username = ENV['USER'] 336 | domain_prefix = "#{username}_#{cwd}" 337 | domain_uuid = "1f22b55d-2d7e-5a24-b4fa-3a8878df5cc5" 338 | 339 | Vagrant.require_version ">= 2.1.0" 340 | Vagrant.configure("2") do |config| 341 | 342 | config.vm.define "sw01" do |node| 343 | guest_name = "sw01" 344 | node.vm.box = "arista/veos" 345 | node.vm.box_version = "4.20.1F" 346 | node.vm.guest = :tinycore 347 | node.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true 348 | 349 | node.ssh.insert_key = false 350 | 351 | node.vm.provider :libvirt do |domain| 352 | domain.default_prefix = "#{domain_prefix}" 353 | domain.cpus = 2 354 | domain.memory = 2048 355 | domain.disk_bus = "ide" 356 | domain.nic_adapter_count = 2 357 | end 358 | 359 | node.vm.network :private_network, 360 | # sw01-eth1 <--> sw02-eth1 361 | :mac => "#{get_mac()}", 362 | :libvirt__tunnel_type => "udp", 363 | :libvirt__tunnel_local_ip => "127.146.53.1", 364 | :libvirt__tunnel_local_port => 10001, 365 | :libvirt__tunnel_ip => "127.146.53.2", 366 | :libvirt__tunnel_port => 10001, 367 | :libvirt__iface_name => "sw01-eth1-#{domain_uuid}", 368 | auto_config: false 369 | 370 | node.vm.network :private_network, 371 | # sw01-eth2 <--> sw02-eth2 372 | :mac => "#{get_mac()}", 373 | :libvirt__tunnel_type => "udp", 374 | :libvirt__tunnel_local_ip => "127.146.53.1", 375 | :libvirt__tunnel_local_port => 10002, 376 | :libvirt__tunnel_ip => "127.146.53.2", 377 | :libvirt__tunnel_port => 10002, 378 | :libvirt__iface_name => "sw01-eth2-#{domain_uuid}", 379 | auto_config: false 380 | 381 | end 382 | config.vm.define "sw02" do |node| 383 | guest_name = "sw02" 384 | node.vm.box = "arista/veos" 385 | node.vm.box_version = "4.20.1F" 386 | node.vm.guest = :tinycore 387 | node.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true 388 | 389 | node.ssh.insert_key = false 390 | 391 | node.vm.provider :libvirt do |domain| 392 | domain.default_prefix = "#{domain_prefix}" 393 | domain.cpus = 2 394 | domain.memory = 2048 395 | domain.storage_pool_name = "disk1" 396 | domain.disk_bus = "ide" 397 | domain.nic_adapter_count = 2 398 | end 399 | 400 | node.vm.network :private_network, 401 | # sw02-eth1 <--> sw01-eth1 402 | :mac => "#{get_mac()}", 403 | :libvirt__tunnel_type => "udp", 404 | :libvirt__tunnel_local_ip => "127.146.53.2", 405 | :libvirt__tunnel_local_port => 10001, 406 | :libvirt__tunnel_ip => "127.146.53.1", 407 | :libvirt__tunnel_port => 10001, 408 | :libvirt__iface_name => "sw02-eth1-#{domain_uuid}", 409 | auto_config: false 410 | 411 | node.vm.network :private_network, 412 | # sw02-eth2 <--> sw01-eth2 413 | :mac => "#{get_mac()}", 414 | :libvirt__tunnel_type => "udp", 415 | :libvirt__tunnel_local_ip => "127.146.53.2", 416 | :libvirt__tunnel_local_port => 10002, 417 | :libvirt__tunnel_ip => "127.146.53.1", 418 | :libvirt__tunnel_port => 10002, 419 | :libvirt__iface_name => "sw02-eth2-#{domain_uuid}", 420 | auto_config: false 421 | 422 | end 423 | 424 | end 425 | ``` 426 | 427 | ### Defaults Per-Guest Type 428 | It is possible to define default values per guest group type. Grifter will 429 | look for a file named `guest-defaults.yml` in the following locations from 430 | the least to most preferred: 431 | 432 | - `/opt/grifter/` 433 | - `~/.grifter/` 434 | - `./` 435 | 436 | ```yaml 437 | arista/veos: 438 | vagrant_box: 439 | version: "4.20.1F" 440 | guest_type: "tinycore" 441 | ssh: 442 | insert_key: False 443 | synced_folder: 444 | enabled: False 445 | provider_config: 446 | nic_adapter_count: 24 447 | cpus: 2 448 | memory: 2048 449 | disk_bus: "ide" 450 | 451 | juniper/vsrx-packetmode: 452 | vagrant_box: 453 | version: "18.3R1-S1.4" 454 | provider: "libvirt" 455 | guest_type: "tinycore" 456 | ssh: 457 | insert_key: False 458 | synced_folder: 459 | enabled: False 460 | provider_config: 461 | nic_adapter_count: 2 462 | disk_bus: "ide" 463 | cpus: 2 464 | memory: 4096 465 | ``` 466 | 467 | Group variables can be over-written by variables at the guest variable level. 468 | The values of the group and guest variables will be merged prior to building 469 | a `Vagrantfile` with the guest variables taking precedence over the group 470 | variables. 471 | 472 | This means you can have a much more succinct guests file by reducing 473 | a lot of duplication. Here is an example of a simplified guest file. The 474 | values from the `arista/veos` guest type in the `guest-defaults.yml` file 475 | will be used to fill in the parameters for the guests. 476 | 477 | ```yaml 478 | sw01: 479 | vagrant_box: 480 | name: "arista/veos" 481 | provider_config: 482 | nic_adapter_count: 2 483 | data_interfaces: 484 | - local_port: 1 485 | remote_guest: "sw02" 486 | remote_port: 1 487 | - local_port: 2 488 | remote_guest: "sw02" 489 | remote_port: 2 490 | 491 | sw02: 492 | vagrant_box: 493 | name: "arista/veos" 494 | provider_config: 495 | nic_adapter_count: 2 496 | data_interfaces: 497 | - local_port: 1 498 | remote_guest: "sw01" 499 | remote_port: 1 500 | - local_port: 2 501 | remote_guest: "sw01" 502 | remote_port: 2 503 | ``` 504 | 505 | The generated `Vagrantfile` below is the same as the one above, but with a 506 | much cleaner guest definition file. 507 | 508 | ```ruby 509 | # -*- mode: ruby -*- 510 | # vi: set ft=ruby : 511 | 512 | def get_mac(oui="28:b7:ad") 513 | "Generate a MAC address" 514 | nic = (1..3).map{"%0.2x"%rand(256)}.join(":") 515 | return "#{oui}:#{nic}" 516 | end 517 | 518 | cwd = Dir.pwd.split("/").last 519 | username = ENV['USER'] 520 | domain_prefix = "#{username}_#{cwd}" 521 | domain_uuid = "d35fb1b6-ecdc-5412-be22-185446af92d6" 522 | 523 | Vagrant.require_version ">= 2.1.0" 524 | Vagrant.configure("2") do |config| 525 | 526 | config.vm.define "sw01" do |node| 527 | guest_name = "sw01" 528 | node.vm.box = "arista/veos" 529 | node.vm.box_version = "4.20.1F" 530 | node.vm.guest = :tinycore 531 | node.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true 532 | 533 | node.ssh.insert_key = false 534 | 535 | node.vm.provider :libvirt do |domain| 536 | domain.default_prefix = "#{domain_prefix}" 537 | domain.cpus = 2 538 | domain.memory = 2048 539 | domain.disk_bus = "ide" 540 | domain.nic_adapter_count = 2 541 | end 542 | 543 | node.vm.network :private_network, 544 | # sw01-eth1 <--> sw02-eth1 545 | :mac => "#{get_mac()}", 546 | :libvirt__tunnel_type => "udp", 547 | :libvirt__tunnel_local_ip => "127.127.145.1", 548 | :libvirt__tunnel_local_port => 10001, 549 | :libvirt__tunnel_ip => "127.127.145.2", 550 | :libvirt__tunnel_port => 10001, 551 | :libvirt__iface_name => "sw01-eth1-#{domain_uuid}", 552 | auto_config: false 553 | 554 | node.vm.network :private_network, 555 | # sw01-eth2 <--> sw02-eth2 556 | :mac => "#{get_mac()}", 557 | :libvirt__tunnel_type => "udp", 558 | :libvirt__tunnel_local_ip => "127.127.145.1", 559 | :libvirt__tunnel_local_port => 10002, 560 | :libvirt__tunnel_ip => "127.127.145.2", 561 | :libvirt__tunnel_port => 10002, 562 | :libvirt__iface_name => "sw01-eth2-#{domain_uuid}", 563 | auto_config: false 564 | 565 | end 566 | config.vm.define "sw02" do |node| 567 | guest_name = "sw02" 568 | node.vm.box = "arista/veos" 569 | node.vm.box_version = "4.20.1F" 570 | node.vm.guest = :tinycore 571 | node.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true 572 | 573 | node.ssh.insert_key = false 574 | 575 | node.vm.provider :libvirt do |domain| 576 | domain.default_prefix = "#{domain_prefix}" 577 | domain.cpus = 2 578 | domain.memory = 2048 579 | domain.storage_pool_name = "disk1" 580 | domain.disk_bus = "ide" 581 | domain.nic_adapter_count = 2 582 | end 583 | 584 | node.vm.network :private_network, 585 | # sw02-eth1 <--> sw01-eth1 586 | :mac => "#{get_mac()}", 587 | :libvirt__tunnel_type => "udp", 588 | :libvirt__tunnel_local_ip => "127.127.145.2", 589 | :libvirt__tunnel_local_port => 10001, 590 | :libvirt__tunnel_ip => "127.127.145.1", 591 | :libvirt__tunnel_port => 10001, 592 | :libvirt__iface_name => "sw02-eth1-#{domain_uuid}", 593 | auto_config: false 594 | 595 | node.vm.network :private_network, 596 | # sw02-eth2 <--> sw01-eth2 597 | :mac => "#{get_mac()}", 598 | :libvirt__tunnel_type => "udp", 599 | :libvirt__tunnel_local_ip => "127.127.145.2", 600 | :libvirt__tunnel_local_port => 10002, 601 | :libvirt__tunnel_ip => "127.127.145.1", 602 | :libvirt__tunnel_port => 10002, 603 | :libvirt__iface_name => "sw02-eth2-#{domain_uuid}", 604 | auto_config: false 605 | 606 | end 607 | 608 | end 609 | ``` 610 | 611 | ## Example Files 612 | Examples of the `config.yml`, `guests-defaults.yml` and `guests.yml` files 613 | can be found [here](grifter/examples) 614 | 615 | 616 | ## Interfaces 617 | There are 3 types of interfaces that can be defined. 618 | 619 | - internal_interfaces 620 | - data_interfaces 621 | - reserved_interfaces 622 | 623 | #### Internal Interfaces 624 | Config location: `guests.yml` 625 | Used for an inter-vm communication channel for multi-vm boxes. 626 | Known examples are the vMX and vQFX. 627 | 628 | #### data_interfaces 629 | Config location: `guests.yml` 630 | Revenue ports that are used to pass data traffic. 631 | 632 | #### reserved_interfaces 633 | Config location: `config.yml` 634 | Interfaces that need to be defined because 'reasons' but cannot be 635 | used. The only known example is the `juniper/vqfx-re`. The number of 636 | reserved_interfaces is defined per-box type in the `config.yml` file. 637 | Grifter builds out the interface definitions automatically as a 638 | blackhole interfaces. 639 | 640 | #### Blackhole Interfaces 641 | Interfaces defined in the Vagratfile relate to interfaces 642 | on the guest vm on a first to last basis. This can be undesirable when 643 | trying to accurately simulate a production environment when devices 644 | can have 48+ ports. 645 | 646 | Grifter will automatically create `blackhole interfaces` to fill out 647 | undefined `data_interfaces` ports up to the box types 648 | `max_data_interfaces` parameter in the `config.yml` file. 649 | 650 | #### Vagrantfile Interface Order 651 | Interfaces are added to the Vagrantfile in the following order. 652 | - internal_interfaces 653 | - reserved_interfaces 654 | - data_interfaces 655 | 656 | Interfaces are configured using the udp tunneling type. This 657 | will create a 'pseudo' layer 1 connection between VM ports. 658 | 659 | ##### Example interface definition 660 | ```yaml 661 | data_interfaces: 662 | - local_port: 1 663 | remote_guest: "sw02" 664 | remote_port: 1 665 | ``` 666 | ##### Rendered Vagrantfile interface 667 | ```ruby 668 | node.vm.network :private_network, 669 | # sw01-eth1 <--> sw02-eth1 670 | :mac => "#{get_mac()}", 671 | :libvirt__tunnel_type => "udp", 672 | :libvirt__tunnel_local_ip => "127.255.255.1", 673 | :libvirt__tunnel_local_port => 10001, 674 | :libvirt__tunnel_ip => "127.255.255.2", 675 | :libvirt__tunnel_port => 10001, 676 | :libvirt__iface_name => "sw01-eth1-#{domain_uuid}", 677 | auto_config: false 678 | ``` 679 | 680 | #### NIC Adapter Count 681 | Config location: `guests.yml` 682 | Defines the total number of `data_interfaces` to create on the VM. 683 | Any undefined `data_interfaces` will be added as a blackhole interface. 684 | 685 | The total is calculated against the sum of the `internal_interfaces`, ` 686 | reserved_interfaces` and `data_interfaces` parameters after blackhole 687 | interfaces have been added automatically by the template system. 688 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 0.2.12 -------------------------------------------------------------------------------- /examples/Vagrantfile: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # vi: set ft=ruby : 3 | 4 | def get_mac(oui="28:b7:ad") 5 | "Generate a MAC address" 6 | nic = (1..3).map{"%0.2x"%rand(256)}.join(":") 7 | return "#{oui}:#{nic}" 8 | end 9 | 10 | cwd = Dir.pwd.split("/").last 11 | username = ENV['USER'] 12 | domain_prefix = "#{username}_#{cwd}" 13 | 14 | Vagrant.configure("2") do |config| 15 | 16 | config.vm.define "sw01" do |node| 17 | guest_name = "sw01" 18 | node.vm.box = "arista/veos" 19 | node.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true 20 | 21 | node.ssh.insert_key = false 22 | 23 | node.vm.provider :libvirt do |domain| 24 | domain.default_prefix = "#{domain_prefix}" 25 | domain.cpus = 2 26 | domain.memory = 2048 27 | domain.disk_bus = "ide" 28 | domain.nic_adapter_count = 2 29 | end 30 | 31 | node.vm.network :private_network, 32 | # sw01-int1 <--> sw02-int1 33 | :mac => "#{get_mac()}", 34 | :libvirt__tunnel_type => "udp", 35 | :libvirt__tunnel_local_ip => "127.255.1.1", 36 | :libvirt__tunnel_local_port => 10001, 37 | :libvirt__tunnel_ip => "127.255.1.2", 38 | :libvirt__tunnel_port => 10001, 39 | :libvirt__iface_name => "eth1", 40 | auto_config: false 41 | 42 | node.vm.network :private_network, 43 | # sw01-int2 <--> sw02-int2 44 | :mac => "#{get_mac()}", 45 | :libvirt__tunnel_type => "udp", 46 | :libvirt__tunnel_local_ip => "127.255.1.1", 47 | :libvirt__tunnel_local_port => 10002, 48 | :libvirt__tunnel_ip => "127.255.1.2", 49 | :libvirt__tunnel_port => 10002, 50 | :libvirt__iface_name => "eth2", 51 | auto_config: false 52 | 53 | end 54 | config.vm.define "sw02" do |node| 55 | guest_name = "sw02" 56 | node.vm.box = "arista/veos" 57 | node.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true 58 | 59 | node.ssh.insert_key = false 60 | 61 | node.vm.provider :libvirt do |domain| 62 | domain.default_prefix = "#{domain_prefix}" 63 | domain.cpus = 2 64 | domain.memory = 2048 65 | domain.disk_bus = "ide" 66 | domain.nic_adapter_count = 2 67 | end 68 | 69 | node.vm.network :private_network, 70 | # sw02-int1 <--> sw01-int1 71 | :mac => "#{get_mac()}", 72 | :libvirt__tunnel_type => "udp", 73 | :libvirt__tunnel_local_ip => "127.255.1.2", 74 | :libvirt__tunnel_local_port => 10001, 75 | :libvirt__tunnel_ip => "127.255.1.1", 76 | :libvirt__tunnel_port => 10001, 77 | :libvirt__iface_name => "eth1", 78 | auto_config: false 79 | 80 | node.vm.network :private_network, 81 | # sw02-int2 <--> sw01-int2 82 | :mac => "#{get_mac()}", 83 | :libvirt__tunnel_type => "udp", 84 | :libvirt__tunnel_local_ip => "127.255.1.2", 85 | :libvirt__tunnel_local_port => 10002, 86 | :libvirt__tunnel_ip => "127.255.1.1", 87 | :libvirt__tunnel_port => 10002, 88 | :libvirt__iface_name => "eth2", 89 | auto_config: false 90 | 91 | end 92 | 93 | end 94 | -------------------------------------------------------------------------------- /examples/cumulus.yml: -------------------------------------------------------------------------------- 1 | --- 2 | sw01: 3 | vagrant_box: 4 | name: "CumulusCommunity/cumulus-vx" 5 | provider_config: 6 | nic_adapter_count: 12 7 | data_interfaces: 8 | - local_port: 1 9 | remote_guest: "sw02" 10 | remote_port: 1 11 | - local_port: 2 12 | remote_guest: "sw02" 13 | remote_port: 2 14 | 15 | sw02: 16 | vagrant_box: 17 | name: "CumulusCommunity/cumulus-vx" 18 | provider_config: 19 | nic_adapter_count: 12 20 | data_interfaces: 21 | - local_port: 1 22 | remote_guest: "sw01" 23 | remote_port: 1 24 | - local_port: 2 25 | remote_guest: "sw01" 26 | remote_port: 2 27 | -------------------------------------------------------------------------------- /examples/guest-data.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # Jinja2 templates are build around data input in this format. 3 | # Every host must follow this data format. 4 | some-guest: 5 | vagrant_box: # vagrant_box parameters 6 | name: # string - required 7 | version: # string - optional | default: "" 8 | url: # string - optional | default: "" 9 | provider: # string - optional | default: "libvirt" 10 | guest_type: # string - optional | default: "" 11 | boot_timeout: # integer - optional | default: 0 12 | throttle_cpu: # boolean - optional | default: False 13 | 14 | ssh: # dict - optional 15 | username: # string - optional | default: "" 16 | password: # string - optional | default: "" 17 | insert_key: # boolean - optional | default: False 18 | 19 | synced_folder: # dict - optional 20 | enabled: # boolean - default: False 21 | id: # string - default: "vagrant-root" 22 | src: # string - default: "." 23 | dst: # string - default: "/vagrant" 24 | 25 | provider_config: # dict - optional 26 | random_hostname: # boolean - optional | default: False 27 | nic_adapter_count: # integer - optional | default: 0 28 | disk_bus: # string - optional | default: "" 29 | cpus: # integer - optional | default: 1 30 | memory: # integer - optional | default: 512 31 | huge_pages: # boolean - optional | default: False 32 | storage_pool: # string - optional | default: "" 33 | additional_storage_volumes: # list - optional 34 | - location: # string 35 | type: # string 36 | bus: # string 37 | device: # string 38 | nic_model_type: # string - optional | default: "" 39 | management_network_mac: # string - optional | default: "" 40 | 41 | internal_interfaces: # list - optional 42 | - local_port: # integer 43 | remote_guest: # string 44 | remote_port: # integer 45 | 46 | data_interfaces: # list - optional 47 | - local_port: # integer 48 | remote_guest: # string 49 | remote_port: # integer 50 | -------------------------------------------------------------------------------- /examples/guest-defaults.yml: -------------------------------------------------------------------------------- 1 | arista/veos: 2 | vagrant_box: 3 | version: "4.20.1F" 4 | ssh: 5 | username: "" 6 | password: "" 7 | insert_key: False 8 | synced_folder: 9 | enabled: False 10 | provider_config: 11 | nic_adapter_count: 8 12 | disk_bus: "ide" 13 | cpus: 2 14 | memory: 2048 15 | 16 | juniper/vsrx: 17 | vagrant_box: 18 | version: "18.1R1.9-packetmode" 19 | ssh: 20 | username: "" 21 | password: "" 22 | insert_key: False 23 | synced_folder: 24 | enabled: False 25 | provider_config: 26 | nic_adapter_count: 8 27 | disk_bus: "ide" 28 | cpus: 2 29 | memory: 4096 30 | -------------------------------------------------------------------------------- /examples/guests.yml: -------------------------------------------------------------------------------- 1 | --- 2 | sw01: 3 | vagrant_box: 4 | name: "arista/veos" 5 | version: "" 6 | url: "" 7 | provider: "libvirt" 8 | guest_type: "" 9 | throttle_cpu: 0 10 | ssh: 11 | username: "" 12 | password: "" 13 | insert_key: False 14 | synced_folder: 15 | enabled: False 16 | provider_config: 17 | random_hostname: False 18 | nic_adapter_count: 2 19 | disk_bus: "ide" 20 | cpus: 2 21 | memory: 2048 22 | huge_pages: False 23 | nic_model_type: "" 24 | management_network_mac: "" 25 | additional_storage_volumes: [] 26 | internal_interfaces: [] 27 | reserved_interfaces: [] 28 | data_interfaces: 29 | - local_port: 1 30 | remote_guest: "sw02" 31 | remote_port: 1 32 | - local_port: 2 33 | remote_guest: "sw02" 34 | remote_port: 2 35 | sw02: 36 | vagrant_box: 37 | name: "arista/veos" 38 | version: "" 39 | url: "" 40 | provider: "libvirt" 41 | guest_type: "" 42 | throttle_cpu: 0 43 | ssh: 44 | username: "" 45 | password: "" 46 | insert_key: False 47 | synced_folder: 48 | enabled: False 49 | provider_config: 50 | random_hostname: False 51 | nic_adapter_count: 2 52 | disk_bus: "ide" 53 | cpus: 2 54 | memory: 2048 55 | huge_pages: False 56 | nic_model_type: "" 57 | management_network_mac: "" 58 | additional_storage_volumes: [] 59 | internal_interfaces: [] 60 | reserved_interfaces: [] 61 | data_interfaces: 62 | - local_port: 1 63 | remote_guest: "sw01" 64 | remote_port: 1 65 | - local_port: 2 66 | remote_guest: "sw01" 67 | remote_port: 2 68 | -------------------------------------------------------------------------------- /grifter/__init__.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import logging 3 | 4 | # from .api import ( 5 | # generate_loopbacks, 6 | # add_blackhole_interfaces, 7 | # update_guest_data, 8 | # update_guest_interfaces, 9 | # generate_vagrant_file, 10 | # ) 11 | from .cli import ( 12 | cli, 13 | ) 14 | # from .loaders import ( 15 | # load_data, 16 | # render_from_template, 17 | # load_config_file, 18 | # ) 19 | # from .validators import ( 20 | # validate_required_keys, 21 | # validate_required_values, 22 | # ) 23 | 24 | handler = logging.StreamHandler(sys.stdout) 25 | formatter = logging.Formatter('%(levelname)s - %(message)s') 26 | handler.setFormatter(formatter) 27 | logger = logging.getLogger(__name__) 28 | logger.addHandler(handler) 29 | logger.setLevel(logging.ERROR) 30 | logger.addHandler(handler) 31 | -------------------------------------------------------------------------------- /grifter/api.py: -------------------------------------------------------------------------------- 1 | import os 2 | import copy 3 | import logging 4 | import random 5 | import pathlib 6 | import time 7 | 8 | from .utils import ( 9 | get_uuid, 10 | remove_duplicates, 11 | sort_nicely, 12 | dict_merge, 13 | ) 14 | from .custom_filters import ( 15 | explode_port, 16 | ) 17 | from .loaders import ( 18 | render_from_template, 19 | load_data, 20 | load_config_file, 21 | ) 22 | from .constants import ( 23 | TEMPLATES_DIR, 24 | BLACKHOLE_LOOPBACK_MAP, 25 | ALL_GUEST_DEFAULTS, 26 | DEFAULT_CONFIG_FILE, 27 | VAGRANTFILE_BACKUP_DIR, 28 | TIMESTAMP_FORMAT, 29 | ) 30 | 31 | logger = logging.getLogger(__name__) 32 | 33 | custom_filters = [ 34 | explode_port, 35 | ] 36 | 37 | 38 | def get_default_config(config=DEFAULT_CONFIG_FILE): 39 | return load_data(config) 40 | 41 | 42 | def generate_connection_strings(connections, dotfile=False): 43 | """ 44 | Generate a list of connection strings. The format of connections 45 | is the output from the 'generate_int_to_port_mappings' function. 46 | :param connections: List of dicts containing connection information ie: 47 | [{'local_guest': 'p1sw1', 48 | 'local_port': 'swp7', 49 | 'remote_guest': 'p1r7', 50 | 'remote_port': 'ge-0/0/9'}] 51 | :param dotfile: Generate a dotfile format undirected link 52 | :return: List of connection strings 53 | """ 54 | def make_link(x): 55 | if dotfile: 56 | return f'''"{x['local_guest']}":"{x['local_port']}" -- "{x['remote_guest']}":"{x['remote_port']}";''' 57 | else: 58 | return f"{x['local_guest']}-{x['local_port']} <--> {x['remote_guest']}-{x['remote_port']}" 59 | connections_list = [] 60 | for connection in connections: 61 | connections_list.append(make_link(connection)) 62 | return sort_nicely(connections_list) 63 | 64 | 65 | def int_to_port_map(name, offset, number_of_interfaces): 66 | """ 67 | Create a dict of interfaces to port maps 68 | :param name: Name of the base interface without the port number eg: swp 69 | :param offset: Number of the first interfaces eg: 0 or 1 70 | :param number_of_interfaces: Number of interfaces to create 71 | :return: Dictionary of interface to data port mappings 72 | """ 73 | mapping = {} 74 | for i in range(offset, number_of_interfaces + offset): 75 | mapping.update({i: f'{name}{i}'}) 76 | return mapping 77 | 78 | 79 | def generate_int_to_port_mappings(data): 80 | """ 81 | Generate a dictionary of interface to port mappings 82 | :param data: Dictionary with the following keys 83 | - data_interface_base 84 | - data_interface_offset 85 | - max_data_interfaces 86 | - internal_interfaces 87 | - reserved_interfaces 88 | :return: Dictionary of interface to port mappings 89 | """ 90 | if data.get('data_interface_base'): 91 | data_interfaces = int_to_port_map(data['data_interface_base'], 92 | data['data_interface_offset'], 93 | data['max_data_interfaces']) 94 | else: 95 | data_interfaces = {} 96 | 97 | if data.get('internal_interfaces'): 98 | internal_interfaces = int_to_port_map( 99 | 'internal-', 1, data['internal_interfaces']) 100 | else: 101 | internal_interfaces = {} 102 | 103 | if data.get('reserved_interfaces'): 104 | reserved_interfaces = int_to_port_map( 105 | 'reserved-', 1, data['reserved_interfaces']) 106 | else: 107 | reserved_interfaces = {} 108 | 109 | return { 110 | 'data_interfaces': data_interfaces, 111 | 'internal_interfaces': internal_interfaces, 112 | 'management_interface': data.get('management_interface', 'mgmt'), 113 | 'reserved_interfaces': reserved_interfaces, 114 | } 115 | 116 | 117 | def generate_guest_interface_mappings(config_file=DEFAULT_CONFIG_FILE): 118 | """ 119 | Create a guest type to interfaces port map dictionary 120 | :param config_file: Path to config file 121 | :return: Dictionary of guest type interface port mappings. 122 | """ 123 | config = get_default_config(config_file) 124 | mappings = {} 125 | for k, v in config['guest_config'].items(): 126 | mappings[k] = generate_int_to_port_mappings(config['guest_config'][k]) 127 | return mappings 128 | 129 | 130 | def generate_connections_list(guests, int_map, unique=False): 131 | """ 132 | Generate a map of interface connections. 133 | :param guests: Dict of guests data 134 | :param int_map: Dict of interface mappings 135 | :param unique: Remove duplicate connection between guests 136 | :return: List of Dicts of connections between guests 137 | """ 138 | dict_keys = ('local_guest', 'local_port', 'remote_guest', 'remote_port') 139 | connections = [] 140 | box_map = {k: v['vagrant_box']['name'] for k, v in guests.items()} 141 | for k, v in guests.items(): 142 | if v.get('data_interfaces'): 143 | for i in v['data_interfaces']: 144 | if not i['remote_guest'] == 'blackhole': 145 | local_box = box_map[k] 146 | local_int = int_map[local_box]['data_interfaces'][i['local_port']] 147 | remote_box = box_map[i['remote_guest']] 148 | remote_int = int_map[remote_box]['data_interfaces'][i['remote_port']] 149 | connections.append((k, local_int, i['remote_guest'], remote_int)) 150 | if unique: 151 | return [dict(zip(dict_keys, i)) for i in remove_duplicates(connections)] 152 | return [dict(zip(dict_keys, i)) for i in connections] 153 | 154 | 155 | def create_reserved_interfaces(num_reserved_interfaces): 156 | return [blackhole_interface_config(i) for i in range(1, num_reserved_interfaces + 1)] 157 | 158 | 159 | def generate_blackhole_interface_map(guests, int_map): 160 | blackhole_interfaces = {} 161 | for guest, data in guests.items(): 162 | blackhole_interfaces.update({guest: []}) 163 | guest_box = data['vagrant_box']['name'] 164 | for interface in data['data_interfaces']: 165 | if interface['remote_guest'] == 'blackhole': 166 | blackhole_interfaces[guest].append( 167 | int_map[guest_box]['data_interfaces'][interface['local_port']] 168 | ) 169 | return blackhole_interfaces 170 | 171 | 172 | def generate_loopbacks(guest_dict=None): 173 | """ 174 | Generate a dict of loopback addresses 175 | :param guest_dict: List of guests 176 | :return: Dictionary of loopback addresses 177 | """ 178 | if guest_dict is None or not isinstance(guest_dict, dict): 179 | raise AttributeError('guest_dict should contain a list of guests') 180 | elif not guest_dict: 181 | raise ValueError('dict of guests is empty') 182 | 183 | def generate_network(): 184 | net = f'127.{random.randint(2, 254)}.{random.randint(2, 254)}' 185 | if net == '127.6.6': 186 | generate_network() 187 | return net 188 | network = generate_network() 189 | guests = list(guest_dict.keys()) 190 | loopbacks = [f'{network}.{i}' for i in range(1, len(guests) + 1)] 191 | guest_to_loopback_map = dict(zip(guests, loopbacks)) 192 | 193 | return {**guest_to_loopback_map, **BLACKHOLE_LOOPBACK_MAP} 194 | 195 | 196 | def merge_user_config(): 197 | default_config = get_default_config() 198 | user_config = load_config_file('config.yml') 199 | if user_config: 200 | merged_config = dict_merge(copy.deepcopy(default_config), user_config) 201 | return merged_config 202 | return default_config 203 | 204 | 205 | def update_guest_data( 206 | guest_data, 207 | guest_defaults_file='guest-defaults.yml', 208 | all_guest_defaults=ALL_GUEST_DEFAULTS): 209 | """ 210 | Build data vars for guests. This function will take all_guest_defaults and merge in 211 | guest and guest group vars. 212 | :param guest_data: Dict of guest data 213 | :param guest_defaults_file: Guest defaults filename 214 | :param all_guest_defaults: All guest default data 215 | :return: Updated Dict of guest data 216 | """ 217 | 218 | guest_defaults = load_config_file(guest_defaults_file) 219 | 220 | new_guest_data = {} 221 | for guest, data in guest_data.items(): 222 | # Copy the default dict, that contains all top level variables 223 | # Group vars, then host vars will be merged into this dict 224 | default_context = copy.deepcopy(all_guest_defaults['guest_defaults']) 225 | if guest_defaults and data['vagrant_box'].get('name') in guest_defaults: 226 | # Merge group vars with host vars 227 | group_context = guest_defaults.get(data['vagrant_box']['name']) 228 | new_context = dict_merge(default_context, group_context) 229 | new_guest_data.update({guest: (dict_merge(new_context, data))}) 230 | else: 231 | # No group vars found, just merge host vars 232 | new_guest_data.update({guest: (dict_merge(default_context, data))}) 233 | return new_guest_data 234 | 235 | 236 | def blackhole_interface_config(port_number): 237 | return { 238 | 'local_port': port_number, 239 | 'remote_guest': 'blackhole', 240 | 'remote_port': 666 241 | } 242 | 243 | 244 | def add_blackhole_interfaces(offset, total_interfaces, interface_list): 245 | """ 246 | Adds blackhole interfaces to host data by inserting a 247 | dict of blackhole config in the correct interface index position. 248 | :param offset: Interface numbering offset. 249 | :param total_interfaces: Total number of interfaces. 250 | :param interface_list: List of interface dicts to update. 251 | :return: New list of interface dicts. 252 | """ 253 | if total_interfaces == len(interface_list): 254 | return interface_list 255 | 256 | updated_interface_list = [] 257 | for i in range(offset, total_interfaces + offset): 258 | for interface in interface_list: 259 | try: 260 | if interface['local_port'] == i: 261 | updated_interface_list.append(interface) 262 | # When a port number is matched terminate the 263 | # loop to prevent adding unnecessary interfaces. 264 | break 265 | except IndexError: 266 | # Have run out of interfaces in the original list. 267 | # Blackhole interfaces will populate the rest of the list 268 | updated_interface_list.append(blackhole_interface_config(i)) 269 | else: 270 | # No match on the port number so add a blackhole interface. 271 | updated_interface_list.append(blackhole_interface_config(i)) 272 | 273 | return updated_interface_list 274 | 275 | 276 | def update_reserved_interfaces(guest_data, config): 277 | updated_guest_dict = {} 278 | for guest, data in guest_data.items(): 279 | guest_box = data['vagrant_box']['name'] 280 | num_reserved_interfaces = config['guest_config'][guest_box]['reserved_interfaces'] 281 | if not num_reserved_interfaces: 282 | updated_guest_dict.update({guest: data}) 283 | else: 284 | data['reserved_interfaces'] = create_reserved_interfaces(num_reserved_interfaces) 285 | updated_guest_dict.update({guest: data}) 286 | return updated_guest_dict 287 | 288 | 289 | def update_guest_interfaces(guest_data, config): 290 | """ 291 | Entrypoint to updating guest data parameters. 292 | :param guest_data: List of host dicts. 293 | :return: New list of host dicts. 294 | """ 295 | updated_guest_dict = {} 296 | for guest, data in guest_data.items(): 297 | guest_box = data['vagrant_box']['name'] 298 | if not data.get('data_interfaces'): 299 | updated_guest_dict.update({guest: data}) 300 | else: 301 | updated_interfaces = add_blackhole_interfaces( 302 | config['guest_config'][guest_box]['data_interface_offset'], 303 | data['provider_config']['nic_adapter_count'], 304 | copy.deepcopy(data['data_interfaces']) 305 | ) 306 | data['data_interfaces'] = updated_interfaces 307 | updated_guest_dict.update({guest: data}) 308 | 309 | return updated_guest_dict 310 | 311 | 312 | def update_guest_additional_storage(guest_data): 313 | """ 314 | Add storage volume size to additional storage volumes 315 | :param guest_data: List of host dicts. 316 | :return: New dict of guest data. 317 | """ 318 | updated_guest_dict = {} 319 | for guest, data in guest_data.items(): 320 | if not data['provider_config'].get('additional_storage_volumes'): 321 | updated_guest_dict.update({guest: data}) 322 | else: 323 | for volume in data['provider_config']['additional_storage_volumes']: 324 | try: 325 | volume['size'] = os.path.getsize(volume['location']) 326 | except OSError: 327 | raise OSError(f'No such file: {volume["location"]}') 328 | updated_guest_dict.update({guest: data}) 329 | 330 | return updated_guest_dict 331 | 332 | 333 | def generate_vagrant_file( 334 | guest_data, loopbacks, template_name='guest.j2', 335 | template_directory=f'{TEMPLATES_DIR}/' 336 | ): 337 | """ 338 | Generate a Vagrantfile in the current directory. 339 | :param guest_data: Dictionary of data to apply to Jinja2 template. 340 | :param loopbacks: Dictionary of loopback addresses. 341 | :param template_name: Name of Jinja2 template 342 | :param template_directory: Template directory location 343 | """ 344 | time_now = time.strftime(TIMESTAMP_FORMAT) 345 | current_vagrantfile = pathlib.Path('Vagrantfile') 346 | if current_vagrantfile.exists(): 347 | backup_dir = pathlib.Path(VAGRANTFILE_BACKUP_DIR) 348 | if not backup_dir.exists(): 349 | backup_dir.mkdir() 350 | dest_file = pathlib.Path(f'{VAGRANTFILE_BACKUP_DIR}/Vagrantfile-{time_now}') 351 | src_file = pathlib.Path('Vagrantfile') 352 | src_file.replace(dest_file) 353 | 354 | interface_map = generate_guest_interface_mappings() 355 | blackhole_interface_map = generate_blackhole_interface_map(guest_data, interface_map) 356 | with open('Vagrantfile', 'w') as f: 357 | vagrantfile = render_from_template( 358 | template_name=template_name, 359 | template_directory=template_directory, 360 | custom_filters=custom_filters, 361 | guests=guest_data, 362 | loopbacks=loopbacks, 363 | interface_mappings=interface_map, 364 | domain_uuid=get_uuid(), 365 | creation_time=time_now, 366 | blackhole_interfaces=blackhole_interface_map, 367 | ) 368 | f.write(vagrantfile) 369 | logger.info('Vagrantfile created') 370 | 371 | 372 | def generate_dotfile(connections_list): 373 | """ 374 | Generate undirected dotfile. 375 | :param connections_list: List of connections strings 376 | """ 377 | with open('topology.dot', 'w') as f: 378 | dotfile = render_from_template( 379 | template_name='topology.dot.j2', 380 | template_directory=f'{TEMPLATES_DIR}/', 381 | connections_list=connections_list, 382 | ) 383 | f.write(dotfile) 384 | logger.info('topology.dot file created') 385 | -------------------------------------------------------------------------------- /grifter/cli.py: -------------------------------------------------------------------------------- 1 | import click 2 | import sys 3 | 4 | from .constants import ( 5 | GUESTS_EXAMPLE_FILE, 6 | GROUPS_EXAMPLE_FILE, 7 | ) 8 | from .loaders import ( 9 | load_data, 10 | load_config_file, 11 | ) 12 | from .api import ( 13 | generate_loopbacks, 14 | update_guest_interfaces, 15 | generate_vagrant_file, 16 | update_guest_data, 17 | update_guest_additional_storage, 18 | generate_guest_interface_mappings, 19 | update_reserved_interfaces, 20 | generate_connections_list, 21 | merge_user_config, 22 | generate_dotfile, 23 | generate_connection_strings, 24 | ) 25 | from .validators import ( 26 | validate_guests_in_guest_config, 27 | validate_guest_interfaces, 28 | validate_data, 29 | validate_config, 30 | ) 31 | 32 | interface_mappings = generate_guest_interface_mappings() 33 | 34 | 35 | def validate_guest_config(config): 36 | errors = validate_config(config) 37 | if errors: 38 | display_errors(errors) 39 | 40 | 41 | def validate_guest_data(guest_data, config): 42 | """ 43 | Validate and update guest data if validation is successful. 44 | :param guest_data: Dict of guest data. 45 | :param config: Dict of config data. 46 | :return: Dict of updated data. 47 | """ 48 | guest_defaults = load_config_file('guest-defaults.yml') 49 | errors = [] 50 | 51 | guest_errors = validate_data(guest_data) 52 | if guest_errors: 53 | errors += guest_errors 54 | 55 | if guest_defaults: 56 | guest_defaults_errors = validate_data(guest_defaults, guest_default_data=True) 57 | if guest_defaults_errors: 58 | errors += guest_defaults_errors 59 | 60 | if not errors: 61 | merged_data = update_guest_data(guest_data) 62 | update_guest_interfaces(merged_data, config) 63 | update_reserved_interfaces(merged_data, config) 64 | update_guest_additional_storage(merged_data) 65 | 66 | try: 67 | validate_guests_in_guest_config(merged_data, config) 68 | validate_guest_interfaces(merged_data, config, interface_mappings) 69 | except AttributeError as e: 70 | errors.append(e) 71 | if not errors: 72 | return merged_data 73 | if errors: 74 | display_errors(errors) 75 | 76 | 77 | def load_data_file(datafile): 78 | """ 79 | Load data file. 80 | :param datafile: Name of datafile 81 | :return: Dict of guest data. 82 | """ 83 | try: 84 | guest_data = load_data(datafile) 85 | except FileNotFoundError: 86 | click.echo(f'Datafile: {datafile} not found.') 87 | sys.exit(1) 88 | return guest_data 89 | 90 | 91 | def display_errors(errors_list): 92 | """ 93 | Outputs a list of errors 94 | :param errors_list: List of errors 95 | """ 96 | for error in errors_list: 97 | click.echo(error) 98 | sys.exit(1) 99 | 100 | 101 | def display_connections(connections, guest=''): 102 | """ 103 | Output a list of connections 104 | :param connections: List of dicts containing connection information ie: 105 | [{'local_guest': 'p1sw1', 106 | 'local_port': 'swp7', 107 | 'remote_guest': 'p1r7', 108 | 'remote_port': 'ge-0/0/9'}] 109 | :param guest: Display connections for guest. 110 | """ 111 | if guest: 112 | guest_connections = [i for i in connections if i['local_guest'] == guest] 113 | connections_list = generate_connection_strings(guest_connections) 114 | else: 115 | connections_list = generate_connection_strings(connections) 116 | 117 | for i in connections_list: 118 | click.echo(i) 119 | 120 | 121 | @click.group(context_settings={'help_option_names': ['-h', '--help']}) 122 | @click.version_option(version='0.2.11') 123 | def cli(): 124 | """Create a Vagrantfile from a YAML data input file.""" 125 | pass 126 | 127 | 128 | @cli.command(help=''' 129 | Create a Vagrantfile. 130 | 131 | DATAFILE - Name of DATAFILE. 132 | ''') 133 | @click.argument('datafile') 134 | def create(datafile): 135 | """Create a Vagrantfile.""" 136 | guest_config = merge_user_config() 137 | validate_guest_config(guest_config) 138 | guest_data = load_data_file(datafile) 139 | validated_guest_data = validate_guest_data(guest_data, guest_config) 140 | loopbacks = generate_loopbacks(guest_data) 141 | generate_vagrant_file(validated_guest_data, loopbacks) 142 | unsorted_connections = generate_connections_list(validated_guest_data, interface_mappings, 143 | unique=True) 144 | connections_list = generate_connection_strings(unsorted_connections, dotfile=True) 145 | generate_dotfile(connections_list) 146 | 147 | 148 | @cli.command(help='Print example file declaration.') 149 | @click.option('--guest', is_flag=True) 150 | @click.option('--group', is_flag=True) 151 | def example(guest, group): 152 | """Display example variable file""" 153 | if guest: 154 | with open(GUESTS_EXAMPLE_FILE, 'r') as f: 155 | click.echo(f.read()) 156 | if group: 157 | with open(GROUPS_EXAMPLE_FILE, 'r') as f: 158 | click.echo(f.read()) 159 | 160 | 161 | @cli.command(help=''' 162 | Show device to device connections. 163 | 164 | DATAFILE - Name of DATAFILE. 165 | ''') 166 | @click.argument('datafile') 167 | @click.argument('guest', default='') 168 | @click.option('--unique', is_flag=True, default=False, help='Remove duplicate connections.') 169 | def connections(datafile, guest, unique): 170 | """Show device to device connections.""" 171 | guest_config = merge_user_config() 172 | validate_guest_config(guest_config) 173 | guest_data = load_data_file(datafile) 174 | validated_guest_data = validate_guest_data(guest_data, guest_config) 175 | connections_list = generate_connections_list(validated_guest_data, interface_mappings, unique) 176 | display_connections(connections_list, guest) 177 | 178 | 179 | @cli.command(help=''' 180 | Create topology.dot file. 181 | 182 | DATAFILE - Name of DATAFILE. 183 | ''') 184 | @click.argument('datafile') 185 | def dotfile(datafile): 186 | """Generate undirected dotfile.""" 187 | guest_config = merge_user_config() 188 | validate_guest_config(guest_config) 189 | guest_data = load_data_file(datafile) 190 | validated_guest_data = validate_guest_data(guest_data, guest_config) 191 | unsorted_connections = generate_connections_list(validated_guest_data, interface_mappings, unique=True) 192 | connections_list = generate_connection_strings(unsorted_connections, dotfile=True) 193 | generate_dotfile(connections_list) 194 | -------------------------------------------------------------------------------- /grifter/config.yml: -------------------------------------------------------------------------------- 1 | --- 2 | guest_pairs: 3 | juniper/vmx: 4 | child: "juniper/vmx-vcp" 5 | parent: "juniper/vmx-vfp" 6 | 7 | juniper/vqfx: 8 | child: "juniper/vqfx-pfe" 9 | parent: "juniper/vqfx-re" 10 | 11 | guest_config: 12 | arista/veos: 13 | data_interface_base: "eth" 14 | data_interface_offset: 1 15 | internal_interfaces: 0 16 | max_data_interfaces: 24 17 | management_interface: "ma1" 18 | reserved_interfaces: 0 19 | 20 | cisco/csr1000v: 21 | data_interface_base: "ge" 22 | data_interface_offset: 2 23 | internal_interfaces: 0 24 | max_data_interfaces: 25 25 | management_interface: "ge1" 26 | reserved_interfaces: 0 27 | 28 | cisco/iosv: 29 | data_interface_base: "ge0/" 30 | data_interface_offset: 1 31 | internal_interfaces: 0 32 | max_data_interfaces: 7 33 | management_interface: "ge0/0" 34 | reserved_interfaces: 0 35 | 36 | cisco/xrv: 37 | data_interface_base: "ge0/0/0/" 38 | data_interface_offset: 0 39 | internal_interfaces: 0 40 | max_data_interfaces: 96 41 | management_interface: "me0" 42 | reserved_interfaces: 0 43 | 44 | CumulusCommunity/cumulus-vx: 45 | data_interface_base: "swp" 46 | data_interface_offset: 1 47 | internal_interfaces: 0 48 | max_data_interfaces: 96 49 | management_interface: "eth0" 50 | reserved_interfaces: 0 51 | 52 | juniper/vmx-vcp: 53 | data_interface_base: "NA" 54 | data_interface_offset: 0 55 | internal_interfaces: 1 56 | max_data_interfaces: 0 57 | management_interface: "fxp0.0" 58 | reserved_interfaces: 0 59 | 60 | juniper/vmx-vfp: 61 | data_interface_base: "ge-0/0/" 62 | data_interface_offset: 0 63 | internal_interfaces: 1 64 | max_data_interfaces: 10 65 | management_interface: "ext" 66 | reserved_interfaces: 0 67 | 68 | juniper/vqfx-pfe: 69 | data_interface_base: "NA" 70 | data_interface_offset: 0 71 | internal_interfaces: 1 72 | max_data_interfaces: 0 73 | management_interface: "eth1" 74 | reserved_interfaces: 0 75 | 76 | juniper/vqfx-re: 77 | data_interface_base: "xe-0/0/" 78 | data_interface_offset: 0 79 | internal_interfaces: 1 80 | max_data_interfaces: 12 81 | management_interface: "em0.0" 82 | reserved_interfaces: 1 83 | 84 | juniper/vsrx: 85 | data_interface_base: "ge-0/0/" 86 | data_interface_offset: 0 87 | internal_interfaces: 0 88 | max_data_interfaces: 16 89 | management_interface: "fxp0.0" 90 | reserved_interfaces: 0 91 | 92 | juniper/vsrx-packetmode: 93 | data_interface_base: "ge-0/0/" 94 | data_interface_offset: 0 95 | internal_interfaces: 0 96 | max_data_interfaces: 16 97 | management_interface: "fxp0.0" 98 | reserved_interfaces: 0 99 | 100 | centos/7: 101 | data_interface_base: "eth" 102 | data_interface_offset: 1 103 | internal_interfaces: 0 104 | max_data_interfaces: 8 105 | management_interface: "eth0" 106 | reserved_interfaces: 0 107 | 108 | generic/ubuntu1804: 109 | data_interface_base: "eth" 110 | data_interface_offset: 1 111 | internal_interfaces: 0 112 | max_data_interfaces: 8 113 | management_interface: "eth0" 114 | reserved_interfaces: 0 115 | 116 | opensuse/openSUSE-15.0-x86_64: 117 | data_interface_base: "eth" 118 | data_interface_offset: 1 119 | internal_interfaces: 0 120 | max_data_interfaces: 8 121 | management_interface: "eth0" 122 | reserved_interfaces: 0 123 | 124 | unknown/unknown: 125 | data_interface_base: "int" 126 | data_interface_offset: 1 127 | internal_interfaces: 0 128 | max_data_interfaces: 8 129 | management_interface: "int0" 130 | reserved_interfaces: 0 131 | -------------------------------------------------------------------------------- /grifter/constants.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from .loaders import load_data 4 | 5 | BASE_DIR = os.path.join(os.path.dirname(__file__)) 6 | 7 | TEMPLATES_DIR = os.path.join(BASE_DIR, 'templates') 8 | 9 | VAGRANTFILE_BACKUP_DIR = 'vagrantfile-backup' 10 | 11 | EXAMPLES_DIR = os.path.join(BASE_DIR, 'examples') 12 | GROUPS_EXAMPLE_FILE = f'{EXAMPLES_DIR}/groups-example.yml' 13 | GUESTS_EXAMPLE_FILE = f'{EXAMPLES_DIR}/guests-example.yml' 14 | 15 | DEFAULT_CONFIG_FILE = f'{BASE_DIR}/config.yml' 16 | GUEST_DEFAULTS_FILE = f'{BASE_DIR}/defaults.yml' 17 | ALL_GUEST_DEFAULTS = load_data(GUEST_DEFAULTS_FILE) 18 | 19 | TESTS_DIR = os.path.join(BASE_DIR, '../tests') 20 | 21 | SCHEMAS_DIR = os.path.join(BASE_DIR, 'schemas') 22 | GUEST_SCHEMA_FILE = f'{SCHEMAS_DIR}/guest-schema.yml' 23 | GUEST_CONFIG_SCHEMA = f'{SCHEMAS_DIR}/guest-config-schema.yml' 24 | GUEST_PAIRS_SCHEMA = f'{SCHEMAS_DIR}/guest-pairs-schema.yml' 25 | 26 | BLACKHOLE_LOOPBACK_MAP = {'blackhole': '127.6.6.6'} 27 | 28 | DATA_INTERFACES_BASE_PORT = 10000 29 | INTERNAL_INTERFACES_BASE_PORT = 11000 30 | RESERVED_INTERFACES_BASE_PORT = 12000 31 | 32 | TIMESTAMP_FORMAT = '%Y-%m-%d--%H-%M-%S' 33 | -------------------------------------------------------------------------------- /grifter/custom_filters.py: -------------------------------------------------------------------------------- 1 | def explode_port(port, base_port=10000): 2 | """ 3 | Create a high port number > 10000 for use with UDP tunnels 4 | :param port: port number to add to the base port 5 | :param base_port: Port number above 10000 6 | :return: Int port number 7 | """ 8 | port_error = 'port must be and integer from 0 to 99 or 666' 9 | 10 | if not isinstance(port, int): 11 | raise AttributeError(port_error) 12 | 13 | if port == 666: 14 | return base_port + port 15 | 16 | elif not 0 <= port < 100: 17 | raise AttributeError(port_error) 18 | 19 | return base_port + port 20 | -------------------------------------------------------------------------------- /grifter/defaults.yml: -------------------------------------------------------------------------------- 1 | guest_defaults: 2 | vagrant_box: 3 | name: "" 4 | version: "" 5 | url: "" 6 | provider: "libvirt" 7 | guest_type: "" 8 | boot_timeout: 0 9 | throttle_cpu: 0 10 | ssh: 11 | username: "" 12 | password: "" 13 | insert_key: False 14 | synced_folder: 15 | enabled: False 16 | provider_config: 17 | random_hostname: False 18 | nic_adapter_count: 0 19 | disk_bus: "" 20 | cpus: 1 21 | memory: 512 22 | huge_pages: False 23 | storage_pool: "" 24 | additional_storage_volumes: [] 25 | nic_model_type: "" 26 | management_network_mac: "" 27 | data_interfaces: [] 28 | internal_interfaces: [] 29 | reserved_interfaces: [] 30 | 31 | guest_config_defaults: 32 | data_interface_base: "" 33 | data_interface_offset: 0 34 | internal_interfaces: 0 35 | max_data_interfaces: 8 36 | management_interface: "mgmt" 37 | reserved_interfaces: 0 38 | -------------------------------------------------------------------------------- /grifter/examples/groups-example.yml: -------------------------------------------------------------------------------- 1 | arista/veos: 2 | vagrant_box: 3 | version: "4.20.1F" 4 | ssh: 5 | insert_key: False 6 | synced_folder: 7 | enabled: False 8 | provider_config: 9 | nic_adapter_count: 8 10 | disk_bus: "ide" 11 | cpus: 2 12 | memory: 2048 13 | 14 | juniper/vsrx: 15 | vagrant_box: 16 | version: "18.1R1.9-packetmode" 17 | url: "http://some-server/some-dir/srx.box" 18 | ssh: 19 | insert_key: False 20 | synced_folder: 21 | enabled: False 22 | provider_config: 23 | nic_adapter_count: 8 24 | disk_bus: "ide" 25 | cpus: 2 26 | memory: 4096 -------------------------------------------------------------------------------- /grifter/examples/guest-defaults.yml: -------------------------------------------------------------------------------- 1 | # Network VMs 2 | arista/veos: 3 | vagrant_box: 4 | version: "4.20.1F" 5 | guest_type: "tinycore" 6 | ssh: 7 | insert_key: False 8 | synced_folder: 9 | enabled: False 10 | provider_config: 11 | driver: "kvm" 12 | cpus: 2 13 | memory: 2048 14 | disk_bus: "ide" 15 | 16 | cisco/xrv: 17 | vagrant_box: 18 | version: "6.1.3" 19 | guest_type: "tinycore" 20 | boot_timeout: 180 21 | ssh: 22 | username: "vagrant" 23 | password: "vagrant" 24 | insert_key: False 25 | synced_folder: 26 | enabled: False 27 | provider_config: 28 | driver: "kvm" 29 | cpus: 1 30 | memory: 4096 31 | disk_bus: "ide" 32 | nic_model_type: "e1000" 33 | 34 | cisco/iosv: 35 | vagrant_box: 36 | version: "15.6-1-T" 37 | guest_type: "tinycore" 38 | ssh: 39 | insert_key: False 40 | synced_folder: 41 | enabled: False 42 | provider_config: 43 | driver: "kvm" 44 | cpus: 1 45 | memory: 512 46 | nic_model_type: "e1000" 47 | 48 | cisco/iosv-l2: 49 | vagrant_box: 50 | version: "15.2" 51 | guest_type: "tinycore" 52 | ssh: 53 | insert_key: False 54 | synced_folder: 55 | enabled: False 56 | provider_config: 57 | driver: "kvm" 58 | cpus: 1 59 | memory: 1024 60 | nic_model_type: "e1000" 61 | 62 | cisco/csr1000v: 63 | vagrant_box: 64 | version: "16.11.01a" 65 | guest_type: "tinycore" 66 | ssh: 67 | insert_key: False 68 | synced_folder: 69 | enabled: False 70 | provider_config: 71 | nic_adapter_count: 8 72 | cpus: 2 73 | memory: 4096 74 | nic_model_type: "vmxnet3" 75 | 76 | CumulusCommunity/cumulus-vx: 77 | vagrant_box: 78 | version: "3.7.1" 79 | provider: "libvirt" 80 | ssh: 81 | insert_key: False 82 | synced_folder: 83 | enabled: False 84 | provider_config: 85 | nic_adapter_count: 52 86 | cpus: 1 87 | memory: 1024 88 | 89 | juniper/vqfx-re: 90 | vagrant_box: 91 | version: "17.4R1.16" 92 | provider: "libvirt" 93 | guest_type: "tinycore" 94 | ssh: 95 | insert_key: False 96 | synced_folder: 97 | enabled: False 98 | provider_config: 99 | nic_adapter_count: 11 100 | nic_model_type: "e1000" 101 | disk_bus: "ide" 102 | cpus: 1 103 | memory: 1024 104 | 105 | juniper/vqfx-pfe: 106 | vagrant_box: 107 | version: "17.4R1.16" 108 | provider: "libvirt" 109 | guest_type: "tinycore" 110 | ssh: 111 | insert_key: False 112 | synced_folder: 113 | enabled: False 114 | provider_config: 115 | nic_adapter_count: 1 116 | nic_model_type: "e1000" 117 | disk_bus: "ide" 118 | cpus: 1 119 | memory: 2048 120 | 121 | juniper/vmx-vcp: 122 | vagrant_box: 123 | version: "18.2R1.9" 124 | provider: "libvirt" 125 | guest_type: "tinycore" 126 | ssh: 127 | insert_key: False 128 | synced_folder: 129 | enabled: False 130 | provider_config: 131 | nic_adapter_count: 1 132 | disk_bus: "ide" 133 | cpus: 1 134 | memory: 1024 135 | additional_storage_volumes: 136 | - location: "/opt/vagrant/storage/vmx-vcp-hdb-18.2R1.9-base.qcow2" 137 | type: "qcow2" 138 | bus: "ide" 139 | device: "hdb" 140 | - location: "/opt/vagrant/storage/vmx-vcp-hdc-18.2R1.9-base.img" 141 | type: "raw" 142 | bus: "ide" 143 | device: "hdc" 144 | 145 | juniper/vmx-vfp: 146 | vagrant_box: 147 | version: "18.2R1.9" 148 | provider: "libvirt" 149 | guest_type: "tinycore" 150 | ssh: 151 | username: "root" 152 | insert_key: False 153 | synced_folder: 154 | enabled: False 155 | provider_config: 156 | nic_adapter_count: 11 157 | disk_bus: "ide" 158 | cpus: 3 159 | memory: 4096 160 | 161 | juniper/vsrx-packetmode: 162 | vagrant_box: 163 | version: "18.3R1-S1.4" 164 | provider: "libvirt" 165 | guest_type: "tinycore" 166 | ssh: 167 | insert_key: False 168 | synced_folder: 169 | enabled: False 170 | provider_config: 171 | nic_adapter_count: 2 172 | disk_bus: "ide" 173 | cpus: 2 174 | memory: 4096 175 | 176 | # Linux VMs 177 | centos/7: 178 | vagrant_box: 179 | version: "1811.02" 180 | provider: "libvirt" 181 | ssh: 182 | insert_key: False 183 | synced_folder: 184 | enabled: False 185 | provider_config: 186 | nic_adapter_count: 2 187 | cpus: 1 188 | memory: 1024 189 | 190 | generic/ubuntu1804: 191 | vagrant_box: 192 | version: "1.8.52" 193 | provider: "libvirt" 194 | ssh: 195 | insert_key: False 196 | synced_folder: 197 | enabled: False 198 | provider_config: 199 | nic_adapter_count: 2 200 | cpus: 1 201 | memory: 1024 202 | 203 | opensuse/openSUSE-15.0-x86_64: 204 | vagrant_box: 205 | version: "1.0.6.20181025" 206 | provider: "libvirt" 207 | ssh: 208 | insert_key: False 209 | synced_folder: 210 | enabled: False 211 | provider_config: 212 | nic_adapter_count: 2 213 | cpus: 1 214 | memory: 1024 215 | -------------------------------------------------------------------------------- /grifter/examples/guests-example.yml: -------------------------------------------------------------------------------- 1 | --- 2 | sw01: 3 | vagrant_box: 4 | name: "arista/veos" 5 | version: "" 6 | url: "" 7 | provider: "libvirt" 8 | guest_type: "" 9 | ssh: 10 | username: "" 11 | password: "" 12 | insert_key: False 13 | synced_folder: 14 | enabled: False 15 | provider_config: 16 | nic_adapter_count: 2 17 | disk_bus: "ide" 18 | cpus: 2 19 | memory: 2048 20 | nic_model_type: "" 21 | management_network_mac: "" 22 | data_interfaces: 23 | - local_port: 1 24 | remote_guest: "sw02" 25 | remote_port: 1 26 | - local_port: 2 27 | remote_guest: "sw02" 28 | remote_port: 2 29 | -------------------------------------------------------------------------------- /grifter/examples/veos-guests.yml: -------------------------------------------------------------------------------- 1 | sw01: 2 | vagrant_box: 3 | name: "arista/veos" 4 | 5 | provider_config: 6 | nic_adapter_count: 2 7 | 8 | data_interfaces: 9 | - local_port: 1 10 | remote_guest: "sw02" 11 | remote_port: 1 12 | - local_port: 2 13 | remote_guest: "sw02" 14 | remote_port: 2 15 | 16 | sw02: 17 | vagrant_box: 18 | name: "arista/veos" 19 | 20 | provider_config: 21 | nic_adapter_count: 2 22 | 23 | data_interfaces: 24 | - local_port: 1 25 | remote_guest: "sw01" 26 | remote_port: 1 27 | - local_port: 2 28 | remote_guest: "sw01" 29 | remote_port: 2 30 | -------------------------------------------------------------------------------- /grifter/examples/vmx-guests.yml: -------------------------------------------------------------------------------- 1 | r01: 2 | vagrant_box: 3 | name: "juniper/vmx-vcp" 4 | 5 | provider_config: 6 | nic_adapter_count: 1 7 | 8 | internal_interfaces: 9 | - local_port: 1 10 | remote_guest: "r01-vfp" 11 | remote_port: 1 12 | 13 | r01-vfp: 14 | vagrant_box: 15 | name: "juniper/vmx-vfp" 16 | 17 | provider_config: 18 | nic_adapter_count: 10 19 | 20 | internal_interfaces: 21 | - local_port: 1 22 | remote_guest: "r01" 23 | remote_port: 1 24 | 25 | data_interfaces: 26 | - local_port: 1 27 | remote_guest: "r02-vfp" 28 | remote_port: 1 29 | - local_port: 2 30 | remote_guest: "r02-vfp" 31 | remote_port: 2 32 | 33 | r02: 34 | vagrant_box: 35 | name: "juniper/vmx-vcp" 36 | 37 | provider_config: 38 | nic_adapter_count: 1 39 | 40 | internal_interfaces: 41 | - local_port: 1 42 | remote_guest: "r02-vfp" 43 | remote_port: 1 44 | 45 | r02-vfp: 46 | vagrant_box: 47 | name: "juniper/vmx-vfp" 48 | 49 | provider_config: 50 | nic_adapter_count: 10 51 | 52 | internal_interfaces: 53 | - local_port: 1 54 | remote_guest: "r02" 55 | remote_port: 1 56 | 57 | data_interfaces: 58 | - local_port: 1 59 | remote_guest: "r01-vfp" 60 | remote_port: 1 61 | - local_port: 2 62 | remote_guest: "r01-vfp" 63 | remote_port: 2 64 | -------------------------------------------------------------------------------- /grifter/examples/vqfx-guests.yml: -------------------------------------------------------------------------------- 1 | sw01: 2 | vagrant_box: 3 | name: "juniper/vqfx-re" 4 | 5 | provider_config: 6 | nic_adapter_count: 12 7 | 8 | internal_interfaces: 9 | - local_port: 1 10 | remote_guest: "sw01-pfe" 11 | remote_port: 1 12 | 13 | data_interfaces: 14 | - local_port: 1 15 | remote_guest: "sw02" 16 | remote_port: 1 17 | - local_port: 2 18 | remote_guest: "sw02" 19 | remote_port: 2 20 | 21 | sw01-pfe: 22 | vagrant_box: 23 | name: "juniper/vqfx-pfe" 24 | 25 | provider_config: 26 | nic_adapter_count: 1 27 | 28 | internal_interfaces: 29 | - local_port: 1 30 | remote_guest: "sw01" 31 | remote_port: 1 32 | 33 | 34 | sw02: 35 | vagrant_box: 36 | name: "juniper/vqfx-re" 37 | 38 | provider_config: 39 | nic_adapter_count: 12 40 | 41 | internal_interfaces: 42 | - local_port: 1 43 | remote_guest: "sw02-pfe" 44 | remote_port: 1 45 | 46 | data_interfaces: 47 | - local_port: 1 48 | remote_guest: "sw01" 49 | remote_port: 1 50 | - local_port: 2 51 | remote_guest: "sw01" 52 | remote_port: 2 53 | 54 | sw02-pfe: 55 | vagrant_box: 56 | name: "juniper/vqfx-pfe" 57 | 58 | provider_config: 59 | nic_adapter_count: 1 60 | 61 | internal_interfaces: 62 | - local_port: 1 63 | remote_guest: "sw02" 64 | remote_port: 1 65 | -------------------------------------------------------------------------------- /grifter/examples/vsrx-guests.yml: -------------------------------------------------------------------------------- 1 | fw01: 2 | vagrant_box: 3 | name: "juniper/vsrx" 4 | 5 | provider_config: 6 | nic_adapter_count: 8 7 | 8 | data_interfaces: 9 | - local_port: 1 10 | remote_guest: "fw02" 11 | remote_port: 1 12 | 13 | fw02: 14 | vagrant_box: 15 | name: "juniper/vsrx" 16 | 17 | provider_config: 18 | nic_adapter_count: 8 19 | 20 | data_interfaces: 21 | - local_port: 1 22 | remote_guest: "fw01" 23 | remote_port: 1 24 | -------------------------------------------------------------------------------- /grifter/loaders.py: -------------------------------------------------------------------------------- 1 | import yaml 2 | import json 3 | import logging 4 | import os 5 | 6 | from jinja2 import Environment, FileSystemLoader 7 | 8 | logger = logging.getLogger(__name__) 9 | 10 | USER_HOME = os.path.expanduser('~') 11 | DEFAULT_CONFIG_DIRS = [ 12 | '/opt/grifter', 13 | f'{USER_HOME}/.grifter', 14 | '.', 15 | ] 16 | 17 | 18 | def render_from_template( 19 | template_name, template_directory, custom_filters=None, 20 | trim_blocks=True, lstrip_blocks=True, **kwargs 21 | ): 22 | """ 23 | Render template with custom filters 24 | """ 25 | loader = FileSystemLoader(template_directory) 26 | env = Environment(loader=loader, trim_blocks=trim_blocks, lstrip_blocks=lstrip_blocks) 27 | 28 | if custom_filters is not None: 29 | for custom_filter in custom_filters: 30 | env.filters[custom_filter.__name__] = custom_filter 31 | 32 | template = env.get_template(template_name) 33 | return template.render(**kwargs) 34 | 35 | 36 | def load_data(location, data_type='yaml'): 37 | """ 38 | Load data file from location 39 | :param location: Location of YAML file 40 | :param data_type: Type of source data YAML|JSON 41 | :return: Dictionary of data 42 | """ 43 | valid_types = ['yaml', 'json'] 44 | if data_type.lower() not in valid_types: 45 | raise AttributeError('Valid data types are yaml or json') 46 | 47 | with open(location, 'r') as f: 48 | if data_type.lower() == 'yaml': 49 | return yaml.safe_load(f) 50 | elif data_type.lower() == 'json': 51 | return json.load(f) 52 | 53 | 54 | def load_config_file(config_file): 55 | """ 56 | Load config_file from the following locations top to 57 | bottom least to most preferred. Value are overwritten not merged: 58 | - /opt/grifter/ 59 | - ~/.grifter/ 60 | - ./ 61 | :param config_file: Guest defaults filename 62 | :return: Dict of guest default data or empty dict 63 | """ 64 | config = {} 65 | for directory in DEFAULT_CONFIG_DIRS: 66 | try: 67 | config = load_data(f'{directory}/{config_file}') 68 | logger.info(f'File: "{directory}/{config_file}" found') 69 | except FileNotFoundError: 70 | logger.warning(f'File: "{directory}/{config_file}" not found') 71 | except PermissionError: 72 | logger.error(f'File: "{directory}/{config_file}" permission denied') 73 | 74 | return config 75 | -------------------------------------------------------------------------------- /grifter/schemas/guest-config-schema.yml: -------------------------------------------------------------------------------- 1 | --- 2 | data_interface_base: 3 | type: "string" 4 | required: True 5 | empty: False 6 | 7 | data_interface_offset: 8 | type: "integer" 9 | required: True 10 | default: 0 11 | 12 | max_data_interfaces: 13 | type: "integer" 14 | required: True 15 | default: 0 16 | 17 | management_interface: 18 | type: "string" 19 | required: True 20 | empty: True 21 | 22 | reserved_interfaces: 23 | type: "integer" 24 | default: 0 25 | 26 | internal_interfaces: 27 | type: "integer" 28 | default: 0 -------------------------------------------------------------------------------- /grifter/schemas/guest-pairs-schema.yml: -------------------------------------------------------------------------------- 1 | --- 2 | child: 3 | type: "string" 4 | required: True 5 | empty: False 6 | 7 | parent: 8 | type: "string" 9 | required: True 10 | empty: False 11 | -------------------------------------------------------------------------------- /grifter/schemas/guest-schema.yml: -------------------------------------------------------------------------------- 1 | --- 2 | vagrant_box: 3 | type: "dict" 4 | required: True 5 | schema: 6 | name: 7 | type: "string" 8 | required: True 9 | empty: False 10 | version: 11 | type: "string" 12 | url: 13 | type: "string" 14 | provider: 15 | type: "string" 16 | allowed: 17 | - "libvirt" 18 | guest_type: 19 | type: "string" 20 | boot_timeout: 21 | type: "integer" 22 | min: 0 23 | max: 300 24 | forbidden: 25 | - 1 26 | throttle_cpu: 27 | type: "integer" 28 | min: 0 29 | max: 99 30 | forbidden: 31 | - 1 32 | ssh: 33 | type: "dict" 34 | schema: 35 | username: 36 | type: "string" 37 | password: 38 | type: "string" 39 | insert_key: 40 | type: "boolean" 41 | 42 | synced_folder: 43 | type: "dict" 44 | schema: 45 | enabled: 46 | type: "boolean" 47 | id: 48 | type: "string" 49 | src: 50 | type: "string" 51 | dst: 52 | type: "string" 53 | 54 | provider_config: 55 | type: "dict" 56 | schema: 57 | random_hostname: 58 | type: "boolean" 59 | nic_adapter_count: 60 | type: "integer" 61 | min: 0 62 | max: 96 63 | disk_bus: 64 | type: "string" 65 | allowed: 66 | - "virtio" 67 | - "ide" 68 | - "sata" 69 | cpus: 70 | type: "integer" 71 | min: 1 72 | max: 16 73 | memory: 74 | type: "integer" 75 | min: 256 76 | max: 32768 77 | huge_pages: 78 | type: "boolean" 79 | storage_pool: 80 | type: "string" 81 | additional_storage_volumes: 82 | type: "list" 83 | schema: 84 | type: "dict" 85 | schema: 86 | location: 87 | type: "string" 88 | required: True 89 | type: 90 | type: "string" 91 | required: True 92 | allowed: 93 | - "qcow2" 94 | - "raw" 95 | bus: 96 | type: "string" 97 | required: True 98 | allowed: 99 | - "ide" 100 | device: 101 | type: "string" 102 | required: True 103 | 104 | nic_model_type: 105 | type: "string" 106 | empty: True 107 | allowed: 108 | - "virtio" 109 | - "e1000" 110 | - "vmxnet3" 111 | management_network_mac: 112 | type: "string" 113 | 114 | internal_interfaces: 115 | type: "list" 116 | schema: 117 | type: "dict" 118 | schema: 119 | local_port: 120 | type: "integer" 121 | min: 0 122 | max: 2 123 | remote_guest: 124 | type: "string" 125 | remote_port: 126 | type: "integer" 127 | min: 0 128 | max: 2 129 | 130 | reserved_interfaces: 131 | type: "list" 132 | schema: 133 | type: "dict" 134 | schema: 135 | local_port: 136 | type: "integer" 137 | min: 0 138 | max: 5 139 | remote_guest: 140 | type: "string" 141 | remote_port: 142 | type: "integer" 143 | min: 0 144 | max: 5 145 | 146 | data_interfaces: 147 | type: "list" 148 | schema: 149 | type: "dict" 150 | schema: 151 | local_port: 152 | type: "integer" 153 | min: 0 154 | max: 96 155 | remote_guest: 156 | type: "string" 157 | remote_port: 158 | type: "integer" 159 | min: 0 160 | max: 96 161 | -------------------------------------------------------------------------------- /grifter/templates/additional-data-storage-trigger.j2: -------------------------------------------------------------------------------- 1 | 2 | add_volumes = [ 3 | {% for volume in data.provider_config.additional_storage_volumes %} 4 | "virsh vol-create-as {{ data.provider_config.storage_pool|default('default', True) }} #{domain_prefix}-#{guest_name}-#{domain_uuid}-{{ volume.location.split('/')[-1] }} {{ volume.size }}", 5 | "sleep 1", 6 | "virsh vol-upload --pool {{ data.provider_config.storage_pool|default('default', True) }} #{domain_prefix}-#{guest_name}-#{domain_uuid}-{{ volume.location.split('/')[-1] }} {{ volume.location }}", 7 | "sleep 1"{{ "," if not loop.last }} 8 | {% endfor %} 9 | ] 10 | add_volumes.each do |i| 11 | node.trigger.before :up do |trigger| 12 | trigger.name = "add-volumes" 13 | trigger.info = "Adding Volumes" 14 | trigger.run = {inline: i} 15 | end 16 | end 17 | 18 | delete_volumes = [ 19 | {% for volume in data.provider_config.additional_storage_volumes %} 20 | "virsh vol-delete #{domain_prefix}-#{guest_name}-#{domain_uuid}-{{ volume.location.split('/')[-1] }} {{ data.provider_config.storage_pool|default('default', True) }}"{{ "," if not loop.last }} 21 | {% endfor %} 22 | ] 23 | delete_volumes.each do |i| 24 | node.trigger.after :destroy do |trigger| 25 | trigger.name = "remove-volumes" 26 | trigger.info = "Removing Volumes" 27 | trigger.run = {inline: i} 28 | end 29 | end 30 | {# #} -------------------------------------------------------------------------------- /grifter/templates/base.j2: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # vi: set ft=ruby : 3 | 4 | # Created: {{ creation_time }} 5 | 6 | {% include 'get-mac.rb' %} 7 | 8 | cwd = Dir.pwd.split("/").last 9 | username = ENV['USER'] 10 | domain_prefix = "#{username}_#{cwd}" 11 | domain_uuid = "{{ domain_uuid }}" 12 | 13 | Vagrant.require_version ">= 2.1.0" 14 | Vagrant.configure("2") do |config| 15 | 16 | {% block guest_config %} 17 | {% endblock guest_config %} 18 | 19 | end 20 | -------------------------------------------------------------------------------- /grifter/templates/blackhole-interfaces-trigger.j2: -------------------------------------------------------------------------------- 1 | 2 | blackhole_interfaces = {{ blackhole_interfaces.get(guest)|replace("'", '"') }} 3 | blackhole_interfaces.each do |interface| 4 | node.trigger.after :up do |trigger| 5 | trigger.info = "Shutting down #{guest_name}-#{interface}" 6 | trigger.run = {inline: "virsh domif-setlink #{domain_prefix}_#{guest_name} #{guest_name}-#{interface}-#{domain_uuid} down"} 7 | end 8 | end 9 | {# #} -------------------------------------------------------------------------------- /grifter/templates/get-mac.rb: -------------------------------------------------------------------------------- 1 | def get_mac(oui="28:b7:ad") 2 | "Generate a MAC address" 3 | nic = (1..3).map{"%0.2x"%rand(256)}.join(":") 4 | return "#{oui}:#{nic}" 5 | end 6 | 7 | -------------------------------------------------------------------------------- /grifter/templates/guest.j2: -------------------------------------------------------------------------------- 1 | {% extends 'base.j2' %} 2 | 3 | {% block guest_config %} 4 | {% for guest, data in guests.items() %} 5 | config.vm.define "{{ guest }}" do |node| 6 | guest_name = "{{ guest }}" 7 | node.vm.box = "{{ data.vagrant_box.name }}" 8 | {% if data.vagrant_box.version %} 9 | node.vm.box_version = "{{ data.vagrant_box.version }}" 10 | {% endif %} 11 | {% if data.vagrant_box.url %} 12 | node.vm.box_url = "{{ data.vagrant_box.url }}" 13 | {% endif %} 14 | {% if data.vagrant_box.guest_type %} 15 | node.vm.guest = :{{ data.vagrant_box.guest_type }} 16 | {% endif %} 17 | {% if data.vagrant_box.boot_timeout %} 18 | node.vm.boot_timeout = {{ data.vagrant_box.boot_timeout }} 19 | {% endif %} 20 | {% if data.synced_folder.enabled %} 21 | node.vm.synced_folder "{{ data.synced_folder.src }}", "{{ data.synced_folder.dst }}", id: "{{ data.synced_folder.id }}" 22 | {% else %} 23 | node.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true 24 | {% endif %} 25 | 26 | {% if not data.insert_ssh_key %} 27 | node.ssh.insert_key = false 28 | {% endif %} 29 | {% if data.ssh.username %} 30 | node.ssh.username = "{{ data.ssh.username }}" 31 | {% endif %} 32 | {% if data.ssh.password %} 33 | node.ssh.password = "{{ data.ssh.password }}" 34 | {% endif %} 35 | 36 | {% include 'libvirt-config.j2' %} 37 | {% if data.vagrant_box.throttle_cpu %} 38 | {% include 'throttle-cpu-trigger.j2' %} 39 | {% endif %} 40 | {% if data.provider_config.additional_storage_volumes %} 41 | {% include 'additional-data-storage-trigger.j2' %} 42 | {% endif %} 43 | {% if blackhole_interfaces.get(guest) %} 44 | {% include 'blackhole-interfaces-trigger.j2' %} 45 | {% endif %} 46 | 47 | {% for interface in data.internal_interfaces %} 48 | {% include 'libvirt-internal-interface-config.j2' %} 49 | {% endfor %} 50 | {% for interface in data.reserved_interfaces %} 51 | {% include 'libvirt-reserved-interface-config.j2' %} 52 | {% endfor %} 53 | {% for interface in data.data_interfaces %} 54 | {% include 'libvirt-data-interface-config.j2' %} 55 | {% endfor %} 56 | end 57 | {% endfor %} 58 | {% endblock guest_config %} 59 | -------------------------------------------------------------------------------- /grifter/templates/libvirt-config.j2: -------------------------------------------------------------------------------- 1 | node.vm.provider :libvirt do |domain| 2 | domain.default_prefix = "#{domain_prefix}" 3 | {% if data.provider_config.random_hostname %} 4 | domain.random_hostname = true 5 | {% endif %} 6 | {% if data.provider_config.cpus %} 7 | domain.cpus = {{ data.provider_config.cpus }} 8 | {% endif %} 9 | {% if data.provider_config.memory %} 10 | domain.memory = {{ data.provider_config.memory }} 11 | {% endif %} 12 | {% if data.provider_config.huge_pages %} 13 | domain.memorybacking :hugepages 14 | {% endif %} 15 | {% if data.provider_config.storage_pool %} 16 | domain.storage_pool_name = "{{ data.provider_config.storage_pool }}" 17 | {% endif %} 18 | {% if data.provider_config.disk_bus %} 19 | domain.disk_bus = "{{ data.provider_config.disk_bus }}" 20 | {% endif %} 21 | {% if data.provider_config.management_network_mac %} 22 | domain.management_network_mac = "{{ data.provider_config.management_network_mac }}" 23 | {% endif %} 24 | {% set total_nics = data.data_interfaces|length + data.internal_interfaces|length + data.reserved_interfaces|length %} 25 | {% if total_nics %} 26 | domain.nic_adapter_count = {{ total_nics }} 27 | {% else %} 28 | domain.nic_adapter_count = 8 29 | {% endif %} 30 | {% if data.provider_config.nic_model_type %} 31 | domain.nic_model_type = "{{ data.provider_config.nic_model_type }}" 32 | {% endif %} 33 | {% if data.provider_config.additional_storage_volumes %} 34 | {% for volume in data.provider_config.additional_storage_volumes %} 35 | domain.storage :file, :path => "#{domain_prefix}-#{guest_name}-#{domain_uuid}-{{ volume.location.split('/')[-1] }}", :size => "{{ volume.size }}", :type => "{{ volume.type }}", :bus => "{{ volume.bus }}", :device => "{{ volume.device }}", :allow_existing => true 36 | {% endfor %} 37 | {% endif %} 38 | end 39 | {# #} -------------------------------------------------------------------------------- /grifter/templates/libvirt-data-interface-config.j2: -------------------------------------------------------------------------------- 1 | {% set local_int_map = interface_mappings[data['vagrant_box']['name']]['data_interfaces'] %} 2 | node.vm.network :private_network, 3 | {% if interface.remote_guest == 'blackhole' %} 4 | # {{ guest }}-{{ local_int_map[interface.local_port] }} <--> blackhole-666 5 | {% else %} 6 | {% set remote_int_map = interface_mappings[guests[interface.remote_guest]['vagrant_box']['name']]['data_interfaces'] %} 7 | # {{ guest }}-{{ local_int_map[interface.local_port] }} <--> {{ interface.remote_guest }}-{{ remote_int_map[interface.remote_port] }} 8 | {% endif %} 9 | :mac => "#{get_mac()}", 10 | :libvirt__tunnel_type => "udp", 11 | :libvirt__tunnel_local_ip => "{{ loopbacks[guest] }}", 12 | :libvirt__tunnel_local_port => {{ interface.local_port|explode_port(base_port=10000) }}, 13 | :libvirt__tunnel_ip => "{{ loopbacks[interface.remote_guest] }}", 14 | :libvirt__tunnel_port => {{ interface.remote_port|explode_port(base_port=10000) }}, 15 | :libvirt__iface_name => "{{ guest }}-{{ local_int_map[interface.local_port] }}-#{domain_uuid}", 16 | auto_config: false 17 | 18 | {# #} -------------------------------------------------------------------------------- /grifter/templates/libvirt-internal-interface-config.j2: -------------------------------------------------------------------------------- 1 | node.vm.network :private_network, 2 | # {{ guest }}-internal-{{ interface.local_port }} <--> {{ interface.remote_guest }}-internal-{{ interface.remote_port }} 3 | :mac => "#{get_mac()}", 4 | :libvirt__tunnel_type => "udp", 5 | :libvirt__tunnel_local_ip => "{{ loopbacks[guest] }}", 6 | :libvirt__tunnel_local_port => {{ interface.local_port|explode_port(base_port=11000) }}, 7 | :libvirt__tunnel_ip => "{{ loopbacks[interface.remote_guest] }}", 8 | :libvirt__tunnel_port => {{ interface.remote_port|explode_port(base_port=11000) }}, 9 | :libvirt__iface_name => "{{ guest }}-internal-{{ interface.local_port }}-#{domain_uuid}", 10 | auto_config: false 11 | 12 | {# #} -------------------------------------------------------------------------------- /grifter/templates/libvirt-reserved-interface-config.j2: -------------------------------------------------------------------------------- 1 | {% set local_int_map = interface_mappings[data['vagrant_box']['name']]['data_interfaces'] %} 2 | node.vm.network :private_network, 3 | # {{ guest }}-reserved-{{ interface.local_port }} <--> blackhole-666 4 | :mac => "#{get_mac()}", 5 | :libvirt__tunnel_type => "udp", 6 | :libvirt__tunnel_local_ip => "{{ loopbacks[guest] }}", 7 | :libvirt__tunnel_local_port => {{ interface.local_port|explode_port(base_port=12000) }}, 8 | :libvirt__tunnel_ip => "{{ loopbacks[interface.remote_guest] }}", 9 | :libvirt__tunnel_port => {{ interface.remote_port|explode_port(base_port=12000) }}, 10 | :libvirt__iface_name => "{{ guest }}-reserved-{{ interface.local_port }}-#{domain_uuid}", 11 | auto_config: false 12 | 13 | {# #} -------------------------------------------------------------------------------- /grifter/templates/throttle-cpu-trigger.j2: -------------------------------------------------------------------------------- 1 | 2 | node.trigger.after :up do |trigger| 3 | trigger.info = "Throttling #{domain_prefix}_#{guest_name} CPU" 4 | trigger.run = {inline: "virsh schedinfo #{domain_prefix}_#{guest_name} --set vcpu_quota={{ 1000 * data.vagrant_box.throttle_cpu }}"} 5 | end 6 | {# #} -------------------------------------------------------------------------------- /grifter/templates/topology.dot.j2: -------------------------------------------------------------------------------- 1 | graph G { 2 | {% for connection in connections_list %} 3 | {{ connection }} 4 | {% endfor %} 5 | } -------------------------------------------------------------------------------- /grifter/utils.py: -------------------------------------------------------------------------------- 1 | import random 2 | import uuid 3 | import string 4 | import re 5 | 6 | 7 | def get_mac(oui='28:b7:ad'): 8 | """ 9 | Generate a random MAC address. 10 | param oui: MAC address OUI 11 | return: MAC address as a string 12 | """ 13 | nic = ':'.join([format(random.randint(0, 255), '02x') for _ in range(0, 3)]) 14 | return f'{oui}:{nic}' 15 | 16 | 17 | def get_uuid(): 18 | """ 19 | Generate a random UUID. 20 | :return: random UUID string. 21 | """ 22 | return str(uuid.uuid5(uuid.uuid4(), string.ascii_letters)) 23 | 24 | 25 | def remove_duplicates(list_of_tuples): 26 | """ 27 | Takes a list of tuples and removes duplicate entries. 28 | Reverses the pairs (0, 1, 2, 3) to (2, 3, 0, 1) for comparison. 29 | :param list_of_tuples: [(0, 1, 2, 3), (2, 3, 0, 1)] 30 | :return: List of unique tuples. 31 | """ 32 | reduced = set() 33 | for i in list_of_tuples: 34 | if not (i[2], i[3], i[0], i[1]) in reduced: 35 | reduced.add(i) 36 | return list(reduced) 37 | 38 | 39 | def sort_nicely(the_list): 40 | """ 41 | Sort the given list in the way that humans expect. 42 | Adapted from: https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/ 43 | :param the_list: The list to sort. 44 | """ 45 | if not isinstance(the_list, list): 46 | raise AttributeError('the_list must be of the type list') 47 | if not the_list: 48 | return the_list 49 | 50 | def convert(text): 51 | return int(text) if text.isdigit() else text 52 | 53 | def alphanum_key(key): 54 | return [convert(c) for c in re.split('([0-9]+)', key)] 55 | 56 | return sorted(the_list, key=alphanum_key) 57 | 58 | 59 | def dict_merge(a, b): 60 | """ 61 | Merge dict-like "b" into dict-like "a". In case keys between them 62 | are the same, merge their sub-dictionaries where possible. Otherwise, 63 | values in "b" overwrite "a". This function mutates "a", deepcopy "a" 64 | if you want to preserve the original "a" dict. 65 | # Shamelessly adapted from: https://stackoverflow.com/a/49060237 66 | """ 67 | for key in b: 68 | if key in a and isinstance(a[key], dict) and isinstance(b[key], dict): 69 | a[key] = dict_merge(a[key], b[key]) 70 | else: 71 | a[key] = b[key] 72 | return a 73 | -------------------------------------------------------------------------------- /grifter/validators.py: -------------------------------------------------------------------------------- 1 | from cerberus import Validator 2 | from grifter.loaders import load_data 3 | from grifter.constants import ( 4 | GUEST_SCHEMA_FILE, 5 | GUEST_CONFIG_SCHEMA, 6 | GUEST_PAIRS_SCHEMA, 7 | 8 | ) 9 | required_keys = ['vagrant_box'] 10 | 11 | 12 | def validate_config(guest_config): 13 | errors = [] 14 | 15 | def _validate(config, schema): 16 | validation_errors = [] 17 | for key, data in config.items(): 18 | result = validate_schema(data, schema) 19 | if result.errors: 20 | validation_errors.append(result.errors) 21 | return validation_errors 22 | 23 | guest_config_result = _validate(guest_config['guest_config'], load_data(GUEST_CONFIG_SCHEMA)) 24 | guest_pairs_result = _validate(guest_config['guest_pairs'], load_data(GUEST_PAIRS_SCHEMA)) 25 | 26 | if guest_config_result: 27 | errors += guest_config_result 28 | 29 | if guest_pairs_result: 30 | errors += guest_pairs_result 31 | 32 | return errors 33 | 34 | 35 | def validate_data(guest_data, guest_default_data=False): 36 | """ 37 | Validate data conforms to required schema 38 | :param guest_data: Guest data dict 39 | :param guest_default_data: True if validating guest defaults 40 | :return: errors list 41 | """ 42 | errors = [] 43 | schema = load_data(GUEST_SCHEMA_FILE) 44 | # Guest default schema should be the same as a guest 45 | # schema apart from the vagrant box name attribute. 46 | if guest_default_data: 47 | schema['vagrant_box']['schema'].pop('name') 48 | 49 | for guest, data in guest_data.items(): 50 | result = validate_schema(data, schema) 51 | if result.errors: 52 | errors.append(result.errors) 53 | 54 | return errors 55 | 56 | 57 | def validate_guests_in_guest_config(guests, config): 58 | """ 59 | Validate guests have a vagrant box config 60 | :param guests: 61 | :param config: 62 | :return: 63 | """ 64 | guest_config = config['guest_config'] 65 | for guest, data in guests.items(): 66 | local_guest = guest 67 | local_box = data['vagrant_box']['name'] 68 | remote_guests = list( 69 | set([i['remote_guest'] for i in data['data_interfaces'] if i['remote_guest'] != 'blackhole']) 70 | ) 71 | 72 | if not guest_config.get(local_box): 73 | raise AttributeError( 74 | f'{local_guest}\'s vagrant box type: {local_box} ' 75 | f'is not defined in the config file.') 76 | 77 | for remote_guest in remote_guests: 78 | if not guests.get(remote_guest): 79 | raise AttributeError( 80 | f'{remote_guest} is not defined in the guests file.') 81 | 82 | return True 83 | 84 | 85 | def validate_guest_interfaces(guests, config, int_map): 86 | guest_config = config['guest_config'] 87 | for guest, data in guests.items(): 88 | local_guest = guest 89 | local_box = data['vagrant_box']['name'] 90 | nic_adapter_count = data['provider_config']['nic_adapter_count'] 91 | max_data_interfaces = guest_config[local_box]['max_data_interfaces'] 92 | internal_interfaces = data['internal_interfaces'] 93 | reserved_interfaces = data['reserved_interfaces'] 94 | box_internal_interfaces = guest_config[local_box]['internal_interfaces'] 95 | num_internal_interfaces = len(internal_interfaces) 96 | num_reserved_interfaces = len(reserved_interfaces) 97 | total_interfaces = max_data_interfaces + num_internal_interfaces + num_reserved_interfaces 98 | 99 | remote_guest_data = [] 100 | if data['data_interfaces']: 101 | for i in data['data_interfaces']: 102 | if i['remote_guest'] != 'blackhole': 103 | remote_guest_data.append((i['remote_guest'], i['remote_port'])) 104 | 105 | if nic_adapter_count > total_interfaces: 106 | raise AttributeError( 107 | f'The number of data interfaces for {local_guest} ' 108 | f'is greater than the allowed {local_box} maximum data interfaces.') 109 | 110 | for interface in data['data_interfaces']: 111 | local_port = interface["local_port"] 112 | if not int_map[local_box]['data_interfaces'].get(local_port): 113 | raise AttributeError( 114 | f'{local_guest}\'s local_port: {local_port} ' 115 | f'is outside the supported range.') 116 | 117 | for rg in remote_guest_data: 118 | remote_guest = rg[0] 119 | remote_port = rg[1] 120 | remote_box = guests[remote_guest]['vagrant_box']['name'] 121 | if not int_map[remote_box]['data_interfaces'].get(remote_port): 122 | raise AttributeError( 123 | f'Error with {local_guest}\'s interface config.\n' 124 | f'{remote_guest}\'s interface: {remote_port} ' 125 | f'is outside the supported range.') 126 | 127 | if num_internal_interfaces != box_internal_interfaces: 128 | raise AttributeError( 129 | f'The number of internal interfaces for {local_guest}: {num_internal_interfaces} ' 130 | f'is not equal to the {local_box} internal interfaces value: ' 131 | f'{box_internal_interfaces}.') 132 | return True 133 | 134 | 135 | def validate_required_keys(guest): 136 | for key in required_keys: 137 | if not guest.get(key): 138 | raise AttributeError(f'{key} is a required key') 139 | if not guest['vagrant_box'].get('name'): 140 | raise AttributeError('vagrant_box["name"] is a required key') 141 | 142 | 143 | def validate_required_values(guest): 144 | if not guest['vagrant_box']['name']: 145 | raise ValueError('vagrant_box["name"] is a required value and cannot be empty') 146 | 147 | 148 | def validate_schema(data, schema): 149 | v = Validator() 150 | v.validate(data, schema) 151 | return v 152 | -------------------------------------------------------------------------------- /requirements-dev.txt: -------------------------------------------------------------------------------- 1 | pytest 2 | pytest-cov 3 | mock 4 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | jinja2 2 | pyyaml 3 | click 4 | cerberus -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | from setuptools import setup, find_packages 3 | 4 | 5 | def read(file_name): 6 | with open(os.path.join(os.path.dirname(__file__), file_name), 'r') as f: 7 | return f.read() 8 | 9 | 10 | setup( 11 | name='grifter', 12 | version=read('VERSION'), 13 | author='Brad Searle', 14 | author_email='bradleysearle@gmail.com', 15 | license='GNU GENERAL PUBLIC LICENSE Version 3', 16 | long_description=read('README.md'), 17 | 18 | include_package_data=True, 19 | packages=find_packages(), 20 | 21 | install_requires=[ 22 | 'jinja2', 23 | 'pyyaml', 24 | 'click', 25 | 'cerberus', 26 | ], 27 | 28 | entry_points={ 29 | 'console_scripts': [ 30 | 'grifter = grifter:cli', 31 | ] 32 | }, 33 | 34 | ) 35 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bwks/grifter/dd47ba13905caf1a1e3f115cd77d6e9c714e6850/tests/__init__.py -------------------------------------------------------------------------------- /tests/mock_data.py: -------------------------------------------------------------------------------- 1 | from grifter.constants import TESTS_DIR 2 | 3 | mock_guest_data = { 4 | 'sw01': { 5 | 'ssh': { 6 | 'username': '', 7 | 'password': '', 8 | 'insert_key': False, 9 | }, 10 | 'internal_interfaces': [], 11 | 'reserved_interfaces': [], 12 | 'data_interfaces': [ 13 | { 14 | 'local_port': 1, 15 | 'remote_guest': 'sw02', 16 | 'remote_port': 1 17 | }, 18 | { 19 | 'local_port': 2, 20 | 'remote_guest': 'sw02', 21 | 'remote_port': 2 22 | } 23 | ], 24 | 'provider_config': { 25 | 'random_hostname': False, 26 | 'cpus': 2, 27 | 'disk_bus': 'ide', 28 | 'management_network_mac': '', 29 | 'nic_model_type': '', 30 | 'memory': 2048, 31 | 'huge_pages': False, 32 | 'nic_adapter_count': 2, 33 | 'additional_storage_volumes': [], 34 | }, 35 | 'synced_folder': { 36 | 'enabled': False, 37 | }, 38 | 'vagrant_box': { 39 | 'name': 'arista/veos', 40 | 'provider': 'libvirt', 41 | 'version': '', 42 | 'url': '', 43 | 'guest_type': '', 44 | 'throttle_cpu': 0 45 | } 46 | }, 47 | 'sw02': { 48 | 'ssh': { 49 | 'username': '', 50 | 'password': '', 51 | 'insert_key': False, 52 | }, 53 | 'internal_interfaces': [], 54 | 'reserved_interfaces': [], 55 | 'data_interfaces': [ 56 | { 57 | 'local_port': 1, 58 | 'remote_guest': 'sw01', 59 | 'remote_port': 1 60 | }, 61 | { 62 | 'local_port': 2, 63 | 'remote_guest': 'sw01', 64 | 'remote_port': 2 65 | } 66 | ], 67 | 'provider_config': { 68 | 'random_hostname': False, 69 | 'cpus': 2, 70 | 'disk_bus': 'ide', 71 | 'management_network_mac': '', 72 | 'nic_model_type': '', 73 | 'memory': 2048, 74 | 'huge_pages': False, 75 | 'nic_adapter_count': 2, 76 | 'additional_storage_volumes': [], 77 | }, 78 | 'synced_folder': { 79 | 'enabled': False, 80 | }, 81 | 'vagrant_box': { 82 | 'name': 'arista/veos', 83 | 'provider': 'libvirt', 84 | 'version': '', 85 | 'url': '', 86 | 'guest_type': '', 87 | 'throttle_cpu': 0 88 | } 89 | } 90 | } 91 | 92 | mock_guest_interfaces = mock_guest_data['sw01']['data_interfaces'] 93 | 94 | mock_additional_storage_volumes = [ 95 | { 96 | 'location': '/fake/location/volume1.qcow2', 97 | 'type': 'qcow2', 98 | 'bus': 'ide', 99 | 'device': 'hdb', 100 | }, 101 | { 102 | 'location': '/fake/location/volume2.img', 103 | 'type': 'raw', 104 | 'bus': 'ide', 105 | 'device': 'hdc', 106 | } 107 | ] 108 | 109 | 110 | with open(f'{TESTS_DIR}/mock_vagrantfile.rb', 'r') as f: 111 | mock_vagrantfile = f.read() 112 | 113 | with open(f'{TESTS_DIR}/mock_vagrantfile_additional_storage_volumes.rb', 'r') as f: 114 | mock_vagrantfile_with_additional_storage_volumes = f.read() 115 | 116 | mock_invalid_guest_data_file = f'{TESTS_DIR}/mock_invalid_guest_data.yml' 117 | 118 | 119 | mock_connection_data = [{'local_guest': 'sw1', 'local_port': 'swp7', 120 | 'remote_guest': 'r7', 'remote_port': 'ge-0/0/9'}] 121 | -------------------------------------------------------------------------------- /tests/mock_invalid_guest_data.yml: -------------------------------------------------------------------------------- 1 | sw01: 2 | vagrant_box: 3 | name: "" 4 | -------------------------------------------------------------------------------- /tests/mock_json_data.json: -------------------------------------------------------------------------------- 1 | { 2 | "some": "data" 3 | } -------------------------------------------------------------------------------- /tests/mock_vagrantfile.rb: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # vi: set ft=ruby : 3 | 4 | # Created: 2018-12-26--17-58-55 5 | 6 | def get_mac(oui="28:b7:ad") 7 | "Generate a MAC address" 8 | nic = (1..3).map{"%0.2x"%rand(256)}.join(":") 9 | return "#{oui}:#{nic}" 10 | end 11 | 12 | cwd = Dir.pwd.split("/").last 13 | username = ENV['USER'] 14 | domain_prefix = "#{username}_#{cwd}" 15 | domain_uuid = "688c29aa-e657-5d27-b4bb-d745aad2812e" 16 | 17 | Vagrant.require_version ">= 2.1.0" 18 | Vagrant.configure("2") do |config| 19 | 20 | config.vm.define "sw01" do |node| 21 | guest_name = "sw01" 22 | node.vm.box = "arista/veos" 23 | node.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true 24 | 25 | node.ssh.insert_key = false 26 | 27 | node.vm.provider :libvirt do |domain| 28 | domain.default_prefix = "#{domain_prefix}" 29 | domain.cpus = 2 30 | domain.memory = 2048 31 | domain.disk_bus = "ide" 32 | domain.nic_adapter_count = 2 33 | end 34 | 35 | node.vm.network :private_network, 36 | # sw01-eth1 <--> sw02-eth1 37 | :mac => "#{get_mac()}", 38 | :libvirt__tunnel_type => "udp", 39 | :libvirt__tunnel_local_ip => "127.255.255.1", 40 | :libvirt__tunnel_local_port => 10001, 41 | :libvirt__tunnel_ip => "127.255.255.2", 42 | :libvirt__tunnel_port => 10001, 43 | :libvirt__iface_name => "sw01-eth1-#{domain_uuid}", 44 | auto_config: false 45 | 46 | node.vm.network :private_network, 47 | # sw01-eth2 <--> sw02-eth2 48 | :mac => "#{get_mac()}", 49 | :libvirt__tunnel_type => "udp", 50 | :libvirt__tunnel_local_ip => "127.255.255.1", 51 | :libvirt__tunnel_local_port => 10002, 52 | :libvirt__tunnel_ip => "127.255.255.2", 53 | :libvirt__tunnel_port => 10002, 54 | :libvirt__iface_name => "sw01-eth2-#{domain_uuid}", 55 | auto_config: false 56 | 57 | end 58 | config.vm.define "sw02" do |node| 59 | guest_name = "sw02" 60 | node.vm.box = "arista/veos" 61 | node.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true 62 | 63 | node.ssh.insert_key = false 64 | 65 | node.vm.provider :libvirt do |domain| 66 | domain.default_prefix = "#{domain_prefix}" 67 | domain.cpus = 2 68 | domain.memory = 2048 69 | domain.disk_bus = "ide" 70 | domain.nic_adapter_count = 2 71 | end 72 | 73 | node.vm.network :private_network, 74 | # sw02-eth1 <--> sw01-eth1 75 | :mac => "#{get_mac()}", 76 | :libvirt__tunnel_type => "udp", 77 | :libvirt__tunnel_local_ip => "127.255.255.2", 78 | :libvirt__tunnel_local_port => 10001, 79 | :libvirt__tunnel_ip => "127.255.255.1", 80 | :libvirt__tunnel_port => 10001, 81 | :libvirt__iface_name => "sw02-eth1-#{domain_uuid}", 82 | auto_config: false 83 | 84 | node.vm.network :private_network, 85 | # sw02-eth2 <--> sw01-eth2 86 | :mac => "#{get_mac()}", 87 | :libvirt__tunnel_type => "udp", 88 | :libvirt__tunnel_local_ip => "127.255.255.2", 89 | :libvirt__tunnel_local_port => 10002, 90 | :libvirt__tunnel_ip => "127.255.255.1", 91 | :libvirt__tunnel_port => 10002, 92 | :libvirt__iface_name => "sw02-eth2-#{domain_uuid}", 93 | auto_config: false 94 | 95 | end 96 | 97 | end -------------------------------------------------------------------------------- /tests/mock_vagrantfile_additional_storage_volumes.rb: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # vi: set ft=ruby : 3 | 4 | # Created: 2018-12-26--17-58-55 5 | 6 | def get_mac(oui="28:b7:ad") 7 | "Generate a MAC address" 8 | nic = (1..3).map{"%0.2x"%rand(256)}.join(":") 9 | return "#{oui}:#{nic}" 10 | end 11 | 12 | cwd = Dir.pwd.split("/").last 13 | username = ENV['USER'] 14 | domain_prefix = "#{username}_#{cwd}" 15 | domain_uuid = "688c29aa-e657-5d27-b4bb-d745aad2812e" 16 | 17 | Vagrant.require_version ">= 2.1.0" 18 | Vagrant.configure("2") do |config| 19 | 20 | config.vm.define "sw01" do |node| 21 | guest_name = "sw01" 22 | node.vm.box = "arista/veos" 23 | node.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true 24 | 25 | node.ssh.insert_key = false 26 | 27 | node.vm.provider :libvirt do |domain| 28 | domain.default_prefix = "#{domain_prefix}" 29 | domain.cpus = 2 30 | domain.memory = 2048 31 | domain.disk_bus = "ide" 32 | domain.nic_adapter_count = 2 33 | domain.storage :file, :path => "#{domain_prefix}-#{guest_name}-#{domain_uuid}-volume1.qcow2", :size => "10000", :type => "qcow2", :bus => "ide", :device => "hdb", :allow_existing => true 34 | domain.storage :file, :path => "#{domain_prefix}-#{guest_name}-#{domain_uuid}-volume2.img", :size => "10000", :type => "raw", :bus => "ide", :device => "hdc", :allow_existing => true 35 | end 36 | 37 | add_volumes = [ 38 | "virsh vol-create-as default #{domain_prefix}-#{guest_name}-#{domain_uuid}-volume1.qcow2 10000", 39 | "sleep 1", 40 | "virsh vol-upload --pool default #{domain_prefix}-#{guest_name}-#{domain_uuid}-volume1.qcow2 /fake/location/volume1.qcow2", 41 | "sleep 1", 42 | "virsh vol-create-as default #{domain_prefix}-#{guest_name}-#{domain_uuid}-volume2.img 10000", 43 | "sleep 1", 44 | "virsh vol-upload --pool default #{domain_prefix}-#{guest_name}-#{domain_uuid}-volume2.img /fake/location/volume2.img", 45 | "sleep 1" 46 | ] 47 | add_volumes.each do |i| 48 | node.trigger.before :up do |trigger| 49 | trigger.name = "add-volumes" 50 | trigger.info = "Adding Volumes" 51 | trigger.run = {inline: i} 52 | end 53 | end 54 | 55 | delete_volumes = [ 56 | "virsh vol-delete #{domain_prefix}-#{guest_name}-#{domain_uuid}-volume1.qcow2 default", 57 | "virsh vol-delete #{domain_prefix}-#{guest_name}-#{domain_uuid}-volume2.img default" 58 | ] 59 | delete_volumes.each do |i| 60 | node.trigger.after :destroy do |trigger| 61 | trigger.name = "remove-volumes" 62 | trigger.info = "Removing Volumes" 63 | trigger.run = {inline: i} 64 | end 65 | end 66 | 67 | node.vm.network :private_network, 68 | # sw01-eth1 <--> sw02-eth1 69 | :mac => "#{get_mac()}", 70 | :libvirt__tunnel_type => "udp", 71 | :libvirt__tunnel_local_ip => "127.255.255.1", 72 | :libvirt__tunnel_local_port => 10001, 73 | :libvirt__tunnel_ip => "127.255.255.2", 74 | :libvirt__tunnel_port => 10001, 75 | :libvirt__iface_name => "sw01-eth1-#{domain_uuid}", 76 | auto_config: false 77 | 78 | node.vm.network :private_network, 79 | # sw01-eth2 <--> sw02-eth2 80 | :mac => "#{get_mac()}", 81 | :libvirt__tunnel_type => "udp", 82 | :libvirt__tunnel_local_ip => "127.255.255.1", 83 | :libvirt__tunnel_local_port => 10002, 84 | :libvirt__tunnel_ip => "127.255.255.2", 85 | :libvirt__tunnel_port => 10002, 86 | :libvirt__iface_name => "sw01-eth2-#{domain_uuid}", 87 | auto_config: false 88 | 89 | end 90 | config.vm.define "sw02" do |node| 91 | guest_name = "sw02" 92 | node.vm.box = "arista/veos" 93 | node.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true 94 | 95 | node.ssh.insert_key = false 96 | 97 | node.vm.provider :libvirt do |domain| 98 | domain.default_prefix = "#{domain_prefix}" 99 | domain.cpus = 2 100 | domain.memory = 2048 101 | domain.disk_bus = "ide" 102 | domain.nic_adapter_count = 2 103 | end 104 | 105 | node.vm.network :private_network, 106 | # sw02-eth1 <--> sw01-eth1 107 | :mac => "#{get_mac()}", 108 | :libvirt__tunnel_type => "udp", 109 | :libvirt__tunnel_local_ip => "127.255.255.2", 110 | :libvirt__tunnel_local_port => 10001, 111 | :libvirt__tunnel_ip => "127.255.255.1", 112 | :libvirt__tunnel_port => 10001, 113 | :libvirt__iface_name => "sw02-eth1-#{domain_uuid}", 114 | auto_config: false 115 | 116 | node.vm.network :private_network, 117 | # sw02-eth2 <--> sw01-eth2 118 | :mac => "#{get_mac()}", 119 | :libvirt__tunnel_type => "udp", 120 | :libvirt__tunnel_local_ip => "127.255.255.2", 121 | :libvirt__tunnel_local_port => 10002, 122 | :libvirt__tunnel_ip => "127.255.255.1", 123 | :libvirt__tunnel_port => 10002, 124 | :libvirt__iface_name => "sw02-eth2-#{domain_uuid}", 125 | auto_config: false 126 | 127 | end 128 | 129 | end -------------------------------------------------------------------------------- /tests/test_api.py: -------------------------------------------------------------------------------- 1 | import copy 2 | import pytest 3 | 4 | from unittest import mock 5 | 6 | from grifter.constants import ( 7 | BASE_DIR, 8 | DEFAULT_CONFIG_FILE, 9 | ) 10 | from grifter.loaders import ( 11 | load_data, 12 | load_config_file, 13 | ) 14 | from grifter.api import ( 15 | generate_loopbacks, 16 | update_guest_interfaces, 17 | add_blackhole_interfaces, 18 | update_guest_data, 19 | update_guest_additional_storage, 20 | int_to_port_map, 21 | generate_int_to_port_mappings, 22 | create_reserved_interfaces, 23 | generate_connection_strings, 24 | ) 25 | from .mock_data import ( 26 | mock_guest_data, 27 | mock_guest_interfaces, 28 | mock_connection_data, 29 | ) 30 | 31 | config = load_data(DEFAULT_CONFIG_FILE) 32 | 33 | 34 | def mock_data(filename): 35 | return load_data(f'{filename}') 36 | 37 | 38 | def test_guest_data_matches_dict(): 39 | assert load_data('examples/guests.yml') == mock_guest_data 40 | 41 | 42 | def test_generate_loopbacks_guest_dict_not_dict_type_raises_exception(): 43 | with pytest.raises(AttributeError): 44 | generate_loopbacks(guest_dict="") 45 | 46 | 47 | def test_generate_loopbacks_guest_dict_is_none_raises_exception(): 48 | with pytest.raises(AttributeError): 49 | generate_loopbacks(guest_dict=None) 50 | 51 | 52 | def test_generate_loopbacks_guest_dict_empty_raises_exception(): 53 | with pytest.raises(ValueError): 54 | generate_loopbacks(guest_dict={}) 55 | 56 | 57 | @mock.patch('random.randint', return_value=255) 58 | def test_generate_loopbacks_returned_loopback_dict(mock_random): 59 | expected_loopback_dict = { 60 | 'blackhole': '127.6.6.6', 61 | 'sw01': '127.255.255.1', 62 | 'sw02': '127.255.255.2' 63 | } 64 | assert generate_loopbacks(mock_guest_data) == expected_loopback_dict 65 | 66 | 67 | def test_guest_without_interfaces(): 68 | expected = { 69 | 'sw01': { 70 | 'ssh': { 71 | 'username': '', 72 | 'password': '', 73 | 'insert_key': False, 74 | }, 75 | 'internal_interfaces': [], 76 | 'reserved_interfaces': [], 77 | 'provider_config': { 78 | 'random_hostname': False, 79 | 'cpus': 2, 80 | 'disk_bus': 'ide', 81 | 'management_network_mac': '', 82 | 'nic_model_type': '', 83 | 'memory': 2048, 84 | 'huge_pages': False, 85 | 'nic_adapter_count': 2, 86 | 'additional_storage_volumes': [], 87 | }, 88 | 'synced_folder': { 89 | 'enabled': False, 90 | }, 91 | 'vagrant_box': { 92 | 'name': 'arista/veos', 93 | 'provider': 'libvirt', 94 | 'version': '', 95 | 'url': '', 96 | 'guest_type': '', 97 | 'throttle_cpu': 0 98 | } 99 | } 100 | } 101 | 102 | guests = copy.deepcopy(mock_guest_data) 103 | guests.pop('sw02') 104 | guests['sw01'].pop('data_interfaces') 105 | result = update_guest_interfaces(guests, config) 106 | assert result == expected 107 | 108 | 109 | def test_update_interfaces_with_same_interface_count_returns_same_list_of_interfaces(): 110 | assert add_blackhole_interfaces(1, 2, mock_guest_interfaces) == mock_guest_interfaces 111 | 112 | 113 | def test_update_interfaces_with_blackhole_interfaces(): 114 | blackhole_interfaces = [ 115 | { 116 | 'local_port': 3, 117 | 'remote_guest': 'blackhole', 118 | 'remote_port': 666 119 | }, 120 | { 121 | 'local_port': 4, 122 | 'remote_guest': 'blackhole', 123 | 'remote_port': 666 124 | } 125 | ] 126 | 127 | expected_intefaces = mock_guest_interfaces + blackhole_interfaces 128 | 129 | assert add_blackhole_interfaces(1, 4, mock_guest_interfaces) == expected_intefaces 130 | 131 | 132 | @mock.patch('grifter.api.load_config_file', side_effect=mock_data) 133 | def test_create_guest_with_group_vars(mock_data): 134 | seed_data = {'sw01': {'vagrant_box': {'name': 'arista/veos'}}} 135 | 136 | expected = { 137 | 'sw01': { 138 | 'ssh': { 139 | 'username': '', 140 | 'password': '', 141 | 'insert_key': False, 142 | }, 143 | 'data_interfaces': [], 144 | 'internal_interfaces': [], 145 | 'reserved_interfaces': [], 146 | 'provider_config': { 147 | 'random_hostname': False, 148 | 'cpus': 2, 149 | 'disk_bus': 'ide', 150 | 'management_network_mac': '', 151 | 'nic_model_type': '', 152 | 'memory': 2048, 153 | 'huge_pages': False, 154 | 'nic_adapter_count': 8, 155 | 'storage_pool': '', 156 | 'additional_storage_volumes': [], 157 | }, 158 | 'synced_folder': { 159 | 'enabled': False 160 | }, 161 | 'vagrant_box': { 162 | 'boot_timeout': 0, 163 | 'name': 'arista/veos', 164 | 'provider': 'libvirt', 165 | 'guest_type': '', 166 | 'version': '4.20.1F', 167 | 'url': '', 168 | 'throttle_cpu': 0 169 | } 170 | } 171 | } 172 | 173 | assert expected == update_guest_data(seed_data, f'{BASE_DIR}/../examples/guest-defaults.yml') 174 | 175 | 176 | def test_create_guest_without_group_vars(): 177 | seed_data = {'sw01': {}} 178 | 179 | expected = { 180 | 'sw01': { 181 | 'ssh': { 182 | 'username': '', 183 | 'password': '', 184 | 'insert_key': False, 185 | }, 186 | 'data_interfaces': [], 187 | 'internal_interfaces': [], 188 | 'reserved_interfaces': [], 189 | 'provider_config': { 190 | 'random_hostname': False, 191 | 'cpus': 1, 192 | 'disk_bus': '', 193 | 'management_network_mac': '', 194 | 'nic_model_type': '', 195 | 'memory': 512, 196 | 'huge_pages': False, 197 | 'nic_adapter_count': 0, 198 | 'storage_pool': '', 199 | 'additional_storage_volumes': [], 200 | }, 201 | 'synced_folder': { 202 | 'enabled': False 203 | }, 204 | 'vagrant_box': { 205 | 'boot_timeout': 0, 206 | 'name': '', 207 | 'provider': 'libvirt', 208 | 'guest_type': '', 209 | 'version': '', 210 | 'url': '', 211 | 'throttle_cpu': 0 212 | } 213 | } 214 | } 215 | 216 | assert expected == update_guest_data(seed_data, 'does-not-exist.yml') 217 | 218 | 219 | @mock.patch('grifter.api.load_config_file', side_effect=mock_data) 220 | def test_load_config_file_with_file_error_returns_empty_dict(mock_data): 221 | expected = {} 222 | assert expected == load_config_file('blah.yml') 223 | 224 | 225 | # noinspection PyPep8Naming 226 | def test_update_guest_additional_storage_raises_OSError(): 227 | guests = { 228 | 'some-guest': { 229 | 'provider_config': { 230 | 'additional_storage_volumes': [ 231 | {'location': '/fake/path/file.img'} 232 | ] 233 | } 234 | } 235 | } 236 | with pytest.raises(OSError): 237 | update_guest_additional_storage(guests) 238 | 239 | 240 | @mock.patch('os.path.getsize', return_value='10000') 241 | def test_update_guest_additional_storage_size(mock_data): 242 | guests = { 243 | 'some-guest': { 244 | 'provider_config': { 245 | 'additional_storage_volumes': [ 246 | {'location': '/fake/path/file.img'} 247 | ] 248 | } 249 | } 250 | } 251 | result = update_guest_additional_storage(guests) 252 | assert result['some-guest']['provider_config']['additional_storage_volumes'][0]['size'] == '10000' 253 | 254 | 255 | def test_int_to_port_map_returns_expected(): 256 | expected = {} 257 | for i in range(0, 12): 258 | expected.update({0 + i: f'ge-0/0/{i}'}) 259 | result = int_to_port_map('ge-0/0/', 0, 12) 260 | assert result == expected 261 | 262 | 263 | def test_generate_int_to_port_mappings(): 264 | data_interfaces = {} 265 | for i in range(0, 12): 266 | data_interfaces.update({0 + i: f'ge-0/0/{i}'}) 267 | expected = { 268 | 'data_interfaces': data_interfaces, 269 | 'internal_interfaces': {1: 'internal-1'}, 270 | 'management_interface': 'fxp0.0', 271 | 'reserved_interfaces': {1: 'reserved-1'} 272 | } 273 | data = { 274 | 'data_interface_base': "ge-0/0/", 275 | 'data_interface_offset': 0, 276 | 'internal_interfaces': 1, 277 | 'max_data_interfaces': 12, 278 | 'management_interface': "fxp0.0", 279 | 'reserved_interfaces': 1, 280 | } 281 | 282 | result = generate_int_to_port_mappings(data) 283 | assert result == expected 284 | 285 | 286 | def test_generate_int_to_port_mappings_empty_interfaces_returns_empty_dict(): 287 | expected = { 288 | 'data_interfaces': {}, 289 | 'internal_interfaces': {}, 290 | 'management_interface': 'mgmt', 291 | 'reserved_interfaces': {} 292 | } 293 | assert generate_int_to_port_mappings({}) == expected 294 | 295 | 296 | def test_create_reserved_interfaces(): 297 | expected = [{ 298 | 'local_port': 1, 299 | 'remote_guest': 'blackhole', 300 | 'remote_port': 666 301 | }] 302 | assert create_reserved_interfaces(1) == expected 303 | 304 | 305 | @pytest.mark.parametrize('data ,expected, dotfile', [ 306 | (mock_connection_data, ['sw1-swp7 <--> r7-ge-0/0/9'], False), 307 | (mock_connection_data, ['"sw1":"swp7" -- "r7":"ge-0/0/9";'], True), 308 | 309 | ]) 310 | def test_generate_connection_strings_return_expected_string_list(data, expected, dotfile): 311 | assert generate_connection_strings(data, dotfile) == expected 312 | -------------------------------------------------------------------------------- /tests/test_cli.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from click.testing import CliRunner 4 | 5 | from grifter.constants import ( 6 | GUESTS_EXAMPLE_FILE, 7 | GROUPS_EXAMPLE_FILE, 8 | ) 9 | from grifter.cli import ( 10 | cli, 11 | load_data_file, 12 | ) 13 | from .mock_data import mock_invalid_guest_data_file 14 | 15 | 16 | def test_cli_example_guest_output(): 17 | runner = CliRunner() 18 | result = runner.invoke(cli, ['example', '--guest']) 19 | 20 | with open(GUESTS_EXAMPLE_FILE, 'r') as f: 21 | expected = f.read() 22 | 23 | assert result.exit_code == 0 24 | assert result.output == f'{expected}\n' 25 | 26 | 27 | def test_cli_example_group_output(): 28 | runner = CliRunner() 29 | result = runner.invoke(cli, ['example', '--group']) 30 | 31 | with open(GROUPS_EXAMPLE_FILE, 'r') as f: 32 | expected = f.read() 33 | 34 | assert result.exit_code == 0 35 | assert result.output == f'{expected}\n' 36 | 37 | 38 | def test_cli_create_with_invalid_data_output(): 39 | runner = CliRunner() 40 | result = runner.invoke(cli, ['create', mock_invalid_guest_data_file]) 41 | 42 | assert result.exit_code == 1 43 | assert result.output == "{'vagrant_box': [{'name': ['empty values not allowed']}]}\n" 44 | 45 | 46 | def test_load_datafile_with_unknown_file_raises_system_exit(): 47 | with pytest.raises(SystemExit): 48 | load_data_file('/some/fake/file') 49 | -------------------------------------------------------------------------------- /tests/test_constants.py: -------------------------------------------------------------------------------- 1 | from grifter.constants import BLACKHOLE_LOOPBACK_MAP 2 | 3 | 4 | def test_blackhole_loopback_map(): 5 | assert BLACKHOLE_LOOPBACK_MAP == {'blackhole': '127.6.6.6'} 6 | -------------------------------------------------------------------------------- /tests/test_custom_filters.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from grifter.custom_filters import explode_port 4 | 5 | test_ports = [ 6 | (0, 10000), 7 | (10, 10010), 8 | ] 9 | 10 | 11 | @pytest.mark.parametrize("a,expected", test_ports) 12 | def test_port_explosion(a, expected): 13 | assert explode_port(a) == expected 14 | 15 | 16 | def test_blackhole_port_expolosion(): 17 | assert explode_port(666) == 10666 18 | 19 | 20 | def test_port_explosion_non_int_raises_exception(): 21 | with pytest.raises(AttributeError): 22 | explode_port('1') 23 | 24 | 25 | def test_port_explosion_greater_than_999_raises_exception(): 26 | with pytest.raises(AttributeError): 27 | explode_port(100) 28 | -------------------------------------------------------------------------------- /tests/test_defaults.py: -------------------------------------------------------------------------------- 1 | from grifter.constants import ALL_GUEST_DEFAULTS 2 | 3 | expected_guest_defaults = { 4 | 'vagrant_box': { 5 | 'name': '', 6 | 'version': '', 7 | 'url': '', 8 | 'provider': 'libvirt', 9 | 'guest_type': '', 10 | 'boot_timeout': 0, 11 | 'throttle_cpu': 0 12 | }, 13 | 'ssh': { 14 | 'username': '', 15 | 'password': '', 16 | 'insert_key': False, 17 | }, 18 | 'synced_folder': { 19 | 'enabled': False, 20 | }, 21 | 'provider_config': { 22 | 'random_hostname': False, 23 | 'nic_adapter_count': 0, 24 | 'disk_bus': '', 25 | 'cpus': 1, 26 | 'memory': 512, 27 | 'huge_pages': False, 28 | 'management_network_mac': '', 29 | 'nic_model_type': '', 30 | 'storage_pool': '', 31 | 'additional_storage_volumes': [] 32 | }, 33 | 'data_interfaces': [], 34 | 'internal_interfaces': [], 35 | 'reserved_interfaces': [] 36 | } 37 | 38 | expected_guest_config_defaults = { 39 | 'data_interface_base': '', 40 | 'data_interface_offset': 0, 41 | 'internal_interfaces': 0, 42 | 'max_data_interfaces': 8, 43 | 'management_interface': 'mgmt', 44 | 'reserved_interfaces': 0, 45 | } 46 | 47 | 48 | def test_default_values(): 49 | assert expected_guest_defaults == ALL_GUEST_DEFAULTS['guest_defaults'] 50 | 51 | 52 | def test_config_default_values(): 53 | assert expected_guest_config_defaults == ALL_GUEST_DEFAULTS['guest_config_defaults'] 54 | -------------------------------------------------------------------------------- /tests/test_loaders.py: -------------------------------------------------------------------------------- 1 | import time 2 | import pytest 3 | from unittest import mock 4 | 5 | from grifter.utils import get_uuid 6 | from grifter.constants import ( 7 | BASE_DIR, 8 | TIMESTAMP_FORMAT, 9 | ) 10 | from grifter.constants import TEMPLATES_DIR 11 | from grifter.custom_filters import ( 12 | explode_port, 13 | ) 14 | from grifter.loaders import ( 15 | render_from_template, 16 | load_data 17 | ) 18 | from grifter.api import ( 19 | generate_loopbacks, 20 | generate_guest_interface_mappings, 21 | ) 22 | from .mock_data import ( 23 | mock_vagrantfile, 24 | mock_guest_data, 25 | ) 26 | 27 | custom_filters = [ 28 | explode_port, 29 | ] 30 | 31 | interface_mappings = generate_guest_interface_mappings() 32 | 33 | 34 | @mock.patch('random.randint', return_value=255) 35 | @mock.patch('uuid.uuid5', return_value="688c29aa-e657-5d27-b4bb-d745aad2812e") 36 | @mock.patch('time.strftime', return_value='2018-12-26--17-58-55') 37 | def test_render_from_template(mock_random, mock_uuid, mock_time): 38 | time_now = time.strftime(TIMESTAMP_FORMAT) 39 | loopbacks = generate_loopbacks(mock_guest_data) 40 | vagrantfile = render_from_template( 41 | template_name='guest.j2', 42 | template_directory=TEMPLATES_DIR, 43 | custom_filters=custom_filters, 44 | guests=mock_guest_data, 45 | loopbacks=loopbacks, 46 | interface_mappings=interface_mappings, 47 | domain_uuid=get_uuid(), 48 | creation_time=time_now, 49 | blackhole_interfaces={}, 50 | ) 51 | assert vagrantfile == mock_vagrantfile 52 | 53 | 54 | def test_load_data_with_invalid_data_type_raises_attribute_error(): 55 | with pytest.raises(AttributeError): 56 | load_data('blah', data_type='invalid') 57 | 58 | 59 | def test_load_json_data(): 60 | data = load_data(f'{BASE_DIR}/../tests/mock_json_data.json', data_type='json') 61 | assert {'some': 'data'} == data 62 | -------------------------------------------------------------------------------- /tests/test_render.py: -------------------------------------------------------------------------------- 1 | import copy 2 | import time 3 | 4 | from unittest import mock 5 | 6 | from grifter.utils import get_uuid 7 | from grifter.api import ( 8 | generate_loopbacks, 9 | update_guest_additional_storage, 10 | generate_guest_interface_mappings, 11 | ) 12 | from grifter.loaders import render_from_template 13 | from grifter.constants import ( 14 | TEMPLATES_DIR, 15 | TIMESTAMP_FORMAT) 16 | from grifter.custom_filters import ( 17 | explode_port, 18 | ) 19 | from tests.mock_data import ( 20 | mock_guest_data, 21 | mock_additional_storage_volumes, 22 | mock_vagrantfile_with_additional_storage_volumes, 23 | ) 24 | 25 | custom_filters = [ 26 | explode_port, 27 | ] 28 | 29 | 30 | @mock.patch('os.path.getsize', return_value='10000') 31 | @mock.patch('random.randint', return_value=255) 32 | @mock.patch('uuid.uuid5', return_value="688c29aa-e657-5d27-b4bb-d745aad2812e") 33 | @mock.patch('time.strftime', return_value='2018-12-26--17-58-55') 34 | def test_render_vagrantfile_with_additional_storage_interfaces(mock_storage_size, mock_random, 35 | mock_uuid, mock_time): 36 | time_now = time.strftime(TIMESTAMP_FORMAT) 37 | guest_data = copy.deepcopy(mock_guest_data) 38 | loopbacks = generate_loopbacks(guest_data) 39 | guest_data['sw01']['provider_config']['additional_storage_volumes'] = mock_additional_storage_volumes 40 | update_guest_additional_storage(guest_data) 41 | 42 | vagrantfile = render_from_template( 43 | template_name='guest.j2', 44 | template_directory=TEMPLATES_DIR, 45 | custom_filters=custom_filters, 46 | guests=guest_data, 47 | loopbacks=loopbacks, 48 | interface_mappings=generate_guest_interface_mappings(), 49 | domain_uuid=get_uuid(), 50 | creation_time=time_now, 51 | blackhole_interfaces={}, 52 | ) 53 | 54 | assert mock_vagrantfile_with_additional_storage_volumes == vagrantfile 55 | 56 | 57 | def test_blackhole_interfaces_trigger_rendering(): 58 | guest = 'sw01' 59 | blackhole_interfaces = {'sw01': ['swp1', 'swp2']} 60 | expected = """ 61 | blackhole_interfaces = ["swp1", "swp2"] 62 | blackhole_interfaces.each do |interface| 63 | node.trigger.after :up do |trigger| 64 | trigger.info = "Shutting down #{guest_name}-#{interface}" 65 | trigger.run = {inline: "virsh domif-setlink #{domain_prefix}_#{guest_name} #{guest_name}-#{interface}-#{domain_uuid} down"} 66 | end 67 | end 68 | """ 69 | result = render_from_template( 70 | template_name='blackhole-interfaces-trigger.j2', 71 | template_directory=TEMPLATES_DIR, 72 | guest=guest, 73 | blackhole_interfaces=blackhole_interfaces, 74 | ) 75 | assert result == expected 76 | 77 | 78 | def test_throttle_cpu_trigger_rendering(): 79 | data = { 80 | 'vagrant_box': {'throttle_cpu': 33} 81 | } 82 | expected = """ 83 | node.trigger.after :up do |trigger| 84 | trigger.info = "Throttling #{domain_prefix}_#{guest_name} CPU" 85 | trigger.run = {inline: "virsh schedinfo #{domain_prefix}_#{guest_name} --set vcpu_quota=33000"} 86 | end 87 | """ 88 | result = render_from_template( 89 | template_name='throttle-cpu-trigger.j2', 90 | template_directory=TEMPLATES_DIR, 91 | data=data, 92 | ) 93 | assert result == expected 94 | -------------------------------------------------------------------------------- /tests/test_utils.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from grifter.utils import ( 4 | get_mac, 5 | remove_duplicates, 6 | sort_nicely, 7 | dict_merge 8 | ) 9 | 10 | 11 | def test_get_mac_returns_default_oui(): 12 | assert get_mac()[0:8] == '28:b7:ad' 13 | 14 | 15 | def test_get_mac_returns_custom_oui(): 16 | assert get_mac('00:00:00')[0:8] == '00:00:00' 17 | 18 | 19 | def test_remote_duplicates(): 20 | data = [(0, 1, 2, 3), (2, 3, 0, 1)] 21 | expected = [(0, 1, 2, 3)] 22 | assert remove_duplicates(data) == expected 23 | 24 | 25 | def test_sort_nicely(): 26 | data = [ 27 | 'p1r50-ge-0/0/9 <--> p1sw10-swp5', 28 | 'p1r1-ge-0/0/2 <--> p1r2-ge-0/0/1', 29 | 'p1r5-ge-0/0/9 <--> p1sw1-swp5', 30 | 'p1r2-ge-0/0/9 <--> p1sw1-swp2', 31 | ] 32 | expected = [ 33 | 'p1r1-ge-0/0/2 <--> p1r2-ge-0/0/1', 34 | 'p1r2-ge-0/0/9 <--> p1sw1-swp2', 35 | 'p1r5-ge-0/0/9 <--> p1sw1-swp5', 36 | 'p1r50-ge-0/0/9 <--> p1sw10-swp5', 37 | ] 38 | assert expected == sort_nicely(data) 39 | 40 | 41 | def test_sort_nicely_empty_list_returns_empty_list(): 42 | data = [] 43 | expected = [] 44 | assert expected == sort_nicely(data) 45 | 46 | 47 | def test_sort_nicely_non_list_type_raises_attribute_error(): 48 | with pytest.raises(AttributeError): 49 | sort_nicely('') 50 | 51 | 52 | def test_dict_merge(): 53 | a = {1: {"a": "A"}, 2: {"b": "B", "c": "C"}, 3: [{1: 2}]} 54 | b = {1: {"a": "A"}, 2: {"b": "D"}, 3: [{4: 5}], 4: {'x': 'y'}, 5: 6} 55 | expected = {1: {'a': 'A'}, 2: {'b': 'D', 'c': 'C'}, 3: [{4: 5}], 4: {'x': 'y'}, 5: 6} 56 | assert dict_merge(a, b) == expected 57 | -------------------------------------------------------------------------------- /tests/test_validators.py: -------------------------------------------------------------------------------- 1 | import copy 2 | import pytest 3 | 4 | from .mock_data import mock_guest_data 5 | 6 | from grifter.api import ( 7 | generate_guest_interface_mappings, 8 | get_default_config 9 | ) 10 | from grifter.constants import ( 11 | GUEST_SCHEMA_FILE, 12 | DEFAULT_CONFIG_FILE, 13 | ) 14 | 15 | from grifter.loaders import load_data 16 | 17 | from grifter.validators import ( 18 | validate_required_keys, 19 | validate_required_values, 20 | validate_schema, 21 | validate_guests_in_guest_config, 22 | validate_guest_interfaces, 23 | validate_data, 24 | validate_config, 25 | ) 26 | 27 | config = load_data(DEFAULT_CONFIG_FILE) 28 | interface_mappings = generate_guest_interface_mappings() 29 | 30 | 31 | def guest_data(): 32 | return copy.deepcopy(mock_guest_data) 33 | 34 | 35 | def mock_guest_remove_key(popme): 36 | guest = copy.deepcopy(mock_guest_data['sw01']) 37 | return guest.pop(popme) 38 | 39 | 40 | @pytest.mark.parametrize('value', [ 41 | 'vagrant_box', 42 | ]) 43 | def test_validate_missing_required_key_name_raises_attribute_error(value): 44 | with pytest.raises(AttributeError): 45 | validate_required_keys(mock_guest_remove_key(value)) 46 | 47 | 48 | def test_validate_missing_required_key_vagrant_box_name_raises_attribute_error(): 49 | guest = guest_data() 50 | guest['sw01']['vagrant_box'].pop('name') 51 | with pytest.raises(AttributeError): 52 | validate_required_keys(guest['sw01']) 53 | 54 | 55 | def test_validate_empty_required_vagrant_box_name_raises_value_error(): 56 | guest = guest_data() 57 | guest['sw01']['vagrant_box']['name'] = '' 58 | with pytest.raises(ValueError): 59 | validate_required_values(guest['sw01']) 60 | 61 | 62 | def test_validate_schema(): 63 | data = {'blah': ''} 64 | schema = {'blah': {'type': 'string'}} 65 | result = validate_schema(data, schema) 66 | assert not result.errors 67 | 68 | 69 | def test_validate_guest_schema(): 70 | data = {'vagrant_box': {'name': 'box-name'}} 71 | schema = load_data(GUEST_SCHEMA_FILE) 72 | result = validate_schema(data, schema) 73 | assert not result.errors 74 | 75 | 76 | def test_validate_guests_in_guest_config_with_valid_config_returns_true(): 77 | assert validate_guests_in_guest_config(mock_guest_data, config) is True 78 | 79 | 80 | def test_validate_guests_in_guest_config_with_box_not_in_config_raises_attribute_error(): 81 | data = guest_data() 82 | data['sw01']['vagrant_box']['name'] = 'blah/blah' 83 | with pytest.raises(AttributeError): 84 | validate_guests_in_guest_config(data, config) 85 | 86 | 87 | def test_validate_guests_in_guest_config_with_missing_remote_guest_raises_attribute_error(): 88 | data = guest_data() 89 | data['sw01']['data_interfaces'][0]['remote_guest'] = 'blah' 90 | with pytest.raises(AttributeError): 91 | validate_guests_in_guest_config(data, config) 92 | 93 | 94 | def test_validate_guest_interfaces_more_data_interfaces_than_max_raises_attribute_error(): 95 | data = guest_data() 96 | data['sw01']['provider_config']['nic_adapter_count'] = 100 97 | with pytest.raises(AttributeError): 98 | validate_guest_interfaces(data, config, interface_mappings) 99 | 100 | 101 | def test_validate_guest_interfaces_local_port_outside_range_raises_attribute_error(): 102 | data = guest_data() 103 | data['sw01']['data_interfaces'][0]['local_port'] = 100 104 | with pytest.raises(AttributeError): 105 | validate_guest_interfaces(data, config, interface_mappings) 106 | 107 | 108 | def test_validate_guest_interfaces_remote_port_outside_range_raises_attribute_error(): 109 | data = guest_data() 110 | data['sw01']['data_interfaces'][0]['remote_port'] = 100 111 | with pytest.raises(AttributeError): 112 | validate_guest_interfaces(data, config, interface_mappings) 113 | 114 | 115 | def test_number_of_internal_interfaces(): 116 | data = guest_data() 117 | data['sw01']['internal_interfaces'] = [1, 2] 118 | with pytest.raises(AttributeError): 119 | validate_guest_interfaces(data, config, interface_mappings) 120 | 121 | 122 | def test_validate_guest_interfaces_with_valid_data_returns_true(): 123 | data = guest_data() 124 | result = validate_guest_interfaces(data, config, interface_mappings) 125 | assert result is True 126 | 127 | 128 | def test_validate_data_returns_list(): 129 | result = validate_data({'guests': {}}) 130 | assert isinstance(result, list) 131 | 132 | 133 | def test_validate_data_with_valid_data_returns_no_errors_in_empty_list(): 134 | result = validate_data(mock_guest_data) 135 | assert not result 136 | 137 | 138 | def test_validate_data_with_invalid_data_returns_list_of_errors(): 139 | # missing vagrant box name field value 140 | data = {'sw01': {'vagrant_box': {'name': ''}}} 141 | result = validate_data(data) 142 | assert result 143 | 144 | 145 | def test_validate_config_default_config_returns_no_errors(): 146 | default_config = get_default_config() 147 | result = validate_config(default_config) 148 | assert not result 149 | --------------------------------------------------------------------------------