stages:
  - build
  - deploy

.default_settings_docker: &default_settings_docker
  tags:
    - ansible-update
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  only:
    - master
    - chat
  except:
    - schedules

.default_settings_cron: &default_settings_cron
  stage: deploy
  tags:
    - ansible
  variables:
    GIT_STRATEGY: none
  only:
    - chat
    - schedules

help:
  <<: *default_settings_cron
  script:
    - echo -e "section_start:$( date +%s ):chat_reply\r\033[0K\n"
    - echo -e "Hallo world"
    - echo -e "\nsection_end:$( date +%s ):chat_reply\r\033[0K"
  except:
    variables:
      - $ANSIBLE_JOB_NAME && $ANSIBLE_JOB_NAME != "help"

BuildDockerImage:
  <<: *default_settings_docker
  stage: build
  script:
    - docker build --pull -t "$CI_REGISTRY_IMAGE" .
    - docker push "$CI_REGISTRY_IMAGE"

Ansible:
  <<: *default_settings_cron
  script:
    - ascr ${CHAT_INPUT} ${ANSIBLE_EXTRA_ARGS}
  except:
    variables:
      - $ANSIBLE_JOB_NAME && $ANSIBLE_JOB_NAME != "Ansible"

AnsibleDirect:
  <<: *default_settings_cron
  script:
    - a -m ${ANSIBLE_COMMAND} -a "${ANSIBLE_ARGS}" ${ANSIBLE_EXTRA_ARGS}
  except:
    variables:
      - $ANSIBLE_JOB_NAME && $ANSIBLE_JOB_NAME != "AnsibleDirect"

RenewLetsEncrypt:
  <<: *default_settings_cron
  script:
    - ascr letsencrypt ${CHAT_INPUT} ${ANSIBLE_EXTRA_ARGS}
  except:
    variables:
      - $ANSIBLE_JOB_NAME && $ANSIBLE_JOB_NAME != "RenewLetsEncrypt"

UpdateNextcloudPlugins:
  <<: *default_settings_cron
  script:
    - ascr nextcloud-plugins ${CHAT_INPUT} ${ANSIBLE_EXTRA_ARGS}
  except:
    variables:
      - $ANSIBLE_JOB_NAME && $ANSIBLE_JOB_NAME != "UpdateNextcloudPlugins"

UpdateHomeAssistant:
  <<: *default_settings_cron
  script:
    - ascr home-assistant ${CHAT_INPUT} ${ANSIBLE_EXTRA_ARGS}
  except:
    variables:
      - $ANSIBLE_JOB_NAME && $ANSIBLE_JOB_NAME != "UpdateHomeAssistant"

UpdateCollabora:
  <<: *default_settings_cron
  script:
    - ascr collabora ${CHAT_INPUT} ${ANSIBLE_EXTRA_ARGS}
  except:
    variables:
      - $ANSIBLE_JOB_NAME && $ANSIBLE_JOB_NAME != "UpdateCollabora"

UpdateKeycloak:
  <<: *default_settings_cron
  script:
    - ascr keycloak ${CHAT_INPUT} ${ANSIBLE_EXTRA_ARGS}
  except:
    variables:
      - $ANSIBLE_JOB_NAME && $ANSIBLE_JOB_NAME != "UpdateKeycloak"

UpdateMatterbridge:
  <<: *default_settings_cron
  script:
    - ascr matterbridge  ${CHAT_INPUT} ${ANSIBLE_EXTRA_ARGS}
  except:
    variables:
      - $ANSIBLE_JOB_NAME && $ANSIBLE_JOB_NAME != "UpdateMatterbridge"

UpdateAlerta:
  <<: *default_settings_cron
  script:
    - ascr alerta ${CHAT_INPUT} ${ANSIBLE_EXTRA_ARGS}
  except:
    variables:
      - $ANSIBLE_JOB_NAME && $ANSIBLE_JOB_NAME != "UpdateAlerta"

UpdatePhotoprism:
  <<: *default_settings_cron
  script:
    - ascr photoprism ${CHAT_INPUT} ${ANSIBLE_EXTRA_ARGS}
  except:
    variables:
      - $ANSIBLE_JOB_NAME && $ANSIBLE_JOB_NAME != "UpdatePhotoprism"

SanityCheck:
  <<: *default_settings_cron
  script:
    - ascr sanity check ${CHAT_INPUT} ${ANSIBLE_EXTRA_ARGS}
  except:
    variables:
      - $ANSIBLE_JOB_NAME && $ANSIBLE_JOB_NAME != "SanityCheck"

SanityList:
  <<: *default_settings_cron
  script:
    - ascr sanity list ${CHAT_INPUT} ${ANSIBLE_EXTRA_ARGS}
  except:
    variables:
      - $ANSIBLE_JOB_NAME && $ANSIBLE_JOB_NAME != "SanityList"

SanityUpgrade:
  <<: *default_settings_cron
  script:
    - ascr sanity upgrade ${CHAT_INPUT} ${ANSIBLE_EXTRA_ARGS}
  except:
    variables:
      - $ANSIBLE_JOB_NAME && $ANSIBLE_JOB_NAME != "SanityUpgrade"

SanityReboot:
  <<: *default_settings_cron
  script:
    - ascr sanity reboot ${CHAT_INPUT} ${ANSIBLE_EXTRA_ARGS}
  except:
    variables:
      - $ANSIBLE_JOB_NAME && $ANSIBLE_JOB_NAME != "SanityReboot"

JailkitUpgrade:
  <<: *default_settings_cron
  script:
    - ascr jailkit-upgrade ${CHAT_INPUT} ${ANSIBLE_EXTRA_ARGS}
  except:
    variables:
      - $ANSIBLE_JOB_NAME && $ANSIBLE_JOB_NAME != "JailkitUpgrade"

MdstatRepair:
  <<: *default_settings_cron
  script:
    - ascr mdstat ${CHAT_INPUT} ${ANSIBLE_EXTRA_ARGS}
  timeout: 5 hours
  except:
    variables:
      - $ANSIBLE_JOB_NAME && $ANSIBLE_JOB_NAME != "MdstatRepair"

CopyDataToStage:
  <<: *default_settings_cron
  script:
    - ascr drupal-data-copy ${ANSIBLE_PROJECT_PATH} ${ANSIBLE_STAGE_FROM} ${ANSIBLE_STAGE_TO} ${CHAT_INPUT} ${ANSIBLE_EXTRA_ARGS}
  except:
    variables:
      - $ANSIBLE_JOB_NAME && $ANSIBLE_JOB_NAME != "CopyDataToStage"
