Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rover 'state rm': state file not found #292

Open
gesnaud opened this issue Nov 24, 2022 · 5 comments
Open

rover 'state rm': state file not found #292

gesnaud opened this issue Nov 24, 2022 · 5 comments
Assignees

Comments

@gesnaud
Copy link

gesnaud commented Nov 24, 2022

Hello,

I'm facing an issue when trying to make a state rm in rover command:

rover -lz /tf/caf/.solution/caf/caf_solution \
-var-folder /tf/caf/my_var_folder \
-a 'state rm' \
-p ${PLAN_FILE_NAME} \
-level level4 \
-env ${LZ_PREFIX} \
-tfstate ${TFSTATE_FILE} \
-tfstate_subscription_id ${TFSTATE_SUB_ID} \
-parallelism 30 \
-target_subscription ${SUB_NAME} \
-id "module.solution.module.network_something[\"blabal\"].azurecaf_name.networksomething"`

And the result is a state file was found! error message::

@calling verify_azure_session
Checking existing Azure session
@calling process_target_subscription
Set subscription to -target_subscription SXXXXX01
caf_command landingzone
target_subscription_id 8XXXXXe
TF_VAR_tfstate_subscription_id 8XXXXXe
Resources from this landing zone are going to be deployed in the following subscription:
{
  "environmentName": "AzureCloud",
  "homeTenantId": "axxxxxx",
  "id": "xxxxxf",
  "isDefault": true,
  "managedByTenants": [],
  "name": "XXXXXX",
  "state": "Enabled",
  "tenantId": "xxxxxx",
  "user": {
    "name": "hey@you.onmicrosoft.com",
    "type": "user"
  }
}
debug: 6XXXXXXX
Tfstates subscription set to XXXX (XXXXXXXXXXX)


mode                          : 'landingzone'
terraform command output file : ''
terraform plan output file    : 'foobar_tfplan'
directory cache               : '/home/vscode/.terraform.cache/foobar_launchpad/rover_jobs/20221124112354655979305'
tf_action                     : 'state list'
command and parameters        : '-var-file=/tf/caf/environements/vxxxxx/landingzone.tfvars -var-file=/tf/caf/environements/xxxx/rgs.tfvars -var-file=/tf/caf/environements/xxxxx/resource.tfvars -parallelism 30'

level (current)               : 'level4'
environment                   : 'foobar_launchpad'
workspace                     : 'tfstate'
tfstate                       : 'foobar.tfstate'
tfstate subscription id       : 'XXXXX'
target subscription           : 'XXXXX'
CI/CD enabled                 : 'false'
Symphony Yaml file path       : ''
Run all tasks                 : 'true'
TF_IN_AUTOMATION              : 'true'

@calling process_actions
@calling verify_parameters
landingzone                   : '/tf/caf/landingzone'
@calling_deploy
@calling get_storage_id
@calling_get_logged_user_object_id
 - AZURE_ENVIRONMENT: AzureCloud
 - ARM_ENVIRONMENT: public
Initalizing az cloud variables
 - logged in user objectId: xxxxxx (hey@you.onmicrosoft.com)
Initializing state with user: hey@you.onmicrosoft.com

launchpad already installed

@calling deploy_from_remote_state
Connecting to the launchpad
@calling login_as_launchpad

Getting launchpad coordinates from subscription: 8xxxx9e
 - keyvault_name: vxxxxx-level4
 - tenant_id : axxxxxx8
 - storage_account_name (current): xxx4
 - storage_account_name (lower): xxx3
 - resource_group (current): xxxxlaunchpad-level4
 - resource_group (lower): xxxlaunchpad-level3
@calling deploy_landingzone
Deploying '/tf/caf/landingzone'
Terraform version 0.15 or greater
Upgrading modules...
Downloading aztfmod/caf/azurerm 5.4.8 for dynamic_keyvault_secrets...
- dynamic_keyvault_secrets in /home/vscode/.terraform.cache/foobar_launchpad/rover_jobs/20221112345679305/modules/dynamic_keyvault_secrets/modules/security/dynamic_keyvault_secrets
- dynamic_keyvault_secrets.secret in /home/vscode/.terraform.cache/foobar_launchpad/rover_jobs/20221112345679305/modules/dynamic_keyvault_secrets/modules/security/dynamic_keyvault_secrets/secret

Initializing the backend...

Successfully configured the backend "azurerm"! Terraform will automatically
use this backend unless the backend configuration changes.

Initializing provider plugins...
- terraform.io/builtin/terraform is built in to Terraform
- Finding hashicorp/azurerm versions matching "~> 2.81.0"...
- Finding latest version of hashicorp/local...
[...]
- Using hashicorp/tls v2.2.0 from the shared cache directory
- Using aztfmod/azurecaf v1.2.22 from the shared cache directory

Terraform has been successfully initialized!
Terraform init return code 0
calling other
@calling other
running terraform state rm -state=/home/vscode/.terraform.cache/foobar_launchpad/rover_jobs/20221112345679305/tfstates/levelX/tfstate/'foobar.tfstate module.solution.module.network_something[\"blabal\"].azurecaf_name.networksomething"
Terraform state rm return code: 0
Terraform returned errors:
No state file was found!

State management commands require a state file. Run this command
in a directory where Terraform has been run or use the -state flag
to point the command to a specific state location.
Error on or near line 555: Error running terraform state rm; exiting with status 2003

@calling clean_up_variables
cleanup variables
clean_up backend_files

But a state show is working fine:

rover -lz /tf/caf/.solution/caf/caf_solution \
-var-folder /tf/caf/my_var_folder \
-a 'state show' \
-p ${PLAN_FILE_NAME} \
-level level4 \
-env ${LZ_PREFIX} \
-tfstate ${TFSTATE_FILE} \
-tfstate_subscription_id ${TFSTATE_SUB_ID} \
-parallelism 30 \
-target_subscription ${SUB_NAME} \
-id "module.solution.module.network_something[\"blabal\"].azurecaf_name.networksomething"`

