|
| 1 | +# |
| 2 | +# Put your custom functions in this class in order to keep the files under lib untainted |
| 3 | +# |
| 4 | +# This class has access to all of the private variables in deploy/lib/server_config.rb |
| 5 | +# |
| 6 | +# any public method you create here can be called from the command line. See |
| 7 | +# the examples below for more information. |
| 8 | +# |
| 9 | +class ServerConfig |
| 10 | + |
| 11 | + # |
| 12 | + # You can easily "override" existing methods with your own implementations. |
| 13 | + # In ruby this is called monkey patching |
| 14 | + # |
| 15 | + # first you would rename the original method |
| 16 | + # alias_method :original_deploy_modules, :deploy_modules |
| 17 | + |
| 18 | + # then you would define your new method |
| 19 | + # def deploy_modules |
| 20 | + # # do your stuff here |
| 21 | + # # ... |
| 22 | + |
| 23 | + # # you can optionally call the original |
| 24 | + # original_deploy_modules |
| 25 | + # end |
| 26 | + |
| 27 | + # |
| 28 | + # you can define your own methods and call them from the command line |
| 29 | + # just like other roxy commands |
| 30 | + # ml local my_custom_method |
| 31 | + # |
| 32 | + # def my_custom_method() |
| 33 | + # # since we are monkey patching we have access to the private methods |
| 34 | + # # in ServerConfig |
| 35 | + # @logger.info(@properties["ml.content-db"]) |
| 36 | + # end |
| 37 | + |
| 38 | + # |
| 39 | + # to create a method that doesn't require an environment (local, prod, etc) |
| 40 | + # you woudl define a class method |
| 41 | + # ml my_static_method |
| 42 | + # |
| 43 | + # def self.my_static_method() |
| 44 | + # # This method is static and thus cannot access private variables |
| 45 | + # # but it can be called without an environment |
| 46 | + # end |
| 47 | + |
| 48 | + # Show-casing some useful overrides, as well as adjusting some module doc permissions |
| 49 | + alias_method :original_deploy_modules, :deploy_modules |
| 50 | + alias_method :original_deploy_rest, :deploy_rest |
| 51 | + alias_method :original_deploy, :deploy |
| 52 | + alias_method :original_clean, :clean |
| 53 | + |
| 54 | + # Integrate deploy_packages into the Roxy deploy command |
| 55 | + def deploy |
| 56 | + what = ARGV.shift |
| 57 | + |
| 58 | + case what |
| 59 | + when 'packages' |
| 60 | + deploy_packages |
| 61 | + else |
| 62 | + ARGV.unshift what |
| 63 | + original_deploy |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + def deploy_modules |
| 68 | + # Uncomment deploy_packages if you would like to use MLPM to deploy MLPM packages, and |
| 69 | + # include MLPM deploy in deploy modules to make sure MLPM depencencies are loaded first. |
| 70 | + |
| 71 | + # Note: you can also move mlpm.json into src/ext/ and deploy plain modules (not REST extensions) that way. |
| 72 | + |
| 73 | + #deploy_packages |
| 74 | + original_deploy_modules |
| 75 | + end |
| 76 | + |
| 77 | + def deploy_packages |
| 78 | + password_prompt |
| 79 | + system %Q!mlpm deploy -u #{ @ml_username } \ |
| 80 | + -p #{ @ml_password } \ |
| 81 | + -H #{ @properties['ml.server'] } \ |
| 82 | + -P #{ @properties['ml.app-port'] }! |
| 83 | + change_permissions(@properties["ml.modules-db"]) |
| 84 | + end |
| 85 | + |
| 86 | + def deploy_rest |
| 87 | + original_deploy_rest |
| 88 | + change_permissions(@properties["ml.modules-db"]) |
| 89 | + end |
| 90 | + |
| 91 | + # Permissions need to be changed for executable code that was not deployed via Roxy directly, |
| 92 | + # to make sure users with app-role can read and execute it. Typically applies to artifacts |
| 93 | + # installed via REST api, which only applies permissions for rest roles. Effectively also includes |
| 94 | + # MLPM, which uses REST api for deployment. It often also applies to artifacts installed with |
| 95 | + # custom code (via app_specific for instance), like alerts. |
| 96 | + def change_permissions(where) |
| 97 | + logger.info "Changing permissions in #{where} for:" |
| 98 | + r = execute_query( |
| 99 | + %Q{ |
| 100 | + xquery version "1.0-ml"; |
| 101 | +
|
| 102 | + let $new-permissions := ( |
| 103 | + xdmp:permission("#{@properties["ml.app-name"]}-role", "read"), |
| 104 | + xdmp:permission("#{@properties["ml.app-name"]}-role", "update"), |
| 105 | + xdmp:permission("#{@properties["ml.app-name"]}-role", "execute") |
| 106 | + ) |
| 107 | +
|
| 108 | + let $uris := |
| 109 | + if (fn:contains(xdmp:database-name(xdmp:database()), "content")) then |
| 110 | +
|
| 111 | + (: This is to make sure all alert files are accessible :) |
| 112 | + cts:uri-match("*alert*") |
| 113 | +
|
| 114 | + else |
| 115 | +
|
| 116 | + (: This is to make sure all triggers, schemas, modules and REST extensions are accessible :) |
| 117 | + cts:uris() |
| 118 | +
|
| 119 | + let $fixes := |
| 120 | + for $uri in $uris |
| 121 | + let $existing-permissions := xdmp:document-get-permissions($uri) |
| 122 | + |
| 123 | + (: Only apply new permissions if really necessary (gives better logging too):) |
| 124 | + where not(ends-with($uri, "/")) |
| 125 | + and count($existing-permissions[fn:string(.) = $new-permissions/fn:string(.)]) ne 3 |
| 126 | + |
| 127 | + return ( |
| 128 | + " " || $uri, |
| 129 | + xdmp:document-set-permissions($uri, $new-permissions) |
| 130 | + ) |
| 131 | + return |
| 132 | + if ($fixes) then |
| 133 | + $fixes |
| 134 | + else |
| 135 | + " no changes needed.." |
| 136 | + }, |
| 137 | + { :db_name => where } |
| 138 | + ) |
| 139 | + r.body = parse_json r.body |
| 140 | + logger.info r.body |
| 141 | + logger.info "" |
| 142 | + end |
| 143 | + |
| 144 | + # Integrate clean_collections into the Roxy clean command |
| 145 | + def clean |
| 146 | + what = ARGV.shift |
| 147 | + |
| 148 | + case what |
| 149 | + when 'collections' |
| 150 | + clean_collections |
| 151 | + else |
| 152 | + ARGV.unshift what |
| 153 | + original_clean |
| 154 | + end |
| 155 | + end |
| 156 | + |
| 157 | + def clean_collections() |
| 158 | + what = ARGV.shift |
| 159 | + r = execute_query( |
| 160 | + %Q{ |
| 161 | + xquery version "1.0-ml"; |
| 162 | +
|
| 163 | + for $collection in fn:tokenize("#{what}", ",") |
| 164 | + where fn:exists(fn:collection($collection)[1]) |
| 165 | + return ( |
| 166 | + xdmp:collection-delete($collection), |
| 167 | + "Cleaned collection " || $collection |
| 168 | + ) |
| 169 | + }, |
| 170 | + { :db_name => @properties["ml.content-db"]} |
| 171 | + ) |
| 172 | + r.body = parse_json r.body |
| 173 | + logger.info r.body |
| 174 | + end |
| 175 | + |
| 176 | +end |
| 177 | + |
| 178 | +# |
| 179 | +# Uncomment, and adjust below code to get help about your app_specific |
| 180 | +# commands included into Roxy help. (ml -h) |
| 181 | +# |
| 182 | + |
| 183 | +class Help |
| 184 | +# def self.app_specific |
| 185 | +# <<-DOC.strip_heredoc |
| 186 | +# |
| 187 | +# App-specific commands: |
| 188 | +# example Installs app-specific alerting |
| 189 | +# DOC |
| 190 | +# end |
| 191 | +# |
| 192 | +# def self.example |
| 193 | +# <<-DOC.strip_heredoc |
| 194 | +# Usage: ml {env} example [args] [options] |
| 195 | +# |
| 196 | +# Runs a special example task against given environment. |
| 197 | +# |
| 198 | +# Arguments: |
| 199 | +# this Do this |
| 200 | +# that Do that |
| 201 | +# |
| 202 | +# Options: |
| 203 | +# --whatever=value |
| 204 | +# DOC |
| 205 | +# end |
| 206 | + class <<self |
| 207 | + alias_method :original_deploy, :deploy |
| 208 | + |
| 209 | + def deploy |
| 210 | + # Concatenate extra lines of documentation after original deploy |
| 211 | + # Help message (with a bit of indent to make it look better) |
| 212 | + original_deploy + " " + |
| 213 | + <<-DOC.strip_heredoc |
| 214 | + packages # deploys MLPM modules and REST extensions using MLPM to the app-port |
| 215 | + DOC |
| 216 | + end |
| 217 | + alias_method :original_clean, :clean |
| 218 | + |
| 219 | + def clean |
| 220 | + # Concatenate extra lines of documentation after original clean |
| 221 | + # Help message (with a bit of indent to make it look better) |
| 222 | + original_clean + "\n " + |
| 223 | + <<-DOC.strip_heredoc |
| 224 | + collections WHAT |
| 225 | + # removes all files from (comma-separated list of) WHAT collection(s) in the content database |
| 226 | + DOC |
| 227 | + end |
| 228 | + end |
| 229 | +end |
0 commit comments