|
1 |
| -#!/bin/sh |
| 1 | +#!/bin/bash |
2 | 2 | #
|
3 |
| -# setup.sh.example |
| 3 | +# This script can be used to safely inject secrets into the Docker |
| 4 | +# build process. |
4 | 5 | #
|
5 |
| -# This script is then used in the Docker image build process as a secret. |
6 |
| -# It should set up the default remote and authenticate if necessary. |
| 6 | +# Configure each `setup_` function defined as required and uncomment |
| 7 | +# them at the bottom. Then save the file as `setup.sh`, which `Makefile.docker` |
| 8 | +# will automatically add as a secret to the Docker image build process. |
7 | 9 | #
|
8 |
| -# Copy this file to setup.sh and modify it to allow Docker configure Conan in |
9 |
| -# the Docker build process. If this file is absent, the default Conan settings |
10 |
| -# are used. |
11 |
| -# |
12 |
| -# Authenticate with the default remote using the correct username and password. |
13 |
| -# This should run without any user interaction. |
14 | 10 |
|
| 11 | +# If any part of this script fails, the build process will too. |
15 | 12 | set -e
|
16 | 13 |
|
17 |
| -export CONAN_REMOTE="https://artifactory.example.com/artifactory/api/conan/cloe-conan-local" |
18 |
| -export CONAN_REMOTE_VERIFY_SSL="True" |
19 |
| -export CONAN_LOGIN_USERNAME= |
20 |
| -export CONAN_PASSWORD= |
| 14 | +setup_hosts() { |
| 15 | + # If you need to update /etc/hosts, you can use `--add-host` on the command line |
| 16 | + # or you can add them here. |
21 | 17 |
|
22 |
| -export VI_LIC_SERVER="vtd-licenses.example.com" |
| 18 | + local HOSTS=( |
| 19 | + "93.184.216.34 example.com" |
| 20 | + ) |
23 | 21 |
|
24 |
| -# Don't try to set up remotes if --network=none. |
25 |
| -if [ "$(ls /sys/class/net)" != "lo" ]; then |
26 |
| - # Set the request timeout to 360 seconds to work-around slow servers. |
27 |
| - conan config set general.request_timeout=360 |
| 22 | + cat /etc/hosts >> /tmp/hosts |
| 23 | + for line in $HOSTS; do |
| 24 | + echo $line >> /tmp/hosts |
| 25 | + done |
| 26 | + mount -o bind /tmp/hosts /etc/hosts |
28 | 27 |
|
29 |
| - if [ "${CONAN_REMOTE}" != "" ]; then |
30 |
| - conan remote clean |
31 |
| - conan remote add default "${CONAN_REMOTE}" "${CONAN_REMOTE_VERIFY_SSL}" |
32 |
| - fi |
| 28 | + CLEANUP_FUNCTIONS+=(cleanup_hosts) |
| 29 | +} |
| 30 | + |
| 31 | +cleanup_hosts() { |
| 32 | + umount /etc/hosts |
| 33 | +} |
| 34 | + |
| 35 | +setup_ssh() { |
| 36 | + # If you need SSH for any reason, you can make your SSH agent available to Docker |
| 37 | + # by passing the arguments `--ssh default=$SSH_AUTH_SOCK`. |
| 38 | + # You can then use ssh-scankey to add known hosts or hosts you want to fetch |
| 39 | + # things from. |
| 40 | + # |
| 41 | + # Using known-hosts is a security feature, and this function effectively |
| 42 | + # circumvents these protections. Consider using a bind mount instead if |
| 43 | + # protection against man-in-the-middle attacks are important! |
| 44 | + |
| 45 | + local HOSTS=( |
| 46 | + "-p 80 github.com" |
| 47 | + ) |
| 48 | + |
| 49 | + for host in $HOSTS; do |
| 50 | + if grep -vqF "$host" ~/.ssh/known_hosts; then |
| 51 | + ssh-keyscan $host >> ~/.ssh/known_hosts |
| 52 | + fi |
| 53 | + done |
| 54 | + |
| 55 | + CLEANUP_FUNCTIONS+=(cleanup_ssh) |
| 56 | +} |
| 57 | + |
| 58 | +cleanup_ssh() { |
| 59 | + rm -rf ~/.ssh |
| 60 | +} |
| 61 | + |
| 62 | +network_available() { |
| 63 | + # If you need to check whether network is available, i.e. Docker network is |
| 64 | + # not "none", then you can use something like this, which checks that there is |
| 65 | + # a network interface that is not "lo". |
| 66 | + ip link | sed -nr 's/^[0-9]+: ([^:]+):.*/\1/p' | grep -vq lo |
| 67 | +} |
| 68 | + |
| 69 | +setup_conan() { |
| 70 | + # Authenticate with the default remote using the correct username and password. |
| 71 | + # This should run without any user interaction. |
| 72 | + |
| 73 | + local CONAN_REMOTE="https://artifactory.example.com/artifactory/api/conan/cloe-conan-local" |
| 74 | + local CONAN_REMOTE_VERIFY_SSL="True" |
| 75 | + local CONAN_LOGIN_USERNAME= |
| 76 | + local CONAN_PASSWORD= |
33 | 77 |
|
34 |
| - if [ "${CONAN_LOGIN_USERNAME}" != "" ]; then |
35 |
| - conan user --remote=default -p |
| 78 | + if network_available; then |
| 79 | + # Set the request timeout to 360 seconds to work-around slow servers. |
| 80 | + conan config set general.request_timeout=360 |
| 81 | + |
| 82 | + if [ "${CONAN_REMOTE}" != "" ]; then |
| 83 | + conan remote add default "${CONAN_REMOTE}" "${CONAN_REMOTE_VERIFY_SSL}" |
| 84 | + fi |
| 85 | + |
| 86 | + if [ "${CONAN_LOGIN_USERNAME}" != "" ]; then |
| 87 | + conan user --remote=default -p |
| 88 | + fi |
36 | 89 | fi
|
| 90 | + |
| 91 | + CLEANUP_FUNCTIONS+=(cleanup_conan) |
| 92 | +} |
| 93 | + |
| 94 | +cleanup_conan() { |
| 95 | + # Deauthenticate so that we don't leak credentials. |
| 96 | + conan user --clean |
| 97 | +} |
| 98 | + |
| 99 | +setup_vtd() { |
| 100 | + # Export environment variable telling VTD where it can find the license server: |
| 101 | + export VI_LIC_SERVER="vtd-licenses.example.com" |
| 102 | +} |
| 103 | + |
| 104 | +upload_conan_packages() { |
| 105 | + # Prequisites: |
| 106 | + # 1. You need to add a 'default' remote and authenticate with it. |
| 107 | + # 2. You need to keep the original 'conancenter' remote, so |
| 108 | + # that Conan can fetch missing export_sources files. |
| 109 | + conan upload -r default --all --force -c "*" |
| 110 | +} |
| 111 | + |
| 112 | +# This array with cleanup functions will be extended by each `setup_` function |
| 113 | +# that is called that needs cleanup after the Docker RUN step is finished. |
| 114 | +# This cleanup is ensured by the call to `trap` below. |
| 115 | +CLEANUP_FUNCTIONS=() |
| 116 | + |
| 117 | +cleanup_all() { |
| 118 | + for func in $CLEANUP_FUNCTIONS; do |
| 119 | + $func |
| 120 | + done |
| 121 | +} |
| 122 | + |
| 123 | +trap cleanup_all EXIT |
| 124 | + |
| 125 | +# Now uncomment the setups you want to happen in a Docker environment: |
| 126 | +# |
| 127 | +# In a Docker RUN step, it's possible to have `--network=none`, in which case |
| 128 | +# we probably don't need to do anything in this script. |
| 129 | +if [ -f /.dockerenv ] && [ "$(ls /sys/class/net)" != "lo" ]; then |
| 130 | + #setup_hosts |
| 131 | + #setup_ssh |
| 132 | + #setup_conan |
| 133 | + #setup_vtd |
37 | 134 | fi
|
38 | 135 |
|
39 |
| -unset CONAN_REMOTE |
40 |
| -unset CONAN_REMOTE_VERIFY_SSL |
41 |
| -unset CONAN_LOGIN_USERNAME |
42 |
| -unset CONAN_PASSWORD |
| 136 | +set +e |
0 commit comments