And the result is:

Terraform has been successfully initialized!
Terraform init return code 0
calling other
@calling other
running terraform state list -state=/home/vscode/.terraform.cache/foobar_launchapd/rover_jobs/2022114158234788/tfstates/level4/tfstate/foobar.tfstate module.solution.module.network_something["blabal"].azurecaf_name.networksomething

module.solution.module.network_something[\"blabal\"].azurecaf_name.networksomething"`
Terraform state list return code: 0

@calling clean_up_variables
cleanup variables
clean_up backend_files

real    0m39.992s
user    0m20.880s
sys     0m8.836s

My questions are:

  • Is the state rm a rover's bug?
  • Why state list or state show are working?
  • When is downloaded the tfsate for state show/list?

My environment

  • aztfmod/rover:1.0.7-2109.2410
@gesnaud
Copy link
Author

gesnaud commented Nov 25, 2022

Hi there o/

I forget to precise that i've made little enhancement on rover's scripts to be able to manage the id resource:

- rover/rover.sh

[...]
        -a|--action)
            export tf_action=$(parameter_value --action "${2}")
            shift 2
            ;;
        -id)
            export tf_id="${2}"
            shift 2
[...]

- rover/tfstate_azurerm.sh

function other {
    echo "@calling other"
    echo "running terraform ${tf_action} -state="${TF_DATA_DIR}/tfstates/${TF_VAR_level}/${TF_VAR_workspace}/${TF_VAR_tf_name}" ${tf_id}"
    download_tfstate
    rm -f $STDERR_FILE

    terraform \
        ${tf_action} \
        -state="${TF_DATA_DIR}/tfstates/${TF_VAR_level}/${TF_VAR_workspace}/${TF_VAR_tf_name}" \
        ${tf_id} 2>$STDERR_FILE | tee ${tf_output_file}
        #${tf_command} 2>$STDERR_FILE | tee ${tf_output_file}

    RETURN_CODE=${PIPESTATUS[0]} && echo "Terraform ${tf_action} return code: ${RETURN_CODE}"

 #   upload_tfstate
[...]

You could notice that I add upload_tfstate and download_tfstate to workaround the no state file. I comment it for now to avoid effects for other commands.

Thanks!

@gesnaud
Copy link
Author

gesnaud commented Nov 28, 2022

Here is a proposal for a clean 'patch' for tfstate_azurerm.sh.
You will notice that I have to use a 'swap' file ( /tmp/tf_pipe_code)as I cannot get variable value from a subshell to the parent 🤔.

rover/rover.sh

[...]
        -a|--action)
            export tf_action=$(parameter_value --action "${2}")
            shift 2
            ;;
        -id)
            export tf_id="${2}"
            shift 2
[...]

tfstate_azurerm.sh

function other {
    echo "@calling other"

    rm -f $STDERR_FILE

    [[ "${tf_action}" == *"rm"* ]] && \
      (echo "tf_action *SHOULD* containing rm: "${tf_action};
          download_tfstate)

    [[ "${tf_action}" == *"state"* ]] && \
      (echo "running terraform ${tf_action} -state="${TF_DATA_DIR}/tfstates/${TF_VAR_level}/${TF_VAR_workspace}/${TF_VAR_tf_name}"  ${tf_id}"; 
       terraform \
        ${tf_action} \
        -state="${TF_DATA_DIR}/tfstates/${TF_VAR_level}/${TF_VAR_workspace}/${TF_VAR_tf_name}" \
        ${tf_id} \
        2>$STDERR_FILE | tee ${tf_output_file};
        echo ${PIPESTATUS[0]} > /tmp/tf_pipe_code);

    [[ "${tf_action}" != *"state"* ]] && \
      (echo "running terraform ${tf_action} -state="${TF_DATA_DIR}/tfstates/${TF_VAR_level}/${TF_VAR_workspace}/${TF_VAR_tf_name}" ${tf_command}"; 
       terraform \
        ${tf_action} \
        -state="${TF_DATA_DIR}/tfstates/${TF_VAR_level}/${TF_VAR_workspace}/${TF_VAR_tf_name}" \
        ${tf_command} \
        2>$STDERR_FILE | tee ${tf_output_file};
        echo ${PIPESTATUS[0]} > /tmp/tf_pipe_code);

    TF_PIPE_CODE=$(cat /tmp/tf_pipe_code);
    rm -f /tmp/tf_pipe_code;
    RETURN_CODE=${TF_PIPE_CODE} && echo "Terraform ${tf_action} return code: ${RETURN_CODE}"

    if [ -s $STDERR_FILE ]; then
        if [ ${tf_output_file+x} ]; then cat $STDERR_FILE >>${tf_output_file}; fi
        echo "Terraform returned errors:"
        cat $STDERR_FILE
        RETURN_CODE=2003
    fi

    if [ $RETURN_CODE != 0 ]; then
        error ${LINENO} "Error running terraform ${tf_action}" $RETURN_CODE
    else
        [[ "${tf_action}" == *"rm"* ]] && upload_tfstate
    fi
    echo "" 
}

@LaurentLesle I was thinking to make a pull request on the branch concerning aztfmod/rover:1.0.7-2109.2410 but I cannot find the matching info?
Where is the matrix version of github aztfmod/rover giving dockerhub aztfmod/rover?

@gesnaud
Copy link
Author

gesnaud commented Jan 27, 2023 via email

@jtdossett
Copy link

jtdossett commented Jan 27, 2023 via email

@gesnaud
Copy link
Author

gesnaud commented Jan 27, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants