aboutsummaryrefslogtreecommitdiff
path: root/templates/drupal.sh.erb
blob: afc3ed18f399f1381f431acdf309cd5062476747 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/bin/bash
#
# Drupal management script.
#

BASE="<%= apache_www_folder %>"
SERIES="5 6 7"

# Get drupal major version
function drupal_get_major {
  echo $1 | sed -e 's/\(^.\).*/\1/'
}

# Check for existing installations
function drupal_check_existing {
  if [ -e "$BASE/drupal-$1" ]; then
    echo "Folder $BASE/drupal-$1 already exists, skipping"
    exit 1
  fi  
}

# Iterate through all drupal instances
function drupal_iterate {
  for version in $SERIES; do
    # Setup base folder
    base="$BASE/drupal-$version"

    if [ ! -d "$base/sites" ]; then
      continue
    fi

    # Setup site folders
    cd $base/sites
    drupals="`ls -I default -I all`"
  
    # Issue updates
    for drupal in $drupals; do
      # Ignore symlinks so sites are updated just once
      if [ ! -h "$drupal" ] || [ ! -h "`readlink $drupal`" ]; then
        if [ "$1" != "cron" ]; then
          echo "Processing $drupal..."
        fi
        drush -l $drupal $*
      fi
    done
  
    # Fix permissions
    if [ -e "$base/sites/all/modules" ]; then
      chown -R root.root $base/sites/all/modules
    fi
    if [ -e "$base/sites/all/themes" ]; then
      chown -R root.root $base/sites/all/themes
    fi
  done
}

# Deploy a fresh drupal tree
function drupal_deploy {
  if [ -z "$1" ]; then
    echo "Usage: `basename $0` deploy <version> [--upgrade]"
    exit 1
  fi

  # Setup
  new="$1"
  drupal_series="`drupal_get_major $new`"
  cd $BASE
  drupal_check_existing $new

  # Deploy a fresh drupal tree
  wget http://ftp.drupal.org/files/projects/drupal-$new.tar.gz
  tar zxvf drupal-$new.tar.gz && rm drupal-$new.tar.gz
  chown -R root.root drupal-$new/

  # Upgrade mode, erase sites folder as the previous should be copied.
  if [ "$2" == "--upgrade" ]; then
    cd drupal-$new && rm -rf sites
  fi

  # Make symlink if needed.
  if [ ! -e "$BASE/drupal-$drupal_series" ]; then
    ( cd $BASE && ln -s drupal-$new drupal-$drupal_series )
  fi
}

# Upgrade a drupal instance using upstream source.
function drupal_upgrade {
  if [ "$#" != "2" ]; then
    echo "Usage: `basename $0` upgrade <old_version> <new_version>"
    exit 1
  fi

  # Setup
  old="$1"
  new="$2"
  old_major="`drupal_get_major $old`"
  new_major="`drupal_get_major $new`"
  extra_folders=""
  
  if [ "$old_major" != "$new_major" ]; then
    echo "Major versions doesn't match"
    exit 1
  fi

  drupal_check_existing $new
  
  # Set drupal series
  if [ "$new_major" == "4" ]; then
    # Get minor versions
    new_minor="`echo $new | sed -e "s/^$new_major\.//"`"
    old_minor="`echo $old | sed -e "s/^$old_major\.//"`"
  
    if [ "$old_minor" != "$new_minor" ]; then
      echo "Minor versions doesn't match"
      exit 1
    fi
    drupal_series="$new_major.$new_minor"
  else
    drupal_series="$new_major"
  fi
  
  cd $BASE
  
  # Deploy a fresh drupal tree
  drupal_deploy $new --upgrade

  # Ensure we're in the new drupal folder
  cd $BASE/drupal-$new

  # Copy files
  cp -Rp ../drupal-$old/{.htaccess,favicon.ico,files/,sites/} . &> /dev/null
  for extra_folder in $extra_folders; do
    if [ -d ../drupal-$old/$extra_folder ]; then
      cp -Rp ../drupal-$old/$extra_folder .
    fi
  done
  
  # Legacy stuff for Drupal 4.x.x
  if [ "$new_major" == "4" ]; then
    rsync -av ../drupal-$old/themes/ themes/
    for module in `ls ../drupal-$old/modules`; do
      if [ -d "../drupal-$old/modules/$module" ]; then
        cp -Rp ../drupal-$old/modules/$module modules/
      fi
    done
  fi
  
  # Copy installation profiles for Drupal 5.x or newer
  if [ "$new_major" != "4" ]; then
    rsync -av --exclude=default ../drupal-$old/profiles/ profiles/
  fi

  # Misc: copy image.imagemagick.inc to includes/ folder
  if [ -e "../drupal-$old/sites/all/modules/image/image.imagemagick.inc" ]; then
    cp ../drupal-$old/sites/all/modules/image/image.imagemagick.inc includes/
  fi
  
  # Change symlink to point to the new location
  cd $BASE ; rm -rf drupal-$drupal_series && ln -s drupal-$new drupal-$drupal_series

  # Done
  echo "Audit: `du -hs drupal-$old`"
  echo "Audit: `du -hs drupal-$new`"
  echo "Check procedure and remove drupal-$old once you make sure that everything is fine."
}

# Run a drupal makefile
function drupal_make {
  if [ -z "$1" ]; then
    echo "Usage: `basename $0` make <series>"
    exit 1
  fi

  series="$1"
  base="$BASE/drupal-$series"
  makefile="/usr/local/share/drupal/drupal$series.make"

  if [ -e "$makefile" ]; then
    if [ ! -e "$base" ]; then
      echo "Please deploy drupal code at $base first"
      exit 1
    fi
    drush dl drush_make
    ( cd $base && drush make -y --no-core $makefile )
  else
    echo "Makefile not found: $makefile"
    exit 1
  fi
}

# Run the video scheduler
function drupal_video_scheduler {
  if [ -z "$2" ]; then
    echo "Usage: `basename $0` video-scheduler <site> <series>"
    exit 1
  fi

  site="$1"
  version="$2"
  drupal_folder="$BASE/drupal-$version"
  scheduler="$drupal_folder/sites/all/modules/video/video_scheduler.php"
  site_folder="$drupal_folder/sites/$site"

  if [ -f "$scheduler" ] && [ -e "$site" ]; then
    php $scheduler -r $drupal_folder -s $site
  fi
}

# Main procedure
if [ -z "$1" ]; then
  echo "Usage: `basename $0` <cron|deploy|update|updatedb|upgrade|run|make|video-scheduler> [arguments]"
  exit 1
elif [ "$1" == "cron" ]; then
  drupal_iterate cron
elif [ "$1" == "deploy" ]; then
  shift
  drupal_deploy $*
elif [ "$1" == "update" ]; then
  shift
  drupal_iterate pm-update $*

  # Update alone might not trigger updatedb in a farm for all instances.
  drupal_iterate updatedb $*
  drupal_iterate cache-clear all $*
elif [ "$1" == "updatedb" ]; then
  shift
  drupal_iterate updatedb $*
elif [ "$1" == "upgrade" ]; then
  shift
  drupal_upgrade $*
elif [ "$1" == "run" ]; then
  shift
  drupal_iterate $*
elif [ "$1" == "make" ]; then
  shift
  drupal_make $*
elif [ "$1" == "video-scheduler" ]; then
  shift
  drupal_video_scheduler $*
else
  echo "No action $1"
  exit 1
fi