forked from Jarli01/xenorchestra_updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxo-update.sh
195 lines (158 loc) · 4.37 KB
/
xo-update.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
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/bash
#Check Git email
gitemail=$(git config --global user.email)
if [ -z "$gitemail" ]; then
echo "Git email required to run XOCE updater";
echo "enter your credentials with the following commands and then rerun this update script"
echo "git config --global user.email \"you@example.com\""
exit 1;
fi
#Check Git name
gituser=$(git config --global user.name)
if [ -z "$gituser" ]; then
echo "Git name required to run XOCE updater";
echo "enter your credentials with the following commands and then rerun this update script"
echo "git config --global user.name \"Your Name\""
exit 1;
fi
updateFromSource ()
{
UPDATE=false
echo Current branch $(git rev-parse --abbrev-ref HEAD)
GetVersions
echo Current version $XOS_VER / $XOW_VER
sleep 5s
if [ "$BRANCH" != "" ]; then
echo "Switching to branch '$BRANCH'..."
sudo git diff-index --quiet HEAD -- || git stash -u && git stash drop
sudo git checkout $BRANCH
fi
sudo git fetch origin -q
REMOTE=$(git rev-parse @{u})
REVISIONS=$( sudo git rev-list HEAD...$REMOTE --count )
echo $REVISIONS updates available
if [ $REVISIONS -ne 0 ] || [ "$FORCE" = true ]; then
UPDATE=true
echo "Updating from source..."
sudo git diff-index --quiet HEAD -- || git stash -u && git stash drop
sudo git pull
echo "Clearing directories..."
sudo rm -rf dist
fi
}
installUpdates()
{
echo "Installing..."
yarn
yarn build
GetVersions
echo Updated version $XOS_VER / $XOW_VER
}
installPlugins()
{
echo "Checking plugins..."
# symlink any missing plugins
dest=/usr/local/lib/node_modules/
for source in =$(ls -d /opt/xen-orchestra/packages/xo-server-*); do
plugin=$(basename $source)
if [ ! -L $dest$plugin ]; then
echo "Creating link for $plugin"
ln -s "$source" "$dest"
fi
done
}
main() {
if [ "$EUID" -ne 0 ]; then
echo "Please run as root (sudo bash)"
exit
fi
while getopts b:fn: opt; do
case $opt in
b)
BRANCH="$OPTARG"
FORCE=true
if [ "$BRANCH" == "" ]; then
BRANCH="stable"
fi;;
f) FORCE=true;;
n)
NODE=true
FORCE=true
VERSION="$OPTARG"
if [ "$VERSION" == "" ]; then
VERSION="lts"
fi;;
esac
done
echo "Stopping xo-server..."
ISACTIVE=$(systemctl is-active xo-server)
if [ "$ISACTIVE" == "active" ]; then
sudo systemctl stop xo-server
else
sudo pkill -f "/bin/xo-server"
fi
if [ "$NODE" = true ]; then
echo "Updating Node.js to '$VERSION' version..."
sudo n "$VERSION"
fi
updateYarn
changeRepos
UPDATE=""
echo "Checking xen-orchestra..."
cd /opt/xen-orchestra
updateFromSource
if [ "$UPDATE" = true ]; then
sed -i 's/< 5/> 0/g' /opt/xen-orchestra/packages/xo-web/src/xo-app/settings/config/index.js
sed -i 's/< 5/> 0/g' /opt/xen-orchestra/packages/xo-web/src/xo-app/xosan/index.js
installUpdates
installPlugins
fi
sleep 5s
if [ "$ISACTIVE" == "active" ]; then
echo "Restarting xo-server..."
sudo systemctl start xo-server
else
echo "Please manually restart xo-server"
fi
}
updateYarn()
{
echo "Checking for Yarn package..."
if [ $(dpkg-query -W -f='${Status}' yarn 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
echo "Installing Yarn..."
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
else
echo "Checking for Yarn update..."
fi
sudo apt-get update > /dev/null
sudo apt-get install --yes yarn
}
changeRepos()
{
echo "Checking for Repo change..."
if [ -d "/opt/xo-server" ]; then
cd /opt
/usr/bin/git clone -b master https://github.com/vatesfr/xen-orchestra
cp /opt/xo-server/.xo-server.yaml /opt/xen-orchestra/packages/xo-server/.xo-server.yaml
mv xo-server xo-server.old
sed -i 's:/opt/xo-server/:/opt/xen-orchestra/packages/xo-server/:g' /lib/systemd/system/xo-server.service
systemctl daemon-reload
FORCE=true
fi
if [ -d "/opt/xo-web" ]; then
mv xo-web xo-web.old
sed -i 's:/opt/xo-web/dist:/opt/xen-orchestra/packages/xo-web/dist:g' /opt/xen-orchestra/packages/xo-server/.xo-server.yaml
FORCE=true
fi
}
GetVersions()
{
if [ -f "/opt/xen-orchestra/packages/xo-server/package.json" ]; then
XOS_VER=$(node -pe "require('/opt/xen-orchestra/packages/xo-server/package.json').version")
fi
if [ -f "/opt/xen-orchestra/packages/xo-web/package.json" ]; then
XOW_VER=$(node -pe "require('/opt/xen-orchestra/packages/xo-web/package.json').version")
fi
}
main "$@"