chef-full.erb 6.13 KB
Newer Older
1 2
<%= "https_proxy=\"#{knife_config[:bootstrap_proxy]}\" export https_proxy" if knife_config[:bootstrap_proxy] %>
<%= "no_proxy=\"#{knife_config[:bootstrap_no_proxy]}\" export no_proxy" if knife_config[:bootstrap_no_proxy] %>
3

4 5 6 7 8 9 10 11 12 13
if test "x$TMPDIR" = "x"; then
  tmp="/tmp"
else
  tmp=$TMPDIR
fi

# secure-ish temp dir creation without having mktemp available (DDoS-able but not exploitable)
tmp_dir="$tmp/install.sh.$$"
(umask 077 && mkdir $tmp_dir) || exit 1

14
exists() {
Thom May's avatar
Thom May committed
15
  if command -v $1 >/dev/null 2>&1
16 17 18 19 20 21 22
  then
    return 0
  else
    return 1
  fi
}

23
http_404_error() {
24
  echo "ERROR 404: Could not retrieve a valid install.sh!"
25 26 27 28 29 30 31 32 33 34 35 36
  exit 1
}

capture_tmp_stderr() {
  # spool up /tmp/stderr from all the commands we called
  if test -f "$tmp_dir/stderr"; then
    output=`cat $tmp_dir/stderr`
    stderr_results="${stderr_results}\nSTDERR from $1:\n\n$output\n"
    rm $tmp_dir/stderr
  fi
}

Julian Dunn's avatar
Julian Dunn committed
37 38 39
# do_wget URL FILENAME
do_wget() {
  echo "trying wget..."
40 41 42 43 44 45 46 47 48 49 50
  wget <%= "--proxy=on " if knife_config[:bootstrap_proxy] %> <%= knife_config[:bootstrap_wget_options] %> -O "$2" "$1" 2>$tmp_dir/stderr
  rc=$?
  # check for 404
  grep "ERROR 404" $tmp_dir/stderr 2>&1 >/dev/null
  if test $? -eq 0; then
    http_404_error
  fi

  # check for bad return status or empty output
  if test $rc -ne 0 || test ! -s "$2"; then
    capture_tmp_stderr "wget"
Julian Dunn's avatar
Julian Dunn committed
51 52
    return 1
  fi
53

Julian Dunn's avatar
Julian Dunn committed
54 55 56 57 58 59
  return 0
}

# do_curl URL FILENAME
do_curl() {
  echo "trying curl..."
60
  curl -sL <%= "--proxy \"#{knife_config[:bootstrap_proxy]}\" " if knife_config[:bootstrap_proxy] %> <%= knife_config[:bootstrap_curl_options] %> -D $tmp_dir/stderr -o "$2" "$1" 2>$tmp_dir/stderr
61 62 63 64 65 66 67 68 69 70
  rc=$?
  # check for 404
  grep "404 Not Found" $tmp_dir/stderr 2>&1 >/dev/null
  if test $? -eq 0; then
    http_404_error
  fi

  # check for bad return status or empty output
  if test $rc -ne 0 || test ! -s "$2"; then
    capture_tmp_stderr "curl"
Julian Dunn's avatar
Julian Dunn committed
71 72
    return 1
  fi
73

Julian Dunn's avatar
Julian Dunn committed
74 75 76 77 78 79
  return 0
}

# do_fetch URL FILENAME
do_fetch() {
  echo "trying fetch..."
80
  fetch -o "$2" "$1" 2>$tmp_dir/stderr
Julian Dunn's avatar
Julian Dunn committed
81 82 83 84 85 86 87 88
  # check for bad return status
  test $? -ne 0 && return 1
  return 0
}

# do_perl URL FILENAME
do_perl() {
  echo "trying perl..."
89
  perl -e "use LWP::Simple; getprint(shift @ARGV);" "$1" > "$2" 2>$tmp_dir/stderr
90 91 92 93 94 95 96 97 98 99 100 101 102
  rc=$?
  # check for 404
  grep "404 Not Found" $tmp_dir/stderr 2>&1 >/dev/null
  if test $? -eq 0; then
    http_404_error
  fi

  # check for bad return status or empty output
  if test $rc -ne 0 || test ! -s "$2"; then
    capture_tmp_stderr "perl"
    return 1
  fi

Julian Dunn's avatar
Julian Dunn committed
103 104 105 106 107 108
  return 0
}

# do_python URL FILENAME
do_python() {
  echo "trying python..."
109 110 111 112 113 114 115 116 117 118 119 120 121
  python -c "import sys,urllib2 ; sys.stdout.write(urllib2.urlopen(sys.argv[1]).read())" "$1" > "$2" 2>$tmp_dir/stderr
  rc=$?
  # check for 404
  grep "HTTP Error 404" $tmp_dir/stderr 2>&1 >/dev/null
  if test $? -eq 0; then
    http_404_error
  fi

  # check for bad return status or empty output
  if test $rc -ne 0 || test ! -s "$2"; then
    capture_tmp_stderr "python"
    return 1
  fi
Julian Dunn's avatar
Julian Dunn committed
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 149 150 151 152 153 154 155
  return 0
}

