├── .gitattributes ├── Jenkins └── Pipeline │ └── Examples │ └── Conditional Build │ ├── Jenkinsfile │ └── README.md ├── LICENSE ├── README.md └── bin ├── README.md ├── assignRole.sh ├── compBuilds.sh ├── compDeployments.sh ├── createGlusterfsClusterApp.sh ├── createLocalProject.sh ├── deleteLocalProject.sh ├── dropAndRecreateDatabase.sh ├── exportTemplate.sh ├── formatConfigurationTemplates.sh ├── genBuilds.sh ├── genDepls.sh ├── genParams.sh ├── genPipelineParams.sh ├── genSecrets.sh ├── genTemplateParams.sh ├── generateLocalProjects.sh ├── getDids.inc ├── getDids.sh ├── getPodByName.sh ├── getRouteInfo.sh ├── grantDeploymentPrivileges.sh ├── initOSProjects.sh ├── installCertificate.sh ├── loadData.sh ├── oc-cluster-down.sh ├── oc-cluster-up.sh ├── oc-pull-image.sh ├── oc-push-image.sh ├── oc-rsync.sh ├── ocFunctions.inc ├── overrides.inc ├── processPipelines.sh ├── runInContainer.sh ├── scaleDeployment.sh ├── settings.sh ├── tagProjectImages.sh ├── testConnection ├── transferRoute.sh └── updateRoutes.sh /.gitattributes: -------------------------------------------------------------------------------- 1 | # Set the default behavior, in case people don't have core.autocrlf set. 2 | * text=auto 3 | 4 | # Declare files that will always have LF line endings on checkout. 5 | *.sh text eol=lf 6 | *.md text eol=lf 7 | *.json text eol=lf 8 | *.conf text eol=lf 9 | **/bin/* text eol=lf -------------------------------------------------------------------------------- /Jenkins/Pipeline/Examples/Conditional Build/Jenkinsfile: -------------------------------------------------------------------------------- 1 | // Edit your app's name below 2 | def APP_NAME = 'YourAppNameHere' 3 | 4 | // Edit your environment TAG names below 5 | def TAG_NAMES = ['dev', 'test', 'prod'] 6 | 7 | // Edit your application's context directory here 8 | def CONTEXT_DIRECTORY = 'YourSourceContextDirectoryHere' 9 | 10 | // You shouldn't have to edit these if you're following the conventions 11 | def BUILD_CONFIG = APP_NAME 12 | def IMAGESTREAM_NAME = APP_NAME 13 | 14 | // Determine whether there were any changes the files within the project's context directory. 15 | @NonCPS 16 | boolean triggerBuild(String contextDirectory) { 17 | // Determine if code has changed within the source context directory. 18 | def changeLogSets = currentBuild.changeSets 19 | def filesChangeCnt = 0 20 | for (int i = 0; i < changeLogSets.size(); i++) { 21 | def entries = changeLogSets[i].items 22 | for (int j = 0; j < entries.length; j++) { 23 | def entry = entries[j] 24 | //echo "${entry.commitId} by ${entry.author} on ${new Date(entry.timestamp)}: ${entry.msg}" 25 | def files = new ArrayList(entry.affectedFiles) 26 | for (int k = 0; k < files.size(); k++) { 27 | def file = files[k] 28 | def filePath = file.path 29 | //echo ">> ${file.path}" 30 | if (filePath.contains(contextDirectory)) { 31 | filesChangeCnt = 1 32 | k = files.size() 33 | j = entries.length 34 | } 35 | } 36 | } 37 | } 38 | 39 | if ( filesChangeCnt < 1 ) { 40 | echo('The changes do not require a build.') 41 | return false 42 | } 43 | else { 44 | echo('The changes require a build.') 45 | return true 46 | } 47 | } 48 | 49 | node { 50 | if( triggerBuild(CONTEXT_DIRECTORY) ) { 51 | // The changeSets contained changes within the project's context directory. 52 | // Perform a build 53 | stage('build ' + BUILD_CONFIG) { 54 | echo "Building: " + BUILD_CONFIG 55 | openshiftBuild bldCfg: BUILD_CONFIG, showBuildLogs: 'true' 56 | openshiftTag destStream: IMAGESTREAM_NAME, verbose: 'true', destTag: '$BUILD_ID', srcStream: IMAGESTREAM_NAME, srcTag: 'latest' 57 | } 58 | 59 | stage('deploy-' + TAG_NAMES[0]) { 60 | openshiftTag destStream: IMAGESTREAM_NAME, verbose: 'true', destTag: TAG_NAMES[0], srcStream: IMAGESTREAM_NAME, srcTag: '$BUILD_ID' 61 | } 62 | } 63 | else { 64 | // The changeSets did not contain any changes within the project's context directory. 65 | // Clearly indicate there were no changes. 66 | stage('No Changes') { 67 | currentBuild.result = 'SUCCESS' 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /Jenkins/Pipeline/Examples/Conditional Build/README.md: -------------------------------------------------------------------------------- 1 | # Example Jenkins file for performing conditional builds 2 | 3 | This example is a first pass at eliminating unnecessary builds and deployments within the OpenShift/Jenkins pipeline. 4 | 5 | Unfortunately the code contained in a Jenkins file is unable to affect the Pipeline's top level SCM settings to take advantage of the include and exclude filters that would stop the builds from being triggered in the first place. 6 | 7 | This example takes steps at the next available level to short-circuit the process, and avoids triggering builds and deployments when there are no changes in the context directory for the pipeline. A 'build' in the Jenkins sense still gets kicked off, but we avoid re-building and deploying things unnecessarily. 8 | 9 | # Examples 10 | 11 | For an example of how to use this Jenkins file in context, have a look at [TheOrgBook Solr](https://github.com/bcgov/TheOrgBook/tree/master/tob-solr) 12 | 13 | Example of the changes this process makes to an existing pipeline; [Jenkinsfile](https://github.com/WadeBarnes/TheOrgBook/commit/fe580ad430567e87b9d5f2bba1461526108b617b#diff-c425b9c6357af57f2c5f73daa0a9999c) 14 | 15 | # Future Enhancements 16 | 17 | The current script only looks for changes under a single location. It would be beneficial for the logic to allow for full include/exclude patterns. 18 | 19 | # Credits 20 | 21 | Thanks to Angelika Ehlers (agehlers) for diving into the problem and developing the code for the initial `triggerBuild` script. -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2019 Province of British Columbia 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenShift Project Tools 2 | 3 | Welcome to the OpenShift project tools repository! 4 | 5 | Here you will find tools to help you manage, automate, and maintain your OpenShift projects and pipelines. 6 | 7 | The resources found in this repository will (should) be in a constant state of evolution as the community as a whole contributes their experiences using the existing tools and breaks new ground solving issues. 8 | 9 | For this to happen we need your support. Please contribute! Whether that be with your experiences or with the code you used to solve a particular issue. 10 | 11 | To contribute directly, please fork the repo and submit pull requests. 12 | 13 | Thank-you and enjoy. 14 | 15 | # Sections 16 | 17 | ## Jenkins 18 | 19 | Jenkins pipeline related tools, utilities, and examples. 20 | 21 | ## bin 22 | 23 | A set of scripts for managing and maintaining OpenShift projects and configurations. 24 | 25 | Refer to the [OpenShift Scripts](./bin/README.md) for details on how to use the scripts. -------------------------------------------------------------------------------- /bin/README.md: -------------------------------------------------------------------------------- 1 | # OpenShift Scripts 2 | 3 | A set of scripts to help you configure and maintain your local and production OpenShift projects. 4 | 5 | Supports both json and yaml based OpenShift configuration templates. 6 | 7 | ## Environment Setup 8 | 9 | 1. Clone this repository to your local machine. 10 | 1. Install [jq](https://stedolan.github.io/jq/). [jq](https://stedolan.github.io/jq/) is used by some of the scripts to manipulate the configuration files in preparation for update/replace operations. The recommended approach is to use either [Homebrew](https://brew.sh/) (MAC) or [Chocolatey](https://chocolatey.org/) (Windows) to install the required packages. 11 | - Windows: 12 | - `chocolatey install jq` 13 | - MAC: 14 | - `brew install jq` 15 | - CentOS: 16 | - `yum install jq` 17 | - Debian/Ubuntu: 18 | - `apt install jq` 19 | 20 | 1. Update your path to include a reference to the `bin` directory 21 | 22 | Using GIT Bash on Windows as an example; 23 | 1. Create a `.bashrc` file in your home directory (`C:\Users\`, for example `C:\Users\Wade`). 24 | 1. Add the line `PATH=${PATH}:/c/openshift-developer-tools/bin` 25 | 1. Restart GIT Bash. _If you have not done this before, GIT will write out some warnings and create some files for you that fix the issues._ 26 | 27 | All of the scripts will be available on the path and can be run from any directory. This is important as many of the scripts expect to be run from the top level `./openshift` directory you will create in your project. 28 | 29 | ### MAC Setup 30 | 31 | These scripts use `sed` and regular expression processing. The default version of `sed` on MAC does support some of the processing. Details can be found here; [Differences between sed on Mac OSX and other "standard" sed?](https://unix.stackexchange.com/questions/13711/differences-between-sed-on-mac-osx-and-other-standard-sed) 32 | 33 | Update your path to have this repo's bin folder in it. You may need to alter the paths in the command below to reflect wherever you cloned your fork to. Append this line to your `~\.bashrc` file: 34 | 35 | ``` 36 | [[ ":$PATH:" != *"/openshift-developer-tools/bin:"* ]] && export PATH="~/openshift-developer-tools/bin:$PATH" 37 | ``` 38 | 39 | Please install `gnu-sed`. 40 | 41 | Using [Homebrew](https://brew.sh): 42 | 43 | ``` 44 | brew install gnu-sed 45 | ``` 46 | 47 | Then update your path and prepend `/usr/local/opt/gnu-sed/libexec/gnubin:` to your existing path so that the system defaults to using `sed` rather than `gsed`. Append this line to your `~\.bashrc` file: 48 | 49 | ``` 50 | [[ ":$PATH:" != *"/usr/local/opt/gnu-sed/libexec/gnubin:"* ]] && export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH" 51 | ``` 52 | 53 | Similarly, you must install GNU find: 54 | 55 | ``` 56 | brew install findutils 57 | ``` 58 | 59 | Then update your path and prepend `/usr/local/opt/findutils/libexec/gnubin:` to your existing path so that the system defaults to using `find` rather than `gfind`. Append this line to your `~\.bashrc` file: 60 | 61 | ``` 62 | [[ ":$PATH:" != *"/usr/local/opt/findutils/libexec/gnubin:"* ]] && export PATH="/usr/local/opt/findutils/libexec/gnubin:$PATH" 63 | ``` 64 | 65 | 66 | Also make sure `usr/local/bin` is at a higher priority on your **PATH** than `usr/bin`. You can do this by making sure `usr/local/bin` is to the left of `usr/bin`, preceding it in the **PATH** string. This will ensure that packages installed by Homebrew override system binaries; in this case `sed`. Append this line to your `~\.bashrc` file: 67 | 68 | ``` 69 | [[ ":$PATH:" != *"/usr/local/bin:"* ]] && export PATH="/usr/local/bin:$PATH" 70 | ``` 71 | 72 | 73 | `brew doctor` can help diagnose such issues. 74 | 75 | 76 | ### Linux Setup 77 | 78 | These scripts use `awk`, but problems may be encountered if `mawk` is used rather than the GNU awk `gawk`. `mawk` is used by default in Ubuntu 20.04 and Kali Linux for the Linux Subsystem for Windows. 79 | 80 | ``` 81 | $ awk -W version 82 | mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan 83 | ``` 84 | 85 | For Ubuntu and Kali Linux, installing `gawk` will make it the default implementation of `awk`: 86 | 87 | ``` 88 | $ sudo apt-get install gawk 89 | [... etc ...] 90 | 91 | $ awk -W version 92 | GNU Awk 4.2.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2) 93 | ``` 94 | 95 | ## Project Structure 96 | 97 | To use these scripts your project structure should be organized in one of two ways; split out by component, or simplified. The one you choose should be based on the complexity of your project and your personal development preferences. 98 | 99 | Regardless of which you choose, you will always have a top level `./openshift` directory in your project where you keep your main project settings (`settings.sh`) file. 100 | 101 | ### Component Project Structure 102 | 103 | [TheOrgBook](https://github.com/bcgov/TheOrgBook) and [Family-Protection-Order](https://github.com/bcgov/Family-Protection-Order) are examples of the Component Project Structure. 104 | 105 | In general terms the structure looks like this, where the code and the openshift templates for the components are separated out into logical bits. 106 | 107 | RootProjectDir 108 | - openshift 109 | - component1 110 | - openshift 111 | - templates 112 | - component2 113 | - openshift 114 | - templates 115 | 116 | ### Simple Project Structure 117 | 118 | [permitify](https://github.com/bcgov/permitify) is an example of the Simple Project Structure. 119 | 120 | In general terms the structure looks like this, where all of the openshift templates for the components are grouped together in a central location. 121 | 122 | RootProjectDir 123 | - openshift 124 | - templates 125 | 126 | ## Settings.sh 127 | 128 | You will need to include a `settings.sh` file in your top level `./openshift` directory that contains your project specific settings. 129 | 130 | At a minimum this file should contain definitions for your `PROJECT_NAMESPACE`, `GIT_URI`, and `GIT_REF` all of which should be setup to be overridable. 131 | 132 | **For Example:** 133 | ``` 134 | export PROJECT_NAMESPACE=${PROJECT_NAMESPACE:-devex-von-permitify} 135 | export GIT_URI=${GIT_URI:-"https://github.com/bcgov/permitify.git"} 136 | export GIT_REF=${GIT_REF:-"master"} 137 | ``` 138 | 139 | **Full Simple Project Structure Example:** 140 | ``` 141 | export PROJECT_NAMESPACE=${PROJECT_NAMESPACE:-devex-von-permitify} 142 | export GIT_URI=${GIT_URI:-"https://github.com/bcgov/permitify.git"} 143 | export GIT_REF=${GIT_REF:-"master"} 144 | 145 | # The project components 146 | # - They are all contained under the main OpenShift folder. 147 | export components="." 148 | 149 | # The builds to be triggered after buildconfigs created (not auto-triggered) 150 | export builds="" 151 | 152 | # The images to be tagged after build 153 | export images="permitify" 154 | 155 | # The routes for the project 156 | export routes="bc-registries worksafe-bc" 157 | ``` 158 | 159 | **Full Component Project Structure Example:** 160 | ``` 161 | export PROJECT_NAMESPACE="devex-von" 162 | 163 | export GIT_URI="https://github.com/bcgov/TheOrgBook.git" 164 | export GIT_REF="master" 165 | 166 | # The templates that should not have their GIT references (uri and ref) over-ridden 167 | # Templates NOT in this list will have they GIT references over-ridden 168 | # with the values of GIT_URI and GIT_REF 169 | export -a skip_git_overrides="schema-spy-build.json solr-base-build.json" 170 | 171 | # The project components 172 | export components="tob-db tob-solr tob-api tob-web" 173 | 174 | # The builds to be triggered after buildconfigs created (not auto-triggered) 175 | export builds="nginx-runtime angular-builder" 176 | 177 | # The images to be tagged after build 178 | export images="angular-on-nginx django solr schema-spy" 179 | 180 | # The routes for the project 181 | export routes="angular-on-nginx django solr schema-spy" 182 | ``` 183 | 184 | ## Settings.local.sh 185 | 186 | You can also have a `settings.local.sh` file in your top level `./openshift` directory that contains any overrides necessary for deploying your project into a local OpenShift environment. 187 | 188 | Typically this will simply contain overrides for the `GIT_URI` and `GIT_REF`, for example: 189 | ``` 190 | export GIT_URI="https://github.com/WadeBarnes/permitify.git" 191 | export GIT_REF="openshift" 192 | ``` 193 | 194 | These overrides come into play when you are generating local param files, and deploying into a local OpenShift environment. 195 | 196 | ## Setting Profiles 197 | 198 | The scripts support setting profiles, which allow you to further manage your settings for different environments or occasions. Unlike local settings (`settings.local.sh`), settings profiles are something you want to check into your repository. 199 | 200 | Settings profiles are loaded between the default settings (`settings.sh`) and the local settings (`settings.local.sh`), allowing you to apply your local settings to your profiles just as you would your default settings. 201 | 202 | Settings profiles work exactly like the other settings files. To define a settings profile you simply have to create a file with the profile name; `settings..sh`. The scripts will automatically detect the profiles and prompt you to either use them or ignore them. 203 | 204 | Scripts that support profiles will expose a `-p ` (lower case p) flag to allow you to load a named profile setting, and a `-P` (upper case P) flag it allow you to use you default settings. 205 | 206 | Refer to the help (`-h`) output of the scripts for more details. 207 | 208 | ## Using the Scripts 209 | 210 | When using the scripts run them from the command line in the top level `./openshift` directory of your project. 211 | 212 | Most, if not all, scripts contain usage information. Run the script with `-h` to see it. 213 | 214 | You will need to install the OC CLI. Get a recent stable (or the latest) [Openshift Command Line tool](https://github.com/openshift/origin/releases) (oc) and install it by extracting the "oc" executable and placing it somewhere on your path. You can also install it with several different package managers. 215 | 216 | ### Starting/Stopping a Local OpenShift Cluster 217 | 218 | Use the `oc-cluster-up.sh` script to start a local OpenShift cluster, and `oc-cluster-down.sh` to stop the cluster. 219 | 220 | ### Creating a Project Set on Your Local Cluster 221 | 222 | If you are resetting your environment run the following script. Give this operation a bit of time to complete before recreating the projects. 223 | 224 | ``` 225 | generateLocalProjects.sh -D 226 | ``` 227 | 228 | Run the following command to create the projects for the local instance. Test and Prod will not likely be used, but are referenced in some of the later scripts: 229 | 230 | ``` 231 | generateLocalProjects.sh 232 | ``` 233 | 234 | ### Initialize the projects - add permissions and storage 235 | 236 | For all of the commands mentioned here, you can use the "-h" parameter for usage help and options. 237 | 238 | ``` 239 | initOSProjects.sh 240 | ``` 241 | 242 | If you are running locally you will see some "No resources found." messages which can be ignored. 243 | 244 | ### Generating Parameter Files 245 | 246 | You will need to have your OpenShift build and deployment templates in place, along with your Jenkinsfiles defining your pipelines in order to generate the parameter files needed to generate your builds and configurations in OpenShift. For examples, have a look at the projects referenced in the Project Structure section. 247 | 248 | Once your templates and Jenkinsfiles are in place run `genParams.sh` from within your top level `./openshift` directory. 249 | 250 | Edit these files as needed for your project. 251 | 252 | #### Generate Local Param Files 253 | 254 | Run the following script to generate a series of files with the extension ".local.param" in the "openshift" folder in the root of the repository: 255 | 256 | ``` 257 | genParams.sh -l 258 | ``` 259 | 260 | The files have all the parameters from the various templates in the project, with all of the parameters initially set to be commented out. 261 | 262 | Edit these files as needed for your project. 263 | 264 | ### Generate the Build and Images in the "tools" project; Deploy Jenkins 265 | 266 | On the command line, change into the "openshift" folder in the root of your repo and run the script: 267 | 268 | ``` 269 | genBuilds.sh -h 270 | ``` 271 | 272 | Review the command line parameters and pass in the appropriate parameters - without the -h. For an initial install, no parameters are needed. 273 | 274 | #### Updating Build and Image Configurations 275 | 276 | If you are adding build and image configurations you can re-run this script. You will encounter errors for any of the resources that already exist, but you can safely ignore these areas and allow the script to continue. 277 | 278 | If you are updating build and image configurations use the `-u` option. 279 | 280 | If you are adding and updating build and image configurations, run the script **without** the `-u` option first to create the new resources and then again **with** the `-u` option to update the existing configurations. 281 | 282 | ## Generate the Deployment Configurations and Deploy the Components 283 | 284 | On the command line, change into the "openshift" folder in the root of your repo and run the script: 285 | 286 | ``` 287 | genDepls.sh -h 288 | ``` 289 | 290 | Review the command line parameters available and rerun with the appropriate parameters - without the -h. For an initial deploy, no parameters are needed. 291 | 292 | #### Updating Deployment Configurations 293 | 294 | If you are adding deployment configurations you can re-run this script. You will encounter errors for any of the resources that already exist, but you can safely ignore these areas and allow the script to continue. 295 | 296 | If you are updating deployment configurations use the `-u` option. 297 | 298 | If you are adding and updating deployment configurations, run the script **without** the `-u` option first to create the new resources and then again **with** the `-u` option to update the existing configurations. 299 | 300 | **_Note;_** 301 | 302 | **_Some settings on some resources are immutable. You will need to delete and recreate the associated resource(s). Care must be taken with resources containing credentials or other auto-generated resources, however. You must insure such resources are replaced using the same values._** 303 | 304 | **_Updating the deployment configurations can affect (overwrite) auto-generated secrets such as the database username and password._** 305 | 306 | ## Fixing routes - for local instances 307 | 308 | In the current instance of the deployment, the routes created are explicitly defined for the Pathfinder (BC Gov) instance of OpenShift. Run the script to create the default routes for your local environment: 309 | 310 | ``` 311 | updateRoutes.sh 312 | ``` 313 | 314 | # Troubleshooting 315 | 316 | ## Disk Pressure Issue (MAC and Windows) 317 | 318 | If you start seeing builds and deploys failing due to disk pressure issues it's because OpenShift thinks you are running out of disk space and will start evicting pods. 319 | 320 | ### Docker on Windows 321 | 322 | The quick fix is to delete the Moby Linux VM and its associated virtual disk and start again. 323 | 324 | ### MiniShift 325 | 326 | The default settings for minishift create a small VM with very little memory and disk. 327 | 328 | The fix is to run the following commands to create a more suitable environment; 329 | ``` 330 | minishift stop 331 | minishift delete 332 | minishift config set disk-size 60g 333 | minishift config set memory 6GB 334 | minishift start 335 | ``` 336 | 337 | ## OpenShift (Docker on MAC) 338 | 339 | If you run into certificate errors like `x509: certificate signed by unknown authority` when trying to connect to your local OpenShift cluster from the command line, log into the cluster from the command line using the token login from the web console. 340 | 1. Login to the web console. 341 | 1. From the **(?)** drop-down select **Command Line Tools** 342 | 1. Copy the login command from the console. 343 | 1. Paste it onto the command line. 344 | 1. You should to prompted to allow insecure connections. 345 | 1. Select `yes` and continue. 346 | 347 | # Scripts 348 | 349 | Following is a list of the top level scripts. There are additional lower level scripts that are not listed here since they are wrapped into top level scripts. 350 | 351 | Use `-h` to get more detailed usage information on the scripts. 352 | 353 | ## testConnection 354 | 355 | A script for testing whether or not one or more host:port combinations are opened or closed. The script can be used to test connections locally or remotely from within a pod in order to test connectivity from that pod to other services. 356 | 357 | Example testing the connectivity from one pod to other pods: 358 | ``` 359 | $ testConnection -f TestConnections.txt -n devex-von-tools -p angular-on-nginx 360 | 361 | Reading list of hosts and ports from TestConnections.txt ... 362 | 363 | Testing connections from devex-von-tools/angular-on-nginx ... 364 | google.com:80 - Open 365 | angular-on-nginx:8080 - Closed 366 | django:8080 - Open 367 | postgresql:5432 - Closed 368 | weasyprint:5001 - Closed 369 | schema-spy:8080 - Closed 370 | ``` 371 | 372 | Run `testConnection -h` for additional details. 373 | 374 | ## dropAndRecreateDatabase.sh 375 | 376 | A helper script to drop and recreate the application database within a given environment. 377 | 378 | Refer to the usage documentation contained in the script for details. Run the script without parameters to see the documentation. 379 | 380 | _This script could be further enhanced to utilize the environment variables within the running pod to determine various database parameters dynamically. The process will require some fussing around with escaping quotes and such to get things just right._ 381 | 382 | ## genBuilds.sh 383 | 384 | Generate the build configurations for the project. 385 | 386 | ## genDepls.sh 387 | 388 | Generate the deployment configurations for the project. 389 | 390 | ## generateLocalProjects.sh 391 | 392 | Generate a set of OpenShift projects in a local cluster. 393 | 394 | ## genParams.sh 395 | 396 | Generate the parameter files for the OpenShift templates defined in a project. 397 | 398 | ## initOSProjects.sh 399 | 400 | Initializes the permissions and storage services for the OpenShift projects. 401 | 402 | ## oc-cluster-down.sh 403 | 404 | Shutdown your local OpenShift cluster. 405 | 406 | ## oc-cluster-up.sh 407 | 408 | Start your local OpenShift cluster. 409 | 410 | ## oc-pull-image.sh 411 | 412 | Pull an image from an OpenShift project into your local Docker registry. 413 | 414 | ## oc-push-image.sh 415 | 416 | Push an image from your local Docker registry into an OpenShift project. 417 | 418 | ## runInContainer.sh 419 | 420 | This script is a wrapper around `oc exec` that allows you to run commands inside a pod instance based on its general name. 421 | 422 | Refer to the usage documentation contained in the script for details. Run the script without parameters to see the documentation. 423 | 424 | ## scaleDeployment.sh 425 | 426 | A helper script to scale a deployment to a particular number of pods. 427 | 428 | ## tagProjectImages.sh 429 | 430 | Tags the project's images, as defined in the project's settings.sh file. 431 | 432 | ## updateRoutes.sh 433 | 434 | For use with local instances. Updates the routes, as defined in the project's settings.sh file. 435 | 436 | ## exportTemplate.sh 437 | 438 | Helper script to export an OpenShift resource as a template. 439 | -------------------------------------------------------------------------------- /bin/assignRole.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | # ================================================================================================================= 6 | # Usage: 7 | # ----------------------------------------------------------------------------------------------------------------- 8 | usage() { 9 | cat <<-EOF 10 | Assigns a role to a user in one or more projects. 11 | 12 | Usage: $0 [ -h -x ] -r -u 13 | 14 | OPTIONS: 15 | ======== 16 | -r the role to assign; typically on of 'view', 'edit', or 'admin' 17 | -u the user to which the role is to be assigned 18 | -f read the project list from a file 19 | -h prints the usage for the script 20 | -x run the script in debug mode to see what's happening 21 | EOF 22 | exit 1 23 | } 24 | # ================================================================================================================= 25 | 26 | # ================================================================================================================= 27 | # Funtions: 28 | # ----------------------------------------------------------------------------------------------------------------- 29 | readProjectList(){ 30 | ( 31 | if [ -f ${projectListFile} ]; then 32 | # Read in the file minus any comments ... 33 | echo "Reading project list from ${projectListFile} ..." >&2 34 | _value=$(sed '/^[[:blank:]]*#/d;s/#.*//' ${projectListFile}) 35 | fi 36 | echo "${_value}" 37 | ) 38 | } 39 | # ================================================================================================================= 40 | 41 | # ================================================================================================================= 42 | # Initialization: 43 | # ----------------------------------------------------------------------------------------------------------------- 44 | # In case you wanted to check what variables were passed 45 | # echo "flags = $*" 46 | while getopts r:u:f:hx FLAG; do 47 | case $FLAG in 48 | r) export role=$OPTARG ;; 49 | u) export user=$OPTARG ;; 50 | f) export projectListFile=$OPTARG ;; 51 | x ) export DEBUG=1 ;; 52 | h ) usage ;; 53 | \?) #unrecognized option - show help 54 | echo -e \\n"Invalid script option"\\n 55 | usage 56 | ;; 57 | esac 58 | done 59 | 60 | # Shift the parameters in case there any more to be used 61 | shift $((OPTIND-1)) 62 | # echo Remaining arguments: $@ 63 | 64 | if [ -f ${OCTOOLSBIN}/ocFunctions.inc ]; then 65 | . ${OCTOOLSBIN}/ocFunctions.inc 66 | fi 67 | 68 | if [ ! -z "${DEBUG}" ]; then 69 | set -x 70 | fi 71 | 72 | if [ ! -z "${projectListFile}" ]; then 73 | projects=$(readProjectList) 74 | else 75 | projects=${@} 76 | fi 77 | 78 | if [ -z "${role}" ] || [ -z "${user}" ] || [ -z "${projects}" ]; then 79 | echo -e \\n"Missing parameters - role, user, or projects."\\n 80 | usage 81 | fi 82 | # ================================================================================================================= 83 | 84 | # ================================================================================================================= 85 | # Main Script 86 | # ----------------------------------------------------------------------------------------------------------------- 87 | for project in ${projects}; do 88 | assignRole "${role}" "${user}" "${project}" 89 | exitOnError 90 | done 91 | # ================================================================================================================= -------------------------------------------------------------------------------- /bin/compBuilds.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | OCTOOLSBIN=$(dirname $0) 3 | 4 | # ================================================================================================================= 5 | # Validation: 6 | # ----------------------------------------------------------------------------------------------------------------- 7 | _component_name=${1} 8 | if [ -z "${_component_name}" ]; then 9 | echo -e \\n"Missing parameter"\\n 10 | exit 1 11 | fi 12 | 13 | # ----------------------------------------------------------------------------------------------------------------- 14 | # Initialization: 15 | # ----------------------------------------------------------------------------------------------------------------- 16 | if [ -f ${OCTOOLSBIN}/ocFunctions.inc ]; then 17 | . ${OCTOOLSBIN}/ocFunctions.inc 18 | fi 19 | 20 | # Turn on debugging if asked 21 | if [ ! -z "${DEBUG}" ]; then 22 | set -x 23 | fi 24 | 25 | # ----------------------------------------------------------------------------------------------------------------- 26 | # Functions: 27 | # ----------------------------------------------------------------------------------------------------------------- 28 | generateBuildConfigs() { 29 | # Suppress the error message from getBuildTemplates when no search path is returned by getTemplateDir 30 | BUILDS=$(getBuildTemplates $(getTemplateDir ${_component_name}) 2>/dev/null || "") 31 | 32 | # echo "Build templates:" 33 | # for build in ${BUILDS}; do 34 | # echo ${build} 35 | # done 36 | # exit 1 37 | 38 | for build in ${BUILDS}; do 39 | echo -e \\n"Processing build configuration; ${build}..." 40 | 41 | _template="${build}" 42 | _template_basename=$(getFilenameWithoutExt ${build}) 43 | _buildConfig="${_template_basename}${BUILD_CONFIG_SUFFIX}" 44 | _searchPath=$(echo $(getDirectory "${_template}") | sed 's~\(^.*/openshift\).*~\1~') 45 | 46 | if [ ! -z "${PROFILE}" ] && [ "${PROFILE}" != "${_defaultProfileName}" ]; then 47 | _paramFileName="${_template_basename}.${PROFILE}" 48 | else 49 | _paramFileName="${_template_basename}" 50 | fi 51 | 52 | PARAMFILE=$(find ${_searchPath} -name "${_paramFileName}.param") 53 | if [ ! -z "${APPLY_LOCAL_SETTINGS}" ]; then 54 | LOCALPARAM=$(find ${_searchPath} -name "${_paramFileName}.local.param") 55 | fi 56 | 57 | if [ -f "${PARAMFILE}" ]; then 58 | PARAMFILE="--param-file=${PARAMFILE}" 59 | else 60 | PARAMFILE="" 61 | fi 62 | 63 | if [ -f "${LOCALPARAM}" ]; then 64 | LOCALPARAM="--param-file=${LOCALPARAM}" 65 | else 66 | LOCALPARAM="" 67 | fi 68 | 69 | oc -n ${TOOLS} process --local --filename=${_template} ${LOCALPARAM} ${PARAMFILE} > ${_buildConfig} 70 | exitOnError 71 | done 72 | } 73 | # ================================================================================================================= 74 | 75 | # ================================================================================================================= 76 | # Main Script: 77 | # ----------------------------------------------------------------------------------------------------------------- 78 | generateBuildConfigs 79 | 80 | if [ -z ${GEN_ONLY} ]; then 81 | echo -e \\n"Deploying build configuration files ..." 82 | deployBuildConfigs 83 | fi 84 | # ================================================================================================================= -------------------------------------------------------------------------------- /bin/compDeployments.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | OCTOOLSBIN=$(dirname $0) 3 | 4 | # ================================================================================================================= 5 | # Validation: 6 | # ----------------------------------------------------------------------------------------------------------------- 7 | _component_name=${1} 8 | if [ -z "${_component_name}" ]; then 9 | echo -e \\n"Missing parameter!"\\n 10 | exit 1 11 | fi 12 | # ----------------------------------------------------------------------------------------------------------------- 13 | # Initialization: 14 | # ----------------------------------------------------------------------------------------------------------------- 15 | if [ -f ${OCTOOLSBIN}/ocFunctions.inc ]; then 16 | . ${OCTOOLSBIN}/ocFunctions.inc 17 | fi 18 | 19 | # Check for dependancies 20 | JQ_EXE=jq 21 | if ! isInstalled ${JQ_EXE}; then 22 | echoWarning "The ${JQ_EXE} executable is required and was not found on your path." 23 | 24 | cat <<-EOF 25 | The recommended approach to installing the required package(s) is to use either [Homebrew](https://brew.sh/) (MAC) 26 | or [Chocolatey](https://chocolatey.org/) (Windows). 27 | 28 | Windows: 29 | - chocolatey install jq 30 | 31 | MAC: 32 | - brew install jq 33 | 34 | EOF 35 | exit 1 36 | fi 37 | 38 | # Turn on debugging if asked 39 | if [ ! -z "${DEBUG}" ]; then 40 | set -x 41 | fi 42 | 43 | # ----------------------------------------------------------------------------------------------------------------- 44 | # Functions: 45 | # ----------------------------------------------------------------------------------------------------------------- 46 | generateConfigs() { 47 | _projectName=$(getProjectName) 48 | 49 | DEPLOYS=$(getDeploymentTemplates $(getTemplateDir ${_component_name})) 50 | # echo "Deployment templates:" 51 | # for deploy in ${DEPLOYS}; do 52 | # echo ${deploy} 53 | # done 54 | # exit 1 55 | 56 | for deploy in ${DEPLOYS}; do 57 | echo -e \\n\\n"Processing deployment configuration; ${deploy} ..." 58 | 59 | _template="${deploy}" 60 | _template_basename=$(getFilenameWithoutExt ${deploy}) 61 | _deploymentConfig="${_template_basename}${DEPLOYMENT_CONFIG_SUFFIX}" 62 | _searchPath=$(echo $(getDirectory "${_template}") | sed 's~\(^.*/openshift\).*~\1~') 63 | PARAM_OVERRIDE_SCRIPT=$(find ${_searchPath} -name "${_template_basename}.overrides.sh") 64 | _componentSettings=$(find ${_searchPath} -name "${_componentSettingsFileName}") 65 | 66 | if [ ! -z ${_componentSettings} ] && [ -f ${_componentSettings} ]; then 67 | echo -e "Loading component level settings from ${_componentSettings} ..." 68 | . ${_componentSettings} 69 | fi 70 | 71 | if [ ! -z "${PROFILE}" ] && [ "${PROFILE}" != "${_defaultProfileName}" ]; then 72 | _paramFileName="${_template_basename}.${PROFILE}" 73 | else 74 | _paramFileName="${_template_basename}" 75 | fi 76 | 77 | PARAMFILE=$(find ${_searchPath} -name "${_paramFileName}.param") 78 | ENVPARAM=$(find ${_searchPath} -name "${_paramFileName}.${DEPLOYMENT_ENV_NAME}.param") 79 | 80 | if [ ! -z "${APPLY_LOCAL_SETTINGS}" ]; then 81 | LOCALPARAM=$(find ${_searchPath} -name "${_paramFileName}.local.param") 82 | fi 83 | 84 | # echoWarning "_template: ${_template}" 85 | # echoWarning "_template_basename: ${_template_basename}" 86 | # echoWarning "_deploymentConfig: ${_deploymentConfig}" 87 | # echoWarning "_searchPath: ${_searchPath}" 88 | # echoWarning PARAM_OVERRIDE_SCRIPT: \"${PARAM_OVERRIDE_SCRIPT}\" 89 | # echoWarning "_componentSettings: ${_componentSettings}" 90 | # echoWarning "_paramFileName: ${_paramFileName}" 91 | # echoWarning "PARAMFILE: ${PARAMFILE}" 92 | # echoWarning "ENVPARAM: ${ENVPARAM}" 93 | # echoWarning "LOCALPARAM: ${LOCALPARAM}" 94 | # exit 1 95 | 96 | # Used to inject variables from parameter files into override scripts 97 | unset overrideScriptVars 98 | 99 | if [ -f "${PARAMFILE}" ]; then 100 | overrideScriptVars="${overrideScriptVars:+~}$(readConf -f -d '\~' ${PARAMFILE})" 101 | PARAMFILE="--param-file=${PARAMFILE}" 102 | else 103 | PARAMFILE="" 104 | fi 105 | 106 | if [ -f "${ENVPARAM}" ]; then 107 | overrideScriptVars+="${overrideScriptVars:+~}$(readConf -f -d '\~' ${ENVPARAM})" 108 | ENVPARAM="--param-file=${ENVPARAM}" 109 | else 110 | ENVPARAM="" 111 | fi 112 | 113 | if [ -f "${LOCALPARAM}" ]; then 114 | overrideScriptVars+="${overrideScriptVars:+~}$(readConf -f -d '\~' ${LOCALPARAM})" 115 | LOCALPARAM="--param-file=${LOCALPARAM}" 116 | else 117 | LOCALPARAM="" 118 | fi 119 | 120 | # echoWarning "overrideScriptVars: ${overrideScriptVars}" 121 | # exit 1 122 | 123 | # Parameter overrides can be defined for individual deployment templates at the root openshift folder level ... 124 | if [ ! -z ${PARAM_OVERRIDE_SCRIPT} ] && [ -f ${PARAM_OVERRIDE_SCRIPT} ]; then 125 | # Read the TSV key=value pairs into an array ... 126 | IFS='~' read -ra overrideScriptVarsArray <<< "${overrideScriptVars}" 127 | echo -e "Loading parameter overrides for ${deploy} ..." 128 | SPECIALDEPLOYPARM+=" $(env "${overrideScriptVarsArray[@]}" ${PARAM_OVERRIDE_SCRIPT})" 129 | fi 130 | 131 | if updateOperation; then 132 | echoWarning "Preparing deployment configuration for update/replace, removing any 'Secret' objects so existing values are left untouched ..." 133 | oc -n ${_projectName} process --local --filename=${_template} ${SPECIALDEPLOYPARM} ${LOCALPARAM} ${ENVPARAM} ${PARAMFILE} \ 134 | | jq 'del(.items[] | select(.kind== "Secret"))' \ 135 | > ${_deploymentConfig} 136 | exitOnError 137 | elif createOperation; then 138 | oc -n ${_projectName} process --local --filename=${_template} ${SPECIALDEPLOYPARM} ${LOCALPARAM} ${ENVPARAM} ${PARAMFILE} > ${_deploymentConfig} 139 | exitOnError 140 | else 141 | echoError "\nUnrecognized operation, $(getOperation). Unable to process template.\n" 142 | exit 1 143 | fi 144 | 145 | if [ ! -z "${SPECIALDEPLOYPARM}" ]; then 146 | unset SPECIALDEPLOYPARM 147 | fi 148 | done 149 | } 150 | # ================================================================================================================= 151 | 152 | # ================================================================================================================= 153 | # Main Script: 154 | # ----------------------------------------------------------------------------------------------------------------- 155 | generateConfigs 156 | 157 | echo -e \\n"Removing temporary param override files ..." 158 | cleanOverrideParamFiles 159 | 160 | if [ -z ${GEN_ONLY} ]; then 161 | echo -e \\n"Deploying deployment configuration files ..." 162 | deployConfigs 163 | fi 164 | # ================================================================================================================= 165 | -------------------------------------------------------------------------------- /bin/createGlusterfsClusterApp.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | # =================================================================================== 6 | usage() { 7 | cat < 14 | 15 | Options: 16 | -h prints the usage for the script 17 | -x run the script in debug mode to see what's happening 18 | ================================================================================ 19 | EOF 20 | exit 1 21 | } 22 | # ------------------------------------------------------------------------------ 23 | # In case you wanted to check what variables were passed 24 | # echo "flags = $*" 25 | while getopts p:xh FLAG; do 26 | case $FLAG in 27 | p ) TARGET_PROJECT_NAME=$OPTARG ;; 28 | x ) export DEBUG=1 ;; 29 | h ) usage ;; 30 | \?) #unrecognized option - show help 31 | echo -e \\n"Invalid script option"\\n 32 | usage 33 | ;; 34 | esac 35 | done 36 | 37 | # Shift the parameters in case there any more to be used 38 | shift $((OPTIND-1)) 39 | # echo Remaining arguments: $@ 40 | 41 | if [ ! -z "${DEBUG}" ]; then 42 | set -x 43 | fi 44 | 45 | if [ -z "${TARGET_PROJECT_NAME}" ]; then 46 | echo -e \\n"Missing parameters - Project Name" 47 | usage 48 | fi 49 | # ----------------------------------------------------------------------------------- 50 | if [ -z "${GLUSTER_ENDPOINT_CONFIG}" ]; then 51 | GLUSTER_ENDPOINT_CONFIG=https://raw.githubusercontent.com/BCDevOps/openshift-tools/master/resources/glusterfs-cluster-app-endpoints.yml 52 | fi 53 | 54 | if [ -z "${GLUSTER_SVC_CONFIG}" ]; then 55 | GLUSTER_SVC_CONFIG=https://raw.githubusercontent.com/BCDevOps/openshift-tools/master/resources/glusterfs-cluster-app-service.yml 56 | fi 57 | 58 | if [ -z "${GLUSTER_SVC_NAME}" ]; then 59 | GLUSTER_SVC_NAME=glusterfs-cluster-app 60 | fi 61 | # ============================================================================== 62 | 63 | RTN_VAL=$(oc projects | grep ${TARGET_PROJECT_NAME}) 64 | if [ -z "$RTN_VAL" ]; then 65 | echo "Unable to create ${GLUSTER_SVC_NAME} in ${TARGET_PROJECT_NAME}, the ${TARGET_PROJECT_NAME} project does not exist ..." 66 | echo 67 | else 68 | RTN_VAL=$(oc get svc -n ${TARGET_PROJECT_NAME} | grep ${GLUSTER_SVC_NAME}) 69 | if [ -z "$RTN_VAL" ]; then 70 | echo "Creating ${GLUSTER_SVC_NAME} in ${TARGET_PROJECT_NAME} ..." 71 | echo 72 | 73 | oc create \ 74 | -f ${GLUSTER_ENDPOINT_CONFIG} \ 75 | -n ${TARGET_PROJECT_NAME} 76 | echo 77 | 78 | oc create \ 79 | -f ${GLUSTER_SVC_CONFIG} \ 80 | -n ${TARGET_PROJECT_NAME} 81 | echo 82 | else 83 | echo "${GLUSTER_SVC_NAME} already exists in ${TARGET_PROJECT_NAME} ..." 84 | echo 85 | fi 86 | fi 87 | -------------------------------------------------------------------------------- /bin/createLocalProject.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | # =================================================================================================== 6 | # Funtions 7 | # --------------------------------------------------------------------------------------------------- 8 | usage() { 9 | cat < [-n ] [-d ] 16 | 17 | OPTIONS: 18 | ======== 19 | -h prints the usage for the script 20 | -x run the script in debug mode to see what's happening 21 | -p the namespace for the project. 22 | -n The display name for the project. 23 | -d The description of the project. 24 | EOF 25 | exit 1 26 | } 27 | 28 | if [ -f ${OCTOOLSBIN}/ocFunctions.inc ]; then 29 | . ${OCTOOLSBIN}/ocFunctions.inc 30 | fi 31 | 32 | createProject (){ 33 | namespace=$1 34 | display_name=$2 35 | description=$3 36 | 37 | echo "Creating new project; ${namespace} ..." 38 | oc new-project ${namespace} --display-name="${display_name}" --description="${description}" >/dev/null 39 | } 40 | # =================================================================================================== 41 | 42 | # =================================================================================================== 43 | # Setup 44 | # --------------------------------------------------------------------------------------------------- 45 | while getopts p:n:d:hx FLAG; do 46 | case $FLAG in 47 | p ) PROJECT_NAMESPACE=$OPTARG ;; 48 | n ) DISPLAY_NAME=$OPTARG ;; 49 | d ) DESCRIPTION=$OPTARG ;; 50 | x ) export DEBUG=1 ;; 51 | h ) usage ;; 52 | \?) #unrecognized option - show help 53 | echo -e \\n"Invalid script option"\\n 54 | usage 55 | ;; 56 | esac 57 | done 58 | 59 | # Shift the parameters in case there any more to be used 60 | shift $((OPTIND-1)) 61 | # echo Remaining arguments: $@ 62 | 63 | if [ ! -z "${DEBUG}" ]; then 64 | set -x 65 | fi 66 | 67 | if [ -z "${PROJECT_NAMESPACE}" ]; then 68 | echo -e \\n"Missing parameters!" 69 | usage 70 | fi 71 | # =================================================================================================== 72 | 73 | if ! isLocalCluster; then 74 | echo "This script can only be run on a local cluster!" 75 | exit 1 76 | fi 77 | 78 | if ! projectExists ${PROJECT_NAMESPACE}; then 79 | createProject ${PROJECT_NAMESPACE} "${DISPLAY_NAME}" "${DESCRIPTION}" 80 | exitOnError 81 | else 82 | echo "${PROJECT_NAMESPACE} exists ..." 83 | fi 84 | -------------------------------------------------------------------------------- /bin/deleteLocalProject.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | # =================================================================================================== 6 | # Funtions 7 | # --------------------------------------------------------------------------------------------------- 8 | usage() { 9 | cat < 16 | 17 | OPTIONS: 18 | ======== 19 | -h prints the usage for the script 20 | -x run the script in debug mode to see what's happening 21 | -p the namespace for the project. 22 | EOF 23 | exit 1 24 | } 25 | 26 | if [ -f ${OCTOOLSBIN}/ocFunctions.inc ]; then 27 | . ${OCTOOLSBIN}/ocFunctions.inc 28 | fi 29 | 30 | deleteProject (){ 31 | projectName=$1 32 | echo "Deleting project; ${projectName} ..." 33 | oc delete project ${projectName} 34 | } 35 | # =================================================================================================== 36 | 37 | # =================================================================================================== 38 | # Setup 39 | # --------------------------------------------------------------------------------------------------- 40 | while getopts p:n:d:hx FLAG; do 41 | case $FLAG in 42 | p ) PROJECT_NAMESPACE=$OPTARG ;; 43 | x ) export DEBUG=1 ;; 44 | h ) usage ;; 45 | \?) #unrecognized option - show help 46 | echo -e \\n"Invalid script option"\\n 47 | usage 48 | ;; 49 | esac 50 | done 51 | 52 | # Shift the parameters in case there any more to be used 53 | shift $((OPTIND-1)) 54 | # echo Remaining arguments: $@ 55 | 56 | if [ ! -z "${DEBUG}" ]; then 57 | set -x 58 | fi 59 | 60 | if [ -z "${PROJECT_NAMESPACE}" ]; then 61 | echo -e \\n"Missing parameters!" 62 | usage 63 | fi 64 | # =================================================================================================== 65 | 66 | if ! isLocalCluster; then 67 | echo "This script can only be run on a local cluster!" 68 | exit 1 69 | fi 70 | 71 | if projectExists ${PROJECT_NAMESPACE}; then 72 | deleteProject ${PROJECT_NAMESPACE} 73 | exitOnError 74 | else 75 | echo "${PROJECT_NAMESPACE} does not exist ..." 76 | fi 77 | -------------------------------------------------------------------------------- /bin/dropAndRecreateDatabase.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | OCTOOLSBIN=$(dirname $0) 3 | 4 | # ============================================================================================================================== 5 | usage () { 6 | echo "========================================================================================" 7 | echo "Drops and recreates the database for a given environment." 8 | echo "----------------------------------------------------------------------------------------" 9 | echo "Usage:" 10 | echo 11 | echo "${0} " 12 | echo 13 | echo "Where:" 14 | echo " - is the project namespace containing the database pod." 15 | echo " - is the name of the database pod." 16 | echo " - is the name of the database." 17 | echo " - is the name of the database user." 18 | echo 19 | echo "Examples:" 20 | echo "${0} devex-von-dev postgresql TheOrgBook_Database TheOrgBook_User" 21 | echo "========================================================================================" 22 | exit 1 23 | } 24 | 25 | exitOnError () { 26 | rtnCd=$? 27 | if [ ${rtnCd} -ne 0 ]; then 28 | echo "An error has occurred while attempting to run a command in a pod! Please check the previous output message(s) for details." 29 | exit ${rtnCd} 30 | fi 31 | } 32 | # ============================================================================================================================== 33 | if [ -z "${1}" ]; then 34 | if [ -z "${PROJECT_NAMESPACE}" ] || [ -z "${DEPLOYMENT_ENV_NAME}" ]; then 35 | usage 36 | else 37 | PROJECT_NAME=${PROJECT_NAMESPACE}-${DEPLOYMENT_ENV_NAME} 38 | fi 39 | else 40 | PROJECT_NAME=${1} 41 | fi 42 | 43 | if [ -z "${2}" ]; then 44 | usage 45 | else 46 | DATABASE_POD_NAME=${2} 47 | fi 48 | 49 | if [ -z "${3}" ]; then 50 | usage 51 | else 52 | DATABASE_NAME=${3} 53 | fi 54 | 55 | if [ -z "${4}" ]; then 56 | usage 57 | else 58 | DATABASE_USER_NAME=${4} 59 | fi 60 | 61 | echo "=============================================================================" 62 | echo "Switching to project ${PROJECT_NAME} ..." 63 | echo "-----------------------------------------------------------------------------" 64 | oc project ${PROJECT_NAME} 65 | echo "============================================================================" 66 | echo 67 | 68 | echo "=============================================================================" 69 | echo "Recreating database ..." 70 | echo "-----------------------------------------------------------------------------" 71 | runInContainer.sh \ 72 | ${DATABASE_POD_NAME} \ 73 | "psql -c 'DROP DATABASE \"${DATABASE_NAME}\";'" 74 | 75 | runInContainer.sh \ 76 | ${DATABASE_POD_NAME} \ 77 | "psql -c 'CREATE DATABASE \"${DATABASE_NAME}\";'" 78 | 79 | runInContainer.sh \ 80 | ${DATABASE_POD_NAME} \ 81 | "psql -c 'GRANT ALL ON DATABASE \"${DATABASE_NAME}\" TO \"${DATABASE_USER_NAME}\";'" 82 | echo "============================================================================" 83 | echo 84 | 85 | echo "=============================================================================" 86 | echo "Listing databases ..." 87 | echo "-----------------------------------------------------------------------------" 88 | runInContainer.sh \ 89 | ${DATABASE_POD_NAME} \ 90 | 'psql -c "\l"' 91 | echo "============================================================================" 92 | echo 93 | -------------------------------------------------------------------------------- /bin/exportTemplate.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | SCRIPT_DIR=$(dirname $0) 3 | 4 | # =================================================================================================== 5 | # Funtions 6 | # --------------------------------------------------------------------------------------------------- 7 | usage (){ 8 | echo "========================================================================================" 9 | echo "Export an OpenShift resource as a template." 10 | echo 11 | echo "----------------------------------------------------------------------------------------" 12 | echo "Usage:" 13 | echo 14 | echo "${0} [output_format] [output_path]" 15 | echo 16 | echo "Where:" 17 | echo " - csv list of resources to export." 18 | echo " - The name of the resource to export." 19 | echo " - The name to assign to the template." 20 | echo " - [output_format] Optional: Output file format; json (default) or yaml." 21 | echo " - [output_path] Optiona: Output path." 22 | echo 23 | echo "Examples:" 24 | echo "${0} bc solr solr-template" 25 | echo "========================================================================================" 26 | exit 1 27 | } 28 | 29 | exitOnError (){ 30 | rtnCd=$? 31 | if [ ${rtnCd} -ne 0 ]; then 32 | echo "An error has occurred.! Please check the previous output message(s) for details." 33 | exit ${rtnCd} 34 | fi 35 | } 36 | # =================================================================================================== 37 | 38 | # =================================================================================================== 39 | # Setup 40 | # --------------------------------------------------------------------------------------------------- 41 | if [ -z "${1}" ]; then 42 | usage 43 | elif [ -z "${2}" ]; then 44 | usage 45 | elif [ -z "${3}" ]; then 46 | usage 47 | else 48 | RESOURCE_LIST=$1 49 | RESOURCE_NAME=$2 50 | TEMPLATE_NAME=$3 51 | fi 52 | 53 | if [ ! -z "${4}" ]; then 54 | OUTPUT_FORMAT=$4 55 | fi 56 | 57 | if [ ! -z "${5}" ]; then 58 | OUTPUT_PATH=$5 59 | fi 60 | 61 | if [ ! -z "${6}" ]; then 62 | usage 63 | fi 64 | 65 | if [ -z "$OUTPUT_FORMAT" ]; then 66 | OUTPUT_FORMAT=json 67 | fi 68 | 69 | if [ -z "$OUTPUT_PATH" ]; then 70 | OUTPUT_PATH="${SCRIPT_DIR}/${TEMPLATE_NAME}.${OUTPUT_FORMAT}" 71 | fi 72 | # =================================================================================================== 73 | 74 | oc export ${RESOURCE_LIST} ${RESOURCE_NAME} --as-template=${TEMPLATE_NAME} -o ${OUTPUT_FORMAT} > ${OUTPUT_PATH} -------------------------------------------------------------------------------- /bin/formatConfigurationTemplates.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | export MSYS_NO_PATHCONV=1 3 | SCRIPT_HOME="$( cd "$( dirname "$0" )" && pwd )" 4 | 5 | # ================================================================================================================= 6 | # Usage: 7 | # ----------------------------------------------------------------------------------------------------------------- 8 | usage() { 9 | cat <<-EOF 10 | Tool to convert configuration templates from one format to another. Currently only supports converting 11 | from json to yaml. 12 | 13 | Usage: ${0} 14 | EOF 15 | } 16 | 17 | # ================================================================================================================= 18 | # Process the local command line arguments and pass everything else along. 19 | # - The 'getopts' options string must start with ':' for this to work. 20 | # ----------------------------------------------------------------------------------------------------------------- 21 | while [ ${OPTIND} -le $# ]; do 22 | if getopts : FLAG; then 23 | case ${FLAG} in 24 | # List of local options: 25 | 26 | # Pass unrecognized options ... 27 | \?) pass+=" -${OPTARG}" ;; 28 | esac 29 | else 30 | # Pass unrecognized arguments ... 31 | pass+=" ${!OPTIND}" 32 | let OPTIND++ 33 | fi 34 | done 35 | 36 | # Pass the unrecognized arguments along for further processing ... 37 | shift $((OPTIND-1)) 38 | set -- "$@" $(echo -e "${pass}" | sed -e 's/^[[:space:]]*//') 39 | # ================================================================================================================= 40 | 41 | # ----------------------------------------------------------------------------------------------------------------- 42 | # Define hook scripts: 43 | # - These must be defined before the main settings script 'settings.sh' is loaded. 44 | # ----------------------------------------------------------------------------------------------------------------- 45 | onRequiredOptionsExist() { 46 | ( 47 | # No required options ... 48 | return 0 49 | ) 50 | } 51 | 52 | onUsesCommandLineArguments() { 53 | ( 54 | # This script is expecting command line arguments to be passed ... 55 | return 0 56 | ) 57 | } 58 | 59 | # ----------------------------------------------------------------------------------------------------------------- 60 | # Initialization: 61 | # ----------------------------------------------------------------------------------------------------------------- 62 | # Load the project settings and functions ... 63 | _includeFile="ocFunctions.inc" 64 | _settingsFile="settings.sh" 65 | if [ ! -z $(type -p ${_includeFile}) ]; then 66 | _includeFilePath=$(type -p ${_includeFile}) 67 | export OCTOOLSBIN=$(dirname ${_includeFilePath}) 68 | 69 | if [ -f ${OCTOOLSBIN}/${_settingsFile} ]; then 70 | . ${OCTOOLSBIN}/${_settingsFile} 71 | fi 72 | 73 | if [ -f ${OCTOOLSBIN}/${_includeFile} ]; then 74 | . ${OCTOOLSBIN}/${_includeFile} 75 | fi 76 | else 77 | _red='\033[0;31m' 78 | _yellow='\033[1;33m' 79 | _nc='\033[0m' # No Color 80 | echo -e \\n"${_red}${_includeFile} could not be found on the path.${_nc}" 81 | echo -e "${_yellow}Please ensure the openshift-developer-tools are installed on and registered on your path.${_nc}" 82 | echo -e "${_yellow}https://github.com/BCDevOps/openshift-developer-tools${_nc}" 83 | fi 84 | # ============================================================================== 85 | 86 | _cmd=$(toLower ${1-convertJsonToYaml}) 87 | shift 88 | 89 | case "${_cmd}" in 90 | convertjsontoyaml) 91 | convertJsonToYaml 92 | ;; 93 | 94 | *) 95 | echoWarning "Unrecognized command; ${_cmd}" 96 | globalUsage 97 | ;; 98 | esac -------------------------------------------------------------------------------- /bin/genBuilds.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | usage() { 6 | cat <<-EOF 7 | Tool to create or update OpenShift build config templates and Jenkins pipeline deployment using 8 | local and project settings. Also triggers builds that aren't auto-triggered ("builds" 9 | variable in settings.sh) and tags the images ("images" variable in settings.sh). 10 | 11 | Usage: 12 | ${0##*/} [options] 13 | EOF 14 | } 15 | 16 | if [ -f ${OCTOOLSBIN}/settings.sh ]; then 17 | . ${OCTOOLSBIN}/settings.sh 18 | fi 19 | 20 | if [ -f ${OCTOOLSBIN}/ocFunctions.inc ]; then 21 | . ${OCTOOLSBIN}/ocFunctions.inc 22 | fi 23 | # ============================================================================== 24 | 25 | echo -e \\n"Removing dangling configuration files ..." 26 | cleanBuildConfigs 27 | 28 | for component in ${components}; do 29 | if [ ! -z "${COMP}" ] && [ ! "${component}" = "." ] && [ ! "${COMP}" = ${component} ]; then 30 | # Only process named component if -c option specified 31 | continue 32 | fi 33 | 34 | echo -e \\n"Configuring the ${TOOLS} environment for ${component} ..." 35 | compBuilds.sh ${component} 36 | exitOnError 37 | done 38 | 39 | # Delete the configuration files if the keep command line option was not specified. 40 | if [ -z "${KEEPJSON}" ]; then 41 | echo -e \\n"Removing temporary build configuration files ..." 42 | cleanBuildConfigs 43 | fi 44 | 45 | if [ -z "${SKIP_PIPELINE_PROCESSING}" ]; then 46 | # Process the Jenkins Pipeline configurations ... 47 | processPipelines.sh 48 | else 49 | echoWarning "\nSkipping Jenkins pipeline processing ..." 50 | fi 51 | 52 | if [ ! -z "${COMP}" ]; then 53 | # If only processing one component stop here. 54 | exit 55 | fi 56 | 57 | if [ -z ${GEN_ONLY} ]; then 58 | # ============================================================================== 59 | # Post Build processing 60 | echo -e \\n"Builds created. Use the OpenShift Console to monitor the progress in the ${TOOLS} project." 61 | echo -e \\n"Pause here until the auto triggered builds complete, and then hit a key to continue the script." 62 | read -n1 -s -r -p "Press a key to continue..." key 63 | echo -e \\n 64 | 65 | for build in ${builds}; do 66 | echo -e \\n"Manually triggering build of ${build}..."\\n 67 | oc -n ${TOOLS} start-build ${build} 68 | exitOnError 69 | echo -e \\n"Use the OpenShift Console to monitor the build in the ${TOOLS} project." 70 | echo -e "Pause here until the build completes, and then hit a key to continue the script." 71 | echo -e \\n 72 | echo -e "If a build hangs take these steps:" 73 | echo -e " - cancel the instance of the build" 74 | echo -e " - edit the Build Config YAML and remove the entire 'resources' node; this should only be an issue for local deployments." 75 | echo -e " - click the Start Build button to restart the build" 76 | echo -e \\n 77 | read -n1 -s -r -p "Press a key to continue..." key 78 | echo -e \\n 79 | done 80 | 81 | if [ ! -z "${images}" ]; then 82 | # Tag the images for deployment to the DEV environment ... 83 | tagProjectImages.sh -s latest -t dev 84 | fi 85 | fi 86 | -------------------------------------------------------------------------------- /bin/genDepls.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | usage() { 6 | cat <<-EOF 7 | Tool to process OpenShift deployment config templates using local and project settings 8 | 9 | Usage: 10 | ${0##*/} [options] 11 | EOF 12 | } 13 | 14 | postDeploymentProcessing() { 15 | cat <<-EOF 16 | 17 | Use the OpenShift Console to monitor the deployment in the ${PROJECT_NAMESPACE}-${DEPLOYMENT_ENV_NAME} project. 18 | 19 | If a deploy hangs take these steps: 20 | - cancel the instance of the deployment 21 | - edit the Deployment Config Resources and remove the entire 'resources' node; this should only be an issue for local deployments." 22 | - click the Deploy button to restart the deploy 23 | 24 | EOF 25 | } 26 | 27 | 28 | if [ -f ${OCTOOLSBIN}/settings.sh ]; then 29 | . ${OCTOOLSBIN}/settings.sh 30 | fi 31 | 32 | if [ -f ${OCTOOLSBIN}/ocFunctions.inc ]; then 33 | . ${OCTOOLSBIN}/ocFunctions.inc 34 | fi 35 | # ============================================================================== 36 | 37 | echo -e \\n"Removing dangling configuration files ..." 38 | cleanConfigs 39 | cleanOverrideParamFiles 40 | 41 | for component in ${components}; do 42 | if [ ! -z "${COMP}" ] && [ ! "${component}" = "." ] && [ ! "${COMP}" = ${component} ]; then 43 | # Only process named component if -c option specified 44 | continue 45 | fi 46 | 47 | echo -e \\n"Configuring the ${DEPLOYMENT_ENV_NAME} environment for ${component} ..." 48 | compDeployments.sh ${component} 49 | exitOnError 50 | done 51 | 52 | # Delete the configuration files if the keep command line option was not specified. 53 | if [ -z "${KEEPJSON}" ]; then 54 | echo -e \\n"Removing temporary deployment configuration files ..." 55 | cleanConfigs 56 | fi 57 | 58 | if [ -z ${GEN_ONLY} ]; then 59 | # If a certificate.conf file is found try to automatically install the cerificates. 60 | deployCertificates 61 | 62 | # Print post deployment processing information 63 | postDeploymentProcessing 64 | fi -------------------------------------------------------------------------------- /bin/genParams.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | # ================================================================================================================= 6 | # Usage: 7 | # ----------------------------------------------------------------------------------------------------------------- 8 | usage() { 9 | cat <<-EOF 10 | A wrapper tool to generate OpenShift template and pipeline parameters files for the application. 11 | 12 | Usage: ./genParams.sh [ -h -f -l -x -c ] 13 | 14 | OPTIONS: 15 | ======== 16 | -h prints the usage for the script 17 | -f force generation even if the file already exists 18 | -l generate local params files - with all parameters commented out 19 | -c to generate parameters for templates of a specific component 20 | -p load a specific settings profile; setting..sh 21 | -P Use the default settings profile; settings.sh. Use this flag to ignore all but the default 22 | settings profile when there is more than one settings profile defined for a project. 23 | -x run the script in debug mode to see what's happening 24 | 25 | Update settings.sh and settings.local.sh files to set defaults 26 | 27 | EOF 28 | exit 29 | } 30 | 31 | # ----------------------------------------------------------------------------------------------------------------- 32 | # Initialization: 33 | # ----------------------------------------------------------------------------------------------------------------- 34 | while getopts p:Pc:flxh FLAG; do 35 | case $FLAG in 36 | h ) usage ;; 37 | \?) #unrecognized option - show help 38 | echo -e \\n"Invalid script option"\\n 39 | usage 40 | ;; 41 | esac 42 | done 43 | # ================================================================================================================= 44 | 45 | # For this to work the two scripts must accept the same set of options. 46 | genTemplateParams.sh $@ 47 | genPipelineParams.sh $@ 48 | -------------------------------------------------------------------------------- /bin/genPipelineParams.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | # ================================================================================================================= 6 | # Usage: 7 | # ----------------------------------------------------------------------------------------------------------------- 8 | usage() { #Usage function 9 | cat <<-EOF 10 | Tool to generate OpenShift Jenkins pipeline parameter files for each Jenkinsfile in an application's repository. 11 | 12 | Usage: 13 | 14 | ${0##*/} [options] 15 | 16 | Options: 17 | ======== 18 | -f force generation even if the file already exists 19 | EOF 20 | } 21 | 22 | # ================================================================================================================= 23 | # Process the local command line arguments and pass everything else along. 24 | # - The 'getopts' options string must start with ':' for this to work. 25 | # ----------------------------------------------------------------------------------------------------------------- 26 | while [ ${OPTIND} -le $# ]; do 27 | if getopts :f FLAG; then 28 | case ${FLAG} in 29 | # List of local options: 30 | f ) FORCE=1 ;; 31 | 32 | # Pass unrecognized options ... 33 | \?) 34 | pass+=" -${OPTARG}" 35 | ;; 36 | esac 37 | else 38 | # Pass unrecognized arguments ... 39 | pass+=" ${!OPTIND}" 40 | let OPTIND++ 41 | fi 42 | done 43 | 44 | # Pass the unrecognized arguments along for further processing ... 45 | shift $((OPTIND-1)) 46 | set -- "$@" $(echo -e "${pass}" | sed -e 's/^[[:space:]]*//') 47 | # ================================================================================================================= 48 | 49 | if [ -f ${OCTOOLSBIN}/settings.sh ]; then 50 | . ${OCTOOLSBIN}/settings.sh 51 | fi 52 | 53 | if [ -f ${OCTOOLSBIN}/ocFunctions.inc ]; then 54 | . ${OCTOOLSBIN}/ocFunctions.inc 55 | fi 56 | # ----------------------------------------------------------------------------------------------------------------- 57 | # Function(s): 58 | # ----------------------------------------------------------------------------------------------------------------- 59 | getLocalPipelineCommentFilter () { 60 | _commentFilter="s~^~#~;" 61 | 62 | # Uncomment the main local settings ... 63 | _commentFilter="${_commentFilter}/SOURCE_REPOSITORY_URL/s~^#~~;" 64 | _commentFilter="${_commentFilter}/SOURCE_REPOSITORY_REF/s~^#~~;" 65 | 66 | echo "sed ${_commentFilter}" 67 | } 68 | 69 | generatePipelineParameterFilter (){ 70 | _jenkinsFile=${1} 71 | if [ -z "${_jenkinsFile}" ]; then 72 | echo -e \\n"generatePipelineParameterFilter; Missing parameter - name of Jenkinsfile"\\n 73 | exit 1 74 | fi 75 | 76 | _directory=$(getDirectory ${_jenkinsFile}) 77 | _jenkinsFileName=$(getJenkinsFileName ${_jenkinsFile}) 78 | _contextDirectory=$(getContextDirectory ${_directory}) 79 | _componentName=$(getComponentNameFromDir ${_directory}) 80 | _pipelineName=$(getPipelineName "${_jenkinsFileName}" "${_componentName}") 81 | 82 | _pipelineJenkinsPathFilter="s~\(^JENKINSFILE_PATH=\).*$~\1${_jenkinsFileName}~" 83 | _pipelineNameFilter="s~\(^NAME=\).*$~\1${_pipelineName}~" 84 | _pipelineContextDirFilter="s~\(^CONTEXT_DIR=\).*$~\1${_contextDirectory}~" 85 | 86 | echo "sed ${_pipelineNameFilter};${_pipelineContextDirFilter};${_pipelineJenkinsPathFilter}" 87 | } 88 | 89 | generatePipelineParameterFile (){ 90 | _jenkinsFile=${1} 91 | _template=${2} 92 | _output=${3} 93 | _force=${4} 94 | _commentFilter=${5} 95 | _parameterFilter=${6} 96 | if [ -z "${_jenkinsFile}" ] || [ -z "${_template}" ]; then 97 | echo -e \\n"generatePipelineParameterFile; Missing parameter!"\\n 98 | exit 1 99 | fi 100 | 101 | if [ -f "${_jenkinsFile}" ]; then 102 | if [ ! -f "${_output}" ] || [ ! -z "${_force}" ]; then 103 | if [ -z "${_force}" ]; then 104 | echo -e "Generating pipeline parameter file for ${_jenkinsFile}; ${_output} ..."\\n 105 | else 106 | echoWarning "Overwriting the pipeline parameter file for ${_jenkinsFile}; ${_output} ...\n" 107 | fi 108 | 109 | # Generate the pipeline parameter file ... 110 | echo -e "#=========================================================" > ${_output} 111 | echo -e "# OpenShift Jenkins pipeline template parameters for:" >> ${_output} 112 | echo -e "# Jenkinsfile: ${_jenkinsFile}" >> ${_output} 113 | echo -e "# Template File: ${_template}" >> ${_output} 114 | echo -e "#=========================================================" >> ${_output} 115 | appendParametersToFile "${_template}" "${_output}" "${_commentFilter}" "${_parameterFilter}" 116 | exitOnError 117 | else 118 | echoWarning "The pipeline parameter file for ${_jenkinsFile} already exists and will not be overwritten; ${_output} ...\n" 119 | export FORCENOTE=1 120 | fi 121 | else 122 | echoError "Unable to generate pipeline parameter file for ${_jenkinsFile}. The file does not exist." 123 | fi 124 | } 125 | # ================================================================================================================= 126 | 127 | # ================================================================================================================= 128 | # Main: 129 | # ----------------------------------------------------------------------------------------------------------------- 130 | if [ ! -z "${SKIP_PIPELINE_PROCESSING}" ]; then 131 | echoWarning "\nSkipping Jenkins pipeline processing ..." 132 | exit 0 133 | fi 134 | 135 | if [ ! -z "${APPLY_LOCAL_SETTINGS}" ]; then 136 | COMMENTFILTER=$(getLocalPipelineCommentFilter) 137 | _outputDir=$(pwd -P) 138 | fi 139 | 140 | echo 141 | echo "=================================================================================================================" 142 | echo "Processing Jenkinsfiles" 143 | echo "-----------------------------------------------------------------------------------------------------------------" 144 | 145 | # Get list of all of the Jenkinsfiles in the project ... 146 | JENKINS_FILES=$(getJenkinsFiles) 147 | 148 | # Generate pipeline parameter files for each one ... 149 | for _jenkinsFile in ${JENKINS_FILES}; do 150 | _outputPath=$(getPipelineParameterFileOutputPath "${_jenkinsFile}" "${_outputDir}") 151 | _parameterFilter=$(generatePipelineParameterFilter "${_jenkinsFile}") 152 | generatePipelineParameterFile "${_jenkinsFile}" "${PIPELINE_JSON}" "${_outputPath}" "${FORCE}" "${COMMENTFILTER}" "${_parameterFilter}" 153 | exitOnError 154 | done 155 | echo "=================================================================================================================" 156 | 157 | # Print informational messages ... 158 | if [ ! -z "${APPLY_LOCAL_SETTINGS}" ] && [ -z "${FORCENOTE}" ]; then 159 | echoWarning "\nLocal files generated with parmeters commented out. Edit the files to uncomment and set parameters as needed.\n" 160 | fi 161 | 162 | if [ ! -z "${FORCENOTE}" ]; then 163 | echoWarning "One or more pipeline parameter files to be generated already exist and were not overwritten.\nUse the -f option to force the overwriting of existing files.\n" 164 | unset FORCENOTE 165 | fi 166 | # ================================================================================================================= 167 | -------------------------------------------------------------------------------- /bin/genSecrets.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | # ================================================================================================================= 5 | # Usage: 6 | # ----------------------------------------------------------------------------------------------------------------- 7 | usage() { 8 | cat <<-EOF 9 | A tool to create or update OpenShift secrets. 10 | 11 | This process is a bit more flexible than using the 'oc secrets' command directly, 12 | in that it allows you to update existing secrets, and create more customized templates. 13 | 14 | The secret templates should be located with their related component template(s). 15 | The name of the template MUST be in the form -secret.json 16 | The values for the secrets must be stored in files in plain text, and the files 17 | MUST be stored in a 'secrets' folder under the project's root openshift directory. 18 | The name of the folder MUST match the name of the template; i.e. './openshift/secrets/-secret' 19 | The name of the files MUST match the name of the matching parameter in the template, where '.' in the 20 | filename will be replaced with '_' when converted to a parameter name. Parameter names in the template 21 | should be in all caps, as the names will be converted to all caps when generated from the filename. 22 | 23 | Your project should define a .gitignore for 'secrets' so you do not accidentally commit your secrets to 24 | source control. 25 | 26 | Example: 27 | 28 | Template: 29 | ./openshift/templates/server/server-secret.json 30 | Containing Parameters: 31 | DISTRICTS_JSON 32 | REGIONS_JSON 33 | USERS_JSON 34 | Files Containing the plain text secrets: 35 | ./openshift/secrets/server-secret/districts.json 36 | ./openshift/secrets/server-secret/regions.json 37 | ./openshift/secrets/server-secret/users.json 38 | 39 | Usage: 40 | ${0##*/} [options] 41 | EOF 42 | } 43 | 44 | if [ -f ${OCTOOLSBIN}/settings.sh ]; then 45 | . ${OCTOOLSBIN}/settings.sh 46 | fi 47 | 48 | if [ -f ${OCTOOLSBIN}/ocFunctions.inc ]; then 49 | . ${OCTOOLSBIN}/ocFunctions.inc 50 | fi 51 | 52 | # ----------------------------------------------------------------------------------------------------------------- 53 | # Function(s): 54 | # ----------------------------------------------------------------------------------------------------------------- 55 | getSecretParamName () { 56 | _secretFile=${1} 57 | if [ -z "${_secretFile}" ]; then 58 | echo -e \\n"getSecretParamName; Missing parameter!"\\n 59 | exit 1 60 | fi 61 | 62 | _filename=$(basename ${_secretFile}) 63 | _paramName=$(echo ${_filename} | tr '[:lower:]' '[:upper:]' | sed "s~\.~_~g") 64 | echo ${_paramName} 65 | } 66 | 67 | getSecretParamValue () { 68 | _secretFile=${1} 69 | if [ -z "${_secretFile}" ]; then 70 | echo -e \\n"getSecretParamValue; Missing parameter!"\\n 71 | exit 1 72 | fi 73 | 74 | # Base64 encode and remove all whitespace ... 75 | _paramValue=$(cat ${_secretFile}|base64|tr -d " \t\n\r") 76 | echo ${_paramValue} 77 | } 78 | 79 | getSecretParamKeyValuePair () { 80 | _secretFile=${1} 81 | if [ -z "${_secretFile}" ]; then 82 | echo -e \\n"getSecretParamKeyValuePair; Missing parameter!"\\n 83 | exit 1 84 | fi 85 | 86 | _name=$(getSecretParamName ${_secretFile}) 87 | _value=$(getSecretParamValue ${_secretFile}) 88 | _param="${_name}=${_value}" 89 | echo ${_param} 90 | } 91 | 92 | createSecretParamFile () { 93 | _template=${1} 94 | if [ -z "${_template}" ]; then 95 | echo -e \\n"createSecretParamFile; Missing parameter!"\\n 96 | exit 1 97 | fi 98 | 99 | _name=$(getFilenameWithoutExt ${_template}) 100 | _output="${_OUTPUT_DIR}/${_name}.secret.param" 101 | _secretFiles=$(getSecretFiles ${_template}) 102 | 103 | # Generate the parameter file ... 104 | echo -e "#=========================================================" > ${_output} 105 | echo -e "# OpenShift template parameters for:" >> ${_output} 106 | echo -e "# JSON Template File: ${_template}" >> ${_output} 107 | echo -e "#=========================================================" >> ${_output} 108 | 109 | # Write the secrets into the parameter file ... 110 | for _secretFile in ${_secretFiles}; do 111 | _keyValuePair=$(getSecretParamKeyValuePair ${_secretFile}) 112 | echo -e "${_keyValuePair}" >> ${_output} 113 | done 114 | 115 | echo ${_output} 116 | } 117 | 118 | getSecretConfigFilename () { 119 | _template=${1} 120 | if [ -z "${_template}" ]; then 121 | echo -e \\n"getSecretConfigFilename; Missing parameter!"\\n 122 | exit 1 123 | fi 124 | 125 | _name=$(getFilenameWithoutExt ${_template}) 126 | _configFileName="${_OUTPUT_DIR}/${_name}_SecretConfig.json" 127 | echo ${_configFileName} 128 | } 129 | 130 | processSecret () { 131 | _template=${1} 132 | if [ -z "${_template}" ]; then 133 | echo -e \\n"processSecret; Missing parameter!"\\n 134 | exit 1 135 | fi 136 | 137 | echo -e \\n"Processing secret configuration; ${_template} ..." 138 | 139 | # Get the related secrets and convert them into template parameters ... 140 | _paramFile=$(createSecretParamFile ${_template}) 141 | _configFile=$(getSecretConfigFilename ${_template}) 142 | 143 | if [ -f "${_paramFile}" ]; then 144 | PARAMFILE="--param-file=${_paramFile}" 145 | else 146 | PARAMFILE="" 147 | fi 148 | 149 | oc -n ${_PROJECT_NAME} process --local --filename=${_template} ${PARAMFILE} > ${_configFile} 150 | exitOnError 151 | 152 | # Always remove the temporay parameter file ... 153 | if [ -f "${_paramFile}" ]; then 154 | rm ${_paramFile} 155 | fi 156 | 157 | if [ -z ${GEN_ONLY} ]; then 158 | oc -n ${_PROJECT_NAME} $(getOcAction) -f ${_configFile} 159 | exitOnError 160 | fi 161 | 162 | # Delete the temp config file if the keep command line option was not specified 163 | if [ -z "${KEEPJSON}" ]; then 164 | rm ${_configFile} 165 | fi 166 | } 167 | # ============================================================================== 168 | 169 | _OUTPUT_DIR=$(getRelativeOutputDir) 170 | _PROJECT_NAME=$(getProjectName) 171 | 172 | echo -e \\n"Deploying secret(s) into the ${DEPLOYMENT_ENV_NAME} (${_PROJECT_NAME}) environment ..."\\n 173 | 174 | # Get list of all of the secret templates in the project ... 175 | pushd ${PROJECT_DIR} >/dev/null 176 | _templates=$(getSecretTemplates) 177 | for _template in ${_templates}; do 178 | processSecret ${_template} 179 | done 180 | popd >/dev/null -------------------------------------------------------------------------------- /bin/genTemplateParams.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | # ================================================================================================================= 6 | # Usage: 7 | # ----------------------------------------------------------------------------------------------------------------- 8 | usage() { 9 | cat <<-EOF 10 | Tool to generate OpenShift template parameters files in expected places (project or local) for BC Gov applications. 11 | 12 | Usage: 13 | ${0##*/} [options] 14 | 15 | Options: 16 | ======== 17 | -f force generation even if the file already exists 18 | EOF 19 | } 20 | 21 | # ----------------------------------------------------------------------------------------------------------------- 22 | # Initialization: 23 | # ----------------------------------------------------------------------------------------------------------------- 24 | 25 | # ================================================================================================================= 26 | # Process the local command line arguments and pass everything else along. 27 | # - The 'getopts' options string must start with ':' for this to work. 28 | # ----------------------------------------------------------------------------------------------------------------- 29 | while [ ${OPTIND} -le $# ]; do 30 | if getopts :f FLAG; then 31 | case ${FLAG} in 32 | # List of local options: 33 | f ) FORCE=1 ;; 34 | 35 | # Pass unrecognized options ... 36 | \?) 37 | pass+=" -${OPTARG}" 38 | ;; 39 | esac 40 | else 41 | # Pass unrecognized arguments ... 42 | pass+=" ${!OPTIND}" 43 | let OPTIND++ 44 | fi 45 | done 46 | 47 | # Pass the unrecognized arguments along for further processing ... 48 | shift $((OPTIND-1)) 49 | set -- "$@" $(echo -e "${pass}" | sed -e 's/^[[:space:]]*//') 50 | # ================================================================================================================= 51 | 52 | if [ -f ${OCTOOLSBIN}/settings.sh ]; then 53 | . ${OCTOOLSBIN}/settings.sh 54 | fi 55 | 56 | if [ -f ${OCTOOLSBIN}/ocFunctions.inc ]; then 57 | . ${OCTOOLSBIN}/ocFunctions.inc 58 | fi 59 | 60 | # What types of files to generate - regular+dev/test/prod or local 61 | if [ ! -z "${APPLY_LOCAL_SETTINGS}" ]; then 62 | PARM_TYPES="l" 63 | else 64 | PARM_TYPES="r d t p" 65 | fi 66 | 67 | # ----------------------------------------------------------------------------------------------------------------- 68 | # Function(s): 69 | # ----------------------------------------------------------------------------------------------------------------- 70 | skipParameterFileGeneration () { 71 | _type=${1} 72 | _isBuildConfig=${2} 73 | if [ -z "${_type}" ]; then 74 | echo -e \\n"skipParameterFileGeneration; Missing parameter - file generation type"\\n 75 | exit 1 76 | fi 77 | 78 | unset _skip 79 | case ${type} in 80 | d ) # Dev File 81 | if [ ! -z "${_isBuildConfig}" ]; then 82 | _skip=1 83 | fi 84 | ;; 85 | t ) # Test File 86 | if [ ! -z "${_isBuildConfig}" ]; then 87 | _skip=1 88 | fi 89 | ;; 90 | p ) # Prod 91 | if [ ! -z "${_isBuildConfig}" ]; then 92 | _skip=1 93 | fi 94 | ;; 95 | esac 96 | 97 | if [ -z "${_skip}" ]; then 98 | return 1 99 | else 100 | return 0 101 | fi 102 | } 103 | 104 | getParameterFileCommentFilter () { 105 | _type=${1} 106 | if [ -z "${_type}" ]; then 107 | echo -e \\n"getParameterFileCommentFilter; Missing parameter!"\\n 108 | exit 1 109 | fi 110 | 111 | # Default; Comment out everything ... 112 | _commentFilter="s~^~#~;" 113 | 114 | case ${_type} in 115 | r ) # Regular file 116 | _commentFilter=cat 117 | ;; 118 | [dtp] ) # Dev, Test, and Prod Files 119 | # Uncomment the main environment specific settings ... 120 | _commentFilter="${_commentFilter}/TAG_NAME/s~^#~~;" 121 | _commentFilter="${_commentFilter}/APPLICATION_DOMAIN/s~^#~~;" 122 | 123 | _commentFilter="sed ${_commentFilter}" 124 | ;; 125 | l ) # Local file 126 | # Uncomment the main local settings ... 127 | _commentFilter="${_commentFilter}/GIT_REPO_URL/s~^#~~;" 128 | _commentFilter="${_commentFilter}/GIT_REF/s~^#~~;" 129 | 130 | _commentFilter="${_commentFilter}/MEMORY_LIMIT/s~^#~~;" 131 | _commentFilter="${_commentFilter}/MEMORY_REQUEST/s~^#~~;" 132 | _commentFilter="${_commentFilter}/CPU_LIMIT/s~^#~~;" 133 | _commentFilter="${_commentFilter}/CPU_REQUEST/s~^#~~;" 134 | 135 | _commentFilter="sed ${_commentFilter}" 136 | ;; 137 | *) # unrecognized option 138 | _commentFilter="sed ${_commentFilter}" 139 | ;; 140 | esac 141 | 142 | echo "${_commentFilter}" 143 | } 144 | 145 | getParameterFileOutputPath () { 146 | _type=${1} 147 | _fileName=${2} 148 | if [ -z "${_type}" ] || [ -z "${_fileName}" ]; then 149 | echo -e \\n"getParameterFileOutputPath; Missing parameter!"\\n 150 | exit 1 151 | fi 152 | 153 | if [ ! -z "${PROFILE}" ] && [ "${PROFILE}" != "${_defaultProfileName}" ]; then 154 | _outputFilename="${_fileName}.${PROFILE}" 155 | else 156 | _outputFilename="${_fileName}" 157 | fi 158 | 159 | case ${_type} in 160 | r ) # Regular file 161 | _output=${_outputFilename}.param 162 | ;; 163 | d ) # Dev File 164 | _output=${_outputFilename}.${DEV}.param 165 | ;; 166 | t ) # Test File 167 | _output=${_outputFilename}.${TEST}.param 168 | ;; 169 | p ) # Prod 170 | _output=${_outputFilename}.${PROD}.param 171 | ;; 172 | l ) # Local Files 173 | _output=${_outputFilename}.local.param 174 | ;; 175 | *) # unrecognized option 176 | echoError "\ngetParameterFileOutputPath; Invalid type option.\n" 177 | ;; 178 | esac 179 | 180 | echo ${_output} 181 | } 182 | 183 | generateParameterFilter (){ 184 | _component=${1} 185 | _type=${2} 186 | _templateName=${3} 187 | if [ -z "${_component}" ] ||[ -z "${_type}" ] || [ -z "${_templateName}" ]; then 188 | echo -e \\n"generateParameterFilter; Missing parameter!"\\n 189 | exit 1 190 | fi 191 | 192 | _parameterFilters="" 193 | _environment=${DEV} 194 | case ${_type} in 195 | # r ) # Regular file 196 | # _output=${_outputPrefix}$( basename ${_fileName}.param ) 197 | # ;; 198 | d ) # Dev File 199 | _environment=${DEV} 200 | ;; 201 | t ) # Test File 202 | _environment=${TEST} 203 | ;; 204 | p ) # Prod 205 | _environment=${PROD} 206 | ;; 207 | esac 208 | 209 | _name=$(basename "${_templateName}") 210 | _name=$(echo ${_name} | sed 's~\(^.*\)-\(build\|deploy\)$~\1~') 211 | _parameterFilters="${_parameterFilters}s~\(^NAME=\).*$~\1${_name}~;" 212 | _parameterFilters="${_parameterFilters}s~\(^\(IMAGE_NAMESPACE\|SOURCE_IMAGE_NAMESPACE\)=\).*$~\1${TOOLS}~;" 213 | 214 | if [ ! -z "${_environment}" ]; then 215 | _parameterFilters="${_parameterFilters}s~\(^TAG_NAME=\).*$~\1${_environment}~;" 216 | 217 | _appDomain="${_name}-${PROJECT_NAMESPACE}-${_environment}${APPLICATION_DOMAIN_POSTFIX}" 218 | _parameterFilters="${_parameterFilters}s~\(^APPLICATION_DOMAIN=\).*$~\1${_appDomain}~;" 219 | fi 220 | 221 | echo "sed ${_parameterFilters}" 222 | } 223 | 224 | generateParameterFile (){ 225 | _component=${1} 226 | _template=${2} 227 | _output=${3} 228 | _force=${4} 229 | _commentFilter=${5} 230 | _parameterFilter=${6} 231 | if [ -z "${_component}" ] || [ -z "${_template}" ]; then 232 | echo -e \\n"generatePipelineParameterFile; Missing parameter!"\\n 233 | exit 1 234 | fi 235 | 236 | if [ -f "${_template}" ]; then 237 | if [ ! -f "${_output}" ] || [ ! -z "${_force}" ]; then 238 | if [ -z "${_force}" ]; then 239 | echo -e "Generating parameter file for ${_template}; ${_output} ..."\\n 240 | else 241 | echoWarning "Overwriting the parameter file for ${_template}; ${_output} ...\n" 242 | fi 243 | 244 | # Generate the parameter file ... 245 | echo -e "#=========================================================" > ${_output} 246 | echo -e "# OpenShift template parameters for:" >> ${_output} 247 | echo -e "# Component: ${_component}" >> ${_output} 248 | echo -e "# Template File: ${_template}" >> ${_output} 249 | echo -e "#=========================================================" >> ${_output} 250 | appendParametersToFile "${_template}" "${_output}" "${_commentFilter}" "${_parameterFilter}" 251 | exitOnError 252 | else 253 | echoWarning "The parameter file for ${_template} already exisits and will not be overwritten; ${_output} ...\n" 254 | export FORCENOTE=1 255 | fi 256 | else 257 | echoError "Unable to generate parameter file for ${_template}. The file does not exist." 258 | fi 259 | } 260 | # ================================================================================================================= 261 | 262 | 263 | # ================================================================================================================= 264 | # Main: 265 | # ----------------------------------------------------------------------------------------------------------------- 266 | for component in ${components}; do 267 | if [ ! -z "${COMP}" ] && [ ! "${component}" = "." ] && [ ! "${COMP}" = ${component} ]; then 268 | # Only process named component if -c option specified 269 | continue 270 | fi 271 | 272 | echo 273 | echo "=================================================================================================================" 274 | echo "Processing templates for ${component}" 275 | echo "-----------------------------------------------------------------------------------------------------------------" 276 | 277 | _configTemplates=$(getConfigTemplates $(getTemplateDir ${component})) 278 | # echo "Configuration templates:" 279 | # for configTemplate in ${_configTemplates}; do 280 | # echo ${configTemplate} 281 | # done 282 | # exit 1 283 | 284 | # Iterate through each file and generate the params files 285 | for file in ${_configTemplates}; do 286 | # Don't generate dev/test/prod param files for Build templates 287 | TEMPLATE=${file} 288 | if isBuildConfig ${TEMPLATE}; then 289 | _isBuildConfig=1 290 | else 291 | unset _isBuildConfig 292 | fi 293 | 294 | for type in ${PARM_TYPES}; do 295 | # Don't create environment specific param files for Build Templates 296 | if ! skipParameterFileGeneration "${type}" "${_isBuildConfig}"; then 297 | _commentFilter=$(getParameterFileCommentFilter "${type}") 298 | _output=$(getParameterFileOutputPath "${type}" "${file%.*}") 299 | _parameterFilter=$(generateParameterFilter "${component}" "${type}" "$(getFilenameWithoutExt ${file})") 300 | # echoWarning "file: ${file}" 301 | # echoWarning "file wo/ext: ${file%.*}" 302 | # echoWarning "_output: ${_output}" 303 | # echoWarning "_commentFilter: ${_commentFilter}" 304 | # echoWarning "_parameterFilter: ${_parameterFilter}" 305 | generateParameterFile "${component}" "${TEMPLATE}" "${_output}" "${FORCE}" "${_commentFilter}" "${_parameterFilter}" 306 | exitOnError 307 | else 308 | # Remove `>/dev/null` to enable this message. 309 | # It's useful for troubleshooting, but annoying otherwise. 310 | echo \ 311 | "Skipping environment specific, environmentType '${type}', parameter file generation for build template; ${file} ..." \ 312 | >/dev/null 313 | fi 314 | done 315 | done 316 | 317 | echo "=================================================================================================================" 318 | done 319 | 320 | # Print informational messages ... 321 | if [ ! -z "${APPLY_LOCAL_SETTINGS}" ] && [ -z "${FORCENOTE}" ]; then 322 | echoWarning "\nLocal files generated with parmeters commented out. Edit the files to uncomment and set parameters as needed.\n" 323 | fi 324 | 325 | if [ ! -z "${FORCENOTE}" ]; then 326 | echoWarning "\nOne or more parameter files to be generated already exist and were not overwritten.\nUse the -f option to force the overwriting of existing files.\n" 327 | unset FORCENOTE 328 | fi 329 | # ================================================================================================================= -------------------------------------------------------------------------------- /bin/generateLocalProjects.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | # =================================================================================================== 6 | # Funtions 7 | # --------------------------------------------------------------------------------------------------- 8 | usage() { 9 | cat < the name of a project set 21 | EOF 22 | } 23 | 24 | # ================================================================================================================= 25 | # Process the local command line arguments and pass everything else along. 26 | # - The 'getopts' options string must start with ':' for this to work. 27 | # ----------------------------------------------------------------------------------------------------------------- 28 | while [ ${OPTIND} -le $# ]; do 29 | if getopts :n:D FLAG; then 30 | case ${FLAG} in 31 | # List of local options: 32 | D ) export DELETE_PROJECTS=1 ;; 33 | n ) PROJECT_NAMESPACE=${OPTARG} ;; 34 | 35 | # Pass unrecognized options ... 36 | \?) 37 | pass+=" -${OPTARG}" 38 | ;; 39 | esac 40 | else 41 | # Pass unrecognized arguments ... 42 | pass+=" ${!OPTIND}" 43 | let OPTIND++ 44 | fi 45 | done 46 | 47 | # Pass the unrecognized arguments along for further processing ... 48 | shift $((OPTIND-1)) 49 | set -- "$@" $(echo -e "${pass}" | sed -e 's/^[[:space:]]*//') 50 | # ================================================================================================================= 51 | 52 | if [ -f ${OCTOOLSBIN}/settings.sh ]; then 53 | . ${OCTOOLSBIN}/settings.sh 54 | fi 55 | 56 | if [ -f ${OCTOOLSBIN}/ocFunctions.inc ]; then 57 | . ${OCTOOLSBIN}/ocFunctions.inc 58 | fi 59 | # =================================================================================================== 60 | 61 | if ! isLocalCluster; then 62 | echo "This script can only be run on a local cluster!" 63 | exit 1 64 | fi 65 | 66 | TOOLS="${TOOLS:-${PROJECT_NAMESPACE}-tools}" 67 | DEV="${DEV:-dev}" 68 | TEST="${TEST:-test}" 69 | PROD="${PROD:-prod}" 70 | 71 | # Iterate through Tools, Dev, Test and Prod projects and create them if they don't exist. 72 | for project in ${TOOLS} ${PROJECT_NAMESPACE}-${DEV} ${PROJECT_NAMESPACE}-${TEST} ${PROJECT_NAMESPACE}-${PROD}; do 73 | 74 | if [ -z ${DELETE_PROJECTS} ]; then 75 | # Create ..." 76 | createLocalProject.sh \ 77 | -p ${project} 78 | exitOnError 79 | else 80 | # Delete ..." 81 | deleteLocalProject.sh \ 82 | -p ${project} 83 | exitOnError 84 | fi 85 | done 86 | 87 | # ToDo: 88 | # - Run the build and deployment generation too. 89 | -------------------------------------------------------------------------------- /bin/getDids.inc: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | UNKNOWN="Unknown" 3 | QUERY_TIMEOUT=30 4 | BCOVRIN_QUERY_TIMEOUT=600 5 | TXN_TYPES=" 6 | 0=NODE 7 | 1=NYM 8 | 3=GET_TXN 9 | 4=TXN_AUTHOR_AGREEMENT 10 | 5=TXN_AUTHOR_AGREEMENT_AML 11 | 6=GET_TXN_AUTHOR_AGREEMENT 12 | 7=GET_TXN_AUTHOR_AGREEMENT_AML 13 | 8=DISABLE_ALL_TXN_AUTHR_AGRMTS 14 | 100=ATTRIB 15 | 101=SCHEMA 16 | 102=CRED_DEF 17 | 103=DISCLO 18 | 104=GET_ATTR 19 | 105=GET_NYM 20 | 107=GET_SCHEMA 21 | 108=GET_CLAIM_DEF 22 | 109=POOL_UPGRADE 23 | 110=NODE_UPGRADE 24 | 111=POOL_CONFIG 25 | 112=CHANGE_KEY 26 | 113=REVOC_REG_DEF 27 | 114=REVOC_REG_ENTRY 28 | 115=GET_REVOC_REG_DEF 29 | 116=GET_REVOC_REG 30 | 117=GET_REVOC_REG_DELTA 31 | 118=POOL_RESTART 32 | 119=VALIDATOR_INFO 33 | 120=AUTH_RULE 34 | 121=GET_AUTH_RULE 35 | 122=AUTH_RULES 36 | " 37 | 38 | # ==================================================================================== 39 | # getDids 40 | # - See getDids.sh for use. 41 | # ------------------------------------------------------------------------------------ 42 | # This function scans through accessible (based on user access) OCP projects to 43 | # discover the DIDs associated to Hyperledger Indy and Aries project components. 44 | # It then scans the Sovrin networks for additional information regarding the 45 | # DID and it's most recent activity. 46 | # The data is then saved to a csv file as well as being output to the console. 47 | # 48 | # Data Collected: 49 | # --------------- 50 | # Project Name: 51 | # - The name (license plate) of the OCP project in which the DID was found. 52 | # 53 | # Display Name: 54 | # - The resource (component) name associated with the DID. 55 | # 56 | # Secret Name: 57 | # - The name of the secret resource associated with the DID. 58 | # 59 | # Agent Name: 60 | # - The name of the deploymentConfig or deployment resource associated with the agent. 61 | # - The field can be formatted as a hyperlink by including the '--hyperlink' switch. 62 | # 63 | # Agent Version: 64 | # - The verison of ACA-Py the agent is running. 65 | # 66 | # Instances: 67 | # - The number of agent instance running. 68 | # 69 | # Storage Type: 70 | # - The type of secure storage the agent is using. 71 | # 72 | # Endorser Role: 73 | # - The agent's endorser role. 74 | # 75 | # DID: 76 | # - The DID. 77 | # 78 | # Ledger: 79 | # - The ledger on which the DID resides. Only the Sovrin networks are scanned. 80 | # - Therefore "Unknown" means the DID resides on a ledger other than one of 81 | # the Sovrin networks. This could be any of the BCovrin or CANdy ledgers. 82 | # - This field requires a ledger scan, which is a time consuming operation. 83 | # The ledger scan can be skipped by including '--no-ledger-scan' switch. 84 | # 85 | # Role: 86 | # - The role assigned to the DID. Only the Sovrin networks are scanned. 87 | # - This field requires a ledger scan, which is a time consuming operation. 88 | # The ledger scan can be skipped by including '--no-ledger-scan' switch. 89 | # 90 | # Last Used Date: 91 | # - The date associated to the last txn written to the ledger by the DID. 92 | # - This field requires a ledger scan, which is a time consuming operation. 93 | # The ledger scan can be skipped by including '--no-ledger-scan' switch. 94 | # 95 | # Last Used For: 96 | # - The last written txn type. 97 | # - This field requires a ledger scan, which is a time consuming operation. 98 | # The ledger scan can be skipped by including '--no-ledger-scan' switch. 99 | # 100 | # Command Line Switches: 101 | # ---------------------- 102 | # --no-ledger-scan: 103 | # - Do not scan ledgers for the DID. This can be used to save some time 104 | # if the informaiton about the DID from the ledgers is not needed. 105 | # 106 | # --hyperlink: 107 | # - Generate a hyperlink for the agent name. Turns the agent name into a 108 | # clickable link when viewing the csv in excel. 109 | # Does not format well for console output. 110 | # 111 | # --no-clean: 112 | # - Skip the cleanup of the von-network environment. 113 | # Useful if you are planning mutiple runs. 114 | # ==================================================================================== 115 | 116 | function getDids() 117 | { 118 | ( 119 | local OPTIND 120 | unset local filter 121 | unset local cluster 122 | while getopts f:d:-: FLAG; do 123 | case $FLAG in 124 | f ) 125 | local filter=${OPTARG} 126 | ;; 127 | d ) 128 | local cluster=${OPTARG} 129 | ;; 130 | - ) 131 | case ${OPTARG} in 132 | "no-ledger-scan"* ) 133 | local no_ledger_scan=1 134 | ;; 135 | "hyperlink"* ) 136 | local hyperlink=1 137 | ;; 138 | "no-clean"* ) 139 | local no_clean=1 140 | ;; 141 | esac 142 | esac 143 | done 144 | shift $((OPTIND-1)) 145 | 146 | initTxnMap 147 | 148 | # von-network needed to convert Seeds into DIDs 149 | if [ ! -d ./von-network ]; then 150 | echoWarning "Cloning von-network to $(pwd)/von-network ..." 151 | git clone --depth 1 https://github.com/bcgov/von-network.git ./von-network 152 | fi 153 | 154 | if [ ! -z "${cluster}" ]; then 155 | context=$(oc config get-contexts | sed 's/*/ /g' | grep ${cluster} | awk '{print $1}' | head -n 1) 156 | else 157 | context=$(oc config current-context) 158 | fi 159 | 160 | if [ -z ${FULLY_QUALIFIED_NAMESPACE} ]; then 161 | projects=$(oc --context=${context} projects -q) 162 | else 163 | projects=$(getProjectName) 164 | fi 165 | 166 | if [ ! -z "${no_ledger_scan}" ]; then 167 | echoWarning "\nLedger Scan - Disabled" 168 | else 169 | echoWarning "\nLedger Scan - Enabled" 170 | fi 171 | 172 | if [ ! -z "${hyperlink}" ]; then 173 | echoWarning "Agent Hyperlink - Enabled" 174 | else 175 | echoWarning "Agent Hyperlink - Disabled" 176 | fi 177 | 178 | if [ ! -z "${no_clean}" ]; then 179 | echoWarning "No Clean - Enabled" 180 | fi 181 | 182 | echo 183 | echo 184 | for project in ${projects}; do 185 | echo -e "\e[1A\e[KScanning project '${project}' ..." 186 | 187 | # Get project name 188 | projectDisplayName=$(oc get project ${project} -o json | jq --raw-output '.metadata.annotations."openshift.io/display-name"') 189 | 190 | # Get a list of seeds in each environment ... 191 | seeds=$(oc -n ${project} --context=${context} get secret --template '{{ range $item := .items }}{{if $item.data.seed}}{{ printf "%s,%s\n" $item.metadata.name $item.data.seed}}{{end}}{{end}}') 192 | 193 | if [ ! -z "${seeds}" ]; then 194 | # Decode the Seeds into DIDs 195 | for item in ${seeds}; do 196 | seed=$(echo ${item##*,} | openssl base64 -d) 197 | pushd ./von-network >/dev/null 198 | did=$(./manage generatedid ${seed} 2>/dev/null | sed -n 's~^DID: \(.*\)$~\1~p') 199 | popd >/dev/null 200 | 201 | if [ -z "${no_ledger_scan}" ]; then 202 | scanLedgers "${did}" 203 | fi 204 | 205 | agentName=$(echo "${item%%,*}" | sed 's~-wallet-credentials~~g') 206 | agentResourceName=$(oc -n ${project} get all -l name=${agentName} 2>/dev/null | grep deployment | awk '{print $1}') 207 | if [ -z "${agentResourceName}" ]; then 208 | agentResourceName=$(oc -n ${project} get all -l app.kubernetes.io/name=${agentName} 2>/dev/null | grep deployment | awk '{print $1}') 209 | fi 210 | 211 | # Defaults 212 | agentHyperlink="${agentName}" 213 | agentHyperlink="${agentName}" 214 | agentVersion="none" 215 | availableReplicas="missing" 216 | storageType="" 217 | endorserRole="" 218 | if [ ! -z "${agentResourceName}" ]; then 219 | # Get agent resource information ... 220 | agentResourceType="${agentResourceName%%.*}" 221 | agentDetails=$(oc -n ${project} get ${agentResourceName} -o json) 222 | agentStartCommand=$(jq -nr "${agentDetails} | .spec.template.spec.containers[0].command | last") 223 | argFile=$(echo ${agentStartCommand} | sed -nr 's~.*--arg-file(.*)~\1~p' | awk '{print $1}' ) 224 | if [ -z ${argFile} ]; then 225 | agentArs=$(jq -nr "${agentDetails} | .spec.template.spec.containers[0].args | last") 226 | argFile=$(echo ${agentArs} | sed -nr 's~.*--arg-file(.*)~\1~p' | awk '{print $1}' ) 227 | fi 228 | if [[ "${argFile}" == "\${"* ]]; then 229 | # argFile is an environmnet variable - resolve it ... 230 | argFileVar=$(echo ${argFile} | tr -d \$\{\}) 231 | argFile=$(jq -nr "${agentDetails} | .spec.template.spec.containers[0].env[] | select(.name==\"${argFileVar}\").value") 232 | fi 233 | 234 | # Instance Count 235 | availableReplicas=$(jq -nr "${agentDetails} | .status.availableReplicas") 236 | 237 | # Endorser Role 238 | endorserRole=$(jq -nr "${agentDetails} | .spec.template.spec.containers[0].env[] | select(.name==\"ACAPY_ENDORSER_ROLE\").value") 239 | if [ -z ${endorserRole} ]; then 240 | endorserRole=$(echo ${agentStartCommand} | sed -nr 's~.*--endorser-protocol-role(.*)~\1~p' | awk '{print $1}' ) 241 | fi 242 | 243 | # Storage Type 244 | storageType=$(jq -nr "${agentDetails} | .spec.template.spec.containers[0].env[] | select(.name==\"ACAPY_WALLET_TYPE\").value") 245 | if [ -z ${storageType} ]; then 246 | storageType=$(echo ${agentStartCommand} | sed -nr 's~.*--wallet-type(.*)~\1~p' | awk '{print $1}' ) 247 | fi 248 | 249 | # Fetch details that can only be collected from a running instance ... 250 | agentVersion="n/a" 251 | if ((${availableReplicas} >= 1)); then 252 | 253 | # ACA-Py version 254 | agentVersion=$(FULLY_QUALIFIED_NAMESPACE=${project} runInContainer "${agentName}" "aca-py --version" 2>/dev/null) 255 | if (( $? !=0 )); then 256 | agentVersion="Error running 'aca-py --version'" 257 | fi 258 | 259 | # Storage Type - when defined in the instance's arg file. 260 | if [ -z ${storageType} ]; then 261 | storageType=$(FULLY_QUALIFIED_NAMESPACE=${project} runInContainer "${agentName}" "grep -i wallet-type ${argFile}" 2>/dev/null | awk '{print $2}') 262 | fi 263 | 264 | # Endorser Role - when defined in the instance's arg file. 265 | if [ -z ${endorserRole} ]; then 266 | endorserRole=$(FULLY_QUALIFIED_NAMESPACE=${project} runInContainer "${agentName}" "grep -i endorser-protocol-role \"${argFile}\"" 2>/dev/null | awk '{print $2}') 267 | fi 268 | fi 269 | 270 | if [ -z "${endorserRole}" ]; then 271 | endorserRole="none" 272 | fi 273 | 274 | if [ -z "${storageType}" ]; then 275 | storageType="not found" 276 | else 277 | # Remove any quotes from around the storage type string. 278 | storageType=$(echo ${storageType} | tr -d \'\") 279 | fi 280 | 281 | agentLink="https://console.apps.silver.devops.gov.bc.ca/k8s/ns/${project}/${agentResourceType}s/${agentName}" 282 | if [ ! -z "${hyperlink}" ]; then 283 | agentHyperlink="\"=HYPERLINK(\"\"${agentLink}\"\",\"\"${agentName}\"\")\"" 284 | fi 285 | fi 286 | 287 | didList+="${project},${projectDisplayName},${item%%,*},${agentHyperlink},${agentVersion},${availableReplicas},${storageType},${endorserRole},${did},${ledger},${role},${lastUsedDate},${lastUsedFor}\n" 288 | done 289 | fi 290 | done 291 | 292 | # ==================================================================================================================== 293 | # For testing and troubleshooting 294 | # -------------------------------------------------------------------------------------------------------------------- 295 | # Sovrin TestNet 296 | # did="RznYFPVhHpYZgsn4Hu3StV" 297 | # # dev.bcovrin 298 | # did="6tGCSqhLKu31BCKRrf7nGe" 299 | # # test.bcovrin 300 | # did="F5xxSw8bAKBWjNvC43dMbm" 301 | # did="NEYQZjF5bs1tTRbCVs5w7Q" 302 | # scanLedgers "${did}" 303 | # didList+="${project},${projectDisplayName},${item%%,*},${did},${ledger},${role},${lastUsedDate},${lastUsedFor}\n" 304 | # ==================================================================================================================== 305 | 306 | if [ ! -z "${filter}" ]; then 307 | echo -e "\e[1A\e[KFiltering did list on '${filter}' ..." 308 | unset filteredList 309 | didListToFilter=$(echo "${didList}" | sed 's~\\n~\n~g') 310 | for item in ${didListToFilter}; do 311 | filteredDid=$(echo "${item}" | grep "${filter}") 312 | if [ ! -z "${filteredDid}" ]; then 313 | filteredList+="${filteredDid}\n" 314 | fi 315 | done 316 | didList="${filteredList}" 317 | fi 318 | 319 | echo -e "\e[1A\e[K" 320 | didList=$(echo "${didList}" | sed 's~\\n~\n~g') 321 | itemCount=$(echo "${didList}" | wc -l) 322 | result="$(echo -e "PROJECT:,DISPLAY NAME:,SECRET NAME:,AGENT NAME:,AGENT VERSION:,INSTANCES:,STORAGE TYPE:,ENDORSER ROLE:,DID:,LEDGER:,ROLE:,LAST USED DATE:,LAST USED FOR:\n${didList}")" 323 | echoWarning "Writing results to $(pwd)/DID-List.csv ...\n" 324 | echo "${result}" > DID-List.csv 325 | echoWarning "Results (${itemCount}):" 326 | echo "${result}" | column -t -s , 327 | 328 | # Clean-up von-network 329 | if [ -z "${no_clean}" ] && [ -d ./von-network ]; then 330 | echoWarning \\n"Removing von-network from $(pwd)/von-network ..." 331 | rm -rf ./von-network 332 | fi 333 | ) 334 | } 335 | 336 | declare -A txnMap 337 | function initTxnMap() { 338 | for txn_type in ${TXN_TYPES}; do 339 | key=${txn_type%%=*} 340 | value=${txn_type#*=} 341 | txnMap[${key}]=${value} 342 | done 343 | } 344 | 345 | # ToDo: 346 | # - The following functions rely on global variables so there is potential for variables 347 | # to get overwritten inadvertently with the wrong data. This should be addressed, 348 | # however at this point things are working as expected. 349 | function scanLedgers() { 350 | local did=${1} 351 | ledger="${UNKNOWN}" 352 | 353 | if isLedgerUnknow ${ledger}; then 354 | scanSovrinLedgers "${did}" 355 | fi 356 | 357 | if isLedgerUnknow ${ledger}; then 358 | scanCandyLedgers "${did}" 359 | fi 360 | 361 | if isLedgerUnknow ${ledger}; then 362 | scanBCovrinLedgers "${did}" 363 | fi 364 | } 365 | 366 | function scanSovrinLedgers() { 367 | local did=${1} 368 | local baseUrl="https://indyscan.io" 369 | 370 | # Scan for the DID on the Sovrin Ledgers 371 | ledgers="SOVRIN_STAGINGNET SOVRIN_MAINNET SOVRIN_BUILDERNET" 372 | for ledger in ${ledgers}; do 373 | scanLedger "${did}" "${ledger}" "${baseUrl}" 374 | if isNotNullOrEmpty "${parsedResult}"; then 375 | break; 376 | fi 377 | done 378 | } 379 | 380 | function scanCandyLedgers() { 381 | local did=${1} 382 | local baseUrl="https://candyscan.idlab.org" 383 | 384 | # Scan for the DID on the CANdy Ledgers 385 | ledgers="CANDY_DEV CANDY_TEST CANDY_PROD" 386 | for ledger in ${ledgers}; do 387 | scanLedger "${did}" "${ledger}" "${baseUrl}" 388 | if isNotNullOrEmpty "${parsedResult}"; then 389 | break; 390 | fi 391 | done 392 | } 393 | 394 | function scanBCovrinLedgers() { 395 | local did=${1} 396 | 397 | # Scan for the DID on the BCovrin Ledgers 398 | # Scan BCovrin Test last, because it takes longer 399 | ledgers="dev.bcovrin prod.bcovrin test.bcovrin" 400 | for ledger in ${ledgers}; do 401 | if [ ${ledger} == "test.bcovrin" ]; then 402 | local baseUrl="http://test.bcovrin.vonx.io:3707" 403 | ledger="BCOVRIN_TEST" 404 | scanLedger "${did}" "${ledger}" "${baseUrl}" 405 | else 406 | local baseUrl="http://${ledger}.vonx.io" 407 | scanBCovrin "${did}" "${ledger}" "${baseUrl}" 408 | fi 409 | 410 | if isNotNullOrEmpty "${parsedResult}"; then 411 | break; 412 | fi 413 | done 414 | } 415 | 416 | function scanBCovrin() { 417 | local did=${1} 418 | ledger=${2} 419 | local baseUrl=${3} 420 | 421 | echo -e "\e[1A\e[KScanning ${ledger} for ${did} ..." 422 | result=$(curl -G -m ${BCOVRIN_QUERY_TIMEOUT} -s "${baseUrl}/ledger/domain" \ 423 | -H 'Cache-Control: no-cache, no-store' \ 424 | -H "accept: application/json" \ 425 | --data-urlencode "page=1" \ 426 | --data-urlencode "page_size=10" \ 427 | --data-urlencode "type=1" \ 428 | --data-urlencode "query=${did}") 429 | getBCovrinData "${did}" "${result}" 430 | 431 | if isNotNullOrEmpty "${parsedResult}"; then 432 | getRole "${parsedResult}" 433 | # Get last used date from the most recent txn associated with the DID 434 | result=$(curl -G -m ${BCOVRIN_QUERY_TIMEOUT} -s "${baseUrl}/ledger/domain" \ 435 | -H "accept: application/json" \ 436 | --data-urlencode "page=1" \ 437 | --data-urlencode "page_size=100" \ 438 | --data-urlencode "type=" \ 439 | --data-urlencode "query=${did}") 440 | getLastUsedInfo_BCovrin "${result}" 441 | else 442 | role="${UNKNOWN}" 443 | ledger="${UNKNOWN}" 444 | lastUsedDate="${UNKNOWN}" 445 | lastUsedFor="${UNKNOWN}" 446 | fi 447 | } 448 | 449 | function scanLedger() { 450 | local did=${1} 451 | ledger=${2} 452 | local baseUrl=${3} 453 | 454 | echo -e "\e[1A\e[KScanning ${ledger} for ${did} ..." 455 | result=$(curl -G -m ${QUERY_TIMEOUT} -s "${baseUrl}/api/networks/${ledger}/ledgers/domain/txs" \ 456 | -H "accept: application/json" \ 457 | --data-urlencode "filterTxNames=[\"NYM\"]" \ 458 | --data-urlencode "format=expansion" \ 459 | --data-urlencode "sortFromRecent=true" \ 460 | --data-urlencode "search=${did}") 461 | getData "${did}" "${result}" 462 | 463 | if isNotNullOrEmpty "${parsedResult}"; then 464 | getRole "${parsedResult}" 465 | # Get last used date from the most recent txn associated with the DID 466 | result=$(curl -G -m ${QUERY_TIMEOUT} -s "${baseUrl}/api/networks/${ledger}/ledgers/domain/txs" \ 467 | -H "accept: application/json" \ 468 | --data-urlencode "size=1" \ 469 | --data-urlencode "format=expansion" \ 470 | --data-urlencode "sortFromRecent=true" \ 471 | --data-urlencode "search=${did}") 472 | getLastUsedInfo "${result}" 473 | else 474 | role="${UNKNOWN}" 475 | ledger="${UNKNOWN}" 476 | lastUsedDate="${UNKNOWN}" 477 | lastUsedFor="${UNKNOWN}" 478 | fi 479 | } 480 | 481 | function getData() { 482 | local did=${1} 483 | local result=${2} 484 | jqQuery="[.[].idata.txn.data | select(.dest==\"${did}\")][0]" 485 | parsedResult=$(echo "${result}" | jq --raw-output "${jqQuery}") 486 | } 487 | 488 | function getBCovrinData() { 489 | local did=${1} 490 | local result=${2} 491 | jqQuery="[.results[].txn.data | select(.dest==\"${did}\")][0]" 492 | parsedResult=$(echo "${result}" | jq --raw-output "${jqQuery}") 493 | } 494 | 495 | function getRole() { 496 | local parsedResult=${1} 497 | role=$(echo "${parsedResult}" | jq --raw-output '.role') 498 | if [ ${role} == "101" ]; then 499 | role="Endorser" 500 | fi 501 | } 502 | 503 | function getLastUsedInfo() { 504 | local result=${1} 505 | lastUsedDate=$(echo "${result}" | jq --raw-output '.[0].idata.txnMetadata.txnTime') 506 | lastUsedFor=$(echo "${result}" | jq --raw-output '.[0].idata.txn.typeName') 507 | } 508 | 509 | function getLastUsedInfo_BCovrin() { 510 | local result=${1} 511 | 512 | lastUsedTimestamp=$(echo "${result}" | jq --raw-output '.results[-1].txnMetadata.txnTime') 513 | lastUsedDate=$(date -d @${lastUsedTimestamp} --utc +%Y-%m-%dT%T.%3NZ) 514 | lastUsedForTxnTypeCd=$(echo "${result}" | jq --raw-output '.results[-1].txn.type') 515 | lastUsedFor=${txnMap[${lastUsedForTxnTypeCd}]} 516 | } 517 | 518 | function isNotNullOrEmpty() { 519 | local parsedResult=${1} 520 | if [ ! -z "${parsedResult}" ] && [ "${parsedResult}" != "null" ]; then 521 | return 0 522 | else 523 | return 1 524 | fi 525 | } 526 | 527 | function isLedgerUnknow() { 528 | local leger=${1} 529 | if [ "${leger}" == "${UNKNOWN}" ]; then 530 | return 0 531 | else 532 | return 1 533 | fi 534 | } -------------------------------------------------------------------------------- /bin/getDids.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | export MSYS_NO_PATHCONV=1 3 | SCRIPT_HOME="$( cd "$( dirname "$0" )" && pwd )" 4 | 5 | # ================================================================================================================= 6 | # Usage: 7 | # ----------------------------------------------------------------------------------------------------------------- 8 | usage() { 9 | cat <<-EOF 10 | Tool to for getting did information from a cluster. This tool is specific to 11 | projects using Hyperledger Indy and Aries. 12 | 13 | Usage: ${0} command [options] 14 | 15 | Commands: 16 | 17 | dids [-f ] [-d ] [--no-ledger-scan] [--hyperlink] [--no-clean] 18 | - Get a list of dids for all the projects in a cluster. 19 | Options: 20 | -f : The keyword to filter the list on. For example SOVRIN_STAGINGNET to return only results from Sovrin StagingNet, 21 | or a99fd4-prod to return only results from the a99fd4-prod namespace in OCP. 22 | -d : Defines the target cluster 23 | --no-ledger-scan: Do not scan ledgers for the DID. This can be used to save some time if the informaiton about the DID from 24 | the ledgers is not needed. 25 | --hyperlink: Generate a hyperlink for the agent name. Turns the agent name into a clickable link when viewing the csv in excel. 26 | Does not format well for console output. 27 | --no-clean: Skip the cleanup of the von-network environment. Useful if you are planning mutiple runs. 28 | Examples: 29 | ${0} dids 30 | ${0} dids -f SOVRIN 31 | ${0} dids -d api-silver-devops-gov-bc-ca:6443 -f SOVRIN 32 | EOF 33 | } 34 | 35 | # ================================================================================================================= 36 | # Process the local command line arguments and pass everything else along. 37 | # - The 'getopts' options string must start with ':' for this to work. 38 | # ----------------------------------------------------------------------------------------------------------------- 39 | while [ ${OPTIND} -le $# ]; do 40 | if getopts :-: FLAG; then 41 | 42 | # echo ${FLAG} 43 | # echo ${OPTARG} 44 | 45 | case ${FLAG} in 46 | # List of local options: 47 | 48 | # Pass any switches ... 49 | - ) switches+=" --${OPTARG}" ;; 50 | 51 | # Pass unrecognized options ... 52 | \?) pass+=" -${OPTARG}" ;; 53 | esac 54 | else 55 | # Pass unrecognized arguments ... 56 | pass+=" ${!OPTIND}" 57 | let OPTIND++ 58 | fi 59 | done 60 | 61 | # Pass the unrecognized arguments along for further processing ... 62 | shift $((OPTIND-1)) 63 | set -- "$@" $(echo -e "${pass}" | sed -e 's/^[[:space:]]*//') 64 | # ================================================================================================================= 65 | 66 | # ----------------------------------------------------------------------------------------------------------------- 67 | # Define hook scripts: 68 | # - These must be defined before the main settings script 'settings.sh' is loaded. 69 | # ----------------------------------------------------------------------------------------------------------------- 70 | onRequiredOptionsExist() { 71 | ( 72 | # No required options ... 73 | return 0 74 | ) 75 | } 76 | 77 | onUsesCommandLineArguments() { 78 | ( 79 | # This script is expecting command line arguments to be passed ... 80 | return 0 81 | ) 82 | } 83 | 84 | # ----------------------------------------------------------------------------------------------------------------- 85 | # Initialization: 86 | # ----------------------------------------------------------------------------------------------------------------- 87 | # Load the project settings and functions ... 88 | _includeFile="ocFunctions.inc" 89 | _getDidsInc="getDids.inc" 90 | _settingsFile="settings.sh" 91 | if [ ! -z $(type -p ${_includeFile}) ]; then 92 | _includeFilePath=$(type -p ${_includeFile}) 93 | export OCTOOLSBIN=$(dirname ${_includeFilePath}) 94 | 95 | if [ -f ${OCTOOLSBIN}/${_settingsFile} ]; then 96 | . ${OCTOOLSBIN}/${_settingsFile} 97 | fi 98 | 99 | if [ -f ${OCTOOLSBIN}/${_includeFile} ]; then 100 | . ${OCTOOLSBIN}/${_includeFile} 101 | fi 102 | 103 | if [ -f ${OCTOOLSBIN}/${_getDidsInc} ]; then 104 | . ${OCTOOLSBIN}/${_getDidsInc} 105 | fi 106 | else 107 | _red='\033[0;31m' 108 | _yellow='\033[1;33m' 109 | _nc='\033[0m' # No Color 110 | echo -e \\n"${_red}${_includeFile} could not be found on the path.${_nc}" 111 | echo -e "${_yellow}Please ensure the openshift-developer-tools are installed on and registered on your path.${_nc}" 112 | echo -e "${_yellow}https://github.com/BCDevOps/openshift-developer-tools${_nc}" 113 | fi 114 | # ============================================================================== 115 | 116 | _cmd=$(toLower ${1-dids}) 117 | shift 118 | 119 | case "${_cmd}" in 120 | dids) 121 | getDids ${@} ${switches} 122 | ;; 123 | 124 | *) 125 | echoWarning "Unrecognized command; ${_cmd}" 126 | globalUsage 127 | ;; 128 | esac -------------------------------------------------------------------------------- /bin/getPodByName.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # ============================================================================================================================== 4 | usage () { 5 | echo "========================================================================================" 6 | echo "Gets the name of a running pod instance by name and optionally by index." 7 | echo "----------------------------------------------------------------------------------------" 8 | echo "Usage:" 9 | echo 10 | echo "${0} [PodIndex]" 11 | echo "Where is the name of the pod, and [PodIndex] is the index of the pod instance." 12 | echo 13 | echo "Example: ${0} django" 14 | echo "========================================================================================" 15 | exit 1 16 | } 17 | # ============================================================================================================================== 18 | 19 | if [ -z "${1}" ]; then 20 | usage 21 | fi 22 | 23 | POD_INSTANCE_NAME=$(oc get pods -l "name=${1}" --template "{{ with index .items ${2:-0} }}{{ .metadata.name }}{{ end }}" --ignore-not-found) 24 | echo ${POD_INSTANCE_NAME} 25 | 26 | 27 | -------------------------------------------------------------------------------- /bin/getRouteInfo.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | export MSYS_NO_PATHCONV=1 3 | SCRIPT_HOME="$( cd "$( dirname "$0" )" && pwd )" 4 | 5 | # ================================================================================================================= 6 | # Usage: 7 | # ----------------------------------------------------------------------------------------------------------------- 8 | usage() { 9 | cat <<-EOF 10 | Tool to for getting route infomration from a cluster. 11 | 12 | Usage: ${0} command [options] 13 | 14 | Commands: 15 | 16 | routes [-f ] [-d ] 17 | - Get a list of routes (host names) for all the projects in a cluster 18 | Options: 19 | -f : Filters the results by hostname 20 | -d : Defines the target cluster 21 | Examples: 22 | ${0} routes 23 | ${0} routes -f vonx.io 24 | ${0} routes -d api-silver-devops-gov-bc-ca:6443 -f vonx.io 25 | 26 | allowLists [-f ] [-d ] 27 | - Get a list of routes (host names) for all the projects in a cluster 28 | Options: 29 | -f : Filters the search on namespace. 30 | -d : Defines the target cluster 31 | Examples: 32 | ${0} allowLists 33 | ${0} allowLists -f prod 34 | ${0} allowLists -d api-silver-devops-gov-bc-ca:6443 -f prod 35 | 36 | EOF 37 | } 38 | 39 | # ================================================================================================================= 40 | # Process the local command line arguments and pass everything else along. 41 | # - The 'getopts' options string must start with ':' for this to work. 42 | # ----------------------------------------------------------------------------------------------------------------- 43 | while [ ${OPTIND} -le $# ]; do 44 | if getopts : FLAG; then 45 | case ${FLAG} in 46 | # List of local options: 47 | 48 | # Pass unrecognized options ... 49 | \?) pass+=" -${OPTARG}" ;; 50 | esac 51 | else 52 | # Pass unrecognized arguments ... 53 | pass+=" ${!OPTIND}" 54 | let OPTIND++ 55 | fi 56 | done 57 | 58 | # Pass the unrecognized arguments along for further processing ... 59 | shift $((OPTIND-1)) 60 | set -- "$@" $(echo -e "${pass}" | sed -e 's/^[[:space:]]*//') 61 | # ================================================================================================================= 62 | 63 | # ----------------------------------------------------------------------------------------------------------------- 64 | # Define hook scripts: 65 | # - These must be defined before the main settings script 'settings.sh' is loaded. 66 | # ----------------------------------------------------------------------------------------------------------------- 67 | onRequiredOptionsExist() { 68 | ( 69 | # No required options ... 70 | return 0 71 | ) 72 | } 73 | 74 | onUsesCommandLineArguments() { 75 | ( 76 | # This script is expecting command line arguments to be passed ... 77 | return 0 78 | ) 79 | } 80 | 81 | # ----------------------------------------------------------------------------------------------------------------- 82 | # Initialization: 83 | # ----------------------------------------------------------------------------------------------------------------- 84 | # Load the project settings and functions ... 85 | _includeFile="ocFunctions.inc" 86 | _settingsFile="settings.sh" 87 | if [ ! -z $(type -p ${_includeFile}) ]; then 88 | _includeFilePath=$(type -p ${_includeFile}) 89 | export OCTOOLSBIN=$(dirname ${_includeFilePath}) 90 | 91 | if [ -f ${OCTOOLSBIN}/${_settingsFile} ]; then 92 | . ${OCTOOLSBIN}/${_settingsFile} 93 | fi 94 | 95 | if [ -f ${OCTOOLSBIN}/${_includeFile} ]; then 96 | . ${OCTOOLSBIN}/${_includeFile} 97 | fi 98 | else 99 | _red='\033[0;31m' 100 | _yellow='\033[1;33m' 101 | _nc='\033[0m' # No Color 102 | echo -e \\n"${_red}${_includeFile} could not be found on the path.${_nc}" 103 | echo -e "${_yellow}Please ensure the openshift-developer-tools are installed on and registered on your path.${_nc}" 104 | echo -e "${_yellow}https://github.com/BCDevOps/openshift-developer-tools${_nc}" 105 | fi 106 | # ============================================================================== 107 | 108 | _cmd=$(toLower ${1-routes}) 109 | shift 110 | 111 | case "${_cmd}" in 112 | routes) 113 | getRoutes ${@} 114 | ;; 115 | 116 | allowlists) 117 | getAllowLists ${@} 118 | ;; 119 | 120 | *) 121 | echoWarning "Unrecognized command; ${_cmd}" 122 | globalUsage 123 | ;; 124 | esac -------------------------------------------------------------------------------- /bin/grantDeploymentPrivileges.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | # ============================================================================== 6 | usage() { 7 | cat < -t 14 | 15 | Options: 16 | -h prints the usage for the script 17 | -x run the script in debug mode to see what's happening 18 | ================================================================================ 19 | EOF 20 | exit 1 21 | } 22 | 23 | if [ -f ${OCTOOLSBIN}/ocFunctions.inc ]; then 24 | . ${OCTOOLSBIN}/ocFunctions.inc 25 | fi 26 | 27 | # ------------------------------------------------------------------------------ 28 | # In case you wanted to check what variables were passed 29 | # echo "flags = $*" 30 | while getopts p:t:s:xh FLAG; do 31 | case $FLAG in 32 | p ) TARGET_PROJECT_NAME=$OPTARG ;; 33 | t ) TOOLS_PROJECT_NAME=$OPTARG ;; 34 | s ) SERVICE_ACCOUNT_NAME=$OPTARG ;; 35 | x ) export DEBUG=1 ;; 36 | h ) usage ;; 37 | \?) #unrecognized option - show help 38 | echo -e \\n"Invalid script option"\\n 39 | usage 40 | ;; 41 | esac 42 | done 43 | 44 | # Shift the parameters in case there any more to be used 45 | shift $((OPTIND-1)) 46 | # echo Remaining arguments: $@ 47 | 48 | if [ ! -z "${DEBUG}" ]; then 49 | set -x 50 | fi 51 | 52 | if [ -z "${TARGET_PROJECT_NAME}" ] || [ -z "${TOOLS_PROJECT_NAME}" ]; then 53 | echo -e \\n"Missing parameters - target project name, tools project name" 54 | usage 55 | fi 56 | 57 | export SERVICE_ACCOUNT_NAME=${SERVICE_ACCOUNT_NAME:-default} 58 | 59 | # ============================================================================== 60 | 61 | echo "Granting deployment configuration access from '${TARGET_PROJECT_NAME}', to '${TOOLS_PROJECT_NAME}', for service account '${SERVICE_ACCOUNT_NAME}' ..." 62 | assignRole \ 63 | system:image-puller \ 64 | system:serviceaccount:${TARGET_PROJECT_NAME}:${SERVICE_ACCOUNT_NAME} \ 65 | ${TOOLS_PROJECT_NAME} 66 | echo 67 | -------------------------------------------------------------------------------- /bin/initOSProjects.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | # Setup artifactory/docker pull creds 6 | USE_PULL_CREDS=${USE_PULL_CREDS:-true} 7 | CRED_SEARCH_NAMES=${CRED_SEARCH_NAMES} 8 | PULL_CREDS=${PULL_CREDS:-artifactory-creds} 9 | DOCKER_REG=${DOCKER_REG:-docker-remote.artifacts.developer.gov.bc.ca} 10 | PROMPT_CREDS=${PROMPT_CREDS:-false} 11 | if [ -z ${CRED_ENVS} ]; then 12 | CRED_ENVS="tools dev test prod" 13 | fi 14 | 15 | # =================================================================================== 16 | usage() { #Usage function 17 | cat <<-EOF 18 | Tool to initialize a set of BC Government standard OpenShift projects. 19 | 20 | Usage: 21 | ${0##*/} [options] 22 | EOF 23 | } 24 | # ------------------------------------------------------------------------------ 25 | 26 | if [ -f ${OCTOOLSBIN}/settings.sh ]; then 27 | . ${OCTOOLSBIN}/settings.sh 28 | fi 29 | 30 | if [ -f ${OCTOOLSBIN}/ocFunctions.inc ]; then 31 | . ${OCTOOLSBIN}/ocFunctions.inc 32 | fi 33 | # =================================================================================== 34 | 35 | # Iterate through Dev, Test and Prod projects granting permissions, etc. 36 | for project in ${PROJECT_NAMESPACE}-${DEV} ${PROJECT_NAMESPACE}-${TEST} ${PROJECT_NAMESPACE}-${PROD}; do 37 | 38 | grantDeploymentPrivileges.sh \ 39 | -p ${project} \ 40 | -t ${TOOLS} 41 | exitOnError 42 | 43 | echo -e \\n"Granting ${JENKINS_SERVICE_ACCOUNT_ROLE} role to ${JENKINS_SERVICE_ACCOUNT_NAME} in ${project}" 44 | assignRole ${JENKINS_SERVICE_ACCOUNT_ROLE} ${JENKINS_SERVICE_ACCOUNT_NAME} ${project} 45 | exitOnError 46 | 47 | done 48 | 49 | # Search for the default artifactory credentials. The naming convention changed at one point. 50 | if [ -z ${CRED_SEARCH_NAMES} ]; then 51 | CRED_SEARCH_NAMES="artifacts-default default-${PROJECT_NAMESPACE}" 52 | fi 53 | 54 | buildPullSecret "${USE_PULL_CREDS}" "${CRED_SEARCH_NAMES}" "${PULL_CREDS}" "${DOCKER_REG}" "${PROMPT_CREDS}" "${CRED_ENVS[@]}" 55 | -------------------------------------------------------------------------------- /bin/installCertificate.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | usage() { 6 | cat <<-EOF 7 | Tool to process automate the installation of certificates on OpenShift routes. 8 | 9 | Usage: ${0} [ Options ] 10 | 11 | Examples: 12 | Using a config file (recommended): 13 | - ${0} -p bc-tob -f certificate.conf 14 | 15 | Using discrete parameters: 16 | - ${0} -p bc-tob -e dev -r test -c server.crt -k private.key 17 | 18 | Options: 19 | ======== 20 | -f ; The path to a config file containing a list of comma separated entries containing the 21 | parameters needed to install certificates on one or more routes. 22 | 23 | File content must be in the form: 24 | projectName,routeName,certFilename,pkFilename 25 | 26 | Example file: 27 | # ========================================================= 28 | # List the projects, routes, and certificates you want to 29 | # install. 30 | # 31 | # The entries must be in the following form: 32 | # - projectName,routeName,certFilename,pkFilename 33 | # -------------------------------------------------------- 34 | devex-von-bc-tob-dev,test,Combined.crt,private.key 35 | devex-von-bc-tob-prod,test2,/c/tmp/Combined.crt,/c/tmp/private.key 36 | 37 | -r ; The name of the route on which to install the certificate. 38 | 39 | -c ; The path to the certificate file. 40 | 41 | -k ; The path to the private key for the certificate. 42 | EOF 43 | } 44 | 45 | # ================================================================================================================= 46 | # Process the local command line arguments and pass everything else along. 47 | # - The 'getopts' options string must start with ':' for this to work. 48 | # ----------------------------------------------------------------------------------------------------------------- 49 | while [ ${OPTIND} -le $# ]; do 50 | if getopts :f:r:c:k: FLAG; then 51 | case ${FLAG} in 52 | # List of local options: 53 | f ) configFile=$OPTARG ;; 54 | r ) routeName=$OPTARG ;; 55 | c ) certFilename=$OPTARG ;; 56 | k ) pkFilename=$OPTARG ;; 57 | 58 | # Pass unrecognized options ... 59 | \?) pass+=" -${OPTARG}" ;; 60 | esac 61 | else 62 | # Pass unrecognized arguments ... 63 | pass+=" ${!OPTIND}" 64 | let OPTIND++ 65 | fi 66 | done 67 | 68 | # Pass the unrecognized arguments along for further processing ... 69 | shift $((OPTIND-1)) 70 | set -- "$@" $(echo -e "${pass}" | sed -e 's/^[[:space:]]*//') 71 | # ================================================================================================================= 72 | 73 | if [ -f ${OCTOOLSBIN}/settings.sh ]; then 74 | . ${OCTOOLSBIN}/settings.sh 75 | fi 76 | 77 | if [ -f ${OCTOOLSBIN}/ocFunctions.inc ]; then 78 | . ${OCTOOLSBIN}/ocFunctions.inc 79 | fi 80 | # ============================================================================== 81 | 82 | projectName=$(getProjectName) 83 | 84 | if [ ! -z ${configFile} ] && [ -f ${configFile} ]; then 85 | routes=$(readConf ${configFile}) 86 | elif [ ! -z ${projectName} ] && [ ! -z ${routeName} ] && [ ! -z ${certFilename} ] && [ ! -z ${pkFilename} ]; then 87 | routes=${projectName},${routeName},${certFilename},${pkFilename} 88 | else 89 | echo -e \\n"Missing one or more required parameters:"\\n 90 | echo -e " - configFile: ${configFile}" 91 | echo -e " - projectName: ${projectName}" 92 | echo -e " - routeName: ${routeName}" 93 | echo -e " - certFilename: ${certFilename}" 94 | echo -e " - pkFilename: ${pkFilename}"\\n 95 | usage 96 | fi 97 | 98 | installCertificates "${projectName}" "${routes}" -------------------------------------------------------------------------------- /bin/loadData.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | usage() { #Usage function 6 | cat <<-EOF 7 | Tool to load data from the APISpec/TestData folder into the app 8 | 9 | Usage: 10 | ${0##*/} [options] 11 | 12 | Options: 13 | ======== 14 | -e load data into the specified server (default: ${LOAD_DATA_SERVER}, Options: local/dev/test/prod/) 15 | EOF 16 | } 17 | 18 | # ================================================================================================================= 19 | # Process the local command line arguments and pass everything else along. 20 | # - The 'getopts' options string must start with ':' for this to work. 21 | # ----------------------------------------------------------------------------------------------------------------- 22 | while [ ${OPTIND} -le $# ]; do 23 | if getopts :e: FLAG; then 24 | case ${FLAG} in 25 | # List of local options: 26 | e ) LOAD_DATA_SERVER=$OPTARG ;; 27 | 28 | # Pass unrecognized options ... 29 | \?) pass+=" -${OPTARG}" ;; 30 | esac 31 | else 32 | # Pass unrecognized arguments ... 33 | pass+=" ${!OPTIND}" 34 | let OPTIND++ 35 | fi 36 | done 37 | 38 | # Pass the unrecognized arguments along for further processing ... 39 | shift $((OPTIND-1)) 40 | set -- "$@" $(echo -e "${pass}" | sed -e 's/^[[:space:]]*//') 41 | # ================================================================================================================= 42 | 43 | if [ -f ${OCTOOLSBIN}/settings.sh ]; then 44 | . ${OCTOOLSBIN}/settings.sh 45 | fi 46 | 47 | # Load Test Data 48 | echo -e "Loading data into TheOrgBook Server: ${LOAD_DATA_SERVER}" 49 | pushd ../APISpec/TestData >/dev/null 50 | ./load-all.sh ${LOAD_DATA_SERVER} 51 | popd >/dev/null 52 | -------------------------------------------------------------------------------- /bin/oc-cluster-down.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # ============================================================================== 4 | # Script for taking down an OpenShift cluter in Docker for Windows 5 | # 6 | # * Requires the OpenShift Origin CLI 7 | # ------------------------------------------------------------------------------ 8 | # 9 | # Usage: 10 | # 11 | # oc-cluster-down.sh 12 | # 13 | # ============================================================================== 14 | 15 | oc cluster down -------------------------------------------------------------------------------- /bin/oc-cluster-up.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # ============================================================================== 4 | # Script for setting up an OpenShift cluter in Docker for Windows 5 | # 6 | # * Requires the OpenShift Origin CLI 7 | # 8 | # todo: 9 | # Path based on platform; 10 | # Windows; /var/lib/origin/data 11 | # MAC; /private/var/lib/origin/data 12 | # ------------------------------------------------------------------------------ 13 | # 14 | # Usage: 15 | # 16 | # oc-cluster-up.sh 17 | # 18 | # ============================================================================== 19 | export MSYS_NO_PATHCONV=1 20 | oc cluster up --metrics=true --host-data-dir=/var/lib/origin/data --use-existing-config -------------------------------------------------------------------------------- /bin/oc-pull-image.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | # ================================================================================================================= 6 | # Usage: 7 | # ----------------------------------------------------------------------------------------------------------------- 8 | usage() { 9 | cat <<-EOF 10 | A helper script to pull images from an OpenShift docker registry. 11 | 12 | Usage: ${0} [ -h -x -r ] -i -n ] 13 | 14 | OPTIONS: 15 | ======== 16 | -i The name of the image to pull. 17 | -n The namespace of the OpenShift project. 18 | For example devex-von-tools 19 | -r Optional. The address of the OpenShift docker registry, 20 | such as your local registry, for example 172.30.1.1:5000. 21 | Defaults to docker-registry.pathfinder.gov.bc.ca 22 | 23 | -h prints the usage for the script 24 | -x run the script in debug mode to see what's happening 25 | 26 | EOF 27 | exit 28 | } 29 | 30 | # ----------------------------------------------------------------------------------------------------------------- 31 | # Initialization: 32 | # ----------------------------------------------------------------------------------------------------------------- 33 | while getopts i:n:r:hx FLAG; do 34 | case $FLAG in 35 | i ) export DOCKER_IMAGE=$OPTARG ;; 36 | n ) export OPENSHIFT_NAMESPACE=$OPTARG ;; 37 | r ) export OPENSHIFT_REGISTRY_ADDRESS=$OPTARG ;; 38 | x ) export DEBUG=1 ;; 39 | h ) usage ;; 40 | \? ) #unrecognized option - show help 41 | echo -e \\n"Invalid script option: -${OPTARG}"\\n 42 | usage 43 | ;; 44 | esac 45 | done 46 | 47 | # Shift the parameters in case there any more to be used 48 | shift $((OPTIND-1)) 49 | # echo Remaining arguments: $@ 50 | 51 | if [ ! -z "${DEBUG}" ]; then 52 | set -x 53 | fi 54 | 55 | if [ -z "${DOCKER_IMAGE}" ] || [ -z "${OPENSHIFT_NAMESPACE}" ]; then 56 | echo -e \\n"Missing parameters - Name of Docker image, OpenShift Namespace"\\n 57 | usage 58 | fi 59 | 60 | if [ -z "${OPENSHIFT_REGISTRY_ADDRESS}" ]; then 61 | OPENSHIFT_REGISTRY_ADDRESS=docker-registry.pathfinder.gov.bc.ca 62 | fi 63 | 64 | OPENSHIFT_IMAGE_SNIPPET=${DOCKER_IMAGE##*/} 65 | OPENSHIFT_IMAGESTREAM_PATH=${OPENSHIFT_REGISTRY_ADDRESS}/${OPENSHIFT_NAMESPACE}/${OPENSHIFT_IMAGE_SNIPPET} 66 | # ================================================================================================================= 67 | 68 | docker login ${OPENSHIFT_REGISTRY_ADDRESS} -u $(oc whoami) -p $(oc whoami -t) 69 | docker pull ${OPENSHIFT_IMAGESTREAM_PATH} 70 | -------------------------------------------------------------------------------- /bin/oc-push-image.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | # ================================================================================================================= 6 | # Usage: 7 | # ----------------------------------------------------------------------------------------------------------------- 8 | usage() { 9 | cat <<-EOF 10 | A helper script to push images to an OpenShift docker registry. 11 | 12 | Usage: ${0} [ -h -x -r ] -i -n ] 13 | 14 | OPTIONS: 15 | ======== 16 | -i The name of the image to push. 17 | -n The namespace of the OpenShift project. 18 | For example devex-von-tools 19 | -r Optional. The address of the OpenShift docker registry, 20 | such as your local registry, for example 172.30.1.1:5000. 21 | Defaults to docker-registry.pathfinder.gov.bc.ca 22 | 23 | -h prints the usage for the script 24 | -x run the script in debug mode to see what's happening 25 | 26 | EOF 27 | exit 28 | } 29 | 30 | # ----------------------------------------------------------------------------------------------------------------- 31 | # Initialization: 32 | # ----------------------------------------------------------------------------------------------------------------- 33 | while getopts i:n:r:hx FLAG; do 34 | case $FLAG in 35 | i ) export DOCKER_IMAGE=$OPTARG ;; 36 | n ) export OPENSHIFT_NAMESPACE=$OPTARG ;; 37 | r ) export OPENSHIFT_REGISTRY_ADDRESS=$OPTARG ;; 38 | x ) export DEBUG=1 ;; 39 | h ) usage ;; 40 | \? ) #unrecognized option - show help 41 | echo -e \\n"Invalid script option: -${OPTARG}"\\n 42 | usage 43 | ;; 44 | esac 45 | done 46 | 47 | # Shift the parameters in case there any more to be used 48 | shift $((OPTIND-1)) 49 | # echo Remaining arguments: $@ 50 | 51 | if [ ! -z "${DEBUG}" ]; then 52 | set -x 53 | fi 54 | 55 | if [ -z "${DOCKER_IMAGE}" ] || [ -z "${OPENSHIFT_NAMESPACE}" ]; then 56 | echo -e \\n"Missing parameters - name of Docker Image, OpenShift Namespace"\\n 57 | usage 58 | fi 59 | 60 | if [ -z "${OPENSHIFT_REGISTRY_ADDRESS}" ]; then 61 | OPENSHIFT_REGISTRY_ADDRESS=docker-registry.pathfinder.gov.bc.ca 62 | fi 63 | 64 | OPENSHIFT_IMAGE_SNIPPET=${DOCKER_IMAGE##*/} 65 | OPENSHIFT_IMAGESTREAM_PATH=${OPENSHIFT_REGISTRY_ADDRESS}/${OPENSHIFT_NAMESPACE}/${OPENSHIFT_IMAGE_SNIPPET} 66 | # ================================================================================================================= 67 | 68 | docker tag ${DOCKER_IMAGE} ${OPENSHIFT_IMAGESTREAM_PATH} 69 | docker login ${OPENSHIFT_REGISTRY_ADDRESS} -u $(oc whoami) -p $(oc whoami -t) 70 | docker push ${OPENSHIFT_IMAGESTREAM_PATH} 71 | -------------------------------------------------------------------------------- /bin/oc-rsync.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | # ================================================================================================================= 6 | # Usage: 7 | # ----------------------------------------------------------------------------------------------------------------- 8 | usage() { 9 | cat <<-EOF 10 | A helper script to copy files and folders to and from pods running in OpenShift. 11 | Accepts the friendly or full name of a pod. 12 | 13 | Usage: 14 | ${0} [ -h -x] -s -d 15 | 16 | OPTIONS: 17 | ======== 18 | -s The source path. 19 | -d The destination path. 20 | 21 | -h prints the usage for the script 22 | -x run the script in debug mode to see what's happening 23 | 24 | Examples: 25 | # Copy local directory to a pod directory 26 | ${0} -s /home/user/source -d devpod:/src 27 | 28 | # Copy pod directory to a local directory 29 | ${0} -s devpod:/src -d /home/user/source 30 | EOF 31 | exit 32 | } 33 | # ----------------------------------------------------------------------------------------------------------------- 34 | # Funtions: 35 | # ----------------------------------------------------------------------------------------------------------------- 36 | resolvePodPath () { 37 | _path=${1} 38 | if [ -z "${_path}" ]; then 39 | echo -e \\n"resolvePodPath; Missing parameter!"\\n 40 | exit 1 41 | fi 42 | 43 | if [[ ${_path} = *":"* ]]; then 44 | _podName=$(echo ${_path} | sed 's~\(^.*\):.*$~\1~') 45 | _podPath=$(echo ${_path} | sed 's~^.*:\(.*$\)~\1~') 46 | _podInstanceName=$(getPodByName.sh ${_podName}) 47 | _path="${_podInstanceName}:${_podPath}" 48 | fi 49 | 50 | echo ${_path} 51 | } 52 | # ----------------------------------------------------------------------------------------------------------------- 53 | # Initialization: 54 | # ----------------------------------------------------------------------------------------------------------------- 55 | while getopts s:d:hx FLAG; do 56 | case $FLAG in 57 | s ) SOURCE=$OPTARG ;; 58 | d ) DESTINATION=$OPTARG ;; 59 | x ) export DEBUG=1 ;; 60 | h ) usage ;; 61 | \? ) #unrecognized option - show help 62 | echo -e \\n"Invalid script option: -${OPTARG}"\\n 63 | usage 64 | ;; 65 | esac 66 | done 67 | 68 | shift $((OPTIND-1)) 69 | 70 | if [ ! -z "${DEBUG}" ]; then 71 | set -x 72 | fi 73 | 74 | if [ -z "${SOURCE}" ] || [ -z "${DESTINATION}" ]; then 75 | echo -e \\n"Missing parameters ..."\\n 76 | usage 77 | fi 78 | 79 | SOURCE=$(resolvePodPath ${SOURCE}) 80 | DESTINATION=$(resolvePodPath ${DESTINATION}) 81 | # ================================================================================================================= 82 | 83 | echo "Copying ${SOURCE} to ${DESTINATION} ..." 84 | oc rsync ${SOURCE} ${DESTINATION} -------------------------------------------------------------------------------- /bin/overrides.inc: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # ================================================================================================================ 4 | # Usage 5 | # ---------------------------------------------------------------------------------------------------------------- 6 | # This is the base script for ALL *.overrides.sh implementations. It contains uility and initialization functions 7 | # for your *.overrides.sh script. 8 | # 9 | # To use this script, add the following code to the TOP of your *.overrides.sh script: 10 | # 11 | # _includeFile=$(type -p overrides.inc) 12 | # if [ ! -z ${_includeFile} ]; then 13 | # . ${_includeFile} 14 | # else 15 | # _red='\033[0;31m'; _yellow='\033[1;33m'; _nc='\033[0m'; echo -e \\n"${_red}overrides.inc could not be found on the path.${_nc}\n${_yellow}Please ensure the openshift-developer-tools are installed on and registered on your path.${_nc}\n${_yellow}https://github.com/BCDevOps/openshift-developer-tools${_nc}"; exit 1; 16 | # fi 17 | # ================================================================================================================ 18 | 19 | # ================================================================================================================ 20 | # Functions 21 | # ---------------------------------------------------------------------------------------------------------------- 22 | toLower() { 23 | echo $(echo ${@} | tr '[:upper:]' '[:lower:]') 24 | } 25 | 26 | printStatusMsg(){ 27 | ( 28 | _msg=${1} 29 | _yellow='\033[1;33m' 30 | _nc='\033[0m' # No Color 31 | printf "\n${_yellow}${_msg}\n${_nc}" >&2 32 | ) 33 | } 34 | 35 | getOperation() { 36 | ( 37 | echo $(toLower ${OPERATION}) 38 | ) 39 | } 40 | 41 | createOperation() { 42 | ( 43 | action=$(getOperation) 44 | if [ ${action} = "create" ]; then 45 | return 0 46 | else 47 | return 1 48 | fi 49 | ) 50 | } 51 | 52 | updateOperation() { 53 | ( 54 | action=$(getOperation) 55 | if [ ${action} = "update" ]; then 56 | return 0 57 | else 58 | return 1 59 | fi 60 | ) 61 | } 62 | 63 | readParameter(){ 64 | ( 65 | _msg=${1} 66 | _paramName=${2} 67 | _defaultValue=${3} 68 | _encode=${4} 69 | 70 | _yellow='\033[1;33m' 71 | _nc='\033[0m' # No Color 72 | _message=$(echo -e "\n${_yellow}${_msg}\n${_nc}") 73 | 74 | read -r -p $"${_message}" ${_paramName} 75 | 76 | writeParameter "${_paramName}" "${_defaultValue}" "${_encode}" 77 | ) 78 | } 79 | 80 | writeParameter(){ 81 | ( 82 | _paramName=${1} 83 | _defaultValue=${2} 84 | _encode=$(toLower ${3}) 85 | 86 | if [ ! -z "${_encode}" ] && [ "${_encode}" = "true" ] ; then 87 | # The key/value pair must be contained on a single line 88 | _encodedValue=$(echo -n "${!_paramName:-${_defaultValue}}"|base64 -w 0) 89 | echo "${_paramName}=${_encodedValue}" >> ${_overrideParamFile} 90 | else 91 | echo "${_paramName}=${!_paramName:-${_defaultValue}}" >> ${_overrideParamFile} 92 | fi 93 | ) 94 | } 95 | 96 | parseHostnameParameter(){ 97 | ( 98 | _sourceParamName=${1} 99 | _destParamName=${2} 100 | # Parses the host name from the url contained in the source parameter, and writes the value into the destination parameter. 101 | eval ${_destParamName}=$(getHostname $(getParameter ${_sourceParamName})) 102 | printStatusMsg "Parsing ${_destParamName} from ${_sourceParamName}; '$(getParameter ${_sourceParamName})' => '${!_destParamName}' ..." 103 | writeParameter "${_destParamName}" "${!_destParamName}" "false" 104 | ) 105 | } 106 | 107 | getParameter(){ 108 | ( 109 | _paramName=${1} 110 | # Reads a parameter from the override param file. 111 | echo $(grep ${_paramName} $"${_overrideParamFile}" | awk -F= '{print $2}') 112 | ) 113 | } 114 | 115 | getHostname(){ 116 | ( 117 | _url=${1} 118 | # Extract the host name from a url. 119 | echo $(echo ${_url} | awk -F[/:] '{print $4}') 120 | ) 121 | } 122 | 123 | generateKey(){ 124 | ( 125 | _length=${1:-48} 126 | # Format can be `-base64` or `-hex` 127 | _format=${2:--base64} 128 | 129 | echo $(openssl rand ${_format} ${_length}) 130 | ) 131 | } 132 | 133 | generateSeed(){ 134 | ( 135 | _prefix=${1} 136 | _seed=$(echo "${_prefix}$(generateKey 32)" | fold -w 32 | head -n 1 ) 137 | _seed=$(echo -n "${_seed}") 138 | echo ${_seed} 139 | ) 140 | } 141 | 142 | generateUsername() { 143 | # Generate a random username ... 144 | _userName=User_$( generateKey | LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1 ) 145 | _userName=$(echo -n "${_userName}") 146 | echo ${_userName} 147 | } 148 | 149 | generatePassword() { 150 | # Generate a random password ... 151 | _password=$( generateKey | LC_CTYPE=C tr -dc 'a-zA-Z0-9_' | fold -w 20 | head -n 1 ) 152 | _password=$(echo -n "${_password}") 153 | echo ${_password} 154 | } 155 | 156 | generateProfileConfigMap() { 157 | _profile=${1} 158 | _deployment_env_name=${2} 159 | _config_map_name=${3} 160 | _config_source_directory=${4} 161 | _output_format=${5} 162 | _output_file=${6} 163 | 164 | printStatusMsg "Combining profile configuration files ..." 165 | 166 | # =========================================================================================================================== 167 | # Combine the profile's default config files with its environment specific config files before generating the config map ... 168 | # --------------------------------------------------------------------------------------------------------------------------- 169 | configRoot=${_config_source_directory} # Default configuration files here 170 | configEnv=${configRoot}/${_deployment_env_name} # Environment specific configuration files here 171 | profileRoot=${configRoot}/${_profile} # Profile specific default configuration files here 172 | profileEnv=${profileRoot}/${_deployment_env_name} # Environment specific profile configuration files here 173 | profileTmp=${profileRoot}/tmp # Temp dir to contain the combined configuration files 174 | mkdir -p ${profileTmp} 175 | 176 | printStatusMsg "Copying default configuration files from ${configRoot} to ${profileTmp} ..." 177 | cp -f ${configRoot}/* ${profileTmp} 2>/dev/null 178 | printStatusMsg "Copying environment specific configuration files from ${configEnv} to ${profileTmp} ..." 179 | cp -f ${configEnv}/* ${profileTmp} 2>/dev/null 180 | printStatusMsg "Copying profile specific default configuration files from ${profileRoot} to ${profileTmp} ..." 181 | cp -f ${profileRoot}/* ${profileTmp} 2>/dev/null 182 | printStatusMsg "Copying environment specific profile configuration files from ${profileEnv} to ${profileTmp} ..." 183 | cp -f ${profileEnv}/* ${profileTmp} 2>/dev/null 184 | # =========================================================================================================================== 185 | 186 | printStatusMsg "Generating ConfigMap; ${_config_map_name} ..." 187 | generateConfigMap "${_config_map_name}" "${profileTmp}" "${_output_format}" "${_output_file}" 188 | 189 | # Remove temporary configuration directory and files ... 190 | printStatusMsg "Cleaning up ..." 191 | rm -rfd ${profileTmp} 192 | } 193 | 194 | generateConfigMap() { 195 | _config_map_name=${1} 196 | _source_file=${2} 197 | _output_format=${3} 198 | _output_file=${4} 199 | if [ -z "${_config_map_name}" ] || [ -z "${_source_file}" ] || [ -z "${_output_format}" ] || [ -z "${_output_file}" ]; then 200 | echo -e \\n"generateConfigMap; Missing parameter!"\\n 201 | exit 1 202 | fi 203 | 204 | # Auto-detect env files .... 205 | extension="${_source_file##*.}" 206 | if [ ! -z "${extension}" ] && [[ "${extension}" == "env" ]]; then 207 | _from_file_type="--from-env-file" 208 | else 209 | _from_file_type="--from-file" 210 | fi 211 | 212 | oc create configmap ${_config_map_name} ${_from_file_type} ${_source_file} --dry-run=client -o ${_output_format} > ${_output_file} 213 | } 214 | 215 | initialize(){ 216 | # Define the name of the override param file. 217 | _scriptName=$(basename ${0%.*}) 218 | export _overrideParamFile=${_scriptName}.param 219 | 220 | printStatusMsg "Initializing ${_scriptName} ..." 221 | 222 | # Remove any previous version of the file ... 223 | if [ -f ${_overrideParamFile} ]; then 224 | printStatusMsg "Removing previous copy of ${_overrideParamFile} ..." 225 | rm -f ${_overrideParamFile} 226 | fi 227 | } 228 | # ================================================================================================================ 229 | 230 | # ================================================================================================================ 231 | # Initialization ... 232 | # ---------------------------------------------------------------------------------------------------------------- 233 | initialize 234 | # ================================================================================================================ -------------------------------------------------------------------------------- /bin/processPipelines.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | if [ -z "${SETTINGS_LOADED}" ] && [ -f ${OCTOOLSBIN}/settings.sh ]; then 6 | . ${OCTOOLSBIN}/settings.sh 7 | fi 8 | 9 | if [ -f ${OCTOOLSBIN}/ocFunctions.inc ]; then 10 | . ${OCTOOLSBIN}/ocFunctions.inc 11 | fi 12 | 13 | # Turn on debugging if asked 14 | if [ ! -z "${DEBUG}" ]; then 15 | set -x 16 | fi 17 | # ================================================================================================================= 18 | 19 | # Get list of all of the Jenkinsfiles in the project ... 20 | JENKINS_FILES=$(getJenkinsFiles) 21 | 22 | # echo "Jenkins files:" 23 | # for _jenkinsFile in ${JENKINS_FILES}; do 24 | # echo ${_jenkinsFile} 25 | # done 26 | # exit 1 27 | 28 | # Local params file path MUST be relative...Hack! 29 | _localParamsDir=openshift 30 | 31 | # Process the pipeline for each one ... 32 | for _jenkinsFile in ${JENKINS_FILES}; do 33 | echo -e \\n"Processing Jenkins Pipeline; ${_jenkinsFile} ..." 34 | 35 | _template="${PIPELINE_JSON}" 36 | _defaultParams=$(getPipelineParameterFileOutputPath "${_jenkinsFile}") 37 | _output="${_jenkinsFile}-pipeline${BUILD_CONFIG_SUFFIX}" 38 | 39 | if [ ! -z "${APPLY_LOCAL_SETTINGS}" ]; then 40 | _localParams=$(getPipelineParameterFileOutputPath "${_jenkinsFile}" "${_localParamsDir}") 41 | fi 42 | 43 | if [ -f "${_defaultParams}" ]; then 44 | _defaultParams="--param-file=${_defaultParams}" 45 | else 46 | _defaultParams="" 47 | fi 48 | 49 | if [ -f "${_localParams}" ]; then 50 | _localParams="--param-file=${_localParams}" 51 | else 52 | _localParams="" 53 | fi 54 | 55 | oc -n ${TOOLS} process --local --filename=${_template} ${_localParams} ${_defaultParams} > ${_output} 56 | exitOnError 57 | done 58 | 59 | if [ -z ${GEN_ONLY} ]; then 60 | echo -e \\n"Deploying pipeline configuration files ..." 61 | deployBuildConfigs 62 | fi 63 | 64 | # Delete the configuration files if the keep command line option was not specified. 65 | if [ -z "${KEEPJSON}" ]; then 66 | echo -e \\n"Removing temporary pipeline configuration files ..." 67 | cleanBuildConfigs 68 | fi 69 | -------------------------------------------------------------------------------- /bin/runInContainer.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | # ============================================================================================================================== 6 | usage () { 7 | echo "========================================================================================" 8 | echo "Runs one-off commands inside the container of a pod." 9 | echo 10 | echo "You can accomplish the same results by using regular commands from OpenShift." 11 | echo "This script is just wrapping calls to 'oc exec' to make it a little more" 12 | echo "convenient to use. In the future, the 'oc' cli tool might incorporate changes" 13 | echo "that make this script obsolete." 14 | echo 15 | echo "Related GitHub issues:" 16 | echo "- https://github.com/GoogleCloudPlatform/kubernetes/issues/8876" 17 | echo "- https://github.com/openshift/origin/issues/2001" 18 | echo 19 | echo "----------------------------------------------------------------------------------------" 20 | echo "Usage:" 21 | echo 22 | echo "${0} [PodIndex] \"\"" 23 | echo 24 | echo "Where:" 25 | echo " - is the name of the pod." 26 | echo " - [PodIndex] is the index of the pod instance, and is optional." 27 | echo " - '' is the command to run on the pod." 28 | echo " It's a good idea to wrap the command in quotes as shown." 29 | echo " You may need to use single or double quotes depending on the command." 30 | echo " Any additional quotes in the command may need to be escaped." 31 | echo " See examples for details." 32 | echo 33 | echo "Examples:" 34 | echo "----------------------------------------------------------------------------------------" 35 | echo "Database Information:" 36 | echo "${0} postgresql 'psql -c \"\l\"'" 37 | echo "${0} postgresql 'psql -c \"\du\"'" 38 | echo 39 | echo "Drop and recreate database; Explicitly:" 40 | echo "${0} postgresql \"psql -c 'DROP DATABASE \"TheOrgBook_Database\";'\"" 41 | echo "${0} postgresql \"psql -c 'CREATE DATABASE \"TheOrgBook_Database\";'\"" 42 | echo "${0} postgresql \"psql -c 'GRANT ALL ON DATABASE \"TheOrgBook_Database\" TO \"TheOrgBook_User\";'\"" 43 | echo 44 | echo "Drop and recreate database; Dynamically using environment variables:" 45 | echo "${0} postgresql 'psql -ac \"DROP DATABASE \\\"\${POSTGRESQL_DATABASE}\\\";\"'" 46 | echo "${0} postgresql 'psql -ac \"CREATE DATABASE \\\"\${POSTGRESQL_DATABASE}\\\";\"'" 47 | echo "${0} postgresql 'psql -ac \"GRANT ALL ON DATABASE \\\"\${POSTGRESQL_DATABASE}\\\" TO \\\"\${POSTGRESQL_USER}\\\";\"'" 48 | echo 49 | echo "Running Python commands:" 50 | echo "${0} django 'python ./manage.py migrate'" 51 | echo "${0} django 'python ./manage.py createsuperuser'" 52 | echo "${0} django 'python ./manage.py shell'" 53 | echo "${0} django 'python ./manage.py rebuild_index --noinput'" 54 | echo "========================================================================================" 55 | exit 1 56 | } 57 | 58 | exitOnError () { 59 | rtnCd=$? 60 | if [ ${rtnCd} -ne 0 ]; then 61 | echo "An error has occurred while attempting to run a command in a pod! Please check the previous output message(s) for details." 62 | exit ${rtnCd} 63 | fi 64 | } 65 | # ============================================================================================================================== 66 | 67 | if [ -z "${1}" ]; then 68 | usage 69 | elif [ -z "${2}" ]; then 70 | usage 71 | elif [ ! -z "${4}" ]; then 72 | usage 73 | else 74 | POD_NAME=${1} 75 | fi 76 | 77 | if [ ! -z "${3}" ]; then 78 | POD_INDEX=${2} 79 | COMMAND=${3} 80 | else 81 | POD_INDEX="0" 82 | COMMAND=${2} 83 | fi 84 | 85 | # Get name of a currently deployed pod by label and index 86 | POD_INSTANCE_NAME=$(getPodByName.sh ${POD_NAME} ${POD_INDEX}) 87 | exitOnError 88 | 89 | echo 90 | echo "Executing command on ${POD_INSTANCE_NAME}:" 91 | echo -e "\t${COMMAND:-echo Hello}" 92 | echo 93 | 94 | # Run command in a container of the specified pod: 95 | oc exec "$POD_INSTANCE_NAME" -- bash -c "${COMMAND:-echo Hello}" 96 | exitOnError 97 | -------------------------------------------------------------------------------- /bin/scaleDeployment.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # ============================================================================================================================== 4 | usage () { 5 | echo "========================================================================================" 6 | echo "Scales the number of pods for a deployment to the set number." 7 | echo "----------------------------------------------------------------------------------------" 8 | echo "Usage:" 9 | echo 10 | echo "${0} " 11 | echo "Where is the name of the deployment configuration, and " 12 | echo "is the number of pods to scale to." 13 | echo 14 | echo "Example: ${0} django 0" 15 | echo "To scale to 0 pods." 16 | echo "========================================================================================" 17 | exit 1 18 | } 19 | 20 | exitOnError () { 21 | rtnCd=$? 22 | if [ ${rtnCd} -ne 0 ]; then 23 | echo "An error has occurred while getting the name of the pod! Please check the previous output message(s) for details." 24 | exit ${rtnCd} 25 | fi 26 | } 27 | # ============================================================================================================================== 28 | 29 | if [ -z "${1}" ]; then 30 | usage 31 | fi 32 | 33 | if [ -z "${2}" ]; then 34 | usage 35 | fi 36 | 37 | if [ ! -z "${3}" ]; then 38 | usage 39 | fi 40 | 41 | oc scale --replicas=${2} dc ${1} 42 | exitOnError 43 | -------------------------------------------------------------------------------- /bin/settings.sh: -------------------------------------------------------------------------------- 1 | # git bash hack on windows - deals with pathname conversions from dos to unix-style 2 | export MSYS_NO_PATHCONV=1 3 | 4 | # ================================================================================================================= 5 | # Functions: 6 | # ----------------------------------------------------------------------------------------------------------------- 7 | globalUsage() { 8 | # Print the scripts useage first ... 9 | usage 10 | 11 | cat <<-EOF 12 | 13 | Global Commands: 14 | 15 | listProfiles 16 | - Get a list of profiles for the project. 17 | Example: 18 | $0 -e null listProfiles 19 | $0 -p myprofile -e null listProfiles 20 | 21 | profileDetails 22 | - Get the details of the project's profile(s). Lists the templates associated to the profile. 23 | Example: 24 | $0 -e null profileDetails - List the details for the project's only profile. 25 | $0 -p myprofile -e null profileDetails - List the details for the 'myprofile' profile. 26 | $0 -p default -e null profileDetails all - List the details for all of the profiles in the project. 27 | 28 | listBuildRefs 29 | - Get a list of your project's build configurations and source code references. 30 | Example: 31 | $0 -p default -e null listBuildRefs 32 | 33 | Global Options: 34 | - Note: Local script options will override these global options. 35 | ======== 36 | -h prints the usage for the script 37 | 38 | -n 39 | Overrides your project and environmnet namespace with a fully qualified namespace. 40 | -e 41 | The environment (dev/test/prod) into which you are deploying (default: ${DEPLOYMENT_ENV_NAME}) 42 | -c 43 | To generate parameters for templates of a specific component 44 | -l apply local settings and parameters 45 | -p 46 | Load a specific settings profile; setting..sh 47 | -P Use the default settings profile; settings.sh. Use this flag to ignore all but the default 48 | settings profile when there is more than one settings profile defined for a project. 49 | -k keep the json produced by processing the template 50 | -g process the templates and generate the configuration files, but do not create or update them 51 | automatically set the -k option 52 | -u update OpenShift deployment configs instead of creating the configs 53 | -x run the script in debug mode to see what's happening 54 | 55 | Update settings.sh and settings.local.sh files to set defaults 56 | EOF 57 | exit 1 58 | } 59 | 60 | echoWarning (){ 61 | _msg=${@} 62 | _yellow='\033[1;33m' 63 | _nc='\033[0m' # No Color 64 | echo -e "${_yellow}${_msg}${_nc}" >&2 65 | } 66 | 67 | getProfiles() { 68 | _profiles=$(find . -name "settings*.sh" | sed "s~^.*settings.~~;s~.sh~~;s~^sh~${_defaultProfileName}~") 69 | echo "${_profiles}" 70 | } 71 | 72 | countProfiles() { 73 | echo "${#}" 74 | } 75 | 76 | profilesSettingsExist() { 77 | ( 78 | _profiles=$(getProfiles) 79 | _profileCount=$(countProfiles $(echo "${_profiles}" | sed "s~^${_defaultProfileName}~~;s~^${_localProfileName}~~")) 80 | 81 | if (( ${_profileCount} >= 1 )); then 82 | return 0 83 | else 84 | return 1 85 | fi 86 | ) 87 | } 88 | 89 | profileExists() 90 | { 91 | _profile=${1} 92 | _profiles=$(getProfiles) 93 | if [[ ${_profiles} == *${_profile}* ]]; then 94 | return 0 95 | else 96 | return 1 97 | fi 98 | } 99 | 100 | printProfiles() { 101 | _profiles=$(getProfiles) 102 | _count=$(countProfiles ${_profiles}) 103 | if [[ ${_count} -eq 1 ]]; then 104 | _profileDescription="profile" 105 | else 106 | _profileDescription="profiles" 107 | fi 108 | 109 | echoWarning "\n==================================================================================================" 110 | echoWarning "Warning:" 111 | echoWarning "Your project contains ${_count} ${_profileDescription}." 112 | echoWarning "Please select the profile you wish to use." 113 | echoWarning "--------------------------------------------------------------------------------------------------" 114 | listProfileDetails ${_profiles} 115 | echoWarning "==================================================================================================" 116 | } 117 | 118 | listProfileDetails() { 119 | local OPTIND 120 | local unset _verbose 121 | while getopts v FLAG; do 122 | case $FLAG in 123 | v ) local _verbose=1 ;; 124 | esac 125 | done 126 | shift $((OPTIND-1)) 127 | 128 | _profiles=${@} 129 | 130 | for _profile in ${_profiles}; do 131 | settingsFile=$(echo ${_settingsFileName}.${_profile}${_settingsFileExt} | sed "s~.${_defaultProfileName}~~") 132 | echoWarning "${_profile} - ${settingsFile}" 133 | 134 | description=$(cat ${settingsFile} | sed -n 's~^.*description[=:][[:space:]]*\(.*\)$~\1~pI') 135 | if [ ! -z "${description}" ]; then 136 | echo " - ${description}" 137 | fi 138 | 139 | if [ ! -z "${_verbose}" ]; then 140 | # Override the current profile settings ... 141 | export PROFILE=${_profile} 142 | export SKIP_PIPELINE_PROCESSING= 143 | export ignore_templates= 144 | export include_templates= 145 | 146 | # Always load the default profile settings ... 147 | . ${PWD}/${_settingsFileName}${_settingsFileExt} 148 | 149 | # Then load the desired profile settings ... 150 | . ${PWD}/${settingsFile} 151 | 152 | # List the templates for the profile ... 153 | templates=$(getBuildTemplates $(getTemplateDir) 2>/dev/null) 154 | echo -e " Build Templates:" 155 | for template in ${templates}; do 156 | echo " - ${template}" 157 | done 158 | 159 | templates=$(getDeploymentTemplates $(getTemplateDir) 2>/dev/null) 160 | echo -e "\n Deployment Templates:" 161 | for template in ${templates}; do 162 | echo " - ${template}" 163 | done 164 | 165 | templates=$(getConfigTemplates $(getTemplateDir) 2>/dev/null) 166 | echo -e "\n Configuration Templates:" 167 | for template in ${templates}; do 168 | echo " - ${template}" 169 | done 170 | fi 171 | done 172 | } 173 | 174 | printSettingsFileNotFound() { 175 | echoWarning "\n==================================================================================================" 176 | echoWarning "Warning:" 177 | echoWarning "--------------------------------------------------------------------------------------------------" 178 | echoWarning "No project settings file (${_settingsFileName}${_settingsFileExt}) was found in '${PWD}'." 179 | echoWarning "Make sure you're running the script from your project's top level 'openshift' directory" 180 | echoWarning "and you have a 'settings.sh' file in that folder." 181 | echoWarning "==================================================================================================" 182 | } 183 | 184 | printLocalSettingsFileNotFound() { 185 | echo -e "\n==================================================================================================" 186 | echo "Information:" 187 | echo "--------------------------------------------------------------------------------------------------" 188 | echo "You've specified you want to apply local profile settings, but no local profile settings" 189 | echo "(${_settingsFileName}.${_localProfileName}${_settingsFileExt}) were found in '${PWD}'." 190 | echo "This is a great way to apply overrides when you are generating local parameter files." 191 | echo "==================================================================================================" 192 | } 193 | 194 | printProfileNotFound() { 195 | _profiles=$(getProfiles) 196 | _count=$(countProfiles ${_profiles}) 197 | 198 | echoWarning "\n==================================================================================================" 199 | echoWarning "Warning:" 200 | echoWarning "--------------------------------------------------------------------------------------------------" 201 | echoWarning "The selected settings profile (${_settingsFileName}.${PROFILE}${_settingsFileExt}) does not exist." 202 | echoWarning "Please select from one of the available profiles." 203 | echoWarning "--------------------------------------------------------------------------------------------------" 204 | for _profile in ${_profiles}; do 205 | echoWarning "$(echo "${_profile} - ${_settingsFileName}.${_profile}${_settingsFileExt}" | sed "s~.${_defaultProfileName}~~")" 206 | done 207 | echoWarning "==================================================================================================" 208 | } 209 | 210 | validateSettings() { 211 | unset _error 212 | 213 | if [ -z "${IGNORE_PROFILES}" ] && [ -z "${PROFILE}" ] && profilesSettingsExist; then 214 | _error=0 215 | printProfiles 216 | fi 217 | 218 | # Load settings in order 219 | # 1. settings.sh 220 | if [ ! ${_error} ]; then 221 | if profileExists "${_defaultProfileName}"; then 222 | _settingsFiles="${_settingsFiles} ./${_settingsFileName}${_settingsFileExt}" 223 | else 224 | _error=0 225 | printSettingsFileNotFound 226 | fi 227 | fi 228 | 229 | # 2. settings.${PROFILE}.sh 230 | if [ ! ${_error} ] && [ -z "${IGNORE_PROFILES}" ]; then 231 | if [ ! -z "${PROFILE}" ] && [ "${PROFILE}" != "${_defaultProfileName}" ] && profileExists ${PROFILE}; then 232 | _settingsFiles="${_settingsFiles} ./${_settingsFileName}.${PROFILE}${_settingsFileExt}" 233 | elif [ ! -z "${PROFILE}" ] && [ "${PROFILE}" != "${_defaultProfileName}" ]; then 234 | _error=0 235 | printProfileNotFound 236 | fi 237 | fi 238 | 239 | # 3. settings.local.sh 240 | if [ ! ${_error} ]; then 241 | if [ ! -z "${APPLY_LOCAL_SETTINGS}" ] && profileExists "${_localProfileName}"; then 242 | _settingsFiles="${_settingsFiles} ./${_settingsFileName}.${_localProfileName}${_settingsFileExt}" 243 | elif [ ! -z "${APPLY_LOCAL_SETTINGS}" ]; then 244 | _error=0 245 | printLocalSettingsFileNotFound 246 | fi 247 | fi 248 | 249 | export SETTINGS_FILES=${_settingsFiles} 250 | 251 | if [ ! ${_error} ]; then 252 | return 0 253 | else 254 | return 1 255 | fi 256 | } 257 | 258 | settingsLoaded() { 259 | ( 260 | if [ -z "${SETTINGS_LOADED}" ]; then 261 | return 1 262 | else 263 | return 0 264 | fi 265 | ) 266 | } 267 | 268 | ensureRequiredOptionsExist() { 269 | ( 270 | # Call the 'onRequiredOptionsExist' hook if defined ... 271 | if type onRequiredOptionsExist &>/dev/null; then 272 | if onRequiredOptionsExist; then 273 | return 0 274 | else 275 | return 1 276 | fi 277 | else 278 | return 0 279 | fi 280 | ) 281 | } 282 | 283 | usesCommandLineArguments() { 284 | ( 285 | # Call the 'onUsesCommandLineArguments' hook if defined ... 286 | if type onUsesCommandLineArguments &>/dev/null; then 287 | if onUsesCommandLineArguments; then 288 | return 0 289 | else 290 | return 1 291 | fi 292 | else 293 | return 1 294 | fi 295 | ) 296 | } 297 | # ================================================================================================================= 298 | 299 | # ================================================================================================================= 300 | # Main Script: 301 | # ----------------------------------------------------------------------------------------------------------------- 302 | if ! settingsLoaded; then 303 | export _componentSettingsFileName=component.settings.sh 304 | _settingsFileName="settings" 305 | _settingsFileExt=".sh" 306 | _localProfileName="local" 307 | export _defaultProfileName="default" 308 | 309 | # ================================================================================================================= 310 | # Process the command line arguments: 311 | # - In case you wanted to check what variables were passed; echo "flags = $*" 312 | # ----------------------------------------------------------------------------------------------------------------- 313 | OPTIND=1 314 | unset pass 315 | while [ ${OPTIND} -le $# ]; do 316 | if getopts :p:Pc:e:lukxhgn: FLAG; then 317 | case $FLAG in 318 | h ) globalUsage ;; 319 | c ) export COMP=$OPTARG ;; 320 | p ) export PROFILE=$OPTARG ;; 321 | P ) export IGNORE_PROFILES=1 ;; 322 | e ) export DEPLOYMENT_ENV_NAME=$OPTARG ;; 323 | l ) export APPLY_LOCAL_SETTINGS=1 ;; 324 | u ) export OPERATION=update ;; 325 | k ) export KEEPJSON=1 ;; 326 | x ) export DEBUG=1 ;; 327 | g ) 328 | export KEEPJSON=1 329 | export GEN_ONLY=1 330 | ;; 331 | n ) 332 | export FULLY_QUALIFIED_NAMESPACE=$OPTARG 333 | echoWarning "Overriding project namespace with fully qualified value, '${FULLY_QUALIFIED_NAMESPACE}' ..." 334 | ;; 335 | 336 | # Collect unrecognized options ... 337 | \?) pass+=" -${OPTARG}" ;; 338 | esac 339 | else 340 | globalArgument=$(echo "${!OPTIND}" | tr '[:upper:]' '[:lower:]') 341 | case "${globalArgument}" in 342 | profiledetails|listprofiles|listbuildrefs) 343 | _globalCmd=${globalArgument} 344 | ;; 345 | *) 346 | # Pass unrecognized arguments ... 347 | pass+=" ${!OPTIND}" 348 | ;; 349 | esac 350 | let OPTIND++ 351 | fi 352 | done 353 | # Pass the unrecognized arguments along for further processing ... 354 | shift $((OPTIND-1)) 355 | set -- "$@" $(echo -e "${pass}" | sed -e 's/^[[:space:]]*//') 356 | OPTIND=1 357 | unset pass 358 | 359 | if [[ ! -z "${@}" ]] && ! usesCommandLineArguments; then 360 | echoWarning "\nUnexpected command line argument(s) were supplied; [${@}]." 361 | echoWarning "If your script is expecting these argument(s) you can turn off the warning by implementing the 'onUsesCommandLineArguments' hook in your script before the main settings script is loaded. The hook should return 0 if you are expecting arguments." 362 | fi 363 | # ================================================================================================================= 364 | 365 | if [ ! -z "${DEBUG}" ]; then 366 | set -x 367 | fi 368 | 369 | if ! ensureRequiredOptionsExist; then 370 | globalUsage 371 | fi 372 | 373 | if validateSettings; then 374 | # Load settings ... 375 | _settingsFiles=${SETTINGS_FILES} 376 | echo -e \\n"Loading settings ..." 377 | for _settingsFile in ${_settingsFiles}; do 378 | echo -e "Loading settings from ${PWD}/${_settingsFile##*/} ..." 379 | . ${_settingsFile} 380 | done 381 | else 382 | echoWarning \\n"Your settings are invalid. Please review the previous messages to correct the errors." 383 | exit 1 384 | fi 385 | 386 | # =========================================================================================================== 387 | # Default settings, many of which you will never need to override. 388 | # ----------------------------------------------------------------------------------------------------------- 389 | # Project Variables 390 | export PROJECT_DIR=${PROJECT_DIR:-..} 391 | 392 | export OPERATION=${OPERATION:-create} 393 | export DEV=${DEV:-dev} 394 | export TEST=${TEST:-test} 395 | export PROD=${PROD:-prod} 396 | 397 | export TOOLS=${TOOLS:-${PROJECT_NAMESPACE}-tools} 398 | export DEPLOYMENT_ENV_NAME=${DEPLOYMENT_ENV_NAME:-${DEPLOYMENT_ENV_NAME:-${DEV}}} 399 | export BUILD_ENV_NAME=${BUILD_ENV_NAME:-tools} 400 | export LOAD_DATA_SERVER=${LOAD_DATA_SERVER:-local} 401 | export TEMPLATE_DIR=${TEMPLATE_DIR:-templates} 402 | export PIPELINE_JSON=${PIPELINE_JSON:-https://raw.githubusercontent.com/BCDevOps/openshift-tools/master/provisioning/pipeline/resources/pipeline-build.json} 403 | export COMPONENT_JENKINSFILE=${COMPONENT_JENKINSFILE:-../Jenkinsfile} 404 | export PIPELINEPARAM=${PIPELINEPARAM:-pipeline.param} 405 | export APPLICATION_DOMAIN_POSTFIX=${APPLICATION_DOMAIN_POSTFIX:-.apps.silver.devops.gov.bc.ca} 406 | 407 | # Jenkins account settings for initialization 408 | export JENKINS_ACCOUNT_NAME=${JENKINS_ACCOUNT_NAME:-jenkins} 409 | export JENKINS_SERVICE_ACCOUNT_NAME=${JENKINS_SERVICE_ACCOUNT_NAME:-system:serviceaccount:${TOOLS}:${JENKINS_ACCOUNT_NAME}} 410 | export JENKINS_SERVICE_ACCOUNT_ROLE=${JENKINS_SERVICE_ACCOUNT_ROLE:-edit} 411 | 412 | # Gluster settings for initialization 413 | export GLUSTER_ENDPOINT_CONFIG=${GLUSTER_ENDPOINT_CONFIG:-https://raw.githubusercontent.com/BCDevOps/openshift-tools/master/resources/glusterfs-cluster-app-endpoints.yml} 414 | export GLUSTER_SVC_CONFIG=${GLUSTER_SVC_CONFIG:-https://raw.githubusercontent.com/BCDevOps/openshift-tools/master/resources/glusterfs-cluster-app-service.yml} 415 | export GLUSTER_SVC_NAME=${GLUSTER_SVC_NAME:-glusterfs-cluster-app} 416 | 417 | 418 | # Build and deployment settings 419 | export DEPLOYMENT_CONFIG_SUFFIX="_DeploymentConfig.json" 420 | export BUILD_CONFIG_SUFFIX="_BuildConfig.json" 421 | export OVERRIDE_PARAM_SUFFIX="overrides.param" 422 | 423 | # =========================================================================================================== 424 | 425 | # =========================================================================================================== 426 | # Settings you should supply in your project settings file 427 | # ----------------------------------------------------------------------------------------------------------- 428 | export PROJECT_NAMESPACE=${PROJECT_NAMESPACE:-""} 429 | 430 | # The templates that should not have their GIT referances(uri and ref) over-ridden 431 | # Templates NOT in this list will have they GIT referances over-ridden 432 | # with the values of GIT_URI and GIT_REF 433 | export skip_git_overrides=${skip_git_overrides:-""} 434 | export GIT_URI=${GIT_URI:-""} 435 | export GIT_REF=${GIT_REF:-"master"} 436 | 437 | # The project components 438 | # - defaults to the support the Simple Project Structure 439 | export components=${components:-"."} 440 | 441 | # The builds to be triggered after buildconfigs created (not auto-triggered) 442 | export builds=${builds:-""} 443 | 444 | # The images to be tagged after build 445 | export images=${images:-""} 446 | 447 | # The routes for the project 448 | export routes=${routes:-""} 449 | # =========================================================================================================== 450 | 451 | # =========================================================================================================== 452 | # Test for important parameters. 453 | # Throw errors/warnings if they are not defined. 454 | # ----------------------------------------------------------------------------------------------------------- 455 | # ToDo: 456 | # - Fill in this section. 457 | # =========================================================================================================== 458 | export SETTINGS_LOADED=1 459 | 460 | if [ ! -z "${_globalCmd}" ]; then 461 | # Requires ocFunctions.inc to be loaded ... 462 | if [ -z "${OC_FUNCTIONS_LOADED}" ]; then 463 | . ocFunctions.inc 464 | fi 465 | 466 | pushd ${SCRIPT_HOME} >/dev/null 467 | case "${_globalCmd}" in 468 | profiledetails) 469 | commandArgs=${@} 470 | if [ ! -z "${commandArgs}" ] && [ "${commandArgs}" == "all" ]; then 471 | profiles=$(getProfiles) 472 | else 473 | profiles=${PROFILE} 474 | fi 475 | 476 | echo 477 | listProfileDetails -v "${profiles}" 478 | ;; 479 | 480 | listprofiles) 481 | echo 482 | listProfileDetails $(getProfiles) 483 | ;; 484 | 485 | listbuildrefs) 486 | listBuildRefs 487 | ;; 488 | 489 | *) 490 | echoWarning "Unrecognized global command; ${_globalCmd}" 491 | globalUsage 492 | ;; 493 | esac 494 | popd >/dev/null 495 | exit 0 496 | fi 497 | else 498 | echo "Settings already loaded ..." 499 | fi 500 | # ================================================================================================================= -------------------------------------------------------------------------------- /bin/tagProjectImages.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | usage() { 6 | cat <<-EOF 7 | Tags the project's images 8 | 9 | Usage: 10 | ${0##*/} [options] -s -t 11 | 12 | Options: 13 | ======== 14 | -s the source tag name 15 | -t the tag to apply 16 | EOF 17 | } 18 | 19 | # ================================================================================================================= 20 | # Process the local command line arguments and pass everything else along. 21 | # - The 'getopts' options string must start with ':' for this to work. 22 | # ----------------------------------------------------------------------------------------------------------------- 23 | while [ ${OPTIND} -le $# ]; do 24 | if getopts :s:t: FLAG; then 25 | case ${FLAG} in 26 | # List of local options: 27 | s) export SOURCE_TAG=$OPTARG ;; 28 | t) export DESTINATION_TAG=$OPTARG ;; 29 | 30 | # Pass unrecognized options ... 31 | \?) pass+=" -${OPTARG}" ;; 32 | esac 33 | else 34 | # Pass unrecognized arguments ... 35 | pass+=" ${!OPTIND}" 36 | let OPTIND++ 37 | fi 38 | done 39 | 40 | # Pass the unrecognized arguments along for further processing ... 41 | shift $((OPTIND-1)) 42 | set -- "$@" $(echo -e "${pass}" | sed -e 's/^[[:space:]]*//') 43 | # ================================================================================================================= 44 | 45 | if [ -f ${OCTOOLSBIN}/settings.sh ]; then 46 | . ${OCTOOLSBIN}/settings.sh 47 | fi 48 | 49 | if [ -f ${OCTOOLSBIN}/ocFunctions.inc ]; then 50 | . ${OCTOOLSBIN}/ocFunctions.inc 51 | fi 52 | 53 | if [ -z "${SOURCE_TAG}" ] || [ -z "${DESTINATION_TAG}" ]; then 54 | echo -e \\n"Missing parameters - source or destination tag"\\n 55 | usage 56 | fi 57 | # ============================================================================== 58 | 59 | echo -e \\n"Tagging images for ${DESTINATION_TAG} environment deployment ..." 60 | for image in ${images}; do 61 | echo -e \\n"Tagging ${image}:${SOURCE_TAG} as ${image}:${DESTINATION_TAG} ..." 62 | oc tag ${image}:${SOURCE_TAG} ${image}:${DESTINATION_TAG} -n ${TOOLS} 63 | exitOnError 64 | done 65 | -------------------------------------------------------------------------------- /bin/testConnection: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | CONNECTION_TIMEOUT=${CONNECTION_TIMEOUT:-1} 5 | 6 | # ================================================================================================================= 7 | # Usage: 8 | # ----------------------------------------------------------------------------------------------------------------- 9 | usage() { 10 | cat <<-EOF 11 | Tests to see if a port is open. The tests can be run locally or remotely on a pod running in OpenShift. 12 | 13 | Usage: $0 [ options ] [host:port] 14 | 15 | Examples: 16 | $0 google.com:80 17 | 18 | $0 -f ./listOfUrisToTest.txt 19 | 20 | $0 -n devex-von-tools -p django google.com:80 21 | 22 | $0 -n devex-von-tools -p django -f ./listOfUrisToTest.txt 23 | 24 | Standard Options: 25 | 26 | -g generate the command and print to the console without executing it 27 | 28 | -f read the connection list from a file 29 | Connections are expected to be listed as host:port, one per line 30 | 31 | -h prints the usage for the script 32 | 33 | OpenShift Option: 34 | -n The OpenShift namespace containing the pod 35 | -p The friendly name of the pod 36 | EOF 37 | exit 1 38 | } 39 | # ================================================================================================================= 40 | 41 | # ================================================================================================================= 42 | # Funtions: 43 | # ----------------------------------------------------------------------------------------------------------------- 44 | function readList(){ 45 | ( 46 | if [ -f ${listFile} ]; then 47 | # Read in the file minus any comments ... 48 | echo -e \\n"Reading list of hosts and ports from ${listFile} ...\\n" >&2 49 | _value=$(sed '/^[[:blank:]]*#/d;s/#.*//' ${listFile}) 50 | fi 51 | echo "${_value}" 52 | ) 53 | } 54 | 55 | # Load openshift-developer-tools functions ... 56 | _includeFile="ocFunctions.inc" 57 | if [ ! -z $(type -p ${_includeFile}) ]; then 58 | _includeFilePath=$(type -p ${_includeFile}) 59 | export OCTOOLSBIN=$(dirname ${_includeFilePath}) 60 | 61 | if [ -f ${OCTOOLSBIN}/${_includeFile} ]; then 62 | . ${OCTOOLSBIN}/${_includeFile} 63 | fi 64 | else 65 | _red='\033[0;31m' 66 | _yellow='\033[1;33m' 67 | _nc='\033[0m' # No Color 68 | echo -e \\n"${_red}${_includeFile} could not be found on the path.${_nc}" 69 | echo -e "${_yellow}Please ensure the openshift-developer-tools are installed on and registered on your path.${_nc}" 70 | echo -e "${_yellow}https://github.com/BCDevOps/openshift-developer-tools${_nc}" 71 | fi 72 | 73 | # Bash on MacOS does not have the timeout function. We'll check the OS and, if it's MacOS (Darwin) based, we will 74 | # create a timeout function using Perl. Perl function from this gist https://gist.github.com/jaytaylor/6527607. 75 | OS=$(uname | tr '[:upper:]' '[:lower:]') 76 | if [[ "$OS" == "darwin"* ]]; then 77 | function timeout() { perl -e 'alarm shift; exec @ARGV' "$@"; } 78 | fi 79 | 80 | # ================================================================================================================= 81 | 82 | # ================================================================================================================= 83 | # Initialization: 84 | # ----------------------------------------------------------------------------------------------------------------- 85 | while getopts f:n:p:gh FLAG; do 86 | case $FLAG in 87 | f) export listFile=$OPTARG ;; 88 | n) export FULLY_QUALIFIED_NAMESPACE=$OPTARG ;; 89 | p) export podName=$OPTARG ;; 90 | g) export GENERATE=1 ;; 91 | h ) usage ;; 92 | \?) #unrecognized option - show help 93 | echo -e \\n"Invalid script option"\\n 94 | usage 95 | ;; 96 | esac 97 | done 98 | shift $((OPTIND-1)) 99 | 100 | if [ ! -z "${listFile}" ]; then 101 | list=$(readList) 102 | else 103 | list=${@} 104 | fi 105 | 106 | if [ -z "${list}" ]; then 107 | echoError \\n"Missing parameters."\\n 108 | usage 109 | fi 110 | 111 | if [ ! -z "${FULLY_QUALIFIED_NAMESPACE}" ] || [ ! -z "${podName}" ]; then 112 | if [ -z "${FULLY_QUALIFIED_NAMESPACE}" ] || [ -z "${podName}" ]; then 113 | echoError \\n"When using any of the OpenShift options, you MUST specify them all."\\n 114 | usage 115 | else 116 | runInPod=1 117 | fi 118 | fi 119 | # ================================================================================================================= 120 | 121 | # ================================================================================================================= 122 | # Main Script 123 | # Inspired by; https://stackoverflow.com/questions/4922943/test-from-shell-script-if-remote-tcp-port-is-open/9463554 124 | # ----------------------------------------------------------------------------------------------------------------- 125 | for item in ${list}; do 126 | IFS=':' read -r -a segments <<< "${item}" && unset IFS 127 | host=${segments[0]} 128 | port=${segments[1]} 129 | 130 | _command+="echo -n ${host}:${port} && timeout ${CONNECTION_TIMEOUT} bash -c \"cat < /dev/null > /dev/tcp/${host}/${port}\" > /dev/null 2>&1 && echo ' - Open' || echo ' - Closed';" 131 | done 132 | 133 | if [ ! -z "${GENERATE}" ]; then 134 | echo -e \\n${_command} 135 | exit 0 136 | fi 137 | 138 | if [ -z "${runInPod}" ]; then 139 | echo -e "Testing connections from the local machine ..." 140 | eval ${_command} 141 | else 142 | echo -e "Testing connections from $(getProjectName)/${podName} ..." 143 | runInContainer \ 144 | "${podName}" \ 145 | "${_command}" 146 | fi 147 | -------------------------------------------------------------------------------- /bin/transferRoute.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | usage() { 6 | cat <<-EOF 7 | Tool to transfer existing OpenShift routes from one project to another. 8 | 9 | Usage: 10 | ${0##*/} [ Options ] 11 | 12 | Examples: 13 | Using a config file (recommended): 14 | - ${0##*/} -s devex-bcgov-dac-dev -d devex-von-bc-tob-prod -f routes.conf 15 | 16 | Using discrete parameters: 17 | - ${0##*/} -s devex-bcgov-dac-dev -d devex-von-bc-tob-prod -r www-orgbook 18 | 19 | Options: 20 | ======== 21 | -f ; The path to a config file containing a list of one or more routes to transfer. 22 | 23 | Example file: 24 | # ========================================================= 25 | # List the routes you want to transfer. 26 | # 27 | # The entries must be in the following form: 28 | # - routeName 29 | # -------------------------------------------------------- 30 | orgbook-pathfinder 31 | orgbook 32 | angular-on-nginx-tmp 33 | www-orgbook 34 | 35 | -r ; The name of the route to transfer. 36 | 37 | -s ; The name of the source project. 38 | 39 | -d ; The name of the destination project. 40 | EOF 41 | } 42 | 43 | # ================================================================================================================= 44 | # Process the local command line arguments and pass everything else along. 45 | # - The 'getopts' options string must start with ':' for this to work. 46 | # ----------------------------------------------------------------------------------------------------------------- 47 | while [ ${OPTIND} -le $# ]; do 48 | if getopts :s:d:f:r: FLAG; then 49 | case ${FLAG} in 50 | # List of local options: 51 | s ) fromProject=$OPTARG ;; 52 | d ) toProject=$OPTARG ;; 53 | f ) configFile=$OPTARG ;; 54 | r ) routeName=$OPTARG ;; 55 | 56 | # Pass unrecognized options ... 57 | \?) pass+=" -${OPTARG}" ;; 58 | esac 59 | else 60 | # Pass unrecognized arguments ... 61 | pass+=" ${!OPTIND}" 62 | let OPTIND++ 63 | fi 64 | done 65 | 66 | # Pass the unrecognized arguments along for further processing ... 67 | shift $((OPTIND-1)) 68 | set -- "$@" $(echo -e "${pass}" | sed -e 's/^[[:space:]]*//') 69 | # ================================================================================================================= 70 | 71 | # Profiles are not used for this script 72 | export IGNORE_PROFILES=1 73 | 74 | if [ -f ${OCTOOLSBIN}/settings.sh ]; then 75 | . ${OCTOOLSBIN}/settings.sh 76 | fi 77 | 78 | if [ -f ${OCTOOLSBIN}/ocFunctions.inc ]; then 79 | . ${OCTOOLSBIN}/ocFunctions.inc 80 | fi 81 | # ============================================================================== 82 | 83 | if [ ! -z ${fromProject} ] && [ ! -z ${toProject} ] && [ ! -z ${configFile} ] && [ -f ${configFile} ]; then 84 | routes=$(readConf ${configFile}) 85 | elif [ ! -z ${fromProject} ] && [ ! -z ${toProject} ] && [ ! -z ${routeName} ]; then 86 | routes=${routeName} 87 | else 88 | echo -e \\n"Missing one or more required parameters:"\\n 89 | echo -e " - fromProject: ${fromProject}" 90 | echo -e " - toProject: ${toProject}" 91 | echo -e " - routeName: ${routeName}" 92 | echo -e " - configFile: ${configFile}"\\n 93 | usage 94 | fi 95 | 96 | transferRoutes "${fromProject}" "${toProject}" "${routes}" -------------------------------------------------------------------------------- /bin/updateRoutes.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OCTOOLSBIN=$(dirname $0) 4 | 5 | usage() { #Usage function 6 | cat <<-EOF 7 | Delete and recreate with defaults the routes in an environment. 8 | 9 | Usage: 10 | ${0##*/} [options] 11 | EOF 12 | } 13 | 14 | if [ -f ${OCTOOLSBIN}/settings.sh ]; then 15 | . ${OCTOOLSBIN}/settings.sh 16 | fi 17 | 18 | if [ -f ${OCTOOLSBIN}/ocFunctions.inc ]; then 19 | . ${OCTOOLSBIN}/ocFunctions.inc 20 | fi 21 | 22 | # =================================================================================== 23 | # Fix routes 24 | projectName=$(getProjectName) 25 | echo -e "Update routes to default in ${projectName} ..." 26 | 27 | for route in ${routes}; do 28 | oc -n ${projectName} delete route ${route} 29 | oc -n ${projectName} create route edge --service=${route} 30 | sleep 3 # Allow the creation of the route to complete 31 | done 32 | --------------------------------------------------------------------------------