summaryrefslogtreecommitdiff
path: root/files/cloud.sh
blob: a18820423e31a3395fae28ad076f25885dd0261b (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
#!/bin/bash

keypair_user="${1}"
pubkey_path="${2}"
dns_servers="${3}"

if [ "${keypair_user}" == "" ]; then
    echo "No keypair user was supplied." >&2
    exit 1
elif [ "${pubkey_path}" == "" ]; then
    echo "No pubkey path was supplied." >&2
    exit 1
elif [ "${dns_servers}" == "" ]; then
    echo "No dns servers supplied." >&2
    exit 1
fi

nameservers=$(for i in ${dns_servers}; do echo -n "'${i}',"; done)
nameservers="${nameservers::-1}"

cat > "data/etc/cloud/cloud.cfg" <<EOF
#cloud-config
# vim: syntax=yaml

users:
  - name: default
  - name: ${keypair_user}
    groups: sudo
    shell: /bin/bash
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
    ssh-authorized-keys:
      - $(cat ${pubkey_path})

manage-resolv-conf: True

resolv_conf:
  nameservers: [${nameservers}]

write_files:
  - content: |
      127.0.0.1       localhost
      127.0.2.1       openbook-database

      # The following lines are desirable for IPv6 capable hosts
      ::1     localhost       ip6-localhost ip6-loopback
      ff02::1 ip6-allnodes
      ff02::2 ip6-allrouters
    path: /etc/hosts

  - path: /root/cloudcfg.sh
    permissions: "0755"
    content: |
      #!/bin/bash

      # Prepare the instance for ansible. This amounts to installing python2.

      python=$(which python || which python3 || which python2 || false)

      case "$(${python} -m platform)" in
          *Ubuntu*)
              export DEBIAN_FRONTEND=noninteractive
              apt-get update
              apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" dist-upgrade
              apt-get -y install python python2.7 ntp ntpdate
              ;;
          *centos*|*redhat*)
              # RHEL systems still use python2 by default; nothing to do
              ;;
          *gentoo*)
              if [ ! -d /usr/portage/sys-devel/gcc ]; then
                  emerge-webrsync
              fi
              emerge --oneshot --update dev-lang/python:2.7 app-portage/gentoolkit net-misc/ntp
              rc-update add ntp-client default
              rc-update add ntpd default
              ;;
      esac

      exit 0

runcmd:
  - /root/cloudcfg.sh
EOF

exit 0