# do_download URL FILENAME
do_download() {
  PATH=/opt/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sfw/bin:/sbin:/bin:/usr/sbin:/usr/bin
  export PATH

  echo "downloading $1"
  echo "  to file $2"

  # we try all of these until we get success.
  # perl, in particular may be present but LWP::Simple may not be installed

  if exists wget; then
    do_wget $1 $2 && return 0
  fi

  if exists curl; then
    do_curl $1 $2 && return 0
  fi

  if exists fetch; then
    do_fetch $1 $2 && return 0
  fi

  if exists perl; then
    do_perl $1 $2 && return 0
  fi

  if exists python; then
    do_python $1 $2 && return 0
  fi

156
  echo ">>>>>> wget, curl, fetch, perl, or python not found on this instance."
157 158 159 160 161

  if test "x$stderr_results" != "x"; then
    echo "\nDEBUG OUTPUT FOLLOWS:\n$stderr_results"
  fi

Julian Dunn's avatar
Julian Dunn committed
162 163 164
  return 16
}

165 166 167 168 169 170
<%# Run any custom commands before installing chef-client -%>
<%# Ex. wait for cloud-init to complete -%>
<% if knife_config[:bootstrap_preinstall_command] %>
  <%= knife_config[:bootstrap_preinstall_command] %>
<% end %>

171 172 173
<% if knife_config[:bootstrap_install_command] %>
  <%= knife_config[:bootstrap_install_command] %>
<% else %>
174
  install_sh="<%= knife_config[:bootstrap_url] ? knife_config[:bootstrap_url] : "https://omnitruck.chef.io/chef/install.sh" %>"
175
  if test -f /usr/bin/<%= Chef::Dist::CLIENT %>; then
176
    echo "-----> Existing <%= Chef::Dist::PRODUCT %> installation detected"
Thom May's avatar
Thom May committed
177
  else
178
    echo "-----> Installing Chef Omnibus (<%= @config[:channel] %>/<%= version_to_install %>)"
179
    do_download ${install_sh} $tmp_dir/install.sh
180
    sh $tmp_dir/install.sh -P <%= @config[:bootstrap_product] %> -c <%= @config[:channel] %> -v <%= version_to_install %>
181
  fi
182
<% end %>
183

184 185 186 187
if test "x$tmp_dir" != "x"; then
  rm -r "$tmp_dir"
fi

188
mkdir -p <%= ChefConfig::Config.etc_chef_dir(false) %>
189

190
<% if client_pem -%>
191
(umask 077 && (cat > <%= ChefConfig::Config.etc_chef_dir(false) %>/client.pem <<'EOP'
192 193
<%= ::File.read(::File.expand_path(client_pem)) %>
EOP
194
)) || exit 1
195 196 197
<% end -%>

<% if validation_key -%>
198
(umask 077 && (cat > <%= ChefConfig::Config.etc_chef_dir(false) %>/validation.pem <<'EOP'
199 200
<%= validation_key %>
EOP
201
)) || exit 1
202
<% end -%>
203

204
<% if encrypted_data_bag_secret -%>
205
(umask 077 && (cat > <%= ChefConfig::Config.etc_chef_dir(false) %>/encrypted_data_bag_secret <<'EOP'
206 207
<%= encrypted_data_bag_secret %>
EOP
208
)) || exit 1
209 210
<% end -%>

211
<% unless trusted_certs.empty? -%>
212
mkdir -p <%= ChefConfig::Config.etc_chef_dir(false) %>/trusted_certs
213
<%= trusted_certs %>
214 215
<% end -%>

216 217
<%# Generate Ohai Hints -%>
<% unless @chef_config[:knife][:hints].nil? || @chef_config[:knife][:hints].empty? -%>
218
mkdir -p <%= ChefConfig::Config.etc_chef_dir(false) %>/ohai/hints
219 220

<% @chef_config[:knife][:hints].each do |name, hash| -%>
221
cat > <%= ChefConfig::Config.etc_chef_dir(false) %>/ohai/hints/<%= name %>.json <<'EOP'
222
<%= Chef::JSONCompat.to_json(hash) %>
223 224
EOP
<% end -%>
225
<% end -%>
226

227
cat > <%= ChefConfig::Config.etc_chef_dir(false) %>/client.rb <<'EOP'
228 229 230
<%= config_content %>
EOP

231
cat > <%= ChefConfig::Config.etc_chef_dir(false) %>/first-boot.json <<'EOP'
232
<%= Chef::JSONCompat.to_json(first_boot) %>
233 234
EOP

235
<% unless client_d.empty? -%>
236
mkdir -p /etc/chef/client.d
237 238 239
<%= client_d %>
<% end -%>

240
echo "Starting the first <%= Chef::Dist::PRODUCT %> Client run..."
241

242
<%= start_chef %>