forked from dennyzhang/devops_public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package_helper.sh
executable file
·121 lines (108 loc) · 3.82 KB
/
package_helper.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
#!/bin/bash -e
##-------------------------------------------------------------------
## @copyright 2016 DennyZhang.com
## Licensed under MIT
## https://raw.githubusercontent.com/DennyZhang/devops_public/tag_v1/LICENSE
##
## File : package_helper.sh
## Author : Denny <[email protected]>
## Description :
## --
## Created : <2016-01-08>
## Updated: Time-stamp: <2017-09-04 18:54:41>
##-------------------------------------------------------------------
function install_package() {
local package=${1?}
local binary_name=${2:-""}
[ -n "$binary_name" ] || binary_name="$package"
# TODO: support more OS
fail_unless_os "ubuntu"
if ! which "$binary_name" 1>/dev/null 2>&1; then
apt-get install -y "$package"
fi
}
function install_package_list() {
# install_package_list "wget,curl,git"
local package_list=${1?}
for package in ${package_list//,/ }; do
install_package "$package"
done
}
function ssh_apt_update() {
set +e
# Sample:
# ssh_apt_update "ssh -i $ssh_key_file -p $ssh_port -o StrictHostKeyChecking=no root@$ssh_server_ip"
local ssh_command=${1?}
echo "Run apt-get -y update"
apt_get_output=$($ssh_command apt-get -y update)
if echo "$apt_get_output" | "Hash Sum mismatch" 1>/dev/null 2>&1; then
echo "apt-get update fail with complain of 'Hash Sum mismatch'"
echo "rm -rf /var/lib/apt/lists/*"
$ssh_command "rm -rf /var/lib/apt/lists/*"
echo "Re-run apt-get -y update"
$ssh_command "apt-get -y update"
fi
# TODO: unset -e without changing previous state
set -e
}
function update_system() {
local os_release_name
os_release_name=$(os_release)
if [ "$os_release_name" == "ubuntu" ]; then
log "apt-get -y update"
rm -rf /var/lib/apt/lists/*
apt-get -y update
fi
if [ "$os_release_name" == "redhat" ] || [ "$os_release_name" == "centos" ]; then
yum -y update
fi
}
function install_chef() {
local chef_version=${1:-"12.4.1"}
if ! which chef-client 1>/dev/null 2>&1; then
export version="$chef_version"
wget -O /tmp/install.sh https://www.opscode.com/chef/install.sh
bash -xe /tmp/install.sh
fi
}
function install_ruby() {
local ruby_version=${1:-"2.1.8"}
echo "TODO: ruby_version: ${ruby_version}"
# apt-get -yqq install python-software-properties && \
# apt-add-repository ppa:brightbox/ruby-ng && \
# apt-get -yqq update && \
# apt-get -yqq install ruby2.1 ruby2.1-dev && \
# rm -rf /usr/bin/ruby && \
# ln -s /usr/bin/ruby2.1 /usr/bin/ruby && \
# rm -rf /usr/local/bin/ruby /usr/local/bin/gem /usr/local/bin/bundle
}
function ubuntu_parse_package_list() {
# parse output of "dpkg -l", to get package name and package version
local package_list=${1?}
package_list=$(echo "$package_list" | grep "^ii " | awk -F' ' '{print $2": "$3}')
echo "$package_list"
}
function get_default_package_list() {
# Related Link: https://github.com/DennyZhang/devops_public/tree/2016-06-16/os_preinstalled_packages
local os_version=${1?}
local package_file=${2:-""}
local tag_name=${3:-"2016-06-16"}
# TODO: don't hardcode download link
package_prefix="https://github.com/DennyZhang/devops_public/raw/${tag_name}/os_preinstalled_packages"
[ -n "$package_file" ] || package_file="/tmp/${os_version}.txt"
case "$os_version" in
ubuntu-14.04)
package_link="${package_prefix}/${os_version}.txt"
if [ ! -f "$package_file" ]; then
command="wget -O $package_file $package_link"
eval "$command"
fi
;;
*)
echo "ERROR: Not supported OS: $os_version"
exit 1
;;
esac
}
######################################################################
## File : package_helper.sh ends