blockscout-terraform/roles/main_software/tasks/main.yml

210 lines
7.5 KiB
YAML

- name: Clone BlockScout
git:
repo: "{{ blockscout_repo }}"
dest: "blockscout-{{ chain }}"
version: "{{ chain_branch[chain] }}"
force: true
when: skip_fetch | bool != true
- name: Git clean
command: "git clean -fdx"
args:
chdir: "blockscout-{{ chain }}"
when: skip_fetch | bool != true
- name: Merge branches
command: "git merge {{ chain_merge_commit[chain] }}"
args:
chdir: "blockscout-{{ chain }}"
when: skip_fetch | bool != true and chain_merge_commit_item != 'false'
vars:
chain_mc: "{{ chain_merge_commit | default({}) }}"
chain_merge_commit_item: "{{ chain_mc[chain] | default('false') }}"
- name: Copy web config files
copy:
src: "blockscout-{{ chain }}/apps/block_scout_web/config/dev.secret.exs.example"
dest: "blockscout-{{ chain }}/apps/block_scout_web/config/dev.secret.exs"
- name: Template explorer config files
template:
src: dev.secret.exs.j2
dest: "blockscout-{{ chain }}/apps/explorer/config/dev.secret.exs"
when: ps_db is defined
- name: Copy default explorer config files
copy:
src: "blockscout-{{ chain }}/apps/explorer/config/dev.secret.exs.example"
dest: "blockscout-{{ chain }}/apps/explorer/config/dev.secret.exs"
when: ps_db is undefined or ps_db == ""
- name: Remove static assets from previous deployment, if any
file:
path: "blockscout-{{ chain }}/apps/block_scout_web/priv/static"
state: absent
- name: Compile BlockScout
command: "mix do {{ item }}"
args:
chdir: "blockscout-{{ chain }}"
with_items:
- deps.get
- local.rebar --force
- deps.compile
- compile
- ecto.drop
- ecto.create
- ecto.migrate
- name: Install Node modules at apps/block_scout_web/assets
command: npm install
args:
chdir: "blockscout-{{ chain }}/apps/block_scout_web/assets"
- name: Execute webpack.js at apps/block_scout_web/assets/node_modules/webpack/bin
command: node_modules/webpack/bin/webpack.js --mode production
args:
chdir: "blockscout-{{ chain }}/apps/block_scout_web/assets"
- name: Instal Node modules at apps/explorer
command: npm install
args:
chdir: "blockscout-{{ chain }}/apps/explorer"
- name: Install SSL certificates
command: mix phx.gen.cert blockscout blockscout.local
args:
chdir: "blockscout-{{ chain }}/apps/block_scout_web"
- name: Fetch environment variables (via access key)
set_fact:
chain_env: "{{ lookup('aws_ssm', path, aws_access_key=aws_access_key, aws_secret_key=aws_secret_key, region=region, shortnames=true, bypath=true, recursive=true ) }}"
vars:
path: "/{{ prefix }}/{{ chain }}"
when: aws_access_key is defined
- name: Fetch environment variables (via profile)
set_fact:
chain_env: "{{ lookup('aws_ssm', path, aws_profile=aws_profile, shortnames=true, bypath=true, recursive=true ) }}"
vars:
path: "/{{ prefix }}/{{ chain }}"
when: aws_profile is defined
- name: Make config variables lowercase
set_fact:
chain_lower_env: "{{ chain_lower_env | combine ({item.key|lower : item.value}) }}"
with_dict: "{{ chain_custom_environment_chain }}"
when: chain_custom_environment_chain|length > 0
vars:
chain_lower_env: {}
chain_custom_environment_chain: "{{ chain_cec[chain] | default({}) if chain_cec[chain]>0 else {} }}"
chain_cec: "{{ chain_custom_environment | default ({}) }}"
- name: Override env variables
set_fact:
chain_env: "{{ chain_env | combine(chain_lower_env) }}"
when: chain_lower_env is defined
- name: Uppercase chain
set_fact:
chain_upper_env: "{{ chain_upper_env | combine ({item.key|upper : item.value}) }}"
with_dict: "{{ chain_env }}"
vars:
chain_upper_env: {}
- name: Start server
block:
- name: Start server
command: "mix phx.server"
environment: "{{ chain_upper_env | combine({'NETWORK_PATH':'/'}) }}"
ignore_errors: true
args:
chdir: "blockscout-{{ chain }}"
async: 10000
poll: 0
- name: User prompt
pause:
prompt: "Please, open your browser and open 4000 port at the machine were Ansible is currently run. BlockScout should appear. Ensure that there is no visual artifacts and then press Enter to continue. Press Ctrl+C and then A if you face any issues to cancel the deployment."
rescue:
- name: 'Stop execution'
fail:
msg: "Execution aborted."
always:
- name: kill server
command: "pkill -f {{ item }}"
with_items:
- beam.smp
- node
- erlang
failed_when: false
when:
- name: Build static assets
command: mix phx.digest
args:
chdir: "blockscout-{{ chain }}"
- name: User prompt
pause:
prompt: "Would you like to remove staging dependencies? [Yes/No] Default: Yes"
register: user_answer
- name: Remove dev dependencies
file:
state: absent
path: "{{ item }}"
with_items:
- "blockscout-{{ chain }}/_build/"
- "blockscout-{{ chain }}/deps/"
- "blockscout-{{ chain }}/apps/block_scout_web/assets/node_modules/"
- "blockscout-{{ chain }}/apps/explorer/node_modules/"
- "blockscout-{{ chain }}/logs/dev/"
when: user_answer.user_input|lower != "false" and user_answer.user_input|lower != "no"
- name: Fix bug with favicon
copy:
src: "{{ item }}"
dest: "blockscout-{{ chain }}/apps/block_scout_web/priv/static/images/"
with_fileglob:
- "blockscout-{{ chain }}/apps/block_scout_web/priv/static/favicon*"
- name: Upload Blockscout to S3
command: "{{ 'AWS_ACCESS_KEY='~aws_access_key~' AWS_SECRET_ACCESS_KEY='~aws_secret_key~' AWS_DEFAULT_REGION='~aws_region if aws_profile is undefined else '' }} aws deploy push --application-name={{ prefix }}-explorer --s3-location s3://{{ prefix }}-explorer-codedeploy-releases/blockscout-{{ chain }}.zip --source=blockscout-{{ chain }} {{ '--profile='~aws_profile if aws_profile is defined else '' }}"
register: push_output
- name: Upload output
debug:
msg: "If deployment will fail, you can try to deploy blockscout manually using the following commands: {{ 'AWS_ACCESS_KEY=XXXXXXXXXXXXXX AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXX AWS_DEFAULT_REGION='~aws_region if aws_profile is undefined else '' }} {{ push_output.stdout_lines }} {{ '--profile='~aws_profile if aws_profile is defined else '' }}"
- name: User prompt
pause:
prompt: "Do you want to update the Parameter Store variables? [Yes/No] Default: Yes"
register: user_answer
- name: Update chain variables
aws_ssm_parameter_store:
name: "/{{ prefix }}/{{ chain }}/{{ item.key }}"
value: "{{ item.value }}"
profile: "{{ profile }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
region: "{{ region }}"
vars:
access_key: "{{ aws_access_key|default(omit) }}"
secret_key: "{{ aws_secret_key|default(omit) }}"
profile: "{{ aws_profile|default(omit) }}"
region: "{{ aws_region|default(omit) }}"
with_dict: "{{ chain_env }}"
when: user_answer.user_input|lower != "false" and user_answer.user_input|lower != "no"
- name: User prompt
pause:
prompt: "Do you want to deploy BlockScout? [Yes/No] Default: Yes"
register: user_answer
- name: Deploy Blockscout
command: "{{ 'AWS_ACCESS_KEY='~aws_access_key~' AWS_SECRET_ACCESS_KEY='~aws_secret_key~' AWS_DEFAULT_REGION='~aws_region if aws_profile is undefined else '' }} {{ push_output.stdout_lines[1] }} --deployment-group-name {{ prefix }}-explorer-dg{{ index }} --deployment-config-name CodeDeployDefault.OneAtATime --description '{{ chain_upper_env['BLOCKSCOUT_VERSION'] }}' {{ '--profile='~aws_profile if aws_profile is defined else '' }}"
when: user_answer.user_input|lower != "false" and user_answer.user_input|lower != "no"