Skip to content

Commit

Permalink
Feature/add check for creds (#22)
Browse files Browse the repository at this point in the history
* ✔️ Added validation for config_file being in the same directory

* ✨ Added check for aws creds

* ✨ Modified README

* 🔧 Remove check for creds when execute awslogin
  • Loading branch information
alubneuski authored Nov 19, 2021
1 parent 018e177 commit fd97dc6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,25 @@ igor restart --app kf-cbioportal --environment qa --account kf-strides
deploy
------

Deploy application from current directory. This operation supports "ecs_service_type_1" and "aws_infra_ec2_module" deployments. This command requires to have Jenkinsfile to be in the same directory.
Deploy application from current directory. This operation supports modules in kids-first organization. This command requires to have Jenkinsfile to be in the same directory.

Options:
[] mode - Choose between 3 modes : build, plan, deploy. Build mode will do a standard docker build. Plan will create plan of resources that will be created. Deploy mode will execute terraform apply command.
[] mode - Choose between 4 modes : build, plan, apply, destroy. Build mode will do a standard docker build. Plan will create plan of resources that will be created. Deploy mode will execute terraform apply command.

***Usage:***

This example show how to deploy a application into kf-strides account , us-east-1 region and dev environment.

~~~
igor deploy --mode deploy --account_name kf-strides --organization kf-strides --region us-east-1 --environment dev --mode deploy --config_file Jenkinsfile
igor deploy --mode deploy --account_name kf-strides --organization kf-strides --region us-east-1 --environment dev --mode destroy --config_file Jenkinsfile
igor deploy --mode plan --account_name kf-strides --organization kf-strides --region us-east-1 --environment dev --config_file Jenkinsfile
igor deploy --mode apply --account_name kf-strides --organization kf-strides --region us-east-1 --environment dev --config_file Jenkinsfile
igor deploy --mode destroy --account_name kf-strides --organization kf-strides --region us-east-1 --environment dev --config_file Jenkinsfile
~~~

The command also supports wildcards. When using wildcards prefix your config file with .deploy. So for instance config files with names some_config.deploy and other_config.deploy both will be executed.

~~~
igor deploy --mode deploy --account_name kf-strides --organization kf-strides --region us-east-1 --environment dev --mode destroy --config_file *.deploy
igor deploy --mode apply --account_name kf-strides --organization kf-strides --region us-east-1 --environment dev --mode destroy --config_file *.deploy
~~~

check-build
Expand Down
4 changes: 4 additions & 0 deletions d3b_cli_igor/deploy_ops/deploy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os, sys, pathlib
from os.path import exists
import click
import stat, shutil
import fnmatch
Expand Down Expand Up @@ -27,6 +28,9 @@ def execute_deploy(account_name, organization, region, environment, config_file,
sys.exit(exit_status)

def deploy(account_name, organization, region, environment, config_file, mode, debug=False):
if (not exists(config_file)):
logger.error("File "+ config_file +" does not exist")
sys.exit(1)
if("*.deploy" in config_file or "*.destroy" in config_file):
files = fnmatch.filter(os.listdir("./"), "*.deploy")
for item in files:
Expand Down
5 changes: 5 additions & 0 deletions d3b_cli_igor/deploy_ops/templates/deploy.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ if [[ "${mode}" == "plan" ]]; then
export ONLY_PLAN="true"
fi

if [[ "${mode}" == "apply" ]]; then
export mode="deploy"
fi


if [[ "${TF_VAR_pre_deploy_config}" == "true" && "${mode}" == "deploy" ]]; then
./playbook/pre_deploy_config
fi
Expand Down
22 changes: 21 additions & 1 deletion igor
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ import click
import time
import d3b_cli_igor.common, d3b_cli_igor.log_ops.app_logs, d3b_cli_igor.app_ops.ecs_deployment, d3b_cli_igor.deploy_ops.deploy, d3b_cli_igor.deploy_ops.generate_config, d3b_cli_igor.utils.shortcuts, d3b_cli_igor.utils.diff, d3b_cli_igor.app_ops.ecs_get_info
import boto3
import boto3
import sys
import d3b_cli_igor.common

logger = d3b_cli_igor.common.get_logger(
__name__, testing_mode=False, log_format="detailed"
)

def check_creds():
sts = boto3.client('sts')
try:
sts.get_caller_identity()
logger.info("Credentials are valid.")
except Exception as e:
logger.error("Credentials are NOT valid." + str(e))
sys.exit(1)

@click.group()
def igor_cli():
Expand Down Expand Up @@ -42,9 +58,10 @@ def get_logs(app, environment, query, hours):
@click.option("--region", nargs=1, required=True)
@click.option("--environment", nargs=1, required=True)
@click.option("--config_file", nargs=1, default="Jenkinsfile", required=False)
@click.option("--mode", type=click.Choice(['plan', 'build', 'deploy', 'destroy']), nargs=1, default="deploy", required=False)
@click.option("--mode", type=click.Choice(['plan', 'build', 'apply', 'destroy']), nargs=1, default="apply", required=False)
@click.option("--debug", is_flag=True, required=False)
def deploy(account_name, organization, region, environment, config_file, mode, debug):
check_creds()
d3b_cli_igor.deploy_ops.deploy.deploy(
account_name, organization, region, environment, config_file, mode, debug
)
Expand Down Expand Up @@ -79,6 +96,7 @@ def awslogin():
@click.option("--environment", nargs=1, required=True)
@click.option("--cidr_block", nargs=1, default="", required=False)
def dev_env_tunnel(environment,cidr_block):
check_creds()
d3b_cli_igor.utils.shortcuts.dev_env_tunnel(environment,cidr_block)

@click.command(name="diff")
Expand Down Expand Up @@ -123,6 +141,7 @@ def split_files(file_to_split,num_of_lines):
@click.option("--environment", nargs=1, required=True, help="Specify Environment")
@click.option("--account", nargs=1, required=True, help="Specify AWS Account")
def restart(app, environment, account):
check_creds()
d3b_cli_igor.app_ops.ecs_deployment.restart(app, environment, account)

@click.command(name="get-info")
Expand All @@ -136,6 +155,7 @@ def restart(app, environment, account):
@click.option("--region", default="us-east-1", nargs=1, required=True)
@click.option("--account", nargs=1, required=True, help="Specify AWS Account")
def get_info(app, environment, account, region):
check_creds()
d3b_cli_igor.app_ops.ecs_get_info.get_info(app, environment, account, region)


Expand Down

0 comments on commit fd97dc6

Please sign in to comment.