~newell-jensen/maas/update-fix-1508741-1.9

« back to all changes in this revision

Viewing changes to scripts/maas-import-isos

  • Committer: Andres Rodriguez
  • Date: 2012-03-01 15:42:15 UTC
  • mto: This revision was merged to the branch mainline in revision 204.
  • Revision ID: andreserl@ubuntu.com-20120301154215-eaq1nlfzdwn8zst6
move bin/ to scripts/

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
#
 
3
#    maas-import-isos - sync and import Ubuntu isos into cobbler
 
4
#
 
5
#    Copyright (C) 2011-2012 Canonical
 
6
#
 
7
#    Authors:
 
8
#               Marc Cluet <marc.cluet@canonical.com>
 
9
#               Dustin Kirkland <kirkland@canonical.com>
 
10
#               Andres Rodriguez <andres.rodriguez@canonical.com>
 
11
#
 
12
#    This program is free software: you can redistribute it and/or modify
 
13
#    it under the terms of the GNU Affero General Public License as
 
14
#    published by the Free Software Foundation, version 3 of the License.
 
15
#
 
16
#    This program is distributed in the hope that it will be useful,
 
17
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
#    GNU Affero General Public License for more details.
 
20
#
 
21
#    You should have received a copy of the GNU Affero General Public License
 
22
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
23
 
 
24
# Ensure root
 
25
if [ "$(id -u)" != "0" ]; then
 
26
        echo "ERROR: $0 must run as root" 2>&1
 
27
        exit 1
 
28
fi
 
29
 
 
30
# Definitions for supported releases and architectures
 
31
[ -r /etc/maas/import_isos ] && . /etc/maas/import_isos
 
32
[ -n "$RELEASES" ] || RELEASES=$(distro-info --supported)
 
33
[ -n "$ARCHES" ] || ARCHES="amd64 i386"
 
34
[ -n "$KSDIR" ] || KSDIR="/var/lib/cobbler/kickstarts"
 
35
[ -n "$PRIORITY" ] || PRIORITY="critical"
 
36
[ -n "$LOCALE" ] || LOCALE="en_US"
 
37
[ -n "$MGMTCLASS_AVAILABLE" ] || MGMTCLASS_AVAILABLE="maas-juju-available"
 
38
[ -n "$MGMTCLASS_ACQUIRED" ] || MGMTCLASS_ACQUIRED="maas-juju-acquired"
 
39
[ -n "$INTERFACE" ] || INTERFACE="auto"
 
40
[ -n "$KOPTS" ] || KOPTS="priority=$PRIORITY locale=$LOCALE netcfg/choose_interface=$INTERFACE"
 
41
[ -n "$ENLIST_PROFILE" ] || ENLIST_PROFILE="ubuntu-enlist"
 
42
if [ -n "$SERVER_IP" ]; then
 
43
        KOPTS="$KOPTS log_host=$SERVER_IP log_port=514"
 
44
else
 
45
        if grep -qs "^server: " /etc/cobbler/settings >/dev/null 2>&1; then
 
46
                SERVER_IP=$(grep "^server: " /etc/cobbler/settings | awk '{print $2}')
 
47
                KOPTS="$KOPTS log_host=$SERVER_IP log_port=514"
 
48
        fi
 
49
fi
 
50
 
 
51
update_settings(){
 
52
        # Updates the parent profiles with new settings. Only affects KOPTS
 
53
        for r in $RELEASES; do
 
54
                for a in $ARCHES; do
 
55
                        [ "$a" = "amd64" ] && a="x86_64"
 
56
                        # If profile exists, update it.
 
57
                        if (cobbler profile list | grep -qs " $r-$a$"); then
 
58
                                cobbler profile edit --name="$r-$a" --kopts="$KOPTS"
 
59
                        fi
 
60
                done
 
61
        done
 
62
}
 
63
 
 
64
import_isos(){
 
65
        # Wget and import net install ISOs
 
66
        for r in $RELEASES; do
 
67
                for a in $ARCHES; do
 
68
                        [ "$a" = "amd64" ] && a="x86_64"
 
69
                        # Skip if cobbler already has this distro/arch combo
 
70
                        if ! (cobbler distro list | grep -qs " $r-$a$"); then
 
71
                                # Import the iso
 
72
                                cobbler-ubuntu-import $r-$a
 
73
                                cobbler profile edit --name="$r-$a" --kopts="$KOPTS" --kickstart="$KSDIR/maas.preseed"
 
74
                        else
 
75
                                # check archive for an updated ISO, update our cache if necessary
 
76
                                cobbler-ubuntu-import -c $r-$a && cobbler-ubuntu-import -u $r-$a
 
77
                                # Make sure the profile is using the MaaS preseed
 
78
                                cobbler profile edit --name="$r-$a" --kickstart="$KSDIR/maas.preseed"
 
79
 
 
80
                        fi
 
81
                        # Skip if cobbler already has this distro/arch profile
 
82
                        if ! (cobbler profile list | grep -qs " $r-$a-juju$"); then
 
83
                                # Add JuJu sub-profile based on the name of the imported ISO
 
84
                                cobbler profile add --name="$r-$a-juju" --parent="$r-$a" --kickstart="$KSDIR/juju.preseed"
 
85
                        fi
 
86
                done
 
87
        done
 
88
}
 
