summaryrefslogtreecommitdiff
path: root/test/lib/ansible_test/_data/setup/remote.sh
blob: 6a70863a0ee9ff217ea9c9dbb5eddf874bda4fdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/sh

set -eu

platform="$1"
python_version="$2"
python_interpreter="python${python_version}"

cd ~/

install_pip () {
    if ! "${python_interpreter}" -m pip.__main__ --version --disable-pip-version-check 2>/dev/null; then
        curl --silent --show-error https://bootstrap.pypa.io/get-pip.py -o /tmp/get-pip.py
        "${python_interpreter}" /tmp/get-pip.py --disable-pip-version-check --quiet
        rm /tmp/get-pip.py
    fi
}

if [ "${platform}" = "freebsd" ]; then
    py_version="$(echo "${python_version}" | tr -d '.')"

    while true; do
        env ASSUME_ALWAYS_YES=YES pkg bootstrap && \
        pkg install -q -y \
            bash \
            curl \
            gtar \
            "python${py_version}" \
            "py${py_version}-Jinja2" \
            "py${py_version}-virtualenv" \
            "py${py_version}-cryptography" \
            sudo \
        && break
        echo "Failed to install packages. Sleeping before trying again..."
        sleep 10
    done

    install_pip

    if ! grep '^PermitRootLogin yes$' /etc/ssh/sshd_config > /dev/null; then
        sed -i '' 's/^# *PermitRootLogin.*$/PermitRootLogin yes/;' /etc/ssh/sshd_config
        service sshd restart
    fi
elif [ "${platform}" = "rhel" ]; then
    if grep '8\.' /etc/redhat-release; then
        while true; do
            yum module install -q -y python36 && \
            yum install -q -y \
                gcc \
                python3-devel \
                python3-jinja2 \
                python3-virtualenv \
                python3-cryptography \
                iptables \
            && break
            echo "Failed to install packages. Sleeping before trying again..."
            sleep 10
        done
    else
        while true; do
            yum install -q -y \
                gcc \
                python-devel \
                python-virtualenv \
                python2-cryptography \
            && break
            echo "Failed to install packages. Sleeping before trying again..."
            sleep 10
        done

        install_pip
    fi
elif [ "${platform}" = "centos" ]; then
    while true; do
        yum install -q -y \
            gcc \
            python-devel \
            python-virtualenv \
            python2-cryptography \
            libffi-devel \
            openssl-devel \
        && break
        echo "Failed to install packages. Sleeping before trying again..."
        sleep 10
    done

    install_pip
elif [ "${platform}" = "osx" ]; then
    while true; do
        pip install --disable-pip-version-check --quiet \
            'virtualenv<20' \
        && break
        echo "Failed to install packages. Sleeping before trying again..."
        sleep 10
    done
elif [ "${platform}" = "aix" ]; then
    chfs -a size=1G /
    chfs -a size=4G /usr
    chfs -a size=1G /var
    chfs -a size=1G /tmp
    chfs -a size=2G /opt
    while true; do
        yum install -q -y \
            gcc \
            libffi-devel \
            python-jinja2 \
            python-cryptography \
            python-pip && \
        pip install --disable-pip-version-check --quiet virtualenv \
        && break
        echo "Failed to install packages. Sleeping before trying again..."
        sleep 10
    done
fi

# Generate our ssh key and add it to our authorized_keys file.
# We also need to add localhost's server keys to known_hosts.

if [ ! -f "${HOME}/.ssh/id_rsa.pub" ]; then
    ssh-keygen -m PEM -q -t rsa -N '' -f "${HOME}/.ssh/id_rsa"
    # newer ssh-keygen PEM output (such as on RHEL 8.1) is not recognized by paramiko
    touch "${HOME}/.ssh/id_rsa.new"
    chmod 0600 "${HOME}/.ssh/id_rsa.new"
    sed 's/\(BEGIN\|END\) PRIVATE KEY/\1 RSA PRIVATE KEY/' "${HOME}/.ssh/id_rsa" > "${HOME}/.ssh/id_rsa.new"
    mv "${HOME}/.ssh/id_rsa.new" "${HOME}/.ssh/id_rsa"
    cat "${HOME}/.ssh/id_rsa.pub" >> "${HOME}/.ssh/authorized_keys"
    chmod 0600 "${HOME}/.ssh/authorized_keys"
    for key in /etc/ssh/ssh_host_*_key.pub; do
        pk=$(cat "${key}")
        echo "localhost ${pk}" >> "${HOME}/.ssh/known_hosts"
    done
fi

# Improve prompts on remote host for interactive use.
# shellcheck disable=SC1117
cat << EOF > ~/.bashrc
if ls --color > /dev/null 2>&1; then
    alias ls='ls --color'
elif ls -G > /dev/null 2>&1; then
    alias ls='ls -G'
fi
export PS1='\[\e]0;\u@\h: \w\a\]\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
EOF

# Make sure ~/ansible/ is the starting directory for interactive shells.
if [ "${platform}" = "osx" ]; then
    echo "cd ~/ansible/" >> ~/.bashrc
fi