diff --git a/nscharm_ns/charms/ns/.build.manifest b/nscharm_ns/charms/ns/.build.manifest new file mode 100644 index 0000000000000000000000000000000000000000..c37dc030b02dc8626088d6851d7db60090073fe9 --- /dev/null +++ b/nscharm_ns/charms/ns/.build.manifest @@ -0,0 +1,277 @@ +{ + "layers": [ + { + "rev": "fcdcea4e5de3e1556c24e6704607862d0ba00a56", + "url": "layer:options" + }, + { + "rev": "1d2489bff56daf2f8d1c06ee9248bb0094e4ad49", + "url": "layer:basic" + }, + { + "rev": "4f3159205af1b3e7a3a532dedcf6b887d2e6d78a", + "url": "layer:osm-ns" + }, + { + "rev": "aa34ff8bbe4d847102174d9038068af0f6ebe575", + "url": "ns" + } + ], + "signatures": { + ".build.manifest": [ + "build", + "dynamic", + "unchecked" + ], + ".gitignore": [ + "layer:basic", + "static", + "0da5c4dcda27cd6406e5bb81cbf68ddccaf728ac764ec15053a165c1449d87d9" + ], + "LICENSE": [ + "layer:basic", + "static", + "cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30" + ], + "Makefile": [ + "layer:basic", + "static", + "b7ab3a34e5faf79b96a8632039a0ad0aa87f2a9b5f0ba604e007cafb22190301" + ], + "README.md": [ + "ns", + "static", + "2d66b85187cdce4854d3a5a39f7458faf882b919b47a83a675654cdcaa520742" + ], + "actions.yaml": [ + "ns", + "dynamic", + "d3eeb0c4a2386661202166c19774b6ae9e04ae71dda52a027bffdf67075a1448" + ], + "actions/add-user": [ + "ns", + "static", + "46bf4be02078a0f5ac60ab81609372a8f9943885b7a4999ce5332ea2f4857036" + ], + "bin/charm-env": [ + "layer:basic", + "static", + "458c53532c19ee357cbf4209ccc7d811810718ba0ea3b0588b3d3ef040a44b8e" + ], + "bin/layer_option": [ + "layer:options", + "static", + "e959bf29da4c5edff28b2602c24113c4df9e25cdc9f2aa3b5d46c8577b2a40cc" + ], + "config.yaml": [ + "ns", + "dynamic", + "ae697230ea4c0e9ac8ec8fa039dbb11304cad42011adcaa40a8412c47159324b" + ], + "copyright": [ + "layer:basic", + "static", + "f6740d66fd60b60f2533d9fcb53907078d1e20920a0219afce7182e2a1c97629" + ], + "copyright.layer-options": [ + "layer:options", + "static", + "f6740d66fd60b60f2533d9fcb53907078d1e20920a0219afce7182e2a1c97629" + ], + "hooks/config-changed": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/hook.template": [ + "layer:basic", + "static", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/install": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/leader-elected": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/leader-settings-changed": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/post-series-upgrade": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/pre-series-upgrade": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/start": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/stop": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/update-status": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/upgrade-charm": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "icon.svg": [ + "ns", + "static", + "d20624e9389af6506a8d8a69ac9bba4d41709601b624c0875fd7d6717b395088" + ], + "layer.yaml": [ + "ns", + "dynamic", + "6e5423e1b33993267456ad163ecf0fcd9e921f99214fbc6f23a1d7902468df8f" + ], + "lib/charms/layer/__init__.py": [ + "layer:basic", + "static", + "dfe0d26c6bf409767de6e2546bc648f150e1b396243619bad3aa0553ab7e0e6f" + ], + "lib/charms/layer/basic.py": [ + "layer:basic", + "static", + "445652dbaa1f0b84a7215da185bcbdff097bb9bbbce11b4c2dbbff90f77719a9" + ], + "lib/charms/layer/execd.py": [ + "layer:basic", + "static", + "fda8bd491032db1db8ddaf4e99e7cc878c6fb5432efe1f91cadb5b34765d076d" + ], + "lib/charms/layer/options.py": [ + "layer:options", + "static", + "8ae7a07d22542fc964f2d2bee8219d1c78a68dace70a1b38d36d4aea47b1c3b2" + ], + "lib/charms/osm/ns.py": [ + "layer:osm-ns", + "static", + "743c4d49f0bd5a143057a0370fc47a837e0ee43d8eceff8e90b7f05e4db7b1e9" + ], + "metadata.yaml": [ + "ns", + "dynamic", + "19db902355c43e4f6c41291e48c7a1d1c789a13774a49b524d1d052dbbacc32f" + ], + "reactive/__init__.py": [ + "layer:basic", + "static", + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + ], + "reactive/ns.py": [ + "ns", + "static", + "1d1c34c9cdb049552d0c79219cade41e70ae4d76782a305be0e8d4d6928afeb4" + ], + "reactive/osm_ns.py": [ + "layer:osm-ns", + "static", + "d29161a94d60dc8666ede44e4bfe7f51fe052f919eb9c03db03b2111a8f2b68c" + ], + "requirements.txt": [ + "layer:basic", + "static", + "0f1c70d27e26005a96d66ad54482877ae20f7737693c833e29dd72bd6ac24892" + ], + "tests/00-setup": [ + "ns", + "static", + "111c079b81d260bbcd716dcf41672372a4cf4aaa14154b6c3055deeedae37a06" + ], + "tests/10-deploy": [ + "ns", + "static", + "1750031833a1d3e3dfa1e7f5ee97f75cb84ac2e483b1470b2b41e17f0b39c173" + ], + "version": [ + "ns", + "dynamic", + "260a9a4811db0cebf6acd4bb4910f460278b8c449d7437a7c908ce864fc6a184" + ], + "wheelhouse/Jinja2-2.10.3.tar.gz": [ + "layer:basic", + "dynamic", + "9fe95f19286cfefaa917656583d020be14e7859c6b0252588391e47db34527de" + ], + "wheelhouse/MarkupSafe-1.1.1.tar.gz": [ + "layer:basic", + "dynamic", + "29872e92839765e546828bb7754a68c418d927cd064fd4708fab9fe9c8bb116b" + ], + "wheelhouse/PyYAML-5.2.tar.gz": [ + "layer:basic", + "dynamic", + "c0ee8eca2c582d29c3c2ec6e2c4f703d1b7f1fb10bc72317355a746057e7346c" + ], + "wheelhouse/Tempita-0.5.2.tar.gz": [ + "layer:basic", + "dynamic", + "cacecf0baa674d356641f1d406b8bff1d756d739c46b869a54de515d08e6fc9c" + ], + "wheelhouse/charmhelpers-0.20.7.tar.gz": [ + "layer:basic", + "dynamic", + "e0f8d005d39cded1b0c5997d8ef1d90832341c67ebeb4a334ad1eb348fbd803a" + ], + "wheelhouse/charms.reactive-1.3.0.tar.gz": [ + "layer:basic", + "dynamic", + "82d2c614c82d64bf56e913990f22663e5de64b99db15838abfd9a064f1cc2f51" + ], + "wheelhouse/netaddr-0.7.19.tar.gz": [ + "layer:basic", + "dynamic", + "38aeec7cdd035081d3a4c306394b19d677623bf76fa0913f6695127c7753aefd" + ], + "wheelhouse/pip-18.1.tar.gz": [ + "layer:basic", + "dynamic", + "c0a292bd977ef590379a3f05d7b7f65135487b67470f6281289a94e015650ea1" + ], + "wheelhouse/pyaml-19.12.0.tar.gz": [ + "layer:basic", + "dynamic", + "b3f636b467864319d7ded1558f86bb305b8612a274f5d443a62dc5eceb1b7176" + ], + "wheelhouse/setuptools-41.6.0.zip": [ + "layer:basic", + "dynamic", + "6afa61b391dcd16cb8890ec9f66cc4015a8a31a6e1c2b4e0c464514be1a3d722" + ], + "wheelhouse/setuptools_scm-1.17.0.tar.gz": [ + "layer:basic", + "dynamic", + "70a4cf5584e966ae92f54a764e6437af992ba42ac4bca7eb37cc5d02b98ec40a" + ], + "wheelhouse/six-1.13.0.tar.gz": [ + "layer:basic", + "dynamic", + "30f610279e8b2578cab6db20741130331735c781b56053c59c4076da27f06b66" + ], + "wheelhouse/wheel-0.33.6.tar.gz": [ + "layer:basic", + "dynamic", + "10c9da68765315ed98850f8e048347c3eb06dd81822dc2ab1d4fde9dc9702646" + ] + } +} \ No newline at end of file diff --git a/nscharm_ns/charms/ns/.gitignore b/nscharm_ns/charms/ns/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..56e95aac517af399ce55d6b518634be955772bd4 --- /dev/null +++ b/nscharm_ns/charms/ns/.gitignore @@ -0,0 +1,5 @@ +*.pyc +*~ +.ropeproject +.settings +.tox diff --git a/nscharm_ns/charms/ns/LICENSE b/nscharm_ns/charms/ns/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..d645695673349e3947e8e5ae42332d0ac3164cd7 --- /dev/null +++ b/nscharm_ns/charms/ns/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/nscharm_ns/charms/ns/Makefile b/nscharm_ns/charms/ns/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..a1ad3a5cd27751144f6bd1a0a7db50f1e11b18eb --- /dev/null +++ b/nscharm_ns/charms/ns/Makefile @@ -0,0 +1,24 @@ +#!/usr/bin/make + +all: lint unit_test + + +.PHONY: clean +clean: + @rm -rf .tox + +.PHONY: apt_prereqs +apt_prereqs: + @# Need tox, but don't install the apt version unless we have to (don't want to conflict with pip) + @which tox >/dev/null || (sudo apt-get install -y python-pip && sudo pip install tox) + +.PHONY: lint +lint: apt_prereqs + @tox --notest + @PATH=.tox/py34/bin:.tox/py35/bin flake8 $(wildcard hooks reactive lib unit_tests tests) + @charm proof + +.PHONY: unit_test +unit_test: apt_prereqs + @echo Starting tests... + tox diff --git a/nscharm_ns/charms/ns/README.md b/nscharm_ns/charms/ns/README.md new file mode 100644 index 0000000000000000000000000000000000000000..1bc51de483e4486c91b3084035a2a2dff38103f2 --- /dev/null +++ b/nscharm_ns/charms/ns/README.md @@ -0,0 +1,117 @@ +# Overview + +This is an example of an Open Source Mano (OSM) Network Service (ns) charm. This is an **experimental** feature, expected to be released with OSM R6 in the spring of 2019. + +This allows a charm to coordinate the execution of [actions] across multiple charms within a model. + +# Usage + +# Application Names + +Each deployed application is named using a set of runtime variables, such as the network service name, the vnf or vdu name, and the vnf-member-index. Because of that, we'll need to provide those values to the NS charm through its configuration. + +# Configuration + +The `config.yaml` file contains a mapping of properties that will be passed from OSM to your deployed charm. Some of those properties are automatically included by the underlying `osm-ns` layer: +- juju-username +- juju-password + +You'll need to pass the name of the network service that you deployed. Even though your network service may be named `nscharm` in the descriptor, the operator provides a name at instantiation time, and `osm-ns` needs this name in order to resolve the application names. You can call it anything you like, as long as it's consistent between the NS charm and the NS Descriptor. + +For each VNF or VDU with a charm you wish to interact with, you need to also pass it's id from the VNF Descriptor. Again, you can call this anything you like, as long as it's consistent between the NS charm and NS Descriptor. + +`config.yaml`: +```yaml +options: + nsr-name: + default: + description: The runtime name of the Network Service, i.e., what its deployed name is. + type: string + + user-member-index: + default: + description: The vnf-member-index of the user VNF. + type: string + user-vdu-id: + default: + description: The id of the VDU containing the charm. + type: string + + policy-member-index: + default: + description: The vnf-member-index of the policy VNF. + type: string + policy-vdu-id: + default: + description: The id of the VDU containing the charm. + type: string +``` + +# NS Descriptor + +The NS descriptor contains a new `ns-configuration` element, similar to that of `vnf-configuration` and `vdu-configuration` available in VNF descriptors. + +The key here is to pass the juju credentials from your environment, available in `~/.local/share/juju/accounts.yaml`. This enables the charm to speak directly to Juju, via the `osm-ns` layer. + +Next, you must map the runtime parameters of your Network Service so that the charm is aware of your topology. + +Lastly, you can then define the primitives, under `config-primitive`, to be available to the operator for day-2 operation. + +```yaml + ns-configuration: + juju: + charm: ns + initial-config-primitive: + - seq: '1' + name: config + parameter: + # Configure Juju credentials + - name: juju-username + value: 'admin' + - name: juju-password + value: '50b4491a7a42d3542e317e3ae94c6c96' + + # Set the runtime name of the network service + - name: nsr-name + value: 'test' + + # For each vnf, map the vnf-member-index and vdu-id + - name: user-member-index + value: '1' + - name: user-vdu-id + value: 'userVM' + + - name: policy-member-index + value: '2' + - name: policy-vdu-id + value: 'policyVM' + + - seq: '2' + name: add-user + parameter: + - name: username + value: root + config-primitive: + - name: add-user + parameter: + - name: username + data-type: STRING +``` + +## Known Limitations and Issues + +This functionality is EXPERIMENTAL. + +# Configuration + +# Contact Information + + +## Open Source Mano (OSM) + + - [OSM website](https://osm.etsi.org/) + - [OSM bug tracker](https://osm.etsi.org/bugzilla/) + - [OSM_TECH](mailto:OSM_TECH@list.etsi.org) mailing list + - [Slack](https://join.slack.com/t/opensourcemano/shared_invite/enQtMzQ3MzYzNTQ0NDIyLWJkMzRjNDM0MjFjODYzMGQ3ODIzMzJlNTg2ZGI5OTdiZjFiNDMyMzYxMjRjNDU4N2FmNjRjNzY5NTE1MjgzOTQ) + +[actions]: https://docs.jujucharms.com/2.5/en/actions diff --git a/nscharm_ns/charms/ns/actions.yaml b/nscharm_ns/charms/ns/actions.yaml new file mode 100644 index 0000000000000000000000000000000000000000..74a4fdfe0628413272e9ac2d42ec600b39b0f60f --- /dev/null +++ b/nscharm_ns/charms/ns/actions.yaml @@ -0,0 +1,42 @@ +## +# Copyright 2016 Canonical Ltd. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +## + +"add-user": + "description": "Add a user" + "params": + "username": + "description": "The username to add" + "type": "string" + "default": "" + "bw": + "description": "" + "type": "integer" + "default": !!int "0" + "qos": + "description": "" + "type": "integer" + "default": !!int "0" + "tariff": + "description": "" + "type": "integer" + "default": !!int "0" + + "required": + # We're only requiring username here, and will use the default values + # for the remaining parameters. This can be changed by adding the + # required parameter names below. + - "username" diff --git a/nscharm_ns/charms/ns/actions/add-user b/nscharm_ns/charms/ns/actions/add-user new file mode 100755 index 0000000000000000000000000000000000000000..7e30af4cfa084120a9343bf71c421b69ec3f2967 --- /dev/null +++ b/nscharm_ns/charms/ns/actions/add-user @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +## +# Copyright 2016 Canonical Ltd. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +## +import sys +sys.path.append('lib') + +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +""" +`set_state` only works here because it's flushed to disk inside the `main()` +loop. remove_state will need to be called inside the action method. +""" +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_ns/charms/ns/bin/charm-env b/nscharm_ns/charms/ns/bin/charm-env new file mode 100755 index 0000000000000000000000000000000000000000..aca7de3c1022ecad619cb82a4ac3160fb6b5677d --- /dev/null +++ b/nscharm_ns/charms/ns/bin/charm-env @@ -0,0 +1,107 @@ +#!/bin/bash + +VERSION="1.0.0" + + +find_charm_dirs() { + # Hopefully, $JUJU_CHARM_DIR is set so which venv to use in unambiguous. + if [[ -n "$JUJU_CHARM_DIR" || -n "$CHARM_DIR" ]]; then + if [[ -z "$JUJU_CHARM_DIR" ]]; then + # accept $CHARM_DIR to be more forgiving + export JUJU_CHARM_DIR="$CHARM_DIR" + fi + if [[ -z "$CHARM_DIR" ]]; then + # set CHARM_DIR as well to help with backwards compatibility + export CHARM_DIR="$JUJU_CHARM_DIR" + fi + return + fi + # Try to guess the value for JUJU_CHARM_DIR by looking for a non-subordinate + # (because there's got to be at least one principle) charm directory; + # if there are several, pick the first by alpha order. + agents_dir="/var/lib/juju/agents" + if [[ -d "$agents_dir" ]]; then + desired_charm="$1" + found_charm_dir="" + if [[ -n "$desired_charm" ]]; then + for charm_dir in $(/bin/ls -d "$agents_dir"/unit-*/charm); do + charm_name="$(JUJU_CHARM_DIR="$charm_dir" charm-env python3 -c 'from charmhelpers.core.hookenv import charm_name; print(charm_name())')" + if [[ "$charm_name" == "$desired_charm" ]]; then + if [[ -n "$found_charm_dir" ]]; then + >&2 echo "Ambiguous possibilities for JUJU_CHARM_DIR matching '$desired_charm'; please run within a Juju hook context" + exit 1 + fi + found_charm_dir="$charm_dir" + fi + done + if [[ -z "$found_charm_dir" ]]; then + >&2 echo "Unable to determine JUJU_CHARM_DIR matching '$desired_charm'; please run within a Juju hook context" + exit 1 + fi + export JUJU_CHARM_DIR="$found_charm_dir" + export CHARM_DIR="$found_charm_dir" + return + fi + # shellcheck disable=SC2126 + non_subordinates="$(grep -L 'subordinate:.*true' "$agents_dir"/unit-*/charm/metadata.yaml | wc -l)" + if [[ "$non_subordinates" -gt 1 ]]; then + >&2 echo 'Ambiguous possibilities for JUJU_CHARM_DIR; please use --charm or run within a Juju hook context' + exit 1 + elif [[ "$non_subordinates" -eq 1 ]]; then + for charm_dir in $(/bin/ls -d "$agents_dir"/unit-*/charm); do + if grep -q 'subordinate:.*true' "$charm_dir/metadata.yaml"; then + continue + fi + export JUJU_CHARM_DIR="$charm_dir" + export CHARM_DIR="$charm_dir" + return + done + fi + fi + >&2 echo 'Unable to determine JUJU_CHARM_DIR; please run within a Juju hook context' + exit 1 +} + +try_activate_venv() { + if [[ -d "$JUJU_CHARM_DIR/../.venv" ]]; then + . "$JUJU_CHARM_DIR/../.venv/bin/activate" + fi +} + +find_wrapped() { + PATH="${PATH/\/usr\/local\/sbin:}" which "$(basename "$0")" +} + + +if [[ "$1" == "--version" || "$1" == "-v" ]]; then + echo "$VERSION" + exit 0 +fi + + +# allow --charm option to hint which JUJU_CHARM_DIR to choose when ambiguous +# NB: --charm option must come first +# NB: option must be processed outside find_charm_dirs to modify $@ +charm_name="" +if [[ "$1" == "--charm" ]]; then + charm_name="$2" + shift; shift +fi + +find_charm_dirs "$charm_name" +try_activate_venv +export PYTHONPATH="$JUJU_CHARM_DIR/lib:$PYTHONPATH" + +if [[ "$(basename "$0")" == "charm-env" ]]; then + # being used as a shebang + exec "$@" +elif [[ "$0" == "$BASH_SOURCE" ]]; then + # being invoked as a symlink wrapping something to find in the venv + exec "$(find_wrapped)" "$@" +elif [[ "$(basename "$BASH_SOURCE")" == "charm-env" ]]; then + # being sourced directly; do nothing + /bin/true +else + # being sourced for wrapped bash helpers + . "$(find_wrapped)" +fi diff --git a/nscharm_ns/charms/ns/bin/layer_option b/nscharm_ns/charms/ns/bin/layer_option new file mode 100755 index 0000000000000000000000000000000000000000..3253ef8aadb95807f26e5ebd8486adde67e5cbc2 --- /dev/null +++ b/nscharm_ns/charms/ns/bin/layer_option @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +import sys +import argparse +from charms import layer + + +parser = argparse.ArgumentParser(description='Access layer options.') +parser.add_argument('section', + help='the section, or layer, the option is from') +parser.add_argument('option', + help='the option to access') + +args = parser.parse_args() +value = layer.options.get(args.section, args.option) +if isinstance(value, bool): + sys.exit(0 if value else 1) +elif isinstance(value, list): + for val in value: + print(val) +else: + print(value) diff --git a/nscharm_ns/charms/ns/config.yaml b/nscharm_ns/charms/ns/config.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1dcfabea1e8861891afa3bccedf6f55bc587fdb1 --- /dev/null +++ b/nscharm_ns/charms/ns/config.yaml @@ -0,0 +1,31 @@ +"options": + "juju-username": + "type": "string" + "default": "" + "description": "The Juju username to authenticate with." + "juju-password": + "type": "string" + "default": "" + "description": "The Juju password to authenticate with." + "ns_config_info": + "type": "string" + "default": "" + "description": "The NS_CONFIG_INFO containing the mapping of NS object to application\ + \ names" + "user-member-index": + "default": !!null "" + "description": "The vnf-member-index of the user VNF." + "type": "string" + "user-vdu-id": + "default": !!null "" + "description": "The id of the VDU containing the charm." + "type": "string" + + "policy-member-index": + "default": !!null "" + "description": "The vnf-member-index of the policy VNF." + "type": "string" + "policy-vdu-id": + "default": !!null "" + "description": "The id of the VDU containing the charm." + "type": "string" diff --git a/nscharm_ns/charms/ns/copyright b/nscharm_ns/charms/ns/copyright new file mode 100644 index 0000000000000000000000000000000000000000..d4fdd18281c632d030a301d26d45b4dabdb308ef --- /dev/null +++ b/nscharm_ns/charms/ns/copyright @@ -0,0 +1,16 @@ +Format: http://dep.debian.net/deps/dep5/ + +Files: * +Copyright: Copyright 2015-2017, Canonical Ltd., All Rights Reserved. +License: Apache License 2.0 + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + . + http://www.apache.org/licenses/LICENSE-2.0 + . + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/nscharm_ns/charms/ns/copyright.layer-options b/nscharm_ns/charms/ns/copyright.layer-options new file mode 100644 index 0000000000000000000000000000000000000000..d4fdd18281c632d030a301d26d45b4dabdb308ef --- /dev/null +++ b/nscharm_ns/charms/ns/copyright.layer-options @@ -0,0 +1,16 @@ +Format: http://dep.debian.net/deps/dep5/ + +Files: * +Copyright: Copyright 2015-2017, Canonical Ltd., All Rights Reserved. +License: Apache License 2.0 + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + . + http://www.apache.org/licenses/LICENSE-2.0 + . + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/nscharm_ns/charms/ns/hooks/config-changed b/nscharm_ns/charms/ns/hooks/config-changed new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_ns/charms/ns/hooks/config-changed @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_ns/charms/ns/hooks/hook.template b/nscharm_ns/charms/ns/hooks/hook.template new file mode 100644 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_ns/charms/ns/hooks/hook.template @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_ns/charms/ns/hooks/install b/nscharm_ns/charms/ns/hooks/install new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_ns/charms/ns/hooks/install @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_ns/charms/ns/hooks/leader-elected b/nscharm_ns/charms/ns/hooks/leader-elected new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_ns/charms/ns/hooks/leader-elected @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_ns/charms/ns/hooks/leader-settings-changed b/nscharm_ns/charms/ns/hooks/leader-settings-changed new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_ns/charms/ns/hooks/leader-settings-changed @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_ns/charms/ns/hooks/post-series-upgrade b/nscharm_ns/charms/ns/hooks/post-series-upgrade new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_ns/charms/ns/hooks/post-series-upgrade @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_ns/charms/ns/hooks/pre-series-upgrade b/nscharm_ns/charms/ns/hooks/pre-series-upgrade new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_ns/charms/ns/hooks/pre-series-upgrade @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_ns/charms/ns/hooks/start b/nscharm_ns/charms/ns/hooks/start new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_ns/charms/ns/hooks/start @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_ns/charms/ns/hooks/stop b/nscharm_ns/charms/ns/hooks/stop new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_ns/charms/ns/hooks/stop @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_ns/charms/ns/hooks/update-status b/nscharm_ns/charms/ns/hooks/update-status new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_ns/charms/ns/hooks/update-status @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_ns/charms/ns/hooks/upgrade-charm b/nscharm_ns/charms/ns/hooks/upgrade-charm new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_ns/charms/ns/hooks/upgrade-charm @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_ns/charms/ns/icon.svg b/nscharm_ns/charms/ns/icon.svg new file mode 100644 index 0000000000000000000000000000000000000000..96a5d0c068d4b3271c7e397f76cf77fdf0651038 --- /dev/null +++ b/nscharm_ns/charms/ns/icon.svg @@ -0,0 +1,279 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + diff --git a/nscharm_ns/charms/ns/layer.yaml b/nscharm_ns/charms/ns/layer.yaml new file mode 100644 index 0000000000000000000000000000000000000000..efb97d72d65b6ceeb3b9ce52af04ee7e5da26af1 --- /dev/null +++ b/nscharm_ns/charms/ns/layer.yaml @@ -0,0 +1,14 @@ +"includes": +- "layer:options" +- "layer:basic" +- "layer:osm-ns" +"exclude": [".travis.yml", "tests", "tox.ini", "test-requirements.txt"] +"options": + "basic": + "use_venv": !!bool "false" + "packages": ["libssl-dev"] + "python_packages": [] + "include_system_packages": !!bool "false" + "osm-ns": {} + "ns": {} +"is": "ns" diff --git a/nscharm_ns/charms/ns/lib/charms/layer/__init__.py b/nscharm_ns/charms/ns/lib/charms/layer/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..a8e0c640642f44cac85df60f3d9b7f73b0bb18bb --- /dev/null +++ b/nscharm_ns/charms/ns/lib/charms/layer/__init__.py @@ -0,0 +1,60 @@ +import sys +from importlib import import_module +from pathlib import Path + + +def import_layer_libs(): + """ + Ensure that all layer libraries are imported. + + This makes it possible to do the following: + + from charms import layer + + layer.foo.do_foo_thing() + + Note: This function must be called after bootstrap. + """ + for module_file in Path('lib/charms/layer').glob('*'): + module_name = module_file.stem + if module_name in ('__init__', 'basic', 'execd') or not ( + module_file.suffix == '.py' or module_file.is_dir() + ): + continue + import_module('charms.layer.{}'.format(module_name)) + + +# Terrible hack to support the old terrible interface. +# Try to get people to call layer.options.get() instead so +# that we can remove this garbage. +# Cribbed from https://stackoverfLow.com/a/48100440/4941864 +class OptionsBackwardsCompatibilityHack(sys.modules[__name__].__class__): + def __call__(self, section=None, layer_file=None): + if layer_file is None: + return self.get(section=section) + else: + return self.get(section=section, + layer_file=Path(layer_file)) + + +def patch_options_interface(): + from charms.layer import options + if sys.version_info.minor >= 5: + options.__class__ = OptionsBackwardsCompatibilityHack + else: + # Py 3.4 doesn't support changing the __class__, so we have to do it + # another way. The last line is needed because we already have a + # reference that doesn't get updated with sys.modules. + name = options.__name__ + hack = OptionsBackwardsCompatibilityHack(name) + hack.get = options.get + sys.modules[name] = hack + sys.modules[__name__].options = hack + + +try: + patch_options_interface() +except ImportError: + # This may fail if pyyaml hasn't been installed yet. But in that + # case, the bootstrap logic will try it again once it has. + pass diff --git a/nscharm_ns/charms/ns/lib/charms/layer/basic.py b/nscharm_ns/charms/ns/lib/charms/layer/basic.py new file mode 100644 index 0000000000000000000000000000000000000000..39319591156a512b6ff46a082f51c3c8382055ec --- /dev/null +++ b/nscharm_ns/charms/ns/lib/charms/layer/basic.py @@ -0,0 +1,286 @@ +import os +import sys +import shutil +from glob import glob +from subprocess import check_call, check_output, CalledProcessError +from time import sleep + +from charms import layer +from charms.layer.execd import execd_preinstall + + +def lsb_release(): + """Return /etc/lsb-release in a dict""" + d = {} + with open('/etc/lsb-release', 'r') as lsb: + for l in lsb: + k, v = l.split('=') + d[k.strip()] = v.strip() + return d + + +def bootstrap_charm_deps(): + """ + Set up the base charm dependencies so that the reactive system can run. + """ + # execd must happen first, before any attempt to install packages or + # access the network, because sites use this hook to do bespoke + # configuration and install secrets so the rest of this bootstrap + # and the charm itself can actually succeed. This call does nothing + # unless the operator has created and populated $JUJU_CHARM_DIR/exec.d. + execd_preinstall() + # ensure that $JUJU_CHARM_DIR/bin is on the path, for helper scripts + charm_dir = os.environ['JUJU_CHARM_DIR'] + os.environ['PATH'] += ':%s' % os.path.join(charm_dir, 'bin') + venv = os.path.abspath('../.venv') + vbin = os.path.join(venv, 'bin') + vpip = os.path.join(vbin, 'pip') + vpy = os.path.join(vbin, 'python') + hook_name = os.path.basename(sys.argv[0]) + is_bootstrapped = os.path.exists('wheelhouse/.bootstrapped') + is_charm_upgrade = hook_name == 'upgrade-charm' + is_series_upgrade = hook_name == 'post-series-upgrade' + post_upgrade = os.path.exists('wheelhouse/.upgrade') + is_upgrade = not post_upgrade and (is_charm_upgrade or is_series_upgrade) + if is_bootstrapped and not is_upgrade: + # older subordinates might have downgraded charm-env, so we should + # restore it if necessary + install_or_update_charm_env() + activate_venv() + # the .upgrade file prevents us from getting stuck in a loop + # when re-execing to activate the venv; at this point, we've + # activated the venv, so it's safe to clear it + if post_upgrade: + os.unlink('wheelhouse/.upgrade') + return + if is_series_upgrade and os.path.exists(venv): + # series upgrade should do a full clear of the venv, rather than just + # updating it, to bring in updates to Python itself + shutil.rmtree(venv) + if is_upgrade: + if os.path.exists('wheelhouse/.bootstrapped'): + os.unlink('wheelhouse/.bootstrapped') + open('wheelhouse/.upgrade', 'w').close() + # bootstrap wheelhouse + if os.path.exists('wheelhouse'): + with open('/root/.pydistutils.cfg', 'w') as fp: + # make sure that easy_install also only uses the wheelhouse + # (see https://github.com/pypa/pip/issues/410) + fp.writelines([ + "[easy_install]\n", + "allow_hosts = ''\n", + "find_links = file://{}/wheelhouse/\n".format(charm_dir), + ]) + apt_install([ + 'python3-pip', + 'python3-setuptools', + 'python3-yaml', + 'python3-dev', + 'python3-wheel', + 'build-essential', + ]) + from charms.layer import options + cfg = options.get('basic') + # include packages defined in layer.yaml + apt_install(cfg.get('packages', [])) + # if we're using a venv, set it up + if cfg.get('use_venv'): + if not os.path.exists(venv): + series = lsb_release()['DISTRIB_CODENAME'] + if series in ('precise', 'trusty'): + apt_install(['python-virtualenv']) + else: + apt_install(['virtualenv']) + cmd = ['virtualenv', '-ppython3', '--never-download', venv] + if cfg.get('include_system_packages'): + cmd.append('--system-site-packages') + check_call(cmd) + os.environ['PATH'] = ':'.join([vbin, os.environ['PATH']]) + pip = vpip + else: + pip = 'pip3' + # save a copy of system pip to prevent `pip3 install -U pip` + # from changing it + if os.path.exists('/usr/bin/pip'): + shutil.copy2('/usr/bin/pip', '/usr/bin/pip.save') + # need newer pip, to fix spurious Double Requirement error: + # https://github.com/pypa/pip/issues/56 + check_call([pip, 'install', '-U', '--no-index', '-f', 'wheelhouse', + 'pip']) + # per https://github.com/juju-solutions/layer-basic/issues/110 + # this replaces the setuptools that was copied over from the system on + # venv create with latest setuptools and adds setuptools_scm + check_call([pip, 'install', '-U', '--no-index', '-f', 'wheelhouse', + 'setuptools', 'setuptools-scm']) + # install the rest of the wheelhouse deps + check_call([pip, 'install', '-U', '--ignore-installed', '--no-index', + '-f', 'wheelhouse'] + glob('wheelhouse/*')) + # re-enable installation from pypi + os.remove('/root/.pydistutils.cfg') + # install python packages from layer options + if cfg.get('python_packages'): + check_call([pip, 'install', '-U'] + cfg.get('python_packages')) + if not cfg.get('use_venv'): + # restore system pip to prevent `pip3 install -U pip` + # from changing it + if os.path.exists('/usr/bin/pip.save'): + shutil.copy2('/usr/bin/pip.save', '/usr/bin/pip') + os.remove('/usr/bin/pip.save') + # setup wrappers to ensure envs are used for scripts + install_or_update_charm_env() + for wrapper in ('charms.reactive', 'charms.reactive.sh', + 'chlp', 'layer_option'): + src = os.path.join('/usr/local/sbin', 'charm-env') + dst = os.path.join('/usr/local/sbin', wrapper) + if not os.path.exists(dst): + os.symlink(src, dst) + if cfg.get('use_venv'): + shutil.copy2('bin/layer_option', vbin) + else: + shutil.copy2('bin/layer_option', '/usr/local/bin/') + # re-link the charm copy to the wrapper in case charms + # call bin/layer_option directly (as was the old pattern) + os.remove('bin/layer_option') + os.symlink('/usr/local/sbin/layer_option', 'bin/layer_option') + # flag us as having already bootstrapped so we don't do it again + open('wheelhouse/.bootstrapped', 'w').close() + # Ensure that the newly bootstrapped libs are available. + # Note: this only seems to be an issue with namespace packages. + # Non-namespace-package libs (e.g., charmhelpers) are available + # without having to reload the interpreter. :/ + reload_interpreter(vpy if cfg.get('use_venv') else sys.argv[0]) + + +def install_or_update_charm_env(): + # On Trusty python3-pkg-resources is not installed + try: + from pkg_resources import parse_version + except ImportError: + apt_install(['python3-pkg-resources']) + from pkg_resources import parse_version + + try: + installed_version = parse_version( + check_output(['/usr/local/sbin/charm-env', + '--version']).decode('utf8')) + except (CalledProcessError, FileNotFoundError): + installed_version = parse_version('0.0.0') + try: + bundled_version = parse_version( + check_output(['bin/charm-env', + '--version']).decode('utf8')) + except (CalledProcessError, FileNotFoundError): + bundled_version = parse_version('0.0.0') + if installed_version < bundled_version: + shutil.copy2('bin/charm-env', '/usr/local/sbin/') + + +def activate_venv(): + """ + Activate the venv if enabled in ``layer.yaml``. + + This is handled automatically for normal hooks, but actions might + need to invoke this manually, using something like: + + # Load modules from $JUJU_CHARM_DIR/lib + import sys + sys.path.append('lib') + + from charms.layer.basic import activate_venv + activate_venv() + + This will ensure that modules installed in the charm's + virtual environment are available to the action. + """ + from charms.layer import options + venv = os.path.abspath('../.venv') + vbin = os.path.join(venv, 'bin') + vpy = os.path.join(vbin, 'python') + use_venv = options.get('basic', 'use_venv') + if use_venv and '.venv' not in sys.executable: + # activate the venv + os.environ['PATH'] = ':'.join([vbin, os.environ['PATH']]) + reload_interpreter(vpy) + layer.patch_options_interface() + layer.import_layer_libs() + + +def reload_interpreter(python): + """ + Reload the python interpreter to ensure that all deps are available. + + Newly installed modules in namespace packages sometimes seemt to + not be picked up by Python 3. + """ + os.execve(python, [python] + list(sys.argv), os.environ) + + +def apt_install(packages): + """ + Install apt packages. + + This ensures a consistent set of options that are often missed but + should really be set. + """ + if isinstance(packages, (str, bytes)): + packages = [packages] + + env = os.environ.copy() + + if 'DEBIAN_FRONTEND' not in env: + env['DEBIAN_FRONTEND'] = 'noninteractive' + + cmd = ['apt-get', + '--option=Dpkg::Options::=--force-confold', + '--assume-yes', + 'install'] + for attempt in range(3): + try: + check_call(cmd + packages, env=env) + except CalledProcessError: + if attempt == 2: # third attempt + raise + try: + # sometimes apt-get update needs to be run + check_call(['apt-get', 'update']) + except CalledProcessError: + # sometimes it's a dpkg lock issue + pass + sleep(5) + else: + break + + +def init_config_states(): + import yaml + from charmhelpers.core import hookenv + from charms.reactive import set_state + from charms.reactive import toggle_state + config = hookenv.config() + config_defaults = {} + config_defs = {} + config_yaml = os.path.join(hookenv.charm_dir(), 'config.yaml') + if os.path.exists(config_yaml): + with open(config_yaml) as fp: + config_defs = yaml.safe_load(fp).get('options', {}) + config_defaults = {key: value.get('default') + for key, value in config_defs.items()} + for opt in config_defs.keys(): + if config.changed(opt): + set_state('config.changed') + set_state('config.changed.{}'.format(opt)) + toggle_state('config.set.{}'.format(opt), config.get(opt)) + toggle_state('config.default.{}'.format(opt), + config.get(opt) == config_defaults[opt]) + + +def clear_config_states(): + from charmhelpers.core import hookenv, unitdata + from charms.reactive import remove_state + config = hookenv.config() + remove_state('config.changed') + for opt in config.keys(): + remove_state('config.changed.{}'.format(opt)) + remove_state('config.set.{}'.format(opt)) + remove_state('config.default.{}'.format(opt)) + unitdata.kv().flush() diff --git a/nscharm_ns/charms/ns/lib/charms/layer/execd.py b/nscharm_ns/charms/ns/lib/charms/layer/execd.py new file mode 100644 index 0000000000000000000000000000000000000000..438d9a1bc90042fd8b20517d5ecf358dfbf08afc --- /dev/null +++ b/nscharm_ns/charms/ns/lib/charms/layer/execd.py @@ -0,0 +1,114 @@ +# Copyright 2014-2016 Canonical Limited. +# +# This file is part of layer-basic, the reactive base layer for Juju. +# +# charm-helpers is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 as +# published by the Free Software Foundation. +# +# charm-helpers is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with charm-helpers. If not, see . + +# This module may only import from the Python standard library. +import os +import sys +import subprocess +import time + +''' +execd/preinstall + +Read the layer-basic docs for more info on how to use this feature. +https://charmsreactive.readthedocs.io/en/latest/layer-basic.html#exec-d-support +''' + + +def default_execd_dir(): + return os.path.join(os.environ['JUJU_CHARM_DIR'], 'exec.d') + + +def execd_module_paths(execd_dir=None): + """Generate a list of full paths to modules within execd_dir.""" + if not execd_dir: + execd_dir = default_execd_dir() + + if not os.path.exists(execd_dir): + return + + for subpath in os.listdir(execd_dir): + module = os.path.join(execd_dir, subpath) + if os.path.isdir(module): + yield module + + +def execd_submodule_paths(command, execd_dir=None): + """Generate a list of full paths to the specified command within exec_dir. + """ + for module_path in execd_module_paths(execd_dir): + path = os.path.join(module_path, command) + if os.access(path, os.X_OK) and os.path.isfile(path): + yield path + + +def execd_sentinel_path(submodule_path): + module_path = os.path.dirname(submodule_path) + execd_path = os.path.dirname(module_path) + module_name = os.path.basename(module_path) + submodule_name = os.path.basename(submodule_path) + return os.path.join(execd_path, + '.{}_{}.done'.format(module_name, submodule_name)) + + +def execd_run(command, execd_dir=None, stop_on_error=True, stderr=None): + """Run command for each module within execd_dir which defines it.""" + if stderr is None: + stderr = sys.stdout + for submodule_path in execd_submodule_paths(command, execd_dir): + # Only run each execd once. We cannot simply run them in the + # install hook, as potentially storage hooks are run before that. + # We cannot rely on them being idempotent. + sentinel = execd_sentinel_path(submodule_path) + if os.path.exists(sentinel): + continue + + try: + subprocess.check_call([submodule_path], stderr=stderr, + universal_newlines=True) + with open(sentinel, 'w') as f: + f.write('{} ran successfully {}\n'.format(submodule_path, + time.ctime())) + f.write('Removing this file will cause it to be run again\n') + except subprocess.CalledProcessError as e: + # Logs get the details. We can't use juju-log, as the + # output may be substantial and exceed command line + # length limits. + print("ERROR ({}) running {}".format(e.returncode, e.cmd), + file=stderr) + print("STDOUT<" +"maintainer": "Adam Israel " +"description": | + +"tags": + # Replace "misc" with one or more whitelisted tags from this list: + # https://jujucharms.com/docs/stable/authors-charm-metadata +- "misc" +"series": +- "xenial" +- "bionic" +"subordinate": !!bool "false" diff --git a/nscharm_ns/charms/ns/reactive/__init__.py b/nscharm_ns/charms/ns/reactive/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/nscharm_ns/charms/ns/reactive/ns.py b/nscharm_ns/charms/ns/reactive/ns.py new file mode 100644 index 0000000000000000000000000000000000000000..4f13011a97c51724a09576f6995e534f3ce3b9bc --- /dev/null +++ b/nscharm_ns/charms/ns/reactive/ns.py @@ -0,0 +1,132 @@ +from charmhelpers.core.hookenv import ( + action_get, + action_fail, + action_set, + status_set, + log, + config, +) + +from charms.reactive import ( + clear_flag, + set_flag, + when, + when_not, +) +import charms.osm.ns +import json +import traceback + +@when_not('ns.installed') +def install_ns(): + set_flag('ns.installed') + status_set('active', 'Ready!') + + +@when('actions.add-user') +def action_add_user(): + """Add a user to the database.""" + err = '' + output = '' + try: + username = action_get('username') + bw = action_get('bw') + qos = action_get('qos') + tariff = action_get('tariff') + + # Get the configuration, which should contain the juju username and + # password. The endpoint and model will be discovered automatically + cfg = config() + + client = charms.osm.ns.NetworkService( + user=cfg['juju-username'], + secret=cfg['juju-password'], + ) + + user_id = add_user(client, username, tariff) + if user_id > 0: + success = set_policy(client, user_id, bw, qos) + else: + log("user_id is 0; add_user failed.") + + log("Output from charm: {}".format(output)) + + except Exception as err: + log(str(err)) + log(str(traceback.format_exc())) + action_fail(str(err)) + else: + action_set({ + 'user-id': user_id, + 'policy-set': success, + }) + finally: + clear_flag('actions.add-user') + + +def add_user(client, username, tariff): + """Add a user to the database and return the id.""" + + cfg = config() + + ns_config_info = json.loads(cfg['ns_config_info']) + log(ns_config_info) + application = ns_config_info['osm-config-mapping']['{}.{}.{}'.format( + '1', # member-vnf-index + 'userVM', # vdu_id + '0', # vdu_count_index + )] + + output = client.ExecutePrimitiveGetOutput( + # The name of the application for adding a user + application, + + # The name of the action to call + "add-user", + + # The parameter(s) required by the above charm and action + params={ + 'username': username, + 'tariff': tariff, + }, + # How long to wait (in seconds) for the action to finish + timeout=500 + ) + + # Get the output from the `add-user` function + user_id = int(output['user-id']) + return user_id + + +def set_policy(client, user_id, bw, qos): + """Set the policy for a user.""" + success = False + + cfg = config() + ns_config_info = json.loads(cfg['ns_config_info']) + application = ns_config_info['osm-config-mapping']['{}.{}.{}'.format( + '2', # member-vnf-index + 'policyVM', # vdu_id + '0', # vdu_count_index + )] + + success = client.ExecutePrimitiveGetOutput( + # The name of the application for policy management + application, + + # The name of the action to call + "set-policy", + + # The parameter(s) required by the above charm and action + params={ + 'user_id': user_id, + 'bw': bw, + 'qos': qos, + }, + + # How long to wait (in seconds) for the action to finish + timeout=500 + ) + + # Get the output from the `add-user` function + return success diff --git a/nscharm_ns/charms/ns/reactive/osm_ns.py b/nscharm_ns/charms/ns/reactive/osm_ns.py new file mode 100644 index 0000000000000000000000000000000000000000..aacdeb5366fdf950f09236196f3ecfa34caf91cf --- /dev/null +++ b/nscharm_ns/charms/ns/reactive/osm_ns.py @@ -0,0 +1,7 @@ +from charms.reactive import when, when_not, set_flag + + +@when_not('osm-stack.installed') +def install_osm_stack(): + + set_flag('osm-stack.installed') diff --git a/nscharm_ns/charms/ns/requirements.txt b/nscharm_ns/charms/ns/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..28ecacab6029381bd43d65f8dcfc0cc704870f71 --- /dev/null +++ b/nscharm_ns/charms/ns/requirements.txt @@ -0,0 +1,2 @@ +flake8 +pytest diff --git a/nscharm_ns/charms/ns/tests/00-setup b/nscharm_ns/charms/ns/tests/00-setup new file mode 100755 index 0000000000000000000000000000000000000000..f0616a560a811bae78d7872dd433d612909c73cd --- /dev/null +++ b/nscharm_ns/charms/ns/tests/00-setup @@ -0,0 +1,5 @@ +#!/bin/bash + +sudo add-apt-repository ppa:juju/stable -y +sudo apt-get update +sudo apt-get install amulet python-requests -y diff --git a/nscharm_ns/charms/ns/tests/10-deploy b/nscharm_ns/charms/ns/tests/10-deploy new file mode 100755 index 0000000000000000000000000000000000000000..52efc97fd622de5c6b6bf857f53145fbd87fcabf --- /dev/null +++ b/nscharm_ns/charms/ns/tests/10-deploy @@ -0,0 +1,35 @@ +#!/usr/bin/python3 + +import amulet +import requests +import unittest + + +class TestCharm(unittest.TestCase): + def setUp(self): + self.d = amulet.Deployment() + + self.d.add('ns') + self.d.expose('ns') + + self.d.setup(timeout=900) + self.d.sentry.wait() + + self.unit = self.d.sentry['ns'][0] + + def test_service(self): + # test we can access over http + page = requests.get('http://{}'.format(self.unit.info['public-address'])) + self.assertEqual(page.status_code, 200) + # Now you can use self.d.sentry[SERVICE][UNIT] to address each of the units and perform + # more in-depth steps. Each self.d.sentry[SERVICE][UNIT] has the following methods: + # - .info - An array of the information of that unit from Juju + # - .file(PATH) - Get the details of a file on that unit + # - .file_contents(PATH) - Get plain text output of PATH file from that unit + # - .directory(PATH) - Get details of directory + # - .directory_contents(PATH) - List files and folders in PATH on that unit + # - .relation(relation, service:rel) - Get relation data from return service + + +if __name__ == '__main__': + unittest.main() diff --git a/nscharm_ns/charms/ns/version b/nscharm_ns/charms/ns/version new file mode 100644 index 0000000000000000000000000000000000000000..1bfc8f6a5d12fa42cfd265b9525ce3c30b16c92b --- /dev/null +++ b/nscharm_ns/charms/ns/version @@ -0,0 +1 @@ +aa34ff8-dirty \ No newline at end of file diff --git a/nscharm_ns/charms/ns/wheelhouse/setuptools-41.6.0.zip b/nscharm_ns/charms/ns/wheelhouse/setuptools-41.6.0.zip new file mode 100644 index 0000000000000000000000000000000000000000..3345759c1ca861c181742e84fc5285e3284a2886 Binary files /dev/null and b/nscharm_ns/charms/ns/wheelhouse/setuptools-41.6.0.zip differ diff --git a/nscharm_ns/nscharm_nsd.yaml b/nscharm_ns/nscharm_nsd.yaml new file mode 100644 index 0000000000000000000000000000000000000000..deade8e3798289ee3ff7e153fb193f29c7a54da0 --- /dev/null +++ b/nscharm_ns/nscharm_nsd.yaml @@ -0,0 +1,50 @@ +nsd:nsd-catalog: + nsd: + - id: nscharm-ns + name: nscharm-ns + short-name: nscharm-ns + description: NS with 2 VNFs + version: '1.0' + logo: osm.png + constituent-vnfd: + - vnfd-id-ref: nscharm-user-vnf + member-vnf-index: '1' + - vnfd-id-ref: nscharm-policy-vnf + member-vnf-index: '2' + vld: + - id: mgmtnet + name: mgmtnet + short-name: mgmtnet + type: ELAN + mgmt-network: 'true' + vim-network-name: mgmt + vnfd-connection-point-ref: + - vnfd-id-ref: nscharm-user-vnf + member-vnf-index-ref: '1' + vnfd-connection-point-ref: vnf-mgmt + - vnfd-id-ref: nscharm-policy-vnf + member-vnf-index-ref: '2' + vnfd-connection-point-ref: vnf-mgmt + + ns-configuration: + juju: + charm: ns + initial-config-primitive: + - seq: '1' + name: config + parameter: + # Configure Juju credentials + - name: juju-username + value: 'admin' + - name: juju-password + value: 'd55ce8ab4efa59e7f1b865bce53f55ed' + - seq: '2' + name: add-user + parameter: + - name: username + value: root + config-primitive: + - name: add-user + parameter: + - name: username + data-type: STRING diff --git a/nscharm_policy_vnf/charms/vnf-policy/.build.manifest b/nscharm_policy_vnf/charms/vnf-policy/.build.manifest new file mode 100644 index 0000000000000000000000000000000000000000..56e04635a503dde5f10463c5702dda8e3d85709a --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/.build.manifest @@ -0,0 +1,351 @@ +{ + "layers": [ + { + "rev": "fcdcea4e5de3e1556c24e6704607862d0ba00a56", + "url": "layer:options" + }, + { + "rev": "1d2489bff56daf2f8d1c06ee9248bb0094e4ad49", + "url": "layer:basic" + }, + { + "rev": "29d9dd2e642048f84e1ac388e03756c6cdec3551", + "url": "layer:sshproxy" + }, + { + "rev": "e6a495fab012f06484ed419fd5c2453e6a92eef1", + "url": "layer:vnfproxy" + }, + { + "rev": "83c13bd97764feab1b162d0ac2303f874ea2856f", + "url": "vnf-policy" + } + ], + "signatures": { + ".build.manifest": [ + "build", + "dynamic", + "unchecked" + ], + ".gitignore": [ + "layer:sshproxy", + "static", + "17526a7f7312e7eefb932d1c514b7bc8425fab5bd1ade149e106ecf8bff67358" + ], + "LICENSE": [ + "layer:basic", + "static", + "cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30" + ], + "Makefile": [ + "layer:basic", + "static", + "b7ab3a34e5faf79b96a8632039a0ad0aa87f2a9b5f0ba604e007cafb22190301" + ], + "README.md": [ + "vnf-policy", + "static", + "6380429bf8915e9bfbd26c06d330c083ed2ad65d52a465e1214428e43beabb88" + ], + "actions.yaml": [ + "vnf-policy", + "dynamic", + "e1ace1900b6520fa5b4f0bc99b3a6cfca4072e72dd49be5d423499ec0515450a" + ], + "actions/generate-ssh-key": [ + "layer:sshproxy", + "static", + "46bf4be02078a0f5ac60ab81609372a8f9943885b7a4999ce5332ea2f4857036" + ], + "actions/get-ssh-public-key": [ + "layer:sshproxy", + "static", + "46bf4be02078a0f5ac60ab81609372a8f9943885b7a4999ce5332ea2f4857036" + ], + "actions/reboot": [ + "layer:vnfproxy", + "static", + "afc0df4409fb6eb7b7e6595490913c1efe329b393d4860ae226d8aaccc01fcf2" + ], + "actions/restart": [ + "layer:vnfproxy", + "static", + "afc0df4409fb6eb7b7e6595490913c1efe329b393d4860ae226d8aaccc01fcf2" + ], + "actions/run": [ + "layer:sshproxy", + "static", + "46bf4be02078a0f5ac60ab81609372a8f9943885b7a4999ce5332ea2f4857036" + ], + "actions/set-policy": [ + "vnf-policy", + "static", + "46bf4be02078a0f5ac60ab81609372a8f9943885b7a4999ce5332ea2f4857036" + ], + "actions/start": [ + "layer:vnfproxy", + "static", + "afc0df4409fb6eb7b7e6595490913c1efe329b393d4860ae226d8aaccc01fcf2" + ], + "actions/stop": [ + "layer:vnfproxy", + "static", + "afc0df4409fb6eb7b7e6595490913c1efe329b393d4860ae226d8aaccc01fcf2" + ], + "actions/upgrade": [ + "layer:vnfproxy", + "static", + "afc0df4409fb6eb7b7e6595490913c1efe329b393d4860ae226d8aaccc01fcf2" + ], + "actions/verify-ssh-credentials": [ + "layer:sshproxy", + "static", + "46bf4be02078a0f5ac60ab81609372a8f9943885b7a4999ce5332ea2f4857036" + ], + "bin/charm-env": [ + "layer:basic", + "static", + "458c53532c19ee357cbf4209ccc7d811810718ba0ea3b0588b3d3ef040a44b8e" + ], + "bin/layer_option": [ + "layer:options", + "static", + "e959bf29da4c5edff28b2602c24113c4df9e25cdc9f2aa3b5d46c8577b2a40cc" + ], + "config.yaml": [ + "layer:vnfproxy", + "dynamic", + "c5bd79d6699ffdf49cc71ab18a6e8f0e7b0ca71fae2f1416a2138154fe6c1a34" + ], + "copyright": [ + "layer:basic", + "static", + "f6740d66fd60b60f2533d9fcb53907078d1e20920a0219afce7182e2a1c97629" + ], + "copyright.layer-options": [ + "layer:options", + "static", + "f6740d66fd60b60f2533d9fcb53907078d1e20920a0219afce7182e2a1c97629" + ], + "hooks/collect-metrics": [ + "layer:vnfproxy", + "static", + "f2a6360cf21993f7dc07785ad702f141679a5a9b03c3db5d50dd06ffbe26bbb2" + ], + "hooks/config-changed": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/hook.template": [ + "layer:basic", + "static", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/install": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/leader-elected": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/leader-settings-changed": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/post-series-upgrade": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/pre-series-upgrade": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/start": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/stop": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/update-status": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/upgrade-charm": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "icon.svg": [ + "vnf-policy", + "static", + "d20624e9389af6506a8d8a69ac9bba4d41709601b624c0875fd7d6717b395088" + ], + "layer.yaml": [ + "vnf-policy", + "dynamic", + "44d108f7fb1b6469fd2629c27c551f5d961e94e8285f785324b96b9c36c372d9" + ], + "lib/charms/layer/__init__.py": [ + "layer:basic", + "static", + "dfe0d26c6bf409767de6e2546bc648f150e1b396243619bad3aa0553ab7e0e6f" + ], + "lib/charms/layer/basic.py": [ + "layer:basic", + "static", + "445652dbaa1f0b84a7215da185bcbdff097bb9bbbce11b4c2dbbff90f77719a9" + ], + "lib/charms/layer/execd.py": [ + "layer:basic", + "static", + "fda8bd491032db1db8ddaf4e99e7cc878c6fb5432efe1f91cadb5b34765d076d" + ], + "lib/charms/layer/options.py": [ + "layer:options", + "static", + "8ae7a07d22542fc964f2d2bee8219d1c78a68dace70a1b38d36d4aea47b1c3b2" + ], + "lib/charms/sshproxy.py": [ + "layer:sshproxy", + "static", + "fba0834f98f570b0b3d6b3e8a7f17b1d8909f50da9410ccda89b586f914b7ffd" + ], + "metadata.yaml": [ + "vnf-policy", + "dynamic", + "ea261c975b1ab509eda9cd654c0f5a1219664c5ff3359e9df7209711ad8cc4bb" + ], + "reactive/__init__.py": [ + "layer:basic", + "static", + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + ], + "reactive/sshproxy.py": [ + "layer:sshproxy", + "static", + "acfaedf414946f8d218e5ebd04177f87258286915ba83b9c42ff7385975da6ab" + ], + "reactive/vnf_policy.py": [ + "vnf-policy", + "static", + "95e056af5cf27cd850494d69bdc09ea707e70d6cb79fbd807466acd76ca270ab" + ], + "reactive/vnfproxy.py": [ + "layer:vnfproxy", + "static", + "8e4101a72f02832e5c233b731981ff483675be608321b8eb9743d605b3f7d77a" + ], + "requirements.txt": [ + "layer:basic", + "static", + "0f1c70d27e26005a96d66ad54482877ae20f7737693c833e29dd72bd6ac24892" + ], + "tests/00-setup": [ + "vnf-policy", + "static", + "111c079b81d260bbcd716dcf41672372a4cf4aaa14154b6c3055deeedae37a06" + ], + "tests/10-deploy": [ + "vnf-policy", + "static", + "0d0154aad1f9694cddc25e318160dedfaa3c6c6471e732f5dcaa60f1df71ce75" + ], + "version": [ + "vnf-policy", + "dynamic", + "ddf4577d49e7c71e11e6dd4f85ad6e74551d5ba89f52b6370de9c141680f9c7f" + ], + "wheelhouse/Jinja2-2.10.3.tar.gz": [ + "layer:basic", + "dynamic", + "9fe95f19286cfefaa917656583d020be14e7859c6b0252588391e47db34527de" + ], + "wheelhouse/MarkupSafe-1.1.1.tar.gz": [ + "layer:basic", + "dynamic", + "29872e92839765e546828bb7754a68c418d927cd064fd4708fab9fe9c8bb116b" + ], + "wheelhouse/PyYAML-5.2.tar.gz": [ + "layer:basic", + "dynamic", + "c0ee8eca2c582d29c3c2ec6e2c4f703d1b7f1fb10bc72317355a746057e7346c" + ], + "wheelhouse/Tempita-0.5.2.tar.gz": [ + "layer:basic", + "dynamic", + "cacecf0baa674d356641f1d406b8bff1d756d739c46b869a54de515d08e6fc9c" + ], + "wheelhouse/charmhelpers-0.20.7.tar.gz": [ + "layer:basic", + "dynamic", + "e0f8d005d39cded1b0c5997d8ef1d90832341c67ebeb4a334ad1eb348fbd803a" + ], + "wheelhouse/charms.reactive-1.3.0.tar.gz": [ + "layer:basic", + "dynamic", + "82d2c614c82d64bf56e913990f22663e5de64b99db15838abfd9a064f1cc2f51" + ], + "wheelhouse/ecdsa-0.14.1.tar.gz": [ + "layer:sshproxy", + "dynamic", + "64c613005f13efec6541bb0a33290d0d03c27abab5f15fbab20fb0ee162bdd8e" + ], + "wheelhouse/netaddr-0.7.19.tar.gz": [ + "layer:basic", + "dynamic", + "38aeec7cdd035081d3a4c306394b19d677623bf76fa0913f6695127c7753aefd" + ], + "wheelhouse/paramiko-1.16.3.tar.gz": [ + "layer:sshproxy", + "dynamic", + "97d932fdb4fec9aadf6bea368123f3ee15b92199f92eb62666370c7fed62d072" + ], + "wheelhouse/pip-18.1.tar.gz": [ + "layer:basic", + "dynamic", + "c0a292bd977ef590379a3f05d7b7f65135487b67470f6281289a94e015650ea1" + ], + "wheelhouse/pyaml-19.4.1.tar.gz": [ + "layer:basic", + "dynamic", + "c79ae98ececda136a034115ca178ee8bf3aa7df236c488c2f55d12f177b88f1e" + ], + "wheelhouse/pycrypto-2.6.1.tar.gz": [ + "layer:sshproxy", + "dynamic", + "f2ce1e989b272cfcb677616763e0a2e7ec659effa67a88aa92b3a65528f60a3c" + ], + "wheelhouse/setuptools-41.6.0.zip": [ + "layer:basic", + "dynamic", + "6afa61b391dcd16cb8890ec9f66cc4015a8a31a6e1c2b4e0c464514be1a3d722" + ], + "wheelhouse/setuptools_scm-1.17.0.tar.gz": [ + "layer:basic", + "dynamic", + "70a4cf5584e966ae92f54a764e6437af992ba42ac4bca7eb37cc5d02b98ec40a" + ], + "wheelhouse/six-1.13.0.tar.gz": [ + "layer:sshproxy", + "dynamic", + "30f610279e8b2578cab6db20741130331735c781b56053c59c4076da27f06b66" + ], + "wheelhouse/wheel-0.33.6.tar.gz": [ + "layer:basic", + "dynamic", + "10c9da68765315ed98850f8e048347c3eb06dd81822dc2ab1d4fde9dc9702646" + ] + } +} \ No newline at end of file diff --git a/nscharm_policy_vnf/charms/vnf-policy/.gitignore b/nscharm_policy_vnf/charms/vnf-policy/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..b8e7ba3a229bda75224369127d7454019fea641f --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/.gitignore @@ -0,0 +1 @@ +trusty/ diff --git a/nscharm_policy_vnf/charms/vnf-policy/LICENSE b/nscharm_policy_vnf/charms/vnf-policy/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..d645695673349e3947e8e5ae42332d0ac3164cd7 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/nscharm_policy_vnf/charms/vnf-policy/Makefile b/nscharm_policy_vnf/charms/vnf-policy/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..a1ad3a5cd27751144f6bd1a0a7db50f1e11b18eb --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/Makefile @@ -0,0 +1,24 @@ +#!/usr/bin/make + +all: lint unit_test + + +.PHONY: clean +clean: + @rm -rf .tox + +.PHONY: apt_prereqs +apt_prereqs: + @# Need tox, but don't install the apt version unless we have to (don't want to conflict with pip) + @which tox >/dev/null || (sudo apt-get install -y python-pip && sudo pip install tox) + +.PHONY: lint +lint: apt_prereqs + @tox --notest + @PATH=.tox/py34/bin:.tox/py35/bin flake8 $(wildcard hooks reactive lib unit_tests tests) + @charm proof + +.PHONY: unit_test +unit_test: apt_prereqs + @echo Starting tests... + tox diff --git a/nscharm_policy_vnf/charms/vnf-policy/README.md b/nscharm_policy_vnf/charms/vnf-policy/README.md new file mode 100644 index 0000000000000000000000000000000000000000..bf5324ebf87ca73451897edc752449d8a3263a6f --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/README.md @@ -0,0 +1,24 @@ +# Overview + +This is an example of an Open Source Mano (OSM) charm used with the Network Service (ns) example. + +# Usage + + +## Known Limitations and Issues + +Network Service charms are an **experimental** feature, expected to be released with OSM R6 in the spring of 2019. + + +# Configuration + +None + +# Contact Information +## Open Source Mano (OSM) + + - [OSM website](https://osm.etsi.org/) + - [OSM bug tracker](https://osm.etsi.org/bugzilla/) + - [OSM_TECH](mailto:OSM_TECH@list.etsi.org) mailing list + - [Slack](https://join.slack.com/t/opensourcemano/shared_invite/enQtMzQ3MzYzNTQ0NDIyLWJkMzRjNDM0MjFjODYzMGQ3ODIzMzJlNTg2ZGI5OTdiZjFiNDMyMzYxMjRjNDU4N2FmNjRjNzY5NTE1MjgzOTQ) + diff --git a/nscharm_policy_vnf/charms/vnf-policy/actions.yaml b/nscharm_policy_vnf/charms/vnf-policy/actions.yaml new file mode 100644 index 0000000000000000000000000000000000000000..4e411b0143183aa6e9304bc90afc4b4873d53c5a --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/actions.yaml @@ -0,0 +1,63 @@ +## +# Copyright 2016 Canonical Ltd. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +## + +"run": + "description": "Run an arbitrary command" + "params": + "command": + "description": "The command to execute." + "type": "string" + "default": "" + "required": + - "command" +"generate-ssh-key": + "description": "Generate a new SSH keypair for this unit. This will replace any\ + \ existing previously generated keypair." +"verify-ssh-credentials": + "description": "Verify that this unit can authenticate with server specified by\ + \ ssh-hostname and ssh-username." +"get-ssh-public-key": + "description": "Get the public SSH key for this unit." +"start": + "description": "Stop the service on the VNF." +"stop": + "description": "Stop the service on the VNF." +"restart": + "description": "Stop the service on the VNF." +"reboot": + "description": "Reboot the VNF virtual machine." +"upgrade": + "description": "Upgrade the software on the VNF." +"set-policy": + "description": "Set a user policy" + "params": + "user_id": + "description": "The user_id to modify the policy of" + "type": "integer" + "default": !!int "0" + "bw": + "description": "" + "type": "integer" + "default": !!int "0" + "qos": + "description": "" + "type": "integer" + "default": !!int "0" + "required": + - "user_id" + - "bw" + - "qos" diff --git a/nscharm_policy_vnf/charms/vnf-policy/actions/generate-ssh-key b/nscharm_policy_vnf/charms/vnf-policy/actions/generate-ssh-key new file mode 100755 index 0000000000000000000000000000000000000000..7e30af4cfa084120a9343bf71c421b69ec3f2967 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/actions/generate-ssh-key @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +## +# Copyright 2016 Canonical Ltd. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +## +import sys +sys.path.append('lib') + +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +""" +`set_state` only works here because it's flushed to disk inside the `main()` +loop. remove_state will need to be called inside the action method. +""" +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_policy_vnf/charms/vnf-policy/actions/get-ssh-public-key b/nscharm_policy_vnf/charms/vnf-policy/actions/get-ssh-public-key new file mode 100755 index 0000000000000000000000000000000000000000..7e30af4cfa084120a9343bf71c421b69ec3f2967 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/actions/get-ssh-public-key @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +## +# Copyright 2016 Canonical Ltd. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +## +import sys +sys.path.append('lib') + +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +""" +`set_state` only works here because it's flushed to disk inside the `main()` +loop. remove_state will need to be called inside the action method. +""" +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_policy_vnf/charms/vnf-policy/actions/reboot b/nscharm_policy_vnf/charms/vnf-policy/actions/reboot new file mode 100755 index 0000000000000000000000000000000000000000..9a2ba24b9efdfa3d21457469c503c050196fb170 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/actions/reboot @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +import sys +sys.path.append('lib') +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_policy_vnf/charms/vnf-policy/actions/restart b/nscharm_policy_vnf/charms/vnf-policy/actions/restart new file mode 100755 index 0000000000000000000000000000000000000000..9a2ba24b9efdfa3d21457469c503c050196fb170 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/actions/restart @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +import sys +sys.path.append('lib') +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_policy_vnf/charms/vnf-policy/actions/run b/nscharm_policy_vnf/charms/vnf-policy/actions/run new file mode 100755 index 0000000000000000000000000000000000000000..7e30af4cfa084120a9343bf71c421b69ec3f2967 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/actions/run @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +## +# Copyright 2016 Canonical Ltd. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +## +import sys +sys.path.append('lib') + +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +""" +`set_state` only works here because it's flushed to disk inside the `main()` +loop. remove_state will need to be called inside the action method. +""" +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_policy_vnf/charms/vnf-policy/actions/set-policy b/nscharm_policy_vnf/charms/vnf-policy/actions/set-policy new file mode 100755 index 0000000000000000000000000000000000000000..7e30af4cfa084120a9343bf71c421b69ec3f2967 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/actions/set-policy @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +## +# Copyright 2016 Canonical Ltd. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +## +import sys +sys.path.append('lib') + +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +""" +`set_state` only works here because it's flushed to disk inside the `main()` +loop. remove_state will need to be called inside the action method. +""" +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_policy_vnf/charms/vnf-policy/actions/start b/nscharm_policy_vnf/charms/vnf-policy/actions/start new file mode 100755 index 0000000000000000000000000000000000000000..9a2ba24b9efdfa3d21457469c503c050196fb170 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/actions/start @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +import sys +sys.path.append('lib') +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_policy_vnf/charms/vnf-policy/actions/stop b/nscharm_policy_vnf/charms/vnf-policy/actions/stop new file mode 100755 index 0000000000000000000000000000000000000000..9a2ba24b9efdfa3d21457469c503c050196fb170 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/actions/stop @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +import sys +sys.path.append('lib') +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_policy_vnf/charms/vnf-policy/actions/upgrade b/nscharm_policy_vnf/charms/vnf-policy/actions/upgrade new file mode 100755 index 0000000000000000000000000000000000000000..9a2ba24b9efdfa3d21457469c503c050196fb170 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/actions/upgrade @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +import sys +sys.path.append('lib') +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_policy_vnf/charms/vnf-policy/actions/verify-ssh-credentials b/nscharm_policy_vnf/charms/vnf-policy/actions/verify-ssh-credentials new file mode 100755 index 0000000000000000000000000000000000000000..7e30af4cfa084120a9343bf71c421b69ec3f2967 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/actions/verify-ssh-credentials @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +## +# Copyright 2016 Canonical Ltd. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +## +import sys +sys.path.append('lib') + +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +""" +`set_state` only works here because it's flushed to disk inside the `main()` +loop. remove_state will need to be called inside the action method. +""" +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_policy_vnf/charms/vnf-policy/bin/charm-env b/nscharm_policy_vnf/charms/vnf-policy/bin/charm-env new file mode 100755 index 0000000000000000000000000000000000000000..aca7de3c1022ecad619cb82a4ac3160fb6b5677d --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/bin/charm-env @@ -0,0 +1,107 @@ +#!/bin/bash + +VERSION="1.0.0" + + +find_charm_dirs() { + # Hopefully, $JUJU_CHARM_DIR is set so which venv to use in unambiguous. + if [[ -n "$JUJU_CHARM_DIR" || -n "$CHARM_DIR" ]]; then + if [[ -z "$JUJU_CHARM_DIR" ]]; then + # accept $CHARM_DIR to be more forgiving + export JUJU_CHARM_DIR="$CHARM_DIR" + fi + if [[ -z "$CHARM_DIR" ]]; then + # set CHARM_DIR as well to help with backwards compatibility + export CHARM_DIR="$JUJU_CHARM_DIR" + fi + return + fi + # Try to guess the value for JUJU_CHARM_DIR by looking for a non-subordinate + # (because there's got to be at least one principle) charm directory; + # if there are several, pick the first by alpha order. + agents_dir="/var/lib/juju/agents" + if [[ -d "$agents_dir" ]]; then + desired_charm="$1" + found_charm_dir="" + if [[ -n "$desired_charm" ]]; then + for charm_dir in $(/bin/ls -d "$agents_dir"/unit-*/charm); do + charm_name="$(JUJU_CHARM_DIR="$charm_dir" charm-env python3 -c 'from charmhelpers.core.hookenv import charm_name; print(charm_name())')" + if [[ "$charm_name" == "$desired_charm" ]]; then + if [[ -n "$found_charm_dir" ]]; then + >&2 echo "Ambiguous possibilities for JUJU_CHARM_DIR matching '$desired_charm'; please run within a Juju hook context" + exit 1 + fi + found_charm_dir="$charm_dir" + fi + done + if [[ -z "$found_charm_dir" ]]; then + >&2 echo "Unable to determine JUJU_CHARM_DIR matching '$desired_charm'; please run within a Juju hook context" + exit 1 + fi + export JUJU_CHARM_DIR="$found_charm_dir" + export CHARM_DIR="$found_charm_dir" + return + fi + # shellcheck disable=SC2126 + non_subordinates="$(grep -L 'subordinate:.*true' "$agents_dir"/unit-*/charm/metadata.yaml | wc -l)" + if [[ "$non_subordinates" -gt 1 ]]; then + >&2 echo 'Ambiguous possibilities for JUJU_CHARM_DIR; please use --charm or run within a Juju hook context' + exit 1 + elif [[ "$non_subordinates" -eq 1 ]]; then + for charm_dir in $(/bin/ls -d "$agents_dir"/unit-*/charm); do + if grep -q 'subordinate:.*true' "$charm_dir/metadata.yaml"; then + continue + fi + export JUJU_CHARM_DIR="$charm_dir" + export CHARM_DIR="$charm_dir" + return + done + fi + fi + >&2 echo 'Unable to determine JUJU_CHARM_DIR; please run within a Juju hook context' + exit 1 +} + +try_activate_venv() { + if [[ -d "$JUJU_CHARM_DIR/../.venv" ]]; then + . "$JUJU_CHARM_DIR/../.venv/bin/activate" + fi +} + +find_wrapped() { + PATH="${PATH/\/usr\/local\/sbin:}" which "$(basename "$0")" +} + + +if [[ "$1" == "--version" || "$1" == "-v" ]]; then + echo "$VERSION" + exit 0 +fi + + +# allow --charm option to hint which JUJU_CHARM_DIR to choose when ambiguous +# NB: --charm option must come first +# NB: option must be processed outside find_charm_dirs to modify $@ +charm_name="" +if [[ "$1" == "--charm" ]]; then + charm_name="$2" + shift; shift +fi + +find_charm_dirs "$charm_name" +try_activate_venv +export PYTHONPATH="$JUJU_CHARM_DIR/lib:$PYTHONPATH" + +if [[ "$(basename "$0")" == "charm-env" ]]; then + # being used as a shebang + exec "$@" +elif [[ "$0" == "$BASH_SOURCE" ]]; then + # being invoked as a symlink wrapping something to find in the venv + exec "$(find_wrapped)" "$@" +elif [[ "$(basename "$BASH_SOURCE")" == "charm-env" ]]; then + # being sourced directly; do nothing + /bin/true +else + # being sourced for wrapped bash helpers + . "$(find_wrapped)" +fi diff --git a/nscharm_policy_vnf/charms/vnf-policy/bin/layer_option b/nscharm_policy_vnf/charms/vnf-policy/bin/layer_option new file mode 100755 index 0000000000000000000000000000000000000000..3253ef8aadb95807f26e5ebd8486adde67e5cbc2 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/bin/layer_option @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +import sys +import argparse +from charms import layer + + +parser = argparse.ArgumentParser(description='Access layer options.') +parser.add_argument('section', + help='the section, or layer, the option is from') +parser.add_argument('option', + help='the option to access') + +args = parser.parse_args() +value = layer.options.get(args.section, args.option) +if isinstance(value, bool): + sys.exit(0 if value else 1) +elif isinstance(value, list): + for val in value: + print(val) +else: + print(value) diff --git a/nscharm_policy_vnf/charms/vnf-policy/config.yaml b/nscharm_policy_vnf/charms/vnf-policy/config.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a7d9291c137d1907b7ef2ba019123aa9201ef107 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/config.yaml @@ -0,0 +1,29 @@ +"options": + "ssh-hostname": + "type": "string" + "default": "" + "description": "The hostname or IP address of the machine to" + "ssh-username": + "type": "string" + "default": "" + "description": "The username to login as." + "ssh-password": + "type": "string" + "default": "" + "description": "The password used to authenticate." + "ssh-private-key": + "type": "string" + "default": "" + "description": "DEPRECATED. The private ssh key to be used to authenticate." + "ssh-public-key": + "type": "string" + "default": "" + "description": "The public key of this unit." + "ssh-key-type": + "type": "string" + "default": "rsa" + "description": "The type of encryption to use for the SSH key." + "ssh-key-bits": + "type": "int" + "default": !!int "4096" + "description": "The number of bits to use for the SSH key." diff --git a/nscharm_policy_vnf/charms/vnf-policy/copyright b/nscharm_policy_vnf/charms/vnf-policy/copyright new file mode 100644 index 0000000000000000000000000000000000000000..d4fdd18281c632d030a301d26d45b4dabdb308ef --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/copyright @@ -0,0 +1,16 @@ +Format: http://dep.debian.net/deps/dep5/ + +Files: * +Copyright: Copyright 2015-2017, Canonical Ltd., All Rights Reserved. +License: Apache License 2.0 + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + . + http://www.apache.org/licenses/LICENSE-2.0 + . + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/nscharm_policy_vnf/charms/vnf-policy/copyright.layer-options b/nscharm_policy_vnf/charms/vnf-policy/copyright.layer-options new file mode 100644 index 0000000000000000000000000000000000000000..d4fdd18281c632d030a301d26d45b4dabdb308ef --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/copyright.layer-options @@ -0,0 +1,16 @@ +Format: http://dep.debian.net/deps/dep5/ + +Files: * +Copyright: Copyright 2015-2017, Canonical Ltd., All Rights Reserved. +License: Apache License 2.0 + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + . + http://www.apache.org/licenses/LICENSE-2.0 + . + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/nscharm_policy_vnf/charms/vnf-policy/hooks/collect-metrics b/nscharm_policy_vnf/charms/vnf-policy/hooks/collect-metrics new file mode 100755 index 0000000000000000000000000000000000000000..ecd7e62385689b00cc023b1057a0061b0f373134 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/hooks/collect-metrics @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 +"""Handle the collect-metrics hook via proxy. + +The normal metrics layer will only execute metrics collection against the local +machine. This hook implements the same approach, but runs the collection code +against the configured ssh proxy. + +Because the metrics hook runs in a restricted context, it can't be run as a +normal reactive event, nor can it access things like config. +""" + +import os +import shlex +from subprocess import check_call, CalledProcessError +import sys +import yaml + +# Load modules from $CHARM_DIR/lib +sys.path.append('lib') +import charms.sshproxy + + +def build_command(doc): + """Build the commands to report metrics. + + Build a list of `add-metric` commands to report the current metrics + back to the Juju controller. + """ + values = {} + metrics = doc.get("metrics", {}) + for metric, mdoc in metrics.items(): + cmd = mdoc.get("command") + if cmd: + try: + value, err = charms.sshproxy._run( + # The command may contain quotes that need to be preserved, + # i.e., `awk '{print $1}' /proc/uptime` + shlex.split(cmd, posix=False) + ) + except Exception as e: + # Ignore all errors + with open("metrics.log", "a") as f: + f.write("{}".format(e)) + continue + + if isinstance(value, bytes): + value = value.decode('utf-8') + + value = value.strip() + if value: + values[metric] = value + + if not values: + return None + command = ["add-metric"] + for metric, value in values.items(): + command.append("%s=%s" % (metric, value)) + return command + + +if __name__ == '__main__': + charm_dir = os.path.dirname(os.path.abspath(os.path.join(__file__, ".."))) + metrics_yaml = os.path.join(charm_dir, "metrics.yaml") + with open(metrics_yaml) as f: + doc = yaml.load(f) + command = build_command(doc) + if command: + check_call(command) diff --git a/nscharm_policy_vnf/charms/vnf-policy/hooks/config-changed b/nscharm_policy_vnf/charms/vnf-policy/hooks/config-changed new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/hooks/config-changed @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_policy_vnf/charms/vnf-policy/hooks/hook.template b/nscharm_policy_vnf/charms/vnf-policy/hooks/hook.template new file mode 100644 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/hooks/hook.template @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_policy_vnf/charms/vnf-policy/hooks/install b/nscharm_policy_vnf/charms/vnf-policy/hooks/install new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/hooks/install @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_policy_vnf/charms/vnf-policy/hooks/leader-elected b/nscharm_policy_vnf/charms/vnf-policy/hooks/leader-elected new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/hooks/leader-elected @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_policy_vnf/charms/vnf-policy/hooks/leader-settings-changed b/nscharm_policy_vnf/charms/vnf-policy/hooks/leader-settings-changed new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/hooks/leader-settings-changed @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_policy_vnf/charms/vnf-policy/hooks/post-series-upgrade b/nscharm_policy_vnf/charms/vnf-policy/hooks/post-series-upgrade new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/hooks/post-series-upgrade @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_policy_vnf/charms/vnf-policy/hooks/pre-series-upgrade b/nscharm_policy_vnf/charms/vnf-policy/hooks/pre-series-upgrade new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/hooks/pre-series-upgrade @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_policy_vnf/charms/vnf-policy/hooks/start b/nscharm_policy_vnf/charms/vnf-policy/hooks/start new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/hooks/start @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_policy_vnf/charms/vnf-policy/hooks/stop b/nscharm_policy_vnf/charms/vnf-policy/hooks/stop new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/hooks/stop @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_policy_vnf/charms/vnf-policy/hooks/update-status b/nscharm_policy_vnf/charms/vnf-policy/hooks/update-status new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/hooks/update-status @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_policy_vnf/charms/vnf-policy/hooks/upgrade-charm b/nscharm_policy_vnf/charms/vnf-policy/hooks/upgrade-charm new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/hooks/upgrade-charm @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_policy_vnf/charms/vnf-policy/icon.svg b/nscharm_policy_vnf/charms/vnf-policy/icon.svg new file mode 100644 index 0000000000000000000000000000000000000000..96a5d0c068d4b3271c7e397f76cf77fdf0651038 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/icon.svg @@ -0,0 +1,279 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + diff --git a/nscharm_policy_vnf/charms/vnf-policy/layer.yaml b/nscharm_policy_vnf/charms/vnf-policy/layer.yaml new file mode 100644 index 0000000000000000000000000000000000000000..baddbc2331e1c2e934006ea7029c8790a36d1073 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/layer.yaml @@ -0,0 +1,16 @@ +"includes": +- "layer:options" +- "layer:basic" +- "layer:sshproxy" +- "layer:vnfproxy" +"exclude": [".travis.yml", "tests", "tox.ini", "test-requirements.txt"] +"options": + "basic": + "use_venv": !!bool "false" + "packages": [] + "python_packages": [] + "include_system_packages": !!bool "false" + "sshproxy": {} + "vnfproxy": {} + "vnf-policy": {} +"is": "vnf-policy" diff --git a/nscharm_policy_vnf/charms/vnf-policy/lib/charms/layer/__init__.py b/nscharm_policy_vnf/charms/vnf-policy/lib/charms/layer/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..a8e0c640642f44cac85df60f3d9b7f73b0bb18bb --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/lib/charms/layer/__init__.py @@ -0,0 +1,60 @@ +import sys +from importlib import import_module +from pathlib import Path + + +def import_layer_libs(): + """ + Ensure that all layer libraries are imported. + + This makes it possible to do the following: + + from charms import layer + + layer.foo.do_foo_thing() + + Note: This function must be called after bootstrap. + """ + for module_file in Path('lib/charms/layer').glob('*'): + module_name = module_file.stem + if module_name in ('__init__', 'basic', 'execd') or not ( + module_file.suffix == '.py' or module_file.is_dir() + ): + continue + import_module('charms.layer.{}'.format(module_name)) + + +# Terrible hack to support the old terrible interface. +# Try to get people to call layer.options.get() instead so +# that we can remove this garbage. +# Cribbed from https://stackoverfLow.com/a/48100440/4941864 +class OptionsBackwardsCompatibilityHack(sys.modules[__name__].__class__): + def __call__(self, section=None, layer_file=None): + if layer_file is None: + return self.get(section=section) + else: + return self.get(section=section, + layer_file=Path(layer_file)) + + +def patch_options_interface(): + from charms.layer import options + if sys.version_info.minor >= 5: + options.__class__ = OptionsBackwardsCompatibilityHack + else: + # Py 3.4 doesn't support changing the __class__, so we have to do it + # another way. The last line is needed because we already have a + # reference that doesn't get updated with sys.modules. + name = options.__name__ + hack = OptionsBackwardsCompatibilityHack(name) + hack.get = options.get + sys.modules[name] = hack + sys.modules[__name__].options = hack + + +try: + patch_options_interface() +except ImportError: + # This may fail if pyyaml hasn't been installed yet. But in that + # case, the bootstrap logic will try it again once it has. + pass diff --git a/nscharm_policy_vnf/charms/vnf-policy/lib/charms/layer/basic.py b/nscharm_policy_vnf/charms/vnf-policy/lib/charms/layer/basic.py new file mode 100644 index 0000000000000000000000000000000000000000..39319591156a512b6ff46a082f51c3c8382055ec --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/lib/charms/layer/basic.py @@ -0,0 +1,286 @@ +import os +import sys +import shutil +from glob import glob +from subprocess import check_call, check_output, CalledProcessError +from time import sleep + +from charms import layer +from charms.layer.execd import execd_preinstall + + +def lsb_release(): + """Return /etc/lsb-release in a dict""" + d = {} + with open('/etc/lsb-release', 'r') as lsb: + for l in lsb: + k, v = l.split('=') + d[k.strip()] = v.strip() + return d + + +def bootstrap_charm_deps(): + """ + Set up the base charm dependencies so that the reactive system can run. + """ + # execd must happen first, before any attempt to install packages or + # access the network, because sites use this hook to do bespoke + # configuration and install secrets so the rest of this bootstrap + # and the charm itself can actually succeed. This call does nothing + # unless the operator has created and populated $JUJU_CHARM_DIR/exec.d. + execd_preinstall() + # ensure that $JUJU_CHARM_DIR/bin is on the path, for helper scripts + charm_dir = os.environ['JUJU_CHARM_DIR'] + os.environ['PATH'] += ':%s' % os.path.join(charm_dir, 'bin') + venv = os.path.abspath('../.venv') + vbin = os.path.join(venv, 'bin') + vpip = os.path.join(vbin, 'pip') + vpy = os.path.join(vbin, 'python') + hook_name = os.path.basename(sys.argv[0]) + is_bootstrapped = os.path.exists('wheelhouse/.bootstrapped') + is_charm_upgrade = hook_name == 'upgrade-charm' + is_series_upgrade = hook_name == 'post-series-upgrade' + post_upgrade = os.path.exists('wheelhouse/.upgrade') + is_upgrade = not post_upgrade and (is_charm_upgrade or is_series_upgrade) + if is_bootstrapped and not is_upgrade: + # older subordinates might have downgraded charm-env, so we should + # restore it if necessary + install_or_update_charm_env() + activate_venv() + # the .upgrade file prevents us from getting stuck in a loop + # when re-execing to activate the venv; at this point, we've + # activated the venv, so it's safe to clear it + if post_upgrade: + os.unlink('wheelhouse/.upgrade') + return + if is_series_upgrade and os.path.exists(venv): + # series upgrade should do a full clear of the venv, rather than just + # updating it, to bring in updates to Python itself + shutil.rmtree(venv) + if is_upgrade: + if os.path.exists('wheelhouse/.bootstrapped'): + os.unlink('wheelhouse/.bootstrapped') + open('wheelhouse/.upgrade', 'w').close() + # bootstrap wheelhouse + if os.path.exists('wheelhouse'): + with open('/root/.pydistutils.cfg', 'w') as fp: + # make sure that easy_install also only uses the wheelhouse + # (see https://github.com/pypa/pip/issues/410) + fp.writelines([ + "[easy_install]\n", + "allow_hosts = ''\n", + "find_links = file://{}/wheelhouse/\n".format(charm_dir), + ]) + apt_install([ + 'python3-pip', + 'python3-setuptools', + 'python3-yaml', + 'python3-dev', + 'python3-wheel', + 'build-essential', + ]) + from charms.layer import options + cfg = options.get('basic') + # include packages defined in layer.yaml + apt_install(cfg.get('packages', [])) + # if we're using a venv, set it up + if cfg.get('use_venv'): + if not os.path.exists(venv): + series = lsb_release()['DISTRIB_CODENAME'] + if series in ('precise', 'trusty'): + apt_install(['python-virtualenv']) + else: + apt_install(['virtualenv']) + cmd = ['virtualenv', '-ppython3', '--never-download', venv] + if cfg.get('include_system_packages'): + cmd.append('--system-site-packages') + check_call(cmd) + os.environ['PATH'] = ':'.join([vbin, os.environ['PATH']]) + pip = vpip + else: + pip = 'pip3' + # save a copy of system pip to prevent `pip3 install -U pip` + # from changing it + if os.path.exists('/usr/bin/pip'): + shutil.copy2('/usr/bin/pip', '/usr/bin/pip.save') + # need newer pip, to fix spurious Double Requirement error: + # https://github.com/pypa/pip/issues/56 + check_call([pip, 'install', '-U', '--no-index', '-f', 'wheelhouse', + 'pip']) + # per https://github.com/juju-solutions/layer-basic/issues/110 + # this replaces the setuptools that was copied over from the system on + # venv create with latest setuptools and adds setuptools_scm + check_call([pip, 'install', '-U', '--no-index', '-f', 'wheelhouse', + 'setuptools', 'setuptools-scm']) + # install the rest of the wheelhouse deps + check_call([pip, 'install', '-U', '--ignore-installed', '--no-index', + '-f', 'wheelhouse'] + glob('wheelhouse/*')) + # re-enable installation from pypi + os.remove('/root/.pydistutils.cfg') + # install python packages from layer options + if cfg.get('python_packages'): + check_call([pip, 'install', '-U'] + cfg.get('python_packages')) + if not cfg.get('use_venv'): + # restore system pip to prevent `pip3 install -U pip` + # from changing it + if os.path.exists('/usr/bin/pip.save'): + shutil.copy2('/usr/bin/pip.save', '/usr/bin/pip') + os.remove('/usr/bin/pip.save') + # setup wrappers to ensure envs are used for scripts + install_or_update_charm_env() + for wrapper in ('charms.reactive', 'charms.reactive.sh', + 'chlp', 'layer_option'): + src = os.path.join('/usr/local/sbin', 'charm-env') + dst = os.path.join('/usr/local/sbin', wrapper) + if not os.path.exists(dst): + os.symlink(src, dst) + if cfg.get('use_venv'): + shutil.copy2('bin/layer_option', vbin) + else: + shutil.copy2('bin/layer_option', '/usr/local/bin/') + # re-link the charm copy to the wrapper in case charms + # call bin/layer_option directly (as was the old pattern) + os.remove('bin/layer_option') + os.symlink('/usr/local/sbin/layer_option', 'bin/layer_option') + # flag us as having already bootstrapped so we don't do it again + open('wheelhouse/.bootstrapped', 'w').close() + # Ensure that the newly bootstrapped libs are available. + # Note: this only seems to be an issue with namespace packages. + # Non-namespace-package libs (e.g., charmhelpers) are available + # without having to reload the interpreter. :/ + reload_interpreter(vpy if cfg.get('use_venv') else sys.argv[0]) + + +def install_or_update_charm_env(): + # On Trusty python3-pkg-resources is not installed + try: + from pkg_resources import parse_version + except ImportError: + apt_install(['python3-pkg-resources']) + from pkg_resources import parse_version + + try: + installed_version = parse_version( + check_output(['/usr/local/sbin/charm-env', + '--version']).decode('utf8')) + except (CalledProcessError, FileNotFoundError): + installed_version = parse_version('0.0.0') + try: + bundled_version = parse_version( + check_output(['bin/charm-env', + '--version']).decode('utf8')) + except (CalledProcessError, FileNotFoundError): + bundled_version = parse_version('0.0.0') + if installed_version < bundled_version: + shutil.copy2('bin/charm-env', '/usr/local/sbin/') + + +def activate_venv(): + """ + Activate the venv if enabled in ``layer.yaml``. + + This is handled automatically for normal hooks, but actions might + need to invoke this manually, using something like: + + # Load modules from $JUJU_CHARM_DIR/lib + import sys + sys.path.append('lib') + + from charms.layer.basic import activate_venv + activate_venv() + + This will ensure that modules installed in the charm's + virtual environment are available to the action. + """ + from charms.layer import options + venv = os.path.abspath('../.venv') + vbin = os.path.join(venv, 'bin') + vpy = os.path.join(vbin, 'python') + use_venv = options.get('basic', 'use_venv') + if use_venv and '.venv' not in sys.executable: + # activate the venv + os.environ['PATH'] = ':'.join([vbin, os.environ['PATH']]) + reload_interpreter(vpy) + layer.patch_options_interface() + layer.import_layer_libs() + + +def reload_interpreter(python): + """ + Reload the python interpreter to ensure that all deps are available. + + Newly installed modules in namespace packages sometimes seemt to + not be picked up by Python 3. + """ + os.execve(python, [python] + list(sys.argv), os.environ) + + +def apt_install(packages): + """ + Install apt packages. + + This ensures a consistent set of options that are often missed but + should really be set. + """ + if isinstance(packages, (str, bytes)): + packages = [packages] + + env = os.environ.copy() + + if 'DEBIAN_FRONTEND' not in env: + env['DEBIAN_FRONTEND'] = 'noninteractive' + + cmd = ['apt-get', + '--option=Dpkg::Options::=--force-confold', + '--assume-yes', + 'install'] + for attempt in range(3): + try: + check_call(cmd + packages, env=env) + except CalledProcessError: + if attempt == 2: # third attempt + raise + try: + # sometimes apt-get update needs to be run + check_call(['apt-get', 'update']) + except CalledProcessError: + # sometimes it's a dpkg lock issue + pass + sleep(5) + else: + break + + +def init_config_states(): + import yaml + from charmhelpers.core import hookenv + from charms.reactive import set_state + from charms.reactive import toggle_state + config = hookenv.config() + config_defaults = {} + config_defs = {} + config_yaml = os.path.join(hookenv.charm_dir(), 'config.yaml') + if os.path.exists(config_yaml): + with open(config_yaml) as fp: + config_defs = yaml.safe_load(fp).get('options', {}) + config_defaults = {key: value.get('default') + for key, value in config_defs.items()} + for opt in config_defs.keys(): + if config.changed(opt): + set_state('config.changed') + set_state('config.changed.{}'.format(opt)) + toggle_state('config.set.{}'.format(opt), config.get(opt)) + toggle_state('config.default.{}'.format(opt), + config.get(opt) == config_defaults[opt]) + + +def clear_config_states(): + from charmhelpers.core import hookenv, unitdata + from charms.reactive import remove_state + config = hookenv.config() + remove_state('config.changed') + for opt in config.keys(): + remove_state('config.changed.{}'.format(opt)) + remove_state('config.set.{}'.format(opt)) + remove_state('config.default.{}'.format(opt)) + unitdata.kv().flush() diff --git a/nscharm_policy_vnf/charms/vnf-policy/lib/charms/layer/execd.py b/nscharm_policy_vnf/charms/vnf-policy/lib/charms/layer/execd.py new file mode 100644 index 0000000000000000000000000000000000000000..438d9a1bc90042fd8b20517d5ecf358dfbf08afc --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/lib/charms/layer/execd.py @@ -0,0 +1,114 @@ +# Copyright 2014-2016 Canonical Limited. +# +# This file is part of layer-basic, the reactive base layer for Juju. +# +# charm-helpers is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 as +# published by the Free Software Foundation. +# +# charm-helpers is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with charm-helpers. If not, see . + +# This module may only import from the Python standard library. +import os +import sys +import subprocess +import time + +''' +execd/preinstall + +Read the layer-basic docs for more info on how to use this feature. +https://charmsreactive.readthedocs.io/en/latest/layer-basic.html#exec-d-support +''' + + +def default_execd_dir(): + return os.path.join(os.environ['JUJU_CHARM_DIR'], 'exec.d') + + +def execd_module_paths(execd_dir=None): + """Generate a list of full paths to modules within execd_dir.""" + if not execd_dir: + execd_dir = default_execd_dir() + + if not os.path.exists(execd_dir): + return + + for subpath in os.listdir(execd_dir): + module = os.path.join(execd_dir, subpath) + if os.path.isdir(module): + yield module + + +def execd_submodule_paths(command, execd_dir=None): + """Generate a list of full paths to the specified command within exec_dir. + """ + for module_path in execd_module_paths(execd_dir): + path = os.path.join(module_path, command) + if os.access(path, os.X_OK) and os.path.isfile(path): + yield path + + +def execd_sentinel_path(submodule_path): + module_path = os.path.dirname(submodule_path) + execd_path = os.path.dirname(module_path) + module_name = os.path.basename(module_path) + submodule_name = os.path.basename(submodule_path) + return os.path.join(execd_path, + '.{}_{}.done'.format(module_name, submodule_name)) + + +def execd_run(command, execd_dir=None, stop_on_error=True, stderr=None): + """Run command for each module within execd_dir which defines it.""" + if stderr is None: + stderr = sys.stdout + for submodule_path in execd_submodule_paths(command, execd_dir): + # Only run each execd once. We cannot simply run them in the + # install hook, as potentially storage hooks are run before that. + # We cannot rely on them being idempotent. + sentinel = execd_sentinel_path(submodule_path) + if os.path.exists(sentinel): + continue + + try: + subprocess.check_call([submodule_path], stderr=stderr, + universal_newlines=True) + with open(sentinel, 'w') as f: + f.write('{} ran successfully {}\n'.format(submodule_path, + time.ctime())) + f.write('Removing this file will cause it to be run again\n') + except subprocess.CalledProcessError as e: + # Logs get the details. We can't use juju-log, as the + # output may be substantial and exceed command line + # length limits. + print("ERROR ({}) running {}".format(e.returncode, e.cmd), + file=stderr) + print("STDOUT< 0: + raise CalledProcessError(returncode=retcode, + cmd=cmd, + output=stderr.decode("utf-8").strip()) + return (stdout.decode('utf-8').strip(), stderr.decode('utf-8').strip()) + + +def _run(cmd, env=None): + """Run a command remotely via SSH. + + Note: The previous behavior was to run the command locally if SSH wasn't + configured, but that can lead to cases where execution succeeds when you'd + expect it not to. + """ + if isinstance(cmd, str): + cmd = shlex.split(cmd) + + if type(cmd) is not list: + cmd = [cmd] + + cfg = get_config() + + if cfg: + if all(k in cfg for k in ['ssh-hostname', 'ssh-username', + 'ssh-password', 'ssh-private-key']): + host = get_host_ip() + user = cfg['ssh-username'] + passwd = cfg['ssh-password'] + key = cfg['ssh-private-key'] # DEPRECATED + + if host and user: + return ssh(cmd, host, user, passwd, key) + + raise Exception("Invalid SSH credentials.") + + +def get_ssh_client(host, user, password=None, key=None): + """Return a connected Paramiko ssh object.""" + client = paramiko.SSHClient() + client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + + pkey = None + + # Check for the DEPRECATED private-key + if key: + f = io.StringIO(key) + pkey = paramiko.RSAKey.from_private_key(f) + else: + # Otherwise, check for the auto-generated private key + if os.path.exists('/root/.ssh/id_juju_sshproxy'): + with open('/root/.ssh/id_juju_sshproxy', 'r') as f: + pkey = paramiko.RSAKey.from_private_key(f) + + ########################################################################### + # There is a bug in some versions of OpenSSH 4.3 (CentOS/RHEL 5) where # + # the server may not send the SSH_MSG_USERAUTH_BANNER message except when # + # responding to an auth_none request. For example, paramiko will attempt # + # to use password authentication when a password is set, but the server # + # could deny that, instead requesting keyboard-interactive. The hack to # + # workaround this is to attempt a reconnect, which will receive the right # + # banner, and authentication can proceed. See the following for more info # + # https://github.com/paramiko/paramiko/issues/432 # + # https://github.com/paramiko/paramiko/pull/438 # + ########################################################################### + + try: + client.connect(host, port=22, username=user, + password=password, pkey=pkey) + except paramiko.ssh_exception.SSHException as e: + if 'Error reading SSH protocol banner' == str(e): + # Once more, with feeling + client.connect(host, port=22, username=user, + password=password, pkey=pkey) + else: + # Reraise the original exception + raise e + + return client + + +def sftp(local_file, remote_file, host, user, password=None, key=None): + """Copy a local file to a remote host.""" + client = get_ssh_client(host, user, password, key) + + # Create an sftp connection from the underlying transport + sftp = paramiko.SFTPClient.from_transport(client.get_transport()) + sftp.put(local_file, remote_file) + client.close() + + +def ssh(cmd, host, user, password=None, key=None): + """Run an arbitrary command over SSH.""" + client = get_ssh_client(host, user, password, key) + + cmds = ' '.join(cmd) + stdin, stdout, stderr = client.exec_command(cmds, get_pty=True) + retcode = stdout.channel.recv_exit_status() + client.close() # @TODO re-use connections + if retcode > 0: + output = stderr.read().strip() + raise CalledProcessError(returncode=retcode, cmd=cmd, + output=output) + return ( + stdout.read().decode('utf-8').strip(), + stderr.read().decode('utf-8').strip() + ) diff --git a/nscharm_policy_vnf/charms/vnf-policy/metadata.yaml b/nscharm_policy_vnf/charms/vnf-policy/metadata.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d1394f7a7e6311997d908965c464ea70570b5434 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/metadata.yaml @@ -0,0 +1,16 @@ +"name": "vnf-policy" +"summary": "" +"maintainer": "Adam Israel " +"description": | + +"tags": + # Replace "misc" with one or more whitelisted tags from this list: + # https://jujucharms.com/docs/stable/authors-charm-metadata +- "misc" +- "osm" +- "vnf" +"series": +- "xenial" +- "bionic" +- "trusty" +"subordinate": !!bool "false" diff --git a/nscharm_policy_vnf/charms/vnf-policy/reactive/__init__.py b/nscharm_policy_vnf/charms/vnf-policy/reactive/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/nscharm_policy_vnf/charms/vnf-policy/reactive/sshproxy.py b/nscharm_policy_vnf/charms/vnf-policy/reactive/sshproxy.py new file mode 100644 index 0000000000000000000000000000000000000000..5ef0f56f09612a9a775a1bc3037685cc1f27755e --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/reactive/sshproxy.py @@ -0,0 +1,210 @@ +## +# Copyright 2016 Canonical Ltd. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +## + +from charmhelpers.core import unitdata +from charmhelpers.core.hookenv import ( + action_fail, + action_get, + action_set, + config, + log, + status_set, + DEBUG, +) + +from charms.reactive.flags import register_trigger + +from charms.reactive import ( + clear_flag, + set_flag, + when, + when_not, + when_any, +) +import charms.sshproxy +import os +import subprocess + +# Register a trigger so that we can respond to config.changed, even if +# it's being cleared by another handler +register_trigger(when='config.changed', + set_flag='sshproxy.reconfigure') + + +# @when_any('config.changed', 'sshproxy.reconfigure') +@when_any('config.set.ssh-hostname', 'config.set.ssh-username', 'config.set.ssh-password', 'sshproxy.reconfigure') +def ssh_configured(): + """Check if charm is properly configured. + + Check to see if the charm is configured with SSH credentials. If so, + set a state flag that can be used to execute ssh-only actions. + + For example: + + @when('sshproxy.configured') + def run_remote_command(cmd): + ... + + @when_not('sshproxy.configured') + def run_local_command(cmd): + ... + """ + log("Checking sshproxy configuration", DEBUG) + cfg = config() + ssh_keys = ['ssh-hostname', 'ssh-username', + 'ssh-password', 'ssh-private-key'] + + if all(k in cfg for k in ssh_keys): + + # Store config in unitdata so it's accessible to sshproxy + db = unitdata.kv() + db.set('config', cfg) + + # Explicitly flush the kv so it's immediately available + db.flush() + + log("Verifying ssh credentials...", DEBUG) + (verified, output) = charms.sshproxy.verify_ssh_credentials() + if verified: + log("SSH credentials verified.", DEBUG) + set_flag('sshproxy.configured') + status_set('active', 'Ready!') + else: + clear_flag('sshproxy.configured') + status_set('blocked', "Remote machine not ready yet: {}".format(output)) + else: + log("No ssh credentials configured", DEBUG) + clear_flag('sshproxy.configured') + status_set('blocked', 'Invalid SSH credentials.') + + +def generate_ssh_key(): + """Generate a new 4096-bit rsa keypair. + + If there is an existing keypair for this unit, it will be overwritten. + """ + cfg = config() + if all(k in cfg for k in ['ssh-key-type', 'ssh-key-bits']): + keytype = cfg['ssh-key-type'] + bits = str(cfg['ssh-key-bits']) + privatekey = '/root/.ssh/id_juju_sshproxy' + publickey = "{}.pub".format(privatekey) + + if os.path.exists(privatekey): + os.remove(privatekey) + if os.path.exists(publickey): + os.remove(publickey) + + cmd = "ssh-keygen -t {} -b {} -N '' -f {}".format( + keytype, + bits, + privatekey + ) + + output, err = charms.sshproxy.run_local([cmd]) + if len(err) == 0: + return True + return False + + +@when('actions.generate-ssh-key') +def action_generate_ssh_key(): + """Generate a new 4096-bit rsa keypair. + + If there is an existing keypair for this unit, it will be overwritten. + """ + try: + if not generate_ssh_key(): + action_fail('Unable to generate ssh key.') + except subprocess.CalledProcessError as e: + action_fail('Command failed: %s (%s)' % + (' '.join(e.cmd), str(e.output))) + finally: + clear_flag('actions.generate-ssh-key') + + +def get_ssh_public_key(): + """Get the public SSH key of this unit.""" + publickey_path = '/root/.ssh/id_juju_sshproxy.pub' + publickey = None + if os.path.exists(publickey_path): + with open(publickey_path, 'r') as f: + publickey = f.read() + + return publickey + + +@when('actions.get-ssh-public-key') +def action_get_ssh_public_key(): + """Get the public SSH key of this unit.""" + try: + action_set({'pubkey': get_ssh_public_key()}) + except subprocess.CalledProcessError as e: + action_fail('Command failed: %s (%s)' % + (' '.join(e.cmd), str(e.output))) + finally: + clear_flag('actions.get-ssh-public-key') + + +@when('actions.verify-ssh-credentials') +def action_verify_ssh_credentials(): + """Verify the ssh credentials have been installed to the VNF. + + Attempts to run a stock command - `hostname` on the remote host. + """ + try: + (verified, output) = charms.sshproxy.verify_ssh_credentials() + action_set({ + 'output': output, + 'verified': verified, + }) + if not verified: + action_fail("Verification failed: {}".format( + output, + )) + finally: + clear_flag('actions.verify-ssh-credentials') + + +@when('actions.run') +def run_command(): + """Run an arbitrary command. + + Run an arbitrary command, either locally or over SSH with the configured + credentials. + """ + try: + cmd = action_get('command') + output, err = charms.sshproxy._run(cmd) + if len(err): + action_fail("Command '{}' returned error code {}".format(cmd, err)) + else: + action_set({'output': output}) + except subprocess.CalledProcessError as e: + action_fail('Command failed: %s (%s)' % + (' '.join(e.cmd), str(e.output))) + finally: + clear_flag('actions.run') + + +@when_not('sshproxy.installed') +def install_vnf_ubuntu_proxy(): + """Install and Configure SSH Proxy.""" + + log("Generating SSH key...", DEBUG) + generate_ssh_key() + set_flag('sshproxy.installed') diff --git a/nscharm_policy_vnf/charms/vnf-policy/reactive/vnf_policy.py b/nscharm_policy_vnf/charms/vnf-policy/reactive/vnf_policy.py new file mode 100644 index 0000000000000000000000000000000000000000..d589dfee4fdfc20420e7bee6f66846e6fa5384ab --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/reactive/vnf_policy.py @@ -0,0 +1,46 @@ +from charmhelpers.core.hookenv import ( + action_get, + action_fail, + action_set, + status_set, + log, +) + +from charms.reactive import ( + clear_flag, + set_flag, + when, + when_not, +) + + +@when('sshproxy.configured') +@when_not('vnf-policy.installed') +def install_vnf_b(): + set_flag('vnf-policy.installed') + status_set('active', 'Ready!') + + +@when('actions.set-policy') +def action_set_policy(): + """Set the policy for a given user. + + Sets the policy (bw and qos) for the specified user_id + """ + err = '' + updated = False + try: + user_id = action_get('user_id') + bw = action_get('bw') + qos = action_get('qos') + + # If this were a functional vnf, you would perform your operation here + # and may return a value to indicate success or failure. + updated = True + + except Exception as err: + action_fail(str(err)) + else: + action_set({'updated': updated}) + finally: + clear_flag('actions.set-policy') diff --git a/nscharm_policy_vnf/charms/vnf-policy/reactive/vnfproxy.py b/nscharm_policy_vnf/charms/vnf-policy/reactive/vnfproxy.py new file mode 100644 index 0000000000000000000000000000000000000000..6616991df245a920116b9301b52602f15ba7a9bc --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/reactive/vnfproxy.py @@ -0,0 +1,89 @@ +from charmhelpers.core.hookenv import ( + action_fail, + action_set, +) + +from charms.reactive import ( + when, + clear_flag, +) +import charms.sshproxy + + +@when('actions.reboot') +def reboot(): + err = '' + try: + result, err = charms.sshproxy._run("reboot") + except: + action_fail('command failed:' + err) + else: + action_set({'outout': result}) + finally: + clear_flag('actions.reboot') + + +############################################################################### +# Below is an example implementation of the start/stop/restart actions. # +# To use this, copy the below code into your layer and add the appropriate # +# command(s) necessary to perform the action. # +############################################################################### + +# @when('actions.start') +# def start(): +# err = '' +# try: +# cmd = "service myname start" +# result, err = charms.sshproxy._run(cmd) +# except: +# action_fail('command failed:' + err) +# else: +# action_set({'outout': result}) +# finally: +# clear_flag('actions.start') +# +# +# @when('actions.stop') +# def stop(): +# err = '' +# try: +# # Enter the command to stop your service(s) +# cmd = "service myname stop" +# result, err = charms.sshproxy._run(cmd) +# except: +# action_fail('command failed:' + err) +# else: +# action_set({'outout': result}) +# finally: +# clear_flag('actions.stop') +# +# +# @when('actions.restart') +# def restart(): +# err = '' +# try: +# # Enter the command to restart your service(s) +# cmd = "service myname restart" +# result, err = charms.sshproxy._run(cmd) +# except: +# action_fail('command failed:' + err) +# else: +# action_set({'outout': result}) +# finally: +# clear_flag('actions.restart') +# +# +# @when('actions.upgrade') +# def upgrade_vnf(): +# err = '' +# try: +# # Add the command(s) to perform a VNF software upgrade +# cmd = '' +# result, err = charms.sshproxy._run(cmd) +# except: +# action_fail('command failed:' + err) +# else: +# action_set({'outout': result}) +# finally: +# clear_flag('actions.upgrade') +# diff --git a/nscharm_policy_vnf/charms/vnf-policy/requirements.txt b/nscharm_policy_vnf/charms/vnf-policy/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..28ecacab6029381bd43d65f8dcfc0cc704870f71 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/requirements.txt @@ -0,0 +1,2 @@ +flake8 +pytest diff --git a/nscharm_policy_vnf/charms/vnf-policy/tests/00-setup b/nscharm_policy_vnf/charms/vnf-policy/tests/00-setup new file mode 100755 index 0000000000000000000000000000000000000000..f0616a560a811bae78d7872dd433d612909c73cd --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/tests/00-setup @@ -0,0 +1,5 @@ +#!/bin/bash + +sudo add-apt-repository ppa:juju/stable -y +sudo apt-get update +sudo apt-get install amulet python-requests -y diff --git a/nscharm_policy_vnf/charms/vnf-policy/tests/10-deploy b/nscharm_policy_vnf/charms/vnf-policy/tests/10-deploy new file mode 100755 index 0000000000000000000000000000000000000000..065c79e39c12fcd23f6e48579d6265bf912686f5 --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/tests/10-deploy @@ -0,0 +1,35 @@ +#!/usr/bin/python3 + +import amulet +import requests +import unittest + + +class TestCharm(unittest.TestCase): + def setUp(self): + self.d = amulet.Deployment() + + self.d.add('vnf-b') + self.d.expose('vnf-b') + + self.d.setup(timeout=900) + self.d.sentry.wait() + + self.unit = self.d.sentry['vnf-b'][0] + + def test_service(self): + # test we can access over http + page = requests.get('http://{}'.format(self.unit.info['public-address'])) + self.assertEqual(page.status_code, 200) + # Now you can use self.d.sentry[SERVICE][UNIT] to address each of the units and perform + # more in-depth steps. Each self.d.sentry[SERVICE][UNIT] has the following methods: + # - .info - An array of the information of that unit from Juju + # - .file(PATH) - Get the details of a file on that unit + # - .file_contents(PATH) - Get plain text output of PATH file from that unit + # - .directory(PATH) - Get details of directory + # - .directory_contents(PATH) - List files and folders in PATH on that unit + # - .relation(relation, service:rel) - Get relation data from return service + + +if __name__ == '__main__': + unittest.main() diff --git a/nscharm_policy_vnf/charms/vnf-policy/version b/nscharm_policy_vnf/charms/vnf-policy/version new file mode 100644 index 0000000000000000000000000000000000000000..e412f8aaf4e137b95d10d1d6949d77de2922bcce --- /dev/null +++ b/nscharm_policy_vnf/charms/vnf-policy/version @@ -0,0 +1 @@ +83c13bd \ No newline at end of file diff --git a/nscharm_policy_vnf/charms/vnf-policy/wheelhouse/setuptools-41.6.0.zip b/nscharm_policy_vnf/charms/vnf-policy/wheelhouse/setuptools-41.6.0.zip new file mode 100644 index 0000000000000000000000000000000000000000..3345759c1ca861c181742e84fc5285e3284a2886 Binary files /dev/null and b/nscharm_policy_vnf/charms/vnf-policy/wheelhouse/setuptools-41.6.0.zip differ diff --git a/nscharm_policy_vnf/cloud_init/cloud-config.txt b/nscharm_policy_vnf/cloud_init/cloud-config.txt new file mode 100755 index 0000000000000000000000000000000000000000..f6c1b9dcf3a0506e7366e58d0318a5df5f2639ce --- /dev/null +++ b/nscharm_policy_vnf/cloud_init/cloud-config.txt @@ -0,0 +1,13 @@ +#cloud-config +password: osm4u +chpasswd: { expire: False } +ssh_pwauth: True + +write_files: +- content: | + # My new helloworld file + + owner: root:root + permissions: '0644' + path: /root/helloworld.txt + diff --git a/nscharm_policy_vnf/nscharm_policy_vnfd.yaml b/nscharm_policy_vnf/nscharm_policy_vnfd.yaml new file mode 100644 index 0000000000000000000000000000000000000000..895c24c9265ed307ef90f76f6fffd6fbc2f278fd --- /dev/null +++ b/nscharm_policy_vnf/nscharm_policy_vnfd.yaml @@ -0,0 +1,68 @@ +vnfd:vnfd-catalog: + vnfd: + - id: nscharm-policy-vnf + name: nscharm-policy-vnf + short-name: nscharm-policy-vnf + version: '1.0' + description: A VNF consisting of 2 VDUs w/proxy charm + logo: osm.png + connection-point: + - id: vnf-mgmt + name: vnf-mgmt + short-name: vnf-mgmt + type: VPORT + # - id: vnf-data + # name: vnf-data + # short-name: vnf-data + # type: VPORT + mgmt-interface: + cp: vnf-mgmt + internal-vld: + - id: internal + name: internal + short-name: internal + type: ELAN + internal-connection-point: + - id-ref: policyVM-internal + vdu: + - id: policyVM + name: policyVM + image: ubuntu1604 + count: '1' + vm-flavor: + vcpu-count: '1' + memory-mb: '1024' + storage-gb: '10' + interface: + - name: policyVM-eth0 + position: '1' + type: EXTERNAL + virtual-interface: + type: VIRTIO + external-connection-point-ref: vnf-mgmt + - name: policyVM-eth1 + position: '2' + type: INTERNAL + virtual-interface: + type: VIRTIO + internal-connection-point-ref: policyVM-internal + internal-connection-point: + - id: policyVM-internal + name: policyVM-internal + short-name: policyVM-internal + type: VPORT + cloud-init-file: cloud-config.txt + vdu-configuration: + juju: + charm: vnf-policy + proxy: true + initial-config-primitive: + - seq: '1' + name: config + parameter: + - name: ssh-hostname + value: + - name: ssh-username + value: ubuntu + - name: ssh-password + value: osm4u \ No newline at end of file diff --git a/nscharm_user_vnf/charms/vnf-user/.build.manifest b/nscharm_user_vnf/charms/vnf-user/.build.manifest new file mode 100644 index 0000000000000000000000000000000000000000..be8ebcf8fb6727e40f7a8e422f7703fb4775daa8 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/.build.manifest @@ -0,0 +1,351 @@ +{ + "layers": [ + { + "rev": "fcdcea4e5de3e1556c24e6704607862d0ba00a56", + "url": "layer:options" + }, + { + "rev": "1d2489bff56daf2f8d1c06ee9248bb0094e4ad49", + "url": "layer:basic" + }, + { + "rev": "29d9dd2e642048f84e1ac388e03756c6cdec3551", + "url": "layer:sshproxy" + }, + { + "rev": "e6a495fab012f06484ed419fd5c2453e6a92eef1", + "url": "layer:vnfproxy" + }, + { + "rev": "8c2332f258eebb94a1d74939d82919d7db6acf6f", + "url": "vnf-user" + } + ], + "signatures": { + ".build.manifest": [ + "build", + "dynamic", + "unchecked" + ], + ".gitignore": [ + "layer:sshproxy", + "static", + "17526a7f7312e7eefb932d1c514b7bc8425fab5bd1ade149e106ecf8bff67358" + ], + "LICENSE": [ + "layer:basic", + "static", + "cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30" + ], + "Makefile": [ + "layer:basic", + "static", + "b7ab3a34e5faf79b96a8632039a0ad0aa87f2a9b5f0ba604e007cafb22190301" + ], + "README.md": [ + "vnf-user", + "static", + "977a02e0bce81227970cca431a9abbf2df0c0eb7c1f8f39df522c01df5a8db7f" + ], + "actions.yaml": [ + "vnf-user", + "dynamic", + "0f81fbe14426ea342af768ad92d07db438db91a5b8714963e221892571815d37" + ], + "actions/add-user": [ + "vnf-user", + "static", + "46bf4be02078a0f5ac60ab81609372a8f9943885b7a4999ce5332ea2f4857036" + ], + "actions/generate-ssh-key": [ + "layer:sshproxy", + "static", + "46bf4be02078a0f5ac60ab81609372a8f9943885b7a4999ce5332ea2f4857036" + ], + "actions/get-ssh-public-key": [ + "layer:sshproxy", + "static", + "46bf4be02078a0f5ac60ab81609372a8f9943885b7a4999ce5332ea2f4857036" + ], + "actions/reboot": [ + "layer:vnfproxy", + "static", + "afc0df4409fb6eb7b7e6595490913c1efe329b393d4860ae226d8aaccc01fcf2" + ], + "actions/restart": [ + "layer:vnfproxy", + "static", + "afc0df4409fb6eb7b7e6595490913c1efe329b393d4860ae226d8aaccc01fcf2" + ], + "actions/run": [ + "layer:sshproxy", + "static", + "46bf4be02078a0f5ac60ab81609372a8f9943885b7a4999ce5332ea2f4857036" + ], + "actions/start": [ + "layer:vnfproxy", + "static", + "afc0df4409fb6eb7b7e6595490913c1efe329b393d4860ae226d8aaccc01fcf2" + ], + "actions/stop": [ + "layer:vnfproxy", + "static", + "afc0df4409fb6eb7b7e6595490913c1efe329b393d4860ae226d8aaccc01fcf2" + ], + "actions/upgrade": [ + "layer:vnfproxy", + "static", + "afc0df4409fb6eb7b7e6595490913c1efe329b393d4860ae226d8aaccc01fcf2" + ], + "actions/verify-ssh-credentials": [ + "layer:sshproxy", + "static", + "46bf4be02078a0f5ac60ab81609372a8f9943885b7a4999ce5332ea2f4857036" + ], + "bin/charm-env": [ + "layer:basic", + "static", + "458c53532c19ee357cbf4209ccc7d811810718ba0ea3b0588b3d3ef040a44b8e" + ], + "bin/layer_option": [ + "layer:options", + "static", + "e959bf29da4c5edff28b2602c24113c4df9e25cdc9f2aa3b5d46c8577b2a40cc" + ], + "config.yaml": [ + "layer:vnfproxy", + "dynamic", + "c5bd79d6699ffdf49cc71ab18a6e8f0e7b0ca71fae2f1416a2138154fe6c1a34" + ], + "copyright": [ + "layer:basic", + "static", + "f6740d66fd60b60f2533d9fcb53907078d1e20920a0219afce7182e2a1c97629" + ], + "copyright.layer-options": [ + "layer:options", + "static", + "f6740d66fd60b60f2533d9fcb53907078d1e20920a0219afce7182e2a1c97629" + ], + "hooks/collect-metrics": [ + "layer:vnfproxy", + "static", + "f2a6360cf21993f7dc07785ad702f141679a5a9b03c3db5d50dd06ffbe26bbb2" + ], + "hooks/config-changed": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/hook.template": [ + "layer:basic", + "static", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/install": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/leader-elected": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/leader-settings-changed": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/post-series-upgrade": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/pre-series-upgrade": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/start": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/stop": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/update-status": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "hooks/upgrade-charm": [ + "layer:basic", + "dynamic", + "2b693cb2a11594a80cc91235c2dc219a0a6303ae62bee8aa87eb35781f7158f7" + ], + "icon.svg": [ + "vnf-user", + "static", + "d20624e9389af6506a8d8a69ac9bba4d41709601b624c0875fd7d6717b395088" + ], + "layer.yaml": [ + "vnf-user", + "dynamic", + "9082d2e11f485d7af081f99542c949e3580add8dcf00a72c90bd096d850b75e1" + ], + "lib/charms/layer/__init__.py": [ + "layer:basic", + "static", + "dfe0d26c6bf409767de6e2546bc648f150e1b396243619bad3aa0553ab7e0e6f" + ], + "lib/charms/layer/basic.py": [ + "layer:basic", + "static", + "445652dbaa1f0b84a7215da185bcbdff097bb9bbbce11b4c2dbbff90f77719a9" + ], + "lib/charms/layer/execd.py": [ + "layer:basic", + "static", + "fda8bd491032db1db8ddaf4e99e7cc878c6fb5432efe1f91cadb5b34765d076d" + ], + "lib/charms/layer/options.py": [ + "layer:options", + "static", + "8ae7a07d22542fc964f2d2bee8219d1c78a68dace70a1b38d36d4aea47b1c3b2" + ], + "lib/charms/sshproxy.py": [ + "layer:sshproxy", + "static", + "fba0834f98f570b0b3d6b3e8a7f17b1d8909f50da9410ccda89b586f914b7ffd" + ], + "metadata.yaml": [ + "vnf-user", + "dynamic", + "230c0ac0564769b19ac10111235b95a2706737645bb4cd32cab9e052d9351afe" + ], + "reactive/__init__.py": [ + "layer:basic", + "static", + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + ], + "reactive/sshproxy.py": [ + "layer:sshproxy", + "static", + "acfaedf414946f8d218e5ebd04177f87258286915ba83b9c42ff7385975da6ab" + ], + "reactive/vnf_user.py": [ + "vnf-user", + "static", + "329c4fe24344bea55151fb8f840caeddce87fc1d97cb61b4f47cbd0690d99f05" + ], + "reactive/vnfproxy.py": [ + "layer:vnfproxy", + "static", + "8e4101a72f02832e5c233b731981ff483675be608321b8eb9743d605b3f7d77a" + ], + "requirements.txt": [ + "layer:basic", + "static", + "0f1c70d27e26005a96d66ad54482877ae20f7737693c833e29dd72bd6ac24892" + ], + "tests/00-setup": [ + "vnf-user", + "static", + "111c079b81d260bbcd716dcf41672372a4cf4aaa14154b6c3055deeedae37a06" + ], + "tests/10-deploy": [ + "vnf-user", + "static", + "839d6a11ddd13334be05f37874bff464b7345aff7bff5fed2dcb7ad7eaaa9a05" + ], + "version": [ + "vnf-user", + "dynamic", + "02b6a61e71b793429e0391c6170475f2d35a07035a1f9db392271e8c44e76dd2" + ], + "wheelhouse/Jinja2-2.10.3.tar.gz": [ + "layer:basic", + "dynamic", + "9fe95f19286cfefaa917656583d020be14e7859c6b0252588391e47db34527de" + ], + "wheelhouse/MarkupSafe-1.1.1.tar.gz": [ + "layer:basic", + "dynamic", + "29872e92839765e546828bb7754a68c418d927cd064fd4708fab9fe9c8bb116b" + ], + "wheelhouse/PyYAML-5.2.tar.gz": [ + "layer:basic", + "dynamic", + "c0ee8eca2c582d29c3c2ec6e2c4f703d1b7f1fb10bc72317355a746057e7346c" + ], + "wheelhouse/Tempita-0.5.2.tar.gz": [ + "layer:basic", + "dynamic", + "cacecf0baa674d356641f1d406b8bff1d756d739c46b869a54de515d08e6fc9c" + ], + "wheelhouse/charmhelpers-0.20.7.tar.gz": [ + "layer:basic", + "dynamic", + "e0f8d005d39cded1b0c5997d8ef1d90832341c67ebeb4a334ad1eb348fbd803a" + ], + "wheelhouse/charms.reactive-1.3.0.tar.gz": [ + "layer:basic", + "dynamic", + "82d2c614c82d64bf56e913990f22663e5de64b99db15838abfd9a064f1cc2f51" + ], + "wheelhouse/ecdsa-0.14.1.tar.gz": [ + "layer:sshproxy", + "dynamic", + "64c613005f13efec6541bb0a33290d0d03c27abab5f15fbab20fb0ee162bdd8e" + ], + "wheelhouse/netaddr-0.7.19.tar.gz": [ + "layer:basic", + "dynamic", + "38aeec7cdd035081d3a4c306394b19d677623bf76fa0913f6695127c7753aefd" + ], + "wheelhouse/paramiko-1.16.3.tar.gz": [ + "layer:sshproxy", + "dynamic", + "97d932fdb4fec9aadf6bea368123f3ee15b92199f92eb62666370c7fed62d072" + ], + "wheelhouse/pip-18.1.tar.gz": [ + "layer:basic", + "dynamic", + "c0a292bd977ef590379a3f05d7b7f65135487b67470f6281289a94e015650ea1" + ], + "wheelhouse/pyaml-19.4.1.tar.gz": [ + "layer:basic", + "dynamic", + "c79ae98ececda136a034115ca178ee8bf3aa7df236c488c2f55d12f177b88f1e" + ], + "wheelhouse/pycrypto-2.6.1.tar.gz": [ + "layer:sshproxy", + "dynamic", + "f2ce1e989b272cfcb677616763e0a2e7ec659effa67a88aa92b3a65528f60a3c" + ], + "wheelhouse/setuptools-41.6.0.zip": [ + "layer:basic", + "dynamic", + "6afa61b391dcd16cb8890ec9f66cc4015a8a31a6e1c2b4e0c464514be1a3d722" + ], + "wheelhouse/setuptools_scm-1.17.0.tar.gz": [ + "layer:basic", + "dynamic", + "70a4cf5584e966ae92f54a764e6437af992ba42ac4bca7eb37cc5d02b98ec40a" + ], + "wheelhouse/six-1.13.0.tar.gz": [ + "layer:sshproxy", + "dynamic", + "30f610279e8b2578cab6db20741130331735c781b56053c59c4076da27f06b66" + ], + "wheelhouse/wheel-0.33.6.tar.gz": [ + "layer:basic", + "dynamic", + "10c9da68765315ed98850f8e048347c3eb06dd81822dc2ab1d4fde9dc9702646" + ] + } +} \ No newline at end of file diff --git a/nscharm_user_vnf/charms/vnf-user/.gitignore b/nscharm_user_vnf/charms/vnf-user/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..b8e7ba3a229bda75224369127d7454019fea641f --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/.gitignore @@ -0,0 +1 @@ +trusty/ diff --git a/nscharm_user_vnf/charms/vnf-user/LICENSE b/nscharm_user_vnf/charms/vnf-user/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..d645695673349e3947e8e5ae42332d0ac3164cd7 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/nscharm_user_vnf/charms/vnf-user/Makefile b/nscharm_user_vnf/charms/vnf-user/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..a1ad3a5cd27751144f6bd1a0a7db50f1e11b18eb --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/Makefile @@ -0,0 +1,24 @@ +#!/usr/bin/make + +all: lint unit_test + + +.PHONY: clean +clean: + @rm -rf .tox + +.PHONY: apt_prereqs +apt_prereqs: + @# Need tox, but don't install the apt version unless we have to (don't want to conflict with pip) + @which tox >/dev/null || (sudo apt-get install -y python-pip && sudo pip install tox) + +.PHONY: lint +lint: apt_prereqs + @tox --notest + @PATH=.tox/py34/bin:.tox/py35/bin flake8 $(wildcard hooks reactive lib unit_tests tests) + @charm proof + +.PHONY: unit_test +unit_test: apt_prereqs + @echo Starting tests... + tox diff --git a/nscharm_user_vnf/charms/vnf-user/README.md b/nscharm_user_vnf/charms/vnf-user/README.md new file mode 100644 index 0000000000000000000000000000000000000000..30175122ed7fc2dca2cef911451cc4f99d922562 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/README.md @@ -0,0 +1,26 @@ +# Overview + +This is an example of an Open Source Mano (OSM) charm used with the Network Service (ns) example. + +Network Service charms are an **experimental** feature, expected to be released with OSM R6 in the spring of 2019. + +# Usage + + +## Known Limitations and Issues + +This not only helps users but gives people a place to start if they want to help +you add features to your charm. + +# Configuration + +None + +# Contact Information +## Open Source Mano (OSM) + + - [OSM website](https://osm.etsi.org/) + - [OSM bug tracker](https://osm.etsi.org/bugzilla/) + - [OSM_TECH](mailto:OSM_TECH@list.etsi.org) mailing list + - [Slack](https://join.slack.com/t/opensourcemano/shared_invite/enQtMzQ3MzYzNTQ0NDIyLWJkMzRjNDM0MjFjODYzMGQ3ODIzMzJlNTg2ZGI5OTdiZjFiNDMyMzYxMjRjNDU4N2FmNjRjNzY5NTE1MjgzOTQ) + diff --git a/nscharm_user_vnf/charms/vnf-user/actions.yaml b/nscharm_user_vnf/charms/vnf-user/actions.yaml new file mode 100644 index 0000000000000000000000000000000000000000..c6cdca61448e6173761254dac7b3427bd3bf18a9 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/actions.yaml @@ -0,0 +1,57 @@ +## +# Copyright 2016 Canonical Ltd. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +## + +"run": + "description": "Run an arbitrary command" + "params": + "command": + "description": "The command to execute." + "type": "string" + "default": "" + "required": + - "command" +"generate-ssh-key": + "description": "Generate a new SSH keypair for this unit. This will replace any\ + \ existing previously generated keypair." +"verify-ssh-credentials": + "description": "Verify that this unit can authenticate with server specified by\ + \ ssh-hostname and ssh-username." +"get-ssh-public-key": + "description": "Get the public SSH key for this unit." +"start": + "description": "Stop the service on the VNF." +"stop": + "description": "Stop the service on the VNF." +"restart": + "description": "Stop the service on the VNF." +"reboot": + "description": "Reboot the VNF virtual machine." +"upgrade": + "description": "Upgrade the software on the VNF." +"add-user": + "description": "Add a new username to the database" + "params": + "username": + "description": "The username to add" + "type": "string" + "default": "" + "tariff": + "description": "" + "type": "integer" + "default": !!int "0" + "required": + - "username" diff --git a/nscharm_user_vnf/charms/vnf-user/actions/add-user b/nscharm_user_vnf/charms/vnf-user/actions/add-user new file mode 100755 index 0000000000000000000000000000000000000000..7e30af4cfa084120a9343bf71c421b69ec3f2967 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/actions/add-user @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +## +# Copyright 2016 Canonical Ltd. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +## +import sys +sys.path.append('lib') + +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +""" +`set_state` only works here because it's flushed to disk inside the `main()` +loop. remove_state will need to be called inside the action method. +""" +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_user_vnf/charms/vnf-user/actions/generate-ssh-key b/nscharm_user_vnf/charms/vnf-user/actions/generate-ssh-key new file mode 100755 index 0000000000000000000000000000000000000000..7e30af4cfa084120a9343bf71c421b69ec3f2967 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/actions/generate-ssh-key @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +## +# Copyright 2016 Canonical Ltd. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +## +import sys +sys.path.append('lib') + +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +""" +`set_state` only works here because it's flushed to disk inside the `main()` +loop. remove_state will need to be called inside the action method. +""" +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_user_vnf/charms/vnf-user/actions/get-ssh-public-key b/nscharm_user_vnf/charms/vnf-user/actions/get-ssh-public-key new file mode 100755 index 0000000000000000000000000000000000000000..7e30af4cfa084120a9343bf71c421b69ec3f2967 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/actions/get-ssh-public-key @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +## +# Copyright 2016 Canonical Ltd. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +## +import sys +sys.path.append('lib') + +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +""" +`set_state` only works here because it's flushed to disk inside the `main()` +loop. remove_state will need to be called inside the action method. +""" +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_user_vnf/charms/vnf-user/actions/reboot b/nscharm_user_vnf/charms/vnf-user/actions/reboot new file mode 100755 index 0000000000000000000000000000000000000000..9a2ba24b9efdfa3d21457469c503c050196fb170 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/actions/reboot @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +import sys +sys.path.append('lib') +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_user_vnf/charms/vnf-user/actions/restart b/nscharm_user_vnf/charms/vnf-user/actions/restart new file mode 100755 index 0000000000000000000000000000000000000000..9a2ba24b9efdfa3d21457469c503c050196fb170 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/actions/restart @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +import sys +sys.path.append('lib') +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_user_vnf/charms/vnf-user/actions/run b/nscharm_user_vnf/charms/vnf-user/actions/run new file mode 100755 index 0000000000000000000000000000000000000000..7e30af4cfa084120a9343bf71c421b69ec3f2967 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/actions/run @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +## +# Copyright 2016 Canonical Ltd. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +## +import sys +sys.path.append('lib') + +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +""" +`set_state` only works here because it's flushed to disk inside the `main()` +loop. remove_state will need to be called inside the action method. +""" +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_user_vnf/charms/vnf-user/actions/start b/nscharm_user_vnf/charms/vnf-user/actions/start new file mode 100755 index 0000000000000000000000000000000000000000..9a2ba24b9efdfa3d21457469c503c050196fb170 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/actions/start @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +import sys +sys.path.append('lib') +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_user_vnf/charms/vnf-user/actions/stop b/nscharm_user_vnf/charms/vnf-user/actions/stop new file mode 100755 index 0000000000000000000000000000000000000000..9a2ba24b9efdfa3d21457469c503c050196fb170 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/actions/stop @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +import sys +sys.path.append('lib') +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_user_vnf/charms/vnf-user/actions/upgrade b/nscharm_user_vnf/charms/vnf-user/actions/upgrade new file mode 100755 index 0000000000000000000000000000000000000000..9a2ba24b9efdfa3d21457469c503c050196fb170 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/actions/upgrade @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +import sys +sys.path.append('lib') +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_user_vnf/charms/vnf-user/actions/verify-ssh-credentials b/nscharm_user_vnf/charms/vnf-user/actions/verify-ssh-credentials new file mode 100755 index 0000000000000000000000000000000000000000..7e30af4cfa084120a9343bf71c421b69ec3f2967 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/actions/verify-ssh-credentials @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +## +# Copyright 2016 Canonical Ltd. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +## +import sys +sys.path.append('lib') + +from charms.reactive import main, set_flag +from charmhelpers.core.hookenv import action_fail, action_name + +""" +`set_state` only works here because it's flushed to disk inside the `main()` +loop. remove_state will need to be called inside the action method. +""" +set_flag('actions.{}'.format(action_name())) + +try: + main() +except Exception as e: + action_fail(repr(e)) diff --git a/nscharm_user_vnf/charms/vnf-user/bin/charm-env b/nscharm_user_vnf/charms/vnf-user/bin/charm-env new file mode 100755 index 0000000000000000000000000000000000000000..aca7de3c1022ecad619cb82a4ac3160fb6b5677d --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/bin/charm-env @@ -0,0 +1,107 @@ +#!/bin/bash + +VERSION="1.0.0" + + +find_charm_dirs() { + # Hopefully, $JUJU_CHARM_DIR is set so which venv to use in unambiguous. + if [[ -n "$JUJU_CHARM_DIR" || -n "$CHARM_DIR" ]]; then + if [[ -z "$JUJU_CHARM_DIR" ]]; then + # accept $CHARM_DIR to be more forgiving + export JUJU_CHARM_DIR="$CHARM_DIR" + fi + if [[ -z "$CHARM_DIR" ]]; then + # set CHARM_DIR as well to help with backwards compatibility + export CHARM_DIR="$JUJU_CHARM_DIR" + fi + return + fi + # Try to guess the value for JUJU_CHARM_DIR by looking for a non-subordinate + # (because there's got to be at least one principle) charm directory; + # if there are several, pick the first by alpha order. + agents_dir="/var/lib/juju/agents" + if [[ -d "$agents_dir" ]]; then + desired_charm="$1" + found_charm_dir="" + if [[ -n "$desired_charm" ]]; then + for charm_dir in $(/bin/ls -d "$agents_dir"/unit-*/charm); do + charm_name="$(JUJU_CHARM_DIR="$charm_dir" charm-env python3 -c 'from charmhelpers.core.hookenv import charm_name; print(charm_name())')" + if [[ "$charm_name" == "$desired_charm" ]]; then + if [[ -n "$found_charm_dir" ]]; then + >&2 echo "Ambiguous possibilities for JUJU_CHARM_DIR matching '$desired_charm'; please run within a Juju hook context" + exit 1 + fi + found_charm_dir="$charm_dir" + fi + done + if [[ -z "$found_charm_dir" ]]; then + >&2 echo "Unable to determine JUJU_CHARM_DIR matching '$desired_charm'; please run within a Juju hook context" + exit 1 + fi + export JUJU_CHARM_DIR="$found_charm_dir" + export CHARM_DIR="$found_charm_dir" + return + fi + # shellcheck disable=SC2126 + non_subordinates="$(grep -L 'subordinate:.*true' "$agents_dir"/unit-*/charm/metadata.yaml | wc -l)" + if [[ "$non_subordinates" -gt 1 ]]; then + >&2 echo 'Ambiguous possibilities for JUJU_CHARM_DIR; please use --charm or run within a Juju hook context' + exit 1 + elif [[ "$non_subordinates" -eq 1 ]]; then + for charm_dir in $(/bin/ls -d "$agents_dir"/unit-*/charm); do + if grep -q 'subordinate:.*true' "$charm_dir/metadata.yaml"; then + continue + fi + export JUJU_CHARM_DIR="$charm_dir" + export CHARM_DIR="$charm_dir" + return + done + fi + fi + >&2 echo 'Unable to determine JUJU_CHARM_DIR; please run within a Juju hook context' + exit 1 +} + +try_activate_venv() { + if [[ -d "$JUJU_CHARM_DIR/../.venv" ]]; then + . "$JUJU_CHARM_DIR/../.venv/bin/activate" + fi +} + +find_wrapped() { + PATH="${PATH/\/usr\/local\/sbin:}" which "$(basename "$0")" +} + + +if [[ "$1" == "--version" || "$1" == "-v" ]]; then + echo "$VERSION" + exit 0 +fi + + +# allow --charm option to hint which JUJU_CHARM_DIR to choose when ambiguous +# NB: --charm option must come first +# NB: option must be processed outside find_charm_dirs to modify $@ +charm_name="" +if [[ "$1" == "--charm" ]]; then + charm_name="$2" + shift; shift +fi + +find_charm_dirs "$charm_name" +try_activate_venv +export PYTHONPATH="$JUJU_CHARM_DIR/lib:$PYTHONPATH" + +if [[ "$(basename "$0")" == "charm-env" ]]; then + # being used as a shebang + exec "$@" +elif [[ "$0" == "$BASH_SOURCE" ]]; then + # being invoked as a symlink wrapping something to find in the venv + exec "$(find_wrapped)" "$@" +elif [[ "$(basename "$BASH_SOURCE")" == "charm-env" ]]; then + # being sourced directly; do nothing + /bin/true +else + # being sourced for wrapped bash helpers + . "$(find_wrapped)" +fi diff --git a/nscharm_user_vnf/charms/vnf-user/bin/layer_option b/nscharm_user_vnf/charms/vnf-user/bin/layer_option new file mode 100755 index 0000000000000000000000000000000000000000..3253ef8aadb95807f26e5ebd8486adde67e5cbc2 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/bin/layer_option @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +import sys +import argparse +from charms import layer + + +parser = argparse.ArgumentParser(description='Access layer options.') +parser.add_argument('section', + help='the section, or layer, the option is from') +parser.add_argument('option', + help='the option to access') + +args = parser.parse_args() +value = layer.options.get(args.section, args.option) +if isinstance(value, bool): + sys.exit(0 if value else 1) +elif isinstance(value, list): + for val in value: + print(val) +else: + print(value) diff --git a/nscharm_user_vnf/charms/vnf-user/config.yaml b/nscharm_user_vnf/charms/vnf-user/config.yaml new file mode 100644 index 0000000000000000000000000000000000000000..a7d9291c137d1907b7ef2ba019123aa9201ef107 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/config.yaml @@ -0,0 +1,29 @@ +"options": + "ssh-hostname": + "type": "string" + "default": "" + "description": "The hostname or IP address of the machine to" + "ssh-username": + "type": "string" + "default": "" + "description": "The username to login as." + "ssh-password": + "type": "string" + "default": "" + "description": "The password used to authenticate." + "ssh-private-key": + "type": "string" + "default": "" + "description": "DEPRECATED. The private ssh key to be used to authenticate." + "ssh-public-key": + "type": "string" + "default": "" + "description": "The public key of this unit." + "ssh-key-type": + "type": "string" + "default": "rsa" + "description": "The type of encryption to use for the SSH key." + "ssh-key-bits": + "type": "int" + "default": !!int "4096" + "description": "The number of bits to use for the SSH key." diff --git a/nscharm_user_vnf/charms/vnf-user/copyright b/nscharm_user_vnf/charms/vnf-user/copyright new file mode 100644 index 0000000000000000000000000000000000000000..d4fdd18281c632d030a301d26d45b4dabdb308ef --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/copyright @@ -0,0 +1,16 @@ +Format: http://dep.debian.net/deps/dep5/ + +Files: * +Copyright: Copyright 2015-2017, Canonical Ltd., All Rights Reserved. +License: Apache License 2.0 + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + . + http://www.apache.org/licenses/LICENSE-2.0 + . + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/nscharm_user_vnf/charms/vnf-user/copyright.layer-options b/nscharm_user_vnf/charms/vnf-user/copyright.layer-options new file mode 100644 index 0000000000000000000000000000000000000000..d4fdd18281c632d030a301d26d45b4dabdb308ef --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/copyright.layer-options @@ -0,0 +1,16 @@ +Format: http://dep.debian.net/deps/dep5/ + +Files: * +Copyright: Copyright 2015-2017, Canonical Ltd., All Rights Reserved. +License: Apache License 2.0 + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + . + http://www.apache.org/licenses/LICENSE-2.0 + . + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/nscharm_user_vnf/charms/vnf-user/hooks/collect-metrics b/nscharm_user_vnf/charms/vnf-user/hooks/collect-metrics new file mode 100755 index 0000000000000000000000000000000000000000..ecd7e62385689b00cc023b1057a0061b0f373134 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/hooks/collect-metrics @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 +"""Handle the collect-metrics hook via proxy. + +The normal metrics layer will only execute metrics collection against the local +machine. This hook implements the same approach, but runs the collection code +against the configured ssh proxy. + +Because the metrics hook runs in a restricted context, it can't be run as a +normal reactive event, nor can it access things like config. +""" + +import os +import shlex +from subprocess import check_call, CalledProcessError +import sys +import yaml + +# Load modules from $CHARM_DIR/lib +sys.path.append('lib') +import charms.sshproxy + + +def build_command(doc): + """Build the commands to report metrics. + + Build a list of `add-metric` commands to report the current metrics + back to the Juju controller. + """ + values = {} + metrics = doc.get("metrics", {}) + for metric, mdoc in metrics.items(): + cmd = mdoc.get("command") + if cmd: + try: + value, err = charms.sshproxy._run( + # The command may contain quotes that need to be preserved, + # i.e., `awk '{print $1}' /proc/uptime` + shlex.split(cmd, posix=False) + ) + except Exception as e: + # Ignore all errors + with open("metrics.log", "a") as f: + f.write("{}".format(e)) + continue + + if isinstance(value, bytes): + value = value.decode('utf-8') + + value = value.strip() + if value: + values[metric] = value + + if not values: + return None + command = ["add-metric"] + for metric, value in values.items(): + command.append("%s=%s" % (metric, value)) + return command + + +if __name__ == '__main__': + charm_dir = os.path.dirname(os.path.abspath(os.path.join(__file__, ".."))) + metrics_yaml = os.path.join(charm_dir, "metrics.yaml") + with open(metrics_yaml) as f: + doc = yaml.load(f) + command = build_command(doc) + if command: + check_call(command) diff --git a/nscharm_user_vnf/charms/vnf-user/hooks/config-changed b/nscharm_user_vnf/charms/vnf-user/hooks/config-changed new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/hooks/config-changed @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_user_vnf/charms/vnf-user/hooks/hook.template b/nscharm_user_vnf/charms/vnf-user/hooks/hook.template new file mode 100644 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/hooks/hook.template @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_user_vnf/charms/vnf-user/hooks/install b/nscharm_user_vnf/charms/vnf-user/hooks/install new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/hooks/install @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_user_vnf/charms/vnf-user/hooks/leader-elected b/nscharm_user_vnf/charms/vnf-user/hooks/leader-elected new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/hooks/leader-elected @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_user_vnf/charms/vnf-user/hooks/leader-settings-changed b/nscharm_user_vnf/charms/vnf-user/hooks/leader-settings-changed new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/hooks/leader-settings-changed @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_user_vnf/charms/vnf-user/hooks/post-series-upgrade b/nscharm_user_vnf/charms/vnf-user/hooks/post-series-upgrade new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/hooks/post-series-upgrade @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_user_vnf/charms/vnf-user/hooks/pre-series-upgrade b/nscharm_user_vnf/charms/vnf-user/hooks/pre-series-upgrade new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/hooks/pre-series-upgrade @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_user_vnf/charms/vnf-user/hooks/start b/nscharm_user_vnf/charms/vnf-user/hooks/start new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/hooks/start @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_user_vnf/charms/vnf-user/hooks/stop b/nscharm_user_vnf/charms/vnf-user/hooks/stop new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/hooks/stop @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_user_vnf/charms/vnf-user/hooks/update-status b/nscharm_user_vnf/charms/vnf-user/hooks/update-status new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/hooks/update-status @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_user_vnf/charms/vnf-user/hooks/upgrade-charm b/nscharm_user_vnf/charms/vnf-user/hooks/upgrade-charm new file mode 100755 index 0000000000000000000000000000000000000000..9858c6be15b8a682778ab4727835151e1b693801 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/hooks/upgrade-charm @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +# Load modules from $JUJU_CHARM_DIR/lib +import sys +sys.path.append('lib') + +from charms.layer import basic # noqa +basic.bootstrap_charm_deps() + +from charmhelpers.core import hookenv # noqa +hookenv.atstart(basic.init_config_states) +hookenv.atexit(basic.clear_config_states) + + +# This will load and run the appropriate @hook and other decorated +# handlers from $JUJU_CHARM_DIR/reactive, $JUJU_CHARM_DIR/hooks/reactive, +# and $JUJU_CHARM_DIR/hooks/relations. +# +# See https://jujucharms.com/docs/stable/authors-charm-building +# for more information on this pattern. +from charms.reactive import main # noqa +main() diff --git a/nscharm_user_vnf/charms/vnf-user/icon.svg b/nscharm_user_vnf/charms/vnf-user/icon.svg new file mode 100644 index 0000000000000000000000000000000000000000..96a5d0c068d4b3271c7e397f76cf77fdf0651038 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/icon.svg @@ -0,0 +1,279 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + diff --git a/nscharm_user_vnf/charms/vnf-user/layer.yaml b/nscharm_user_vnf/charms/vnf-user/layer.yaml new file mode 100644 index 0000000000000000000000000000000000000000..dd758c57765230bb62f721a72d90bed80f1f74c3 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/layer.yaml @@ -0,0 +1,16 @@ +"includes": +- "layer:options" +- "layer:basic" +- "layer:sshproxy" +- "layer:vnfproxy" +"exclude": [".travis.yml", "tests", "tox.ini", "test-requirements.txt"] +"options": + "basic": + "use_venv": !!bool "false" + "packages": [] + "python_packages": [] + "include_system_packages": !!bool "false" + "sshproxy": {} + "vnfproxy": {} + "vnf-user": {} +"is": "vnf-user" diff --git a/nscharm_user_vnf/charms/vnf-user/lib/charms/layer/__init__.py b/nscharm_user_vnf/charms/vnf-user/lib/charms/layer/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..a8e0c640642f44cac85df60f3d9b7f73b0bb18bb --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/lib/charms/layer/__init__.py @@ -0,0 +1,60 @@ +import sys +from importlib import import_module +from pathlib import Path + + +def import_layer_libs(): + """ + Ensure that all layer libraries are imported. + + This makes it possible to do the following: + + from charms import layer + + layer.foo.do_foo_thing() + + Note: This function must be called after bootstrap. + """ + for module_file in Path('lib/charms/layer').glob('*'): + module_name = module_file.stem + if module_name in ('__init__', 'basic', 'execd') or not ( + module_file.suffix == '.py' or module_file.is_dir() + ): + continue + import_module('charms.layer.{}'.format(module_name)) + + +# Terrible hack to support the old terrible interface. +# Try to get people to call layer.options.get() instead so +# that we can remove this garbage. +# Cribbed from https://stackoverfLow.com/a/48100440/4941864 +class OptionsBackwardsCompatibilityHack(sys.modules[__name__].__class__): + def __call__(self, section=None, layer_file=None): + if layer_file is None: + return self.get(section=section) + else: + return self.get(section=section, + layer_file=Path(layer_file)) + + +def patch_options_interface(): + from charms.layer import options + if sys.version_info.minor >= 5: + options.__class__ = OptionsBackwardsCompatibilityHack + else: + # Py 3.4 doesn't support changing the __class__, so we have to do it + # another way. The last line is needed because we already have a + # reference that doesn't get updated with sys.modules. + name = options.__name__ + hack = OptionsBackwardsCompatibilityHack(name) + hack.get = options.get + sys.modules[name] = hack + sys.modules[__name__].options = hack + + +try: + patch_options_interface() +except ImportError: + # This may fail if pyyaml hasn't been installed yet. But in that + # case, the bootstrap logic will try it again once it has. + pass diff --git a/nscharm_user_vnf/charms/vnf-user/lib/charms/layer/basic.py b/nscharm_user_vnf/charms/vnf-user/lib/charms/layer/basic.py new file mode 100644 index 0000000000000000000000000000000000000000..39319591156a512b6ff46a082f51c3c8382055ec --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/lib/charms/layer/basic.py @@ -0,0 +1,286 @@ +import os +import sys +import shutil +from glob import glob +from subprocess import check_call, check_output, CalledProcessError +from time import sleep + +from charms import layer +from charms.layer.execd import execd_preinstall + + +def lsb_release(): + """Return /etc/lsb-release in a dict""" + d = {} + with open('/etc/lsb-release', 'r') as lsb: + for l in lsb: + k, v = l.split('=') + d[k.strip()] = v.strip() + return d + + +def bootstrap_charm_deps(): + """ + Set up the base charm dependencies so that the reactive system can run. + """ + # execd must happen first, before any attempt to install packages or + # access the network, because sites use this hook to do bespoke + # configuration and install secrets so the rest of this bootstrap + # and the charm itself can actually succeed. This call does nothing + # unless the operator has created and populated $JUJU_CHARM_DIR/exec.d. + execd_preinstall() + # ensure that $JUJU_CHARM_DIR/bin is on the path, for helper scripts + charm_dir = os.environ['JUJU_CHARM_DIR'] + os.environ['PATH'] += ':%s' % os.path.join(charm_dir, 'bin') + venv = os.path.abspath('../.venv') + vbin = os.path.join(venv, 'bin') + vpip = os.path.join(vbin, 'pip') + vpy = os.path.join(vbin, 'python') + hook_name = os.path.basename(sys.argv[0]) + is_bootstrapped = os.path.exists('wheelhouse/.bootstrapped') + is_charm_upgrade = hook_name == 'upgrade-charm' + is_series_upgrade = hook_name == 'post-series-upgrade' + post_upgrade = os.path.exists('wheelhouse/.upgrade') + is_upgrade = not post_upgrade and (is_charm_upgrade or is_series_upgrade) + if is_bootstrapped and not is_upgrade: + # older subordinates might have downgraded charm-env, so we should + # restore it if necessary + install_or_update_charm_env() + activate_venv() + # the .upgrade file prevents us from getting stuck in a loop + # when re-execing to activate the venv; at this point, we've + # activated the venv, so it's safe to clear it + if post_upgrade: + os.unlink('wheelhouse/.upgrade') + return + if is_series_upgrade and os.path.exists(venv): + # series upgrade should do a full clear of the venv, rather than just + # updating it, to bring in updates to Python itself + shutil.rmtree(venv) + if is_upgrade: + if os.path.exists('wheelhouse/.bootstrapped'): + os.unlink('wheelhouse/.bootstrapped') + open('wheelhouse/.upgrade', 'w').close() + # bootstrap wheelhouse + if os.path.exists('wheelhouse'): + with open('/root/.pydistutils.cfg', 'w') as fp: + # make sure that easy_install also only uses the wheelhouse + # (see https://github.com/pypa/pip/issues/410) + fp.writelines([ + "[easy_install]\n", + "allow_hosts = ''\n", + "find_links = file://{}/wheelhouse/\n".format(charm_dir), + ]) + apt_install([ + 'python3-pip', + 'python3-setuptools', + 'python3-yaml', + 'python3-dev', + 'python3-wheel', + 'build-essential', + ]) + from charms.layer import options + cfg = options.get('basic') + # include packages defined in layer.yaml + apt_install(cfg.get('packages', [])) + # if we're using a venv, set it up + if cfg.get('use_venv'): + if not os.path.exists(venv): + series = lsb_release()['DISTRIB_CODENAME'] + if series in ('precise', 'trusty'): + apt_install(['python-virtualenv']) + else: + apt_install(['virtualenv']) + cmd = ['virtualenv', '-ppython3', '--never-download', venv] + if cfg.get('include_system_packages'): + cmd.append('--system-site-packages') + check_call(cmd) + os.environ['PATH'] = ':'.join([vbin, os.environ['PATH']]) + pip = vpip + else: + pip = 'pip3' + # save a copy of system pip to prevent `pip3 install -U pip` + # from changing it + if os.path.exists('/usr/bin/pip'): + shutil.copy2('/usr/bin/pip', '/usr/bin/pip.save') + # need newer pip, to fix spurious Double Requirement error: + # https://github.com/pypa/pip/issues/56 + check_call([pip, 'install', '-U', '--no-index', '-f', 'wheelhouse', + 'pip']) + # per https://github.com/juju-solutions/layer-basic/issues/110 + # this replaces the setuptools that was copied over from the system on + # venv create with latest setuptools and adds setuptools_scm + check_call([pip, 'install', '-U', '--no-index', '-f', 'wheelhouse', + 'setuptools', 'setuptools-scm']) + # install the rest of the wheelhouse deps + check_call([pip, 'install', '-U', '--ignore-installed', '--no-index', + '-f', 'wheelhouse'] + glob('wheelhouse/*')) + # re-enable installation from pypi + os.remove('/root/.pydistutils.cfg') + # install python packages from layer options + if cfg.get('python_packages'): + check_call([pip, 'install', '-U'] + cfg.get('python_packages')) + if not cfg.get('use_venv'): + # restore system pip to prevent `pip3 install -U pip` + # from changing it + if os.path.exists('/usr/bin/pip.save'): + shutil.copy2('/usr/bin/pip.save', '/usr/bin/pip') + os.remove('/usr/bin/pip.save') + # setup wrappers to ensure envs are used for scripts + install_or_update_charm_env() + for wrapper in ('charms.reactive', 'charms.reactive.sh', + 'chlp', 'layer_option'): + src = os.path.join('/usr/local/sbin', 'charm-env') + dst = os.path.join('/usr/local/sbin', wrapper) + if not os.path.exists(dst): + os.symlink(src, dst) + if cfg.get('use_venv'): + shutil.copy2('bin/layer_option', vbin) + else: + shutil.copy2('bin/layer_option', '/usr/local/bin/') + # re-link the charm copy to the wrapper in case charms + # call bin/layer_option directly (as was the old pattern) + os.remove('bin/layer_option') + os.symlink('/usr/local/sbin/layer_option', 'bin/layer_option') + # flag us as having already bootstrapped so we don't do it again + open('wheelhouse/.bootstrapped', 'w').close() + # Ensure that the newly bootstrapped libs are available. + # Note: this only seems to be an issue with namespace packages. + # Non-namespace-package libs (e.g., charmhelpers) are available + # without having to reload the interpreter. :/ + reload_interpreter(vpy if cfg.get('use_venv') else sys.argv[0]) + + +def install_or_update_charm_env(): + # On Trusty python3-pkg-resources is not installed + try: + from pkg_resources import parse_version + except ImportError: + apt_install(['python3-pkg-resources']) + from pkg_resources import parse_version + + try: + installed_version = parse_version( + check_output(['/usr/local/sbin/charm-env', + '--version']).decode('utf8')) + except (CalledProcessError, FileNotFoundError): + installed_version = parse_version('0.0.0') + try: + bundled_version = parse_version( + check_output(['bin/charm-env', + '--version']).decode('utf8')) + except (CalledProcessError, FileNotFoundError): + bundled_version = parse_version('0.0.0') + if installed_version < bundled_version: + shutil.copy2('bin/charm-env', '/usr/local/sbin/') + + +def activate_venv(): + """ + Activate the venv if enabled in ``layer.yaml``. + + This is handled automatically for normal hooks, but actions might + need to invoke this manually, using something like: + + # Load modules from $JUJU_CHARM_DIR/lib + import sys + sys.path.append('lib') + + from charms.layer.basic import activate_venv + activate_venv() + + This will ensure that modules installed in the charm's + virtual environment are available to the action. + """ + from charms.layer import options + venv = os.path.abspath('../.venv') + vbin = os.path.join(venv, 'bin') + vpy = os.path.join(vbin, 'python') + use_venv = options.get('basic', 'use_venv') + if use_venv and '.venv' not in sys.executable: + # activate the venv + os.environ['PATH'] = ':'.join([vbin, os.environ['PATH']]) + reload_interpreter(vpy) + layer.patch_options_interface() + layer.import_layer_libs() + + +def reload_interpreter(python): + """ + Reload the python interpreter to ensure that all deps are available. + + Newly installed modules in namespace packages sometimes seemt to + not be picked up by Python 3. + """ + os.execve(python, [python] + list(sys.argv), os.environ) + + +def apt_install(packages): + """ + Install apt packages. + + This ensures a consistent set of options that are often missed but + should really be set. + """ + if isinstance(packages, (str, bytes)): + packages = [packages] + + env = os.environ.copy() + + if 'DEBIAN_FRONTEND' not in env: + env['DEBIAN_FRONTEND'] = 'noninteractive' + + cmd = ['apt-get', + '--option=Dpkg::Options::=--force-confold', + '--assume-yes', + 'install'] + for attempt in range(3): + try: + check_call(cmd + packages, env=env) + except CalledProcessError: + if attempt == 2: # third attempt + raise + try: + # sometimes apt-get update needs to be run + check_call(['apt-get', 'update']) + except CalledProcessError: + # sometimes it's a dpkg lock issue + pass + sleep(5) + else: + break + + +def init_config_states(): + import yaml + from charmhelpers.core import hookenv + from charms.reactive import set_state + from charms.reactive import toggle_state + config = hookenv.config() + config_defaults = {} + config_defs = {} + config_yaml = os.path.join(hookenv.charm_dir(), 'config.yaml') + if os.path.exists(config_yaml): + with open(config_yaml) as fp: + config_defs = yaml.safe_load(fp).get('options', {}) + config_defaults = {key: value.get('default') + for key, value in config_defs.items()} + for opt in config_defs.keys(): + if config.changed(opt): + set_state('config.changed') + set_state('config.changed.{}'.format(opt)) + toggle_state('config.set.{}'.format(opt), config.get(opt)) + toggle_state('config.default.{}'.format(opt), + config.get(opt) == config_defaults[opt]) + + +def clear_config_states(): + from charmhelpers.core import hookenv, unitdata + from charms.reactive import remove_state + config = hookenv.config() + remove_state('config.changed') + for opt in config.keys(): + remove_state('config.changed.{}'.format(opt)) + remove_state('config.set.{}'.format(opt)) + remove_state('config.default.{}'.format(opt)) + unitdata.kv().flush() diff --git a/nscharm_user_vnf/charms/vnf-user/lib/charms/layer/execd.py b/nscharm_user_vnf/charms/vnf-user/lib/charms/layer/execd.py new file mode 100644 index 0000000000000000000000000000000000000000..438d9a1bc90042fd8b20517d5ecf358dfbf08afc --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/lib/charms/layer/execd.py @@ -0,0 +1,114 @@ +# Copyright 2014-2016 Canonical Limited. +# +# This file is part of layer-basic, the reactive base layer for Juju. +# +# charm-helpers is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 as +# published by the Free Software Foundation. +# +# charm-helpers is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with charm-helpers. If not, see . + +# This module may only import from the Python standard library. +import os +import sys +import subprocess +import time + +''' +execd/preinstall + +Read the layer-basic docs for more info on how to use this feature. +https://charmsreactive.readthedocs.io/en/latest/layer-basic.html#exec-d-support +''' + + +def default_execd_dir(): + return os.path.join(os.environ['JUJU_CHARM_DIR'], 'exec.d') + + +def execd_module_paths(execd_dir=None): + """Generate a list of full paths to modules within execd_dir.""" + if not execd_dir: + execd_dir = default_execd_dir() + + if not os.path.exists(execd_dir): + return + + for subpath in os.listdir(execd_dir): + module = os.path.join(execd_dir, subpath) + if os.path.isdir(module): + yield module + + +def execd_submodule_paths(command, execd_dir=None): + """Generate a list of full paths to the specified command within exec_dir. + """ + for module_path in execd_module_paths(execd_dir): + path = os.path.join(module_path, command) + if os.access(path, os.X_OK) and os.path.isfile(path): + yield path + + +def execd_sentinel_path(submodule_path): + module_path = os.path.dirname(submodule_path) + execd_path = os.path.dirname(module_path) + module_name = os.path.basename(module_path) + submodule_name = os.path.basename(submodule_path) + return os.path.join(execd_path, + '.{}_{}.done'.format(module_name, submodule_name)) + + +def execd_run(command, execd_dir=None, stop_on_error=True, stderr=None): + """Run command for each module within execd_dir which defines it.""" + if stderr is None: + stderr = sys.stdout + for submodule_path in execd_submodule_paths(command, execd_dir): + # Only run each execd once. We cannot simply run them in the + # install hook, as potentially storage hooks are run before that. + # We cannot rely on them being idempotent. + sentinel = execd_sentinel_path(submodule_path) + if os.path.exists(sentinel): + continue + + try: + subprocess.check_call([submodule_path], stderr=stderr, + universal_newlines=True) + with open(sentinel, 'w') as f: + f.write('{} ran successfully {}\n'.format(submodule_path, + time.ctime())) + f.write('Removing this file will cause it to be run again\n') + except subprocess.CalledProcessError as e: + # Logs get the details. We can't use juju-log, as the + # output may be substantial and exceed command line + # length limits. + print("ERROR ({}) running {}".format(e.returncode, e.cmd), + file=stderr) + print("STDOUT< 0: + raise CalledProcessError(returncode=retcode, + cmd=cmd, + output=stderr.decode("utf-8").strip()) + return (stdout.decode('utf-8').strip(), stderr.decode('utf-8').strip()) + + +def _run(cmd, env=None): + """Run a command remotely via SSH. + + Note: The previous behavior was to run the command locally if SSH wasn't + configured, but that can lead to cases where execution succeeds when you'd + expect it not to. + """ + if isinstance(cmd, str): + cmd = shlex.split(cmd) + + if type(cmd) is not list: + cmd = [cmd] + + cfg = get_config() + + if cfg: + if all(k in cfg for k in ['ssh-hostname', 'ssh-username', + 'ssh-password', 'ssh-private-key']): + host = get_host_ip() + user = cfg['ssh-username'] + passwd = cfg['ssh-password'] + key = cfg['ssh-private-key'] # DEPRECATED + + if host and user: + return ssh(cmd, host, user, passwd, key) + + raise Exception("Invalid SSH credentials.") + + +def get_ssh_client(host, user, password=None, key=None): + """Return a connected Paramiko ssh object.""" + client = paramiko.SSHClient() + client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + + pkey = None + + # Check for the DEPRECATED private-key + if key: + f = io.StringIO(key) + pkey = paramiko.RSAKey.from_private_key(f) + else: + # Otherwise, check for the auto-generated private key + if os.path.exists('/root/.ssh/id_juju_sshproxy'): + with open('/root/.ssh/id_juju_sshproxy', 'r') as f: + pkey = paramiko.RSAKey.from_private_key(f) + + ########################################################################### + # There is a bug in some versions of OpenSSH 4.3 (CentOS/RHEL 5) where # + # the server may not send the SSH_MSG_USERAUTH_BANNER message except when # + # responding to an auth_none request. For example, paramiko will attempt # + # to use password authentication when a password is set, but the server # + # could deny that, instead requesting keyboard-interactive. The hack to # + # workaround this is to attempt a reconnect, which will receive the right # + # banner, and authentication can proceed. See the following for more info # + # https://github.com/paramiko/paramiko/issues/432 # + # https://github.com/paramiko/paramiko/pull/438 # + ########################################################################### + + try: + client.connect(host, port=22, username=user, + password=password, pkey=pkey) + except paramiko.ssh_exception.SSHException as e: + if 'Error reading SSH protocol banner' == str(e): + # Once more, with feeling + client.connect(host, port=22, username=user, + password=password, pkey=pkey) + else: + # Reraise the original exception + raise e + + return client + + +def sftp(local_file, remote_file, host, user, password=None, key=None): + """Copy a local file to a remote host.""" + client = get_ssh_client(host, user, password, key) + + # Create an sftp connection from the underlying transport + sftp = paramiko.SFTPClient.from_transport(client.get_transport()) + sftp.put(local_file, remote_file) + client.close() + + +def ssh(cmd, host, user, password=None, key=None): + """Run an arbitrary command over SSH.""" + client = get_ssh_client(host, user, password, key) + + cmds = ' '.join(cmd) + stdin, stdout, stderr = client.exec_command(cmds, get_pty=True) + retcode = stdout.channel.recv_exit_status() + client.close() # @TODO re-use connections + if retcode > 0: + output = stderr.read().strip() + raise CalledProcessError(returncode=retcode, cmd=cmd, + output=output) + return ( + stdout.read().decode('utf-8').strip(), + stderr.read().decode('utf-8').strip() + ) diff --git a/nscharm_user_vnf/charms/vnf-user/metadata.yaml b/nscharm_user_vnf/charms/vnf-user/metadata.yaml new file mode 100644 index 0000000000000000000000000000000000000000..db361c49be8212560c0e0ffec0c07efa50326424 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/metadata.yaml @@ -0,0 +1,16 @@ +"name": "vnf-user" +"summary": "" +"maintainer": "Adam Israel " +"description": | + +"tags": + # Replace "misc" with one or more whitelisted tags from this list: + # https://jujucharms.com/docs/stable/authors-charm-metadata +- "misc" +- "osm" +- "vnf" +"series": +- "xenial" +- "bionic" +- "trusty" +"subordinate": !!bool "false" diff --git a/nscharm_user_vnf/charms/vnf-user/reactive/__init__.py b/nscharm_user_vnf/charms/vnf-user/reactive/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/nscharm_user_vnf/charms/vnf-user/reactive/sshproxy.py b/nscharm_user_vnf/charms/vnf-user/reactive/sshproxy.py new file mode 100644 index 0000000000000000000000000000000000000000..5ef0f56f09612a9a775a1bc3037685cc1f27755e --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/reactive/sshproxy.py @@ -0,0 +1,210 @@ +## +# Copyright 2016 Canonical Ltd. +# All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +## + +from charmhelpers.core import unitdata +from charmhelpers.core.hookenv import ( + action_fail, + action_get, + action_set, + config, + log, + status_set, + DEBUG, +) + +from charms.reactive.flags import register_trigger + +from charms.reactive import ( + clear_flag, + set_flag, + when, + when_not, + when_any, +) +import charms.sshproxy +import os +import subprocess + +# Register a trigger so that we can respond to config.changed, even if +# it's being cleared by another handler +register_trigger(when='config.changed', + set_flag='sshproxy.reconfigure') + + +# @when_any('config.changed', 'sshproxy.reconfigure') +@when_any('config.set.ssh-hostname', 'config.set.ssh-username', 'config.set.ssh-password', 'sshproxy.reconfigure') +def ssh_configured(): + """Check if charm is properly configured. + + Check to see if the charm is configured with SSH credentials. If so, + set a state flag that can be used to execute ssh-only actions. + + For example: + + @when('sshproxy.configured') + def run_remote_command(cmd): + ... + + @when_not('sshproxy.configured') + def run_local_command(cmd): + ... + """ + log("Checking sshproxy configuration", DEBUG) + cfg = config() + ssh_keys = ['ssh-hostname', 'ssh-username', + 'ssh-password', 'ssh-private-key'] + + if all(k in cfg for k in ssh_keys): + + # Store config in unitdata so it's accessible to sshproxy + db = unitdata.kv() + db.set('config', cfg) + + # Explicitly flush the kv so it's immediately available + db.flush() + + log("Verifying ssh credentials...", DEBUG) + (verified, output) = charms.sshproxy.verify_ssh_credentials() + if verified: + log("SSH credentials verified.", DEBUG) + set_flag('sshproxy.configured') + status_set('active', 'Ready!') + else: + clear_flag('sshproxy.configured') + status_set('blocked', "Remote machine not ready yet: {}".format(output)) + else: + log("No ssh credentials configured", DEBUG) + clear_flag('sshproxy.configured') + status_set('blocked', 'Invalid SSH credentials.') + + +def generate_ssh_key(): + """Generate a new 4096-bit rsa keypair. + + If there is an existing keypair for this unit, it will be overwritten. + """ + cfg = config() + if all(k in cfg for k in ['ssh-key-type', 'ssh-key-bits']): + keytype = cfg['ssh-key-type'] + bits = str(cfg['ssh-key-bits']) + privatekey = '/root/.ssh/id_juju_sshproxy' + publickey = "{}.pub".format(privatekey) + + if os.path.exists(privatekey): + os.remove(privatekey) + if os.path.exists(publickey): + os.remove(publickey) + + cmd = "ssh-keygen -t {} -b {} -N '' -f {}".format( + keytype, + bits, + privatekey + ) + + output, err = charms.sshproxy.run_local([cmd]) + if len(err) == 0: + return True + return False + + +@when('actions.generate-ssh-key') +def action_generate_ssh_key(): + """Generate a new 4096-bit rsa keypair. + + If there is an existing keypair for this unit, it will be overwritten. + """ + try: + if not generate_ssh_key(): + action_fail('Unable to generate ssh key.') + except subprocess.CalledProcessError as e: + action_fail('Command failed: %s (%s)' % + (' '.join(e.cmd), str(e.output))) + finally: + clear_flag('actions.generate-ssh-key') + + +def get_ssh_public_key(): + """Get the public SSH key of this unit.""" + publickey_path = '/root/.ssh/id_juju_sshproxy.pub' + publickey = None + if os.path.exists(publickey_path): + with open(publickey_path, 'r') as f: + publickey = f.read() + + return publickey + + +@when('actions.get-ssh-public-key') +def action_get_ssh_public_key(): + """Get the public SSH key of this unit.""" + try: + action_set({'pubkey': get_ssh_public_key()}) + except subprocess.CalledProcessError as e: + action_fail('Command failed: %s (%s)' % + (' '.join(e.cmd), str(e.output))) + finally: + clear_flag('actions.get-ssh-public-key') + + +@when('actions.verify-ssh-credentials') +def action_verify_ssh_credentials(): + """Verify the ssh credentials have been installed to the VNF. + + Attempts to run a stock command - `hostname` on the remote host. + """ + try: + (verified, output) = charms.sshproxy.verify_ssh_credentials() + action_set({ + 'output': output, + 'verified': verified, + }) + if not verified: + action_fail("Verification failed: {}".format( + output, + )) + finally: + clear_flag('actions.verify-ssh-credentials') + + +@when('actions.run') +def run_command(): + """Run an arbitrary command. + + Run an arbitrary command, either locally or over SSH with the configured + credentials. + """ + try: + cmd = action_get('command') + output, err = charms.sshproxy._run(cmd) + if len(err): + action_fail("Command '{}' returned error code {}".format(cmd, err)) + else: + action_set({'output': output}) + except subprocess.CalledProcessError as e: + action_fail('Command failed: %s (%s)' % + (' '.join(e.cmd), str(e.output))) + finally: + clear_flag('actions.run') + + +@when_not('sshproxy.installed') +def install_vnf_ubuntu_proxy(): + """Install and Configure SSH Proxy.""" + + log("Generating SSH key...", DEBUG) + generate_ssh_key() + set_flag('sshproxy.installed') diff --git a/nscharm_user_vnf/charms/vnf-user/reactive/vnf_user.py b/nscharm_user_vnf/charms/vnf-user/reactive/vnf_user.py new file mode 100644 index 0000000000000000000000000000000000000000..10e01741cefecd03985909ad13b5e0cd22aa47f3 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/reactive/vnf_user.py @@ -0,0 +1,51 @@ +from charmhelpers.core.hookenv import ( + action_get, + action_fail, + action_set, + status_set, + log, +) + +from charms.reactive import ( + clear_flag, + set_flag, + when, + when_not, +) +import random + + +@when('sshproxy.configured') +@when_not('vnf-user.installed') +def install_vnf_user(): + set_flag('vnf-a.installed') + status_set('active', 'Ready!') + + +@when('actions.add-user') +def action_add_user(): + """Run the touch command. + + Runs touch on vnf-a and vnf-b and returns the success or failure for each. + """ + err = '' + user_id = 0 + try: + username = action_get('username') + tariff = action_get('tariff') + + # If this were a functional VNF, it would add the username to its + # database. For the purposes of demonstrating actions, this will return + # a random number. + user_id = random.randint(1, 100) + + except Exception as err: + # This marks the action as having failed, so the ns charm knows not to + # continue with the add_user operation. + action_fail(str(err)) + else: + # This will put the user_id in the action output, which will be read + # by the ns charm. Multiple values could be returned here. + action_set({'user-id': user_id}) + finally: + clear_flag('actions.add-user') diff --git a/nscharm_user_vnf/charms/vnf-user/reactive/vnfproxy.py b/nscharm_user_vnf/charms/vnf-user/reactive/vnfproxy.py new file mode 100644 index 0000000000000000000000000000000000000000..6616991df245a920116b9301b52602f15ba7a9bc --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/reactive/vnfproxy.py @@ -0,0 +1,89 @@ +from charmhelpers.core.hookenv import ( + action_fail, + action_set, +) + +from charms.reactive import ( + when, + clear_flag, +) +import charms.sshproxy + + +@when('actions.reboot') +def reboot(): + err = '' + try: + result, err = charms.sshproxy._run("reboot") + except: + action_fail('command failed:' + err) + else: + action_set({'outout': result}) + finally: + clear_flag('actions.reboot') + + +############################################################################### +# Below is an example implementation of the start/stop/restart actions. # +# To use this, copy the below code into your layer and add the appropriate # +# command(s) necessary to perform the action. # +############################################################################### + +# @when('actions.start') +# def start(): +# err = '' +# try: +# cmd = "service myname start" +# result, err = charms.sshproxy._run(cmd) +# except: +# action_fail('command failed:' + err) +# else: +# action_set({'outout': result}) +# finally: +# clear_flag('actions.start') +# +# +# @when('actions.stop') +# def stop(): +# err = '' +# try: +# # Enter the command to stop your service(s) +# cmd = "service myname stop" +# result, err = charms.sshproxy._run(cmd) +# except: +# action_fail('command failed:' + err) +# else: +# action_set({'outout': result}) +# finally: +# clear_flag('actions.stop') +# +# +# @when('actions.restart') +# def restart(): +# err = '' +# try: +# # Enter the command to restart your service(s) +# cmd = "service myname restart" +# result, err = charms.sshproxy._run(cmd) +# except: +# action_fail('command failed:' + err) +# else: +# action_set({'outout': result}) +# finally: +# clear_flag('actions.restart') +# +# +# @when('actions.upgrade') +# def upgrade_vnf(): +# err = '' +# try: +# # Add the command(s) to perform a VNF software upgrade +# cmd = '' +# result, err = charms.sshproxy._run(cmd) +# except: +# action_fail('command failed:' + err) +# else: +# action_set({'outout': result}) +# finally: +# clear_flag('actions.upgrade') +# diff --git a/nscharm_user_vnf/charms/vnf-user/requirements.txt b/nscharm_user_vnf/charms/vnf-user/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..28ecacab6029381bd43d65f8dcfc0cc704870f71 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/requirements.txt @@ -0,0 +1,2 @@ +flake8 +pytest diff --git a/nscharm_user_vnf/charms/vnf-user/tests/00-setup b/nscharm_user_vnf/charms/vnf-user/tests/00-setup new file mode 100755 index 0000000000000000000000000000000000000000..f0616a560a811bae78d7872dd433d612909c73cd --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/tests/00-setup @@ -0,0 +1,5 @@ +#!/bin/bash + +sudo add-apt-repository ppa:juju/stable -y +sudo apt-get update +sudo apt-get install amulet python-requests -y diff --git a/nscharm_user_vnf/charms/vnf-user/tests/10-deploy b/nscharm_user_vnf/charms/vnf-user/tests/10-deploy new file mode 100755 index 0000000000000000000000000000000000000000..5012ce68890027e16e72572bbda164fa69fd1023 --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/tests/10-deploy @@ -0,0 +1,35 @@ +#!/usr/bin/python3 + +import amulet +import requests +import unittest + + +class TestCharm(unittest.TestCase): + def setUp(self): + self.d = amulet.Deployment() + + self.d.add('vnf-a') + self.d.expose('vnf-a') + + self.d.setup(timeout=900) + self.d.sentry.wait() + + self.unit = self.d.sentry['vnf-a'][0] + + def test_service(self): + # test we can access over http + page = requests.get('http://{}'.format(self.unit.info['public-address'])) + self.assertEqual(page.status_code, 200) + # Now you can use self.d.sentry[SERVICE][UNIT] to address each of the units and perform + # more in-depth steps. Each self.d.sentry[SERVICE][UNIT] has the following methods: + # - .info - An array of the information of that unit from Juju + # - .file(PATH) - Get the details of a file on that unit + # - .file_contents(PATH) - Get plain text output of PATH file from that unit + # - .directory(PATH) - Get details of directory + # - .directory_contents(PATH) - List files and folders in PATH on that unit + # - .relation(relation, service:rel) - Get relation data from return service + + +if __name__ == '__main__': + unittest.main() diff --git a/nscharm_user_vnf/charms/vnf-user/version b/nscharm_user_vnf/charms/vnf-user/version new file mode 100644 index 0000000000000000000000000000000000000000..9fac92dcdf62023fb1920ef612b093cd9d1d000f --- /dev/null +++ b/nscharm_user_vnf/charms/vnf-user/version @@ -0,0 +1 @@ +8c2332f \ No newline at end of file diff --git a/nscharm_user_vnf/charms/vnf-user/wheelhouse/setuptools-41.6.0.zip b/nscharm_user_vnf/charms/vnf-user/wheelhouse/setuptools-41.6.0.zip new file mode 100644 index 0000000000000000000000000000000000000000..3345759c1ca861c181742e84fc5285e3284a2886 Binary files /dev/null and b/nscharm_user_vnf/charms/vnf-user/wheelhouse/setuptools-41.6.0.zip differ diff --git a/nscharm_user_vnf/cloud_init/cloud-config.txt b/nscharm_user_vnf/cloud_init/cloud-config.txt new file mode 100755 index 0000000000000000000000000000000000000000..f6c1b9dcf3a0506e7366e58d0318a5df5f2639ce --- /dev/null +++ b/nscharm_user_vnf/cloud_init/cloud-config.txt @@ -0,0 +1,13 @@ +#cloud-config +password: osm4u +chpasswd: { expire: False } +ssh_pwauth: True + +write_files: +- content: | + # My new helloworld file + + owner: root:root + permissions: '0644' + path: /root/helloworld.txt + diff --git a/nscharm_user_vnf/nscharm_user_vnfd.yaml b/nscharm_user_vnf/nscharm_user_vnfd.yaml new file mode 100644 index 0000000000000000000000000000000000000000..93be6a963b3e61368f9858566f24b323b28644e9 --- /dev/null +++ b/nscharm_user_vnf/nscharm_user_vnfd.yaml @@ -0,0 +1,64 @@ +vnfd:vnfd-catalog: + vnfd: + - id: nscharm-user-vnf + name: nscharm-user-vnf + short-name: nscharm-user-vnf + version: '1.0' + description: A VNF consisting of 2 VDUs w/proxy charm + logo: osm.png + connection-point: + - id: vnf-mgmt + name: vnf-mgmt + short-name: vnf-mgmt + type: VPORT + mgmt-interface: + cp: vnf-mgmt + internal-vld: + - id: internal + name: internal + short-name: internal + type: ELAN + internal-connection-point: + - id-ref: userVM-internal + vdu: + - id: userVM + name: userVM + image: ubuntu1604 + count: '1' + vm-flavor: + vcpu-count: '1' + memory-mb: '1024' + storage-gb: '10' + interface: + - name: userVM-eth0 + position: '1' + type: EXTERNAL + virtual-interface: + type: VIRTIO + external-connection-point-ref: vnf-mgmt + - name: userVM-eth1 + position: '2' + type: INTERNAL + virtual-interface: + type: VIRTIO + internal-connection-point-ref: userVM-internal + internal-connection-point: + - id: userVM-internal + name: userVM-internal + short-name: userVM-internal + type: VPORT + cloud-init-file: cloud-config.txt + vdu-configuration: + juju: + charm: vnf-user + proxy: true + initial-config-primitive: + - seq: '1' + name: config + parameter: + - name: ssh-hostname + value: + - name: ssh-username + value: ubuntu + - name: ssh-password + value: osm4u \ No newline at end of file