~kwmonroe/charms/trusty/ibm-java/may-2016

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
#!/bin/bash
source charms.reactive.sh
set -e

ARCHIVE_DIR=$CHARM_DIR/files/archives

HOST=`config-get host`
PACKAGE_DIR=`config-get package_dir`
USERNAME=`config-get username`
PASSWORD=`config-get password`

# Fail fast if we're not on a supported arch. Also set defaults for filename
# and sha1sum based on arch.
ARCHITECTURE=`uname -m`
DEFAULT_INSTALLER_FILE_SDK=""
DEFAULT_INSTALLER_SDK_SHA=""
if [ "$ARCHITECTURE" = "x86_64" ]; then
	DEFAULT_INSTALLER_FILE_SDK="ibm-java-x86_64-sdk-8.0-2.0.bin"
	DEFAULT_INSTALLER_SDK_SHA="71b3bf11ac6e04d8d697cca80ea00ab841f3ea57"
elif [ "$ARCHITECTURE" = "ppc64le" ]; then
	DEFAULT_INSTALLER_FILE_SDK="ibm-java-ppc64le-sdk-8.0-2.0.bin"
	DEFAULT_INSTALLER_SDK_SHA="73a12fd8491d8d18d2ad809a77d8bfde747cb899"
else
	status-set blocked "IBM Java: Platform not supported"
fi


# Ensure we have a target installation directory
JAVA_INSTALL_PATH=$(config-get java_path_name)

# Check whether IBM Java is installed
function is_javasdk_installed()
{
	if  [ -f "$JAVA_INSTALL_PATH/bin/javac" ]; then
		echo "True"
	else
		echo "False"
	fi
}

function is_jre_installed()
{
	if  [ -f "$JAVA_INSTALL_PATH/jre/bin/java" ]; then
		echo "True"
	else
		echo "False"
	fi
}


# Remove IBM Java, if installed. This function doesn't log or set status
# when it runs because we have different reasons for removing this software.
# Be sure to set appropriate log and status messages before and after this
# function is called.
function remove_java_software()
{
	javasdk_installed=`is_javasdk_installed`
	jre_installed=`is_jre_installed`
	if [ -d "$JAVA_INSTALL_PATH" ]; then
		$JAVA_INSTALL_PATH/_uninstall/uninstall
		rm -rf $JAVA_INSTALL_PATH
		if  [ $javasdk_installed == "True" ]; then
			sed --in-place "s|$JAVA_INSTALL_PATH/bin:||" /etc/profile
			update-alternatives --remove "javac" "$JAVA_INSTALL_PATH/bin/javac"
		if [ $jre_installed == "True" ]; then
			sed --in-place "s|$JAVA_INSTALL_PATH/jre/bin:||" /etc/profile
			update-alternatives --remove "java" "$JAVA_INSTALL_PATH/jre/bin/java"
		fi
	else
		juju-log "JAVA_INSTALL_PATH not found; could not remove java software"
	fi
}


