├── .gitignore ├── LICENSE ├── README.md ├── bin ├── awsenv ├── awsenv-default ├── awsenv-help ├── awsenv-import ├── awsenv-import-git ├── awsenv-init ├── awsenv-list ├── awsenv-root ├── awsenv-update ├── awsenv-use ├── awsenv-validate ├── awsenv-validate-cert ├── awsenv-validate-credentials ├── awsenv-validate-rsa-key └── awsenv-version └── envs └── .gitkeep /.gitignore: -------------------------------------------------------------------------------- 1 | # Contains the users environments and SHOULD NOT be commited 2 | envs/* 3 | !envs/.gitkeep 4 | 5 | # This file contains the default environment for awsenv-init 6 | default 7 | 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Simple AWS Environment Management 2 | 3 | [awsenv][] lets you easily switch between multiple [AWS][] environments. It's 4 | simple, unobtrusive, and follows the UNIX tradition of single-purpose tool that 5 | do one thing well. 6 | 7 | [awsenv][] is heavily inspired by [rbenv][] from [sstephenson][]. 8 | 9 | ## How it works 10 | 11 | [awsenv][] operates on the per-user directory `~/.awsenv`. Environments in 12 | [awsenv][] correspond to subdirectories of `~/.awsenv/envs`. For example, you 13 | might have `~/.awsenv/envs/acme-corporation` and `~/.awsenv/envs/private`. 14 | 15 | ## Installation 16 | 17 | ### Basic checkout 18 | 19 | 1. Check out [awsenv][] into `~/.awsenv`. 20 | 21 | $ git clone git://github.com/michaelcontento/awsenv.git "$HOME/.awsenv" 22 | 23 | 2. Add `~/.awsenv/bin` to your `$PATH` for access to the [awsenv][] command-line 24 | utility. 25 | 26 | $ echo 'export PATH="$HOME/.awsenv/bin:$PATH"' >> ~/.bash_profile 27 | 28 | **Zsh note:** Modify your `~/.zshenv` file instead of `~/.bash_profile`. 29 | 30 | 3. Add `awsenv init` to your shell to load the default environment. 31 | 32 | $ echo 'eval "$(awsenv init -)"' >> ~/.bash_profile 33 | 34 | **Zsh note:** Modify your `~/.zshenv` file instead of `~/.bash_profile`. 35 | 36 | 4. Restart your shell so the path changes take effect. You can now begin using 37 | [awsenv][]. 38 | 39 | $ exec $SHELL 40 | 41 | ### Upgrading 42 | 43 | If you've installed [awsenv][] using the instructions above, you can upgrade 44 | your installation at any time using [git][]. 45 | 46 | To upgrade to the latest development version of [awsenv][], use `git pull`: 47 | 48 | $ cd ~/.awsenv 49 | $ git pull 50 | 51 | Or use the builtin update command: 52 | 53 | $ awsenv update 54 | 55 | To upgrade to a specific release of [awsenv][], check out the corresponding tag: 56 | 57 | $ cd ~/.awsenv 58 | $ git fetch 59 | $ git tag 60 | 0.1.0 61 | 0.2.0 62 | $ git checkout 0.2.0 63 | 64 | ## Usage 65 | 66 | Like [git][], the [awsenv][] command delegates to subcommands based on its first 67 | argument. The most common subcommands are: 68 | 69 | ### awsenv init 70 | 71 | This is the only command that will modify your current shell. Here is what this 72 | command does: 73 | 74 | 1. Exposes the usage of [awsenv][] to the world with the variables described 75 | below 76 | 2. Configures various variables required by the [AWS][] tools: 77 | * Credential variables (e.g. `AWS_CREDENTIAL_FILE`) 78 | * Variables regarding the used identity file (e.g. `AWS_IDENTITY_FILE`) 79 | * Try to define `JAVA_HOME`, if not already set 80 | 3. Add the used identity file to the `ssh-agent` 81 | 82 | Run `awsenv init -` for yourself to see exactly what happens under the hood. 83 | 84 | The special name `-` tells [awsenv][] to use the environment selected with 85 | `awsenv use` previously. 86 | 87 | [awsenv][] expose it's presence to the outside world with the following 88 | variables: 89 | 90 | * `AWSENV_LOADED=1` indicates that [awsenv][] is loaded properly 91 | * `AWSENV_NAME` contains the name of the currently used environment 92 | 93 | ### awsenv use 94 | 95 | Set the global environment name to be used in all shells by writing the name to 96 | the `~/.awsenv/default` file. 97 | 98 | $ awsenv use acme-corporation 99 | 100 | All environments are stored as seperate directory in `~/.awsenv/env`. 101 | 102 | ### awsenv list 103 | 104 | Display a list of all currently installed environments. 105 | 106 | $ awsenv list 107 | 108 | ### awsenv import 109 | 110 | This command helps you to import environments into [awsenv][]. Just read the 111 | help of this command, checkout the [awsenv-example-env][] repository and you 112 | should be able to setup all required files without any problems. 113 | 114 | $ awsenv import git git://github.com/michaelcontento/awsenv-example-env.git example 115 | 116 | Currently only environments stored as [Git][] repository are supported. But you 117 | can help to expand this list by simply creating a new executable named 118 | `awsenv-import-`. Pull requests are welcome! 119 | 120 | ## Development 121 | 122 | The [awsenv][] source code is hosted on [GitHub][]. It's clean, modular, and 123 | easy to understand, even if you're not a shell hacker. 124 | 125 | Please feel free to submit pull requests and file bugs on the [issue tracker][]. 126 | 127 | ## License 128 | 129 | Copyright 2009-2012 Michael Contento 130 | 131 | Licensed under the Apache License, Version 2.0 (the "License"); 132 | you may not use this file except in compliance with the License. 133 | You may obtain a copy of the License at 134 | 135 | http://www.apache.org/licenses/LICENSE-2.0 136 | 137 | Unless required by applicable law or agreed to in writing, software 138 | distributed under the License is distributed on an "AS IS" BASIS, 139 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 140 | See the License for the specific language governing permissions and 141 | limitations under the License. 142 | 143 | [AWS]: https://aws.amazon.com/ 144 | [GitHub]: https://github.com 145 | [awsenv]: https://github.com/michaelcontento/awsenv 146 | [awsenv-example-env]: https://github.com/michaelcontento/awsenv-example-env 147 | [git]: http://git-scm.com 148 | [issue tracker]: https://github.com/michaelcontento/awsenv/issues 149 | [rbenv]: https://github.com/sstephenson/rbenv 150 | [sstephenson]: https://github.com/sstephenson 151 | -------------------------------------------------------------------------------- /bin/awsenv: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | [ -n "$AWSENV_DEBUG" ] && set -x 5 | 6 | COMMAND=$1 7 | case $COMMAND in 8 | "" | "-h" | "--help") 9 | awsenv help 10 | ;; 11 | 12 | * ) 13 | COMMAND_PATH="$(command -v "awsenv-$COMMAND" || true)" 14 | if [ -z "$COMMAND_PATH" ]; then 15 | awsenv version 16 | echo "error: no such command '$COMMAND'" >&2 17 | exit 1 18 | fi 19 | 20 | shift 1 21 | exec "$COMMAND_PATH" "$@" 22 | ;; 23 | esac 24 | -------------------------------------------------------------------------------- /bin/awsenv-default: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | [ -n "$AWSENV_DEBUG" ] && set -x 5 | 6 | AWSENV_ROOT="$(awsenv root)" 7 | DEFAULT_FILE="$AWSENV_ROOT/default" 8 | 9 | if [ ! -f "$DEFAULT_FILE" ]; then 10 | echo "" 11 | exit 0 12 | fi 13 | 14 | DEFAULT="$(cat $DEFAULT_FILE)" 15 | 16 | if [ ! -d "$AWSENV_ROOT/envs/$DEFAULT" ]; then 17 | echo "" 18 | exit 0 19 | fi 20 | 21 | echo "$DEFAULT" 22 | -------------------------------------------------------------------------------- /bin/awsenv-help: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | [ -n "$AWSENV_DEBUG" ] && set -x 5 | 6 | awsenv version 7 | echo "usage: awsenv []" 8 | echo "" 9 | echo "Some useful awsenv commands are:" 10 | echo " default Returns the default environment used for 'awsenv init -'" 11 | echo " help Print this help" 12 | echo " import Import new environments easily into awsenv" 13 | echo " init Configure the current session" 14 | echo " list A list of all available environments" 15 | echo " root Returns the path where awsenv is installed" 16 | echo " update Updates awsenv to the latest version" 17 | echo " use Set a new default environment" 18 | echo " validate Check if the given environment is valid" 19 | echo " version Print the version number" 20 | echo "" 21 | echo "For full documentation, see: https://github.com/michaelcontento/awsenv" 22 | -------------------------------------------------------------------------------- /bin/awsenv-import: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | [ -n "$AWSENV_DEBUG" ] && set -x 5 | 6 | if [ $# -lt 2 ]; then 7 | awsenv version 8 | echo "usage: awsenv import [alias]" 9 | echo "" 10 | echo "error: not enough arguments given." >&2 11 | exit 1 12 | fi 13 | 14 | TYPE="$1" 15 | PATH_="$2" 16 | ALIAS="$3" 17 | 18 | awsenv "import-$TYPE" "$PATH_" "$ALIAS" 19 | -------------------------------------------------------------------------------- /bin/awsenv-import-git: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | [ -n "$AWSENV_DEBUG" ] && set -x 5 | 6 | if [ $# -eq 0 ]; then 7 | awsenv version 8 | echo "usage: awsenv import-git [alias]" 9 | echo "" 10 | echo "error: no arguments given." >&2 11 | exit 1 12 | fi 13 | 14 | AWSENV_ROOT="$(awsenv root)" 15 | cd "$AWSENV_ROOT/envs" 16 | 17 | PATH_="$1" 18 | ALIAS="$2" 19 | 20 | if [ "$ALIAS" == "" ]; then 21 | ALIAS="${PATH_##*:}" 22 | ALIAS="${ALIAS##*/}" 23 | ALIAS="${ALIAS%.git}" 24 | fi 25 | 26 | if [ ! -d "$ALIAS" ]; then 27 | git clone --quiet "$PATH_" "$ALIAS" 28 | else 29 | cd "$ALIAS" 30 | 31 | if [ ! -d ".git" ]; then 32 | awsenv version 33 | echo "error: existing environment doesn't look like a git repo." >&2 34 | exit 1 35 | fi 36 | 37 | FOUND="$(grep "$PATH_" ".git/config" | wc -l | tr -d " ")" 38 | if [ "$FOUND" == "0" ]; then 39 | awsenv version 40 | echo "error: environment already exists with other remote url." >&2 41 | exit 1 42 | fi 43 | 44 | git pull --quiet --ff-only --no-rebase 45 | fi 46 | 47 | if [ "$AWSENV_NAME" != "$ALIAS" ]; then 48 | awsenv version 49 | echo "notice: the environment '$ALIAS' has to be selected / activated." 50 | echo "hint: use 'awsenv use $ALIAS' to use this environment as new default." 51 | echo "hint: or switch for current session only with: eval \"\$(awsenv init $ALIAS)\"" 52 | fi 53 | -------------------------------------------------------------------------------- /bin/awsenv-init: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | [ -n "$AWSENV_DEBUG" ] && set -x 5 | 6 | if [ $# -eq 0 ]; then 7 | awsenv version 8 | echo "usage: awsenv init " 9 | echo "" 10 | echo "error: no argument given." >&2 11 | echo "notice: use '-' to load the environment selected with 'awsenv use'." 12 | exit 1 13 | fi 14 | 15 | if [ "$1" == "-" ]; then 16 | DEFAULT="$(awsenv default)" 17 | 18 | if [ "$DEFAULT" == "" ]; then 19 | echo "# $(awsenv version)" 20 | echo "# error: you have to set a environment with 'awsenv use' first." 21 | exit 1 22 | fi 23 | else 24 | DEFAULT="$1" 25 | fi 26 | 27 | AWSENV_ROOT="$(awsenv root)" 28 | ENVIRONMENT="$AWSENV_ROOT/envs/$DEFAULT" 29 | 30 | if [ ! -d "$ENVIRONMENT" ]; then 31 | echo "# $(awsenv version)" 32 | echo "# error: there is no environment named '$DEFAULT'." >&2 33 | exit 1 34 | fi 35 | 36 | function globSingleFile() 37 | { 38 | local start=$1 39 | local end=$2 40 | local result="$(echo "$start"*"$end")" 41 | 42 | # The result can contain multiple results and that should lead to an error. 43 | # How do we achieve this? Just check for single spaces in the result! But 44 | # first we strip all escaped spaces, cause they are allowed and would lead 45 | # to false positives. 46 | local shouldNotBeEmpty="$(echo "$result" | sed 's/\\ //' | grep -v " ")" 47 | 48 | if [ "$shouldNotBeEmpty" == "" ]; then 49 | # Sed from OSX is a little bit old / different ... 50 | if [ "$(uname -s)" == "Darwin" ]; then 51 | local sedExtendedRegex="-E" 52 | else 53 | local sedExtendedRegex="-r" 54 | fi 55 | 56 | echo "# error: multiple files detected for '$start*$end'." >&2 57 | result="$(echo "$result" | sed $sedExtendedRegex 's/([^\]) .*/\1/g')" 58 | fi 59 | 60 | echo "$result" 61 | } 62 | 63 | function exportAwsEnvGlobals() 64 | { 65 | echo "" 66 | echo "# Let others detect us" 67 | echo "export AWSENV_LOADED=1" 68 | echo "export AWSENV_NAME='$DEFAULT'" 69 | } 70 | 71 | function exportAwsCredentials() 72 | { 73 | echo "" 74 | echo "# Path to the credentials / keys" 75 | 76 | local ec2Cert="$(globSingleFile "$ENVIRONMENT/cert-")" 77 | local ec2PrivateKey="$(globSingleFile "$ENVIRONMENT/pk-")" 78 | local awsCredentialFile="$(globSingleFile "$ENVIRONMENT/credentials")" 79 | local awsAccessKey="$(grep "AWSAccessKeyId" "$awsCredentialFile" | cut -d"=" -f2)" 80 | local awsSecretKey="$(grep "AWSSecretKey" "$awsCredentialFile" | cut -d"=" -f2)" 81 | 82 | echo "export AWS_CREDENTIAL_FILE='$awsCredentialFile'" 83 | 84 | if [ -f "$ec2Cert" ]; then 85 | echo "export EC2_CERT='$ec2Cert'" 86 | else 87 | echo "" 88 | echo "# No X.509 certificates for EC2 found." 89 | echo "unset EC2_CERT" 90 | fi 91 | 92 | if [ -f "$ec2PrivateKey" ]; then 93 | echo "export EC2_PRIVATE_KEY='$ec2PrivateKey'" 94 | else 95 | echo "" 96 | echo "# No EC2 private key file found." 97 | echo "unset EC2_PRIVATE_KEY" 98 | fi 99 | 100 | echo "" 101 | echo "export AWS_ACCESS_KEY_ID='$awsAccessKey'" 102 | echo "export AWS_SECRET_ACCESS_KEY='$awsSecretKey'" 103 | echo "" 104 | echo "# EC2 still needs the keys as separate variables" 105 | echo "export EC2_ACCESS_KEY='$awsAccessKey'" 106 | echo "export EC2_SECRET_KEY='$awsSecretKey'" 107 | } 108 | 109 | function exportAwsIdentityFile() 110 | { 111 | echo "" 112 | 113 | local awsIdentityFile="$(globSingleFile "$ENVIRONMENT/" "-keypair")" 114 | 115 | if [ -f "$awsIdentityFile" ]; then 116 | echo "# Default ssh key" 117 | echo "export AWS_IDENTITY_FILE='$awsIdentityFile'" 118 | echo "export AWS_SSH_KEY_ID='$(basename "$awsIdentityFile")'" 119 | 120 | if [ -x "$(command -v ssh-add || true)" ]; then 121 | echo "# ... also added to your ssh agent" 122 | chmod 600 "$awsIdentityFile" 123 | ssh-add "$awsIdentityFile" &> /dev/null 124 | fi 125 | else 126 | echo "# No EC2 SSH keypair found." 127 | echo "unset AWS_IDENTITY_FILE AWS_SSH_KEY_ID" 128 | fi 129 | } 130 | 131 | function exportJavaHome() 132 | { 133 | echo "" 134 | echo "# Try to ensure a valid JAVA_HOME" 135 | if [ "$JAVA_HOME" != "" ]; then 136 | echo "# ... already set and we obey" 137 | return 138 | fi 139 | 140 | local osxJavaHome="/System/Library/Frameworks/JavaVM.framework/Home/" 141 | if [ -d "$osxJavaHome" ]; then 142 | echo "export JAVA_HOME='$osxJavaHome'" 143 | return 144 | fi 145 | 146 | local detected=$(readlink -f "$(which java)" | sed "s:jre/bin/java::") 147 | if [ -d "$detected" ]; then 148 | echo "export JAVA_HOME='$detected'" 149 | return 150 | fi 151 | 152 | echo "error: unable to detect JAVA_HOME" >&2 153 | exit 1 154 | } 155 | 156 | function exportCustomConfig() 157 | { 158 | echo "" 159 | echo "# Custom configuration" 160 | 161 | local configFile="$ENVIRONMENT/config" 162 | 163 | if [ ! -e "$configFile" ]; then 164 | echo "# ... no custom configuration found" 165 | return 166 | fi 167 | 168 | cat "$configFile" 169 | } 170 | 171 | # And now export everything 172 | exportAwsEnvGlobals 173 | exportAwsCredentials 174 | exportAwsIdentityFile 175 | exportJavaHome 176 | exportCustomConfig 177 | 178 | -------------------------------------------------------------------------------- /bin/awsenv-list: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | [ -n "$AWSENV_DEBUG" ] && set -x 5 | 6 | AWSENV_ROOT="$(awsenv root)" 7 | AWSENV_DEFAULT="$(awsenv default)" 8 | 9 | ENVIRONMENTS="$(ls -1 "$AWSENV_ROOT/envs")" 10 | for NAME in $ENVIRONMENTS; do 11 | if [ "$NAME" == "$AWSENV_NAME" ]; then 12 | CURRENT=" \033[32m(current)\033[0m" 13 | else 14 | CURRENT="" 15 | fi 16 | 17 | if [ "$NAME" == "$AWSENV_DEFAULT" ]; then 18 | DEFAULT=" \033[32m(default)\033[0m" 19 | else 20 | DEFAULT="" 21 | fi 22 | 23 | echo -e "$NAME$CURRENT$DEFAULT" 24 | done 25 | -------------------------------------------------------------------------------- /bin/awsenv-root: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | [ -n "$AWSENV_DEBUG" ] && set -x 5 | 6 | # Simpe function for resolving a relative path to an absolute one 7 | function realpath 8 | { 9 | local path="$1" 10 | local cwd="$(pwd)" 11 | 12 | while [ -n "$path" ]; do 13 | cd "${path%/*}" 14 | local name="${path##*/}" 15 | path="$(readlink "$name" || true)" 16 | done 17 | 18 | pwd 19 | cd "$cwd" 20 | } 21 | 22 | AWSENV_ROOT="$(realpath "$0")" # Resolve this file 23 | AWSENV_ROOT="${AWSENV_ROOT%/*}" # And strip the "/bin" directory 24 | 25 | echo "$AWSENV_ROOT" 26 | -------------------------------------------------------------------------------- /bin/awsenv-update: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | [ -n "$AWSENV_DEBUG" ] && set -x 5 | 6 | cd "$(awsenv root)" 7 | git pull 8 | 9 | awsenv rehash --force 10 | -------------------------------------------------------------------------------- /bin/awsenv-use: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | [ -n "$AWSENV_DEBUG" ] && set -x 5 | 6 | if [ $# -eq 0 ]; then 7 | awsenv version 8 | echo "usage: awsenv use " 9 | echo "" 10 | echo "error: no arguments given." >&2 11 | exit 1 12 | fi 13 | 14 | NEW_DEFAULT="$1" 15 | OLD_DEFAULT="$(awsenv default)" 16 | 17 | if [ "$OLD_DEFAULT" == "$NEW_DEFAULT" ]; then 18 | exit 0 19 | fi 20 | 21 | AWSENV_ROOT="$(awsenv root)" 22 | ENVIRONMENT="$AWSENV_ROOT/envs/$NEW_DEFAULT" 23 | 24 | if [ ! -d "$ENVIRONMENT" ]; then 25 | awsenv version 26 | echo "error: there is no environment named '$NEW_DEFAULT'." >&2 27 | exit 1 28 | fi 29 | 30 | awsenv validate "$NEW_DEFAULT" 31 | 32 | echo "$NEW_DEFAULT" > "$AWSENV_ROOT/default" 33 | 34 | if [ "$AWENV_NAME" != "$NEW_DEFAULT" ]; then 35 | awsenv version 36 | echo "notice: the environment has to be activated with: eval \"\$(awsenv init -)\"" 37 | fi 38 | -------------------------------------------------------------------------------- /bin/awsenv-validate: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | [ -n "$AWSENV_DEBUG" ] && set -x 5 | 6 | if [ $# -eq 0 ]; then 7 | awsenv version 8 | echo "usage: awsenv use " 9 | echo "" 10 | echo "error: no arguments given." >&2 11 | exit 1 12 | fi 13 | 14 | AWSENV_ROOT="$(awsenv root)" 15 | ENVIRONMENT="$AWSENV_ROOT/envs/$1" 16 | 17 | if [ ! -d "$ENVIRONMENT" ]; then 18 | awsenv version 19 | echo "error: there is no environment named '$NEW_DEFAULT'." >&2 20 | exit 1 21 | fi 22 | 23 | # NOTE: EC2_CERT and EC2_PRIVATE_KEY are deprecated -- those warnings could be 24 | # removed entirely: 25 | # http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/CLTRG-common-args-api.html 26 | awsenv validate-cert "$ENVIRONMENT/cert-"* 27 | awsenv validate-credentials "$ENVIRONMENT/credentials" 28 | awsenv validate-rsa-key "$ENVIRONMENT/"*"-keypair" 29 | awsenv validate-rsa-key "$ENVIRONMENT/pk-"* 30 | -------------------------------------------------------------------------------- /bin/awsenv-validate-cert: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | [ -n "$AWSENV_DEBUG" ] && set -x 5 | 6 | if [ $# -eq 0 ]; then 7 | awsenv version 8 | echo "usage: awsenv validate-cert " 9 | echo "" 10 | echo "error: no arguments given." >&2 11 | exit 1 12 | fi 13 | 14 | FILE="$1" 15 | 16 | if [ -f "$FILE" ]; then 17 | TOKEN_START="-----BEGIN CERTIFICATE-----" 18 | TOKEN_END="-----END CERTIFICATE-----" 19 | 20 | if [ "$(head -n1 "$FILE")" != "$TOKEN_START" ]; then 21 | awsenv version 22 | echo "error: certificate without start token ($TOKEN_START)." >&2 23 | exit 1 24 | fi 25 | 26 | if [ "$(tail -n1 "$FILE")" != "$TOKEN_END" ]; then 27 | awsenv version 28 | echo "error: certificate without end token ($TOKEN_END)." >&2 29 | exit 1 30 | fi 31 | else 32 | echo "warning: no EC2 certificate exists at '$FILE'." >&2 33 | fi 34 | 35 | -------------------------------------------------------------------------------- /bin/awsenv-validate-credentials: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | [ -n "$AWSENV_DEBUG" ] && set -x 5 | 6 | if [ $# -eq 0 ]; then 7 | awsenv version 8 | echo "usage: awsenv validate-credentials " 9 | echo "" 10 | echo "error: no arguments given." >&2 11 | exit 1 12 | fi 13 | 14 | FILE="$1" 15 | 16 | if [ ! -f "$FILE" ]; then 17 | awsenv version 18 | echo "error: file '$FILE' does not exists." >&2 19 | exit 1 20 | fi 21 | 22 | set +e 23 | egrep -q "^AWSAccessKeyId=[A-Za-z0-9]+" "$FILE" 24 | GREP_SUCCESS=$? 25 | set -e 26 | 27 | if [ $GREP_SUCCESS -ne 0 ]; then 28 | awsenv version 29 | echo "error: key 'AWSAccessKeyId' not properly set." >&2 30 | exit 1 31 | fi 32 | 33 | set +e 34 | egrep -q "^AWSSecretKey=[A-Za-z0-9/\+]+" "$FILE" 35 | GREP_SUCCESS=$? 36 | set -e 37 | 38 | if [ $GREP_SUCCESS -ne 0 ]; then 39 | awsenv version 40 | echo "error: key 'AWSSecretKey' not properly set." >&2 41 | exit 1 42 | fi 43 | -------------------------------------------------------------------------------- /bin/awsenv-validate-rsa-key: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | [ -n "$AWSENV_DEBUG" ] && set -x 5 | 6 | if [ $# -eq 0 ]; then 7 | awsenv version 8 | echo "usage: awsenv validate-rsa-key " 9 | echo "" 10 | echo "error: no arguments given." >&2 11 | exit 1 12 | fi 13 | 14 | FILE="$1" 15 | 16 | if [ -f "$FILE" ]; then 17 | TOKEN_START="-----BEGIN RSA PRIVATE KEY-----" 18 | TOKEN_END="-----END RSA PRIVATE KEY-----" 19 | 20 | if [ "$(head -n1 "$FILE")" != "$TOKEN_START" ]; then 21 | awsenv version 22 | echo "error: start token ($TOKEN_START) missing." >&2 23 | exit 1 24 | fi 25 | 26 | if [ "$(tail -n1 "$FILE")" != "$TOKEN_END" ]; then 27 | awsenv version 28 | echo "error: end token ($TOKEN_END) missing." >&2 29 | exit 1 30 | fi 31 | else 32 | echo "warning: no key file exists at '$FILE'." >&2 33 | fi 34 | 35 | -------------------------------------------------------------------------------- /bin/awsenv-version: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | [ -n "$AWSENV_DEBUG" ] && set -x 5 | 6 | echo "awsenv 0.3.0" 7 | -------------------------------------------------------------------------------- /envs/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/michaelcontento/awsenv/4aed0f27876e92fa99c210c211711e0023fc2ef9/envs/.gitkeep --------------------------------------------------------------------------------