From 0b191ef9024fddb74d57b7990941946832e02099 Mon Sep 17 00:00:00 2001 From: Chamila de Alwis Date: Wed, 5 Nov 2014 17:03:32 +0530 Subject: [PATCH] Added python cartridge agent specifics to the python agent puppet module --- .../parser/functions/difference.rb} | 31 ++++--- .../modules/python_agent/manifests/init.pp | 87 +++++++++++++------ .../python_agent/manifests/initialize.pp | 33 ++++--- .../python_agent/manifests/push_templates.pp | 6 +- .../remove_templates.pp} | 8 +- .../templates/extensions/addons/_mysql.erb | 32 ------- .../templates/extensions/addons/_nodejs.erb | 21 ----- .../templates/extensions/addons/_php.erb | 18 ---- .../templates/extensions/addons/_ruby.erb | 34 -------- 9 files changed, 105 insertions(+), 165 deletions(-) rename tools/puppet3/modules/python_agent/lib/{facter/stratos_facts.rb => puppet/parser/functions/difference.rb} (60%) mode change 100644 => 100755 rename tools/puppet3/modules/python_agent/{templates/extensions/addons/_jboss-as.erb => manifests/remove_templates.pp} (83%) mode change 100755 => 100644 delete mode 100644 tools/puppet3/modules/python_agent/templates/extensions/addons/_mysql.erb delete mode 100755 tools/puppet3/modules/python_agent/templates/extensions/addons/_nodejs.erb delete mode 100755 tools/puppet3/modules/python_agent/templates/extensions/addons/_php.erb delete mode 100644 tools/puppet3/modules/python_agent/templates/extensions/addons/_ruby.erb diff --git a/tools/puppet3/modules/python_agent/lib/facter/stratos_facts.rb b/tools/puppet3/modules/python_agent/lib/puppet/parser/functions/difference.rb old mode 100644 new mode 100755 similarity index 60% rename from tools/puppet3/modules/python_agent/lib/facter/stratos_facts.rb rename to tools/puppet3/modules/python_agent/lib/puppet/parser/functions/difference.rb index 3cab535efe..dbfc6ef550 --- a/tools/puppet3/modules/python_agent/lib/facter/stratos_facts.rb +++ b/tools/puppet3/modules/python_agent/lib/puppet/parser/functions/difference.rb @@ -5,9 +5,9 @@ # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -15,15 +15,24 @@ # specific language governing permissions and limitations # under the License. -if FileTest.exists?("/tmp/payload/launch-params") +# difference.rb +module Puppet::Parser::Functions + newfunction(:difference, :type => :rvalue +) do |arguments| - configs = File.read("/tmp/payload/launch-params").split(",").map(&:strip) + # Two arguments are required + raise(Puppet::ParseError, "difference(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size != 2 - configs.each { |x| key_value_pair = x.split("=").map(&:strip) - Facter.add("stratos_" + key_value_pair[0].to_s){ - setcode { - key_value_pair[1].to_s - } - } - } + first = arguments[0] + second = arguments[1] + + unless first.is_a?(Array) && second.is_a?(Array) + raise(Puppet::ParseError, 'difference(): Requires 2 arrays') + end + + result = first - second + + return result + end end diff --git a/tools/puppet3/modules/python_agent/manifests/init.pp b/tools/puppet3/modules/python_agent/manifests/init.pp index 4e170b686e..9beb6aeb51 100644 --- a/tools/puppet3/modules/python_agent/manifests/init.pp +++ b/tools/puppet3/modules/python_agent/manifests/init.pp @@ -16,7 +16,7 @@ # under the License. class python_agent( - $version = '1.0.0', + $version = '4.1.0', $owner = 'root', $group = 'root', $target = "/mnt", @@ -24,17 +24,18 @@ $enable_artifact_update = true, $auto_commit = false, $auto_checkout = true, + $module = 'undef', + $custom_templates = [], + $exclude_templates = [] ){ - $deployment_code = 'cartridge-agent' - #$carbon_version = $version $service_code = 'cartridge-agent' - #$carbon_home = "${target}/apache-stratos-${service_code}-${carbon_version}" - $agent_home= "${target}/${service_code}" + $agent_name = "apache-stratos-python-${service_code}-${version}" + $agent_home= "${target}/${agent_name}" tag($service_code) - $service_templates = [ + $default_templates = [ 'extensions/clean.sh', 'extensions/instance-activated.sh', 'extensions/instance-started.sh', @@ -49,49 +50,79 @@ 'extensions/mount-volumes.sh', 'extensions/subscription-domain-added.sh', 'extensions/subscription-domain-removed.sh', - ] + 'agent.conf', + 'logging.ini', + ] - python_agent::initialize { $deployment_code: + agent::initialize { $service_code: repo => $package_repo, - version => $version, - service => $service_code, + version => $carbon_version, + service => $agent_name, local_dir => $local_package_dir, target => $target, owner => $owner, } - exec { 'copy launch-params to python agent-payload': + exec { 'copy launch-params to agent_home': path => '/bin/', - command => "mkdir -p ${target}/${service_code}/payload; cp /tmp/payload/launch-params ${target}/${service_code}/payload/launch-params", - require => Py_Agent::Initialize[$deployment_code], + command => "mkdir -p ${agent_home}/payload; cp /tmp/payload/launch-params ${agent_home}/payload/launch-params", + require => Agent::Initialize[$service_code]; } exec { 'make extension folder': path => '/bin/', command => "mkdir -p ${target}/${service_code}/extensions", - require => Exec["copy launch-params to python agent-payload"], + require => Agent::Initialize[$service_code]; } - python_agent::push_templates { $service_templates: + +# excluding templates which are not needed by a cartridge module from default_templates + $default_templates_excluded = difference($default_templates,$exclude_templates) + +# excluding custom_templates, if any,(which will be overrided by a cartridge module) from default_templates + $service_templates = $module ? { + 'undef' => $default_templates_excluded, + default => difference($default_templates_excluded,$custom_templates) + } + +# applying default extensions + agent::push_templates { + $service_templates: target => $agent_home, - require => Exec["make extension folder"], + template_dir => "agent", + require => Agent::Initialize[$service_code]; + } + +# applying custom extensions + unless $module == 'undef' { + agent::push_templates { + $custom_templates: + target => $carbon_home, + template_dir => "${module}/agent", + require => [Agent::Initialize[$service_code]] + } } - file { "${target}/${service_code}/agent.conf": - ensure => file, - content => template("python_agent/agent.conf.erb"), - require => Py_Agent::Push_Templates[$service_templates], +# removing default extensions which are shipped by agent.zip + agent::remove_templates { + $exclude_templates: + target => $carbon_home, } - file { "${target}/${service_code}/logging.ini": - ensure => file, - content => template("python_agent/logging.ini.erb"), - require => File["${target}/${service_code}/agent.conf"], + $required_resources = $module ? { + 'undef' => [ + Exec['copy launch-params to agent_home'], + Agent::Push_templates[$default_templates_excluded], + ], + default =>[ + Exec['copy launch-params to agent_home'], + Agent::Push_templates[$default_templates_excluded], + Agent::Push_templates[$custom_templates] ] } - python_agent::start { $deployment_code: + agent::start { $service_code: owner => $owner, - target => "${target}/${service_code}", - require => File["${target}/${service_code}/logging.ini"], + target => $agent_home, + require => $required_resources } -} +} \ No newline at end of file diff --git a/tools/puppet3/modules/python_agent/manifests/initialize.pp b/tools/puppet3/modules/python_agent/manifests/initialize.pp index a2f2f2753d..73a2c5a641 100755 --- a/tools/puppet3/modules/python_agent/manifests/initialize.pp +++ b/tools/puppet3/modules/python_agent/manifests/initialize.pp @@ -17,7 +17,7 @@ # Initializing the deployment -define python_agent::initialize ($repo, $version, $service, $local_dir, $target, $owner,) { +define python_agent::initialize ($repo, $version, $agent_name, $local_dir, $target, $owner,) { exec { "updates": path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', @@ -52,19 +52,18 @@ path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', command => "pip install gittle", require => Exec["pip installs-psutil"]; - } - exec { 'cleanup-python-agent': - path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', - command => "rm -rf /${local_dir};rm -rf ${target}/${service};", - require => Exec["pip installs-gittle"]; + "pip installs-pexpect": + path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', + command => "pip install pexpect", + require => Exec["pip installs-gittle"]; } exec { "creating_target_for_python_${name}": path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', command => "mkdir -p ${target}", - require => Exec["cleanup-python-agent"]; + require => Exec["pip installs-pexpect"]; "creating_local_package_repo_for_python_${name}": path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/java/bin/', @@ -74,28 +73,28 @@ } file { - "/${local_dir}/${service}.zip": + "/${local_dir}/${agent_name}.zip": ensure => present, - source => ["puppet:///modules/python_agent/${service}.zip"], + source => ["puppet:///modules/python_agent/${agent_name}.zip"], require => Exec["creating_local_package_repo_for_python_${name}"], } exec { - "extracting_${service}.zip_for_${name}": + "extracting_${agent_name}.zip_for_${name}": path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', cwd => $target, - #/mnt/cartridge-agent/agent.py - unless => "test -d ${target}/${service}/agent.conf", - command => "unzip -o ${local_dir}/${service}.zip", + #/mnt/apache-stratos-python-cartridge-agent-1.0.0/agent.py + unless => "test -d ${target}/${agent_name}/agent.conf", + command => "unzip -o ${local_dir}/${agent_name}.zip", logoutput => 'on_failure', - require => File["/${local_dir}/${service}.zip"]; + require => File["/${local_dir}/${agent_name}.zip"]; "setting_permission_for_python_${name}": path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', cwd => $target, - command => "chown -R ${owner}:${owner} ${target}/${service} ; - chmod -R 755 ${target}/${service}", + command => "chown -R ${owner}:${owner} ${target}/${agent_name} ; + chmod -R 755 ${target}/${agent_name}", logoutput => 'on_failure', - require => Exec["extracting_${service}.zip_for_${name}"]; + require => Exec["extracting_${agent_name}.zip_for_${name}"]; } } diff --git a/tools/puppet3/modules/python_agent/manifests/push_templates.pp b/tools/puppet3/modules/python_agent/manifests/push_templates.pp index 94545adb20..515c30b9dd 100755 --- a/tools/puppet3/modules/python_agent/manifests/push_templates.pp +++ b/tools/puppet3/modules/python_agent/manifests/push_templates.pp @@ -17,12 +17,12 @@ # Apply the templates -define python_agent::push_templates ($target) { +define agent::push_templates ($target,$template_dir) { file { "${target}/${name}": ensure => present, owner => $agent::owner, group => $agent::group, mode => '0755', - content => template("python_agent/${name}.erb"), + content => template("${template_dir}/${name}.erb"), } -} +} \ No newline at end of file diff --git a/tools/puppet3/modules/python_agent/templates/extensions/addons/_jboss-as.erb b/tools/puppet3/modules/python_agent/manifests/remove_templates.pp old mode 100755 new mode 100644 similarity index 83% rename from tools/puppet3/modules/python_agent/templates/extensions/addons/_jboss-as.erb rename to tools/puppet3/modules/python_agent/manifests/remove_templates.pp index b6e23be641..5284e4405a --- a/tools/puppet3/modules/python_agent/templates/extensions/addons/_jboss-as.erb +++ b/tools/puppet3/modules/python_agent/manifests/remove_templates.pp @@ -15,4 +15,10 @@ # specific language governing permissions and limitations # under the License. -chown -R <%= @jboss_user %>:<%= @jboss_group %> <%= @docroot %> +# Removing default extensions shipped with agent + +define agent::remove_templates ($target) { + file { "${target}/${name}": + ensure => absent + } +} diff --git a/tools/puppet3/modules/python_agent/templates/extensions/addons/_mysql.erb b/tools/puppet3/modules/python_agent/templates/extensions/addons/_mysql.erb deleted file mode 100644 index 88f8891249..0000000000 --- a/tools/puppet3/modules/python_agent/templates/extensions/addons/_mysql.erb +++ /dev/null @@ -1,32 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -export PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin' - -PASSWD=`facter -p stratos_mysql_password` - -/etc/init.d/mysql start - -# Set mysql password -mysqladmin -uroot password "$PASSWD" - -# Remove other users -mysql -uroot -p"$PASSWD" -Bse "DELETE from mysql.user WHERE password=''" - -# Set root user with remote access -mysql -uroot -p"$PASSWD" -Bse "CREATE USER 'root'@'%' IDENTIFIED BY '${PASSWD}'" - diff --git a/tools/puppet3/modules/python_agent/templates/extensions/addons/_nodejs.erb b/tools/puppet3/modules/python_agent/templates/extensions/addons/_nodejs.erb deleted file mode 100755 index 463eb533b2..0000000000 --- a/tools/puppet3/modules/python_agent/templates/extensions/addons/_nodejs.erb +++ /dev/null @@ -1,21 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -export PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin" -NODEJS_HOME=<%= @stratos_app_path %> -npm install express -node web.js > /dev/null 2&1 & diff --git a/tools/puppet3/modules/python_agent/templates/extensions/addons/_php.erb b/tools/puppet3/modules/python_agent/templates/extensions/addons/_php.erb deleted file mode 100755 index adb80595e8..0000000000 --- a/tools/puppet3/modules/python_agent/templates/extensions/addons/_php.erb +++ /dev/null @@ -1,18 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -chown -R www-data:www-data /var/www diff --git a/tools/puppet3/modules/python_agent/templates/extensions/addons/_ruby.erb b/tools/puppet3/modules/python_agent/templates/extensions/addons/_ruby.erb deleted file mode 100644 index 9117ddeb3e..0000000000 --- a/tools/puppet3/modules/python_agent/templates/extensions/addons/_ruby.erb +++ /dev/null @@ -1,34 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -export PATH="/usr/local/rvm/gems/ruby-2.1.0/bin:/usr/local/rvm/gems/ruby-2.1.0@global/bin:/usr/local/rvm/rubies/ruby-2.1.0/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/rvm/bin:/root/bin" - -export HOME="/root/" -source ~/.bashrc -source /etc/profile.d/rvm.sh -rvm use 2.1.0 - -RUBY_HOME=<%= @stratos_app_path %> -GIT_REPO=<%= @stratos_git_repo %> - -mkdir -p $RUBY_HOME - -git clone $GIT_REPO $RUBY_HOME -cd $RUBY_HOME -bundle install -#rails server > /dev/null 2>&1 & -rails server > /tmp/ruby.log 2>&1 &