#Downloading IBM Java Package
function download_java()
{
	#Exit if URL to download package is null
	if [ "$USERNAME" == "" -o "$PASSWORD" == "" -o "$HOST" == "" -o "$PACKAGE_DIR" == "" ]; then
		status-set blocked "IBM Java: Failed to download files from repository. Please provide values for host,package_dir,username and password in config.yaml"
		exit 0
	fi

	# If an install was done, get the name of the package from which it was done
	if [ -f  $ARCHIVE_DIR/*.bin ]; then
		pkg_name=`ls $ARCHIVE_DIR/*.bin`
		if [ $? == 0 ]; then
			pkg_name=`basename $pkg_name`
		fi
	fi

	# Get the package name to check whether the user wants to install a different package
	cfg_pkg_name=`config-get java_file_name`

	# Set package name to a predefined value if the user has not provided a package name
	if [ "$cfg_pkg_name" == "" ]; then
		cfg_pkg_name=${DEFAULT_INSTALLER_FILE_SDK}
	fi

	# The user has configured a different package name
	if  [ "$pkg_name" != "$cfg_pkg_name" ]; then
		#Download the new IBM Java package
		status-set maintenance "Downloading IBM Java installer"
		$CHARM_DIR/files/sftp_get.sh $USERNAME $PASSWORD $HOST $PACKAGE_DIR/$cfg_pkg_name $ARCHIVE_DIR
		if [ $? -eq 0 ]; then
			status-set maintenance "IBM Java: Downloaded IBM Java package."
			chmod -R 777 $ARCHIVE_DIR/*
			# Remove previously downloaded and installed IBM Java software
			if [ "$pkg_name" != "" ]; then
				rm  $ARCHIVE_DIR/$pkg_name
				status-set maintenance "Removing older IBM Java (if installed)"
				remove_java_software
			fi
		else
			status-set blocked "IBM Java:Download failed. Ensure the download URL and package name are correctly set in the charm configuration."
			exit 0
		fi
	fi

	# Check integrity of downloaded package
	SHA=`config-get sha`
	if [ "$SHA" == "" ]; then
		SHA=${DEFAULT_INSTALLER_SDK_SHA}
	fi
	if [ "$SHA" != "`sha1sum $ARCHIVE_DIR/$cfg_pkg_name | cut -d" " -f1`" ]; then
		status-set blocked "SHA mismatch; set 'sha' to the sha1sum of ${cfg_pkg_name}"
		exit 0
	fi
}


# Check Environment Variables for IBM Java
function check_javapath()
{
	if grep -q "$JAVA_INSTALL_PATH" /etc/profile
	then
		echo "True"
	else
		echo "False"
	fi
}


javasdk_license_accepted=`config-get accept-ibm-java-license`
if [ $javasdk_license_accepted == "True" ]; then
	set_state 'license.accepted'
else
	remove_state 'license.accepted'
	status-set blocked "Set 'accept-ibm-java-license' to 'True' to install IBM Java"
fi


@hook 'install'
function init() {
	# Install expect tool
	apt-get install -y expect
}


@when 'java.connected' 'license.accepted'
@when_not 'java.installed'
function install() {
	if [[ ! -d $ARCHIVE_DIR ]]; then
		mkdir $ARCHIVE_DIR
	fi
	cd $ARCHIVE_DIR
	echo -e "LICENSE_ACCEPTED=TRUE\nUSER_INSTALL_DIR=$JAVA_INSTALL_PATH\nINSTALLER_UI=silent" > installer.properties
	download_java
	status-set maintenance "Installing IBM Java"
	$ARCHIVE_DIR/$cfg_pkg_name -i silent -f $ARCHIVE_DIR/installer.properties
	set_java_path=`check_javapath`
	if [ $set_java_path == "False" ]; then
		cd /etc
		echo "export PATH=$JAVA_INSTALL_PATH/bin:$JAVA_INSTALL_PATH/jre/bin:$PATH" >> profile
		source /etc/profile
	else
		source /etc/profile
	fi

	update-alternatives --install "/usr/bin/java" "java" "$JAVA_INSTALL_PATH/jre/bin/java" 1
	update-alternatives --set "java" "$JAVA_INSTALL_PATH/jre/bin/java"
	if [ $javasdk_installed == "True" ]; then
		update-alternatives --install "/usr/bin/javac" "javac" "$JAVA_INSTALL_PATH/bin/javac" 1
		update-alternatives --set "javac" "$JAVA_INSTALL_PATH/bin/javac"
	fi

	java -version
	if [ $? -eq 0 ]; then
		# Send relation data
		java_home=$(readlink -f /usr/bin/java | sed "s:/bin/java::")
		java_version=$(java -version 2>&1 | grep -i version | head -1 | awk -F '"' {'print $2'})
		relation_call --state=java.connected set_ready $java_home $java_version

		set_state 'java.installed'
		if [ $javasdk_installed == "True" ]; then
			status-set active "Ready: IBM Java SDK $java_version"
		else
			status-set active "Ready: IBM Java Runtime Environment $java_version"
		fi
	else
		update-alternatives --remove "java" "$JAVA_INSTALL_PATH/jre/bin/java"
		if [ $javasdk_installed == "True" ]; then
			update-alternatives --remove "javac" "$JAVA_INSTALL_PATH/bin/javac"
		fi
		status-set blocked "IBM Java: Error while installing IBM Java"
	fi
}

@when 'java.connected' 'java.installed' 'license.accepted'
@when 'config.changed'
function check_version() {
	if charms.reactive is_state 'config.changed.java_path_name' || \
           charms.reactive is_state 'config.changed.java_file_name' || \
           charms.reactive is_state 'config.changed.sha'; then
		status-set maintenance "Uninstalling all IBM Java versions before installing new one"
		remove_java_software
		install
	fi
}

@when 'java.installed'
@when_not 'java.connected'
function uninstall() {
	# Uninstall all versions of IBM Java
	status-set maintenance "Uninstalling all IBM Java versions"
	remove_java_software
	remove_state 'java.installed'
	status-set blocked "IBM Java (all versions) uninstalled"
}

@when 'java.connected' 'java.installed'
@when_not 'license.accepted'
function unaccepted_license() {
	# If we're connected and installed, but the user has unaccepted the
	# license, remove the software and disable java-ready.
	uninstall
	relation_call --state=java.connected unset_ready
}

reactive_handler_main