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

228 lines
8.7 KiB
YAML
Raw Normal View History

2019-03-19 10:09:10 -07:00
- name: Clone BlockScout
git:
repo: "{{ blockscout_repo }}"
dest: "blockscout-{{ group_names[0] }}-{{ chain }}"
version: "{{ branch }}"
2019-03-19 10:09:10 -07:00
force: true
when: skip_fetch | bool != true
2019-03-20 09:20:47 -07:00
- name: Git clean
command: "git clean -fdx"
args:
chdir: "blockscout-{{ group_names[0] }}-{{ chain }}"
2019-03-20 09:20:47 -07:00
when: skip_fetch | bool != true
2019-03-19 10:09:10 -07:00
- name: Merge branches
command: "git merge {{ merge_commit_item }}"
2019-03-19 10:09:10 -07:00
args:
chdir: "blockscout-{{ group_names[0] }}-{{ chain }}"
when: merge_commit_item and not skip_fetch | bool
2019-03-19 10:09:10 -07:00
vars:
merge_commit_item: "{{ merge_commit | default(false) }}"
2019-03-19 10:09:10 -07:00
- name: Copy web config files
copy:
src: "blockscout-{{ group_names[0] }}-{{ chain }}/apps/block_scout_web/config/dev.secret.exs.example"
dest: "blockscout-{{ group_names[0] }}-{{ chain }}/apps/block_scout_web/config/dev.secret.exs"
2019-03-19 10:09:10 -07:00
- name: Template explorer config files
2019-03-19 10:09:10 -07:00
template:
src: dev.secret.exs.j2
dest: "blockscout-{{ group_names[0] }}-{{ chain }}/apps/explorer/config/dev.secret.exs"
when: ps_user is defined
2019-04-23 17:02:33 -07:00
- name: Copy default explorer config files
copy:
src: "blockscout-{{ group_names[0] }}-{{ chain }}/apps/explorer/config/dev.secret.exs.example"
dest: "blockscout-{{ group_names[0] }}-{{ chain }}/apps/explorer/config/dev.secret.exs"
when: ps_user is undefined or ps_user == ""
2019-03-19 10:09:10 -07:00
- name: Remove static assets from previous deployment, if any
file:
path: "blockscout-{{ group_names[0] }}-{{ chain }}/apps/block_scout_web/priv/static"
2019-03-19 10:09:10 -07:00
state: absent
- name: Compile BlockScout
2019-03-20 09:20:47 -07:00
command: "mix do {{ item }}"
2019-03-19 10:09:10 -07:00
args:
chdir: "blockscout-{{ group_names[0] }}-{{ chain }}"
2019-03-20 09:20:47 -07:00
with_items:
- deps.get
- local.rebar --force
- deps.compile
- compile
- ecto.drop
- ecto.create
- ecto.migrate
2019-03-19 10:09:10 -07:00
- name: Install Node modules at apps/block_scout_web/assets
2019-03-20 09:20:47 -07:00
command: npm install
2019-03-19 10:09:10 -07:00
args:
chdir: "blockscout-{{ group_names[0] }}-{{ chain }}/apps/block_scout_web/assets"
2019-03-19 10:09:10 -07:00
2019-03-20 09:20:47 -07:00
- 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-{{ group_names[0] }}-{{ chain }}/apps/block_scout_web/assets"
2019-03-20 09:20:47 -07:00
2019-03-19 10:09:10 -07:00
- name: Instal Node modules at apps/explorer
command: npm install
args:
chdir: "blockscout-{{ group_names[0] }}-{{ chain }}/apps/explorer"
2019-03-19 10:09:10 -07:00
- name: Install SSL certificates
command: mix phx.gen.cert blockscout blockscout.local
args:
chdir: "blockscout-{{ group_names[0] }}-{{ chain }}/apps/block_scout_web"
2019-03-19 10:09:10 -07:00
2019-04-23 17:02:33 -07:00
- name: Fetch environment variables (via access key)
2019-03-27 08:32:51 -07:00
set_fact:
env: "{{ lookup('aws_ssm', path, aws_access_key=aws_access_key, aws_secret_key=aws_secret_key, region=aws_region|default('us-east-1'), shortnames=true, bypath=true, recursive=true ) }}"
2019-03-27 08:32:51 -07:00
vars:
path: "/{{ group_names[0] }}/{{ chain }}"
2019-04-23 17:02:33 -07:00
when: aws_access_key is defined
- name: Fetch environment variables (via profile)
set_fact:
env_compiled: "{{ lookup('aws_ssm', path, aws_profile=aws_profile, shortnames=true, bypath=true, recursive=true ) }}"
2019-04-23 17:02:33 -07:00
vars:
path: "/{{ group_names[0] }}/{{ chain }}"
2019-05-01 12:26:39 -07:00
when: aws_access_key is undefined
2019-03-27 08:32:51 -07:00
- name: Make config variables lowercase
2019-03-27 08:32:51 -07:00
set_fact:
lower_env: "{{ lower_env | combine ({item.key|lower : item.value}) }}"
with_dict: "{{ custom_environment_chain }}"
when: custom_environment_chain|length > 0
2019-03-27 08:32:51 -07:00
vars:
lower_env: {}
custom_environment_chain: "{{ env_vars | default({}) if env_vars>0 else {} }}"
- name: Override env variables
set_fact:
env_compiled: "{{ env_compilated | combine(lower_env) }}"
when: lower_env is defined
2019-03-27 08:32:51 -07:00
- name: Uppercase chain
set_fact:
upper_env: "{{ upper_env | combine ({item.key|upper : item.value}) }}"
with_dict: "{{ env_compiled }}"
2019-03-27 08:32:51 -07:00
vars:
upper_env: {}
2019-03-27 08:32:51 -07:00
2019-03-19 10:09:10 -07:00
- name: Start server
2019-03-20 09:20:47 -07:00
block:
- set_fact:
server_port: "{{ 65535|random(seed=inventory_hostname,start=1024) }}"
- set_fact:
server_env: "{{ upper_env | combine({'NETWORK_PATH':'/','PORT':server_port,'MIX_ENV':'prod'}) }}"
2019-03-20 09:20:47 -07:00
- name: Start server
command: "mix phx.server"
environment: "{{ server_env }}"
2019-03-20 09:20:47 -07:00
args:
chdir: "blockscout-{{ group_names[0] }}-{{ chain }}"
2019-03-20 09:20:47 -07:00
async: 10000
poll: 0
- debug:
msg: "Please, open your browser at following addresses:"
run_once: true
- debug:
msg: "{{ ansible_host }}:{{ server_port }}"
2019-03-20 09:20:47 -07:00
- name: User prompt
pause:
prompt: "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. Note: Localhost stands for the machine were Ansible is currently run."
run_once: true
register: prompt
2019-03-20 09:20:47 -07:00
always:
- name: kill server
2019-04-23 17:02:33 -07:00
command: "pkill -f {{ item }}"
2019-03-20 09:20:47 -07:00
with_items:
- beam.smp
- webpack.js
failed_when: false
- name: Check for execution interrupt
fail:
msg: "Execution aborted"
when: prompt is failed
2019-03-20 09:20:47 -07:00
2019-03-19 10:09:10 -07:00
- name: Build static assets
command: mix phx.digest
args:
chdir: "blockscout-{{ group_names[0] }}-{{ chain }}"
2019-03-19 10:09:10 -07:00
2019-03-27 08:32:51 -07:00
- name: User prompt
pause:
prompt: "Would you like to remove staging dependencies? [Yes/No] Default: Yes"
2019-03-27 08:32:51 -07:00
register: user_answer
until: user_answer.user_input|lower != "false" and user_answer.user_input|lower != "no" and user_answer.user_input|lower != "true" and user_answer.user_input|lower != "yes"
retries: 10000
delay: 1
2019-03-27 08:32:51 -07:00
2019-03-19 10:09:10 -07:00
- name: Remove dev dependencies
file:
state: absent
path: "{{ item }}"
with_items:
- "blockscout-{{ group_names[0] }}-{{ chain }}/_build/"
- "blockscout-{{ group_names[0] }}-{{ chain }}/deps/"
- "blockscout-{{ group_names[0] }}-{{ chain }}/apps/block_scout_web/assets/node_modules/"
- "blockscout-{{ group_names[0] }}-{{ chain }}/apps/explorer/node_modules/"
- "blockscout-{{ group_names[0] }}-{{ chain }}/logs/dev/"
when: user_answer.user_input | lower | bool
2019-03-19 10:09:10 -07:00
- name: Fix bug with favicon
replace:
2019-05-02 09:42:17 -07:00
regexp: '\"favicon\.ico\"\:\"favicon-[a-z0-9]+?\.ico\"'
replace: '"images/favicon.ico":"favicon.ico"'
path: "blockscout-{{ group_names[0] }}-{{ chain }}/apps/block_scout_web/priv/static/cache_manifest.json"
2019-03-19 10:09:10 -07:00
- 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={{ group_names[0] }}-explorer --s3-location s3://{{ group_names[0] }}-explorer-codedeploy-releases/blockscout-{{ group_names[0] }}-{{ chain }}.zip --source=blockscout-{{ group_names[0] }}-{{ chain }} {{ '--profile='~aws_profile if aws_profile is defined else '' }}"
2019-03-19 10:09:10 -07:00
register: push_output
2019-03-27 08:32:51 -07:00
- name: Upload output
debug:
2019-04-24 04:30:29 -07:00
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 '' }}"
2019-03-27 08:32:51 -07:00
2019-03-19 10:09:10 -07:00
- name: User prompt
pause:
prompt: "Do you want to update the Parameter Store variables? [Yes/No] Default: Yes"
2019-03-19 10:09:10 -07:00
register: user_answer
until: user_answer.user_input | lower != "false" and user_answer.user_input | lower != "no" and user_answer.user_input | lower != "true" and user_answer.user_input | lower != "yes"
retries: 10000
delay: 1
2019-03-19 10:09:10 -07:00
- name: Update chain variables
aws_ssm_parameter_store:
name: "/{{ group_names[0] }}/{{ chain }}/{{ item.key }}"
2019-03-19 10:09:10 -07:00
value: "{{ item.value }}"
2019-04-23 17:02:33 -07:00
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: "{{ lower_env }}"
when: user_answer.user_input | lower | bool
2019-03-27 08:32:51 -07:00
- name: User prompt
pause:
prompt: "Do you want to deploy BlockScout? [Yes/No] Default: Yes"
2019-03-27 08:32:51 -07:00
register: user_answer
until: user_answer.user_input | lower != "false" and user_answer.user_input | lower != "no" and user_answer.user_input | lower != "true" and user_answer.user_input | lower != "yes"
retries: 10000
delay: 1
2019-03-19 10:09:10 -07:00
- 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 {{ group_names[0] }}-explorer-dg{{ groups[group_names[0]].index(inventory_hostname) }} --deployment-config-name CodeDeployDefault.OneAtATime --description '{{ env_compiled['BLOCKSCOUT_VERSION'] }}' {{ '--profile='~aws_profile if aws_profile is defined else '' }}"
when: user_answer.user_input | lower | bool