-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.sh
183 lines (163 loc) · 6.35 KB
/
main.sh
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
#!/bin/bash
############################################################
# main.sh
# written by Daniel Corrigan <[email protected]>
# This script peforms a few tasks.
#
# create new xenial, trusty, 14.04, 16.04 aptly mirrors from scratch
# update apt mirrors from remote
# create new snapshots
# switch existing pubished repos to new snapshots
# clean up old snapshots
#
##########################################################
# usage
usage() {
echo "usage: $0 updatemirrors, updatedev, updateprod, removesnapshots YYYYMMDD, initialmirrors"
exit 0
}
# require first argument
if [ -z $1 ]; then usage; fi
# date and passphrase
date=`date +"%Y%m%d"`
passphrase="ABC123"
log_file=/home/aptly/aptly.log
# get time
function get_time () {
date=`date +"%Y-%m-%d %H:%M"`
echo $date
}
# log and echo
function log_and_echo () {
current_time=$(get_time)
echo -e "$current_time - $1"
echo -e "$current_time - $1" >> ${log_file}
}
# new graph
newgraph() {
log_and_echo "Generating New Graph"
aptly graph -layout="vertical" -format="png" -output="/vol1/aptly/public/current.png" > /dev/null
}
# Keep all mirrors up to date
update_from_remote_mirrors() {
log_and_echo "Updating from remote mirrors"
for mirror in `aptly mirror list -raw`; do
aptly mirror update ${mirror} > /dev/null
done
log_and_echo "Updating from remote mirrors Complete"
}
# Create initial mirrors
create_initial_mirrors() {
log_and_echo "Creating Initial Mirrors"
distros=(trusty xenial)
for distro in ${distros[@]}; do
aptly mirror create -architectures="amd64" ${distro}-main http://us.archive.ubuntu.com/ubuntu/ ${distro} main restricted universe multiverse > /dev/null
aptly mirror create -architectures="amd64" ${distro}-updates http://us.archive.ubuntu.com/ubuntu/ ${distro}-updates main restricted universe multiverse > /dev/null
aptly mirror create -architectures="amd64" ${distro}-security http://security.ubuntu.com/ubuntu/ ${distro}-security main restricted universe multiverse > /dev/null
aptly mirror create -architectures="amd64" ${distro}-backports http://us.archive.ubuntu.com/ubuntu/ ${distro}-backports main restricted universe multiverse > /dev/null
done
log_and_echo "Initial Mirrors Complete"
}
# remove snapshots
remove_snapshots() {
snapshot_date=$1
log_and_echo "Removing snapshots dated $1"
distros=(trusty xenial)
for distro in ${distros[@]}; do
# drop final and included snapshots
aptly snapshot drop ${distro}-final-${snapshot_date} > /dev/null
aptly snapshot drop ${distro}-main-${snapshot_date} > /dev/null
aptly snapshot drop ${distro}-updates-${snapshot_date} > /dev/null
aptly snapshot drop ${distro}-security-${snapshot_date} > /dev/null
aptly snapshot drop ${distro}-backports-${snapshot_date} > /dev/null
done
log_and_echo "Removal of snapshots Complete"
}
# Create current date main distro snapshots
update_dev() {
new_or_existing=$1
log_and_echo "Updating DEV - $new_or_existing"
distros=(trusty xenial)
for distro in ${distros[@]}; do
# create todays snapshots
log_and_echo "Creating ${date} snapshots for ${distro}"
aptly snapshot create ${distro}-main-${date} from mirror ${distro}-main > /dev/null
aptly snapshot create ${distro}-updates-${date} from mirror ${distro}-updates > /dev/null
aptly snapshot create ${distro}-security-${date} from mirror ${distro}-security > /dev/null
aptly snapshot create ${distro}-backports-${date} from mirror ${distro}-backports > /dev/null
# merge todays snapshots into common "final" repo
log_and_echo "Merging Snapshots for ${distro}"
aptly snapshot merge -latest ${distro}-final-${date} ${distro}-main-${date} ${distro}-updates-${date} ${distro}-security-${date} ${distro}-backports-${date} > /dev/null
if [[ $new_or_existing == "existing" ]]; then
# switch published repos to new snapshot
log_and_echo "Publish Switching DEV ${distro} to new snapshot ${distro}-final-${date}"
aptly publish switch -passphrase="${passphrase}" ${distro} dev ${distro}-final-${date} > /dev/null
log_and_echo "Publish Switching DEV ${distro} to new snapshot ${distro}-final-${date} Complete"
elif [[ $new_or_existing == "new" ]]; then
# create new published repo
log_and_echo "Publishing DEV ${distro} to new snapshot ${distro}-final-${date}"
aptly publish snapshot -passphrase="${passphrase}" -distribution="${distro}" ${distro}-final-${date} dev > /dev/null
log_and_echo "Publishing $DEV {distro} to new snapshot ${distro}-final-${date} Complete"
else
exit 1
fi
done
log_and_echo "Updating DEV - $new_or_existing Complete"
}
# switch published prod to current dev snapshot
update_prod() {
dev_current_publish_date=`aptly publish list|grep dev|egrep -o "xenial-final-[0-9]{8}"|awk -F"-" '{print $3}'`
distros=(trusty xenial)
log_and_echo "Publish Switching PROD ${distro} to DEV snapshot ${distro}-final-${dev_current_publish_date}"
for distro in ${distros[@]}; do
aptly publish switch -passphrase="${passphrase}" ${distro} prod ${distro}-final-${dev_current_publish_date} > /dev/null
done
log_and_echo "Publish Switching PROD ${distro} to DEV snapshot ${distro}-final-${dev_current_publish_date} Complete"
}
# script begins
command=$1
argument=$2
case $command in
initialmirrors)
doit() {
create_initial_mirrors
update_from_remote_mirrors
update_dev new
update_prod
newgraph
}
if [[ $argument == "FORCE" ]]; then
doit
else
echo -e "WARNING: This will build a completely from blank disk repo for xenial and trusty and 14.04 and 16.04.\nThis process will take over a day.\nYou can skip the this prompt by running: $0 initialmirrors FORCE"
echo -n "You must type FORCE to run this command now: "
read argument
if [[ "$argument" == "FORCE" ]]; then
doit
else
echo "You must type FORCE"
exit 1
fi
fi
;;
updatedev)
update_from_remote_mirrors
update_dev existing
newgraph
;;
updateprod)
update_prod
newgraph
;;
updatemirrors)
update_from_remote_mirrors
;;
removesnapshots)
if [ -z $arugement ]; then echo "usage: $0 removesnaphots YYYYMMDD"; exit 0; fi
remove_snapshots $argument
newgraph
;;
*)
usage
;;
esac