89
 
 
90
add_mgmt_classes(){
 
91
        # Add Management Classes for JuJu
 
92
        for m in $MGMTCLASS_AVAILABLE $MGMTCLASS_ACQUIRED; do
 
93
                (cobbler mgmtclass list | grep -qs " $m$") || cobbler mgmtclass add --name="$m"
 
94
        done
 
95
}
 
96
 
 
97
add_enlist_profile(){
 
98
        STABLE=$(distro-info --stable)
 
99
        DEVEL=$(distro-info --devel)
 
100
        PARENT_PROFILE=""
 
101
 
 
102
        # Check what release to use as parent profile
 
103
        if [ `echo $STABLE "precise" | awk '{ print ($1 >= $2) ? "True" : "False" }'` = True ]; then
 
104
                PARENT_PROFILE="$STABLE-i386"
 
105
        else
 
106
                PARENT_PROFILE="$DEVEL-i386"
 
107
        fi
 
108
 
 
109
        # Add enlist profile
 
110
        if cobbler profile list | grep -qs " $ENLIST_PROFILE"; then
 
111
                cobbler profile edit --name="$ENLIST_PROFILE" --parent="$PARENT_PROFILE"
 
112
        else
 
113
                cobbler profile add --name="$ENLIST_PROFILE" --parent="$PARENT_PROFILE" --kickstart="$KSDIR/ubuntu-enlist.preseed"
 
114
        fi
 
115
 
 
116
        # Add 'default' system if doesn't exist.
 
117
        (cobbler system list | grep -qs " default") || cobbler system add --name="default" --profile="$ENLIST_PROFILE"
 
118
}
 
119
 
 
120
Usage() {
 
121
        cat <<EOF
 
122
Usage: ${0##*/} [ options ]
 
123
 
 
124
   import Ubuntu releases and update default MaaS settings
 
125
 
 
126
   options:
 
127
      -i | --import-isos      Import the Ubuntu ISOs and update default
 
128
                              settings used for MaaS. (default)
 
129
      -u | --update-settings  Updates all profiles based on new settings
 
130
                              in /etc/maas/import_isos.
 
131
 
 
132
   The --import process downloads and imports a list of Ubuntu releases,
 
133
   adding the default MaaS settings for each of the profiles.  If a
 
134
   release has already been imported but is out of date, the updated ISO
 
135
   will be downloaded again and reimported.
 
136
 
 
137
   If the ISOs have already been imported, update-settings will update all
 
138
   the profiles within cobbler based on the modifications of
 
139
   /etc/maas/import_isos.
 
140
 
 
141
   Example:
 
142
    - ${0##*/} -i
 
143
EOF
 
144
}
 
145
 
 
146
bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; exit 1; }
 
147
 
 
148
short_opts="hiu"
 
149
long_opts="import-isos,update-settings"
 
150
getopt_out=$(getopt --name "${0##*/}" \
 
151
        --options "${short_opts}" --long "${long_opts}" -- "$@") &&
 
152
        eval set -- "${getopt_out}" ||
 
153
        bad_Usage
 
154
 
 
155
do_import_isos=false
 
156
do_update_settings=false
 
157
 
 
158
while [ $# -ne 0 ]; do
 
159
        cur=${1};
 
160
        case "$cur" in
 
161
                -h|--help) Usage ; exit 0;;
 
162
                -i|--import-isos) do_import_isos=true;;
 
163
                -u|--update-settings) do_update_settings=true;;
 
164
                --) shift; break;;
 
165
        esac
 
166
        shift;
 
167
done
 
168
 
 
169
## check arguments here
 
170
[ $# -ne 0 ] && bad_Usage
 
171
 
 
172
# if no action specified, import by default
 
173
( ! $do_import_isos && ! $do_update_settings  ) && do_import_isos=true
 
174
# do not allow import + update
 
175
( $do_import_isos && $do_update_settings ) && bad_Usage
 
176
 
 
177
$do_import_isos && import_isos 
 
178
$do_update_settings && update_settings 
 
179
 
 
180
# Add management classes if new have being defined.
 
181
add_mgmt_classes
 
182
 
 
183
# Add enlist profile if doesn't exist
 
184
add_enlist_profile
 
185
 
 
186
# Sync changes with cobbler daemon
 
187
cobbler sync