/ templates / scripts / build.sh.j2
build.sh.j2
 1  #!/usr/bin/env bash
 2  # vim: ft=sh
 3  set -e
 4  
 5  function headIsDetached() {
 6      [[ $(git rev-parse --abbrev-ref --symbolic-full-name HEAD) == "HEAD" ]];
 7  }
 8  
 9  function binaryExists() {
10      ls -l \
11  {% for target in beacon_node_update_build_targets %}
12          build/{{ target }}_${COMMIT} \
13  {% endfor %}
14          2>&1 1>/dev/null
15  }
16  
17  function fetchChanges() {
18      # We cannot use "git pull" in here, because history may be changed upstream
19      git fetch
20      git reset --hard "origin/${BRANCH}"
21  }
22  
23  function buildBinaries() {
24      # Control number of jobs used to lower impact on running nodes
25      export MAKEFLAGS="-j{{ beacon_node_update_build_jobs | int }}"
26  {% if beacon_node_nim_commit is defined and beacon_node_nim_commit != "" %}
27      export NIM_COMMIT={{ beacon_node_nim_commit }}
28  {% endif %}
29  
30      make update OVERRIDE=1
31      make {{ beacon_node_update_build_targets | join(" ") }} \
32          LOG_LEVEL="{{ beacon_node_update_build_log_level }}" \
33          NIMFLAGS="{{ beacon_node_update_build_nim_flags }}"
34  
35      # Rename binaries to match commit they were built from.
36  {% for target in beacon_node_update_build_targets %}
37      mv "build/{{ target }}" "build/{{ target }}_${COMMIT}"
38  {% endfor %}
39  
40      # Create a symbolic link to the latest version
41  {% for target in beacon_node_update_build_targets %}
42      ln -vfrs build/{{ target }}_${COMMIT} {{ beacon_node_bin_path }}/{{ target }}
43  {% endfor %}
44  
45      # Delete copies that are older than N days
46      find build -mtime +{{ beacon_node_update_build_days_kept }} -exec rm '{}' \+
47  }
48  
49  #-------------------------------------------------------------------------------
50  
51  BRANCH="${1:-{{ beacon_node_repo_branch }}}"
52  SERVICE="{{ beacon_node_service_name }}.service"
53  SERVICE_PATH="{{ beacon_node_service_path }}"
54  
55  echo " >>> Build Start: $(date -u +"%Y-%m-%dT%H:%M:%SZ")"
56  echo " >>> Building branch: ${BRANCH}"
57  cd "${SERVICE_PATH}"
58  
59  if [[ "${USER}" != "{{ beacon_node_user }}" ]]; then
60      echo "Incorrect user: ${USER}" >&2
61      echo "Expected: {{ beacon_node_user }}" >&2
62      exit 1
63  fi
64  
65  # Build the Beacon node binaries
66  pushd repo >/dev/null
67  
68  # Detached HEAD means we're probably on a tag
69  if headIsDetached; then
70      echo " >>> Deatached HEAD, nothing to fetch."
71  else
72      echo " >>> Fetching changes..."
73      fetchChanges
74  fi
75  
76  COMMIT=$(git rev-parse --short=8 HEAD)
77  
78  if binaryExists && [[ ! "${*}" =~ '^.* --force .*$' ]]; then
79      echo " >>> Binary already built"
80      exit 0
81  else
82      echo " >>> Building binaries..."
83      buildBinaries
84  fi
85  
86  {% if beacon_node_update_restarts_service %}
87  # Avoid faiure on first Ansible run due to missing service.
88  if [[ $(systemctl is-enabled "${SERVICE}" || true) != "enabled" ]]; then
89      echo " !!! No service to restart!"
90      exit
91  else
92      echo " >>> Restarting service..."
93      sudo systemctl restart "${SERVICE}"
94  fi
95  
96  {% endif %}
97  popd >/dev/null
98  
99  echo " >>> SUCCESS"