summaryrefslogtreecommitdiffstats
path: root/conf/osx/createpackage.sh
blob: 32cbebfc50a8875b7103ce9f89cac68bbe046ff7 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
#
# Script to build an "old style" not flat pkg out of the puppet repository.
#
# Author: Nigel Kersten (nigelk@google.com)
#
# Last Updated: 2008-07-31
#
# Copyright 2008 Google Inc.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#      http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License


INSTALLRB="install.rb"
BINDIR="/usr/bin"
SITELIBDIR="/usr/lib/ruby/site_ruby/1.8"
PACKAGEMAKER="/Developer/usr/bin/packagemaker"
PROTO_PLIST="PackageInfo.plist"
PREFLIGHT="preflight"


function find_installer() {
  # we walk up three directories to make this executable from the root, 
  # root/conf or root/conf/osx
  if [ -f "./${INSTALLRB}" ]; then
    installer="$(pwd)/${INSTALLRB}"
  elif [ -f "../${INSTALLRB}" ]; then
    installer="$(pwd)/../${INSTALLRB}"
  elif [ -f "../../${INSTALLRB}" ]; then
    installer="$(pwd)/../${INSTALLRB}"
  else
    installer=""
  fi
}

function find_puppet_root() {
  puppet_root=$(dirname "${installer}")
}

function install_puppet() {
  echo "Installing Puppet to ${pkgroot}"
  "${installer}" --destdir="${pkgroot}" --bindir="${BINDIR}" --sitelibdir="${SITELIBDIR}"
  chown -R root:admin "${pkgroot}"
}

function get_puppet_version() {
  puppet_version=$(RUBYLIB="${pkgroot}/${SITELIBDIR}:${RUBYLIB}" ruby -e "require 'puppet'; puts Puppet.version")
}

function prepare_package() {
  # As we can't specify to follow symlinks from the command line, we have
  # to go through the hassle of creating an Info.plist file for packagemaker
  # to look at for package creation and substitue the version strings out.
  # Major/Minor versions can only be integers, so we have "0" and "245" for
  # puppet version 0.24.5
  # Note too that for 10.5 compatibility this Info.plist *must* be set to 
  # follow symlinks.
  VER1=$(echo ${puppet_version} | awk -F "." '{print $1}')
  VER2=$(echo ${puppet_version} | awk -F "." '{print $2}')
  VER3=$(echo ${puppet_version} | awk -F "." '{print $3}')
  major_version="${VER1}"
  minor_version="${VER2}${VER3}"
  cp "${puppet_root}/conf/osx/${PROTO_PLIST}" "${pkgtemp}"
  sed -i '' "s/{SHORTVERSION}/${puppet_version}/g" "${pkgtemp}/${PROTO_PLIST}"
  sed -i '' "s/{MAJORVERSION}/${major_version}/g" "${pkgtemp}/${PROTO_PLIST}"
  sed -i '' "s/{MINORVERSION}/${minor_version}/g" "${pkgtemp}/${PROTO_PLIST}"
  
  # We need to create a preflight script to remove traces of previous
  # puppet installs due to limitations in Apple's pkg format.
  mkdir "${pkgtemp}/scripts"
  cp "${puppet_root}/conf/osx/${PREFLIGHT}" "${pkgtemp}/scripts"
  
  # substitute in the sitelibdir specified above on the assumption that this
  # is where any previous puppet install exists that should be cleaned out.
  sed -i '' "s|{SITELIBDIR}|${SITELIBDIR}|g" "${pkgtemp}/scripts/${PREFLIGHT}"
  chmod 0755 "${pkgtemp}/scripts/${PREFLIGHT}"
}

function create_package() {
  rm -fr "$(pwd)/puppet-${puppet_version}.pkg"
  echo "Building package"
  echo "Note that packagemaker is reknowned for spurious errors. Don't panic."
  "${PACKAGEMAKER}" --root "${pkgroot}" \
                    --info "${pkgtemp}/${PROTO_PLIST}" \
                    --scripts ${pkgtemp}/scripts \
                    --out "$(pwd)/puppet-${puppet_version}.pkg"
  if [ $? -ne 0 ]; then
    echo "There was a problem building the package."
    cleanup_and_exit 1
    exit 1
  else
    echo "The package has been built at:"
    echo "$(pwd)/puppet-${puppet_version}.pkg"
  fi
}

function cleanup_and_exit() {
  if [ -d "${pkgroot}" ]; then
    rm -fr "${pkgroot}"
  fi
  if [ -d "${pkgtemp}" ]; then
    rm -fr "${pkgtemp}"
  fi
  exit $1
}

# Program entry point
function main() {

  if [ $(whoami) != "root" ]; then
    echo "This script needs to be run as root via su or sudo."
    cleanup_and_exit 1
  fi

  find_installer
  
  if [ ! "${installer}" ]; then
    echo "Unable to find ${INSTALLRB}"
    cleanup_and_exit 1
  fi

  find_puppet_root
  
  if [ ! "${puppet_root}" ]; then
    echo "Unable to find puppet repository root."
    cleanup_and_exit 1
  fi
  
  pkgroot=$(mktemp -d -t puppetpkg)
  
  if [ ! "${pkgroot}" ]; then
    echo "Unable to create temporary package root."
    cleanup_and_exit 1
  fi
  
  pkgtemp=$(mktemp -d -t puppettmp)
  
  if [ ! "${pkgtemp}" ]; then
    echo "Unable to create temporary package root."
    cleanup_and_exit 1
  fi
  
  install_puppet
  get_puppet_version
  
  if [ ! "${puppet_version}" ]; then
    echo "Unable to retrieve puppet version"
    cleanup_and_exit 1
  fi
  
  prepare_package
  create_package
  
  cleanup_and_exit 0
}

main "$@"