#!/bin/bash

set -xe

# THIS IS STUPID AND MAYBE A LITTLE EVIL!!
# IT IS A TEMPORARY HACK ONLY UNTIL WE GET THESE FIXES UPSTREAM!!

patch -p0 <<'PATCH'
--- /opt/rh/ruby193/root/usr/libexec/mcollective/mcollective/agent/openshift.ddl
+++ /opt/rh/ruby193/root/usr/libexec/mcollective/mcollective/agent/openshift.ddl	2013-11-15 08:14:15.964000018 -0700
@@ -477,6 +477,15 @@
         :optional       => false,
         :maxlength      => 32
 
+    input :secret_token,
+        :prompt         => "Application secret token",
+        :description    => "Application secret token",
+        :type           => :string,
+        :validation     => '^[\w\-]+$',
+        :optional       => false,
+        :minlength      => 128,
+        :maxlength      => 128
+
     input :namespace,
         :prompt         => "Namespace",
         :description    => "Namespace",
@@ -500,6 +509,22 @@
         :optional       => false,
         :list           => ["true", "false"]
 
+    input :auth_token,
+        :prompt         => "Broker auth token",
+        :description    => "Broker auth token",
+        :type           => :string,
+        :validation     => '^.+$',
+        :optional       => true,
+        :maxlength      => 128
+
+    input :auth_iv,
+        :prompt         => "Broker auth iv",
+        :description    => "Broker auth iv",
+        :type           => :string,
+        :validation     => '^.+$',
+        :optional       => true,
+        :maxlength      => 512
+
     output  :time,
             :description => "The time as a message",
             :display_as => "Time"
--- -	2013-11-15 08:40:01.032651778 -0700
+++ /opt/rh/ruby193/root/usr/libexec/mcollective/mcollective/agent/openshift.rb	2013-11-15 08:14:15.951000018 -0700
@@ -219,6 +219,9 @@
         validate :namespace, /^.+$/
         uuid = request[:uuid]
         application_uuid = request[:app_uuid]
+        secret_token = request[:secret_token]
+        auth_iv = request[:auth_iv]
+        auth_token = request[:auth_token]
         namespace = request[:namespace]
         version = request[:version]
         ignore_cartridge_version = request[:ignore_cartridge_version] == 'true' ? true : false
@@ -229,7 +232,7 @@
 
         begin
           require 'openshift-origin-node/model/upgrade'
-          upgrader = OpenShift::Runtime::Upgrader.new(uuid, application_uuid, namespace, version, hostname, ignore_cartridge_version, OpenShift::Runtime::Utils::Hourglass.new(235))
+          upgrader = OpenShift::Runtime::Upgrader.new(uuid, application_uuid, secret_token, auth_iv, auth_token, namespace, version, hostname, ignore_cartridge_version, OpenShift::Runtime::Utils::Hourglass.new(235))
         rescue Exception => e
           report_exception e
           exitcode = 1
--- -	2013-11-15 08:40:06.249310478 -0700
+++ /opt/rh/ruby193/root/usr/share/gems/gems/openshift-origin-node-1.17.0/lib/openshift-origin-node/model/upgrade.rb	2013-11-15 08:14:15.937000018 -0700
@@ -72,9 +72,12 @@
                   :ignore_cartridge_version, :gear_home, :gear_env, :progress, :container, 
                   :gear_extension, :config, :hourglass
 
-      def initialize(uuid, application_uuid, namespace, version, hostname, ignore_cartridge_version, hourglass = nil)
+      def initialize(uuid, application_uuid, secret_token, auth_iv, auth_token, namespace, version, hostname, ignore_cartridge_version, hourglass = nil)
         @uuid = uuid
         @application_uuid = application_uuid
+        @secret_token = secret_token
+        @auth_iv = auth_iv
+        @auth_token = auth_token
         @namespace = namespace
         @version = version
         @hostname = hostname
PATCH

patch -p0 <<'PATCH'
--- /opt/rh/ruby193/root/usr/share/gems/gems/openshift-origin-node-1.17.0/lib/openshift-origin-node/model/upgrade.rb	2013-11-15 14:43:08.853000006 -0700
+++ /opt/rh/ruby193/root/usr/share/gems/gems/openshift-origin-node-1.17.0/lib/openshift-origin-node/model/upgrade.rb	2013-11-15 16:17:11.818482130 -0700
@@ -625,7 +625,7 @@
 
         progress.step 'stop_gear' do |context, errors|
           begin
-            container.stop_gear(user_initiated: false)
+            container.stop_gear(user_initiated: false, init: true)
           rescue Exception => e
             msg = "Stop gear failed with an exception: #{e.message}"
             progress.log msg
PATCH
